2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
28 #define GLINFO_LOCATION (*gl_info)
30 /** Prints the GLSL info log which will contain error messages if they exist */
31 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
33 int infologLength = 0;
36 GL_EXTCALL(glGetObjectParameterivARB(obj,
37 GL_OBJECT_INFO_LOG_LENGTH_ARB,
40 /* A size of 1 is just a null-terminated string, so the log should be bigger than
41 * that if there are errors. */
42 if (infologLength > 1)
44 infoLog = (char *)HeapAlloc(GetProcessHeap(), 0, infologLength);
45 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
46 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
47 HeapFree(GetProcessHeap(), 0, infoLog);
52 * Loads (pixel shader) samplers
54 void shader_glsl_load_psamplers(
55 WineD3D_GL_Info *gl_info,
56 IWineD3DStateBlock* iface) {
58 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
59 GLhandleARB programId = stateBlock->glsl_program->programId;
62 char sampler_name[20];
64 for (i=0; i< GL_LIMITS(samplers); ++i) {
65 if (stateBlock->textures[i] != NULL) {
66 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
67 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
69 TRACE_(d3d_shader)("Loading %s for texture %d\n", sampler_name, i);
70 GL_EXTCALL(glUniform1iARB(name_loc, i));
71 checkGLcall("glUniform1iARB");
78 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
79 * When constant_list == NULL, it will load all the constants.
81 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
82 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
83 struct list *constant_list) {
84 local_constant* lconst;
89 if (TRACE_ON(d3d_shader)) {
90 for (i = 0; i < max_constants; ++i) {
91 tmp_loc = constant_locations[i];
93 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
94 constants[i * 4 + 0], constants[i * 4 + 1],
95 constants[i * 4 + 2], constants[i * 4 + 3]);
99 for (i = 0; i < max_constants; ++i) {
100 tmp_loc = constant_locations[i];
102 /* We found this uniform name in the program - go ahead and send the data */
103 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
106 checkGLcall("glUniform4fvARB()");
108 constant_entry *constant;
109 if (TRACE_ON(d3d_shader)) {
110 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
112 tmp_loc = constant_locations[i];
114 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
115 constants[i * 4 + 0], constants[i * 4 + 1],
116 constants[i * 4 + 2], constants[i * 4 + 3]);
120 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
122 tmp_loc = constant_locations[i];
124 /* We found this uniform name in the program - go ahead and send the data */
125 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
128 checkGLcall("glUniform4fvARB()");
131 /* Load immediate constants */
132 if (TRACE_ON(d3d_shader)) {
133 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
134 tmp_loc = constant_locations[lconst->idx];
136 GLfloat* values = (GLfloat*)lconst->value;
137 TRACE("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
138 values[0], values[1], values[2], values[3]);
142 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
143 tmp_loc = constant_locations[lconst->idx];
145 /* We found this uniform name in the program - go ahead and send the data */
146 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
149 checkGLcall("glUniform4fvARB()");
153 * Loads integer constants (aka uniforms) into the currently set GLSL program.
154 * When @constants_set == NULL, it will load all the constants.
156 void shader_glsl_load_constantsI(
157 IWineD3DBaseShaderImpl* This,
158 WineD3D_GL_Info *gl_info,
159 GLhandleARB programId,
160 unsigned max_constants,
162 BOOL* constants_set) {
167 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
168 const char* prefix = is_pshader? "PI":"VI";
171 for (i=0; i<max_constants; ++i) {
172 if (NULL == constants_set || constants_set[i]) {
174 TRACE("Loading constants %i: %i, %i, %i, %i\n",
175 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
177 /* TODO: Benchmark and see if it would be beneficial to store the
178 * locations of the constants to avoid looking up each time */
179 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
180 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
182 /* We found this uniform name in the program - go ahead and send the data */
183 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
184 checkGLcall("glUniform4ivARB");
189 /* Load immediate constants */
190 ptr = list_head(&This->baseShader.constantsI);
192 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
193 unsigned int idx = lconst->idx;
194 GLint* values = (GLint*) lconst->value;
196 TRACE("Loading local constants %i: %i, %i, %i, %i\n", idx,
197 values[0], values[1], values[2], values[3]);
199 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
200 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
202 /* We found this uniform name in the program - go ahead and send the data */
203 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
204 checkGLcall("glUniform4ivARB");
206 ptr = list_next(&This->baseShader.constantsI, ptr);
211 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
212 * When @constants_set == NULL, it will load all the constants.
214 void shader_glsl_load_constantsB(
215 IWineD3DBaseShaderImpl* This,
216 WineD3D_GL_Info *gl_info,
217 GLhandleARB programId,
218 unsigned max_constants,
220 BOOL* constants_set) {
225 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
226 const char* prefix = is_pshader? "PB":"VB";
229 for (i=0; i<max_constants; ++i) {
230 if (NULL == constants_set || constants_set[i]) {
232 TRACE("Loading constants %i: %i;\n", i, constants[i*4]);
234 /* TODO: Benchmark and see if it would be beneficial to store the
235 * locations of the constants to avoid looking up each time */
236 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
237 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
239 /* We found this uniform name in the program - go ahead and send the data */
240 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
241 checkGLcall("glUniform1ivARB");
246 /* Load immediate constants */
247 ptr = list_head(&This->baseShader.constantsB);
249 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
250 unsigned int idx = lconst->idx;
251 GLint* values = (GLint*) lconst->value;
253 TRACE("Loading local constants %i: %i\n", idx, values[0]);
255 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
256 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
258 /* We found this uniform name in the program - go ahead and send the data */
259 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
260 checkGLcall("glUniform1ivARB");
262 ptr = list_next(&This->baseShader.constantsB, ptr);
269 * Loads the app-supplied constants into the currently set GLSL program.
271 void shader_glsl_load_constants(
272 IWineD3DDevice* device,
274 char useVertexShader) {
276 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
277 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
278 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*) deviceImpl->wineD3D)->gl_info;
280 GLhandleARB *constant_locations;
281 struct list *constant_list;
282 GLhandleARB programId;
284 if (!stateBlock->glsl_program) {
285 /* No GLSL program set - nothing to do. */
288 programId = stateBlock->glsl_program->programId;
290 if (useVertexShader) {
291 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
292 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) vshader;
295 IWineD3DVertexDeclarationImpl* vertexDeclaration =
296 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
298 constant_locations = stateBlock->glsl_program->vuniformF_locations;
299 constant_list = &stateBlock->set_vconstantsF;
301 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
302 /* Load DirectX 8 float constants/uniforms for vertex shader */
303 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
304 vertexDeclaration->constants, constant_locations, NULL);
307 /* Load DirectX 9 float constants/uniforms for vertex shader */
308 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
309 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
311 /* Load DirectX 9 integer constants/uniforms for vertex shader */
312 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
313 stateBlock->vertexShaderConstantI,
314 stateBlock->set.vertexShaderConstantsI);
316 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
317 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
318 stateBlock->vertexShaderConstantB,
319 stateBlock->set.vertexShaderConstantsB);
321 /* Upload the position fixup params */
322 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
323 checkGLcall("glGetUniformLocationARB");
324 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
325 checkGLcall("glUniform4fvARB");
328 if (usePixelShader) {
330 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
332 constant_locations = stateBlock->glsl_program->puniformF_locations;
333 constant_list = &stateBlock->set_pconstantsF;
335 /* Load pixel shader samplers */
336 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
338 /* Load DirectX 9 float constants/uniforms for pixel shader */
339 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
340 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
342 /* Load DirectX 9 integer constants/uniforms for pixel shader */
343 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
344 stateBlock->pixelShaderConstantI,
345 stateBlock->set.pixelShaderConstantsI);
347 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
348 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
349 stateBlock->pixelShaderConstantB,
350 stateBlock->set.pixelShaderConstantsB);
354 /** Generate the variable & register declarations for the GLSL output target */
355 void shader_generate_glsl_declarations(
356 IWineD3DBaseShader *iface,
357 shader_reg_maps* reg_maps,
358 SHADER_BUFFER* buffer,
359 WineD3D_GL_Info* gl_info) {
361 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
364 /* There are some minor differences between pixel and vertex shaders */
365 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
366 char prefix = pshader ? 'P' : 'V';
368 /* Prototype the subroutines */
369 for (i = 0; i < This->baseShader.limits.label; i++) {
370 if (reg_maps->labels[i])
371 shader_addline(buffer, "void subroutine%lu();\n", i);
374 /* Declare the constants (aka uniforms) */
375 if (This->baseShader.limits.constant_float > 0) {
376 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
377 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
378 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
381 if (This->baseShader.limits.constant_int > 0)
382 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
384 if (This->baseShader.limits.constant_bool > 0)
385 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
388 shader_addline(buffer, "uniform vec4 posFixup;\n");
390 /* Declare texture samplers */
391 for (i = 0; i < This->baseShader.limits.sampler; i++) {
392 if (reg_maps->samplers[i]) {
394 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
398 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
401 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
403 case WINED3DSTT_CUBE:
404 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
406 case WINED3DSTT_VOLUME:
407 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
410 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
411 FIXME("Unrecognized sampler type: %#x\n", stype);
417 /* Declare address variables */
418 for (i = 0; i < This->baseShader.limits.address; i++) {
419 if (reg_maps->address[i])
420 shader_addline(buffer, "ivec4 A%d;\n", i);
423 /* Declare texture coordinate temporaries and initialize them */
424 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
425 if (reg_maps->texcoord[i])
426 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
429 /* Declare input register temporaries */
430 for (i=0; i < This->baseShader.limits.packed_input; i++) {
431 if (reg_maps->packed_input[i])
432 shader_addline(buffer, "vec4 IN%lu;\n", i);
435 /* Declare output register temporaries */
436 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
437 if (reg_maps->packed_output[i])
438 shader_addline(buffer, "vec4 OUT%lu;\n", i);
441 /* Declare temporary variables */
442 for(i = 0; i < This->baseShader.limits.temporary; i++) {
443 if (reg_maps->temporary[i])
444 shader_addline(buffer, "vec4 R%lu;\n", i);
447 /* Declare attributes */
448 for (i = 0; i < This->baseShader.limits.attributes; i++) {
449 if (reg_maps->attributes[i])
450 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
453 /* Declare loop register aL */
454 if (reg_maps->loop) {
455 shader_addline(buffer, "int aL;\n");
456 shader_addline(buffer, "int tmpInt;\n");
459 /* Temporary variables for matrix operations */
460 shader_addline(buffer, "vec4 tmp0;\n");
461 shader_addline(buffer, "vec4 tmp1;\n");
463 /* Start the main program */
464 shader_addline(buffer, "void main() {\n");
467 /*****************************************************************************
468 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
470 * For more information, see http://wiki.winehq.org/DirectX-Shaders
471 ****************************************************************************/
474 static void shader_glsl_add_param(
475 SHADER_OPCODE_ARG* arg,
477 const DWORD addr_token,
483 /** Used for opcode modifiers - They multiply the result by the specified amount */
484 static const char* shift_glsl_tab[] = {
486 "2.0 * ", /* 1 (x2) */
487 "4.0 * ", /* 2 (x4) */
488 "8.0 * ", /* 3 (x8) */
489 "16.0 * ", /* 4 (x16) */
490 "32.0 * ", /* 5 (x32) */
497 "0.0625 * ", /* 12 (d16) */
498 "0.125 * ", /* 13 (d8) */
499 "0.25 * ", /* 14 (d4) */
500 "0.5 * " /* 15 (d2) */
503 /** Print the beginning of the generated GLSL string. example: "reg_name.xyzw = vec4("
504 * Will also change the reg_mask if necessary (not all register types are equal in DX vs GL) */
505 static void shader_glsl_add_dst(DWORD param, const char* reg_name, char* reg_mask, char* outStr) {
507 int shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
510 if ((shader_get_regtype(param) == WINED3DSPR_RASTOUT)
511 && ((param & WINED3DSP_REGNUM_MASK) != 0)) {
512 /* gl_FogFragCoord or glPointSize - both floats */
513 strcpy(cast, "float");
514 strcpy(reg_mask, "");
516 } else if (reg_name[0] == 'A') {
517 /* Address register for vertex shaders (ivec4) */
518 strcpy(cast, "ivec4");
521 /* Everything else should be a 4 component float vector */
522 strcpy(cast, "vec4");
525 sprintf(outStr, "%s%s = %s%s(", reg_name, reg_mask, shift_glsl_tab[shift], cast);
528 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
529 static void shader_glsl_gen_modifier (
532 const char *in_regswizzle,
537 if (instr == WINED3DSIO_TEXKILL)
540 switch (instr & WINED3DSP_SRCMOD_MASK) {
541 case WINED3DSPSM_NONE:
542 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
544 case WINED3DSPSM_NEG:
545 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
547 case WINED3DSPSM_NOT:
548 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
550 case WINED3DSPSM_BIAS:
551 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
553 case WINED3DSPSM_BIASNEG:
554 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
556 case WINED3DSPSM_SIGN:
557 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
559 case WINED3DSPSM_SIGNNEG:
560 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
562 case WINED3DSPSM_COMP:
563 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
566 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
568 case WINED3DSPSM_X2NEG:
569 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
571 case WINED3DSPSM_DZ: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
572 sprintf(out_str, "vec4(%s.r / %s.b, %s.g / %s.b, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
575 sprintf(out_str, "vec4(%s.r / %s.a, %s.g / %s.a, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
577 case WINED3DSPSM_ABS:
578 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
580 case WINED3DSPSM_ABSNEG:
581 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
584 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
585 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
589 /** Writes the GLSL variable name that corresponds to the register that the
590 * DX opcode parameter is trying to access */
591 static void shader_glsl_get_register_name(
593 const DWORD addr_token,
596 SHADER_OPCODE_ARG* arg) {
598 /* oPos, oFog and oPts in D3D */
599 const char* hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
601 DWORD reg = param & WINED3DSP_REGNUM_MASK;
602 DWORD regtype = shader_get_regtype(param);
603 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
604 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
605 WineD3D_GL_Info* gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
607 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
613 case WINED3DSPR_TEMP:
614 sprintf(tmpStr, "R%u", reg);
616 case WINED3DSPR_INPUT:
618 /* Pixel shaders >= 3.0 */
619 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
620 sprintf(tmpStr, "IN%u", reg);
623 strcpy(tmpStr, "gl_Color");
625 strcpy(tmpStr, "gl_SecondaryColor");
628 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
630 sprintf(tmpStr, "attrib%u", reg);
633 case WINED3DSPR_CONST:
635 const char* prefix = pshader? "PC":"VC";
637 /* Relative addressing */
638 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
640 /* Relative addressing on shaders 2.0+ have a relative address token,
641 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
642 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
643 char relStr[100], relReg[50], relMask[6];
644 shader_glsl_add_param(arg, addr_token, 0, TRUE, relReg, relMask, relStr);
645 sprintf(tmpStr, "%s[%s + %u]", prefix, relStr, reg);
647 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
650 sprintf(tmpStr, "%s[%u]", prefix, reg);
654 case WINED3DSPR_CONSTINT:
656 sprintf(tmpStr, "PI[%u]", reg);
658 sprintf(tmpStr, "VI[%u]", reg);
660 case WINED3DSPR_CONSTBOOL:
662 sprintf(tmpStr, "PB[%u]", reg);
664 sprintf(tmpStr, "VB[%u]", reg);
666 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
668 sprintf(tmpStr, "T%u", reg);
670 sprintf(tmpStr, "A%u", reg);
673 case WINED3DSPR_LOOP:
674 sprintf(tmpStr, "aL");
676 case WINED3DSPR_SAMPLER:
678 sprintf(tmpStr, "Psampler%u", reg);
680 sprintf(tmpStr, "Vsampler%u", reg);
682 case WINED3DSPR_COLOROUT:
683 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
684 sprintf(tmpStr, "gl_FragData[%u]", reg);
686 /* TODO: See GL_ARB_draw_buffers */
687 FIXME("Unsupported write to render target %u\n", reg);
689 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
691 WARN("This OpenGL implementation doesn't support writing to multiple render targets!\n");
693 sprintf(tmpStr, "gl_FragColor");
696 case WINED3DSPR_RASTOUT:
697 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
699 case WINED3DSPR_DEPTHOUT:
700 sprintf(tmpStr, "gl_FragDepth");
702 case WINED3DSPR_ATTROUT:
704 sprintf(tmpStr, "gl_FrontColor");
706 sprintf(tmpStr, "gl_FrontSecondaryColor");
709 case WINED3DSPR_TEXCRDOUT:
710 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
711 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
712 sprintf(tmpStr, "OUT%u", reg);
714 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
717 FIXME("Unhandled register name Type(%d)\n", regtype);
718 sprintf(tmpStr, "unrecognized_register");
722 strcat(regstr, tmpStr);
725 /* Writes the GLSL writemask for the destination register */
726 static void shader_glsl_get_output_register_swizzle(
731 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
732 strcat(write_mask, ".");
733 if (param & WINED3DSP_WRITEMASK_0) strcat(write_mask, "x");
734 if (param & WINED3DSP_WRITEMASK_1) strcat(write_mask, "y");
735 if (param & WINED3DSP_WRITEMASK_2) strcat(write_mask, "z");
736 if (param & WINED3DSP_WRITEMASK_3) strcat(write_mask, "w");
740 static void shader_glsl_get_input_register_swizzle(
745 const char swizzle_reg_chars_color_fix[] = "zyxw";
746 const char swizzle_reg_chars[] = "xyzw";
747 const char* swizzle_regs = NULL;
750 DWORD swizzle = (param & WINED3DVS_SWIZZLE_MASK) >> WINED3DVS_SWIZZLE_SHIFT;
751 DWORD swizzle_x = swizzle & 0x03;
752 DWORD swizzle_y = (swizzle >> 2) & 0x03;
753 DWORD swizzle_z = (swizzle >> 4) & 0x03;
754 DWORD swizzle_w = (swizzle >> 6) & 0x03;
757 swizzle_regs = swizzle_reg_chars_color_fix;
759 swizzle_regs = swizzle_reg_chars;
763 * swizzle bits fields:
766 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) == swizzle) {
768 sprintf(reg_mask, ".%c%c%c%c",
769 swizzle_regs[swizzle_x],
770 swizzle_regs[swizzle_y],
771 swizzle_regs[swizzle_z],
772 swizzle_regs[swizzle_w]);
776 if (swizzle_x == swizzle_y &&
777 swizzle_x == swizzle_z &&
778 swizzle_x == swizzle_w)
780 sprintf(reg_mask, ".%c", swizzle_regs[swizzle_x]);
782 sprintf(reg_mask, ".%c%c%c%c",
783 swizzle_regs[swizzle_x],
784 swizzle_regs[swizzle_y],
785 swizzle_regs[swizzle_z],
786 swizzle_regs[swizzle_w]);
790 /** From a given parameter token, generate the corresponding GLSL string.
791 * Also, return the actual register name and swizzle in case the
792 * caller needs this information as well. */
793 static void shader_glsl_add_param(
794 SHADER_OPCODE_ARG* arg,
796 const DWORD addr_token,
802 BOOL is_color = FALSE;
803 reg_mask[0] = reg_name[0] = out_str[0] = 0;
805 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
808 shader_glsl_get_input_register_swizzle(param, is_color, reg_mask);
809 shader_glsl_gen_modifier(param, reg_name, reg_mask, out_str);
811 shader_glsl_get_output_register_swizzle(param, reg_mask);
812 sprintf(out_str, "%s%s", reg_name, reg_mask);
816 /** Process GLSL instruction modifiers */
817 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
819 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
821 if (arg->opcode->dst_token && mask != 0) {
826 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
828 if (mask & WINED3DSPDM_SATURATE) {
829 /* _SAT means to clamp the value of the register to between 0 and 1 */
830 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
832 if (mask & WINED3DSPDM_MSAMPCENTROID) {
833 FIXME("_centroid modifier not handled\n");
835 if (mask & WINED3DSPDM_PARTIALPRECISION) {
836 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
841 static inline const char* shader_get_comp_op(
842 const DWORD opcode) {
844 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
846 case COMPARISON_GT: return ">";
847 case COMPARISON_EQ: return "==";
848 case COMPARISON_GE: return ">=";
849 case COMPARISON_LT: return "<";
850 case COMPARISON_NE: return "!=";
851 case COMPARISON_LE: return "<=";
853 FIXME("Unrecognized comparison value: %u\n", op);
858 /*****************************************************************************
860 * Begin processing individual instruction opcodes
862 ****************************************************************************/
864 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
865 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
867 CONST SHADER_OPCODE* curOpcode = arg->opcode;
868 SHADER_BUFFER* buffer = arg->buffer;
870 char dst_reg[50], src0_reg[50], src1_reg[50];
871 char dst_mask[6], src0_mask[6], src1_mask[6];
872 char dst_str[100], src0_str[100], src1_str[100];
874 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
875 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
876 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
877 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
878 strcat(tmpLine, "vec4(");
879 strcat(tmpLine, src0_str);
880 strcat(tmpLine, ")");
882 /* Determine the GLSL operator to use based on the opcode */
883 switch (curOpcode->opcode) {
884 case WINED3DSIO_MUL: strcat(tmpLine, " * "); break;
885 case WINED3DSIO_ADD: strcat(tmpLine, " + "); break;
886 case WINED3DSIO_SUB: strcat(tmpLine, " - "); break;
888 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
891 shader_addline(buffer, "%svec4(%s))%s;\n", tmpLine, src1_str, dst_mask);
894 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
895 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
897 SHADER_BUFFER* buffer = arg->buffer;
899 char dst_str[100], src0_str[100];
900 char dst_reg[50], src0_reg[50];
901 char dst_mask[6], src0_mask[6];
903 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
904 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
905 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
906 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
909 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
910 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
912 CONST SHADER_OPCODE* curOpcode = arg->opcode;
913 SHADER_BUFFER* buffer = arg->buffer;
915 char dst_str[100], src0_str[100], src1_str[100];
916 char dst_reg[50], src0_reg[50], src1_reg[50];
917 char dst_mask[6], src0_mask[6], src1_mask[6];
920 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
921 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
922 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
924 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpDest);
926 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
927 if (curOpcode->opcode == WINED3DSIO_DP4)
928 strcpy(cast, "vec4(");
930 strcpy(cast, "vec3(");
932 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
933 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
936 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
937 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
939 CONST SHADER_OPCODE* curOpcode = arg->opcode;
940 SHADER_BUFFER* buffer = arg->buffer;
942 char dst_str[100], src_str[100];
943 char dst_reg[50], src_reg[50];
944 char dst_mask[6], src_mask[6];
947 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
949 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
951 /* Determine the GLSL function to use based on the opcode */
952 /* TODO: Possibly make this a table for faster lookups */
953 switch (curOpcode->opcode) {
954 case WINED3DSIO_MIN: strcat(tmpLine, "min"); break;
955 case WINED3DSIO_MAX: strcat(tmpLine, "max"); break;
956 case WINED3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
957 case WINED3DSIO_ABS: strcat(tmpLine, "abs"); break;
958 case WINED3DSIO_FRC: strcat(tmpLine, "fract"); break;
959 case WINED3DSIO_POW: strcat(tmpLine, "pow"); break;
960 case WINED3DSIO_CRS: strcat(tmpLine, "cross"); break;
961 case WINED3DSIO_NRM: strcat(tmpLine, "normalize"); break;
962 case WINED3DSIO_LOGP:
963 case WINED3DSIO_LOG: strcat(tmpLine, "log2"); break;
964 case WINED3DSIO_EXP: strcat(tmpLine, "exp2"); break;
965 case WINED3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
966 case WINED3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
967 case WINED3DSIO_SGN: strcat(tmpLine, "sign"); break;
969 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
973 strcat(tmpLine, "(");
975 if (curOpcode->num_params > 0) {
976 strcat(tmpLine, "vec4(");
977 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
978 strcat(tmpLine, src_str);
979 strcat(tmpLine, ")");
980 for (i = 2; i < curOpcode->num_params; ++i) {
981 strcat(tmpLine, ", vec4(");
982 shader_glsl_add_param(arg, arg->src[i-1], arg->src_addr[i-1], TRUE, src_reg, src_mask, src_str);
983 strcat(tmpLine, src_str);
984 strcat(tmpLine, ")");
987 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
991 /** Process the WINED3DSIO_EXPP instruction in GLSL:
992 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
993 * dst.x = 2^(floor(src))
994 * dst.y = src - floor(src)
995 * dst.z = 2^src (partial precision is allowed, but optional)
997 * For 2.0 shaders, just do this (honoring writemask and swizzle):
998 * dst = 2^src; (partial precision is allowed, but optional)
1000 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1003 char dst_str[100], src_str[100];
1004 char dst_reg[50], src_reg[50];
1005 char dst_mask[6], src_mask[6];
1006 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1007 DWORD hex_version = This->baseShader.hex_version;
1009 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1010 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1011 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1013 if (hex_version < WINED3DPS_VERSION(2,0)) {
1014 shader_addline(arg->buffer, "tmp0.x = vec4(exp2(floor(%s))).x;\n", src_str);
1015 shader_addline(arg->buffer, "tmp0.y = vec4(%s - floor(%s)).y;\n", src_str, src_str);
1016 shader_addline(arg->buffer, "tmp0.z = vec4(exp2(%s)).x;\n", src_str);
1017 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1018 shader_addline(arg->buffer, "%svec4(tmp0))%s;\n", tmpLine, dst_mask);
1020 shader_addline(arg->buffer, "%svec4(exp2(%s)))%s;\n", tmpLine, src_str, dst_mask);
1024 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1025 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1028 char dst_str[100], src_str[100];
1029 char dst_reg[50], src_reg[50];
1030 char dst_mask[6], src_mask[6];
1032 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1033 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1034 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1035 strcat(tmpLine, "1.0 / ");
1036 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
1039 /** Process signed comparison opcodes in GLSL. */
1040 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1043 char dst_str[100], src0_str[100], src1_str[100];
1044 char dst_reg[50], src0_reg[50], src1_reg[50];
1045 char dst_mask[6], src0_mask[6], src1_mask[6];
1047 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1048 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1049 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1051 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
1052 if (strlen(src0_mask) != 2) {
1053 shader_glsl_map2gl(arg);
1057 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1059 switch (arg->opcode->opcode) {
1060 case WINED3DSIO_SLT: strcpy(compareStr, "<"); break;
1061 case WINED3DSIO_SGE: strcpy(compareStr, ">="); break;
1063 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1065 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
1066 tmpLine, src0_str, compareStr, src1_str, dst_mask);
1070 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
1071 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1074 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1075 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1076 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1078 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1079 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1080 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1081 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1083 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1084 shader_addline(arg->buffer, "%smix(vec4(%s), vec4(%s), vec4(lessThan(vec4(%s), vec4(0.0)))))%s;\n",
1085 tmpLine, src1_str, src2_str, src0_str, dst_mask);
1088 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
1089 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1092 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1093 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1094 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1096 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1097 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1098 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1099 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1100 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1101 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
1102 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1105 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1106 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1109 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1110 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1111 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1113 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1114 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1115 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1116 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1117 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1119 shader_addline(arg->buffer, "%s(vec4(%s) * vec4(%s)) + vec4(%s))%s;\n",
1120 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1123 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1124 Vertex shaders to GLSL codes */
1125 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1127 int nComponents = 0;
1128 SHADER_OPCODE_ARG tmpArg;
1130 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1132 /* Set constants for the temporary argument */
1133 tmpArg.shader = arg->shader;
1134 tmpArg.buffer = arg->buffer;
1135 tmpArg.src[0] = arg->src[0];
1136 tmpArg.src_addr[0] = arg->src_addr[0];
1137 tmpArg.src_addr[1] = arg->src_addr[1];
1138 tmpArg.reg_maps = arg->reg_maps;
1140 switch(arg->opcode->opcode) {
1141 case WINED3DSIO_M4x4:
1143 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[WINED3DSIO_DP4];
1145 case WINED3DSIO_M4x3:
1147 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[WINED3DSIO_DP4];
1149 case WINED3DSIO_M3x4:
1151 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[WINED3DSIO_DP3];
1153 case WINED3DSIO_M3x3:
1155 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[WINED3DSIO_DP3];
1157 case WINED3DSIO_M3x2:
1159 tmpArg.opcode = &IWineD3DVertexShaderImpl_shader_ins[WINED3DSIO_DP3];
1165 for (i = 0; i < nComponents; i++) {
1166 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1167 tmpArg.src[1] = arg->src[1]+i;
1168 shader_glsl_dot(&tmpArg);
1173 The LRP instruction performs a component-wise linear interpolation
1174 between the second and third operands using the first operand as the
1175 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
1177 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1180 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1181 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1182 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1184 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1185 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1186 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1187 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1189 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1191 shader_addline(arg->buffer, "%s%s + %s * (%s - %s))%s;\n",
1192 tmpLine, src2_str, src0_str, src1_str, src2_str, dst_mask);
1195 /** Process the WINED3DSIO_LIT instruction in GLSL:
1196 * dst.x = dst.w = 1.0
1197 * dst.y = (src0.x > 0) ? src0.x
1198 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1199 * where src.w is clamped at +- 128
1201 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1203 char dst_str[100], src0_str[100];
1204 char dst_reg[50], src0_reg[50];
1205 char dst_mask[6], src0_mask[6];
1207 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1208 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1210 shader_addline(arg->buffer,
1211 "%s = vec4(1.0, (%s.x > 0.0 ? %s.x : 0.0), (%s.x > 0.0 ? ((%s.y > 0.0) ? pow(%s.y, clamp(%s.w, -128.0, 128.0)) : 0.0) : 0.0), 1.0)%s;\n",
1212 dst_str, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, dst_mask);
1215 /** Process the WINED3DSIO_DST instruction in GLSL:
1217 * dst.y = src0.x * src0.y
1221 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1223 char dst_str[100], src0_str[100], src1_str[100];
1224 char dst_reg[50], src0_reg[50], src1_reg[50];
1225 char dst_mask[6], src0_mask[6], src1_mask[6];
1227 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1228 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1229 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1231 shader_addline(arg->buffer, "%s = vec4(1.0, %s.x * %s.y, %s.z, %s.w)%s;\n",
1232 dst_str, src0_reg, src1_reg, src0_reg, src1_reg, dst_mask);
1235 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1236 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1237 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1239 * dst.x = cos(src0.?)
1240 * dst.y = sin(src0.?)
1244 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1246 char dst_str[100], src0_str[100];
1247 char dst_reg[50], src0_reg[50];
1248 char dst_mask[6], src0_mask[6];
1250 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1251 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1253 shader_addline(arg->buffer, "%s = vec4(cos(%s), sin(%s), %s.z, %s.w)%s;\n",
1254 dst_str, src0_str, src0_str, dst_reg, dst_reg, dst_mask);
1257 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1258 * Start a for() loop where src0.y is the initial value of aL,
1259 * increment aL by src0.z for a total of src0.x iterations.
1260 * Need to use a temporary variable for this operation.
1262 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1268 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1270 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1271 src1_reg, src1_reg, src1_reg);
1274 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1275 shader_addline(arg->buffer, "}\n");
1278 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1284 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1285 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s.x; tmpInt++) {\n", src0_reg);
1288 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1294 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1295 shader_addline(arg->buffer, "if (%s) {\n", src0_str);
1298 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1300 char src0_str[100], src1_str[100];
1301 char src0_reg[50], src1_reg[50];
1302 char src0_mask[6], src1_mask[6];
1304 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1305 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1307 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1308 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1311 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1312 shader_addline(arg->buffer, "} else {\n");
1315 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1316 shader_addline(arg->buffer, "break;\n");
1319 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1321 char src0_str[100], src1_str[100];
1322 char src0_reg[50], src1_reg[50];
1323 char src0_mask[6], src1_mask[6];
1325 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1326 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1328 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1329 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1332 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1334 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1335 shader_addline(arg->buffer, "}\n");
1336 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1339 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1340 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1341 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1344 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1350 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1351 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1352 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_str, snum);
1355 /*********************************************
1356 * Pixel Shader Specific Code begins here
1357 ********************************************/
1358 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1360 /* FIXME: Make this work for more than just 2D textures */
1362 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1363 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1365 SHADER_BUFFER* buffer = arg->buffer;
1366 DWORD hex_version = This->baseShader.hex_version;
1368 char dst_str[100], dst_reg[50], dst_mask[6];
1369 char coord_str[100], coord_reg[50], coord_mask[6];
1370 char sampler_str[100], sampler_reg[50], sampler_mask[6];
1371 DWORD reg_dest_code = arg->dst & WINED3DSP_REGNUM_MASK;
1372 DWORD sampler_code, sampler_type;
1374 /* All versions have a destination register */
1375 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1377 /* 1.0-1.3: Use destination register as coordinate source.
1378 1.4+: Use provided coordinate source register. */
1379 if (hex_version < WINED3DPS_VERSION(1,4))
1380 strcpy(coord_reg, dst_reg);
1382 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, coord_reg, coord_mask, coord_str);
1384 /* 1.0-1.4: Use destination register as coordinate source.
1385 * 2.0+: Use provided coordinate source register. */
1386 if (hex_version < WINED3DPS_VERSION(2,0)) {
1387 sprintf(sampler_str, "Psampler%u", reg_dest_code);
1388 sampler_code = reg_dest_code;
1391 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, sampler_reg, sampler_mask, sampler_str);
1392 sampler_code = arg->src[1] & WINED3DSP_REGNUM_MASK;
1395 sampler_type = arg->reg_maps->samplers[sampler_code] & WINED3DSP_TEXTURETYPE_MASK;
1396 if(deviceImpl->stateBlock->textureState[sampler_code][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
1397 switch(sampler_type) {
1400 shader_addline(buffer, "%s = texture2DProj(%s, %s);\n", dst_str, sampler_str, coord_reg);
1402 case WINED3DSTT_CUBE:
1403 shader_addline(buffer, "%s = textureCubeProj(%s, %s);\n", dst_str, sampler_str, coord_reg);
1405 case WINED3DSTT_VOLUME:
1406 shader_addline(buffer, "%s = texture3DProj(%s, %s);\n", dst_str, sampler_str, coord_reg);
1409 shader_addline(buffer, "%s = unrecognized_stype(%s, %s.stp);\n", dst_str, sampler_str, coord_reg);
1410 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1414 switch(sampler_type) {
1417 shader_addline(buffer, "%s = texture2D(%s, %s.st);\n", dst_str, sampler_str, coord_reg);
1419 case WINED3DSTT_CUBE:
1420 shader_addline(buffer, "%s = textureCube(%s, %s.stp);\n", dst_str, sampler_str, coord_reg);
1422 case WINED3DSTT_VOLUME:
1423 shader_addline(buffer, "%s = texture3D(%s, %s.stp);\n", dst_str, sampler_str, coord_reg);
1426 shader_addline(buffer, "%s = unrecognized_stype(%s, %s.stp);\n", dst_str, sampler_str, coord_reg);
1427 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1433 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1435 /* FIXME: Make this work for more than just 2D textures */
1437 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1438 SHADER_BUFFER* buffer = arg->buffer;
1439 DWORD hex_version = This->baseShader.hex_version;
1446 shader_glsl_add_param(arg, arg->dst, 0, FALSE, tmpReg, tmpMask, tmpStr);
1448 if (hex_version != WINED3DPS_VERSION(1,4)) {
1449 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1450 shader_addline(buffer, "%s = clamp(gl_TexCoord[%u], 0.0, 1.0);\n", tmpReg, reg);
1452 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1453 shader_addline(buffer, "%s = gl_TexCoord[%u]%s;\n", tmpStr, reg2, tmpMask);
1457 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1458 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1459 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1460 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1462 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1463 char src0_str[100], dst_str[100];
1464 char src0_name[50], dst_name[50];
1465 char src0_mask[6], dst_mask[6];
1467 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1468 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1470 shader_addline(arg->buffer, "tmp0.x = dot(vec3(gl_TexCoord[%u]), vec3(%s));\n", dstreg, src0_str);
1471 shader_addline(arg->buffer, "%s = vec4(texture1D(Psampler%u, tmp0.x))%s;\n", dst_str, dstreg, dst_mask);
1474 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1475 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1476 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1478 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1479 char src0_str[100], dst_str[100];
1480 char src0_name[50], dst_name[50];
1481 char src0_mask[6], dst_mask[6];
1483 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1484 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1486 shader_addline(arg->buffer, "%s = vec4(dot(vec3(T%u), vec3(%s)))%s;\n",
1487 dst_str, dstreg, src0_str, dst_mask);
1490 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1491 * Calculate the depth as dst.x / dst.y */
1492 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1498 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1500 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_reg, dst_reg);
1503 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1504 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1505 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1506 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1508 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1510 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1511 char src0_str[100], dst_str[100];
1512 char src0_name[50], dst_name[50];
1513 char src0_mask[6], dst_mask[6];
1515 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1516 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1518 shader_addline(arg->buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", dstreg, src0_str);
1519 shader_addline(arg->buffer, "gl_FragDepth = vec4((tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y)%s;\n", dst_str, dst_name);
1522 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1523 * Calculate the 1st of a 2-row matrix multiplication. */
1524 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1526 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1527 SHADER_BUFFER* buffer = arg->buffer;
1532 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1533 shader_addline(buffer, "tmp0.x = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1536 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1537 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1538 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1540 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1541 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1542 SHADER_BUFFER* buffer = arg->buffer;
1543 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1548 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1549 shader_addline(buffer, "tmp0.%c = dot(vec3(T%u), vec3(%s));\n", 'x' + current_state->current_row, reg, src0_str);
1550 current_state->texcoord_w[current_state->current_row++] = reg;
1553 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1555 /* FIXME: Make this work for more than just 2D textures */
1557 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1558 SHADER_BUFFER* buffer = arg->buffer;
1563 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1564 shader_addline(buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1565 shader_addline(buffer, "T%u = texture2D(Psampler%u, tmp0.st);\n", reg, reg);
1568 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1569 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculate coordinates */
1570 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1576 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1577 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1578 DWORD stype = arg->reg_maps->samplers[src0_regnum] & WINED3DSP_TEXTURETYPE_MASK;
1579 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1580 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1583 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1584 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1585 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1587 strcpy(dimensions, "");
1588 FIXME("Unrecognized sampler type: %#x\n", stype);
1592 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1593 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1594 shader_addline(arg->buffer, "T%u = texture%s(Psampler%u, tmp0.%s);\n",
1595 reg, dimensions, reg, (stype == WINED3DSTT_2D) ? "xy" : "xyz");
1596 current_state->current_row = 0;
1599 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1600 * Perform the 3rd row of a 3x3 matrix multiply */
1601 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
1606 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1607 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1608 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1610 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1612 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1613 shader_addline(arg->buffer, "T%u = vec4(tmp0.x, tmp0.y, tmp0.z, 1.0);\n", reg);
1614 current_state->current_row = 0;
1617 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1618 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1619 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1621 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1622 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1624 char src0_str[100], src0_name[50], src0_mask[6];
1625 char src1_str[100], src1_name[50], src1_mask[6];
1626 SHADER_BUFFER* buffer = arg->buffer;
1627 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1628 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1631 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1632 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1633 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1635 strcpy(dimensions, "");
1636 FIXME("Unrecognized sampler type: %#x\n", stype);
1640 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1641 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_name, src1_mask, src1_str);
1643 /* Perform the last matrix multiply operation */
1644 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1646 /* Calculate reflection vector */
1647 shader_addline(buffer, "tmp0.xyz = reflect(-vec3(%s), vec3(tmp0));\n", src1_str);
1649 /* Sample the texture */
1650 shader_addline(buffer, "T%u = texture%s(Psampler%u, tmp0.%s);\n",
1651 reg, dimensions, reg, (stype == WINED3DSTT_2D) ? "xy" : "xyz");
1652 current_state->current_row = 0;
1655 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1656 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1657 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1659 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1660 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1661 SHADER_BUFFER* buffer = arg->buffer;
1662 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1663 char src0_str[100], src0_name[50], src0_mask[6];
1665 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1667 /* Perform the last matrix multiply operation */
1668 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1670 /* Construct the eye-ray vector from w coordinates */
1671 shader_addline(buffer, "tmp1.x = gl_TexCoord[%u].w;\n", current_state->texcoord_w[0]);
1672 shader_addline(buffer, "tmp1.y = gl_TexCoord[%u].w;\n", current_state->texcoord_w[1]);
1673 shader_addline(buffer, "tmp1.z = gl_TexCoord[%u].w;\n", reg);
1675 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
1676 shader_addline(buffer, "tmp0.x = dot(vec3(tmp0), vec3(tmp1));\n");
1677 shader_addline(buffer, "tmp0 = tmp0.w * tmp0;\n");
1678 shader_addline(buffer, "tmp0 = (2.0 * tmp0) - tmp1;\n");
1681 * We don't really know if a Cube or a Volume texture is being sampled, but since Cube textures
1682 * are used more commonly, we'll default to that.
1683 * We probably need to push back the pixel shader generation code until drawPrimitive() for
1684 * shader versions < 2.0, since that's the only time we can guarantee that we're sampling
1685 * the correct type of texture because we can lookup what textures are bound at that point.
1687 shader_addline(buffer, "T%u = textureCube(Psampler%u, tmp0.xyz);\n", reg, reg);
1688 current_state->current_row = 0;
1691 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1692 * Apply a fake bump map transform.
1693 * FIXME: Should apply the BUMPMAPENV matrix. For now, just sample the texture */
1694 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1696 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1697 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1699 FIXME("Not applying the BUMPMAPENV matrix for pixel shader instruction texbem.\n");
1700 shader_addline(arg->buffer, "T%u = texture2D(Psampler%u, gl_TexCoord[%u].xy + T%u.xy);\n",
1701 reg1, reg1, reg1, reg2);
1704 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1705 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1706 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1709 char dst_str[100], src0_str[100];
1710 char dst_reg[50], src0_reg[50];
1711 char dst_mask[6], src0_mask[6];
1712 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1714 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1715 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1717 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1718 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1719 tmpLine, src0_regnum, dst_reg, dst_mask);
1722 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1723 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1724 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1727 char dst_str[100], src0_str[100];
1728 char dst_reg[50], src0_reg[50];
1729 char dst_mask[6], src0_mask[6];
1730 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1732 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1733 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1735 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1736 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1737 tmpLine, src0_regnum, dst_reg, dst_mask);
1740 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
1741 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
1742 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1745 char dst_str[100], src0_str[100];
1746 char dst_reg[50], src0_reg[50];
1747 char dst_mask[6], src0_mask[6];
1749 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1750 DWORD stype = arg->reg_maps->samplers[src0_regnum] & WINED3DSP_TEXTURETYPE_MASK;
1752 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1753 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1754 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1756 strcpy(dimensions, "");
1757 FIXME("Unrecognized sampler type: %#x\n", stype);
1761 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1762 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1764 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1765 shader_addline(arg->buffer, "%stexture%s(Psampler%u, %s.%s))%s;\n",
1766 tmpLine, dimensions, src0_regnum, dst_reg, (stype == WINED3DSTT_2D) ? "xy" : "xyz", dst_mask);
1769 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
1770 * If any of the first 3 components are < 0, discard this pixel */
1771 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
1773 char dst_str[100], dst_name[50], dst_mask[6];
1775 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1776 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_name);
1779 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
1780 * dst = dot2(src0, src1) + src2 */
1781 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
1784 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1785 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1786 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1788 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1789 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1790 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1791 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1792 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1793 shader_addline(arg->buffer, "%sdot(vec2(%s), vec2(%s)) + %s)%s;\n",
1794 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1797 void pshader_glsl_input_pack(
1798 SHADER_BUFFER* buffer,
1799 semantic* semantics_in) {
1803 for (i = 0; i < MAX_REG_INPUT; i++) {
1805 DWORD usage_token = semantics_in[i].usage;
1806 DWORD register_token = semantics_in[i].reg;
1807 DWORD usage, usage_idx;
1811 if (!usage_token) continue;
1812 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1813 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1814 shader_glsl_get_output_register_swizzle(register_token, reg_mask);
1818 case D3DDECLUSAGE_COLOR:
1820 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
1821 i, reg_mask, reg_mask);
1822 else if (usage_idx == 1)
1823 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
1824 i, reg_mask, reg_mask);
1826 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
1827 i, reg_mask, reg_mask);
1830 case D3DDECLUSAGE_TEXCOORD:
1831 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
1832 i, reg_mask, usage_idx, reg_mask );
1835 case D3DDECLUSAGE_FOG:
1836 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
1837 i, reg_mask, reg_mask);
1841 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
1842 i, reg_mask, reg_mask);
1847 /*********************************************
1848 * Vertex Shader Specific Code begins here
1849 ********************************************/
1851 void vshader_glsl_output_unpack(
1852 SHADER_BUFFER* buffer,
1853 semantic* semantics_out) {
1857 for (i = 0; i < MAX_REG_OUTPUT; i++) {
1859 DWORD usage_token = semantics_out[i].usage;
1860 DWORD register_token = semantics_out[i].reg;
1861 DWORD usage, usage_idx;
1865 if (!usage_token) continue;
1867 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1868 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1869 shader_glsl_get_output_register_swizzle(register_token, reg_mask);
1873 case D3DDECLUSAGE_COLOR:
1875 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1876 else if (usage_idx == 1)
1877 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1879 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1882 case D3DDECLUSAGE_POSITION:
1883 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1886 case D3DDECLUSAGE_TEXCOORD:
1887 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
1888 usage_idx, reg_mask, i, reg_mask);
1891 case WINED3DSHADERDECLUSAGE_PSIZE:
1892 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
1895 case WINED3DSHADERDECLUSAGE_FOG:
1896 shader_addline(buffer, "gl_FogFragCoord%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1900 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);