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
23 * D3D shader asm has swizzles on source parameters, and write masks for
24 * destination parameters. GLSL uses swizzles for both. The result of this is
25 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
26 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
27 * mask for the destination parameter into account.
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 #define GLINFO_LOCATION (*gl_info)
38 /** Prints the GLSL info log which will contain error messages if they exist */
39 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
41 int infologLength = 0;
44 GL_EXTCALL(glGetObjectParameterivARB(obj,
45 GL_OBJECT_INFO_LOG_LENGTH_ARB,
48 /* A size of 1 is just a null-terminated string, so the log should be bigger than
49 * that if there are errors. */
50 if (infologLength > 1)
52 infoLog = (char *)HeapAlloc(GetProcessHeap(), 0, infologLength);
53 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
54 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
55 HeapFree(GetProcessHeap(), 0, infoLog);
60 * Loads (pixel shader) samplers
62 void shader_glsl_load_psamplers(
63 WineD3D_GL_Info *gl_info,
64 IWineD3DStateBlock* iface) {
66 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
67 GLhandleARB programId = stateBlock->glsl_program->programId;
70 char sampler_name[20];
72 for (i=0; i< GL_LIMITS(samplers); ++i) {
73 if (stateBlock->textures[i] != NULL) {
74 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
75 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
77 TRACE("Loading %s for texture %d\n", sampler_name, i);
78 GL_EXTCALL(glUniform1iARB(name_loc, i));
79 checkGLcall("glUniform1iARB");
86 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
87 * When constant_list == NULL, it will load all the constants.
89 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
90 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
91 struct list *constant_list) {
92 local_constant* lconst;
97 if (TRACE_ON(d3d_shader)) {
98 for (i = 0; i < max_constants; ++i) {
99 tmp_loc = constant_locations[i];
101 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
102 constants[i * 4 + 0], constants[i * 4 + 1],
103 constants[i * 4 + 2], constants[i * 4 + 3]);
107 for (i = 0; i < max_constants; ++i) {
108 tmp_loc = constant_locations[i];
110 /* We found this uniform name in the program - go ahead and send the data */
111 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
114 checkGLcall("glUniform4fvARB()");
116 constant_entry *constant;
117 if (TRACE_ON(d3d_shader)) {
118 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
120 tmp_loc = constant_locations[i];
122 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
123 constants[i * 4 + 0], constants[i * 4 + 1],
124 constants[i * 4 + 2], constants[i * 4 + 3]);
128 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
130 tmp_loc = constant_locations[i];
132 /* We found this uniform name in the program - go ahead and send the data */
133 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
136 checkGLcall("glUniform4fvARB()");
139 /* Load immediate constants */
140 if (TRACE_ON(d3d_shader)) {
141 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
142 tmp_loc = constant_locations[lconst->idx];
144 GLfloat* values = (GLfloat*)lconst->value;
145 TRACE("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
146 values[0], values[1], values[2], values[3]);
150 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
151 tmp_loc = constant_locations[lconst->idx];
153 /* We found this uniform name in the program - go ahead and send the data */
154 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
157 checkGLcall("glUniform4fvARB()");
161 * Loads integer constants (aka uniforms) into the currently set GLSL program.
162 * When @constants_set == NULL, it will load all the constants.
164 void shader_glsl_load_constantsI(
165 IWineD3DBaseShaderImpl* This,
166 WineD3D_GL_Info *gl_info,
167 GLhandleARB programId,
168 unsigned max_constants,
170 BOOL* constants_set) {
175 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
176 const char* prefix = is_pshader? "PI":"VI";
179 for (i=0; i<max_constants; ++i) {
180 if (NULL == constants_set || constants_set[i]) {
182 TRACE("Loading constants %i: %i, %i, %i, %i\n",
183 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
185 /* TODO: Benchmark and see if it would be beneficial to store the
186 * locations of the constants to avoid looking up each time */
187 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
188 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
190 /* We found this uniform name in the program - go ahead and send the data */
191 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
192 checkGLcall("glUniform4ivARB");
197 /* Load immediate constants */
198 ptr = list_head(&This->baseShader.constantsI);
200 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
201 unsigned int idx = lconst->idx;
202 GLint* values = (GLint*) lconst->value;
204 TRACE("Loading local constants %i: %i, %i, %i, %i\n", idx,
205 values[0], values[1], values[2], values[3]);
207 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
208 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
210 /* We found this uniform name in the program - go ahead and send the data */
211 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
212 checkGLcall("glUniform4ivARB");
214 ptr = list_next(&This->baseShader.constantsI, ptr);
219 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
220 * When @constants_set == NULL, it will load all the constants.
222 void shader_glsl_load_constantsB(
223 IWineD3DBaseShaderImpl* This,
224 WineD3D_GL_Info *gl_info,
225 GLhandleARB programId,
226 unsigned max_constants,
228 BOOL* constants_set) {
233 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
234 const char* prefix = is_pshader? "PB":"VB";
237 for (i=0; i<max_constants; ++i) {
238 if (NULL == constants_set || constants_set[i]) {
240 TRACE("Loading constants %i: %i;\n", i, constants[i*4]);
242 /* TODO: Benchmark and see if it would be beneficial to store the
243 * locations of the constants to avoid looking up each time */
244 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
245 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
247 /* We found this uniform name in the program - go ahead and send the data */
248 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
249 checkGLcall("glUniform1ivARB");
254 /* Load immediate constants */
255 ptr = list_head(&This->baseShader.constantsB);
257 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
258 unsigned int idx = lconst->idx;
259 GLint* values = (GLint*) lconst->value;
261 TRACE("Loading local constants %i: %i\n", idx, values[0]);
263 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
264 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
266 /* We found this uniform name in the program - go ahead and send the data */
267 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
268 checkGLcall("glUniform1ivARB");
270 ptr = list_next(&This->baseShader.constantsB, ptr);
277 * Loads the app-supplied constants into the currently set GLSL program.
279 void shader_glsl_load_constants(
280 IWineD3DDevice* device,
282 char useVertexShader) {
284 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
285 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
286 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*) deviceImpl->wineD3D)->gl_info;
288 GLhandleARB *constant_locations;
289 struct list *constant_list;
290 GLhandleARB programId;
292 if (!stateBlock->glsl_program) {
293 /* No GLSL program set - nothing to do. */
296 programId = stateBlock->glsl_program->programId;
298 if (useVertexShader) {
299 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
300 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) vshader;
303 IWineD3DVertexDeclarationImpl* vertexDeclaration =
304 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
306 constant_locations = stateBlock->glsl_program->vuniformF_locations;
307 constant_list = &stateBlock->set_vconstantsF;
309 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
310 /* Load DirectX 8 float constants/uniforms for vertex shader */
311 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
312 vertexDeclaration->constants, constant_locations, NULL);
315 /* Load DirectX 9 float constants/uniforms for vertex shader */
316 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
317 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
319 /* Load DirectX 9 integer constants/uniforms for vertex shader */
320 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
321 stateBlock->vertexShaderConstantI,
322 stateBlock->set.vertexShaderConstantsI);
324 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
325 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
326 stateBlock->vertexShaderConstantB,
327 stateBlock->set.vertexShaderConstantsB);
329 /* Upload the position fixup params */
330 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
331 checkGLcall("glGetUniformLocationARB");
332 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
333 checkGLcall("glUniform4fvARB");
336 if (usePixelShader) {
338 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
340 constant_locations = stateBlock->glsl_program->puniformF_locations;
341 constant_list = &stateBlock->set_pconstantsF;
343 /* Load pixel shader samplers */
344 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
346 /* Load DirectX 9 float constants/uniforms for pixel shader */
347 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
348 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
350 /* Load DirectX 9 integer constants/uniforms for pixel shader */
351 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
352 stateBlock->pixelShaderConstantI,
353 stateBlock->set.pixelShaderConstantsI);
355 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
356 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
357 stateBlock->pixelShaderConstantB,
358 stateBlock->set.pixelShaderConstantsB);
362 /** Generate the variable & register declarations for the GLSL output target */
363 void shader_generate_glsl_declarations(
364 IWineD3DBaseShader *iface,
365 shader_reg_maps* reg_maps,
366 SHADER_BUFFER* buffer,
367 WineD3D_GL_Info* gl_info) {
369 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
372 /* There are some minor differences between pixel and vertex shaders */
373 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
374 char prefix = pshader ? 'P' : 'V';
376 /* Prototype the subroutines */
377 for (i = 0; i < This->baseShader.limits.label; i++) {
378 if (reg_maps->labels[i])
379 shader_addline(buffer, "void subroutine%lu();\n", i);
382 /* Declare the constants (aka uniforms) */
383 if (This->baseShader.limits.constant_float > 0) {
384 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
385 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
386 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
389 if (This->baseShader.limits.constant_int > 0)
390 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
392 if (This->baseShader.limits.constant_bool > 0)
393 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
396 shader_addline(buffer, "uniform vec4 posFixup;\n");
398 /* Declare texture samplers */
399 for (i = 0; i < This->baseShader.limits.sampler; i++) {
400 if (reg_maps->samplers[i]) {
402 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
406 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
409 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
411 case WINED3DSTT_CUBE:
412 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
414 case WINED3DSTT_VOLUME:
415 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
418 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
419 FIXME("Unrecognized sampler type: %#x\n", stype);
425 /* Declare address variables */
426 for (i = 0; i < This->baseShader.limits.address; i++) {
427 if (reg_maps->address[i])
428 shader_addline(buffer, "ivec4 A%d;\n", i);
431 /* Declare texture coordinate temporaries and initialize them */
432 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
433 if (reg_maps->texcoord[i])
434 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
437 /* Declare input register temporaries */
438 for (i=0; i < This->baseShader.limits.packed_input; i++) {
439 if (reg_maps->packed_input[i])
440 shader_addline(buffer, "vec4 IN%lu;\n", i);
443 /* Declare output register temporaries */
444 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
445 if (reg_maps->packed_output[i])
446 shader_addline(buffer, "vec4 OUT%lu;\n", i);
449 /* Declare temporary variables */
450 for(i = 0; i < This->baseShader.limits.temporary; i++) {
451 if (reg_maps->temporary[i])
452 shader_addline(buffer, "vec4 R%lu;\n", i);
455 /* Declare attributes */
456 for (i = 0; i < This->baseShader.limits.attributes; i++) {
457 if (reg_maps->attributes[i])
458 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
461 /* Declare loop register aL */
462 if (reg_maps->loop) {
463 shader_addline(buffer, "int aL;\n");
464 shader_addline(buffer, "int tmpInt;\n");
467 /* Temporary variables for matrix operations */
468 shader_addline(buffer, "vec4 tmp0;\n");
469 shader_addline(buffer, "vec4 tmp1;\n");
471 /* Start the main program */
472 shader_addline(buffer, "void main() {\n");
475 /*****************************************************************************
476 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
478 * For more information, see http://wiki.winehq.org/DirectX-Shaders
479 ****************************************************************************/
482 static void shader_glsl_add_param(
483 SHADER_OPCODE_ARG* arg,
485 const DWORD addr_token,
491 /** Used for opcode modifiers - They multiply the result by the specified amount */
492 static const char * const shift_glsl_tab[] = {
494 "2.0 * ", /* 1 (x2) */
495 "4.0 * ", /* 2 (x4) */
496 "8.0 * ", /* 3 (x8) */
497 "16.0 * ", /* 4 (x16) */
498 "32.0 * ", /* 5 (x32) */
505 "0.0625 * ", /* 12 (d16) */
506 "0.125 * ", /* 13 (d8) */
507 "0.25 * ", /* 14 (d4) */
508 "0.5 * " /* 15 (d2) */
511 /** Print the beginning of the generated GLSL string. example: "reg_name.xyzw = vec4("
512 * Will also change the reg_mask if necessary (not all register types are equal in DX vs GL) */
513 static void shader_glsl_add_dst(DWORD param, const char* reg_name, char* reg_mask, char* outStr) {
515 int shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
518 if ((shader_get_regtype(param) == WINED3DSPR_RASTOUT)
519 && ((param & WINED3DSP_REGNUM_MASK) != 0)) {
520 /* gl_FogFragCoord or glPointSize - both floats */
521 strcpy(cast, "float");
522 strcpy(reg_mask, "");
524 } else if (reg_name[0] == 'A') {
525 /* Address register for vertex shaders (ivec4) */
526 strcpy(cast, "ivec4");
529 /* Everything else should be a 4 component float vector */
530 strcpy(cast, "vec4");
533 sprintf(outStr, "%s%s = %s%s(", reg_name, reg_mask, shift_glsl_tab[shift], cast);
536 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
537 static void shader_glsl_gen_modifier (
540 const char *in_regswizzle,
545 if (instr == WINED3DSIO_TEXKILL)
548 switch (instr & WINED3DSP_SRCMOD_MASK) {
549 case WINED3DSPSM_NONE:
550 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
552 case WINED3DSPSM_NEG:
553 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
555 case WINED3DSPSM_NOT:
556 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
558 case WINED3DSPSM_BIAS:
559 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
561 case WINED3DSPSM_BIASNEG:
562 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
564 case WINED3DSPSM_SIGN:
565 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
567 case WINED3DSPSM_SIGNNEG:
568 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
570 case WINED3DSPSM_COMP:
571 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
574 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
576 case WINED3DSPSM_X2NEG:
577 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
579 case WINED3DSPSM_DZ: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
580 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);
583 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);
585 case WINED3DSPSM_ABS:
586 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
588 case WINED3DSPSM_ABSNEG:
589 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
592 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
593 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
597 /** Writes the GLSL variable name that corresponds to the register that the
598 * DX opcode parameter is trying to access */
599 static void shader_glsl_get_register_name(
601 const DWORD addr_token,
604 SHADER_OPCODE_ARG* arg) {
606 /* oPos, oFog and oPts in D3D */
607 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
609 DWORD reg = param & WINED3DSP_REGNUM_MASK;
610 DWORD regtype = shader_get_regtype(param);
611 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
612 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
613 WineD3D_GL_Info* gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
615 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
621 case WINED3DSPR_TEMP:
622 sprintf(tmpStr, "R%u", reg);
624 case WINED3DSPR_INPUT:
626 /* Pixel shaders >= 3.0 */
627 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
628 sprintf(tmpStr, "IN%u", reg);
631 strcpy(tmpStr, "gl_Color");
633 strcpy(tmpStr, "gl_SecondaryColor");
636 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
638 sprintf(tmpStr, "attrib%u", reg);
641 case WINED3DSPR_CONST:
643 const char* prefix = pshader? "PC":"VC";
645 /* Relative addressing */
646 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
648 /* Relative addressing on shaders 2.0+ have a relative address token,
649 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
650 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
651 char relStr[100], relReg[50], relMask[6];
652 shader_glsl_add_param(arg, addr_token, 0, TRUE, relReg, relMask, relStr);
653 sprintf(tmpStr, "%s[%s + %u]", prefix, relStr, reg);
655 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
658 sprintf(tmpStr, "%s[%u]", prefix, reg);
662 case WINED3DSPR_CONSTINT:
664 sprintf(tmpStr, "PI[%u]", reg);
666 sprintf(tmpStr, "VI[%u]", reg);
668 case WINED3DSPR_CONSTBOOL:
670 sprintf(tmpStr, "PB[%u]", reg);
672 sprintf(tmpStr, "VB[%u]", reg);
674 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
676 sprintf(tmpStr, "T%u", reg);
678 sprintf(tmpStr, "A%u", reg);
681 case WINED3DSPR_LOOP:
682 sprintf(tmpStr, "aL");
684 case WINED3DSPR_SAMPLER:
686 sprintf(tmpStr, "Psampler%u", reg);
688 sprintf(tmpStr, "Vsampler%u", reg);
690 case WINED3DSPR_COLOROUT:
691 if (reg >= GL_LIMITS(buffers)) {
692 WARN("Write to render target %u, only %d supported\n", reg, 4);
694 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
695 sprintf(tmpStr, "gl_FragData[%u]", reg);
696 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
697 sprintf(tmpStr, "gl_FragColor");
700 case WINED3DSPR_RASTOUT:
701 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
703 case WINED3DSPR_DEPTHOUT:
704 sprintf(tmpStr, "gl_FragDepth");
706 case WINED3DSPR_ATTROUT:
708 sprintf(tmpStr, "gl_FrontColor");
710 sprintf(tmpStr, "gl_FrontSecondaryColor");
713 case WINED3DSPR_TEXCRDOUT:
714 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
715 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
716 sprintf(tmpStr, "OUT%u", reg);
718 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
721 FIXME("Unhandled register name Type(%d)\n", regtype);
722 sprintf(tmpStr, "unrecognized_register");
726 strcat(regstr, tmpStr);
729 /* Get the GLSL write mask for the destination register */
730 static void shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
731 char *ptr = write_mask;
733 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
735 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
736 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
737 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
738 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
744 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, char *swizzle_str) {
745 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
746 * but addressed as "rgba". To fix this we need to swap the register's x
747 * and z components. */
748 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
749 char *ptr = swizzle_str;
751 /* swizzle bits fields: wwzzyyxx */
752 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
753 DWORD swizzle_x = swizzle & 0x03;
754 DWORD swizzle_y = (swizzle >> 2) & 0x03;
755 DWORD swizzle_z = (swizzle >> 4) & 0x03;
756 DWORD swizzle_w = (swizzle >> 6) & 0x03;
758 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
759 * generate a swizzle string. Unless we need to our own swizzling. */
760 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
762 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
763 *ptr++ = swizzle_chars[swizzle_x];
765 *ptr++ = swizzle_chars[swizzle_x];
766 *ptr++ = swizzle_chars[swizzle_y];
767 *ptr++ = swizzle_chars[swizzle_z];
768 *ptr++ = swizzle_chars[swizzle_w];
775 /** From a given parameter token, generate the corresponding GLSL string.
776 * Also, return the actual register name and swizzle in case the
777 * caller needs this information as well. */
778 static void shader_glsl_add_param(
779 SHADER_OPCODE_ARG* arg,
781 const DWORD addr_token,
787 BOOL is_color = FALSE;
788 reg_mask[0] = reg_name[0] = out_str[0] = 0;
790 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
793 shader_glsl_get_swizzle(param, is_color, reg_mask);
794 shader_glsl_gen_modifier(param, reg_name, reg_mask, out_str);
796 shader_glsl_get_write_mask(param, reg_mask);
797 sprintf(out_str, "%s%s", reg_name, reg_mask);
801 /** Process GLSL instruction modifiers */
802 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
804 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
806 if (arg->opcode->dst_token && mask != 0) {
811 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
813 if (mask & WINED3DSPDM_SATURATE) {
814 /* _SAT means to clamp the value of the register to between 0 and 1 */
815 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
817 if (mask & WINED3DSPDM_MSAMPCENTROID) {
818 FIXME("_centroid modifier not handled\n");
820 if (mask & WINED3DSPDM_PARTIALPRECISION) {
821 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
826 static inline const char* shader_get_comp_op(
827 const DWORD opcode) {
829 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
831 case COMPARISON_GT: return ">";
832 case COMPARISON_EQ: return "==";
833 case COMPARISON_GE: return ">=";
834 case COMPARISON_LT: return "<";
835 case COMPARISON_NE: return "!=";
836 case COMPARISON_LE: return "<=";
838 FIXME("Unrecognized comparison value: %u\n", op);
843 static void shader_glsl_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
844 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
845 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
846 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
847 const char sampler_prefix = shader_is_pshader_version(This->baseShader.hex_version) ? 'P' : 'V';
848 SHADER_BUFFER* buffer = arg->buffer;
850 if(deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
851 /* Note that there's no such thing as a projected cube texture. */
852 switch(sampler_type) {
854 shader_addline(buffer, "%s = texture2DProj(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
856 case WINED3DSTT_VOLUME:
857 shader_addline(buffer, "%s = texture3DProj(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
860 shader_addline(buffer, "%s = unrecognized_stype(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
861 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
865 switch(sampler_type) {
867 shader_addline(buffer, "%s = texture2D(%csampler%u, %s.xy);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
869 case WINED3DSTT_CUBE:
870 shader_addline(buffer, "%s = textureCube(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
872 case WINED3DSTT_VOLUME:
873 shader_addline(buffer, "%s = texture3D(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
876 shader_addline(buffer, "%s = unrecognized_stype(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
877 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
884 /*****************************************************************************
886 * Begin processing individual instruction opcodes
888 ****************************************************************************/
890 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
891 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
893 CONST SHADER_OPCODE* curOpcode = arg->opcode;
894 SHADER_BUFFER* buffer = arg->buffer;
896 char dst_reg[50], src0_reg[50], src1_reg[50];
897 char dst_mask[6], src0_mask[6], src1_mask[6];
898 char dst_str[100], src0_str[100], src1_str[100];
900 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
901 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
902 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
903 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
904 strcat(tmpLine, "vec4(");
905 strcat(tmpLine, src0_str);
906 strcat(tmpLine, ")");
908 /* Determine the GLSL operator to use based on the opcode */
909 switch (curOpcode->opcode) {
910 case WINED3DSIO_MUL: strcat(tmpLine, " * "); break;
911 case WINED3DSIO_ADD: strcat(tmpLine, " + "); break;
912 case WINED3DSIO_SUB: strcat(tmpLine, " - "); break;
914 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
917 shader_addline(buffer, "%svec4(%s))%s;\n", tmpLine, src1_str, dst_mask);
920 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
921 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
923 SHADER_BUFFER* buffer = arg->buffer;
925 char dst_str[100], src0_str[100];
926 char dst_reg[50], src0_reg[50];
927 char dst_mask[6], src0_mask[6];
929 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
930 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
931 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
932 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
935 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
936 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
938 CONST SHADER_OPCODE* curOpcode = arg->opcode;
939 SHADER_BUFFER* buffer = arg->buffer;
941 char dst_str[100], src0_str[100], src1_str[100];
942 char dst_reg[50], src0_reg[50], src1_reg[50];
943 char dst_mask[6], src0_mask[6], src1_mask[6];
946 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
947 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
948 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
950 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpDest);
952 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
953 if (curOpcode->opcode == WINED3DSIO_DP4)
954 strcpy(cast, "vec4(");
956 strcpy(cast, "vec3(");
958 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
959 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
962 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
963 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
965 CONST SHADER_OPCODE* curOpcode = arg->opcode;
966 SHADER_BUFFER* buffer = arg->buffer;
968 char dst_str[100], src_str[100];
969 char dst_reg[50], src_reg[50];
970 char dst_mask[6], src_mask[6];
973 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
975 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
977 /* Determine the GLSL function to use based on the opcode */
978 /* TODO: Possibly make this a table for faster lookups */
979 switch (curOpcode->opcode) {
980 case WINED3DSIO_MIN: strcat(tmpLine, "min"); break;
981 case WINED3DSIO_MAX: strcat(tmpLine, "max"); break;
982 case WINED3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
983 case WINED3DSIO_ABS: strcat(tmpLine, "abs"); break;
984 case WINED3DSIO_FRC: strcat(tmpLine, "fract"); break;
985 case WINED3DSIO_POW: strcat(tmpLine, "pow"); break;
986 case WINED3DSIO_CRS: strcat(tmpLine, "cross"); break;
987 case WINED3DSIO_NRM: strcat(tmpLine, "normalize"); break;
988 case WINED3DSIO_LOGP:
989 case WINED3DSIO_LOG: strcat(tmpLine, "log2"); break;
990 case WINED3DSIO_EXP: strcat(tmpLine, "exp2"); break;
991 case WINED3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
992 case WINED3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
993 case WINED3DSIO_SGN: strcat(tmpLine, "sign"); break;
995 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
999 strcat(tmpLine, "(");
1001 if (curOpcode->num_params > 0) {
1002 strcat(tmpLine, "vec4(");
1003 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1004 strcat(tmpLine, src_str);
1005 strcat(tmpLine, ")");
1006 for (i = 2; i < curOpcode->num_params; ++i) {
1007 strcat(tmpLine, ", vec4(");
1008 shader_glsl_add_param(arg, arg->src[i-1], arg->src_addr[i-1], TRUE, src_reg, src_mask, src_str);
1009 strcat(tmpLine, src_str);
1010 strcat(tmpLine, ")");
1013 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
1017 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1018 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1019 * dst.x = 2^(floor(src))
1020 * dst.y = src - floor(src)
1021 * dst.z = 2^src (partial precision is allowed, but optional)
1023 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1024 * dst = 2^src; (partial precision is allowed, but optional)
1026 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1029 char dst_str[100], src_str[100];
1030 char dst_reg[50], src_reg[50];
1031 char dst_mask[6], src_mask[6];
1032 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1033 DWORD hex_version = This->baseShader.hex_version;
1035 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1036 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1037 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1039 if (hex_version < WINED3DPS_VERSION(2,0)) {
1040 shader_addline(arg->buffer, "tmp0.x = vec4(exp2(floor(%s))).x;\n", src_str);
1041 shader_addline(arg->buffer, "tmp0.y = vec4(%s - floor(%s)).y;\n", src_str, src_str);
1042 shader_addline(arg->buffer, "tmp0.z = vec4(exp2(%s)).x;\n", src_str);
1043 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1044 shader_addline(arg->buffer, "%svec4(tmp0))%s;\n", tmpLine, dst_mask);
1046 shader_addline(arg->buffer, "%svec4(exp2(%s)))%s;\n", tmpLine, src_str, dst_mask);
1050 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1051 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1054 char dst_str[100], src_str[100];
1055 char dst_reg[50], src_reg[50];
1056 char dst_mask[6], src_mask[6];
1058 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1059 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1060 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1061 strcat(tmpLine, "1.0 / ");
1062 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
1065 /** Process signed comparison opcodes in GLSL. */
1066 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1069 char dst_str[100], src0_str[100], src1_str[100];
1070 char dst_reg[50], src0_reg[50], src1_reg[50];
1071 char dst_mask[6], src0_mask[6], src1_mask[6];
1073 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1074 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1075 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1077 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
1078 if (strlen(src0_mask) != 2) {
1079 shader_glsl_map2gl(arg);
1083 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1085 switch (arg->opcode->opcode) {
1086 case WINED3DSIO_SLT: strcpy(compareStr, "<"); break;
1087 case WINED3DSIO_SGE: strcpy(compareStr, ">="); break;
1089 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1091 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
1092 tmpLine, src0_str, compareStr, src1_str, dst_mask);
1096 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
1097 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1100 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1101 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1102 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1104 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1105 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1106 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1107 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1109 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1110 shader_addline(arg->buffer, "%smix(vec4(%s), vec4(%s), vec4(lessThan(vec4(%s), vec4(0.0)))))%s;\n",
1111 tmpLine, src1_str, src2_str, src0_str, dst_mask);
1114 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
1115 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1118 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1119 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1120 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1122 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1123 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1124 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1125 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1126 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1127 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
1128 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1131 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1132 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1135 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1136 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1137 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1139 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1140 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1141 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1142 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1143 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1145 shader_addline(arg->buffer, "%s(vec4(%s) * vec4(%s)) + vec4(%s))%s;\n",
1146 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1149 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1150 Vertex shaders to GLSL codes */
1151 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1153 int nComponents = 0;
1154 SHADER_OPCODE_ARG tmpArg;
1156 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1158 /* Set constants for the temporary argument */
1159 tmpArg.shader = arg->shader;
1160 tmpArg.buffer = arg->buffer;
1161 tmpArg.src[0] = arg->src[0];
1162 tmpArg.src_addr[0] = arg->src_addr[0];
1163 tmpArg.src_addr[1] = arg->src_addr[1];
1164 tmpArg.reg_maps = arg->reg_maps;
1166 switch(arg->opcode->opcode) {
1167 case WINED3DSIO_M4x4:
1169 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1171 case WINED3DSIO_M4x3:
1173 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1175 case WINED3DSIO_M3x4:
1177 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1179 case WINED3DSIO_M3x3:
1181 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1183 case WINED3DSIO_M3x2:
1185 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1191 for (i = 0; i < nComponents; i++) {
1192 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1193 tmpArg.src[1] = arg->src[1]+i;
1194 shader_glsl_dot(&tmpArg);
1199 The LRP instruction performs a component-wise linear interpolation
1200 between the second and third operands using the first operand as the
1201 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
1203 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1206 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1207 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1208 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1210 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1211 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1212 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1213 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1215 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1217 shader_addline(arg->buffer, "%s%s + %s * (%s - %s))%s;\n",
1218 tmpLine, src2_str, src0_str, src1_str, src2_str, dst_mask);
1221 /** Process the WINED3DSIO_LIT instruction in GLSL:
1222 * dst.x = dst.w = 1.0
1223 * dst.y = (src0.x > 0) ? src0.x
1224 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1225 * where src.w is clamped at +- 128
1227 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1229 char dst_str[100], src0_str[100];
1230 char dst_reg[50], src0_reg[50];
1231 char dst_mask[6], src0_mask[6];
1233 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1234 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1236 shader_addline(arg->buffer,
1237 "%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",
1238 dst_str, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, dst_mask);
1241 /** Process the WINED3DSIO_DST instruction in GLSL:
1243 * dst.y = src0.x * src0.y
1247 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1249 char dst_str[100], src0_str[100], src1_str[100];
1250 char dst_reg[50], src0_reg[50], src1_reg[50];
1251 char dst_mask[6], src0_mask[6], src1_mask[6];
1253 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1254 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1255 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1257 shader_addline(arg->buffer, "%s = vec4(1.0, %s.x * %s.y, %s.z, %s.w)%s;\n",
1258 dst_str, src0_reg, src1_reg, src0_reg, src1_reg, dst_mask);
1261 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1262 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1263 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1265 * dst.x = cos(src0.?)
1266 * dst.y = sin(src0.?)
1270 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1272 char dst_str[100], src0_str[100];
1273 char dst_reg[50], src0_reg[50];
1274 char dst_mask[6], src0_mask[6];
1276 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1277 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1279 shader_addline(arg->buffer, "%s = vec4(cos(%s), sin(%s), %s.z, %s.w)%s;\n",
1280 dst_str, src0_str, src0_str, dst_reg, dst_reg, dst_mask);
1283 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1284 * Start a for() loop where src0.y is the initial value of aL,
1285 * increment aL by src0.z for a total of src0.x iterations.
1286 * Need to use a temporary variable for this operation.
1288 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1294 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1296 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1297 src1_reg, src1_reg, src1_reg);
1300 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1301 shader_addline(arg->buffer, "}\n");
1304 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1310 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1311 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s.x; tmpInt++) {\n", src0_reg);
1314 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1320 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1321 shader_addline(arg->buffer, "if (%s) {\n", src0_str);
1324 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1326 char src0_str[100], src1_str[100];
1327 char src0_reg[50], src1_reg[50];
1328 char src0_mask[6], src1_mask[6];
1330 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1331 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1333 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1334 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1337 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1338 shader_addline(arg->buffer, "} else {\n");
1341 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1342 shader_addline(arg->buffer, "break;\n");
1345 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1347 char src0_str[100], src1_str[100];
1348 char src0_reg[50], src1_reg[50];
1349 char src0_mask[6], src1_mask[6];
1351 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1352 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1354 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1355 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1358 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1360 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1361 shader_addline(arg->buffer, "}\n");
1362 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1365 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1366 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1367 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1370 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1376 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1377 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1378 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_str, snum);
1381 /*********************************************
1382 * Pixel Shader Specific Code begins here
1383 ********************************************/
1384 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1385 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1387 DWORD hex_version = This->baseShader.hex_version;
1389 char dst_str[100], dst_reg[50], dst_mask[6];
1390 char coord_str[100], coord_reg[50], coord_mask[6];
1392 /* All versions have a destination register */
1393 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1395 /* 1.0-1.3: Use destination register as coordinate source.
1396 1.4+: Use provided coordinate source register. */
1397 if (hex_version < WINED3DPS_VERSION(1,4))
1398 strcpy(coord_reg, dst_reg);
1400 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, coord_reg, coord_mask, coord_str);
1402 /* 1.0-1.4: Use destination register as sampler source.
1403 * 2.0+: Use provided sampler source. */
1404 if (hex_version < WINED3DPS_VERSION(2,0)) {
1405 shader_glsl_sample(arg, arg->dst & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1407 shader_glsl_sample(arg, arg->src[1] & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1412 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1414 /* FIXME: Make this work for more than just 2D textures */
1416 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1417 SHADER_BUFFER* buffer = arg->buffer;
1418 DWORD hex_version = This->baseShader.hex_version;
1425 shader_glsl_add_param(arg, arg->dst, 0, FALSE, tmpReg, tmpMask, tmpStr);
1427 if (hex_version != WINED3DPS_VERSION(1,4)) {
1428 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1429 shader_addline(buffer, "%s = clamp(gl_TexCoord[%u], 0.0, 1.0);\n", tmpReg, reg);
1431 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1432 shader_addline(buffer, "%s = gl_TexCoord[%u]%s;\n", tmpStr, reg2, tmpMask);
1436 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1437 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1438 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1439 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1441 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1442 char src0_str[100], dst_str[100];
1443 char src0_name[50], dst_name[50];
1444 char src0_mask[6], dst_mask[6];
1446 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1447 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1449 shader_addline(arg->buffer, "tmp0.x = dot(vec3(gl_TexCoord[%u]), vec3(%s));\n", dstreg, src0_str);
1450 shader_addline(arg->buffer, "%s = vec4(texture1D(Psampler%u, tmp0.x))%s;\n", dst_str, dstreg, dst_mask);
1453 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1454 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1455 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1457 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1458 char src0_str[100], dst_str[100];
1459 char src0_name[50], dst_name[50];
1460 char src0_mask[6], dst_mask[6];
1462 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1463 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1465 shader_addline(arg->buffer, "%s = vec4(dot(vec3(T%u), vec3(%s)))%s;\n",
1466 dst_str, dstreg, src0_str, dst_mask);
1469 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1470 * Calculate the depth as dst.x / dst.y */
1471 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1477 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1479 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_reg, dst_reg);
1482 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1483 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1484 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1485 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1487 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1489 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1490 char src0_str[100], dst_str[100];
1491 char src0_name[50], dst_name[50];
1492 char src0_mask[6], dst_mask[6];
1494 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1495 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1497 shader_addline(arg->buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", dstreg, src0_str);
1498 shader_addline(arg->buffer, "gl_FragDepth = vec4((tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y)%s;\n", dst_str, dst_name);
1501 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1502 * Calculate the 1st of a 2-row matrix multiplication. */
1503 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1505 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1506 SHADER_BUFFER* buffer = arg->buffer;
1511 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1512 shader_addline(buffer, "tmp0.x = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1515 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1516 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1517 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1519 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1520 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1521 SHADER_BUFFER* buffer = arg->buffer;
1522 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1527 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1528 shader_addline(buffer, "tmp0.%c = dot(vec3(T%u), vec3(%s));\n", 'x' + current_state->current_row, reg, src0_str);
1529 current_state->texcoord_w[current_state->current_row++] = reg;
1532 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1534 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1535 SHADER_BUFFER* buffer = arg->buffer;
1540 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1541 shader_addline(buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1543 /* Sample the texture using the calculated coordinates */
1544 sprintf(dst_str, "T%u", reg);
1545 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1548 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1549 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1550 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1555 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1556 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1557 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1559 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1560 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1562 /* Sample the texture using the calculated coordinates */
1563 sprintf(dst_str, "T%u", reg);
1564 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1565 current_state->current_row = 0;
1568 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1569 * Perform the 3rd row of a 3x3 matrix multiply */
1570 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
1575 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1576 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1577 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1579 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1581 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1582 shader_addline(arg->buffer, "T%u = vec4(tmp0.x, tmp0.y, tmp0.z, 1.0);\n", reg);
1583 current_state->current_row = 0;
1586 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1587 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1588 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1590 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1591 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1594 char src0_str[100], src0_name[50], src0_mask[6];
1595 char src1_str[100], src1_name[50], src1_mask[6];
1596 SHADER_BUFFER* buffer = arg->buffer;
1597 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1598 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1601 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1602 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1603 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1605 strcpy(dimensions, "");
1606 FIXME("Unrecognized sampler type: %#x\n", stype);
1610 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1611 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_name, src1_mask, src1_str);
1613 /* Perform the last matrix multiply operation */
1614 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1616 /* Calculate reflection vector */
1617 shader_addline(buffer, "tmp0.xyz = reflect(-vec3(%s), vec3(tmp0));\n", src1_str);
1619 /* Sample the texture */
1620 sprintf(dst_str, "T%u", reg);
1621 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1622 current_state->current_row = 0;
1625 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1626 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1627 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1629 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1630 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1631 SHADER_BUFFER* buffer = arg->buffer;
1632 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1634 char src0_str[100], src0_name[50], src0_mask[6];
1636 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1638 /* Perform the last matrix multiply operation */
1639 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1641 /* Construct the eye-ray vector from w coordinates */
1642 shader_addline(buffer, "tmp1.x = gl_TexCoord[%u].w;\n", current_state->texcoord_w[0]);
1643 shader_addline(buffer, "tmp1.y = gl_TexCoord[%u].w;\n", current_state->texcoord_w[1]);
1644 shader_addline(buffer, "tmp1.z = gl_TexCoord[%u].w;\n", reg);
1646 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
1647 shader_addline(buffer, "tmp0.x = dot(vec3(tmp0), vec3(tmp1));\n");
1648 shader_addline(buffer, "tmp0 = tmp0.w * tmp0;\n");
1649 shader_addline(buffer, "tmp0 = (2.0 * tmp0) - tmp1;\n");
1651 /* Sample the texture using the calculated coordinates */
1652 sprintf(dst_str, "T%u", reg);
1653 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1654 current_state->current_row = 0;
1657 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1658 * Apply a fake bump map transform.
1659 * FIXME: Should apply the BUMPMAPENV matrix. For now, just sample the texture */
1660 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1662 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1663 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1665 FIXME("Not applying the BUMPMAPENV matrix for pixel shader instruction texbem.\n");
1666 shader_addline(arg->buffer, "T%u = texture2D(Psampler%u, gl_TexCoord[%u].xy + T%u.xy);\n",
1667 reg1, reg1, reg1, reg2);
1670 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1671 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1672 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1675 char dst_str[100], src0_str[100];
1676 char dst_reg[50], src0_reg[50];
1677 char dst_mask[6], src0_mask[6];
1678 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1680 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1681 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1683 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1684 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1685 tmpLine, src0_regnum, dst_reg, dst_mask);
1688 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1689 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1690 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1693 char dst_str[100], src0_str[100];
1694 char dst_reg[50], src0_reg[50];
1695 char dst_mask[6], src0_mask[6];
1696 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1698 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1699 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1701 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1702 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1703 tmpLine, src0_regnum, dst_reg, dst_mask);
1706 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
1707 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
1708 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1711 char dst_str[100], src0_str[100];
1712 char dst_reg[50], src0_reg[50];
1713 char dst_mask[6], src0_mask[6];
1715 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1716 DWORD stype = arg->reg_maps->samplers[src0_regnum] & WINED3DSP_TEXTURETYPE_MASK;
1718 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1719 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1720 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1722 strcpy(dimensions, "");
1723 FIXME("Unrecognized sampler type: %#x\n", stype);
1727 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1728 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1730 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1731 shader_addline(arg->buffer, "%stexture%s(Psampler%u, %s.%s))%s;\n",
1732 tmpLine, dimensions, src0_regnum, dst_reg, (stype == WINED3DSTT_2D) ? "xy" : "xyz", dst_mask);
1735 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
1736 * If any of the first 3 components are < 0, discard this pixel */
1737 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
1739 char dst_str[100], dst_name[50], dst_mask[6];
1741 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1742 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_name);
1745 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
1746 * dst = dot2(src0, src1) + src2 */
1747 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
1750 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1751 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1752 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1754 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1755 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1756 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1757 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1758 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1759 shader_addline(arg->buffer, "%sdot(vec2(%s), vec2(%s)) + %s)%s;\n",
1760 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1763 void pshader_glsl_input_pack(
1764 SHADER_BUFFER* buffer,
1765 semantic* semantics_in) {
1769 for (i = 0; i < MAX_REG_INPUT; i++) {
1771 DWORD usage_token = semantics_in[i].usage;
1772 DWORD register_token = semantics_in[i].reg;
1773 DWORD usage, usage_idx;
1777 if (!usage_token) continue;
1778 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1779 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1780 shader_glsl_get_write_mask(register_token, reg_mask);
1784 case D3DDECLUSAGE_COLOR:
1786 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
1787 i, reg_mask, reg_mask);
1788 else if (usage_idx == 1)
1789 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
1790 i, reg_mask, reg_mask);
1792 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
1793 i, reg_mask, reg_mask);
1796 case D3DDECLUSAGE_TEXCOORD:
1797 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
1798 i, reg_mask, usage_idx, reg_mask );
1801 case D3DDECLUSAGE_FOG:
1802 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
1803 i, reg_mask, reg_mask);
1807 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
1808 i, reg_mask, reg_mask);
1813 /*********************************************
1814 * Vertex Shader Specific Code begins here
1815 ********************************************/
1817 void vshader_glsl_output_unpack(
1818 SHADER_BUFFER* buffer,
1819 semantic* semantics_out) {
1823 for (i = 0; i < MAX_REG_OUTPUT; i++) {
1825 DWORD usage_token = semantics_out[i].usage;
1826 DWORD register_token = semantics_out[i].reg;
1827 DWORD usage, usage_idx;
1831 if (!usage_token) continue;
1833 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1834 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1835 shader_glsl_get_write_mask(register_token, reg_mask);
1839 case D3DDECLUSAGE_COLOR:
1841 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1842 else if (usage_idx == 1)
1843 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1845 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1848 case D3DDECLUSAGE_POSITION:
1849 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1852 case D3DDECLUSAGE_TEXCOORD:
1853 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
1854 usage_idx, reg_mask, i, reg_mask);
1857 case WINED3DSHADERDECLUSAGE_PSIZE:
1858 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
1861 case WINED3DSHADERDECLUSAGE_FOG:
1862 shader_addline(buffer, "gl_FogFragCoord%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1866 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1871 /** Attach a GLSL pixel or vertex shader object to the shader program */
1872 static void attach_glsl_shader(IWineD3DDevice *iface, IWineD3DBaseShader* shader) {
1873 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1874 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1875 GLhandleARB shaderObj = ((IWineD3DBaseShaderImpl*)shader)->baseShader.prgId;
1876 if (This->stateBlock->glsl_program && shaderObj != 0) {
1877 TRACE("Attaching GLSL shader object %u to program %u\n", shaderObj, This->stateBlock->glsl_program->programId);
1878 GL_EXTCALL(glAttachObjectARB(This->stateBlock->glsl_program->programId, shaderObj));
1879 checkGLcall("glAttachObjectARB");
1883 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
1884 * It sets the programId on the current StateBlock (because it should be called
1885 * inside of the DrawPrimitive() part of the render loop).
1887 * If a program for the given combination does not exist, create one, and store
1888 * the program in the list. If it creates a program, it will link the given
1891 * We keep the shader programs around on a list because linking
1892 * shader objects together is an expensive operation. It's much
1893 * faster to loop through a list of pre-compiled & linked programs
1894 * each time that the application sets a new pixel or vertex shader
1895 * than it is to re-link them together at that time.
1897 * The list will be deleted in IWineD3DDevice::Release().
1899 static void set_glsl_shader_program(IWineD3DDevice *iface) {
1900 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1901 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1902 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
1903 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
1904 struct glsl_shader_prog_link *curLink = NULL;
1905 struct glsl_shader_prog_link *newLink = NULL;
1906 struct list *ptr = NULL;
1907 GLhandleARB programId = 0;
1911 ptr = list_head( &This->glsl_shader_progs );
1913 /* At least one program exists - see if it matches our ps/vs combination */
1914 curLink = LIST_ENTRY( ptr, struct glsl_shader_prog_link, entry );
1915 if (vshader == curLink->vertexShader && pshader == curLink->pixelShader) {
1916 /* Existing Program found, use it */
1917 TRACE("Found existing program (%u) for this vertex/pixel shader combination\n",
1918 curLink->programId);
1919 This->stateBlock->glsl_program = curLink;
1922 /* This isn't the entry we need - try the next one */
1923 ptr = list_next( &This->glsl_shader_progs, ptr );
1926 /* If we get to this point, then no matching program exists, so we create one */
1927 programId = GL_EXTCALL(glCreateProgramObjectARB());
1928 TRACE("Created new GLSL shader program %u\n", programId);
1930 /* Allocate a new link for the list of programs */
1931 newLink = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
1932 newLink->programId = programId;
1933 This->stateBlock->glsl_program = newLink;
1935 /* Attach GLSL vshader */
1936 if (NULL != vshader && This->vs_selected_mode == SHADER_GLSL) {
1938 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
1941 TRACE("Attaching vertex shader to GLSL program\n");
1942 attach_glsl_shader(iface, (IWineD3DBaseShader*)vshader);
1944 /* Bind vertex attributes to a corresponding index number to match
1945 * the same index numbers as ARB_vertex_programs (makes loading
1946 * vertex attributes simpler). With this method, we can use the
1947 * exact same code to load the attributes later for both ARB and
1950 * We have to do this here because we need to know the Program ID
1951 * in order to make the bindings work, and it has to be done prior
1952 * to linking the GLSL program. */
1953 for (i = 0; i < max_attribs; ++i) {
1954 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
1955 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
1957 checkGLcall("glBindAttribLocationARB");
1958 newLink->vertexShader = vshader;
1961 /* Attach GLSL pshader */
1962 if (NULL != pshader && This->ps_selected_mode == SHADER_GLSL) {
1963 TRACE("Attaching pixel shader to GLSL program\n");
1964 attach_glsl_shader(iface, (IWineD3DBaseShader*)pshader);
1965 newLink->pixelShader = pshader;
1968 /* Link the program */
1969 TRACE("Linking GLSL shader program %u\n", programId);
1970 GL_EXTCALL(glLinkProgramARB(programId));
1971 print_glsl_info_log(&GLINFO_LOCATION, programId);
1972 list_add_head( &This->glsl_shader_progs, &newLink->entry);
1974 newLink->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
1975 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
1976 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
1977 newLink->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
1979 newLink->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
1980 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
1981 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
1982 newLink->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
1988 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
1989 GLhandleARB program_id;
1990 GLhandleARB vshader_id, pshader_id;
1991 const char *blt_vshader[] = {
1994 " gl_Position = gl_Vertex;\n"
1995 " gl_FrontColor = vec4(1.0);\n"
1996 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
1997 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2001 const char *blt_pshader[] = {
2002 "uniform sampler2D sampler;\n"
2005 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2009 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2010 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2011 GL_EXTCALL(glCompileShaderARB(vshader_id));
2013 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2014 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2015 GL_EXTCALL(glCompileShaderARB(pshader_id));
2017 program_id = GL_EXTCALL(glCreateProgramObjectARB());
2018 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2019 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2020 GL_EXTCALL(glLinkProgramARB(program_id));
2022 print_glsl_info_log(&GLINFO_LOCATION, program_id);
2027 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2028 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2029 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2030 GLhandleARB program_id = 0;
2032 if (useVS || usePS) set_glsl_shader_program(iface);
2033 else This->stateBlock->glsl_program = NULL;
2035 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2036 if (program_id) TRACE("Using GLSL program %u\n", program_id);
2037 GL_EXTCALL(glUseProgramObjectARB(program_id));
2038 checkGLcall("glUseProgramObjectARB");
2041 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2042 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2043 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2044 static GLhandleARB program_id = 0;
2045 static GLhandleARB loc = -1;
2048 program_id = create_glsl_blt_shader(gl_info);
2049 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2052 GL_EXTCALL(glUseProgramObjectARB(program_id));
2053 GL_EXTCALL(glUniform1iARB(loc, 0));
2056 static void shader_glsl_cleanup(BOOL usePS, BOOL useVS) {
2060 const shader_backend_t glsl_shader_backend = {
2061 &shader_glsl_select,
2062 &shader_glsl_select_depth_blt,
2063 &shader_glsl_load_constants,
2064 &shader_glsl_cleanup