// 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 "Globals.h" #include #include "Render.h" #include "VertexShader.h" #include "VertexShaderManager.h" #include "BPStructs.h" #include "VertexLoader.h" 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); bool GenerateVertexShader(VERTEXSHADER& vs, u32 components) { 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 || Renderer::GetZBufferTarget()!=0; 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 & VertexLoader::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 & VertexLoader::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 & VertexLoader::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 & VertexLoader::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 & VertexLoader::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 & VertexLoader::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 & VertexLoader::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 & VertexLoader::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= 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\0"); return VertexShaderMngr::CompileVertexShader(vs, 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; }