Advertisement
Guest User

Untitled

a guest
Oct 19th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.11 KB | None | 0 0
  1.  
  2. template<class T>
  3. static void GenerateGenericLightShader(T& out, const char* lightsName, int index, const char* swizzle)
  4. {
  5.  
  6.     out.Write("switch (g_aColorChansLight%d%d[i])\n", swizzle, index);
  7.     out.Write("{\n");
  8.     out.Write("case 0:\n");
  9.     out.Write("{\n");
  10.         out.Write("lacc.%s += " LIGHT_COL";\n", swizzle, LIGHT_COL_PARAMS(lightsName, index, swizzle));
  11.     out.Write("} break;\n");
  12.     out.Write("case 1:\n");
  13.     out.Write("{\n");
  14.         out.Write("ldir = normalize(" LIGHT_POS".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(lightsName, index));
  15.         out.Write("lacc.%s += %sdot(ldir, _norm0)) * " LIGHT_COL";\n",
  16.         swizzle, chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0," : "(", LIGHT_COL_PARAMS(lightsName, index, swizzle));
  17.     out.Write("} break;\n");
  18.     out.Write("case 2:\n");
  19.     out.Write("{\n");
  20.         out.Write("ldir = " LIGHT_POS".xyz - pos.xyz;\n", LIGHT_POS_PARAMS(lightsName, index));
  21.         out.Write("dist2 = dot(ldir, ldir);\n"
  22.         "dist = sqrt(dist2);\n"
  23.         "ldir = ldir / dist;\n"
  24.         "attn = max(0.0, dot(ldir, normalize(" LIGHT_DIR".xyz)));\n",
  25.         LIGHT_DIR_PARAMS(lightsName, index));
  26.         // attn*attn may overflow
  27.         out.Write("attn = max(0.0, " LIGHT_COSATT".x + " LIGHT_COSATT".y*attn + " LIGHT_COSATT".z*attn*attn) / dot(" LIGHT_DISTATT".xyz, float3(1.0,dist,dist2));\n",
  28.         LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_DISTATT_PARAMS(lightsName, index));
  29.     out.Write("} break;\n");
  30.     out.Write("case 3:\n");
  31.     out.Write("{\n");
  32.         // specular
  33.         out.Write("ldir = normalize(" LIGHT_POS".xyz);\n", LIGHT_POS_PARAMS(lightsName, index));
  34.         out.Write("attn = (dot(_norm0,ldir) >= 0.0) ? max(0.0, dot(_norm0, normalize(" LIGHT_DIR".xyz))) : 0.0;\n", LIGHT_DIR_PARAMS(lightsName, index));
  35.         // attn*attn may overflow
  36.         out.Write("attn = max(0.0, " LIGHT_COSATT".x + " LIGHT_COSATT".y*attn + " LIGHT_COSATT".z*attn*attn) / (" LIGHT_DISTATT".x + " LIGHT_DISTATT".y*attn + " LIGHT_DISTATT".z*attn*attn);\n",
  37.         LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_COSATT_PARAMS(lightsName, index),
  38.         LIGHT_DISTATT_PARAMS(lightsName, index), LIGHT_DISTATT_PARAMS(lightsName, index), LIGHT_DISTATT_PARAMS(lightsName, index));
  39.     out.Write("} break;\n");
  40.     out.Write("}\n");
  41.  
  42.     out.Write("switch (g_aColorChansLightDiffuse%d%d[i])\n", swizzle, index);
  43.     out.Write("{\n");
  44.     out.Write("case 0:\n");
  45.     out.Write("{\n");
  46.         out.Write("lacc.%s += attn * " LIGHT_COL";\n", swizzle, LIGHT_COL_PARAMS(lightsName, index, swizzle));
  47.     out.Write("} break;\n");
  48.     out.Write("case 1:\n");
  49.     out.Write("{\n");
  50.         out.Write("lacc.%s += attn * %sdot(ldir, _norm0)) * " LIGHT_COL";\n",
  51.         swizzle,
  52.         chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0," : "(",
  53.         LIGHT_COL_PARAMS(lightsName, index, swizzle));
  54.     out.Write("} break;\n");
  55.     out.Write("}\n");
  56.  
  57. }
  58.  
  59.  
  60.  
  61.  
  62. template<class T>
  63. static void GenerateGenericLightingShader(T& out, int components, const char* materialsName, const char* lightsName, const char* inColorName, const char* dest, int numColorChans)
  64. {
  65.  
  66.     out.Write("vector <int,8> g_aColorChan0;\n");
  67.     out.Write("vector <int,8> g_aColorChan1;\n");
  68.     out.Write("vector <int,8> g_aColorChansLightxyz0;\n");
  69.     out.Write("vector <int,8> g_aColorChansLightxyz1;\n");
  70.     out.Write("vector <int,8> g_aColorChansLightw0;\n");
  71.     out.Write("vector <int,8> g_aColorChansLightw1;\n");
  72.     out.Write("vector <int,8> g_aColorChansLightDiffusexyz0;\n");
  73.     out.Write("vector <int,8> g_aColorChansLightDiffusexyz1;\n");
  74.     out.Write("vector <int,8> g_aColorChansLightDiffusew0;\n");
  75.     out.Write("vector <int,8> g_aColorChansLightDiffuseW1;\n");
  76.    
  77.  
  78.     for (unsigned int j = 0; j < numColorChans; j++)
  79.     {
  80.         out.Write("{\n");
  81.  
  82.         out.Write("switch (g_aColorChan%d[%d])\n", j, 0);
  83.         out.Write("{\n");
  84.         if (components & (VB_HAS_COL0 << j))
  85.         {
  86.             out.Write("case 0:\n");
  87.             out.Write("{\n");
  88.             out.Write("mat =%s%d;\n", inColorName, j);
  89.             out.Write("} break;\n");
  90.         }
  91.         if (components & VB_HAS_COL0)
  92.         {
  93.             out.Write("case 1:n");
  94.             out.Write("{\n");
  95.             out.Write("mat = %s0;\n", inColorName);
  96.             out.Write("} break;\n");
  97.         }
  98.         out.Write("case 2:n");
  99.         out.Write("{\n");
  100.         out.Write("mat = float4(1.0,1.0,1.0,1.0);\n");
  101.         out.Write("} break;\n");
  102.         out.Write("case 3:\n");
  103.         out.Write("{\n");
  104.         out.Write("mat = %s[%d];\n", materialsName, j + 2);
  105.         out.Write("} break;\n");
  106.         out.Write("}\n");
  107.  
  108.         out.Write("switch (g_aColorChan%d[%d])\n", j, 1);
  109.         out.Write("{\n");
  110.         if (components & (VB_HAS_COL0 << j))
  111.         {
  112.             out.Write("case 0:\n");
  113.             out.Write("{\n");
  114.             out.Write("lacc = %s%d;\n", inColorName, j);
  115.             out.Write("} break;\n");
  116.         }
  117.         if (components & VB_HAS_COL0)
  118.         {
  119.             out.Write("case 1:n");
  120.             out.Write("{\n");
  121.             out.Write("lacc = %s0;\n", inColorName);
  122.             out.Write("} break;\n");
  123.         }
  124.         out.Write("case 2:n");
  125.         out.Write("{\n");
  126.         out.Write("lacc = float4(1.0,1.0,1.0,1.0);\n");
  127.         out.Write("} break;\n");
  128.         out.Write("case 3:\n");
  129.         out.Write("{\n");
  130.         out.Write("lacc = %s[%d];\n", materialsName, j);
  131.         out.Write("} break;\n");
  132.         out.Write("case 4:\n");
  133.         out.Write("{\n");
  134.         out.Write("lacc = float4(1.0,1.0,1.0,1.0);\n");
  135.         out.Write("} break;\n");
  136.         out.Write("}\n");
  137.  
  138.         //id -1
  139.         out.Write("switch (g_aColorChan%d[%d])\n", j, 2);
  140.         out.Write("{\n");
  141.         if (components & (VB_HAS_COL0 << j))
  142.         {
  143.             out.Write("case 0:\n");
  144.             out.Write("{\n");
  145.             object.Write("mat.w = %s%d.w;\n", inColorName, j);
  146.             out.Write("} break;\n");
  147.         }
  148.         if (components & VB_HAS_COL0)
  149.         {
  150.             out.Write("case 1:n");
  151.             out.Write("{\n");
  152.             object.Write("mat.w = %s0.w;\n", inColorName);
  153.             out.Write("} break;\n");
  154.         }
  155.         out.Write("case 2:n");
  156.         out.Write("{\n");
  157.         object.Write("mat.w = 1.0;\n");
  158.         out.Write("} break;\n");
  159.         out.Write("case 3:\n");
  160.         out.Write("{\n");
  161.         object.Write("mat.w = %s[%d].w;\n", materialsName, j + 2);
  162.         out.Write("} break;\n");
  163.         out.Write("}\n");
  164.    
  165.         out.Write("switch (g_aColorChan%d[%d])\n", j, 1);
  166.         out.Write("{\n");
  167.         if (components & (VB_HAS_COL0 << j))
  168.         {
  169.             out.Write("case 0:\n");
  170.             out.Write("{\n");
  171.             object.Write("lacc.w = %s%d.w;\n", inColorName, j);
  172.             out.Write("} break;\n");
  173.         }
  174.         if (components & VB_HAS_COL0)
  175.         {
  176.             out.Write("case 1:n");
  177.             out.Write("{\n");
  178.             object.Write("lacc.w = %s0.w;\n", inColorName);
  179.             out.Write("} break;\n");
  180.         }
  181.         out.Write("case 2:n");
  182.         out.Write("{\n");
  183.         object.Write("lacc.w = 1.0;\n");
  184.         out.Write("} break;\n");
  185.         out.Write("case 3:\n");
  186.         out.Write("{\n");
  187.         object.Write("lacc.w = %s[%d].w;\n", materialsName, j);
  188.         out.Write("} break;\n");
  189.         out.Write("case 4:\n");
  190.         out.Write("{\n");
  191.         object.Write("lacc.w = 1.0;\n");
  192.         out.Write("} break;\n");
  193.         out.Write("}\n");
  194.  
  195.  
  196.         out.Write("for( i = 0; i < 8; i++)");
  197.         out.Write("{\n");
  198.             out.Write("if (g_aColorChansLight%d%d[i] != -1)\n", "xyz", j);
  199.             out.Write("{\n");
  200.                         GenerateGenericLightShader(out, lightsName, j, "xyz");
  201.             out.Write("}\n");
  202.             out.Write("if (g_aColorChansLight%d%d[i] != -1)\n", "w", j);
  203.             out.Write("{\n");
  204.                         GenerateGenericLightShader(out, lightsName, j, "w");
  205.             out.Write("}\n");
  206.         out.Write("}\n");
  207.  
  208.         out.Write("%s%d = mat * clamp(lacc,0.0,1.0);\n", dest, j);
  209.  
  210.         out.Write("}\n");
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement