// Copyright (C) 2003 Dolphin Project. // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, version 2.0. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License 2.0 for more details. // A copy of the GPL 2.0 should have been included with the program. // If not, see http://www.gnu.org/licenses/ // Official SVN repository and contact information can be found at // http://code.google.com/p/dolphin-emu/ #include #include "Profiler.h" #include "NativeVertexFormat.h" #include "BPMemory.h" #include "VertexShaderGen.h" // Mash together all the inputs that contribute to the code of a generated vertex shader into // a unique identifier, basically containing all the bits. Yup, it's a lot .... void GetVertexShaderId(VERTEXSHADERUID& vid, u32 components) { vid.values[0] = components | (xfregs.numTexGens << 23) | (xfregs.nNumChans << 27) | ((u32)xfregs.bEnableDualTexTransform << 29); for (int i = 0; i < 2; ++i) { vid.values[1+i] = xfregs.colChans[i].color.enablelighting ? (u32)xfregs.colChans[i].color.hex : (u32)xfregs.colChans[i].color.matsource; vid.values[1+i] |= (xfregs.colChans[i].alpha.enablelighting ? (u32)xfregs.colChans[i].alpha.hex : (u32)xfregs.colChans[i].alpha.matsource) << 15; } // fog vid.values[1] |= (((u32)bpmem.fog.c_proj_fsel.fsel & 3) << 30); vid.values[2] |= (((u32)bpmem.fog.c_proj_fsel.fsel >> 2) << 30); u32* pcurvalue = &vid.values[3]; for (int i = 0; i < xfregs.numTexGens; ++i) { TexMtxInfo tinfo = xfregs.texcoords[i].texmtxinfo; if (tinfo.texgentype != XF_TEXGEN_EMBOSS_MAP) tinfo.hex &= 0x7ff; if (tinfo.texgentype != XF_TEXGEN_REGULAR) tinfo.projection = 0; u32 val = ((tinfo.hex >> 1) & 0x1ffff); if (xfregs.bEnableDualTexTransform && tinfo.texgentype == XF_TEXGEN_REGULAR) { // rewrite normalization and post index val |= ((u32)xfregs.texcoords[i].postmtxinfo.index << 17) | ((u32)xfregs.texcoords[i].postmtxinfo.normalize << 23); } switch (i & 3) { case 0: pcurvalue[0] |= val; break; case 1: pcurvalue[0] |= val << 24; pcurvalue[1] = val >> 8; ++pcurvalue; break; case 2: pcurvalue[0] |= val << 16; pcurvalue[1] = val >> 16; ++pcurvalue; break; case 3: pcurvalue[0] |= val << 8; ++pcurvalue; break; } } } static char text[16384]; #define WRITE p+=sprintf #define LIGHTS_POS "" char *GenerateLightShader(char* p, int index, const LitChannel& chan, const char* dest, int coloralpha); const char *GenerateVertexShader(u32 components, bool D3D) { text[sizeof(text) - 1] = 0x7C; // canary DVSTARTPROFILE(); _assert_( bpmem.genMode.numtexgens == xfregs.numTexGens); _assert_( bpmem.genMode.numcolchans == xfregs.nNumChans); u32 lightMask = 0; if (xfregs.nNumChans > 0) lightMask |= xfregs.colChans[0].color.GetFullLightMask() | xfregs.colChans[0].alpha.GetFullLightMask(); if (xfregs.nNumChans > 1) lightMask |= xfregs.colChans[1].color.GetFullLightMask() | xfregs.colChans[1].alpha.GetFullLightMask(); char *p = text; WRITE(p, "//Vertex Shader: comp:%x, \n", components); WRITE(p, "typedef struct {\n" " float4 T0, T1, T2;\n" " float4 N0, N1, N2;\n" "} s_"I_POSNORMALMATRIX";\n\n" "typedef struct {\n" " float4 t;\n" "} FLT4;\n" "typedef struct {\n" " FLT4 T[24];\n" "} s_"I_TEXMATRICES";\n\n" "typedef struct {\n" " FLT4 T[64];\n" "} s_"I_TRANSFORMMATRICES";\n\n" "typedef struct {\n" " FLT4 T[32];\n" "} s_"I_NORMALMATRICES";\n\n" "typedef struct {\n" " FLT4 T[64];\n" "} s_"I_POSTTRANSFORMMATRICES";\n\n" "typedef struct {\n" " float4 col;\n" " float4 cosatt;\n" " float4 distatt;\n" " float4 pos;\n" " float4 dir;\n" "} Light;\n\n" "typedef struct {\n" " Light lights[8];\n" "} s_"I_LIGHTS";\n\n" "typedef struct {\n" " float4 C0, C1, C2, C3;\n" "} s_"I_MATERIALS";\n\n" "typedef struct {\n" " float4 T0,T1,T2,T3;\n" "} s_"I_PROJECTION";\n" "typedef struct {\n" " float4 params;\n" // a, b, c, b_shift "} s_"I_FOGPARAMS";\n\n"); WRITE(p, "struct VS_OUTPUT {\n"); WRITE(p, " float4 pos : POSITION;\n"); WRITE(p, " float4 colors[2] : COLOR0;\n"); if (xfregs.numTexGens < 7) { for (int i = 0; i < xfregs.numTexGens; ++i) WRITE(p, " float3 tex%d : TEXCOORD%d;\n", i, i); WRITE(p, " float4 clipPos : TEXCOORD%d;\n", xfregs.numTexGens); } else { // clip position is in w of first 4 texcoords for (int i = 0; i < xfregs.numTexGens; ++i) WRITE(p, " float%d tex%d : TEXCOORD%d;\n", i<4?4:3, i, i); } WRITE(p, "};\n"); WRITE(p, "\n"); // uniforms // bool bTexMtx = ((components & VB_HAS_TEXMTXIDXALL)<= 32 ? (posmtx-32) : posmtx;\n"); WRITE(p, "float3 N0 = "I_NORMALMATRICES".T[normidx].t.xyz, N1 = "I_NORMALMATRICES".T[normidx+1].t.xyz, N2 = "I_NORMALMATRICES".T[normidx+2].t.xyz;\n"); } if (components & VB_HAS_NRM0) WRITE(p, "half3 _norm0 = half3(dot(N0, rawnorm0), dot(N1, rawnorm0), dot(N2, rawnorm0));\n" "half3 norm0 = normalize(_norm0);\n"); if (components & VB_HAS_NRM1) WRITE(p, "half3 _norm1 = half3(dot(N0, rawnorm1), dot(N1, rawnorm1), dot(N2, rawnorm1));\n"); //"half3 norm1 = normalize(_norm1);\n"); if (components & VB_HAS_NRM2) WRITE(p, "half3 _norm2 = half3(dot(N0, rawnorm2), dot(N1, rawnorm2), dot(N2, rawnorm2));\n"); //"half3 norm2 = normalize(_norm2);\n"); } else { WRITE(p, "float4 pos = float4(dot("I_POSNORMALMATRIX".T0, rawpos), dot("I_POSNORMALMATRIX".T1, rawpos), dot("I_POSNORMALMATRIX".T2, rawpos), 1);\n"); if (components & VB_HAS_NRM0) WRITE(p, "half3 _norm0 = half3(dot("I_POSNORMALMATRIX".N0.xyz, rawnorm0), dot("I_POSNORMALMATRIX".N1.xyz, rawnorm0), dot("I_POSNORMALMATRIX".N2.xyz, rawnorm0));\n" "half3 norm0 = normalize(_norm0);\n"); if (components & VB_HAS_NRM1) WRITE(p, "half3 _norm1 = half3(dot("I_POSNORMALMATRIX".N0.xyz, rawnorm1), dot("I_POSNORMALMATRIX".N1.xyz, rawnorm1), dot("I_POSNORMALMATRIX".N2.xyz, rawnorm1));\n"); //"half3 norm1 = normalize(_norm1);\n"); if (components & VB_HAS_NRM2) WRITE(p, "half3 _norm2 = half3(dot("I_POSNORMALMATRIX".N0.xyz, rawnorm2), dot("I_POSNORMALMATRIX".N1.xyz, rawnorm2), dot("I_POSNORMALMATRIX".N2.xyz, rawnorm2));\n"); //"half3 norm2 = normalize(_norm2);\n"); } if (!(components & VB_HAS_NRM0)) WRITE(p, "half3 _norm0 = half3(0,0,0), norm0 = half3(0,0,0);\n"); WRITE(p, "o.pos = float4(dot("I_PROJECTION".T0, pos), dot("I_PROJECTION".T1, pos), dot("I_PROJECTION".T2, pos), dot("I_PROJECTION".T3, pos));\n"); WRITE(p, "half4 mat;\n" // = half4(1,1,1,1), lacc = half4(0,0,0,0);\n" "half3 ldir, h;\n" "half dist, dist2, attn;\n"); // lights/colors for (int j = 0; j < xfregs.nNumChans; j++) { // bool bColorAlphaSame = xfregs.colChans[j].color.hex == xfregs.colChans[j].alpha.hex; unused const LitChannel& color = xfregs.colChans[j].color; const LitChannel& alpha = xfregs.colChans[j].alpha; WRITE(p, "{\n"); WRITE(p, "half4 lacc = half4(1,1,1,1);\n"); if (color.matsource) {// from vertex if (components & (VB_HAS_COL0 << j)) WRITE(p, "mat = color%d;\n", j); else WRITE(p, "mat = half4(1,1,1,1);\n"); } else // from color WRITE(p, "mat = "I_MATERIALS".C%d;\n", j+2); if (color.enablelighting) { if (color.ambsource) { // from vertex if (components & (VB_HAS_COL0< 0 ? max(0.0f, dot(norm0, "I_LIGHTS".lights[%d].dir.xyz)) : 0;\n", index, index); WRITE(p, "ldir = half3(1,attn,attn*attn);\n"); WRITE(p, "attn = max(0.0f, dot("I_LIGHTS".lights[%d].cosatt.xyz, ldir)) / dot("I_LIGHTS".lights[%d].distatt.xyz, ldir);\n", index, index); } switch (chan.diffusefunc) { case LIGHTDIF_NONE: WRITE(p, "%s.%s += attn * "I_LIGHTS".lights[%d].col.%s;\n", dest, swizzle, index, swizzle); break; case LIGHTDIF_SIGN: case LIGHTDIF_CLAMP: WRITE(p, "%s.%s += attn * %sdot(ldir, norm0)) * "I_LIGHTS".lights[%d].col.%s;\n", dest, swizzle, chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0f," :"(", index, swizzle); break; default: _assert_(0); } } WRITE(p, "\n"); return p; }