2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
28 #define GLINFO_LOCATION (*gl_info)
30 /** Prints the GLSL info log which will contain error messages if they exist */
31 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
33 int infologLength = 0;
36 GL_EXTCALL(glGetObjectParameterivARB(obj,
37 GL_OBJECT_INFO_LOG_LENGTH_ARB,
40 /* A size of 1 is just a null-terminated string, so the log should be bigger than
41 * that if there are errors. */
42 if (infologLength > 1)
44 infoLog = (char *)HeapAlloc(GetProcessHeap(), 0, infologLength);
45 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
46 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
47 HeapFree(GetProcessHeap(), 0, infoLog);
52 * Loads (pixel shader) samplers
54 void shader_glsl_load_psamplers(
55 WineD3D_GL_Info *gl_info,
56 IWineD3DStateBlock* iface) {
58 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
59 GLhandleARB programId = stateBlock->glsl_program->programId;
62 char sampler_name[20];
64 for (i=0; i< GL_LIMITS(samplers); ++i) {
65 if (stateBlock->textures[i] != NULL) {
66 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
67 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
69 TRACE_(d3d_shader)("Loading %s for texture %d\n", sampler_name, i);
70 GL_EXTCALL(glUniform1iARB(name_loc, i));
71 checkGLcall("glUniform1iARB");
78 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
79 * When constant_list == NULL, it will load all the constants.
81 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
82 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
83 struct list *constant_list) {
84 local_constant* lconst;
89 if (TRACE_ON(d3d_shader)) {
90 for (i = 0; i < max_constants; ++i) {
91 tmp_loc = constant_locations[i];
93 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
94 constants[i * 4 + 0], constants[i * 4 + 1],
95 constants[i * 4 + 2], constants[i * 4 + 3]);
99 for (i = 0; i < max_constants; ++i) {
100 tmp_loc = constant_locations[i];
102 /* We found this uniform name in the program - go ahead and send the data */
103 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
106 checkGLcall("glUniform4fvARB()");
108 constant_entry *constant;
109 if (TRACE_ON(d3d_shader)) {
110 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
112 tmp_loc = constant_locations[i];
114 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
115 constants[i * 4 + 0], constants[i * 4 + 1],
116 constants[i * 4 + 2], constants[i * 4 + 3]);
120 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
122 tmp_loc = constant_locations[i];
124 /* We found this uniform name in the program - go ahead and send the data */
125 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
128 checkGLcall("glUniform4fvARB()");
131 /* Load immediate constants */
132 if (TRACE_ON(d3d_shader)) {
133 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
134 tmp_loc = constant_locations[lconst->idx];
136 GLfloat* values = (GLfloat*)lconst->value;
137 TRACE("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
138 values[0], values[1], values[2], values[3]);
142 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
143 tmp_loc = constant_locations[lconst->idx];
145 /* We found this uniform name in the program - go ahead and send the data */
146 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
149 checkGLcall("glUniform4fvARB()");
153 * Loads integer constants (aka uniforms) into the currently set GLSL program.
154 * When @constants_set == NULL, it will load all the constants.
156 void shader_glsl_load_constantsI(
157 IWineD3DBaseShaderImpl* This,
158 WineD3D_GL_Info *gl_info,
159 GLhandleARB programId,
160 unsigned max_constants,
162 BOOL* constants_set) {
167 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
168 const char* prefix = is_pshader? "PI":"VI";
171 for (i=0; i<max_constants; ++i) {
172 if (NULL == constants_set || constants_set[i]) {
174 TRACE("Loading constants %i: %i, %i, %i, %i\n",
175 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
177 /* TODO: Benchmark and see if it would be beneficial to store the
178 * locations of the constants to avoid looking up each time */
179 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
180 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
182 /* We found this uniform name in the program - go ahead and send the data */
183 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
184 checkGLcall("glUniform4ivARB");
189 /* Load immediate constants */
190 ptr = list_head(&This->baseShader.constantsI);
192 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
193 unsigned int idx = lconst->idx;
194 GLint* values = (GLint*) lconst->value;
196 TRACE("Loading local constants %i: %i, %i, %i, %i\n", idx,
197 values[0], values[1], values[2], values[3]);
199 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
200 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
202 /* We found this uniform name in the program - go ahead and send the data */
203 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
204 checkGLcall("glUniform4ivARB");
206 ptr = list_next(&This->baseShader.constantsI, ptr);
211 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
212 * When @constants_set == NULL, it will load all the constants.
214 void shader_glsl_load_constantsB(
215 IWineD3DBaseShaderImpl* This,
216 WineD3D_GL_Info *gl_info,
217 GLhandleARB programId,
218 unsigned max_constants,
220 BOOL* constants_set) {
225 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
226 const char* prefix = is_pshader? "PB":"VB";
229 for (i=0; i<max_constants; ++i) {
230 if (NULL == constants_set || constants_set[i]) {
232 TRACE("Loading constants %i: %i;\n", i, constants[i*4]);
234 /* TODO: Benchmark and see if it would be beneficial to store the
235 * locations of the constants to avoid looking up each time */
236 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
237 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
239 /* We found this uniform name in the program - go ahead and send the data */
240 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
241 checkGLcall("glUniform1ivARB");
246 /* Load immediate constants */
247 ptr = list_head(&This->baseShader.constantsB);
249 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
250 unsigned int idx = lconst->idx;
251 GLint* values = (GLint*) lconst->value;
253 TRACE("Loading local constants %i: %i\n", idx, values[0]);
255 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
256 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
258 /* We found this uniform name in the program - go ahead and send the data */
259 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
260 checkGLcall("glUniform1ivARB");
262 ptr = list_next(&This->baseShader.constantsB, ptr);
269 * Loads the app-supplied constants into the currently set GLSL program.
271 void shader_glsl_load_constants(
272 IWineD3DDevice* device,
274 char useVertexShader) {
276 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
277 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
278 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*) deviceImpl->wineD3D)->gl_info;
280 GLhandleARB *constant_locations;
281 struct list *constant_list;
282 GLhandleARB programId;
284 if (!stateBlock->glsl_program) {
285 /* No GLSL program set - nothing to do. */
288 programId = stateBlock->glsl_program->programId;
290 if (useVertexShader) {
291 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
292 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) vshader;
295 IWineD3DVertexDeclarationImpl* vertexDeclaration =
296 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
298 constant_locations = stateBlock->glsl_program->vuniformF_locations;
299 constant_list = &stateBlock->set_vconstantsF;
301 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
302 /* Load DirectX 8 float constants/uniforms for vertex shader */
303 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
304 vertexDeclaration->constants, constant_locations, NULL);
307 /* Load DirectX 9 float constants/uniforms for vertex shader */
308 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
309 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
311 /* Load DirectX 9 integer constants/uniforms for vertex shader */
312 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
313 stateBlock->vertexShaderConstantI,
314 stateBlock->set.vertexShaderConstantsI);
316 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
317 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
318 stateBlock->vertexShaderConstantB,
319 stateBlock->set.vertexShaderConstantsB);
321 /* Upload the position fixup params */
322 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
323 checkGLcall("glGetUniformLocationARB");
324 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
325 checkGLcall("glUniform4fvARB");
328 if (usePixelShader) {
330 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
332 constant_locations = stateBlock->glsl_program->puniformF_locations;
333 constant_list = &stateBlock->set_pconstantsF;
335 /* Load pixel shader samplers */
336 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
338 /* Load DirectX 9 float constants/uniforms for pixel shader */
339 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
340 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
342 /* Load DirectX 9 integer constants/uniforms for pixel shader */
343 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
344 stateBlock->pixelShaderConstantI,
345 stateBlock->set.pixelShaderConstantsI);
347 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
348 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
349 stateBlock->pixelShaderConstantB,
350 stateBlock->set.pixelShaderConstantsB);
354 /** Generate the variable & register declarations for the GLSL output target */
355 void shader_generate_glsl_declarations(
356 IWineD3DBaseShader *iface,
357 shader_reg_maps* reg_maps,
358 SHADER_BUFFER* buffer,
359 WineD3D_GL_Info* gl_info) {
361 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
364 /* There are some minor differences between pixel and vertex shaders */
365 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
366 char prefix = pshader ? 'P' : 'V';
368 /* Prototype the subroutines */
369 for (i = 0; i < This->baseShader.limits.label; i++) {
370 if (reg_maps->labels[i])
371 shader_addline(buffer, "void subroutine%lu();\n", i);
374 /* Declare the constants (aka uniforms) */
375 if (This->baseShader.limits.constant_float > 0) {
376 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
377 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
378 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
381 if (This->baseShader.limits.constant_int > 0)
382 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
384 if (This->baseShader.limits.constant_bool > 0)
385 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
388 shader_addline(buffer, "uniform vec4 posFixup;\n");
390 /* Declare texture samplers */
391 for (i = 0; i < This->baseShader.limits.sampler; i++) {
392 if (reg_maps->samplers[i]) {
394 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
398 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
401 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
403 case WINED3DSTT_CUBE:
404 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
406 case WINED3DSTT_VOLUME:
407 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
410 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
411 FIXME("Unrecognized sampler type: %#x\n", stype);
417 /* Declare address variables */
418 for (i = 0; i < This->baseShader.limits.address; i++) {
419 if (reg_maps->address[i])
420 shader_addline(buffer, "ivec4 A%d;\n", i);
423 /* Declare texture coordinate temporaries and initialize them */
424 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
425 if (reg_maps->texcoord[i])
426 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
429 /* Declare input register temporaries */
430 for (i=0; i < This->baseShader.limits.packed_input; i++) {
431 if (reg_maps->packed_input[i])
432 shader_addline(buffer, "vec4 IN%lu;\n", i);
435 /* Declare output register temporaries */
436 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
437 if (reg_maps->packed_output[i])
438 shader_addline(buffer, "vec4 OUT%lu;\n", i);
441 /* Declare temporary variables */
442 for(i = 0; i < This->baseShader.limits.temporary; i++) {
443 if (reg_maps->temporary[i])
444 shader_addline(buffer, "vec4 R%lu;\n", i);
447 /* Declare attributes */
448 for (i = 0; i < This->baseShader.limits.attributes; i++) {
449 if (reg_maps->attributes[i])
450 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
453 /* Declare loop register aL */
454 if (reg_maps->loop) {
455 shader_addline(buffer, "int aL;\n");
456 shader_addline(buffer, "int tmpInt;\n");
459 /* Temporary variables for matrix operations */
460 shader_addline(buffer, "vec4 tmp0;\n");
461 shader_addline(buffer, "vec4 tmp1;\n");
463 /* Start the main program */
464 shader_addline(buffer, "void main() {\n");
467 /*****************************************************************************
468 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
470 * For more information, see http://wiki.winehq.org/DirectX-Shaders
471 ****************************************************************************/
474 static void shader_glsl_add_param(
475 SHADER_OPCODE_ARG* arg,
477 const DWORD addr_token,
483 /** Used for opcode modifiers - They multiply the result by the specified amount */
484 static const char * const shift_glsl_tab[] = {
486 "2.0 * ", /* 1 (x2) */
487 "4.0 * ", /* 2 (x4) */
488 "8.0 * ", /* 3 (x8) */
489 "16.0 * ", /* 4 (x16) */
490 "32.0 * ", /* 5 (x32) */
497 "0.0625 * ", /* 12 (d16) */
498 "0.125 * ", /* 13 (d8) */
499 "0.25 * ", /* 14 (d4) */
500 "0.5 * " /* 15 (d2) */
503 /** Print the beginning of the generated GLSL string. example: "reg_name.xyzw = vec4("
504 * Will also change the reg_mask if necessary (not all register types are equal in DX vs GL) */
505 static void shader_glsl_add_dst(DWORD param, const char* reg_name, char* reg_mask, char* outStr) {
507 int shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
510 if ((shader_get_regtype(param) == WINED3DSPR_RASTOUT)
511 && ((param & WINED3DSP_REGNUM_MASK) != 0)) {
512 /* gl_FogFragCoord or glPointSize - both floats */
513 strcpy(cast, "float");
514 strcpy(reg_mask, "");
516 } else if (reg_name[0] == 'A') {
517 /* Address register for vertex shaders (ivec4) */
518 strcpy(cast, "ivec4");
521 /* Everything else should be a 4 component float vector */
522 strcpy(cast, "vec4");
525 sprintf(outStr, "%s%s = %s%s(", reg_name, reg_mask, shift_glsl_tab[shift], cast);
528 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
529 static void shader_glsl_gen_modifier (
532 const char *in_regswizzle,
537 if (instr == WINED3DSIO_TEXKILL)
540 switch (instr & WINED3DSP_SRCMOD_MASK) {
541 case WINED3DSPSM_NONE:
542 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
544 case WINED3DSPSM_NEG:
545 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
547 case WINED3DSPSM_NOT:
548 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
550 case WINED3DSPSM_BIAS:
551 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
553 case WINED3DSPSM_BIASNEG:
554 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
556 case WINED3DSPSM_SIGN:
557 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
559 case WINED3DSPSM_SIGNNEG:
560 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
562 case WINED3DSPSM_COMP:
563 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
566 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
568 case WINED3DSPSM_X2NEG:
569 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
571 case WINED3DSPSM_DZ: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
572 sprintf(out_str, "vec4(%s.r / %s.b, %s.g / %s.b, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
575 sprintf(out_str, "vec4(%s.r / %s.a, %s.g / %s.a, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
577 case WINED3DSPSM_ABS:
578 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
580 case WINED3DSPSM_ABSNEG:
581 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
584 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
585 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
589 /** Writes the GLSL variable name that corresponds to the register that the
590 * DX opcode parameter is trying to access */
591 static void shader_glsl_get_register_name(
593 const DWORD addr_token,
596 SHADER_OPCODE_ARG* arg) {
598 /* oPos, oFog and oPts in D3D */
599 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
601 DWORD reg = param & WINED3DSP_REGNUM_MASK;
602 DWORD regtype = shader_get_regtype(param);
603 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
604 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
605 WineD3D_GL_Info* gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
607 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
613 case WINED3DSPR_TEMP:
614 sprintf(tmpStr, "R%u", reg);
616 case WINED3DSPR_INPUT:
618 /* Pixel shaders >= 3.0 */
619 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
620 sprintf(tmpStr, "IN%u", reg);
623 strcpy(tmpStr, "gl_Color");
625 strcpy(tmpStr, "gl_SecondaryColor");
628 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
630 sprintf(tmpStr, "attrib%u", reg);
633 case WINED3DSPR_CONST:
635 const char* prefix = pshader? "PC":"VC";
637 /* Relative addressing */
638 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
640 /* Relative addressing on shaders 2.0+ have a relative address token,
641 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
642 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
643 char relStr[100], relReg[50], relMask[6];
644 shader_glsl_add_param(arg, addr_token, 0, TRUE, relReg, relMask, relStr);
645 sprintf(tmpStr, "%s[%s + %u]", prefix, relStr, reg);
647 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
650 sprintf(tmpStr, "%s[%u]", prefix, reg);
654 case WINED3DSPR_CONSTINT:
656 sprintf(tmpStr, "PI[%u]", reg);
658 sprintf(tmpStr, "VI[%u]", reg);
660 case WINED3DSPR_CONSTBOOL:
662 sprintf(tmpStr, "PB[%u]", reg);
664 sprintf(tmpStr, "VB[%u]", reg);
666 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
668 sprintf(tmpStr, "T%u", reg);
670 sprintf(tmpStr, "A%u", reg);
673 case WINED3DSPR_LOOP:
674 sprintf(tmpStr, "aL");
676 case WINED3DSPR_SAMPLER:
678 sprintf(tmpStr, "Psampler%u", reg);
680 sprintf(tmpStr, "Vsampler%u", reg);
682 case WINED3DSPR_COLOROUT:
683 if (reg >= GL_LIMITS(buffers)) {
684 WARN("Write to render target %u, only %d supported\n", reg, 4);
686 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
687 sprintf(tmpStr, "gl_FragData[%u]", reg);
688 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
689 sprintf(tmpStr, "gl_FragColor");
692 case WINED3DSPR_RASTOUT:
693 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
695 case WINED3DSPR_DEPTHOUT:
696 sprintf(tmpStr, "gl_FragDepth");
698 case WINED3DSPR_ATTROUT:
700 sprintf(tmpStr, "gl_FrontColor");
702 sprintf(tmpStr, "gl_FrontSecondaryColor");
705 case WINED3DSPR_TEXCRDOUT:
706 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
707 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
708 sprintf(tmpStr, "OUT%u", reg);
710 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
713 FIXME("Unhandled register name Type(%d)\n", regtype);
714 sprintf(tmpStr, "unrecognized_register");
718 strcat(regstr, tmpStr);
721 /* Writes the GLSL writemask for the destination register */
722 static void shader_glsl_get_output_register_swizzle(
727 if ((param & WINED3DSP_WRITEMASK_ALL) != WINED3DSP_WRITEMASK_ALL) {
728 strcat(write_mask, ".");
729 if (param & WINED3DSP_WRITEMASK_0) strcat(write_mask, "x");
730 if (param & WINED3DSP_WRITEMASK_1) strcat(write_mask, "y");
731 if (param & WINED3DSP_WRITEMASK_2) strcat(write_mask, "z");
732 if (param & WINED3DSP_WRITEMASK_3) strcat(write_mask, "w");
736 static void shader_glsl_get_input_register_swizzle(
741 const char swizzle_reg_chars_color_fix[] = "zyxw";
742 const char swizzle_reg_chars[] = "xyzw";
743 const char* swizzle_regs = NULL;
746 DWORD swizzle = (param & WINED3DVS_SWIZZLE_MASK) >> WINED3DVS_SWIZZLE_SHIFT;
747 DWORD swizzle_x = swizzle & 0x03;
748 DWORD swizzle_y = (swizzle >> 2) & 0x03;
749 DWORD swizzle_z = (swizzle >> 4) & 0x03;
750 DWORD swizzle_w = (swizzle >> 6) & 0x03;
753 swizzle_regs = swizzle_reg_chars_color_fix;
755 swizzle_regs = swizzle_reg_chars;
759 * swizzle bits fields:
762 if ((WINED3DVS_NOSWIZZLE >> WINED3DVS_SWIZZLE_SHIFT) == swizzle) {
764 sprintf(reg_mask, ".%c%c%c%c",
765 swizzle_regs[swizzle_x],
766 swizzle_regs[swizzle_y],
767 swizzle_regs[swizzle_z],
768 swizzle_regs[swizzle_w]);
772 if (swizzle_x == swizzle_y &&
773 swizzle_x == swizzle_z &&
774 swizzle_x == swizzle_w)
776 sprintf(reg_mask, ".%c", swizzle_regs[swizzle_x]);
778 sprintf(reg_mask, ".%c%c%c%c",
779 swizzle_regs[swizzle_x],
780 swizzle_regs[swizzle_y],
781 swizzle_regs[swizzle_z],
782 swizzle_regs[swizzle_w]);
786 /** From a given parameter token, generate the corresponding GLSL string.
787 * Also, return the actual register name and swizzle in case the
788 * caller needs this information as well. */
789 static void shader_glsl_add_param(
790 SHADER_OPCODE_ARG* arg,
792 const DWORD addr_token,
798 BOOL is_color = FALSE;
799 reg_mask[0] = reg_name[0] = out_str[0] = 0;
801 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
804 shader_glsl_get_input_register_swizzle(param, is_color, reg_mask);
805 shader_glsl_gen_modifier(param, reg_name, reg_mask, out_str);
807 shader_glsl_get_output_register_swizzle(param, reg_mask);
808 sprintf(out_str, "%s%s", reg_name, reg_mask);
812 /** Process GLSL instruction modifiers */
813 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
815 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
817 if (arg->opcode->dst_token && mask != 0) {
822 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
824 if (mask & WINED3DSPDM_SATURATE) {
825 /* _SAT means to clamp the value of the register to between 0 and 1 */
826 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
828 if (mask & WINED3DSPDM_MSAMPCENTROID) {
829 FIXME("_centroid modifier not handled\n");
831 if (mask & WINED3DSPDM_PARTIALPRECISION) {
832 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
837 static inline const char* shader_get_comp_op(
838 const DWORD opcode) {
840 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
842 case COMPARISON_GT: return ">";
843 case COMPARISON_EQ: return "==";
844 case COMPARISON_GE: return ">=";
845 case COMPARISON_LT: return "<";
846 case COMPARISON_NE: return "!=";
847 case COMPARISON_LE: return "<=";
849 FIXME("Unrecognized comparison value: %u\n", op);
854 static void shader_glsl_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
855 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
856 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
857 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
858 const char sampler_prefix = shader_is_pshader_version(This->baseShader.hex_version) ? 'P' : 'V';
859 SHADER_BUFFER* buffer = arg->buffer;
861 if(deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
862 /* Note that there's no such thing as a projected cube texture. */
863 switch(sampler_type) {
865 shader_addline(buffer, "%s = texture2DProj(%cshader%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
867 case WINED3DSTT_VOLUME:
868 shader_addline(buffer, "%s = texture3DProj(%cshader%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
871 shader_addline(buffer, "%s = unrecognized_stype(%cshader%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
872 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
876 switch(sampler_type) {
878 shader_addline(buffer, "%s = texture2D(%csampler%u, %s.xy);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
880 case WINED3DSTT_CUBE:
881 shader_addline(buffer, "%s = textureCube(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
883 case WINED3DSTT_VOLUME:
884 shader_addline(buffer, "%s = texture3D(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
887 shader_addline(buffer, "%s = unrecognized_stype(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
888 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
895 /*****************************************************************************
897 * Begin processing individual instruction opcodes
899 ****************************************************************************/
901 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
902 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
904 CONST SHADER_OPCODE* curOpcode = arg->opcode;
905 SHADER_BUFFER* buffer = arg->buffer;
907 char dst_reg[50], src0_reg[50], src1_reg[50];
908 char dst_mask[6], src0_mask[6], src1_mask[6];
909 char dst_str[100], src0_str[100], src1_str[100];
911 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
912 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
913 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
914 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
915 strcat(tmpLine, "vec4(");
916 strcat(tmpLine, src0_str);
917 strcat(tmpLine, ")");
919 /* Determine the GLSL operator to use based on the opcode */
920 switch (curOpcode->opcode) {
921 case WINED3DSIO_MUL: strcat(tmpLine, " * "); break;
922 case WINED3DSIO_ADD: strcat(tmpLine, " + "); break;
923 case WINED3DSIO_SUB: strcat(tmpLine, " - "); break;
925 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
928 shader_addline(buffer, "%svec4(%s))%s;\n", tmpLine, src1_str, dst_mask);
931 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
932 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
934 SHADER_BUFFER* buffer = arg->buffer;
936 char dst_str[100], src0_str[100];
937 char dst_reg[50], src0_reg[50];
938 char dst_mask[6], src0_mask[6];
940 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
941 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
942 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
943 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
946 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
947 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
949 CONST SHADER_OPCODE* curOpcode = arg->opcode;
950 SHADER_BUFFER* buffer = arg->buffer;
952 char dst_str[100], src0_str[100], src1_str[100];
953 char dst_reg[50], src0_reg[50], src1_reg[50];
954 char dst_mask[6], src0_mask[6], src1_mask[6];
957 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
958 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
959 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
961 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpDest);
963 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
964 if (curOpcode->opcode == WINED3DSIO_DP4)
965 strcpy(cast, "vec4(");
967 strcpy(cast, "vec3(");
969 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
970 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
973 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
974 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
976 CONST SHADER_OPCODE* curOpcode = arg->opcode;
977 SHADER_BUFFER* buffer = arg->buffer;
979 char dst_str[100], src_str[100];
980 char dst_reg[50], src_reg[50];
981 char dst_mask[6], src_mask[6];
984 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
986 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
988 /* Determine the GLSL function to use based on the opcode */
989 /* TODO: Possibly make this a table for faster lookups */
990 switch (curOpcode->opcode) {
991 case WINED3DSIO_MIN: strcat(tmpLine, "min"); break;
992 case WINED3DSIO_MAX: strcat(tmpLine, "max"); break;
993 case WINED3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
994 case WINED3DSIO_ABS: strcat(tmpLine, "abs"); break;
995 case WINED3DSIO_FRC: strcat(tmpLine, "fract"); break;
996 case WINED3DSIO_POW: strcat(tmpLine, "pow"); break;
997 case WINED3DSIO_CRS: strcat(tmpLine, "cross"); break;
998 case WINED3DSIO_NRM: strcat(tmpLine, "normalize"); break;
999 case WINED3DSIO_LOGP:
1000 case WINED3DSIO_LOG: strcat(tmpLine, "log2"); break;
1001 case WINED3DSIO_EXP: strcat(tmpLine, "exp2"); break;
1002 case WINED3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
1003 case WINED3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
1004 case WINED3DSIO_SGN: strcat(tmpLine, "sign"); break;
1006 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1010 strcat(tmpLine, "(");
1012 if (curOpcode->num_params > 0) {
1013 strcat(tmpLine, "vec4(");
1014 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1015 strcat(tmpLine, src_str);
1016 strcat(tmpLine, ")");
1017 for (i = 2; i < curOpcode->num_params; ++i) {
1018 strcat(tmpLine, ", vec4(");
1019 shader_glsl_add_param(arg, arg->src[i-1], arg->src_addr[i-1], TRUE, src_reg, src_mask, src_str);
1020 strcat(tmpLine, src_str);
1021 strcat(tmpLine, ")");
1024 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
1028 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1029 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1030 * dst.x = 2^(floor(src))
1031 * dst.y = src - floor(src)
1032 * dst.z = 2^src (partial precision is allowed, but optional)
1034 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1035 * dst = 2^src; (partial precision is allowed, but optional)
1037 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1040 char dst_str[100], src_str[100];
1041 char dst_reg[50], src_reg[50];
1042 char dst_mask[6], src_mask[6];
1043 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1044 DWORD hex_version = This->baseShader.hex_version;
1046 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1047 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1048 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1050 if (hex_version < WINED3DPS_VERSION(2,0)) {
1051 shader_addline(arg->buffer, "tmp0.x = vec4(exp2(floor(%s))).x;\n", src_str);
1052 shader_addline(arg->buffer, "tmp0.y = vec4(%s - floor(%s)).y;\n", src_str, src_str);
1053 shader_addline(arg->buffer, "tmp0.z = vec4(exp2(%s)).x;\n", src_str);
1054 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1055 shader_addline(arg->buffer, "%svec4(tmp0))%s;\n", tmpLine, dst_mask);
1057 shader_addline(arg->buffer, "%svec4(exp2(%s)))%s;\n", tmpLine, src_str, dst_mask);
1061 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1062 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1065 char dst_str[100], src_str[100];
1066 char dst_reg[50], src_reg[50];
1067 char dst_mask[6], src_mask[6];
1069 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1070 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src_reg, src_mask, src_str);
1071 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1072 strcat(tmpLine, "1.0 / ");
1073 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
1076 /** Process signed comparison opcodes in GLSL. */
1077 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1080 char dst_str[100], src0_str[100], src1_str[100];
1081 char dst_reg[50], src0_reg[50], src1_reg[50];
1082 char dst_mask[6], src0_mask[6], src1_mask[6];
1084 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1085 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1086 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1088 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
1089 if (strlen(src0_mask) != 2) {
1090 shader_glsl_map2gl(arg);
1094 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1096 switch (arg->opcode->opcode) {
1097 case WINED3DSIO_SLT: strcpy(compareStr, "<"); break;
1098 case WINED3DSIO_SGE: strcpy(compareStr, ">="); break;
1100 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1102 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
1103 tmpLine, src0_str, compareStr, src1_str, dst_mask);
1107 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
1108 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1111 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1112 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1113 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1115 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1116 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1117 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1118 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1120 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1121 shader_addline(arg->buffer, "%smix(vec4(%s), vec4(%s), vec4(lessThan(vec4(%s), vec4(0.0)))))%s;\n",
1122 tmpLine, src1_str, src2_str, src0_str, dst_mask);
1125 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
1126 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1129 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1130 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1131 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1133 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1134 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1135 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1136 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1137 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1138 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
1139 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1142 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1143 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1146 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1147 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1148 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1150 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1151 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1152 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1153 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1154 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1156 shader_addline(arg->buffer, "%s(vec4(%s) * vec4(%s)) + vec4(%s))%s;\n",
1157 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1160 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1161 Vertex shaders to GLSL codes */
1162 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1164 int nComponents = 0;
1165 SHADER_OPCODE_ARG tmpArg;
1167 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1169 /* Set constants for the temporary argument */
1170 tmpArg.shader = arg->shader;
1171 tmpArg.buffer = arg->buffer;
1172 tmpArg.src[0] = arg->src[0];
1173 tmpArg.src_addr[0] = arg->src_addr[0];
1174 tmpArg.src_addr[1] = arg->src_addr[1];
1175 tmpArg.reg_maps = arg->reg_maps;
1177 switch(arg->opcode->opcode) {
1178 case WINED3DSIO_M4x4:
1180 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1182 case WINED3DSIO_M4x3:
1184 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1186 case WINED3DSIO_M3x4:
1188 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1190 case WINED3DSIO_M3x3:
1192 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1194 case WINED3DSIO_M3x2:
1196 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1202 for (i = 0; i < nComponents; i++) {
1203 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1204 tmpArg.src[1] = arg->src[1]+i;
1205 shader_glsl_dot(&tmpArg);
1210 The LRP instruction performs a component-wise linear interpolation
1211 between the second and third operands using the first operand as the
1212 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
1214 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1217 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1218 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1219 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1221 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1222 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1223 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1224 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1226 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1228 shader_addline(arg->buffer, "%s%s + %s * (%s - %s))%s;\n",
1229 tmpLine, src2_str, src0_str, src1_str, src2_str, dst_mask);
1232 /** Process the WINED3DSIO_LIT instruction in GLSL:
1233 * dst.x = dst.w = 1.0
1234 * dst.y = (src0.x > 0) ? src0.x
1235 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1236 * where src.w is clamped at +- 128
1238 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1240 char dst_str[100], src0_str[100];
1241 char dst_reg[50], src0_reg[50];
1242 char dst_mask[6], src0_mask[6];
1244 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1245 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1247 shader_addline(arg->buffer,
1248 "%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",
1249 dst_str, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, dst_mask);
1252 /** Process the WINED3DSIO_DST instruction in GLSL:
1254 * dst.y = src0.x * src0.y
1258 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1260 char dst_str[100], src0_str[100], src1_str[100];
1261 char dst_reg[50], src0_reg[50], src1_reg[50];
1262 char dst_mask[6], src0_mask[6], src1_mask[6];
1264 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1265 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1266 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1268 shader_addline(arg->buffer, "%s = vec4(1.0, %s.x * %s.y, %s.z, %s.w)%s;\n",
1269 dst_str, src0_reg, src1_reg, src0_reg, src1_reg, dst_mask);
1272 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1273 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1274 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1276 * dst.x = cos(src0.?)
1277 * dst.y = sin(src0.?)
1281 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1283 char dst_str[100], src0_str[100];
1284 char dst_reg[50], src0_reg[50];
1285 char dst_mask[6], src0_mask[6];
1287 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1288 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1290 shader_addline(arg->buffer, "%s = vec4(cos(%s), sin(%s), %s.z, %s.w)%s;\n",
1291 dst_str, src0_str, src0_str, dst_reg, dst_reg, dst_mask);
1294 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1295 * Start a for() loop where src0.y is the initial value of aL,
1296 * increment aL by src0.z for a total of src0.x iterations.
1297 * Need to use a temporary variable for this operation.
1299 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1305 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1307 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1308 src1_reg, src1_reg, src1_reg);
1311 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1312 shader_addline(arg->buffer, "}\n");
1315 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1321 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1322 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s.x; tmpInt++) {\n", src0_reg);
1325 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1331 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1332 shader_addline(arg->buffer, "if (%s) {\n", src0_str);
1335 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1337 char src0_str[100], src1_str[100];
1338 char src0_reg[50], src1_reg[50];
1339 char src0_mask[6], src1_mask[6];
1341 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1342 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1344 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1345 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1348 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1349 shader_addline(arg->buffer, "} else {\n");
1352 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1353 shader_addline(arg->buffer, "break;\n");
1356 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1358 char src0_str[100], src1_str[100];
1359 char src0_reg[50], src1_reg[50];
1360 char src0_mask[6], src1_mask[6];
1362 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1363 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1365 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1366 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1369 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1371 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1372 shader_addline(arg->buffer, "}\n");
1373 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1376 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1377 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1378 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1381 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1387 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1388 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1389 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_str, snum);
1392 /*********************************************
1393 * Pixel Shader Specific Code begins here
1394 ********************************************/
1395 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1396 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1398 DWORD hex_version = This->baseShader.hex_version;
1400 char dst_str[100], dst_reg[50], dst_mask[6];
1401 char coord_str[100], coord_reg[50], coord_mask[6];
1403 /* All versions have a destination register */
1404 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1406 /* 1.0-1.3: Use destination register as coordinate source.
1407 1.4+: Use provided coordinate source register. */
1408 if (hex_version < WINED3DPS_VERSION(1,4))
1409 strcpy(coord_reg, dst_reg);
1411 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, coord_reg, coord_mask, coord_str);
1413 /* 1.0-1.4: Use destination register as sampler source.
1414 * 2.0+: Use provided sampler source. */
1415 if (hex_version < WINED3DPS_VERSION(2,0)) {
1416 shader_glsl_sample(arg, arg->dst & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1418 shader_glsl_sample(arg, arg->src[1] & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1423 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1425 /* FIXME: Make this work for more than just 2D textures */
1427 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1428 SHADER_BUFFER* buffer = arg->buffer;
1429 DWORD hex_version = This->baseShader.hex_version;
1436 shader_glsl_add_param(arg, arg->dst, 0, FALSE, tmpReg, tmpMask, tmpStr);
1438 if (hex_version != WINED3DPS_VERSION(1,4)) {
1439 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1440 shader_addline(buffer, "%s = clamp(gl_TexCoord[%u], 0.0, 1.0);\n", tmpReg, reg);
1442 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1443 shader_addline(buffer, "%s = gl_TexCoord[%u]%s;\n", tmpStr, reg2, tmpMask);
1447 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1448 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1449 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1450 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1452 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1453 char src0_str[100], dst_str[100];
1454 char src0_name[50], dst_name[50];
1455 char src0_mask[6], dst_mask[6];
1457 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1458 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1460 shader_addline(arg->buffer, "tmp0.x = dot(vec3(gl_TexCoord[%u]), vec3(%s));\n", dstreg, src0_str);
1461 shader_addline(arg->buffer, "%s = vec4(texture1D(Psampler%u, tmp0.x))%s;\n", dst_str, dstreg, dst_mask);
1464 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1465 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1466 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1468 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1469 char src0_str[100], dst_str[100];
1470 char src0_name[50], dst_name[50];
1471 char src0_mask[6], dst_mask[6];
1473 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1474 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1476 shader_addline(arg->buffer, "%s = vec4(dot(vec3(T%u), vec3(%s)))%s;\n",
1477 dst_str, dstreg, src0_str, dst_mask);
1480 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1481 * Calculate the depth as dst.x / dst.y */
1482 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1488 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1490 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_reg, dst_reg);
1493 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1494 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1495 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1496 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1498 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1500 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1501 char src0_str[100], dst_str[100];
1502 char src0_name[50], dst_name[50];
1503 char src0_mask[6], dst_mask[6];
1505 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1506 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1508 shader_addline(arg->buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", dstreg, src0_str);
1509 shader_addline(arg->buffer, "gl_FragDepth = vec4((tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y)%s;\n", dst_str, dst_name);
1512 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1513 * Calculate the 1st of a 2-row matrix multiplication. */
1514 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1516 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1517 SHADER_BUFFER* buffer = arg->buffer;
1522 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1523 shader_addline(buffer, "tmp0.x = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1526 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1527 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1528 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1530 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1531 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1532 SHADER_BUFFER* buffer = arg->buffer;
1533 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1538 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1539 shader_addline(buffer, "tmp0.%c = dot(vec3(T%u), vec3(%s));\n", 'x' + current_state->current_row, reg, src0_str);
1540 current_state->texcoord_w[current_state->current_row++] = reg;
1543 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1545 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1546 SHADER_BUFFER* buffer = arg->buffer;
1551 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1552 shader_addline(buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1554 /* Sample the texture using the calculated coordinates */
1555 sprintf(dst_str, "T%u", reg);
1556 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1559 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1560 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1561 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1566 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1567 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1568 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1570 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1571 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1573 /* Sample the texture using the calculated coordinates */
1574 sprintf(dst_str, "T%u", reg);
1575 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1576 current_state->current_row = 0;
1579 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1580 * Perform the 3rd row of a 3x3 matrix multiply */
1581 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
1586 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1587 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1588 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1590 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1592 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1593 shader_addline(arg->buffer, "T%u = vec4(tmp0.x, tmp0.y, tmp0.z, 1.0);\n", reg);
1594 current_state->current_row = 0;
1597 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1598 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1599 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1601 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1602 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1605 char src0_str[100], src0_name[50], src0_mask[6];
1606 char src1_str[100], src1_name[50], src1_mask[6];
1607 SHADER_BUFFER* buffer = arg->buffer;
1608 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1609 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1612 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1613 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1614 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1616 strcpy(dimensions, "");
1617 FIXME("Unrecognized sampler type: %#x\n", stype);
1621 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1622 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_name, src1_mask, src1_str);
1624 /* Perform the last matrix multiply operation */
1625 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1627 /* Calculate reflection vector */
1628 shader_addline(buffer, "tmp0.xyz = reflect(-vec3(%s), vec3(tmp0));\n", src1_str);
1630 /* Sample the texture */
1631 sprintf(dst_str, "T%u", reg);
1632 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1633 current_state->current_row = 0;
1636 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1637 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1638 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1640 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1641 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1642 SHADER_BUFFER* buffer = arg->buffer;
1643 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1645 char src0_str[100], src0_name[50], src0_mask[6];
1647 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_name, src0_mask, src0_str);
1649 /* Perform the last matrix multiply operation */
1650 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1652 /* Construct the eye-ray vector from w coordinates */
1653 shader_addline(buffer, "tmp1.x = gl_TexCoord[%u].w;\n", current_state->texcoord_w[0]);
1654 shader_addline(buffer, "tmp1.y = gl_TexCoord[%u].w;\n", current_state->texcoord_w[1]);
1655 shader_addline(buffer, "tmp1.z = gl_TexCoord[%u].w;\n", reg);
1657 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
1658 shader_addline(buffer, "tmp0.x = dot(vec3(tmp0), vec3(tmp1));\n");
1659 shader_addline(buffer, "tmp0 = tmp0.w * tmp0;\n");
1660 shader_addline(buffer, "tmp0 = (2.0 * tmp0) - tmp1;\n");
1662 /* Sample the texture using the calculated coordinates */
1663 sprintf(dst_str, "T%u", reg);
1664 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1665 current_state->current_row = 0;
1668 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1669 * Apply a fake bump map transform.
1670 * FIXME: Should apply the BUMPMAPENV matrix. For now, just sample the texture */
1671 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1673 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1674 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1676 FIXME("Not applying the BUMPMAPENV matrix for pixel shader instruction texbem.\n");
1677 shader_addline(arg->buffer, "T%u = texture2D(Psampler%u, gl_TexCoord[%u].xy + T%u.xy);\n",
1678 reg1, reg1, reg1, reg2);
1681 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1682 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1683 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1686 char dst_str[100], src0_str[100];
1687 char dst_reg[50], src0_reg[50];
1688 char dst_mask[6], src0_mask[6];
1689 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1691 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1692 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1694 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1695 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1696 tmpLine, src0_regnum, dst_reg, dst_mask);
1699 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1700 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1701 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1704 char dst_str[100], src0_str[100];
1705 char dst_reg[50], src0_reg[50];
1706 char dst_mask[6], src0_mask[6];
1707 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1709 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1710 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1712 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1713 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1714 tmpLine, src0_regnum, dst_reg, dst_mask);
1717 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
1718 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
1719 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1722 char dst_str[100], src0_str[100];
1723 char dst_reg[50], src0_reg[50];
1724 char dst_mask[6], src0_mask[6];
1726 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1727 DWORD stype = arg->reg_maps->samplers[src0_regnum] & WINED3DSP_TEXTURETYPE_MASK;
1729 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1730 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1731 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1733 strcpy(dimensions, "");
1734 FIXME("Unrecognized sampler type: %#x\n", stype);
1738 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1739 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1741 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1742 shader_addline(arg->buffer, "%stexture%s(Psampler%u, %s.%s))%s;\n",
1743 tmpLine, dimensions, src0_regnum, dst_reg, (stype == WINED3DSTT_2D) ? "xy" : "xyz", dst_mask);
1746 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
1747 * If any of the first 3 components are < 0, discard this pixel */
1748 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
1750 char dst_str[100], dst_name[50], dst_mask[6];
1752 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_name, dst_mask, dst_str);
1753 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_name);
1756 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
1757 * dst = dot2(src0, src1) + src2 */
1758 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
1761 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1762 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1763 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1765 shader_glsl_add_param(arg, arg->dst, 0, FALSE, dst_reg, dst_mask, dst_str);
1766 shader_glsl_add_param(arg, arg->src[0], arg->src_addr[0], TRUE, src0_reg, src0_mask, src0_str);
1767 shader_glsl_add_param(arg, arg->src[1], arg->src_addr[1], TRUE, src1_reg, src1_mask, src1_str);
1768 shader_glsl_add_param(arg, arg->src[2], arg->src_addr[2], TRUE, src2_reg, src2_mask, src2_str);
1769 shader_glsl_add_dst(arg->dst, dst_reg, dst_mask, tmpLine);
1770 shader_addline(arg->buffer, "%sdot(vec2(%s), vec2(%s)) + %s)%s;\n",
1771 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1774 void pshader_glsl_input_pack(
1775 SHADER_BUFFER* buffer,
1776 semantic* semantics_in) {
1780 for (i = 0; i < MAX_REG_INPUT; i++) {
1782 DWORD usage_token = semantics_in[i].usage;
1783 DWORD register_token = semantics_in[i].reg;
1784 DWORD usage, usage_idx;
1788 if (!usage_token) continue;
1789 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1790 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1791 shader_glsl_get_output_register_swizzle(register_token, reg_mask);
1795 case D3DDECLUSAGE_COLOR:
1797 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
1798 i, reg_mask, reg_mask);
1799 else if (usage_idx == 1)
1800 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
1801 i, reg_mask, reg_mask);
1803 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
1804 i, reg_mask, reg_mask);
1807 case D3DDECLUSAGE_TEXCOORD:
1808 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
1809 i, reg_mask, usage_idx, reg_mask );
1812 case D3DDECLUSAGE_FOG:
1813 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
1814 i, reg_mask, reg_mask);
1818 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
1819 i, reg_mask, reg_mask);
1824 /*********************************************
1825 * Vertex Shader Specific Code begins here
1826 ********************************************/
1828 void vshader_glsl_output_unpack(
1829 SHADER_BUFFER* buffer,
1830 semantic* semantics_out) {
1834 for (i = 0; i < MAX_REG_OUTPUT; i++) {
1836 DWORD usage_token = semantics_out[i].usage;
1837 DWORD register_token = semantics_out[i].reg;
1838 DWORD usage, usage_idx;
1842 if (!usage_token) continue;
1844 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1845 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1846 shader_glsl_get_output_register_swizzle(register_token, reg_mask);
1850 case D3DDECLUSAGE_COLOR:
1852 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1853 else if (usage_idx == 1)
1854 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1856 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1859 case D3DDECLUSAGE_POSITION:
1860 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1863 case D3DDECLUSAGE_TEXCOORD:
1864 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
1865 usage_idx, reg_mask, i, reg_mask);
1868 case WINED3DSHADERDECLUSAGE_PSIZE:
1869 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
1872 case WINED3DSHADERDECLUSAGE_FOG:
1873 shader_addline(buffer, "gl_FogFragCoord%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1877 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1882 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
1883 GLhandleARB program_id;
1884 GLhandleARB vshader_id, pshader_id;
1885 const char *blt_vshader[] = {
1888 " gl_Position = gl_Vertex;\n"
1889 " gl_FrontColor = vec4(1.0);\n"
1890 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
1891 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
1895 const char *blt_pshader[] = {
1896 "uniform sampler2D sampler;\n"
1899 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
1903 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
1904 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
1905 GL_EXTCALL(glCompileShaderARB(vshader_id));
1907 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
1908 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
1909 GL_EXTCALL(glCompileShaderARB(pshader_id));
1911 program_id = GL_EXTCALL(glCreateProgramObjectARB());
1912 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
1913 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
1914 GL_EXTCALL(glLinkProgramARB(program_id));
1916 print_glsl_info_log(&GLINFO_LOCATION, program_id);
1921 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
1922 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1923 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1924 GLhandleARB program_id = 0;
1926 if (useVS || usePS) set_glsl_shader_program(iface);
1927 else This->stateBlock->glsl_program = NULL;
1929 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
1930 if (program_id) TRACE("Using GLSL program %u\n", program_id);
1931 GL_EXTCALL(glUseProgramObjectARB(program_id));
1932 checkGLcall("glUseProgramObjectARB");
1935 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
1936 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1937 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1938 static GLhandleARB program_id = 0;
1939 static GLhandleARB loc = -1;
1942 program_id = create_glsl_blt_shader(gl_info);
1943 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
1946 GL_EXTCALL(glUseProgramObjectARB(program_id));
1947 GL_EXTCALL(glUniform1iARB(loc, 0));
1950 static void shader_glsl_cleanup(BOOL usePS, BOOL useVS) {
1954 const shader_backend_t glsl_shader_backend = {
1955 &shader_glsl_select,
1956 &shader_glsl_select_depth_blt,
1957 &shader_glsl_load_constants,
1958 &shader_glsl_cleanup