From c9b37b4ec98ef7728b041cb8d2d6c24e3c37e84d Mon Sep 17 00:00:00 2001 From: XProger Date: Fri, 11 May 2018 19:35:38 +0300 Subject: [PATCH] #15 D3D9 ui shader; fix ortho matrix --- src/gapi_d3d9.h | 46 +++++++++++-------------- src/gapi_gl.h | 3 +- src/platform/win/main.cpp | 7 ++-- src/shaders/base.hlsl | 32 +++--------------- src/shaders/common.hlsl | 26 +++++++++++++++ src/shaders/filter.hlsl | 70 ++++----------------------------------- src/shaders/gui.glsl | 3 +- src/shaders/gui.hlsl | 30 ++--------------- src/shaders/water.hlsl | 26 +-------------- src/ui.h | 7 ++-- src/utils.h | 4 +-- 11 files changed, 73 insertions(+), 181 deletions(-) create mode 100644 src/shaders/common.hlsl diff --git a/src/gapi_d3d9.h b/src/gapi_d3d9.h index 1883de4..fd3c09e 100644 --- a/src/gapi_d3d9.h +++ b/src/gapi_d3d9.h @@ -21,11 +21,6 @@ namespace GAPI { #include "shaders/gui_vs.h" #include "shaders/gui_ps.h" - const char *SHADER_BASE = NULL; - const char *SHADER_WATER = NULL; - const char *SHADER_FILTER = NULL; - const char *SHADER_GUI = NULL; - using namespace Core; typedef ::Vertex Vertex; @@ -45,20 +40,20 @@ namespace GAPI { int reg; int usage; } bindings[uMAX] = { - { 0, USAGE_VS | USAGE_PS }, // uParam - { 1, USAGE_VS | USAGE_PS }, // uTexParam - { 2, USAGE_VS | USAGE_PS }, // uViewProj - { 6, USAGE_VS | USAGE_PS }, // uBasis - { 70, USAGE_VS | USAGE_PS }, // uLightProj - { 102, USAGE_VS | USAGE_PS }, // uMaterial - { 103, USAGE_VS | USAGE_PS }, // uAmbient - { 109, USAGE_VS | USAGE_PS }, // uFogParams - { 110, USAGE_VS | USAGE_PS }, // uViewPos - { 111, USAGE_VS | USAGE_PS }, // uLightPos - { 115, USAGE_VS | USAGE_PS }, // uLightColor - { 119, USAGE_VS | USAGE_PS }, // uRoomSize - { 120, USAGE_VS | USAGE_PS }, // uPosScale - { 122, USAGE_VS | USAGE_PS }, // uContacts + { 1, USAGE_VS | USAGE_PS }, // uParam + { 2, USAGE_VS | USAGE_PS }, // uTexParam + { 3, USAGE_VS | USAGE_PS }, // uViewProj + { 7, USAGE_VS | USAGE_PS }, // uBasis + { 71, USAGE_VS | USAGE_PS }, // uLightProj + { 103, USAGE_VS | USAGE_PS }, // uMaterial + { 104, USAGE_VS | USAGE_PS }, // uAmbient + { 110, USAGE_VS | USAGE_PS }, // uFogParams + { 111, USAGE_VS | USAGE_PS }, // uViewPos + { 112, USAGE_VS | USAGE_PS }, // uLightPos + { 116, USAGE_VS | USAGE_PS }, // uLightColor + { 120, USAGE_VS | USAGE_PS }, // uRoomSize + { 121, USAGE_VS | USAGE_PS }, // uPosScale + { 123, USAGE_VS | USAGE_PS }, // uContacts }; struct Shader { @@ -209,12 +204,12 @@ namespace GAPI { // Mesh struct Mesh { - LPDIRECT3DINDEXBUFFER9 IB; - LPDIRECT3DVERTEXBUFFER9 VB; + LPDIRECT3DINDEXBUFFER9 IB; + LPDIRECT3DVERTEXBUFFER9 VB; - int iCount; - int vCount; - bool dynamic; + int iCount; + int vCount; + bool dynamic; Mesh(bool dynamic) : IB(NULL), VB(NULL), dynamic(dynamic) {} @@ -281,7 +276,7 @@ namespace GAPI { LOG("Vendor : %s\n", adapterInfo.Description); LOG("Renderer : Direct3D 9.0c\n"); - support.maxAniso = 1; + support.maxAniso = 16; support.maxVectors = 16; support.shaderBinary = false; support.VAO = false; @@ -291,7 +286,6 @@ namespace GAPI { support.texNPOT = false; support.texRG = false; support.texBorder = false; - support.maxAniso = false; support.colorFloat = false; support.colorHalf = false; support.texFloatLinear = false; diff --git a/src/gapi_gl.h b/src/gapi_gl.h index 43289c1..9a331ae 100644 --- a/src/gapi_gl.h +++ b/src/gapi_gl.h @@ -896,7 +896,7 @@ namespace GAPI { */ #ifdef FFP - support.maxAniso = 1; + support.maxAniso = 0; support.maxVectors = 0; support.shaderBinary = false; support.VAO = false; @@ -906,7 +906,6 @@ namespace GAPI { support.texNPOT = false; support.texRG = false; support.texBorder = false; - support.maxAniso = false; support.colorFloat = false; support.colorHalf = false; support.texFloatLinear = false; diff --git a/src/platform/win/main.cpp b/src/platform/win/main.cpp index 2c9dcfc..2ff64d6 100644 --- a/src/platform/win/main.cpp +++ b/src/platform/win/main.cpp @@ -459,13 +459,12 @@ HWND hWnd; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; if (!(D3D = Direct3DCreate9(D3D_SDK_VERSION))) { - LOG("! cant't initialize DirectX"); + LOG("! cant't initialize DirectX\n"); return; } - if (!SUCCEEDED(D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device))) { - LOG("! can't create device"); - } + if (!SUCCEEDED(D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device))) + LOG("! can't create device\n"); } void ContextDelete() { diff --git a/src/shaders/base.hlsl b/src/shaders/base.hlsl index 3844ffb..0b61566 100644 --- a/src/shaders/base.hlsl +++ b/src/shaders/base.hlsl @@ -1,13 +1,4 @@ -#define MAX_LIGHTS 4 -#define MAX_CONTACTS 15 - -struct VS_INPUT { - float4 aCoord : POSITION; - float4 aNormal : NORMAL; - float4 aTexCoord : TEXCOORD0; - float4 aColor : COLOR0; - float4 aLight : COLOR1; -}; +#include "common.hlsl" struct VS_OUTPUT { float4 wPos : POSITION; @@ -21,21 +12,6 @@ struct VS_OUTPUT { float4 vLight : COLOR3; }; -float4 uParam : register( c0 ); -float4 uTexParam : register( c1 ); -float4x4 uViewProj : register( c2 ); -float4 uBasis[32 * 2] : register( c6 ); -float4x4 uLightProj : register( c70 ); -float4 uMaterial : register( c102 ); -float4 uAmbient[6] : register( c103 ); -float4 uFogParams : register( c109 ); -float4 uViewPos : register( c110 ); -float4 uLightPos[MAX_LIGHTS] : register( c111 ); -float4 uLightColor[MAX_LIGHTS] : register( c115 ); -float4 uRoomSize : register( c119 ); -float4 uPosScale[2] : register( c120 ); -float4 uContacts[MAX_CONTACTS] : register( c122 ); - #ifdef VERTEX float3 mulQuat(float4 q, float3 v) { return v + 2.0 * cross(q.xyz, cross(q.xyz, v) + v * q.w); @@ -57,11 +33,11 @@ VS_OUTPUT main(VS_INPUT In) { Out.vTexCoord = In.aTexCoord * (1.0 / 32767.0); Out.vTexCoord.xy *= Out.vTexCoord.zw; - Out.vDiffuse = float4(In.aColor.xyz * In.aLight.xyz, 1.0); + Out.vDiffuse = float4(In.aColor.xyz * (uMaterial.x), uMaterial.w); Out.vNormal = In.aNormal; Out.vViewVec = float4(uViewPos.xyz - Out.vCoord, 0.0); Out.vAmbient = float3(0.2, 0.2, 0.2); - Out.vLightMap = float4(1.0, 1.0, 1.0, 0.0); + Out.vLightMap = In.aLight; Out.vLight = float4(0.5, 0.5, 0.5, 0.5); return Out; } @@ -76,6 +52,6 @@ sampler sEnvironment : register(s4); sampler sMask : register(s5); float4 main(VS_OUTPUT In) : COLOR0 { - return (In.vDiffuse * tex2D(sDiffuse, In.vTexCoord.xy / In.vTexCoord.zw)).bgra; + return (In.vDiffuse * In.vLightMap * tex2D(sDiffuse, In.vTexCoord.xy / In.vTexCoord.zw)).bgra; } #endif \ No newline at end of file diff --git a/src/shaders/common.hlsl b/src/shaders/common.hlsl new file mode 100644 index 0000000..5813e44 --- /dev/null +++ b/src/shaders/common.hlsl @@ -0,0 +1,26 @@ +#define MAX_LIGHTS 4 +#define MAX_CONTACTS 15 + +struct VS_INPUT { + float4 aCoord : POSITION; + float4 aNormal : NORMAL; + float4 aTexCoord : TEXCOORD0; + float4 aColor : COLOR0; + float4 aLight : COLOR1; +}; + +int4 uInfo : register( c0 ); +float4 uParam : register( c1 ); +float4 uTexParam : register( c2 ); +float4x4 uViewProj : register( c3 ); +float4 uBasis[32 * 2] : register( c7 ); +float4x4 uLightProj : register( c71 ); +float4 uMaterial : register( c103 ); +float4 uAmbient[6] : register( c104 ); +float4 uFogParams : register( c110 ); +float4 uViewPos : register( c111 ); +float4 uLightPos[MAX_LIGHTS] : register( c112 ); +float4 uLightColor[MAX_LIGHTS] : register( c116 ); +float4 uRoomSize : register( c120 ); +float4 uPosScale[2] : register( c121 ); +float4 uContacts[MAX_CONTACTS] : register( c123 ); \ No newline at end of file diff --git a/src/shaders/filter.hlsl b/src/shaders/filter.hlsl index 3844ffb..4d8d14a 100644 --- a/src/shaders/filter.hlsl +++ b/src/shaders/filter.hlsl @@ -1,81 +1,25 @@ -#define MAX_LIGHTS 4 -#define MAX_CONTACTS 15 - -struct VS_INPUT { - float4 aCoord : POSITION; - float4 aNormal : NORMAL; - float4 aTexCoord : TEXCOORD0; - float4 aColor : COLOR0; - float4 aLight : COLOR1; -}; +#include "common.hlsl" struct VS_OUTPUT { float4 wPos : POSITION; - float3 vCoord : TEXCOORD2; - float4 vTexCoord : TEXCOORD0; + float2 vTexCoord : TEXCOORD0; float4 vDiffuse : COLOR0; - float4 vNormal : NORMAL; - float4 vViewVec : TEXCOORD1; - float3 vAmbient : COLOR1; - float4 vLightMap : COLOR2; - float4 vLight : COLOR3; }; -float4 uParam : register( c0 ); -float4 uTexParam : register( c1 ); -float4x4 uViewProj : register( c2 ); -float4 uBasis[32 * 2] : register( c6 ); -float4x4 uLightProj : register( c70 ); -float4 uMaterial : register( c102 ); -float4 uAmbient[6] : register( c103 ); -float4 uFogParams : register( c109 ); -float4 uViewPos : register( c110 ); -float4 uLightPos[MAX_LIGHTS] : register( c111 ); -float4 uLightColor[MAX_LIGHTS] : register( c115 ); -float4 uRoomSize : register( c119 ); -float4 uPosScale[2] : register( c120 ); -float4 uContacts[MAX_CONTACTS] : register( c122 ); - #ifdef VERTEX -float3 mulQuat(float4 q, float3 v) { - return v + 2.0 * cross(q.xyz, cross(q.xyz, v) + v * q.w); -} - -float3 mulBasis(float4 rot, float3 pos, float3 v) { - return mulQuat(rot, v) + pos; -} - VS_OUTPUT main(VS_INPUT In) { VS_OUTPUT Out; - - int index = int(In.aCoord.w * 2.0); - float4 rBasisRot = uBasis[index]; - float4 rBasisPos = uBasis[index + 1]; - Out.vCoord = mulBasis(rBasisRot, rBasisPos.xyz, In.aCoord.xyz); - Out.wPos = mul(uViewProj, float4(Out.vCoord, rBasisPos.w)); - - Out.vTexCoord = In.aTexCoord * (1.0 / 32767.0); - Out.vTexCoord.xy *= Out.vTexCoord.zw; - - Out.vDiffuse = float4(In.aColor.xyz * In.aLight.xyz, 1.0); - Out.vNormal = In.aNormal; - Out.vViewVec = float4(uViewPos.xyz - Out.vCoord, 0.0); - Out.vAmbient = float3(0.2, 0.2, 0.2); - Out.vLightMap = float4(1.0, 1.0, 1.0, 0.0); - Out.vLight = float4(0.5, 0.5, 0.5, 0.5); + Out.wPos = float4(In.aCoord.xy * (1.0 / 32767.0), 0.0, 1.0); + Out.vTexCoord = In.aTexCoord.xy * (1.0 / 32767.0); + Out.vDiffuse = In.aLight; return Out; } #else // PIXEL -sampler sDiffuse : register(s0); -sampler sNormal : register(s1); -sampler sReflect : register(s2); -sampler sShadow : register(s3); -sampler sEnvironment : register(s4); -sampler sMask : register(s5); +sampler sDiffuse : register(s0); float4 main(VS_OUTPUT In) : COLOR0 { - return (In.vDiffuse * tex2D(sDiffuse, In.vTexCoord.xy / In.vTexCoord.zw)).bgra; + return (In.vDiffuse * tex2D(sDiffuse, In.vTexCoord.xy)).bgra; } #endif \ No newline at end of file diff --git a/src/shaders/gui.glsl b/src/shaders/gui.glsl index 93e760b..99a5999 100644 --- a/src/shaders/gui.glsl +++ b/src/shaders/gui.glsl @@ -9,7 +9,6 @@ varying vec4 vColor; #ifdef VERTEX uniform mat4 uViewProj; - uniform vec4 uPosScale; uniform vec4 uMaterial; attribute vec4 aCoord; @@ -19,7 +18,7 @@ varying vec4 vColor; void main() { vTexCoord = aTexCoord.xy; vColor = aLight * uMaterial; - gl_Position = uViewProj * vec4(aCoord.xy * uPosScale.zw + uPosScale.xy, 0.0, 1.0); + gl_Position = uViewProj * vec4(aCoord.xyz, 1.0); } #else uniform sampler2D sDiffuse; diff --git a/src/shaders/gui.hlsl b/src/shaders/gui.hlsl index beff268..7f2f280 100644 --- a/src/shaders/gui.hlsl +++ b/src/shaders/gui.hlsl @@ -1,28 +1,4 @@ -#define MAX_LIGHTS 4 -#define MAX_CONTACTS 15 - -struct VS_INPUT { - float4 aCoord : POSITION; - float4 aNormal : NORMAL; - float4 aTexCoord : TEXCOORD0; - float4 aColor : COLOR0; - float4 aLight : COLOR1; -}; - -float4 uParam : register( c0 ); -float4 uTexParam : register( c1 ); -float4x4 uViewProj : register( c2 ); -float4 uBasis[32 * 2] : register( c6 ); -float4x4 uLightProj : register( c70 ); -float4 uMaterial : register( c102 ); -float4 uAmbient[6] : register( c103 ); -float4 uFogParams : register( c109 ); -float4 uViewPos : register( c110 ); -float4 uLightPos[MAX_LIGHTS] : register( c111 ); -float4 uLightColor[MAX_LIGHTS] : register( c115 ); -float4 uRoomSize : register( c119 ); -float4 uPosScale[2] : register( c120 ); -float4 uContacts[MAX_CONTACTS] : register( c122 ); +#include "common.hlsl" struct VS_OUTPUT { float4 wPos : POSITION; @@ -33,7 +9,7 @@ struct VS_OUTPUT { #ifdef VERTEX VS_OUTPUT main(VS_INPUT In) { VS_OUTPUT Out; - Out.wPos = mul(uViewProj, float4(In.aCoord.xy * uPosScale[0].zw + uPosScale[0].xy, 0.0, 1.0)); + Out.wPos = mul(uViewProj, float4(In.aCoord.xy, 0.0, 1.0)); Out.vTexCoord = In.aTexCoord.xy * (1.0 / 32767.0); Out.vDiffuse = In.aLight * uMaterial; return Out; @@ -44,6 +20,6 @@ VS_OUTPUT main(VS_INPUT In) { sampler sDiffuse : register(s0); float4 main(VS_OUTPUT In) : COLOR0 { - return float4(1.0, 1.0, 1.0, 1.0);//(In.vDiffuse * tex2D(sDiffuse, In.vTexCoord.xy)).bgra; + return (In.vDiffuse * tex2D(sDiffuse, In.vTexCoord.xy)).bgra; } #endif \ No newline at end of file diff --git a/src/shaders/water.hlsl b/src/shaders/water.hlsl index 3844ffb..1341dec 100644 --- a/src/shaders/water.hlsl +++ b/src/shaders/water.hlsl @@ -1,13 +1,4 @@ -#define MAX_LIGHTS 4 -#define MAX_CONTACTS 15 - -struct VS_INPUT { - float4 aCoord : POSITION; - float4 aNormal : NORMAL; - float4 aTexCoord : TEXCOORD0; - float4 aColor : COLOR0; - float4 aLight : COLOR1; -}; +#include "common.hlsl" struct VS_OUTPUT { float4 wPos : POSITION; @@ -21,21 +12,6 @@ struct VS_OUTPUT { float4 vLight : COLOR3; }; -float4 uParam : register( c0 ); -float4 uTexParam : register( c1 ); -float4x4 uViewProj : register( c2 ); -float4 uBasis[32 * 2] : register( c6 ); -float4x4 uLightProj : register( c70 ); -float4 uMaterial : register( c102 ); -float4 uAmbient[6] : register( c103 ); -float4 uFogParams : register( c109 ); -float4 uViewPos : register( c110 ); -float4 uLightPos[MAX_LIGHTS] : register( c111 ); -float4 uLightColor[MAX_LIGHTS] : register( c115 ); -float4 uRoomSize : register( c119 ); -float4 uPosScale[2] : register( c120 ); -float4 uContacts[MAX_CONTACTS] : register( c122 ); - #ifdef VERTEX float3 mulQuat(float4 q, float3 v) { return v + 2.0 * cross(q.xyz, cross(q.xyz, v) + v * q.w); diff --git a/src/ui.h b/src/ui.h index 9778fa6..89a41f2 100644 --- a/src/ui.h +++ b/src/ui.h @@ -296,7 +296,6 @@ namespace UI { game->setShader(Core::passGUI, Shader::DEFAULT); Core::setMaterial(1, 1, 1, 1); - Core::active.shader->setParam(uPosScale, vec4(0, 0, 1, 1)); buffer.iCount = buffer.vCount = 0; @@ -476,7 +475,11 @@ namespace UI { } void renderControl(const vec2 &pos, float size, bool active) { - Core::active.shader->setParam(uPosScale, vec4(pos, vec2(size * (active ? 2.0f : 1.0f) / 32767.0f))); + vec2 scale = vec2(size * (active ? 2.0f : 1.0f) / 32767.0f); + mat4 m = Core::mViewProj; + m.translate(vec3(pos.x, pos.y, 0.0)); + m.scale(vec3(scale.x, scale.y, 1.0)); + Core::active.shader->setParam(uViewProj, m); Core::active.shader->setParam(uMaterial, vec4(1.0f, 1.0f, 1.0f, active ? 0.7f : 0.5f)); game->getMesh()->renderCircle(); } diff --git a/src/utils.h b/src/utils.h index 574f191..c7f4e1f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -500,8 +500,8 @@ struct mat4 { e11 = 2.0f / (t - b); e22 = 2.0f / (znear - zfar); e03 = (l + r) / (l - r); - e13 = (b + t) / (b - t); - e23 = (zfar + znear) / (znear - zfar); + e13 = (t + b) / (b - t); + e23 = znear / (znear - zfar); } mat4(float fov, float aspect, float znear, float zfar) {