2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 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);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
37 #define GLINFO_LOCATION (*gl_info)
52 } glsl_sample_function_t;
54 /** Prints the GLSL info log which will contain error messages if they exist */
55 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
57 int infologLength = 0;
60 GL_EXTCALL(glGetObjectParameterivARB(obj,
61 GL_OBJECT_INFO_LOG_LENGTH_ARB,
64 /* A size of 1 is just a null-terminated string, so the log should be bigger than
65 * that if there are errors. */
66 if (infologLength > 1)
68 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
69 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
70 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
71 HeapFree(GetProcessHeap(), 0, infoLog);
76 * Loads (pixel shader) samplers
78 static void shader_glsl_load_psamplers(
79 WineD3D_GL_Info *gl_info,
80 IWineD3DStateBlock* iface) {
82 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
83 GLhandleARB programId = stateBlock->glsl_program->programId;
86 char sampler_name[20];
88 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
89 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
90 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
92 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
93 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
94 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
95 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
96 checkGLcall("glUniform1iARB");
98 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
104 static void shader_glsl_load_vsamplers(WineD3D_GL_Info *gl_info, IWineD3DStateBlock* iface) {
105 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
106 GLhandleARB programId = stateBlock->glsl_program->programId;
107 GLhandleARB name_loc;
108 char sampler_name[20];
111 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
112 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
113 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
114 if (name_loc != -1) {
115 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
116 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
117 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
118 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
119 checkGLcall("glUniform1iARB");
121 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
128 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
129 * When constant_list == NULL, it will load all the constants.
131 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
132 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
133 struct list *constant_list) {
134 constants_entry *constant;
135 local_constant* lconst;
140 if (TRACE_ON(d3d_shader)) {
141 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
146 tmp_loc = constant_locations[i];
148 TRACE_(d3d_constants)("Loading constants %i: %f, %f, %f, %f\n", i,
149 constants[i * 4 + 0], constants[i * 4 + 1],
150 constants[i * 4 + 2], constants[i * 4 + 3]);
155 LIST_FOR_EACH_ENTRY(constant, constant_list, constants_entry, entry) {
160 tmp_loc = constant_locations[i];
162 /* We found this uniform name in the program - go ahead and send the data */
163 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
167 checkGLcall("glUniform4fvARB()");
169 /* Load immediate constants */
170 if (TRACE_ON(d3d_shader)) {
171 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
172 tmp_loc = constant_locations[lconst->idx];
174 GLfloat* values = (GLfloat*)lconst->value;
175 TRACE_(d3d_constants)("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
176 values[0], values[1], values[2], values[3]);
180 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
181 tmp_loc = constant_locations[lconst->idx];
183 /* We found this uniform name in the program - go ahead and send the data */
184 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
187 checkGLcall("glUniform4fvARB()");
191 * Loads integer constants (aka uniforms) into the currently set GLSL program.
192 * When @constants_set == NULL, it will load all the constants.
194 static void shader_glsl_load_constantsI(
195 IWineD3DBaseShaderImpl* This,
196 WineD3D_GL_Info *gl_info,
197 GLhandleARB programId,
198 unsigned max_constants,
200 BOOL* constants_set) {
205 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
206 const char* prefix = is_pshader? "PI":"VI";
209 for (i=0; i<max_constants; ++i) {
210 if (NULL == constants_set || constants_set[i]) {
212 TRACE_(d3d_constants)("Loading constants %i: %i, %i, %i, %i\n",
213 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
215 /* TODO: Benchmark and see if it would be beneficial to store the
216 * locations of the constants to avoid looking up each time */
217 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
218 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
220 /* We found this uniform name in the program - go ahead and send the data */
221 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
222 checkGLcall("glUniform4ivARB");
227 /* Load immediate constants */
228 ptr = list_head(&This->baseShader.constantsI);
230 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
231 unsigned int idx = lconst->idx;
232 GLint* values = (GLint*) lconst->value;
234 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
235 values[0], values[1], values[2], values[3]);
237 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
238 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
240 /* We found this uniform name in the program - go ahead and send the data */
241 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
242 checkGLcall("glUniform4ivARB");
244 ptr = list_next(&This->baseShader.constantsI, ptr);
249 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
250 * When @constants_set == NULL, it will load all the constants.
252 static void shader_glsl_load_constantsB(
253 IWineD3DBaseShaderImpl* This,
254 WineD3D_GL_Info *gl_info,
255 GLhandleARB programId,
256 unsigned max_constants,
258 BOOL* constants_set) {
263 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
264 const char* prefix = is_pshader? "PB":"VB";
267 for (i=0; i<max_constants; ++i) {
268 if (NULL == constants_set || constants_set[i]) {
270 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i*4]);
272 /* TODO: Benchmark and see if it would be beneficial to store the
273 * locations of the constants to avoid looking up each time */
274 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
275 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
277 /* We found this uniform name in the program - go ahead and send the data */
278 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
279 checkGLcall("glUniform1ivARB");
284 /* Load immediate constants */
285 ptr = list_head(&This->baseShader.constantsB);
287 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
288 unsigned int idx = lconst->idx;
289 GLint* values = (GLint*) lconst->value;
291 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
293 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
294 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
296 /* We found this uniform name in the program - go ahead and send the data */
297 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
298 checkGLcall("glUniform1ivARB");
300 ptr = list_next(&This->baseShader.constantsB, ptr);
307 * Loads the app-supplied constants into the currently set GLSL program.
309 void shader_glsl_load_constants(
310 IWineD3DDevice* device,
312 char useVertexShader) {
314 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
315 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
316 WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
318 GLhandleARB *constant_locations;
319 struct list *constant_list;
320 GLhandleARB programId;
323 if (!stateBlock->glsl_program) {
324 /* No GLSL program set - nothing to do. */
327 programId = stateBlock->glsl_program->programId;
329 if (useVertexShader) {
330 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
333 constant_locations = stateBlock->glsl_program->vuniformF_locations;
334 constant_list = &stateBlock->set_vconstantsF;
336 /* Load vertex shader samplers */
337 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)stateBlock);
339 /* Load DirectX 9 float constants/uniforms for vertex shader */
340 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
341 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
343 /* Load DirectX 9 integer constants/uniforms for vertex shader */
344 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
345 stateBlock->vertexShaderConstantI,
346 stateBlock->set.vertexShaderConstantsI);
348 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
349 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
350 stateBlock->vertexShaderConstantB,
351 stateBlock->set.vertexShaderConstantsB);
353 /* Upload the position fixup params */
354 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
355 checkGLcall("glGetUniformLocationARB");
356 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
357 checkGLcall("glUniform4fvARB");
360 if (usePixelShader) {
362 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
364 constant_locations = stateBlock->glsl_program->puniformF_locations;
365 constant_list = &stateBlock->set_pconstantsF;
367 /* Load pixel shader samplers */
368 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
370 /* Load DirectX 9 float constants/uniforms for pixel shader */
371 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
372 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
374 /* Load DirectX 9 integer constants/uniforms for pixel shader */
375 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
376 stateBlock->pixelShaderConstantI,
377 stateBlock->set.pixelShaderConstantsI);
379 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
380 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
381 stateBlock->pixelShaderConstantB,
382 stateBlock->set.pixelShaderConstantsB);
384 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
385 * It can't be 0 for a valid texbem instruction.
387 if(((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat != -1) {
388 float *data = (float *) &stateBlock->textureState[(int) ((IWineD3DPixelShaderImpl *) pshader)->needsbumpmat][WINED3DTSS_BUMPENVMAT00];
389 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "bumpenvmat"));
390 checkGLcall("glGetUniformLocationARB");
391 GL_EXTCALL(glUniformMatrix2fvARB(pos, 1, 0, data));
392 checkGLcall("glUniform4fvARB");
397 /** Generate the variable & register declarations for the GLSL output target */
398 void shader_generate_glsl_declarations(
399 IWineD3DBaseShader *iface,
400 shader_reg_maps* reg_maps,
401 SHADER_BUFFER* buffer,
402 WineD3D_GL_Info* gl_info) {
404 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
407 /* There are some minor differences between pixel and vertex shaders */
408 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
409 char prefix = pshader ? 'P' : 'V';
411 /* Prototype the subroutines */
412 for (i = 0; i < This->baseShader.limits.label; i++) {
413 if (reg_maps->labels[i])
414 shader_addline(buffer, "void subroutine%lu();\n", i);
417 /* Declare the constants (aka uniforms) */
418 if (This->baseShader.limits.constant_float > 0) {
419 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
420 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
421 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
424 if (This->baseShader.limits.constant_int > 0)
425 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
427 if (This->baseShader.limits.constant_bool > 0)
428 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
431 shader_addline(buffer, "uniform vec4 posFixup;\n");
432 else if(reg_maps->bumpmat != -1)
433 shader_addline(buffer, "uniform mat2 bumpenvmat;\n");
435 /* Declare texture samplers */
436 for (i = 0; i < This->baseShader.limits.sampler; i++) {
437 if (reg_maps->samplers[i]) {
439 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
443 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
446 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
448 case WINED3DSTT_CUBE:
449 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
451 case WINED3DSTT_VOLUME:
452 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
455 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
456 FIXME("Unrecognized sampler type: %#x\n", stype);
462 /* Declare address variables */
463 for (i = 0; i < This->baseShader.limits.address; i++) {
464 if (reg_maps->address[i])
465 shader_addline(buffer, "ivec4 A%d;\n", i);
468 /* Declare texture coordinate temporaries and initialize them */
469 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
470 if (reg_maps->texcoord[i])
471 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
474 /* Declare input register temporaries */
475 for (i=0; i < This->baseShader.limits.packed_input; i++) {
476 if (reg_maps->packed_input[i])
477 shader_addline(buffer, "vec4 IN%lu;\n", i);
480 /* Declare output register temporaries */
481 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
482 if (reg_maps->packed_output[i])
483 shader_addline(buffer, "vec4 OUT%lu;\n", i);
486 /* Declare temporary variables */
487 for(i = 0; i < This->baseShader.limits.temporary; i++) {
488 if (reg_maps->temporary[i])
489 shader_addline(buffer, "vec4 R%lu;\n", i);
492 /* Declare attributes */
493 for (i = 0; i < This->baseShader.limits.attributes; i++) {
494 if (reg_maps->attributes[i])
495 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
498 /* Declare loop register aL */
499 if (reg_maps->loop) {
500 shader_addline(buffer, "int aL;\n");
501 shader_addline(buffer, "int tmpInt;\n");
504 /* Temporary variables for matrix operations */
505 shader_addline(buffer, "vec4 tmp0;\n");
506 shader_addline(buffer, "vec4 tmp1;\n");
508 /* Start the main program */
509 shader_addline(buffer, "void main() {\n");
512 /*****************************************************************************
513 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
515 * For more information, see http://wiki.winehq.org/DirectX-Shaders
516 ****************************************************************************/
519 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
520 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
522 /** Used for opcode modifiers - They multiply the result by the specified amount */
523 static const char * const shift_glsl_tab[] = {
525 "2.0 * ", /* 1 (x2) */
526 "4.0 * ", /* 2 (x4) */
527 "8.0 * ", /* 3 (x8) */
528 "16.0 * ", /* 4 (x16) */
529 "32.0 * ", /* 5 (x32) */
536 "0.0625 * ", /* 12 (d16) */
537 "0.125 * ", /* 13 (d8) */
538 "0.25 * ", /* 14 (d4) */
539 "0.5 * " /* 15 (d2) */
542 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
543 static void shader_glsl_gen_modifier (
546 const char *in_regswizzle,
551 if (instr == WINED3DSIO_TEXKILL)
554 switch (instr & WINED3DSP_SRCMOD_MASK) {
555 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
557 case WINED3DSPSM_NONE:
558 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
560 case WINED3DSPSM_NEG:
561 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
563 case WINED3DSPSM_NOT:
564 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
566 case WINED3DSPSM_BIAS:
567 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
569 case WINED3DSPSM_BIASNEG:
570 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
572 case WINED3DSPSM_SIGN:
573 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
575 case WINED3DSPSM_SIGNNEG:
576 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
578 case WINED3DSPSM_COMP:
579 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
582 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
584 case WINED3DSPSM_X2NEG:
585 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
587 case WINED3DSPSM_ABS:
588 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
590 case WINED3DSPSM_ABSNEG:
591 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
594 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
595 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
599 /** Writes the GLSL variable name that corresponds to the register that the
600 * DX opcode parameter is trying to access */
601 static void shader_glsl_get_register_name(
603 const DWORD addr_token,
606 SHADER_OPCODE_ARG* arg) {
608 /* oPos, oFog and oPts in D3D */
609 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
611 DWORD reg = param & WINED3DSP_REGNUM_MASK;
612 DWORD regtype = shader_get_regtype(param);
613 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
614 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
615 WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
617 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
623 case WINED3DSPR_TEMP:
624 sprintf(tmpStr, "R%u", reg);
626 case WINED3DSPR_INPUT:
628 /* Pixel shaders >= 3.0 */
629 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
630 sprintf(tmpStr, "IN%u", reg);
633 strcpy(tmpStr, "gl_Color");
635 strcpy(tmpStr, "gl_SecondaryColor");
638 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
640 sprintf(tmpStr, "attrib%u", reg);
643 case WINED3DSPR_CONST:
645 const char* prefix = pshader? "PC":"VC";
647 /* Relative addressing */
648 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
650 /* Relative addressing on shaders 2.0+ have a relative address token,
651 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
652 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
653 glsl_src_param_t rel_param;
654 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
655 sprintf(tmpStr, "%s[%s + %u]", prefix, rel_param.param_str, reg);
657 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
660 sprintf(tmpStr, "%s[%u]", prefix, reg);
664 case WINED3DSPR_CONSTINT:
666 sprintf(tmpStr, "PI[%u]", reg);
668 sprintf(tmpStr, "VI[%u]", reg);
670 case WINED3DSPR_CONSTBOOL:
672 sprintf(tmpStr, "PB[%u]", reg);
674 sprintf(tmpStr, "VB[%u]", reg);
676 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
678 sprintf(tmpStr, "T%u", reg);
680 sprintf(tmpStr, "A%u", reg);
683 case WINED3DSPR_LOOP:
684 sprintf(tmpStr, "aL");
686 case WINED3DSPR_SAMPLER:
688 sprintf(tmpStr, "Psampler%u", reg);
690 sprintf(tmpStr, "Vsampler%u", reg);
692 case WINED3DSPR_COLOROUT:
693 if (reg >= GL_LIMITS(buffers)) {
694 WARN("Write to render target %u, only %d supported\n", reg, 4);
696 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
697 sprintf(tmpStr, "gl_FragData[%u]", reg);
698 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
699 sprintf(tmpStr, "gl_FragColor");
702 case WINED3DSPR_RASTOUT:
703 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
705 case WINED3DSPR_DEPTHOUT:
706 sprintf(tmpStr, "gl_FragDepth");
708 case WINED3DSPR_ATTROUT:
710 sprintf(tmpStr, "gl_FrontColor");
712 sprintf(tmpStr, "gl_FrontSecondaryColor");
715 case WINED3DSPR_TEXCRDOUT:
716 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
717 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
718 sprintf(tmpStr, "OUT%u", reg);
720 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
722 case WINED3DSPR_MISCTYPE:
725 sprintf(tmpStr, "gl_FragCoord");
727 /* gl_FrontFacing could be used for vFace, but note that
728 * gl_FrontFacing is a bool, while vFace is a float for
729 * which the sign determines front/back */
730 FIXME("Unhandled misctype register %d\n", reg);
731 sprintf(tmpStr, "unrecognized_register");
735 FIXME("Unhandled register name Type(%d)\n", regtype);
736 sprintf(tmpStr, "unrecognized_register");
740 strcat(regstr, tmpStr);
743 /* Get the GLSL write mask for the destination register */
744 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
745 char *ptr = write_mask;
746 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
748 if (shader_is_scalar(param)) {
749 mask = WINED3DSP_WRITEMASK_0;
752 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
753 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
754 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
755 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
763 static size_t shader_glsl_get_write_mask_size(DWORD write_mask) {
766 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
767 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
768 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
769 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
774 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
775 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
776 * but addressed as "rgba". To fix this we need to swap the register's x
777 * and z components. */
778 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
779 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
780 char *ptr = swizzle_str;
782 if (!shader_is_scalar(param)) {
784 /* swizzle bits fields: wwzzyyxx */
785 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
786 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
787 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
788 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
794 /* From a given parameter token, generate the corresponding GLSL string.
795 * Also, return the actual register name and swizzle in case the
796 * caller needs this information as well. */
797 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
798 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param) {
799 BOOL is_color = FALSE;
802 src_param->reg_name[0] = '\0';
803 src_param->param_str[0] = '\0';
804 swizzle_str[0] = '\0';
806 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
808 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
809 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
812 /* From a given parameter token, generate the corresponding GLSL string.
813 * Also, return the actual register name and swizzle in case the
814 * caller needs this information as well. */
815 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
816 const DWORD addr_token, glsl_dst_param_t *dst_param) {
817 BOOL is_color = FALSE;
819 dst_param->mask_str[0] = '\0';
820 dst_param->reg_name[0] = '\0';
822 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
823 return shader_glsl_get_write_mask(param, dst_param->mask_str);
826 /* Append the destination part of the instruction to the buffer, return the effective write mask */
827 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg, const DWORD param) {
828 glsl_dst_param_t dst_param;
832 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
835 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
836 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
842 /* Append the destination part of the instruction to the buffer, return the effective write mask */
843 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
844 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
847 /** Process GLSL instruction modifiers */
848 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
850 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
852 if (arg->opcode->dst_token && mask != 0) {
853 glsl_dst_param_t dst_param;
855 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
857 if (mask & WINED3DSPDM_SATURATE) {
858 /* _SAT means to clamp the value of the register to between 0 and 1 */
859 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
860 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
862 if (mask & WINED3DSPDM_MSAMPCENTROID) {
863 FIXME("_centroid modifier not handled\n");
865 if (mask & WINED3DSPDM_PARTIALPRECISION) {
866 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
871 static inline const char* shader_get_comp_op(
872 const DWORD opcode) {
874 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
876 case COMPARISON_GT: return ">";
877 case COMPARISON_EQ: return "==";
878 case COMPARISON_GE: return ">=";
879 case COMPARISON_LT: return "<";
880 case COMPARISON_NE: return "!=";
881 case COMPARISON_LE: return "<=";
883 FIXME("Unrecognized comparison value: %u\n", op);
888 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, glsl_sample_function_t *sample_function) {
889 /* Note that there's no such thing as a projected cube texture. */
890 switch(sampler_type) {
892 sample_function->name = projected ? "texture1DProj" : "texture1D";
893 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
896 sample_function->name = projected ? "texture2DProj" : "texture2D";
897 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
899 case WINED3DSTT_CUBE:
900 sample_function->name = "textureCube";
901 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
903 case WINED3DSTT_VOLUME:
904 sample_function->name = projected ? "texture3DProj" : "texture3D";
905 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
908 sample_function->name = "";
909 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
915 /*****************************************************************************
917 * Begin processing individual instruction opcodes
919 ****************************************************************************/
921 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
922 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
923 CONST SHADER_OPCODE* curOpcode = arg->opcode;
924 SHADER_BUFFER* buffer = arg->buffer;
925 glsl_src_param_t src0_param;
926 glsl_src_param_t src1_param;
930 /* Determine the GLSL operator to use based on the opcode */
931 switch (curOpcode->opcode) {
932 case WINED3DSIO_MUL: op = '*'; break;
933 case WINED3DSIO_ADD: op = '+'; break;
934 case WINED3DSIO_SUB: op = '-'; break;
937 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
941 write_mask = shader_glsl_append_dst(buffer, arg);
942 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
943 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
944 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
947 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
948 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
949 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
950 SHADER_BUFFER* buffer = arg->buffer;
951 glsl_src_param_t src0_param;
954 write_mask = shader_glsl_append_dst(buffer, arg);
955 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
957 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
958 * shader versions WINED3DSIO_MOVA is used for this. */
959 if ((WINED3DSHADER_VERSION_MAJOR(shader->baseShader.hex_version) == 1 &&
960 !shader_is_pshader_version(shader->baseShader.hex_version) &&
961 shader_get_regtype(arg->dst) == WINED3DSPR_ADDR) ||
962 arg->opcode->opcode == WINED3DSIO_MOVA) {
963 /* We need to *round* to the nearest int here. */
964 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
966 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n", mask_size, src0_param.param_str, mask_size, src0_param.param_str);
968 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
971 shader_addline(buffer, "%s);\n", src0_param.param_str);
975 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
976 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
977 CONST SHADER_OPCODE* curOpcode = arg->opcode;
978 SHADER_BUFFER* buffer = arg->buffer;
979 glsl_src_param_t src0_param;
980 glsl_src_param_t src1_param;
981 DWORD dst_write_mask, src_write_mask;
984 dst_write_mask = shader_glsl_append_dst(buffer, arg);
985 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
987 /* dp3 works on vec3, dp4 on vec4 */
988 if (curOpcode->opcode == WINED3DSIO_DP4) {
989 src_write_mask = WINED3DSP_WRITEMASK_ALL;
991 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
994 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
995 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
998 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1000 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1004 /* Note that this instruction has some restrictions. The destination write mask
1005 * can't contain the w component, and the source swizzles have to be .xyzw */
1006 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
1007 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1008 glsl_src_param_t src0_param;
1009 glsl_src_param_t src1_param;
1012 shader_glsl_get_write_mask(arg->dst, dst_mask);
1013 shader_glsl_append_dst(arg->buffer, arg);
1014 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1015 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1016 shader_addline(arg->buffer, "cross(%s, %s).%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1019 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1020 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1021 * GLSL uses the value as-is. */
1022 void shader_glsl_pow(SHADER_OPCODE_ARG *arg) {
1023 SHADER_BUFFER *buffer = arg->buffer;
1024 glsl_src_param_t src0_param;
1025 glsl_src_param_t src1_param;
1026 DWORD dst_write_mask;
1029 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1030 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1032 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1033 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1036 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1038 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1042 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1043 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1044 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1045 SHADER_BUFFER* buffer = arg->buffer;
1046 glsl_src_param_t src_param;
1047 const char *instruction;
1048 char arguments[256];
1052 /* Determine the GLSL function to use based on the opcode */
1053 /* TODO: Possibly make this a table for faster lookups */
1054 switch (curOpcode->opcode) {
1055 case WINED3DSIO_MIN: instruction = "min"; break;
1056 case WINED3DSIO_MAX: instruction = "max"; break;
1057 case WINED3DSIO_ABS: instruction = "abs"; break;
1058 case WINED3DSIO_FRC: instruction = "fract"; break;
1059 case WINED3DSIO_NRM: instruction = "normalize"; break;
1060 case WINED3DSIO_LOGP:
1061 case WINED3DSIO_LOG: instruction = "log2"; break;
1062 case WINED3DSIO_EXP: instruction = "exp2"; break;
1063 case WINED3DSIO_SGN: instruction = "sign"; break;
1064 default: instruction = "";
1065 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1069 write_mask = shader_glsl_append_dst(buffer, arg);
1071 arguments[0] = '\0';
1072 if (curOpcode->num_params > 0) {
1073 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1074 strcat(arguments, src_param.param_str);
1075 for (i = 2; i < curOpcode->num_params; ++i) {
1076 strcat(arguments, ", ");
1077 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1078 strcat(arguments, src_param.param_str);
1082 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1085 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1086 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1087 * dst.x = 2^(floor(src))
1088 * dst.y = src - floor(src)
1089 * dst.z = 2^src (partial precision is allowed, but optional)
1091 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1092 * dst = 2^src; (partial precision is allowed, but optional)
1094 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1095 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1096 glsl_src_param_t src_param;
1098 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1100 if (shader->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
1103 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1104 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1105 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1106 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1108 shader_glsl_append_dst(arg->buffer, arg);
1109 shader_glsl_get_write_mask(arg->dst, dst_mask);
1110 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1115 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1116 mask_size = shader_glsl_get_write_mask_size(write_mask);
1118 if (mask_size > 1) {
1119 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1121 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1126 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1127 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1128 glsl_src_param_t src_param;
1132 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1133 mask_size = shader_glsl_get_write_mask_size(write_mask);
1134 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1136 if (mask_size > 1) {
1137 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1139 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1143 void shader_glsl_rsq(SHADER_OPCODE_ARG* arg) {
1144 SHADER_BUFFER* buffer = arg->buffer;
1145 glsl_src_param_t src_param;
1149 write_mask = shader_glsl_append_dst(buffer, arg);
1150 mask_size = shader_glsl_get_write_mask_size(write_mask);
1152 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1154 if (mask_size > 1) {
1155 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1157 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1161 /** Process signed comparison opcodes in GLSL. */
1162 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1163 glsl_src_param_t src0_param;
1164 glsl_src_param_t src1_param;
1168 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1169 mask_size = shader_glsl_get_write_mask_size(write_mask);
1170 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1171 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1173 if (mask_size > 1) {
1174 const char *compare;
1176 switch(arg->opcode->opcode) {
1177 case WINED3DSIO_SLT: compare = "lessThan"; break;
1178 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1179 default: compare = "";
1180 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1183 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1184 src0_param.param_str, src1_param.param_str);
1186 const char *compare;
1188 switch(arg->opcode->opcode) {
1189 case WINED3DSIO_SLT: compare = "<"; break;
1190 case WINED3DSIO_SGE: compare = ">="; break;
1191 default: compare = "";
1192 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1195 shader_addline(arg->buffer, "(%s %s %s) ? 1.0 : 0.0);\n",
1196 src0_param.param_str, compare, src1_param.param_str);
1200 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1201 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1202 glsl_src_param_t src0_param;
1203 glsl_src_param_t src1_param;
1204 glsl_src_param_t src2_param;
1205 DWORD write_mask, cmp_channel = 0;
1208 /* Cycle through all source0 channels */
1209 for (i=0; i<4; i++) {
1211 /* Find the destination channels which use the current source0 channel */
1212 for (j=0; j<4; j++) {
1213 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1214 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1215 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1218 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1219 if (!write_mask) continue;
1221 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1222 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1223 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1225 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1226 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1230 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1231 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1232 * the compare is done per component of src0. */
1233 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1234 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
1235 glsl_src_param_t src0_param;
1236 glsl_src_param_t src1_param;
1237 glsl_src_param_t src2_param;
1238 DWORD write_mask, cmp_channel = 0;
1241 if (shader->baseShader.hex_version < WINED3DPS_VERSION(1, 4)) {
1242 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1243 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1244 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1245 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1246 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1247 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1250 /* Cycle through all source0 channels */
1251 for (i=0; i<4; i++) {
1253 /* Find the destination channels which use the current source0 channel */
1254 for (j=0; j<4; j++) {
1255 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1256 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1257 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1260 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1261 if (!write_mask) continue;
1263 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1264 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1265 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1267 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1268 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1272 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1273 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1274 glsl_src_param_t src0_param;
1275 glsl_src_param_t src1_param;
1276 glsl_src_param_t src2_param;
1279 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1280 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1281 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1282 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1283 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1284 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1287 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1288 Vertex shaders to GLSL codes */
1289 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1291 int nComponents = 0;
1292 SHADER_OPCODE_ARG tmpArg;
1294 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1296 /* Set constants for the temporary argument */
1297 tmpArg.shader = arg->shader;
1298 tmpArg.buffer = arg->buffer;
1299 tmpArg.src[0] = arg->src[0];
1300 tmpArg.src_addr[0] = arg->src_addr[0];
1301 tmpArg.src_addr[1] = arg->src_addr[1];
1302 tmpArg.reg_maps = arg->reg_maps;
1304 switch(arg->opcode->opcode) {
1305 case WINED3DSIO_M4x4:
1307 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1309 case WINED3DSIO_M4x3:
1311 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1313 case WINED3DSIO_M3x4:
1315 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1317 case WINED3DSIO_M3x3:
1319 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1321 case WINED3DSIO_M3x2:
1323 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1329 for (i = 0; i < nComponents; i++) {
1330 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1331 tmpArg.src[1] = arg->src[1]+i;
1332 shader_glsl_dot(&tmpArg);
1337 The LRP instruction performs a component-wise linear interpolation
1338 between the second and third operands using the first operand as the
1339 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1340 This is equivalent to mix(src2, src1, src0);
1342 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1343 glsl_src_param_t src0_param;
1344 glsl_src_param_t src1_param;
1345 glsl_src_param_t src2_param;
1348 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1350 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1351 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1352 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1354 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1355 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1358 /** Process the WINED3DSIO_LIT instruction in GLSL:
1359 * dst.x = dst.w = 1.0
1360 * dst.y = (src0.x > 0) ? src0.x
1361 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1362 * where src.w is clamped at +- 128
1364 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1365 glsl_src_param_t src0_param;
1366 glsl_src_param_t src1_param;
1367 glsl_src_param_t src3_param;
1370 shader_glsl_append_dst(arg->buffer, arg);
1371 shader_glsl_get_write_mask(arg->dst, dst_mask);
1373 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1374 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
1375 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
1377 shader_addline(arg->buffer, "vec4(1.0, (%s > 0.0 ? %s : 0.0), (%s > 0.0 ? ((%s > 0.0) ? pow(%s, clamp(%s, -128.0, 128.0)) : 0.0) : 0.0), 1.0)%s);\n",
1378 src0_param.param_str, src0_param.param_str, src0_param.param_str, src1_param.param_str, src1_param.param_str, src3_param.param_str, dst_mask);
1381 /** Process the WINED3DSIO_DST instruction in GLSL:
1383 * dst.y = src0.x * src0.y
1387 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1388 glsl_src_param_t src0y_param;
1389 glsl_src_param_t src0z_param;
1390 glsl_src_param_t src1y_param;
1391 glsl_src_param_t src1w_param;
1394 shader_glsl_append_dst(arg->buffer, arg);
1395 shader_glsl_get_write_mask(arg->dst, dst_mask);
1397 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
1398 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
1399 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
1400 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
1402 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
1403 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
1406 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1407 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1408 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1410 * dst.x = cos(src0.?)
1411 * dst.y = sin(src0.?)
1415 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1416 glsl_src_param_t src0_param;
1419 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1420 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1422 switch (write_mask) {
1423 case WINED3DSP_WRITEMASK_0:
1424 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
1427 case WINED3DSP_WRITEMASK_1:
1428 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
1431 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1432 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
1436 ERR("Write mask should be .x, .y or .xy\n");
1441 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1442 * Start a for() loop where src1.y is the initial value of aL,
1443 * increment aL by src1.z for a total of src1.x iterations.
1444 * Need to use a temporary variable for this operation.
1446 /* FIXME: I don't think nested loops will work correctly this way. */
1447 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1448 glsl_src_param_t src1_param;
1450 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
1452 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1453 src1_param.reg_name, src1_param.reg_name, src1_param.reg_name);
1456 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1457 shader_addline(arg->buffer, "}\n");
1460 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1461 glsl_src_param_t src0_param;
1463 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1464 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s; tmpInt++) {\n", src0_param.param_str);
1467 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1468 glsl_src_param_t src0_param;
1470 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1471 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
1474 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1475 glsl_src_param_t src0_param;
1476 glsl_src_param_t src1_param;
1478 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1479 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1481 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1482 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1485 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1486 shader_addline(arg->buffer, "} else {\n");
1489 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1490 shader_addline(arg->buffer, "break;\n");
1493 /* FIXME: According to MSDN the compare is done per component. */
1494 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1495 glsl_src_param_t src0_param;
1496 glsl_src_param_t src1_param;
1498 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1499 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1501 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1502 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
1505 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1507 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1508 shader_addline(arg->buffer, "}\n");
1509 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1512 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1513 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1514 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1517 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1518 glsl_src_param_t src1_param;
1520 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1521 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1522 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_param.param_str, snum);
1525 /*********************************************
1526 * Pixel Shader Specific Code begins here
1527 ********************************************/
1528 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1529 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1530 DWORD hex_version = This->baseShader.hex_version;
1531 char dst_swizzle[6];
1532 glsl_sample_function_t sample_function;
1538 /* All versions have a destination register */
1539 shader_glsl_append_dst(arg->buffer, arg);
1541 /* 1.0-1.4: Use destination register as sampler source.
1542 * 2.0+: Use provided sampler source. */
1543 if (hex_version < WINED3DPS_VERSION(1,4)) {
1544 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1547 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1548 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1550 if (flags & WINED3DTTFF_PROJECTED) {
1552 switch (flags & ~WINED3DTTFF_PROJECTED) {
1553 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1554 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
1555 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
1556 case WINED3DTTFF_COUNT4:
1557 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
1562 } else if (hex_version < WINED3DPS_VERSION(2,0)) {
1563 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1564 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1566 if (src_mod == WINED3DSPSM_DZ) {
1568 mask = WINED3DSP_WRITEMASK_2;
1569 } else if (src_mod == WINED3DSPSM_DW) {
1571 mask = WINED3DSP_WRITEMASK_3;
1576 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1577 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
1578 /* ps 2.0 texldp instruction always divides by the fourth component. */
1580 mask = WINED3DSP_WRITEMASK_3;
1586 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1587 shader_glsl_get_sample_function(sampler_type, projected, &sample_function);
1588 mask |= sample_function.coord_mask;
1590 if (hex_version < WINED3DPS_VERSION(2,0)) {
1591 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
1593 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
1596 /* 1.0-1.3: Use destination register as coordinate source.
1597 1.4+: Use provided coordinate source register. */
1598 if (hex_version < WINED3DPS_VERSION(1,4)) {
1600 shader_glsl_get_write_mask(mask, coord_mask);
1601 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
1602 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
1604 glsl_src_param_t coord_param;
1605 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
1606 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
1607 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
1611 void shader_glsl_texldl(SHADER_OPCODE_ARG* arg) {
1612 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
1613 glsl_sample_function_t sample_function;
1614 glsl_src_param_t coord_param, lod_param;
1615 char dst_swizzle[6];
1619 shader_glsl_append_dst(arg->buffer, arg);
1620 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
1622 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
1623 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1624 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1625 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &coord_param);
1627 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
1629 if (shader_is_pshader_version(This->baseShader.hex_version)) {
1630 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
1631 * However, they seem to work just fine in fragment shaders as well. */
1632 WARN("Using %sLod in fragment shader.\n", sample_function.name);
1633 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
1634 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
1636 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
1637 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
1641 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1643 /* FIXME: Make this work for more than just 2D textures */
1645 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1646 SHADER_BUFFER* buffer = arg->buffer;
1647 DWORD hex_version = This->baseShader.hex_version;
1651 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1652 shader_glsl_get_write_mask(write_mask, dst_mask);
1654 if (hex_version != WINED3DPS_VERSION(1,4)) {
1655 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1656 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
1658 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1659 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
1660 char dst_swizzle[6];
1662 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
1664 if (src_mod == WINED3DSPSM_DZ) {
1665 glsl_src_param_t div_param;
1666 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
1667 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
1669 if (mask_size > 1) {
1670 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
1672 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
1674 } else if (src_mod == WINED3DSPSM_DW) {
1675 glsl_src_param_t div_param;
1676 size_t mask_size = shader_glsl_get_write_mask_size(write_mask);
1677 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
1679 if (mask_size > 1) {
1680 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
1682 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
1685 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
1690 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1691 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1692 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1693 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1694 glsl_src_param_t src0_param;
1696 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1697 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1699 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1701 shader_glsl_append_dst(arg->buffer, arg);
1702 shader_glsl_get_write_mask(arg->dst, dst_mask);
1703 shader_addline(arg->buffer, "texture2D(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.5))%s);\n",
1704 sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
1707 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1708 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1709 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1710 glsl_src_param_t src0_param;
1711 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1712 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1716 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
1717 mask_size = shader_glsl_get_write_mask_size(dst_mask);
1718 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1720 if (mask_size > 1) {
1721 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
1723 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
1727 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1728 * Calculate the depth as dst.x / dst.y */
1729 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1730 glsl_dst_param_t dst_param;
1732 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1734 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_param.reg_name, dst_param.reg_name);
1737 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1738 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1739 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1740 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1742 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1743 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1744 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1745 glsl_src_param_t src0_param;
1747 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1749 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
1750 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
1753 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1754 * Calculate the 1st of a 2-row matrix multiplication. */
1755 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1756 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1757 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1758 SHADER_BUFFER* buffer = arg->buffer;
1759 glsl_src_param_t src0_param;
1761 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1762 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1765 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1766 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1767 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1769 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1770 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1771 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1772 SHADER_BUFFER* buffer = arg->buffer;
1773 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1774 glsl_src_param_t src0_param;
1776 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1777 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
1778 current_state->texcoord_w[current_state->current_row++] = reg;
1781 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1782 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1783 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1784 SHADER_BUFFER* buffer = arg->buffer;
1785 glsl_src_param_t src0_param;
1788 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1789 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1791 shader_glsl_append_dst(buffer, arg);
1792 shader_glsl_get_write_mask(arg->dst, dst_mask);
1794 /* Sample the texture using the calculated coordinates */
1795 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
1798 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1799 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1800 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1801 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1802 glsl_src_param_t src0_param;
1804 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1805 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1806 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1807 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1808 glsl_sample_function_t sample_function;
1810 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1811 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1813 shader_glsl_append_dst(arg->buffer, arg);
1814 shader_glsl_get_write_mask(arg->dst, dst_mask);
1815 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1817 /* Sample the texture using the calculated coordinates */
1818 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
1820 current_state->current_row = 0;
1823 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1824 * Perform the 3rd row of a 3x3 matrix multiply */
1825 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
1826 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1827 glsl_src_param_t src0_param;
1829 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1830 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1831 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1833 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1835 shader_glsl_append_dst(arg->buffer, arg);
1836 shader_glsl_get_write_mask(arg->dst, dst_mask);
1837 shader_addline(arg->buffer, "vec4(tmp.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
1839 current_state->current_row = 0;
1842 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1843 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1844 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1846 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1847 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1848 glsl_src_param_t src0_param;
1849 glsl_src_param_t src1_param;
1851 SHADER_BUFFER* buffer = arg->buffer;
1852 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1853 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1854 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1855 glsl_sample_function_t sample_function;
1857 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1858 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1860 /* Perform the last matrix multiply operation */
1861 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
1863 /* Calculate reflection vector, 2*(tmp0.src1)*tmp0-src1
1864 * This is equivalent to reflect(-src1, tmp0); */
1865 shader_addline(buffer, "tmp0.xyz = reflect(-(%s), tmp0.xyz);\n", src1_param.param_str);
1867 shader_glsl_append_dst(buffer, arg);
1868 shader_glsl_get_write_mask(arg->dst, dst_mask);
1869 shader_glsl_get_sample_function(stype, FALSE, &sample_function);
1871 /* Sample the texture */
1872 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
1874 current_state->current_row = 0;
1877 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1878 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1879 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1881 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1882 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1883 SHADER_BUFFER* buffer = arg->buffer;
1884 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1885 glsl_src_param_t src0_param;
1887 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1888 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1889 glsl_sample_function_t sample_function;
1891 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1893 /* Perform the last matrix multiply operation */
1894 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
1896 /* Construct the eye-ray vector from w coordinates */
1897 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
1898 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
1900 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(tmp0.tmp1)*tmp0-tmp1
1901 * This is equivalent to reflect(-tmp1, tmp0); */
1902 shader_addline(buffer, "tmp0.xyz = reflect(-tmp1.xyz, tmp0.xyz);\n");
1904 shader_glsl_append_dst(buffer, arg);
1905 shader_glsl_get_write_mask(arg->dst, dst_mask);
1906 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1908 /* Sample the texture using the calculated coordinates */
1909 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
1911 current_state->current_row = 0;
1914 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1915 * Apply a fake bump map transform.
1916 * texbem is pshader <= 1.3 only, this saves a few version checks
1918 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1919 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1920 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
1921 char dst_swizzle[6];
1922 glsl_sample_function_t sample_function;
1923 glsl_src_param_t coord_param;
1930 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1931 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
1933 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
1934 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
1935 mask = sample_function.coord_mask;
1937 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
1939 shader_glsl_get_write_mask(mask, coord_mask);
1941 /* with projective textures, texbem only divides the static texture coord, not the displacement,
1942 * so we can't let the GL handle this.
1944 if (flags & WINED3DTTFF_PROJECTED) {
1946 char coord_div_mask[3];
1947 switch (flags & ~WINED3DTTFF_PROJECTED) {
1948 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
1949 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
1950 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
1951 case WINED3DTTFF_COUNT4:
1952 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
1954 shader_glsl_get_write_mask(div_mask, coord_div_mask);
1955 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
1958 shader_glsl_append_dst(arg->buffer, arg);
1959 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
1960 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat * %s, 0.0, 0.0)%s )%s);\n",
1961 sample_function.name, sampler_idx, sampler_idx, coord_mask, coord_param.param_str, coord_mask, dst_swizzle);
1964 void pshader_glsl_bem(SHADER_OPCODE_ARG* arg) {
1965 glsl_src_param_t src0_param, src1_param;
1967 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
1968 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
1970 shader_glsl_append_dst(arg->buffer, arg);
1971 shader_addline(arg->buffer, "%s + bumpenvmat * %s);\n",
1972 src0_param.param_str, src1_param.param_str);
1975 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1976 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1977 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1978 glsl_src_param_t src0_param;
1979 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1982 shader_glsl_append_dst(arg->buffer, arg);
1983 shader_glsl_get_write_mask(arg->dst, dst_mask);
1984 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1986 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
1989 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1990 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1991 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1992 glsl_src_param_t src0_param;
1993 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
1996 shader_glsl_append_dst(arg->buffer, arg);
1997 shader_glsl_get_write_mask(arg->dst, dst_mask);
1998 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2000 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2003 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2004 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2005 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
2006 glsl_src_param_t src0_param;
2008 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2009 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2010 glsl_sample_function_t sample_function;
2012 shader_glsl_append_dst(arg->buffer, arg);
2013 shader_glsl_get_write_mask(arg->dst, dst_mask);
2014 shader_glsl_get_sample_function(sampler_type, FALSE, &sample_function);
2015 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2017 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2020 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2021 * If any of the first 3 components are < 0, discard this pixel */
2022 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
2023 glsl_dst_param_t dst_param;
2025 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2026 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2029 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2030 * dst = dot2(src0, src1) + src2 */
2031 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
2032 glsl_src_param_t src0_param;
2033 glsl_src_param_t src1_param;
2034 glsl_src_param_t src2_param;
2038 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2039 mask_size = shader_glsl_get_write_mask_size(write_mask);
2041 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2042 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2043 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2045 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2048 void pshader_glsl_input_pack(
2049 SHADER_BUFFER* buffer,
2050 semantic* semantics_in) {
2054 for (i = 0; i < MAX_REG_INPUT; i++) {
2056 DWORD usage_token = semantics_in[i].usage;
2057 DWORD register_token = semantics_in[i].reg;
2058 DWORD usage, usage_idx;
2062 if (!usage_token) continue;
2063 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2064 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2065 shader_glsl_get_write_mask(register_token, reg_mask);
2069 case WINED3DDECLUSAGE_COLOR:
2071 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
2072 i, reg_mask, reg_mask);
2073 else if (usage_idx == 1)
2074 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
2075 i, reg_mask, reg_mask);
2077 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
2078 i, reg_mask, reg_mask);
2081 case WINED3DDECLUSAGE_TEXCOORD:
2082 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
2083 i, reg_mask, usage_idx, reg_mask );
2086 case WINED3DDECLUSAGE_FOG:
2087 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
2088 i, reg_mask, reg_mask);
2092 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
2093 i, reg_mask, reg_mask);
2098 /*********************************************
2099 * Vertex Shader Specific Code begins here
2100 ********************************************/
2102 void vshader_glsl_output_unpack(
2103 SHADER_BUFFER* buffer,
2104 semantic* semantics_out) {
2108 for (i = 0; i < MAX_REG_OUTPUT; i++) {
2110 DWORD usage_token = semantics_out[i].usage;
2111 DWORD register_token = semantics_out[i].reg;
2112 DWORD usage, usage_idx;
2116 if (!usage_token) continue;
2118 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2119 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2120 shader_glsl_get_write_mask(register_token, reg_mask);
2124 case WINED3DDECLUSAGE_COLOR:
2126 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2127 else if (usage_idx == 1)
2128 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2130 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2133 case WINED3DDECLUSAGE_POSITION:
2134 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2137 case WINED3DDECLUSAGE_TEXCOORD:
2138 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
2139 usage_idx, reg_mask, i, reg_mask);
2142 case WINED3DDECLUSAGE_PSIZE:
2143 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
2146 case WINED3DDECLUSAGE_FOG:
2147 shader_addline(buffer, "gl_FogFragCoord = OUT%u%s;\n", i, reg_mask);
2151 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
2156 static void add_glsl_program_entry(IWineD3DDeviceImpl *device, struct glsl_shader_prog_link *entry) {
2157 glsl_program_key_t *key;
2159 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2160 key->vshader = entry->vshader;
2161 key->pshader = entry->pshader;
2163 hash_table_put(device->glsl_program_lookup, key, entry);
2166 static struct glsl_shader_prog_link *get_glsl_program_entry(IWineD3DDeviceImpl *device,
2167 GLhandleARB vshader, GLhandleARB pshader) {
2168 glsl_program_key_t key;
2170 key.vshader = vshader;
2171 key.pshader = pshader;
2173 return (struct glsl_shader_prog_link *)hash_table_get(device->glsl_program_lookup, &key);
2176 void delete_glsl_program_entry(IWineD3DDevice *iface, struct glsl_shader_prog_link *entry) {
2177 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2178 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2179 glsl_program_key_t *key;
2181 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2182 key->vshader = entry->vshader;
2183 key->pshader = entry->pshader;
2184 hash_table_remove(This->glsl_program_lookup, key);
2186 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2187 if (entry->vshader) list_remove(&entry->vshader_entry);
2188 if (entry->pshader) list_remove(&entry->pshader_entry);
2189 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2190 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2191 HeapFree(GetProcessHeap(), 0, entry);
2194 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
2195 * It sets the programId on the current StateBlock (because it should be called
2196 * inside of the DrawPrimitive() part of the render loop).
2198 * If a program for the given combination does not exist, create one, and store
2199 * the program in the hash table. If it creates a program, it will link the
2200 * given objects, too.
2202 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
2203 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2204 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2205 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
2206 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
2207 struct glsl_shader_prog_link *entry = NULL;
2208 GLhandleARB programId = 0;
2212 GLhandleARB vshader_id = use_vs ? ((IWineD3DBaseShaderImpl*)vshader)->baseShader.prgId : 0;
2213 GLhandleARB pshader_id = use_ps ? ((IWineD3DBaseShaderImpl*)pshader)->baseShader.prgId : 0;
2214 entry = get_glsl_program_entry(This, vshader_id, pshader_id);
2216 This->stateBlock->glsl_program = entry;
2220 /* If we get to this point, then no matching program exists, so we create one */
2221 programId = GL_EXTCALL(glCreateProgramObjectARB());
2222 TRACE("Created new GLSL shader program %u\n", programId);
2224 /* Create the entry */
2225 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
2226 entry->programId = programId;
2227 entry->vshader = vshader_id;
2228 entry->pshader = pshader_id;
2229 /* Add the hash table entry */
2230 add_glsl_program_entry(This, entry);
2232 /* Set the current program */
2233 This->stateBlock->glsl_program = entry;
2235 /* Attach GLSL vshader */
2237 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
2240 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
2241 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
2242 checkGLcall("glAttachObjectARB");
2244 /* Bind vertex attributes to a corresponding index number to match
2245 * the same index numbers as ARB_vertex_programs (makes loading
2246 * vertex attributes simpler). With this method, we can use the
2247 * exact same code to load the attributes later for both ARB and
2250 * We have to do this here because we need to know the Program ID
2251 * in order to make the bindings work, and it has to be done prior
2252 * to linking the GLSL program. */
2253 for (i = 0; i < max_attribs; ++i) {
2254 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
2255 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
2257 checkGLcall("glBindAttribLocationARB");
2259 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
2262 /* Attach GLSL pshader */
2264 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
2265 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
2266 checkGLcall("glAttachObjectARB");
2268 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
2271 /* Link the program */
2272 TRACE("Linking GLSL shader program %u\n", programId);
2273 GL_EXTCALL(glLinkProgramARB(programId));
2274 print_glsl_info_log(&GLINFO_LOCATION, programId);
2276 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
2277 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
2278 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
2279 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2281 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
2282 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
2283 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
2284 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2288 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
2289 GLhandleARB program_id;
2290 GLhandleARB vshader_id, pshader_id;
2291 const char *blt_vshader[] = {
2294 " gl_Position = gl_Vertex;\n"
2295 " gl_FrontColor = vec4(1.0);\n"
2296 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2297 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2301 const char *blt_pshader[] = {
2302 "uniform sampler2D sampler;\n"
2305 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2309 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2310 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2311 GL_EXTCALL(glCompileShaderARB(vshader_id));
2313 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2314 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2315 GL_EXTCALL(glCompileShaderARB(pshader_id));
2317 program_id = GL_EXTCALL(glCreateProgramObjectARB());
2318 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2319 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2320 GL_EXTCALL(glLinkProgramARB(program_id));
2322 print_glsl_info_log(&GLINFO_LOCATION, program_id);
2327 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2328 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2329 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2330 GLhandleARB program_id = 0;
2332 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
2333 else This->stateBlock->glsl_program = NULL;
2335 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2336 if (program_id) TRACE("Using GLSL program %u\n", program_id);
2337 GL_EXTCALL(glUseProgramObjectARB(program_id));
2338 checkGLcall("glUseProgramObjectARB");
2341 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2342 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2343 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2344 static GLhandleARB program_id = 0;
2345 static GLhandleARB loc = -1;
2348 program_id = create_glsl_blt_shader(gl_info);
2349 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2352 GL_EXTCALL(glUseProgramObjectARB(program_id));
2353 GL_EXTCALL(glUniform1iARB(loc, 0));
2356 static void shader_glsl_cleanup(IWineD3DDevice *iface) {
2357 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2358 WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
2359 GL_EXTCALL(glUseProgramObjectARB(0));
2362 const shader_backend_t glsl_shader_backend = {
2363 &shader_glsl_select,
2364 &shader_glsl_select_depth_blt,
2365 &shader_glsl_load_constants,
2366 &shader_glsl_cleanup