area.effect 3.6 KB
Newer Older
J
James Park 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
uniform float4x4 ViewProj;
uniform float4x4 color_matrix;
uniform float3 color_range_min = {0.0, 0.0, 0.0};
uniform float3 color_range_max = {1.0, 1.0, 1.0};
uniform float2 base_dimension_i;
uniform texture2d image;

sampler_state def_sampler {
	Filter   = Linear;
	AddressU = Clamp;
	AddressV = Clamp;
};

struct VertInOut {
	float4 pos : POSITION;
	float2 uv  : TEXCOORD0;
};

VertInOut VSDefault(VertInOut vert_in)
{
	VertInOut vert_out;
	vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
	vert_out.uv  = vert_in.uv;
	return vert_out;
}

float4 PSDrawAreaRGBA(VertInOut vert_in) : TARGET
{
	float4 totalcolor = float4(0.0, 0.0, 0.0, 0.0);

	const float2 uv = vert_in.uv;
	const float2 uvdelta = float2(ddx(uv.x), ddy(uv.y));

	const float2 uvhalfdelta = 0.5 * uvdelta;
	const float2 uvmin = uv - uvhalfdelta;
	const float2 uvmax = uv + uvhalfdelta;

	const int2 loadindexmin = int2(uvmin / base_dimension_i);
	const int2 loadindexmax = int2(uvmax / base_dimension_i);

	const float2 targetpos = uv / uvdelta;
	const float2 targetposleft = targetpos - 0.5;
	const float2 targetposright = targetpos + 0.5;
	for (int loadindexy = loadindexmin.y; loadindexy <= loadindexmax.y; ++loadindexy)
	{
		for (int loadindexx = loadindexmin.x; loadindexx <= loadindexmax.x; ++loadindexx)
		{
			const float2 loadindex = float2(loadindexx, loadindexy);
			const float2 potentialtargetmin = loadindex / uvdelta * base_dimension_i;
			const float2 potentialtargetmax = (loadindex + 1.0) / uvdelta * base_dimension_i;
			const float2 targetmin = max(potentialtargetmin, targetposleft);
			const float2 targetmax = min(potentialtargetmax, targetposright);
			const float area = (targetmax.x - targetmin.x) * (targetmax.y - targetmin.y);
			const float4 sample = image.SampleLevel(def_sampler, (loadindex + 0.5) * base_dimension_i, 0.0);
			totalcolor += area * float4(sample.rgb * sample.a, sample.a);
		}
	}

	return float4(totalcolor.rgb / totalcolor.a, totalcolor.a);
}

float3 ConvertFromYuv(float3 yuv)
{
	yuv = clamp(yuv, color_range_min, color_range_max);
	return saturate(mul(float4(yuv, 1.0), color_matrix)).rgb;
}

float4 PSDrawAreaMatrix(VertInOut vert_in) : TARGET
{
	float3 totalcolor = float3(0.0, 0.0, 0.0);

	const float2 uv = vert_in.uv;
	const float2 uvdelta = float2(ddx(uv.x), ddy(uv.y));

	const float2 uvhalfdelta = 0.5 * uvdelta;
	const float2 uvmin = uv - uvhalfdelta;
	const float2 uvmax = uv + uvhalfdelta;

	const int2 loadindexmin = int2(uvmin / base_dimension_i);
	const int2 loadindexmax = int2(uvmax / base_dimension_i);

	const float2 targetpos = uv / uvdelta;
	const float2 targetposleft = targetpos - 0.5;
	const float2 targetposright = targetpos + 0.5;
	for (int loadindexy = loadindexmin.y; loadindexy <= loadindexmax.y; ++loadindexy)
	{
		for (int loadindexx = loadindexmin.x; loadindexx <= loadindexmax.x; ++loadindexx)
		{
			const float2 loadindex = float2(loadindexx, loadindexy);
			const float2 potentialtargetmin = loadindex / uvdelta * base_dimension_i;
			const float2 potentialtargetmax = (loadindex + 1.0) / uvdelta * base_dimension_i;
			const float2 targetmin = max(potentialtargetmin, targetposleft);
			const float2 targetmax = min(potentialtargetmax, targetposright);
			const float area = (targetmax.x - targetmin.x) * (targetmax.y - targetmin.y);
			const float3 yuv = image.SampleLevel(def_sampler, (loadindex + 0.5) * base_dimension_i, 0.0).xyz;
			totalcolor += area * ConvertFromYuv(yuv);
		}
	}

	return float4(totalcolor, 1.0);
}

technique Draw
{
	pass
	{
		vertex_shader = VSDefault(vert_in);
		pixel_shader  = PSDrawAreaRGBA(vert_in);
	}
}

technique DrawMatrix
{
	pass
	{
		vertex_shader = VSDefault(vert_in);
		pixel_shader  = PSDrawAreaMatrix(vert_in);
	}
}