// Copyright (C) 2003-2008 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 "VertexShader.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, u32 zbufrender) { vid.values[0] = components | (xfregs.numTexGens << 23) | (xfregs.nNumChans << 27) | ((u32)xfregs.bEnableDualTexTransform << 29) | (zbufrender << 30); 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); char *GenerateVertexShader(u32 components, bool has_zbuffer_target) { 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(); bool bOutputZ = bpmem.ztex2.op==ZTEXTURE_ADD || has_zbuffer_target; int ztexcoord = -1; 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 outputting Z, embed the Z coordinate in the w component of a texture coordinate // if number of tex gens occupies all the texture coordinates, use the last tex coord // otherwise use the next available tex coord for (int i = 0; i < xfregs.numTexGens; ++i) { WRITE(p, " float%d tex%d : TEXCOORD%d;\n", (i==(xfregs.numTexGens-1)&&bOutputZ)?4:3, i, i); } if (bOutputZ && xfregs.numTexGens == 0) { ztexcoord = 0; WRITE(p, " float4 tex%d : TEXCOORD%d;\n", ztexcoord, ztexcoord); } else if (bOutputZ) ztexcoord = xfregs.numTexGens - 1; 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, lacc;\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"); if (color.matsource) {// from vertex if (components & (VB_HAS_COL0<= 0 ) WRITE(p, "o.tex%d.w = o.pos.z/o.pos.w;\n", ztexcoord); // if (bpmem.fog.c_proj_fsel.fsel != 0) { // switch (bpmem.fog.c_proj_fsel.fsel) { // case 1: // linear // break; // case 4: // exp // break; // case 5: // exp2 // break; // case 6: // backward exp // break; // case 7: // backward exp2 // break; // } // // WRITE(p, "o.fog = o.pos.z/o.pos.w;\n"); // } WRITE(p, "return o;\n}\n"); if (text[sizeof(text) - 1] != 0x7C) PanicAlert("VertexShader generator - buffer too small, canary has been eaten!"); return text; } // coloralpha - 1 if color, 2 if alpha char* GenerateLightShader(char* p, int index, const LitChannel& chan, const char* dest, int coloralpha) { const char* swizzle = "xyzw"; if (coloralpha == 1 ) swizzle = "xyz"; else if (coloralpha == 2 ) swizzle = "w"; if (!(chan.attnfunc & 1)) { // atten disabled switch (chan.diffusefunc) { case LIGHTDIF_NONE: WRITE(p, "%s.%s += "I_LIGHTS".lights[%d].col.%s;\n", dest, swizzle, index, swizzle); break; case LIGHTDIF_SIGN: case LIGHTDIF_CLAMP: WRITE(p, "ldir = normalize("I_LIGHTS".lights[%d].pos.xyz - pos.xyz);\n", index); WRITE(p, "%s.%s += %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); } } else { // spec and spot WRITE(p, "ldir = "I_LIGHTS".lights[%d].pos.xyz - pos.xyz;\n", index); if (chan.attnfunc == 3) { // spot WRITE(p, "dist2 = dot(ldir, ldir);\n" "dist = sqrt(dist2);\n" "ldir = ldir / dist;\n" "attn = max(0.0f, dot(ldir, "I_LIGHTS".lights[%d].dir.xyz));\n",index); WRITE(p, "attn = max(0.0f, dot("I_LIGHTS".lights[%d].cosatt.xyz, half3(1, attn, attn*attn))) / dot("I_LIGHTS".lights[%d].distatt.xyz, half3(1,dist,dist2));\n", index, index); } else if (chan.attnfunc == 1) { // specular WRITE(p, "attn = dot(norm0, "I_LIGHTS".lights[%d].pos.xyz) > 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; }