-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBitmapCommon.fxh
More file actions
133 lines (114 loc) · 3.92 KB
/
BitmapCommon.fxh
File metadata and controls
133 lines (114 loc) · 3.92 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
shared float2 ViewportScale;
shared float2 ViewportPosition;
shared float4x4 ProjectionMatrix;
shared float4x4 ModelViewMatrix;
float4 TransformPosition (float4 position, float offset) {
// Transform to view space, then offset by half a pixel to align texels with screen pixels
#ifdef FNA
// ... Except for OpenGL, who don't need no half pixels
float4 modelViewPos = mul(position, ModelViewMatrix);
#else
float4 modelViewPos = mul(position, ModelViewMatrix) - float4(offset, offset, 0, 0);
#endif
// Finally project after offsetting
return mul(modelViewPos, ProjectionMatrix);
}
uniform const float2 BitmapTextureSize;
uniform const float2 HalfTexel;
Texture2D BitmapTexture : register(t0);
sampler TextureSampler : register(s0) {
Texture = (BitmapTexture);
};
Texture2D SecondTexture : register(t1);
sampler TextureSampler2 : register(s1) {
Texture = (SecondTexture);
};
const float2 Corners[] = {
{0, 0},
{1, 0},
{1, 1},
{0, 1}
};
inline float2 ComputeRegionSize(
in float4 texRgn : POSITION1
) {
return texRgn.zw - texRgn.xy;
}
inline float2 ComputeCorner(
in int2 cornerIndex : BLENDINDICES0,
in float2 regionSize
) {
float2 corner = Corners[cornerIndex.x];
return corner * regionSize;
}
inline float2 ComputeTexCoord(
in int2 cornerIndex : BLENDINDICES0,
in float2 corner,
in float4 texRgn : POSITION1,
out float2 texTL : TEXCOORD1,
out float2 texBR : TEXCOORD2
) {
texTL = min(texRgn.xy, texRgn.zw);
texBR = max(texRgn.xy, texRgn.zw);
return clamp(
texRgn.xy + corner, texTL, texBR
);
}
inline float2 ComputeRotatedCorner(
in float2 corner,
in float4 texRgn : POSITION1,
in float4 scaleOrigin : POSITION2, // scalex, scaley, originx, originy
in float rotation : POSITION3
) {
float2 regionSize = abs(texRgn.zw - texRgn.xy);
corner = abs(corner);
corner -= (scaleOrigin.zw * regionSize);
float2 sinCos, rotatedCorner;
corner *= scaleOrigin.xy;
corner *= BitmapTextureSize;
sincos(rotation, sinCos.x, sinCos.y);
return float2(
(sinCos.y * corner.x) - (sinCos.x * corner.y),
(sinCos.x * corner.x) + (sinCos.y * corner.y)
);
}
void ScreenSpaceVertexShader(
in float3 position : POSITION0, // x, y
in float4 texRgn : POSITION1, // x1, y1, x2, y2
in float4 scaleOrigin : POSITION2, // scalex, scaley, originx, originy
in float rotation : POSITION3,
inout float4 multiplyColor : COLOR0,
inout float4 addColor : COLOR1,
in int2 cornerIndex : BLENDINDICES0, // 0-3
out float2 texCoord : TEXCOORD0,
out float2 texTL : TEXCOORD1,
out float2 texBR : TEXCOORD2,
out float4 result : POSITION0
) {
float2 regionSize = ComputeRegionSize(texRgn);
float2 corner = ComputeCorner(cornerIndex, regionSize);
texCoord = ComputeTexCoord(cornerIndex, corner, texRgn, texTL, texBR);
float2 rotatedCorner = ComputeRotatedCorner(corner, texRgn, scaleOrigin, rotation);
position.xy += rotatedCorner;
result = TransformPosition(float4(position.xy, position.z, 1), 0.0);
}
void WorldSpaceVertexShader(
in float3 position : POSITION0, // x, y
in float4 texRgn : POSITION1, // x1, y1, x2, y2
in float4 scaleOrigin : POSITION2, // scalex, scaley, originx, originy
in float rotation : POSITION3,
inout float4 multiplyColor : COLOR0,
inout float4 addColor : COLOR1,
in int2 cornerIndex : BLENDINDICES0, // 0-3
out float2 texCoord : TEXCOORD0,
out float2 texTL : TEXCOORD1,
out float2 texBR : TEXCOORD2,
out float4 result : POSITION0
) {
float2 regionSize = ComputeRegionSize(texRgn);
float2 corner = ComputeCorner(cornerIndex, regionSize);
texCoord = ComputeTexCoord(cornerIndex, corner, texRgn, texTL, texBR);
float2 rotatedCorner = ComputeRotatedCorner(corner, texRgn, scaleOrigin, rotation);
position.xy += rotatedCorner - ViewportPosition;
result = TransformPosition(float4(position.xy * ViewportScale, position.z, 1), 0.0);
}