wined3d: Pass the correct mask to shader_glsl_add_src_param() in shader_glsl_get_regi...
[wine/testsucceed.git] / dlls / wined3d / glsl_shader.c
blob8ff8942b6a04551f9dc319598a0cadb9d0b4a554
1 /*
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.
30 #include "config.h"
31 #include <stdio.h>
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 #define GLINFO_LOCATION (*gl_info)
38 /** Prints the GLSL info log which will contain error messages if they exist */
39 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
41 int infologLength = 0;
42 char *infoLog;
44 GL_EXTCALL(glGetObjectParameterivARB(obj,
45 GL_OBJECT_INFO_LOG_LENGTH_ARB,
46 &infologLength));
48 /* A size of 1 is just a null-terminated string, so the log should be bigger than
49 * that if there are errors. */
50 if (infologLength > 1)
52 infoLog = (char *)HeapAlloc(GetProcessHeap(), 0, infologLength);
53 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
54 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
55 HeapFree(GetProcessHeap(), 0, infoLog);
59 /**
60 * Loads (pixel shader) samplers
62 void shader_glsl_load_psamplers(
63 WineD3D_GL_Info *gl_info,
64 IWineD3DStateBlock* iface) {
66 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
67 GLhandleARB programId = stateBlock->glsl_program->programId;
68 GLhandleARB name_loc;
69 int i;
70 char sampler_name[20];
72 for (i=0; i< GL_LIMITS(samplers); ++i) {
73 if (stateBlock->textures[i] != NULL) {
74 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
75 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
76 if (name_loc != -1) {
77 TRACE("Loading %s for texture %d\n", sampler_name, i);
78 GL_EXTCALL(glUniform1iARB(name_loc, i));
79 checkGLcall("glUniform1iARB");
85 /**
86 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
87 * When constant_list == NULL, it will load all the constants.
89 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
90 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
91 struct list *constant_list) {
92 local_constant* lconst;
93 GLhandleARB tmp_loc;
94 int i;
96 if (!constant_list) {
97 if (TRACE_ON(d3d_shader)) {
98 for (i = 0; i < max_constants; ++i) {
99 tmp_loc = constant_locations[i];
100 if (tmp_loc != -1) {
101 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
102 constants[i * 4 + 0], constants[i * 4 + 1],
103 constants[i * 4 + 2], constants[i * 4 + 3]);
107 for (i = 0; i < max_constants; ++i) {
108 tmp_loc = constant_locations[i];
109 if (tmp_loc != -1) {
110 /* We found this uniform name in the program - go ahead and send the data */
111 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
114 checkGLcall("glUniform4fvARB()");
115 } else {
116 constant_entry *constant;
117 if (TRACE_ON(d3d_shader)) {
118 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
119 i = constant->idx;
120 tmp_loc = constant_locations[i];
121 if (tmp_loc != -1) {
122 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
123 constants[i * 4 + 0], constants[i * 4 + 1],
124 constants[i * 4 + 2], constants[i * 4 + 3]);
128 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
129 i = constant->idx;
130 tmp_loc = constant_locations[i];
131 if (tmp_loc != -1) {
132 /* We found this uniform name in the program - go ahead and send the data */
133 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
136 checkGLcall("glUniform4fvARB()");
139 /* Load immediate constants */
140 if (TRACE_ON(d3d_shader)) {
141 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
142 tmp_loc = constant_locations[lconst->idx];
143 if (tmp_loc != -1) {
144 GLfloat* values = (GLfloat*)lconst->value;
145 TRACE("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
146 values[0], values[1], values[2], values[3]);
150 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
151 tmp_loc = constant_locations[lconst->idx];
152 if (tmp_loc != -1) {
153 /* We found this uniform name in the program - go ahead and send the data */
154 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
157 checkGLcall("glUniform4fvARB()");
160 /**
161 * Loads integer constants (aka uniforms) into the currently set GLSL program.
162 * When @constants_set == NULL, it will load all the constants.
164 void shader_glsl_load_constantsI(
165 IWineD3DBaseShaderImpl* This,
166 WineD3D_GL_Info *gl_info,
167 GLhandleARB programId,
168 unsigned max_constants,
169 int* constants,
170 BOOL* constants_set) {
172 GLhandleARB tmp_loc;
173 int i;
174 char tmp_name[8];
175 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
176 const char* prefix = is_pshader? "PI":"VI";
177 struct list* ptr;
179 for (i=0; i<max_constants; ++i) {
180 if (NULL == constants_set || constants_set[i]) {
182 TRACE("Loading constants %i: %i, %i, %i, %i\n",
183 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
185 /* TODO: Benchmark and see if it would be beneficial to store the
186 * locations of the constants to avoid looking up each time */
187 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
188 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
189 if (tmp_loc != -1) {
190 /* We found this uniform name in the program - go ahead and send the data */
191 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
192 checkGLcall("glUniform4ivARB");
197 /* Load immediate constants */
198 ptr = list_head(&This->baseShader.constantsI);
199 while (ptr) {
200 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
201 unsigned int idx = lconst->idx;
202 GLint* values = (GLint*) lconst->value;
204 TRACE("Loading local constants %i: %i, %i, %i, %i\n", idx,
205 values[0], values[1], values[2], values[3]);
207 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
208 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
209 if (tmp_loc != -1) {
210 /* We found this uniform name in the program - go ahead and send the data */
211 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
212 checkGLcall("glUniform4ivARB");
214 ptr = list_next(&This->baseShader.constantsI, ptr);
218 /**
219 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
220 * When @constants_set == NULL, it will load all the constants.
222 void shader_glsl_load_constantsB(
223 IWineD3DBaseShaderImpl* This,
224 WineD3D_GL_Info *gl_info,
225 GLhandleARB programId,
226 unsigned max_constants,
227 BOOL* constants,
228 BOOL* constants_set) {
230 GLhandleARB tmp_loc;
231 int i;
232 char tmp_name[8];
233 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
234 const char* prefix = is_pshader? "PB":"VB";
235 struct list* ptr;
237 for (i=0; i<max_constants; ++i) {
238 if (NULL == constants_set || constants_set[i]) {
240 TRACE("Loading constants %i: %i;\n", i, constants[i*4]);
242 /* TODO: Benchmark and see if it would be beneficial to store the
243 * locations of the constants to avoid looking up each time */
244 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
245 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
246 if (tmp_loc != -1) {
247 /* We found this uniform name in the program - go ahead and send the data */
248 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
249 checkGLcall("glUniform1ivARB");
254 /* Load immediate constants */
255 ptr = list_head(&This->baseShader.constantsB);
256 while (ptr) {
257 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
258 unsigned int idx = lconst->idx;
259 GLint* values = (GLint*) lconst->value;
261 TRACE("Loading local constants %i: %i\n", idx, values[0]);
263 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
264 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
265 if (tmp_loc != -1) {
266 /* We found this uniform name in the program - go ahead and send the data */
267 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
268 checkGLcall("glUniform1ivARB");
270 ptr = list_next(&This->baseShader.constantsB, ptr);
277 * Loads the app-supplied constants into the currently set GLSL program.
279 void shader_glsl_load_constants(
280 IWineD3DDevice* device,
281 char usePixelShader,
282 char useVertexShader) {
284 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
285 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
286 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*) deviceImpl->wineD3D)->gl_info;
288 GLhandleARB *constant_locations;
289 struct list *constant_list;
290 GLhandleARB programId;
292 if (!stateBlock->glsl_program) {
293 /* No GLSL program set - nothing to do. */
294 return;
296 programId = stateBlock->glsl_program->programId;
298 if (useVertexShader) {
299 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
300 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) vshader;
301 GLint pos;
303 IWineD3DVertexDeclarationImpl* vertexDeclaration =
304 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
306 constant_locations = stateBlock->glsl_program->vuniformF_locations;
307 constant_list = &stateBlock->set_vconstantsF;
309 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
310 /* Load DirectX 8 float constants/uniforms for vertex shader */
311 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
312 vertexDeclaration->constants, constant_locations, NULL);
315 /* Load DirectX 9 float constants/uniforms for vertex shader */
316 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
317 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
319 /* Load DirectX 9 integer constants/uniforms for vertex shader */
320 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
321 stateBlock->vertexShaderConstantI,
322 stateBlock->set.vertexShaderConstantsI);
324 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
325 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
326 stateBlock->vertexShaderConstantB,
327 stateBlock->set.vertexShaderConstantsB);
329 /* Upload the position fixup params */
330 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
331 checkGLcall("glGetUniformLocationARB");
332 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
333 checkGLcall("glUniform4fvARB");
336 if (usePixelShader) {
338 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
340 constant_locations = stateBlock->glsl_program->puniformF_locations;
341 constant_list = &stateBlock->set_pconstantsF;
343 /* Load pixel shader samplers */
344 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
346 /* Load DirectX 9 float constants/uniforms for pixel shader */
347 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
348 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
350 /* Load DirectX 9 integer constants/uniforms for pixel shader */
351 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
352 stateBlock->pixelShaderConstantI,
353 stateBlock->set.pixelShaderConstantsI);
355 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
356 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
357 stateBlock->pixelShaderConstantB,
358 stateBlock->set.pixelShaderConstantsB);
362 /** Generate the variable & register declarations for the GLSL output target */
363 void shader_generate_glsl_declarations(
364 IWineD3DBaseShader *iface,
365 shader_reg_maps* reg_maps,
366 SHADER_BUFFER* buffer,
367 WineD3D_GL_Info* gl_info) {
369 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
370 int i;
372 /* There are some minor differences between pixel and vertex shaders */
373 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
374 char prefix = pshader ? 'P' : 'V';
376 /* Prototype the subroutines */
377 for (i = 0; i < This->baseShader.limits.label; i++) {
378 if (reg_maps->labels[i])
379 shader_addline(buffer, "void subroutine%lu();\n", i);
382 /* Declare the constants (aka uniforms) */
383 if (This->baseShader.limits.constant_float > 0) {
384 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
385 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
386 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
389 if (This->baseShader.limits.constant_int > 0)
390 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
392 if (This->baseShader.limits.constant_bool > 0)
393 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
395 if(!pshader)
396 shader_addline(buffer, "uniform vec4 posFixup;\n");
398 /* Declare texture samplers */
399 for (i = 0; i < This->baseShader.limits.sampler; i++) {
400 if (reg_maps->samplers[i]) {
402 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
403 switch (stype) {
405 case WINED3DSTT_1D:
406 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
407 break;
408 case WINED3DSTT_2D:
409 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
410 break;
411 case WINED3DSTT_CUBE:
412 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
413 break;
414 case WINED3DSTT_VOLUME:
415 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
416 break;
417 default:
418 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
419 FIXME("Unrecognized sampler type: %#x\n", stype);
420 break;
425 /* Declare address variables */
426 for (i = 0; i < This->baseShader.limits.address; i++) {
427 if (reg_maps->address[i])
428 shader_addline(buffer, "ivec4 A%d;\n", i);
431 /* Declare texture coordinate temporaries and initialize them */
432 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
433 if (reg_maps->texcoord[i])
434 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
437 /* Declare input register temporaries */
438 for (i=0; i < This->baseShader.limits.packed_input; i++) {
439 if (reg_maps->packed_input[i])
440 shader_addline(buffer, "vec4 IN%lu;\n", i);
443 /* Declare output register temporaries */
444 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
445 if (reg_maps->packed_output[i])
446 shader_addline(buffer, "vec4 OUT%lu;\n", i);
449 /* Declare temporary variables */
450 for(i = 0; i < This->baseShader.limits.temporary; i++) {
451 if (reg_maps->temporary[i])
452 shader_addline(buffer, "vec4 R%lu;\n", i);
455 /* Declare attributes */
456 for (i = 0; i < This->baseShader.limits.attributes; i++) {
457 if (reg_maps->attributes[i])
458 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
461 /* Declare loop register aL */
462 if (reg_maps->loop) {
463 shader_addline(buffer, "int aL;\n");
464 shader_addline(buffer, "int tmpInt;\n");
467 /* Temporary variables for matrix operations */
468 shader_addline(buffer, "vec4 tmp0;\n");
469 shader_addline(buffer, "vec4 tmp1;\n");
471 /* Start the main program */
472 shader_addline(buffer, "void main() {\n");
475 /*****************************************************************************
476 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
478 * For more information, see http://wiki.winehq.org/DirectX-Shaders
479 ****************************************************************************/
481 /* Prototypes */
482 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
483 const DWORD addr_token, DWORD mask, char *reg_name, char *swizzle, char *out_str);
485 /* FIXME: This should be removed */
486 static void shader_glsl_add_src_param_old(SHADER_OPCODE_ARG* arg, const DWORD param,
487 const DWORD addr_token, char *reg_name, char *swizzle, char *out_str) {
488 shader_glsl_add_src_param(arg, param, addr_token, 0, reg_name, swizzle, out_str);
491 /** Used for opcode modifiers - They multiply the result by the specified amount */
492 static const char * const shift_glsl_tab[] = {
493 "", /* 0 (none) */
494 "2.0 * ", /* 1 (x2) */
495 "4.0 * ", /* 2 (x4) */
496 "8.0 * ", /* 3 (x8) */
497 "16.0 * ", /* 4 (x16) */
498 "32.0 * ", /* 5 (x32) */
499 "", /* 6 (x64) */
500 "", /* 7 (x128) */
501 "", /* 8 (d256) */
502 "", /* 9 (d128) */
503 "", /* 10 (d64) */
504 "", /* 11 (d32) */
505 "0.0625 * ", /* 12 (d16) */
506 "0.125 * ", /* 13 (d8) */
507 "0.25 * ", /* 14 (d4) */
508 "0.5 * " /* 15 (d2) */
511 /** Print the beginning of the generated GLSL string. example: "reg_name.xyzw = vec4("
512 * Will also change the reg_mask if necessary (not all register types are equal in DX vs GL) */
513 static void shader_glsl_add_dst_old(DWORD param, const char* reg_name, char* reg_mask, char* outStr) {
515 int shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
516 char cast[6];
518 if ((shader_get_regtype(param) == WINED3DSPR_RASTOUT)
519 && ((param & WINED3DSP_REGNUM_MASK) != 0)) {
520 /* gl_FogFragCoord or glPointSize - both floats */
521 strcpy(cast, "float");
522 strcpy(reg_mask, "");
524 } else if (reg_name[0] == 'A') {
525 /* Address register for vertex shaders (ivec4) */
526 strcpy(cast, "ivec4");
528 } else {
529 /* Everything else should be a 4 component float vector */
530 strcpy(cast, "vec4");
533 sprintf(outStr, "%s%s = %s%s(", reg_name, reg_mask, shift_glsl_tab[shift], cast);
536 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
537 static void shader_glsl_gen_modifier (
538 const DWORD instr,
539 const char *in_reg,
540 const char *in_regswizzle,
541 char *out_str) {
543 out_str[0] = 0;
545 if (instr == WINED3DSIO_TEXKILL)
546 return;
548 switch (instr & WINED3DSP_SRCMOD_MASK) {
549 case WINED3DSPSM_NONE:
550 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
551 break;
552 case WINED3DSPSM_NEG:
553 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
554 break;
555 case WINED3DSPSM_NOT:
556 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
557 break;
558 case WINED3DSPSM_BIAS:
559 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
560 break;
561 case WINED3DSPSM_BIASNEG:
562 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
563 break;
564 case WINED3DSPSM_SIGN:
565 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
566 break;
567 case WINED3DSPSM_SIGNNEG:
568 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
569 break;
570 case WINED3DSPSM_COMP:
571 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
572 break;
573 case WINED3DSPSM_X2:
574 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
575 break;
576 case WINED3DSPSM_X2NEG:
577 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
578 break;
579 case WINED3DSPSM_DZ: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
580 sprintf(out_str, "vec4(%s.r / %s.b, %s.g / %s.b, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
581 break;
582 case WINED3DSPSM_DW:
583 sprintf(out_str, "vec4(%s.r / %s.a, %s.g / %s.a, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
584 break;
585 case WINED3DSPSM_ABS:
586 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
587 break;
588 case WINED3DSPSM_ABSNEG:
589 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
590 break;
591 default:
592 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
593 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
597 /** Writes the GLSL variable name that corresponds to the register that the
598 * DX opcode parameter is trying to access */
599 static void shader_glsl_get_register_name(
600 const DWORD param,
601 const DWORD addr_token,
602 char* regstr,
603 BOOL* is_color,
604 SHADER_OPCODE_ARG* arg) {
606 /* oPos, oFog and oPts in D3D */
607 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
609 DWORD reg = param & WINED3DSP_REGNUM_MASK;
610 DWORD regtype = shader_get_regtype(param);
611 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
612 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
613 WineD3D_GL_Info* gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
615 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
616 char tmpStr[50];
618 *is_color = FALSE;
620 switch (regtype) {
621 case WINED3DSPR_TEMP:
622 sprintf(tmpStr, "R%u", reg);
623 break;
624 case WINED3DSPR_INPUT:
625 if (pshader) {
626 /* Pixel shaders >= 3.0 */
627 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
628 sprintf(tmpStr, "IN%u", reg);
629 else {
630 if (reg==0)
631 strcpy(tmpStr, "gl_Color");
632 else
633 strcpy(tmpStr, "gl_SecondaryColor");
635 } else {
636 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
637 *is_color = TRUE;
638 sprintf(tmpStr, "attrib%u", reg);
640 break;
641 case WINED3DSPR_CONST:
643 const char* prefix = pshader? "PC":"VC";
645 /* Relative addressing */
646 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
648 /* Relative addressing on shaders 2.0+ have a relative address token,
649 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
650 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
651 char relStr[100], relReg[50], relMask[6];
652 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, relReg, relMask, relStr);
653 sprintf(tmpStr, "%s[%s + %u]", prefix, relStr, reg);
654 } else
655 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
657 } else
658 sprintf(tmpStr, "%s[%u]", prefix, reg);
660 break;
662 case WINED3DSPR_CONSTINT:
663 if (pshader)
664 sprintf(tmpStr, "PI[%u]", reg);
665 else
666 sprintf(tmpStr, "VI[%u]", reg);
667 break;
668 case WINED3DSPR_CONSTBOOL:
669 if (pshader)
670 sprintf(tmpStr, "PB[%u]", reg);
671 else
672 sprintf(tmpStr, "VB[%u]", reg);
673 break;
674 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
675 if (pshader) {
676 sprintf(tmpStr, "T%u", reg);
677 } else {
678 sprintf(tmpStr, "A%u", reg);
680 break;
681 case WINED3DSPR_LOOP:
682 sprintf(tmpStr, "aL");
683 break;
684 case WINED3DSPR_SAMPLER:
685 if (pshader)
686 sprintf(tmpStr, "Psampler%u", reg);
687 else
688 sprintf(tmpStr, "Vsampler%u", reg);
689 break;
690 case WINED3DSPR_COLOROUT:
691 if (reg >= GL_LIMITS(buffers)) {
692 WARN("Write to render target %u, only %d supported\n", reg, 4);
694 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
695 sprintf(tmpStr, "gl_FragData[%u]", reg);
696 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
697 sprintf(tmpStr, "gl_FragColor");
699 break;
700 case WINED3DSPR_RASTOUT:
701 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
702 break;
703 case WINED3DSPR_DEPTHOUT:
704 sprintf(tmpStr, "gl_FragDepth");
705 break;
706 case WINED3DSPR_ATTROUT:
707 if (reg == 0) {
708 sprintf(tmpStr, "gl_FrontColor");
709 } else {
710 sprintf(tmpStr, "gl_FrontSecondaryColor");
712 break;
713 case WINED3DSPR_TEXCRDOUT:
714 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
715 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
716 sprintf(tmpStr, "OUT%u", reg);
717 else
718 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
719 break;
720 default:
721 FIXME("Unhandled register name Type(%d)\n", regtype);
722 sprintf(tmpStr, "unrecognized_register");
723 break;
726 strcat(regstr, tmpStr);
729 /* Get the GLSL write mask for the destination register */
730 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
731 char *ptr = write_mask;
732 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
734 /* gl_FogFragCoord and glPointSize are floats, fixup the write mask. */
735 if ((shader_get_regtype(param) == WINED3DSPR_RASTOUT) && ((param & WINED3DSP_REGNUM_MASK) != 0)) {
736 mask = WINED3DSP_WRITEMASK_0;
737 } else {
738 if (mask != WINED3DSP_WRITEMASK_ALL) {
739 *ptr++ = '.';
740 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
741 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
742 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
743 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
747 *ptr = '\0';
749 return mask;
752 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
753 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
754 * but addressed as "rgba". To fix this we need to swap the register's x
755 * and z components. */
756 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
757 char *ptr = swizzle_str;
759 /* swizzle bits fields: wwzzyyxx */
760 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
761 DWORD swizzle_x = swizzle & 0x03;
762 DWORD swizzle_y = (swizzle >> 2) & 0x03;
763 DWORD swizzle_z = (swizzle >> 4) & 0x03;
764 DWORD swizzle_w = (swizzle >> 6) & 0x03;
766 /* FIXME: This is here to keep shader_glsl_add_src_param_old happy.
767 * As soon as that is removed, this needs to go as well. */
768 if (!mask) {
769 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
770 * generate a swizzle string. Unless we need to our own swizzling. */
771 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
772 *ptr++ = '.';
773 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
774 *ptr++ = swizzle_chars[swizzle_x];
775 } else {
776 *ptr++ = swizzle_chars[swizzle_x];
777 *ptr++ = swizzle_chars[swizzle_y];
778 *ptr++ = swizzle_chars[swizzle_z];
779 *ptr++ = swizzle_chars[swizzle_w];
782 } else {
783 *ptr++ = '.';
784 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle_x];
785 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[swizzle_y];
786 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[swizzle_z];
787 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[swizzle_w];
790 *ptr = '\0';
793 /* From a given parameter token, generate the corresponding GLSL string.
794 * Also, return the actual register name and swizzle in case the
795 * caller needs this information as well. */
796 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
797 const DWORD addr_token, DWORD mask, char *reg_name, char *swizzle, char *out_str) {
798 BOOL is_color = FALSE;
799 swizzle[0] = reg_name[0] = out_str[0] = 0;
801 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
803 shader_glsl_get_swizzle(param, is_color, mask, swizzle);
804 shader_glsl_gen_modifier(param, reg_name, swizzle, out_str);
807 /* From a given parameter token, generate the corresponding GLSL string.
808 * Also, return the actual register name and swizzle in case the
809 * caller needs this information as well. */
810 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
811 const DWORD addr_token, char *reg_name, char *write_mask, char *out_str) {
812 BOOL is_color = FALSE;
813 DWORD mask;
814 write_mask[0] = reg_name[0] = out_str[0] = 0;
816 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
818 mask = shader_glsl_get_write_mask(param, write_mask);
819 sprintf(out_str, "%s%s", reg_name, write_mask);
821 return mask;
824 #if 0
825 /* Append the destination part of the instruction to the buffer, return the effective write mask */
826 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
827 char reg_name[50], write_mask[6], reg_str[100];
828 DWORD mask;
829 int shift;
831 shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
832 mask = shader_glsl_add_dst_param(arg, arg->dst, arg->dst_addr, reg_name, write_mask, reg_str);
833 shader_addline(buffer, "%s = %s(", reg_str, shift_glsl_tab[shift]);
835 return mask;
837 #endif
839 /** Process GLSL instruction modifiers */
840 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
842 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
844 if (arg->opcode->dst_token && mask != 0) {
845 char dst_reg[50];
846 char dst_mask[6];
847 char dst_str[100];
849 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
851 if (mask & WINED3DSPDM_SATURATE) {
852 /* _SAT means to clamp the value of the register to between 0 and 1 */
853 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
855 if (mask & WINED3DSPDM_MSAMPCENTROID) {
856 FIXME("_centroid modifier not handled\n");
858 if (mask & WINED3DSPDM_PARTIALPRECISION) {
859 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
864 static inline const char* shader_get_comp_op(
865 const DWORD opcode) {
867 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
868 switch (op) {
869 case COMPARISON_GT: return ">";
870 case COMPARISON_EQ: return "==";
871 case COMPARISON_GE: return ">=";
872 case COMPARISON_LT: return "<";
873 case COMPARISON_NE: return "!=";
874 case COMPARISON_LE: return "<=";
875 default:
876 FIXME("Unrecognized comparison value: %u\n", op);
877 return "(\?\?)";
881 static void shader_glsl_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
882 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
883 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
884 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
885 const char sampler_prefix = shader_is_pshader_version(This->baseShader.hex_version) ? 'P' : 'V';
886 SHADER_BUFFER* buffer = arg->buffer;
888 if(deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
889 /* Note that there's no such thing as a projected cube texture. */
890 switch(sampler_type) {
891 case WINED3DSTT_2D:
892 shader_addline(buffer, "%s = texture2DProj(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
893 break;
894 case WINED3DSTT_VOLUME:
895 shader_addline(buffer, "%s = texture3DProj(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
896 break;
897 default:
898 shader_addline(buffer, "%s = unrecognized_stype(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
899 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
900 break;
902 } else {
903 switch(sampler_type) {
904 case WINED3DSTT_2D:
905 shader_addline(buffer, "%s = texture2D(%csampler%u, %s.xy);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
906 break;
907 case WINED3DSTT_CUBE:
908 shader_addline(buffer, "%s = textureCube(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
909 break;
910 case WINED3DSTT_VOLUME:
911 shader_addline(buffer, "%s = texture3D(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
912 break;
913 default:
914 shader_addline(buffer, "%s = unrecognized_stype(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
915 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
916 break;
922 /*****************************************************************************
924 * Begin processing individual instruction opcodes
926 ****************************************************************************/
928 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
929 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
931 CONST SHADER_OPCODE* curOpcode = arg->opcode;
932 SHADER_BUFFER* buffer = arg->buffer;
933 char tmpLine[256];
934 char dst_reg[50], src0_reg[50], src1_reg[50];
935 char dst_mask[6], src0_mask[6], src1_mask[6];
936 char dst_str[100], src0_str[100], src1_str[100];
938 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
939 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
940 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
941 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
942 strcat(tmpLine, "vec4(");
943 strcat(tmpLine, src0_str);
944 strcat(tmpLine, ")");
946 /* Determine the GLSL operator to use based on the opcode */
947 switch (curOpcode->opcode) {
948 case WINED3DSIO_MUL: strcat(tmpLine, " * "); break;
949 case WINED3DSIO_ADD: strcat(tmpLine, " + "); break;
950 case WINED3DSIO_SUB: strcat(tmpLine, " - "); break;
951 default:
952 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
953 break;
955 shader_addline(buffer, "%svec4(%s))%s;\n", tmpLine, src1_str, dst_mask);
958 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
959 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
961 SHADER_BUFFER* buffer = arg->buffer;
962 char tmpLine[256];
963 char dst_str[100], src0_str[100];
964 char dst_reg[50], src0_reg[50];
965 char dst_mask[6], src0_mask[6];
967 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
968 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
969 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
970 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
973 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
974 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
976 CONST SHADER_OPCODE* curOpcode = arg->opcode;
977 SHADER_BUFFER* buffer = arg->buffer;
978 char tmpDest[100];
979 char dst_str[100], src0_str[100], src1_str[100];
980 char dst_reg[50], src0_reg[50], src1_reg[50];
981 char dst_mask[6], src0_mask[6], src1_mask[6];
982 char cast[6];
984 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
985 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
986 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
988 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpDest);
990 /* Need to cast the src vectors to vec3 for dp3, and vec4 for dp4 */
991 if (curOpcode->opcode == WINED3DSIO_DP4)
992 strcpy(cast, "vec4(");
993 else
994 strcpy(cast, "vec3(");
996 shader_addline(buffer, "%sdot(%s%s), %s%s)))%s;\n",
997 tmpDest, cast, src0_str, cast, src1_str, dst_mask);
1000 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1001 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1003 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1004 SHADER_BUFFER* buffer = arg->buffer;
1005 char tmpLine[256];
1006 char dst_str[100], src_str[100];
1007 char dst_reg[50], src_reg[50];
1008 char dst_mask[6], src_mask[6];
1009 unsigned i;
1011 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1013 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1015 /* Determine the GLSL function to use based on the opcode */
1016 /* TODO: Possibly make this a table for faster lookups */
1017 switch (curOpcode->opcode) {
1018 case WINED3DSIO_MIN: strcat(tmpLine, "min"); break;
1019 case WINED3DSIO_MAX: strcat(tmpLine, "max"); break;
1020 case WINED3DSIO_RSQ: strcat(tmpLine, "inversesqrt"); break;
1021 case WINED3DSIO_ABS: strcat(tmpLine, "abs"); break;
1022 case WINED3DSIO_FRC: strcat(tmpLine, "fract"); break;
1023 case WINED3DSIO_POW: strcat(tmpLine, "pow"); break;
1024 case WINED3DSIO_CRS: strcat(tmpLine, "cross"); break;
1025 case WINED3DSIO_NRM: strcat(tmpLine, "normalize"); break;
1026 case WINED3DSIO_LOGP:
1027 case WINED3DSIO_LOG: strcat(tmpLine, "log2"); break;
1028 case WINED3DSIO_EXP: strcat(tmpLine, "exp2"); break;
1029 case WINED3DSIO_SGE: strcat(tmpLine, "greaterThanEqual"); break;
1030 case WINED3DSIO_SLT: strcat(tmpLine, "lessThan"); break;
1031 case WINED3DSIO_SGN: strcat(tmpLine, "sign"); break;
1032 default:
1033 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1034 break;
1037 strcat(tmpLine, "(");
1039 if (curOpcode->num_params > 0) {
1040 strcat(tmpLine, "vec4(");
1041 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src_reg, src_mask, src_str);
1042 strcat(tmpLine, src_str);
1043 strcat(tmpLine, ")");
1044 for (i = 2; i < curOpcode->num_params; ++i) {
1045 strcat(tmpLine, ", vec4(");
1046 shader_glsl_add_src_param_old(arg, arg->src[i-1], arg->src_addr[i-1], src_reg, src_mask, src_str);
1047 strcat(tmpLine, src_str);
1048 strcat(tmpLine, ")");
1051 shader_addline(buffer, "%s))%s;\n", tmpLine, dst_mask);
1055 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1056 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1057 * dst.x = 2^(floor(src))
1058 * dst.y = src - floor(src)
1059 * dst.z = 2^src (partial precision is allowed, but optional)
1060 * dst.w = 1.0;
1061 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1062 * dst = 2^src; (partial precision is allowed, but optional)
1064 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1066 char tmpLine[256];
1067 char dst_str[100], src_str[100];
1068 char dst_reg[50], src_reg[50];
1069 char dst_mask[6], src_mask[6];
1070 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1071 DWORD hex_version = This->baseShader.hex_version;
1073 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1074 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src_reg, src_mask, src_str);
1075 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1077 if (hex_version < WINED3DPS_VERSION(2,0)) {
1078 shader_addline(arg->buffer, "tmp0.x = vec4(exp2(floor(%s))).x;\n", src_str);
1079 shader_addline(arg->buffer, "tmp0.y = vec4(%s - floor(%s)).y;\n", src_str, src_str);
1080 shader_addline(arg->buffer, "tmp0.z = vec4(exp2(%s)).x;\n", src_str);
1081 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1082 shader_addline(arg->buffer, "%svec4(tmp0))%s;\n", tmpLine, dst_mask);
1083 } else {
1084 shader_addline(arg->buffer, "%svec4(exp2(%s)))%s;\n", tmpLine, src_str, dst_mask);
1088 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1089 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1091 char tmpLine[256];
1092 char dst_str[100], src_str[100];
1093 char dst_reg[50], src_reg[50];
1094 char dst_mask[6], src_mask[6];
1096 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1097 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src_reg, src_mask, src_str);
1098 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1099 strcat(tmpLine, "1.0 / ");
1100 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
1103 /** Process signed comparison opcodes in GLSL. */
1104 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1106 char tmpLine[256];
1107 char dst_str[100], src0_str[100], src1_str[100];
1108 char dst_reg[50], src0_reg[50], src1_reg[50];
1109 char dst_mask[6], src0_mask[6], src1_mask[6];
1111 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1112 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1113 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1115 /* If we are comparing vectors and not scalars, we should process this through map2gl using the GLSL functions. */
1116 if (strlen(src0_mask) != 2) {
1117 shader_glsl_map2gl(arg);
1118 } else {
1119 char compareStr[3];
1120 compareStr[0] = 0;
1121 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1123 switch (arg->opcode->opcode) {
1124 case WINED3DSIO_SLT: strcpy(compareStr, "<"); break;
1125 case WINED3DSIO_SGE: strcpy(compareStr, ">="); break;
1126 default:
1127 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1129 shader_addline(arg->buffer, "%s(float(%s) %s float(%s)) ? 1.0 : 0.0)%s;\n",
1130 tmpLine, src0_str, compareStr, src1_str, dst_mask);
1134 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
1135 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1137 char tmpLine[256];
1138 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1139 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1140 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1142 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1143 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1144 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1145 shader_glsl_add_src_param_old(arg, arg->src[2], arg->src_addr[2], src2_reg, src2_mask, src2_str);
1147 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1148 shader_addline(arg->buffer, "%smix(vec4(%s), vec4(%s), vec4(lessThan(vec4(%s), vec4(0.0)))))%s;\n",
1149 tmpLine, src1_str, src2_str, src0_str, dst_mask);
1152 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
1153 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1155 char tmpLine[256];
1156 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1157 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1158 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1160 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1161 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1162 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1163 shader_glsl_add_src_param_old(arg, arg->src[2], arg->src_addr[2], src2_reg, src2_mask, src2_str);
1164 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1165 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
1166 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1169 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1170 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1172 char tmpLine[256];
1173 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1174 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1175 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1177 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1178 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1179 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1180 shader_glsl_add_src_param_old(arg, arg->src[2], arg->src_addr[2], src2_reg, src2_mask, src2_str);
1181 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1183 shader_addline(arg->buffer, "%s(vec4(%s) * vec4(%s)) + vec4(%s))%s;\n",
1184 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1187 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1188 Vertex shaders to GLSL codes */
1189 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1190 int i;
1191 int nComponents = 0;
1192 SHADER_OPCODE_ARG tmpArg;
1194 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1196 /* Set constants for the temporary argument */
1197 tmpArg.shader = arg->shader;
1198 tmpArg.buffer = arg->buffer;
1199 tmpArg.src[0] = arg->src[0];
1200 tmpArg.src_addr[0] = arg->src_addr[0];
1201 tmpArg.src_addr[1] = arg->src_addr[1];
1202 tmpArg.reg_maps = arg->reg_maps;
1204 switch(arg->opcode->opcode) {
1205 case WINED3DSIO_M4x4:
1206 nComponents = 4;
1207 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1208 break;
1209 case WINED3DSIO_M4x3:
1210 nComponents = 3;
1211 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1212 break;
1213 case WINED3DSIO_M3x4:
1214 nComponents = 4;
1215 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1216 break;
1217 case WINED3DSIO_M3x3:
1218 nComponents = 3;
1219 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1220 break;
1221 case WINED3DSIO_M3x2:
1222 nComponents = 2;
1223 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1224 break;
1225 default:
1226 break;
1229 for (i = 0; i < nComponents; i++) {
1230 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1231 tmpArg.src[1] = arg->src[1]+i;
1232 shader_glsl_dot(&tmpArg);
1237 The LRP instruction performs a component-wise linear interpolation
1238 between the second and third operands using the first operand as the
1239 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
1241 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1243 char tmpLine[256];
1244 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1245 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1246 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1248 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1249 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1250 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1251 shader_glsl_add_src_param_old(arg, arg->src[2], arg->src_addr[2], src2_reg, src2_mask, src2_str);
1253 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1255 shader_addline(arg->buffer, "%s%s + %s * (%s - %s))%s;\n",
1256 tmpLine, src2_str, src0_str, src1_str, src2_str, dst_mask);
1259 /** Process the WINED3DSIO_LIT instruction in GLSL:
1260 * dst.x = dst.w = 1.0
1261 * dst.y = (src0.x > 0) ? src0.x
1262 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1263 * where src.w is clamped at +- 128
1265 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1267 char dst_str[100], src0_str[100];
1268 char dst_reg[50], src0_reg[50];
1269 char dst_mask[6], src0_mask[6];
1271 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1272 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1274 shader_addline(arg->buffer,
1275 "%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",
1276 dst_str, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, dst_mask);
1279 /** Process the WINED3DSIO_DST instruction in GLSL:
1280 * dst.x = 1.0
1281 * dst.y = src0.x * src0.y
1282 * dst.z = src0.z
1283 * dst.w = src1.w
1285 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1287 char dst_str[100], src0_str[100], src1_str[100];
1288 char dst_reg[50], src0_reg[50], src1_reg[50];
1289 char dst_mask[6], src0_mask[6], src1_mask[6];
1291 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1292 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1293 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1295 shader_addline(arg->buffer, "%s = vec4(1.0, %s.x * %s.y, %s.z, %s.w)%s;\n",
1296 dst_str, src0_reg, src1_reg, src0_reg, src1_reg, dst_mask);
1299 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1300 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1301 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1303 * dst.x = cos(src0.?)
1304 * dst.y = sin(src0.?)
1305 * dst.z = dst.z
1306 * dst.w = dst.w
1308 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1310 char dst_str[100], src0_str[100];
1311 char dst_reg[50], src0_reg[50];
1312 char dst_mask[6], src0_mask[6];
1314 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1315 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1317 shader_addline(arg->buffer, "%s = vec4(cos(%s), sin(%s), %s.z, %s.w)%s;\n",
1318 dst_str, src0_str, src0_str, dst_reg, dst_reg, dst_mask);
1321 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1322 * Start a for() loop where src0.y is the initial value of aL,
1323 * increment aL by src0.z for a total of src0.x iterations.
1324 * Need to use a temporary variable for this operation.
1326 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1328 char src1_str[100];
1329 char src1_reg[50];
1330 char src1_mask[6];
1332 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1334 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1335 src1_reg, src1_reg, src1_reg);
1338 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1339 shader_addline(arg->buffer, "}\n");
1342 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1344 char src0_str[100];
1345 char src0_reg[50];
1346 char src0_mask[6];
1348 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1349 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s.x; tmpInt++) {\n", src0_reg);
1352 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1354 char src0_str[100];
1355 char src0_reg[50];
1356 char src0_mask[6];
1358 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1359 shader_addline(arg->buffer, "if (%s) {\n", src0_str);
1362 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1364 char src0_str[100], src1_str[100];
1365 char src0_reg[50], src1_reg[50];
1366 char src0_mask[6], src1_mask[6];
1368 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1369 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1371 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1372 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1375 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1376 shader_addline(arg->buffer, "} else {\n");
1379 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1380 shader_addline(arg->buffer, "break;\n");
1383 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1385 char src0_str[100], src1_str[100];
1386 char src0_reg[50], src1_reg[50];
1387 char src0_mask[6], src1_mask[6];
1389 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1390 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1392 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1393 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1396 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1398 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1399 shader_addline(arg->buffer, "}\n");
1400 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1403 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1404 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1405 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1408 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1410 char src1_str[100];
1411 char src1_reg[50];
1412 char src1_mask[6];
1414 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1415 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1416 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_str, snum);
1419 /*********************************************
1420 * Pixel Shader Specific Code begins here
1421 ********************************************/
1422 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1423 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1425 DWORD hex_version = This->baseShader.hex_version;
1427 char dst_str[100], dst_reg[50], dst_mask[6];
1428 char coord_str[100], coord_reg[50], coord_mask[6];
1430 /* All versions have a destination register */
1431 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1433 /* 1.0-1.3: Use destination register as coordinate source.
1434 1.4+: Use provided coordinate source register. */
1435 if (hex_version < WINED3DPS_VERSION(1,4))
1436 strcpy(coord_reg, dst_reg);
1437 else
1438 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], coord_reg, coord_mask, coord_str);
1440 /* 1.0-1.4: Use destination register as sampler source.
1441 * 2.0+: Use provided sampler source. */
1442 if (hex_version < WINED3DPS_VERSION(2,0)) {
1443 shader_glsl_sample(arg, arg->dst & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1444 } else {
1445 shader_glsl_sample(arg, arg->src[1] & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1450 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1452 /* FIXME: Make this work for more than just 2D textures */
1454 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1455 SHADER_BUFFER* buffer = arg->buffer;
1456 DWORD hex_version = This->baseShader.hex_version;
1458 char tmpStr[100];
1459 char tmpReg[50];
1460 char tmpMask[6];
1461 tmpReg[0] = 0;
1463 shader_glsl_add_dst_param(arg, arg->dst, 0, tmpReg, tmpMask, tmpStr);
1465 if (hex_version != WINED3DPS_VERSION(1,4)) {
1466 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1467 shader_addline(buffer, "%s = clamp(gl_TexCoord[%u], 0.0, 1.0);\n", tmpReg, reg);
1468 } else {
1469 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1470 shader_addline(buffer, "%s = gl_TexCoord[%u]%s;\n", tmpStr, reg2, tmpMask);
1474 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1475 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1476 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1477 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1479 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1480 char src0_str[100], dst_str[100];
1481 char src0_name[50], dst_name[50];
1482 char src0_mask[6], dst_mask[6];
1484 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1485 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1487 shader_addline(arg->buffer, "tmp0.x = dot(vec3(gl_TexCoord[%u]), vec3(%s));\n", dstreg, src0_str);
1488 shader_addline(arg->buffer, "%s = vec4(texture1D(Psampler%u, tmp0.x))%s;\n", dst_str, dstreg, dst_mask);
1491 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1492 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1493 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1495 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1496 char src0_str[100], dst_str[100];
1497 char src0_name[50], dst_name[50];
1498 char src0_mask[6], dst_mask[6];
1500 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1501 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1503 shader_addline(arg->buffer, "%s = vec4(dot(vec3(T%u), vec3(%s)))%s;\n",
1504 dst_str, dstreg, src0_str, dst_mask);
1507 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1508 * Calculate the depth as dst.x / dst.y */
1509 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1511 char dst_str[100];
1512 char dst_reg[50];
1513 char dst_mask[6];
1515 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1517 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_reg, dst_reg);
1520 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1521 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1522 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1523 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1525 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1527 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1528 char src0_str[100], dst_str[100];
1529 char src0_name[50], dst_name[50];
1530 char src0_mask[6], dst_mask[6];
1532 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1533 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1535 shader_addline(arg->buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", dstreg, src0_str);
1536 shader_addline(arg->buffer, "gl_FragDepth = vec4((tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y)%s;\n", dst_str, dst_name);
1539 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1540 * Calculate the 1st of a 2-row matrix multiplication. */
1541 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1543 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1544 SHADER_BUFFER* buffer = arg->buffer;
1545 char src0_str[100];
1546 char src0_name[50];
1547 char src0_mask[6];
1549 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1550 shader_addline(buffer, "tmp0.x = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1553 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1554 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1555 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1557 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1558 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1559 SHADER_BUFFER* buffer = arg->buffer;
1560 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1561 char src0_str[100];
1562 char src0_name[50];
1563 char src0_mask[6];
1565 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1566 shader_addline(buffer, "tmp0.%c = dot(vec3(T%u), vec3(%s));\n", 'x' + current_state->current_row, reg, src0_str);
1567 current_state->texcoord_w[current_state->current_row++] = reg;
1570 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1571 char dst_str[8];
1572 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1573 SHADER_BUFFER* buffer = arg->buffer;
1574 char src0_str[100];
1575 char src0_name[50];
1576 char src0_mask[6];
1578 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1579 shader_addline(buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1581 /* Sample the texture using the calculated coordinates */
1582 sprintf(dst_str, "T%u", reg);
1583 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1586 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1587 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1588 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1589 char dst_str[8];
1590 char src0_str[100];
1591 char src0_name[50];
1592 char src0_mask[6];
1593 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1594 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1595 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1597 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1598 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1600 /* Sample the texture using the calculated coordinates */
1601 sprintf(dst_str, "T%u", reg);
1602 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1603 current_state->current_row = 0;
1606 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1607 * Perform the 3rd row of a 3x3 matrix multiply */
1608 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
1610 char src0_str[100];
1611 char src0_name[50];
1612 char src0_mask[6];
1613 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1614 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1615 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1617 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1619 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1620 shader_addline(arg->buffer, "T%u = vec4(tmp0.x, tmp0.y, tmp0.z, 1.0);\n", reg);
1621 current_state->current_row = 0;
1624 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1625 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1626 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1628 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1629 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1630 char dimensions[5];
1631 char dst_str[8];
1632 char src0_str[100], src0_name[50], src0_mask[6];
1633 char src1_str[100], src1_name[50], src1_mask[6];
1634 SHADER_BUFFER* buffer = arg->buffer;
1635 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1636 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1638 switch (stype) {
1639 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1640 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1641 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1642 default:
1643 strcpy(dimensions, "");
1644 FIXME("Unrecognized sampler type: %#x\n", stype);
1645 break;
1648 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1649 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_name, src1_mask, src1_str);
1651 /* Perform the last matrix multiply operation */
1652 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1654 /* Calculate reflection vector */
1655 shader_addline(buffer, "tmp0.xyz = reflect(-vec3(%s), vec3(tmp0));\n", src1_str);
1657 /* Sample the texture */
1658 sprintf(dst_str, "T%u", reg);
1659 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1660 current_state->current_row = 0;
1663 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1664 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1665 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1667 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1668 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1669 SHADER_BUFFER* buffer = arg->buffer;
1670 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1671 char dst_str[8];
1672 char src0_str[100], src0_name[50], src0_mask[6];
1674 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1676 /* Perform the last matrix multiply operation */
1677 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1679 /* Construct the eye-ray vector from w coordinates */
1680 shader_addline(buffer, "tmp1.x = gl_TexCoord[%u].w;\n", current_state->texcoord_w[0]);
1681 shader_addline(buffer, "tmp1.y = gl_TexCoord[%u].w;\n", current_state->texcoord_w[1]);
1682 shader_addline(buffer, "tmp1.z = gl_TexCoord[%u].w;\n", reg);
1684 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
1685 shader_addline(buffer, "tmp0.x = dot(vec3(tmp0), vec3(tmp1));\n");
1686 shader_addline(buffer, "tmp0 = tmp0.w * tmp0;\n");
1687 shader_addline(buffer, "tmp0 = (2.0 * tmp0) - tmp1;\n");
1689 /* Sample the texture using the calculated coordinates */
1690 sprintf(dst_str, "T%u", reg);
1691 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1692 current_state->current_row = 0;
1695 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1696 * Apply a fake bump map transform.
1697 * FIXME: Should apply the BUMPMAPENV matrix. For now, just sample the texture */
1698 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1700 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1701 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1703 FIXME("Not applying the BUMPMAPENV matrix for pixel shader instruction texbem.\n");
1704 shader_addline(arg->buffer, "T%u = texture2D(Psampler%u, gl_TexCoord[%u].xy + T%u.xy);\n",
1705 reg1, reg1, reg1, reg2);
1708 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1709 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1710 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1712 char tmpLine[255];
1713 char dst_str[100], src0_str[100];
1714 char dst_reg[50], src0_reg[50];
1715 char dst_mask[6], src0_mask[6];
1716 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1718 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1719 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1721 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1722 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1723 tmpLine, src0_regnum, dst_reg, dst_mask);
1726 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1727 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1728 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1730 char tmpLine[255];
1731 char dst_str[100], src0_str[100];
1732 char dst_reg[50], src0_reg[50];
1733 char dst_mask[6], src0_mask[6];
1734 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1736 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1737 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1739 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1740 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1741 tmpLine, src0_regnum, dst_reg, dst_mask);
1744 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
1745 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
1746 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1748 char tmpLine[255];
1749 char dst_str[100], src0_str[100];
1750 char dst_reg[50], src0_reg[50];
1751 char dst_mask[6], src0_mask[6];
1752 char dimensions[5];
1753 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1754 DWORD stype = arg->reg_maps->samplers[src0_regnum] & WINED3DSP_TEXTURETYPE_MASK;
1755 switch (stype) {
1756 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1757 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1758 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1759 default:
1760 strcpy(dimensions, "");
1761 FIXME("Unrecognized sampler type: %#x\n", stype);
1762 break;
1765 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1766 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1768 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1769 shader_addline(arg->buffer, "%stexture%s(Psampler%u, %s.%s))%s;\n",
1770 tmpLine, dimensions, src0_regnum, dst_reg, (stype == WINED3DSTT_2D) ? "xy" : "xyz", dst_mask);
1773 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
1774 * If any of the first 3 components are < 0, discard this pixel */
1775 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
1777 char dst_str[100], dst_name[50], dst_mask[6];
1779 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1780 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_name);
1783 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
1784 * dst = dot2(src0, src1) + src2 */
1785 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
1787 char tmpLine[256];
1788 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1789 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1790 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1792 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1793 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1794 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1795 shader_glsl_add_src_param_old(arg, arg->src[2], arg->src_addr[2], src2_reg, src2_mask, src2_str);
1796 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1797 shader_addline(arg->buffer, "%sdot(vec2(%s), vec2(%s)) + %s)%s;\n",
1798 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1801 void pshader_glsl_input_pack(
1802 SHADER_BUFFER* buffer,
1803 semantic* semantics_in) {
1805 unsigned int i;
1807 for (i = 0; i < MAX_REG_INPUT; i++) {
1809 DWORD usage_token = semantics_in[i].usage;
1810 DWORD register_token = semantics_in[i].reg;
1811 DWORD usage, usage_idx;
1812 char reg_mask[6];
1814 /* Uninitialized */
1815 if (!usage_token) continue;
1816 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1817 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1818 shader_glsl_get_write_mask(register_token, reg_mask);
1820 switch(usage) {
1822 case D3DDECLUSAGE_COLOR:
1823 if (usage_idx == 0)
1824 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
1825 i, reg_mask, reg_mask);
1826 else if (usage_idx == 1)
1827 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
1828 i, reg_mask, reg_mask);
1829 else
1830 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
1831 i, reg_mask, reg_mask);
1832 break;
1834 case D3DDECLUSAGE_TEXCOORD:
1835 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
1836 i, reg_mask, usage_idx, reg_mask );
1837 break;
1839 case D3DDECLUSAGE_FOG:
1840 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
1841 i, reg_mask, reg_mask);
1842 break;
1844 default:
1845 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
1846 i, reg_mask, reg_mask);
1851 /*********************************************
1852 * Vertex Shader Specific Code begins here
1853 ********************************************/
1855 void vshader_glsl_output_unpack(
1856 SHADER_BUFFER* buffer,
1857 semantic* semantics_out) {
1859 unsigned int i;
1861 for (i = 0; i < MAX_REG_OUTPUT; i++) {
1863 DWORD usage_token = semantics_out[i].usage;
1864 DWORD register_token = semantics_out[i].reg;
1865 DWORD usage, usage_idx;
1866 char reg_mask[6];
1868 /* Uninitialized */
1869 if (!usage_token) continue;
1871 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1872 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1873 shader_glsl_get_write_mask(register_token, reg_mask);
1875 switch(usage) {
1877 case D3DDECLUSAGE_COLOR:
1878 if (usage_idx == 0)
1879 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1880 else if (usage_idx == 1)
1881 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1882 else
1883 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1884 break;
1886 case D3DDECLUSAGE_POSITION:
1887 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1888 break;
1890 case D3DDECLUSAGE_TEXCOORD:
1891 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
1892 usage_idx, reg_mask, i, reg_mask);
1893 break;
1895 case WINED3DSHADERDECLUSAGE_PSIZE:
1896 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
1897 break;
1899 case WINED3DSHADERDECLUSAGE_FOG:
1900 shader_addline(buffer, "gl_FogFragCoord%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1901 break;
1903 default:
1904 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1909 /** Attach a GLSL pixel or vertex shader object to the shader program */
1910 static void attach_glsl_shader(IWineD3DDevice *iface, IWineD3DBaseShader* shader) {
1911 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1912 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1913 GLhandleARB shaderObj = ((IWineD3DBaseShaderImpl*)shader)->baseShader.prgId;
1914 if (This->stateBlock->glsl_program && shaderObj != 0) {
1915 TRACE("Attaching GLSL shader object %u to program %u\n", shaderObj, This->stateBlock->glsl_program->programId);
1916 GL_EXTCALL(glAttachObjectARB(This->stateBlock->glsl_program->programId, shaderObj));
1917 checkGLcall("glAttachObjectARB");
1921 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
1922 * It sets the programId on the current StateBlock (because it should be called
1923 * inside of the DrawPrimitive() part of the render loop).
1925 * If a program for the given combination does not exist, create one, and store
1926 * the program in the list. If it creates a program, it will link the given
1927 * objects, too.
1929 * We keep the shader programs around on a list because linking
1930 * shader objects together is an expensive operation. It's much
1931 * faster to loop through a list of pre-compiled & linked programs
1932 * each time that the application sets a new pixel or vertex shader
1933 * than it is to re-link them together at that time.
1935 * The list will be deleted in IWineD3DDevice::Release().
1937 static void set_glsl_shader_program(IWineD3DDevice *iface) {
1938 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1939 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1940 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
1941 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
1942 struct glsl_shader_prog_link *curLink = NULL;
1943 struct glsl_shader_prog_link *newLink = NULL;
1944 struct list *ptr = NULL;
1945 GLhandleARB programId = 0;
1946 int i;
1947 char glsl_name[8];
1949 ptr = list_head( &This->glsl_shader_progs );
1950 while (ptr) {
1951 /* At least one program exists - see if it matches our ps/vs combination */
1952 curLink = LIST_ENTRY( ptr, struct glsl_shader_prog_link, entry );
1953 if (vshader == curLink->vertexShader && pshader == curLink->pixelShader) {
1954 /* Existing Program found, use it */
1955 TRACE("Found existing program (%u) for this vertex/pixel shader combination\n",
1956 curLink->programId);
1957 This->stateBlock->glsl_program = curLink;
1958 return;
1960 /* This isn't the entry we need - try the next one */
1961 ptr = list_next( &This->glsl_shader_progs, ptr );
1964 /* If we get to this point, then no matching program exists, so we create one */
1965 programId = GL_EXTCALL(glCreateProgramObjectARB());
1966 TRACE("Created new GLSL shader program %u\n", programId);
1968 /* Allocate a new link for the list of programs */
1969 newLink = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
1970 newLink->programId = programId;
1971 This->stateBlock->glsl_program = newLink;
1973 /* Attach GLSL vshader */
1974 if (NULL != vshader && This->vs_selected_mode == SHADER_GLSL) {
1975 int i;
1976 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
1977 char tmp_name[10];
1979 TRACE("Attaching vertex shader to GLSL program\n");
1980 attach_glsl_shader(iface, (IWineD3DBaseShader*)vshader);
1982 /* Bind vertex attributes to a corresponding index number to match
1983 * the same index numbers as ARB_vertex_programs (makes loading
1984 * vertex attributes simpler). With this method, we can use the
1985 * exact same code to load the attributes later for both ARB and
1986 * GLSL shaders.
1988 * We have to do this here because we need to know the Program ID
1989 * in order to make the bindings work, and it has to be done prior
1990 * to linking the GLSL program. */
1991 for (i = 0; i < max_attribs; ++i) {
1992 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
1993 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
1995 checkGLcall("glBindAttribLocationARB");
1996 newLink->vertexShader = vshader;
1999 /* Attach GLSL pshader */
2000 if (NULL != pshader && This->ps_selected_mode == SHADER_GLSL) {
2001 TRACE("Attaching pixel shader to GLSL program\n");
2002 attach_glsl_shader(iface, (IWineD3DBaseShader*)pshader);
2003 newLink->pixelShader = pshader;
2006 /* Link the program */
2007 TRACE("Linking GLSL shader program %u\n", programId);
2008 GL_EXTCALL(glLinkProgramARB(programId));
2009 print_glsl_info_log(&GLINFO_LOCATION, programId);
2010 list_add_head( &This->glsl_shader_progs, &newLink->entry);
2012 newLink->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
2013 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
2014 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
2015 newLink->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2017 newLink->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
2018 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
2019 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
2020 newLink->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2023 return;
2026 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
2027 GLhandleARB program_id;
2028 GLhandleARB vshader_id, pshader_id;
2029 const char *blt_vshader[] = {
2030 "void main(void)\n"
2031 "{\n"
2032 " gl_Position = gl_Vertex;\n"
2033 " gl_FrontColor = vec4(1.0);\n"
2034 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2035 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2036 "}\n"
2039 const char *blt_pshader[] = {
2040 "uniform sampler2D sampler;\n"
2041 "void main(void)\n"
2042 "{\n"
2043 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2044 "}\n"
2047 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2048 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2049 GL_EXTCALL(glCompileShaderARB(vshader_id));
2051 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2052 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2053 GL_EXTCALL(glCompileShaderARB(pshader_id));
2055 program_id = GL_EXTCALL(glCreateProgramObjectARB());
2056 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2057 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2058 GL_EXTCALL(glLinkProgramARB(program_id));
2060 print_glsl_info_log(&GLINFO_LOCATION, program_id);
2062 return program_id;
2065 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2066 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2067 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2068 GLhandleARB program_id = 0;
2070 if (useVS || usePS) set_glsl_shader_program(iface);
2071 else This->stateBlock->glsl_program = NULL;
2073 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2074 if (program_id) TRACE("Using GLSL program %u\n", program_id);
2075 GL_EXTCALL(glUseProgramObjectARB(program_id));
2076 checkGLcall("glUseProgramObjectARB");
2079 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2080 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2081 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2082 static GLhandleARB program_id = 0;
2083 static GLhandleARB loc = -1;
2085 if (!program_id) {
2086 program_id = create_glsl_blt_shader(gl_info);
2087 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2090 GL_EXTCALL(glUseProgramObjectARB(program_id));
2091 GL_EXTCALL(glUniform1iARB(loc, 0));
2094 static void shader_glsl_cleanup(BOOL usePS, BOOL useVS) {
2095 /* Nothing to do */
2098 const shader_backend_t glsl_shader_backend = {
2099 &shader_glsl_select,
2100 &shader_glsl_select_depth_blt,
2101 &shader_glsl_load_constants,
2102 &shader_glsl_cleanup