switched d3d im2d to use a VS so we can have fog

This commit is contained in:
aap 2021-02-12 20:09:37 +01:00
parent fb859c0fa6
commit 41ae7b9b61
12 changed files with 275 additions and 76 deletions

View File

@ -43,7 +43,9 @@ void
openIm2D(void)
{
D3DVERTEXELEMENT9 elements[4] = {
{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
// can't get proper fog with this :(
// { 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 },
{ 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, offsetof(Im2DVertex, color), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
{ 0, offsetof(Im2DVertex, u), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
D3DDECL_END()
@ -99,6 +101,20 @@ im2DRenderTriangle(void *vertices, int32 numVertices, int32 vert1, int32 vert2,
im2DRenderPrimitive(PRIMTYPETRILIST, tmpprimbuf, 3);
}
void
im2DSetXform(void)
{
float xform[4];
Camera *cam;
cam = (Camera*)engine->currentCamera;
xform[0] = 2.0f/cam->frameBuffer->width;
xform[1] = -2.0f/cam->frameBuffer->height;
xform[2] = -1.0f;
xform[3] = 1.0f;
// TODO: should cache this...
d3ddevice->SetVertexShaderConstantF(VSLOC_afterLights, xform, 1);
}
void
im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
{
@ -113,6 +129,9 @@ im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
setStreamSource(0, im2dvertbuf, 0, sizeof(Im2DVertex));
setVertexDeclaration(im2ddecl);
im2DSetXform();
setVertexShader(im2d_VS);
if(im2dOverridePS)
setPixelShader(im2dOverridePS);
else if(engine->device.getRenderState(TEXTURERASTER))
@ -167,6 +186,9 @@ im2DRenderIndexedPrimitive(PrimitiveType primType,
setIndices(im2dindbuf);
setVertexDeclaration(im2ddecl);
im2DSetXform();
setVertexShader(im2d_VS);
if(im2dOverridePS)
setPixelShader(im2dOverridePS);
else if(engine->device.getRenderState(TEXTURERASTER))

View File

@ -27,6 +27,7 @@ void *default_amb_dir_VS;
void *default_all_VS;
void *default_PS;
void *default_tex_PS;
void *im2d_VS;
void *im2d_PS;
void *im2d_tex_PS;
@ -66,6 +67,12 @@ createDefaultShaders(void)
assert(default_tex_PS);
}
{
static
#include "shaders/im2d_VS.h"
im2d_VS = createVertexShader((void*)VS_NAME);
assert(im2d_VS);
}
{
static
#include "shaders/im2d_PS.h"
@ -95,6 +102,8 @@ destroyDefaultShaders(void)
destroyPixelShader(default_tex_PS);
default_tex_PS = nil;
destroyVertexShader(im2d_VS);
im2d_VS = nil;
destroyPixelShader(im2d_PS);
im2d_PS = nil;
destroyPixelShader(im2d_tex_PS);

View File

@ -64,15 +64,17 @@ struct Im3DVertex
struct Im2DVertex
{
float32 x, y, z;
float32 q;
//float32 q; // recipz no longer used because we have a vertex stage now
float32 w;
uint32 color;
float32 u, v;
void setScreenX(float32 x) { this->x = x; }
void setScreenY(float32 y) { this->y = y; }
void setScreenZ(float32 z) { this->z = z; }
void setCameraZ(float32 z) { }
void setRecipCameraZ(float32 recipz) { this->q = recipz; }
void setCameraZ(float32 z) { this->w = z; }
// void setRecipCameraZ(float32 recipz) { this->q = recipz; }
void setRecipCameraZ(float32 recipz) { this->w = 1.0f/recipz; }
void setColor(uint8 r, uint8 g, uint8 b, uint8 a) { this->color = COLOR_ARGB(a, r, g, b); }
void setU(float32 u, float recipZ) { this->u = u; }
void setV(float32 v, float recipZ) { this->v = v; }
@ -80,8 +82,10 @@ struct Im2DVertex
float getScreenX(void) { return this->x; }
float getScreenY(void) { return this->y; }
float getScreenZ(void) { return this->z; }
float getCameraZ(void) { return 1.0f/this->q; }
float getRecipCameraZ(void) { return this->q; }
// float getCameraZ(void) { return 1.0f/this->q; }
// float getRecipCameraZ(void) { return this->q; }
float getCameraZ(void) { return this->w; }
float getRecipCameraZ(void) { return 1.0f/this->w; }
RGBA getColor(void) { return makeRGBA(this->color>>16 & 0xFF, this->color>>8 & 0xFF,
this->color & 0xFF, this->color>>24 & 0xFF); }
float getU(void) { return this->u; }
@ -396,6 +400,7 @@ extern void *default_amb_dir_VS;
extern void *default_all_VS;
extern void *default_PS;
extern void *default_tex_PS;
extern void *im2d_VS;
extern void *im2d_PS;
extern void *im2d_tex_PS;
void createDefaultShaders(void);

View File

@ -4,22 +4,47 @@
//
// fxc /nologo /T ps_2_0 /Fh im2d_PS.h im2d_PS.hlsl
//
ps_2_0
dcl v0
mov oC0, v0
//
// Parameters:
//
// float4 fogColor;
//
//
// Registers:
//
// Name Reg Size
// ------------ ----- ----
// fogColor c0 1
//
// approximately 1 instruction slot used
ps_2_0
dcl t0.xyz
dcl v0
add r0.xyz, v0, -c0
mad r0.xyz, t0.z, r0, c0
mov r0.w, v0.w
mov oC0, r0
// approximately 4 instruction slots used
#endif
const BYTE g_ps20_main[] =
{
0, 2, 255, 255, 254, 255,
22, 0, 67, 84, 65, 66,
28, 0, 0, 0, 35, 0,
34, 0, 67, 84, 65, 66,
28, 0, 0, 0, 83, 0,
0, 0, 0, 2, 255, 255,
0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 28, 0,
0, 0, 0, 1, 0, 0,
28, 0, 0, 0, 112, 115,
76, 0, 0, 0, 48, 0,
0, 0, 2, 0, 0, 0,
1, 0, 2, 0, 60, 0,
0, 0, 0, 0, 0, 0,
102, 111, 103, 67, 111, 108,
111, 114, 0, 171, 171, 171,
1, 0, 3, 0, 1, 0,
4, 0, 1, 0, 0, 0,
0, 0, 0, 0, 112, 115,
95, 50, 95, 48, 0, 77,
105, 99, 114, 111, 115, 111,
102, 116, 32, 40, 82, 41,
@ -29,9 +54,19 @@ const BYTE g_ps20_main[] =
108, 101, 114, 32, 57, 46,
50, 57, 46, 57, 53, 50,
46, 51, 49, 49, 49, 0,
31, 0, 0, 2, 0, 0,
0, 128, 0, 0, 7, 176,
31, 0, 0, 2, 0, 0,
0, 128, 0, 0, 15, 144,
2, 0, 0, 3, 0, 0,
7, 128, 0, 0, 228, 144,
0, 0, 228, 161, 4, 0,
0, 4, 0, 0, 7, 128,
0, 0, 170, 176, 0, 0,
228, 128, 0, 0, 228, 160,
1, 0, 0, 2, 0, 0,
8, 128, 0, 0, 255, 144,
1, 0, 0, 2, 0, 8,
15, 128, 0, 0, 228, 144,
15, 128, 0, 0, 228, 128,
255, 255, 0, 0
};

View File

@ -1,11 +1,12 @@
struct VS_out {
float4 Position : POSITION;
float2 TexCoord0 : TEXCOORD0;
float3 TexCoord0 : TEXCOORD0;
float4 Color : COLOR0;
};
sampler2D tex0 : register(s0);
float4 fogColor : register(c0);
float4 main(VS_out input) : COLOR
{
@ -13,5 +14,6 @@ float4 main(VS_out input) : COLOR
#ifdef TEX
color *= tex2D(tex0, input.TexCoord0.xy);
#endif
color.rgb = lerp(fogColor.rgb, color.rgb, input.TexCoord0.z);
return color;
}

107
src/d3d/shaders/im2d_VS.h Normal file
View File

@ -0,0 +1,107 @@
#if 0
//
// Generated by Microsoft (R) HLSL Shader Compiler 9.29.952.3111
//
// fxc /nologo /T vs_2_0 /Fh im2d_VS.h im2d_VS.hlsl
//
//
// Parameters:
//
// float4 fogData;
// float4 xform;
//
//
// Registers:
//
// Name Reg Size
// ------------ ----- ----
// fogData c14 1
// xform c41 1
//
vs_2_0
def c0, 1, 0, 0, 0
dcl_position v0
dcl_texcoord v1
dcl_color v2
add r0.x, v0.w, -c14.y
mul r0.x, r0.x, c14.z
max r0.x, r0.x, c14.w
min oT0.z, r0.x, c0.x
mad r0.xy, v0, c41, c41.zwzw
mov r0.z, v0.z
mul oPos.xyz, r0, v0.w
mov oPos.w, v0.w
mov oT0.xy, v1
mov oD0, v2
// approximately 10 instruction slots used
#endif
const BYTE g_vs20_main[] =
{
0, 2, 254, 255, 254, 255,
40, 0, 67, 84, 65, 66,
28, 0, 0, 0, 105, 0,
0, 0, 0, 2, 254, 255,
2, 0, 0, 0, 28, 0,
0, 0, 0, 1, 0, 0,
98, 0, 0, 0, 68, 0,
0, 0, 2, 0, 14, 0,
1, 0, 58, 0, 76, 0,
0, 0, 0, 0, 0, 0,
92, 0, 0, 0, 2, 0,
41, 0, 1, 0, 166, 0,
76, 0, 0, 0, 0, 0,
0, 0, 102, 111, 103, 68,
97, 116, 97, 0, 1, 0,
3, 0, 1, 0, 4, 0,
1, 0, 0, 0, 0, 0,
0, 0, 120, 102, 111, 114,
109, 0, 118, 115, 95, 50,
95, 48, 0, 77, 105, 99,
114, 111, 115, 111, 102, 116,
32, 40, 82, 41, 32, 72,
76, 83, 76, 32, 83, 104,
97, 100, 101, 114, 32, 67,
111, 109, 112, 105, 108, 101,
114, 32, 57, 46, 50, 57,
46, 57, 53, 50, 46, 51,
49, 49, 49, 0, 171, 171,
81, 0, 0, 5, 0, 0,
15, 160, 0, 0, 128, 63,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
31, 0, 0, 2, 0, 0,
0, 128, 0, 0, 15, 144,
31, 0, 0, 2, 5, 0,
0, 128, 1, 0, 15, 144,
31, 0, 0, 2, 10, 0,
0, 128, 2, 0, 15, 144,
2, 0, 0, 3, 0, 0,
1, 128, 0, 0, 255, 144,
14, 0, 85, 161, 5, 0,
0, 3, 0, 0, 1, 128,
0, 0, 0, 128, 14, 0,
170, 160, 11, 0, 0, 3,
0, 0, 1, 128, 0, 0,
0, 128, 14, 0, 255, 160,
10, 0, 0, 3, 0, 0,
4, 224, 0, 0, 0, 128,
0, 0, 0, 160, 4, 0,
0, 4, 0, 0, 3, 128,
0, 0, 228, 144, 41, 0,
228, 160, 41, 0, 238, 160,
1, 0, 0, 2, 0, 0,
4, 128, 0, 0, 170, 144,
5, 0, 0, 3, 0, 0,
7, 192, 0, 0, 228, 128,
0, 0, 255, 144, 1, 0,
0, 2, 0, 0, 8, 192,
0, 0, 255, 144, 1, 0,
0, 2, 0, 0, 3, 224,
1, 0, 228, 144, 1, 0,
0, 2, 0, 0, 15, 208,
2, 0, 228, 144, 255, 255,
0, 0
};

View File

@ -0,0 +1,30 @@
#include "standardConstants.h"
struct VS_in
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD0;
float4 Color : COLOR0;
};
struct VS_out {
float4 Position : POSITION;
float3 TexCoord0 : TEXCOORD0;
float4 Color : COLOR0;
};
float4 xform : register(c41);
VS_out main(in VS_in input)
{
VS_out output;
output.Position = input.Position;
output.Position.xy = output.Position.xy * xform.xy + xform.zw;
output.TexCoord0.z = clamp((output.Position.w - fogEnd)*fogRange, fogDisable, 1.0);
output.Position.xyz *= output.Position.w;
output.Color = input.Color;
output.TexCoord0.xy = input.TexCoord;
return output;
}

View File

@ -7,6 +7,7 @@
//
// Parameters:
//
// float4 fogColor;
// sampler2D tex0;
//
//
@ -14,31 +15,42 @@
//
// Name Reg Size
// ------------ ----- ----
// fogColor c0 1
// tex0 s0 1
//
ps_2_0
dcl t0.xy
dcl t0.xyz
dcl v0
dcl_2d s0
texld r0, t0, s0
mul r0, r0, v0
mov oC0, r0
mad r0.xyz, v0, r0, -c0
mul r1.w, r0.w, v0.w
mad r1.xyz, t0.z, r0, c0
mov oC0, r1
// approximately 3 instruction slots used (1 texture, 2 arithmetic)
// approximately 5 instruction slots used (1 texture, 4 arithmetic)
#endif
const BYTE g_ps20_main[] =
{
0, 2, 255, 255, 254, 255,
33, 0, 67, 84, 65, 66,
28, 0, 0, 0, 79, 0,
45, 0, 67, 84, 65, 66,
28, 0, 0, 0, 127, 0,
0, 0, 0, 2, 255, 255,
1, 0, 0, 0, 28, 0,
2, 0, 0, 0, 28, 0,
0, 0, 0, 1, 0, 0,
72, 0, 0, 0, 48, 0,
0, 0, 3, 0, 0, 0,
1, 0, 2, 0, 56, 0,
120, 0, 0, 0, 68, 0,
0, 0, 2, 0, 0, 0,
1, 0, 2, 0, 80, 0,
0, 0, 0, 0, 0, 0,
96, 0, 0, 0, 3, 0,
0, 0, 1, 0, 2, 0,
104, 0, 0, 0, 0, 0,
0, 0, 102, 111, 103, 67,
111, 108, 111, 114, 0, 171,
171, 171, 1, 0, 3, 0,
1, 0, 4, 0, 1, 0,
0, 0, 0, 0, 0, 0,
116, 101, 120, 48, 0, 171,
171, 171, 4, 0, 12, 0,
@ -55,17 +67,23 @@ const BYTE g_ps20_main[] =
53, 50, 46, 51, 49, 49,
49, 0, 31, 0, 0, 2,
0, 0, 0, 128, 0, 0,
3, 176, 31, 0, 0, 2,
7, 176, 31, 0, 0, 2,
0, 0, 0, 128, 0, 0,
15, 144, 31, 0, 0, 2,
0, 0, 0, 144, 0, 8,
15, 160, 66, 0, 0, 3,
0, 0, 15, 128, 0, 0,
228, 176, 0, 8, 228, 160,
5, 0, 0, 3, 0, 0,
15, 128, 0, 0, 228, 128,
0, 0, 228, 144, 1, 0,
0, 2, 0, 8, 15, 128,
0, 0, 228, 128, 255, 255,
0, 0
4, 0, 0, 4, 0, 0,
7, 128, 0, 0, 228, 144,
0, 0, 228, 128, 0, 0,
228, 161, 5, 0, 0, 3,
1, 0, 8, 128, 0, 0,
255, 128, 0, 0, 255, 144,
4, 0, 0, 4, 1, 0,
7, 128, 0, 0, 170, 176,
0, 0, 228, 128, 0, 0,
228, 160, 1, 0, 0, 2,
0, 8, 15, 128, 1, 0,
228, 128, 255, 255, 0, 0
};

View File

@ -1,25 +0,0 @@
float4x4 combinedMat : register(c0);
struct VS_in
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD0;
float4 Color : COLOR0;
};
struct VS_out {
float4 Position : POSITION;
float2 TexCoord0 : TEXCOORD0;
float4 Color : COLOR0;
};
VS_out main(in VS_in input)
{
VS_out output;
output.Position = mul(combinedMat, input.Position);
output.TexCoord0 = input.TexCoord;
output.Color = input.Color;
return output;
}

View File

@ -6,5 +6,6 @@
"%DXSDK_DIR%\utilities\bin\x86\fxc.exe" /nologo /T ps_2_0 /Fh default_PS.h default_PS.hlsl
"%DXSDK_DIR%\utilities\bin\x86\fxc.exe" /nologo /T ps_2_0 /DTEX /Fh default_tex_PS.h default_PS.hlsl
"%DXSDK_DIR%\utilities\bin\x86\fxc.exe" /nologo /T vs_2_0 /Fh im2d_VS.h im2d_VS.hlsl
"%DXSDK_DIR%\utilities\bin\x86\fxc.exe" /nologo /T ps_2_0 /Fh im2d_PS.h im2d_PS.hlsl
"%DXSDK_DIR%\utilities\bin\x86\fxc.exe" /nologo /T ps_2_0 /DTEX /Fh im2d_tex_PS.h im2d_PS.hlsl

View File

@ -112,12 +112,21 @@ im2DRenderTriangle(void *vertices, int32 numVertices, int32 vert1, int32 vert2,
}
void
im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
im2DSetXform(void)
{
GLfloat xform[4];
Camera *cam;
cam = (Camera*)engine->currentCamera;
xform[0] = 2.0f/cam->frameBuffer->width;
xform[1] = -2.0f/cam->frameBuffer->height;
xform[2] = -1.0f;
xform[3] = 1.0f;
glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform);
}
void
im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
{
#ifdef RW_GL_USE_VAOS
glBindVertexArray(im2DVao);
#endif
@ -126,11 +135,6 @@ im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im2DVertex), nil, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices*sizeof(Im2DVertex), vertices);
xform[0] = 2.0f/cam->frameBuffer->width;
xform[1] = -2.0f/cam->frameBuffer->height;
xform[2] = -1.0f;
xform[3] = 1.0f;
if(im2dOverrideShader)
im2dOverrideShader->use();
else
@ -139,7 +143,7 @@ im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
setAttribPointers(im2dattribDesc, 3);
#endif
glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform);
im2DSetXform();
flushCache();
glDrawArrays(primTypeMap[primType], 0, numVertices);
@ -153,10 +157,6 @@ im2DRenderIndexedPrimitive(PrimitiveType primType,
void *vertices, int32 numVertices,
void *indices, int32 numIndices)
{
GLfloat xform[4];
Camera *cam;
cam = (Camera*)engine->currentCamera;
#ifdef RW_GL_USE_VAOS
glBindVertexArray(im2DVao);
#endif
@ -169,11 +169,6 @@ im2DRenderIndexedPrimitive(PrimitiveType primType,
glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im2DVertex), nil, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices*sizeof(Im2DVertex), vertices);
xform[0] = 2.0f/cam->frameBuffer->width;
xform[1] = -2.0f/cam->frameBuffer->height;
xform[2] = -1.0f;
xform[3] = 1.0f;
if(im2dOverrideShader)
im2dOverrideShader->use();
else
@ -182,7 +177,7 @@ im2DRenderIndexedPrimitive(PrimitiveType primType,
setAttribPointers(im2dattribDesc, 3);
#endif
glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform);
im2DSetXform();
flushCache();
glDrawElements(primTypeMap[primType], numIndices,

View File

@ -131,7 +131,7 @@ struct Im2DVertex
void setScreenY(float32 y) { this->y = y; }
void setScreenZ(float32 z) { this->z = z; }
// This is a bit unefficient but we have to counteract GL's divide, so multiply
void setCameraZ(float32 z) { }
void setCameraZ(float32 z) { this->w = z; }
void setRecipCameraZ(float32 recipz) { this->w = 1.0f/recipz; }
void setColor(uint8 r, uint8 g, uint8 b, uint8 a) {
this->r = r; this->g = g; this->b = b; this->a = a; }