2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * D3D shader asm has swizzles on source parameters, and write masks for
24 * destination parameters. GLSL uses swizzles for both. The result of this is
25 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
26 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
27 * mask for the destination parameter into account.
32 #include "wined3d_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
36 #define GLINFO_LOCATION (*gl_info)
38 /** Prints the GLSL info log which will contain error messages if they exist */
39 void print_glsl_info_log(WineD3D_GL_Info *gl_info, GLhandleARB obj) {
41 int infologLength = 0;
44 GL_EXTCALL(glGetObjectParameterivARB(obj,
45 GL_OBJECT_INFO_LOG_LENGTH_ARB,
48 /* A size of 1 is just a null-terminated string, so the log should be bigger than
49 * that if there are errors. */
50 if (infologLength > 1)
52 infoLog = (char *)HeapAlloc(GetProcessHeap(), 0, infologLength);
53 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
54 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
55 HeapFree(GetProcessHeap(), 0, infoLog);
60 * Loads (pixel shader) samplers
62 void shader_glsl_load_psamplers(
63 WineD3D_GL_Info *gl_info,
64 IWineD3DStateBlock* iface) {
66 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
67 GLhandleARB programId = stateBlock->glsl_program->programId;
70 char sampler_name[20];
72 for (i=0; i< GL_LIMITS(samplers); ++i) {
73 if (stateBlock->textures[i] != NULL) {
74 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
75 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
77 TRACE("Loading %s for texture %d\n", sampler_name, i);
78 GL_EXTCALL(glUniform1iARB(name_loc, i));
79 checkGLcall("glUniform1iARB");
86 * Loads floating point constants (aka uniforms) into the currently set GLSL program.
87 * When constant_list == NULL, it will load all the constants.
89 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl* This, WineD3D_GL_Info *gl_info,
90 unsigned int max_constants, float* constants, GLhandleARB *constant_locations,
91 struct list *constant_list) {
92 local_constant* lconst;
97 if (TRACE_ON(d3d_shader)) {
98 for (i = 0; i < max_constants; ++i) {
99 tmp_loc = constant_locations[i];
101 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
102 constants[i * 4 + 0], constants[i * 4 + 1],
103 constants[i * 4 + 2], constants[i * 4 + 3]);
107 for (i = 0; i < max_constants; ++i) {
108 tmp_loc = constant_locations[i];
110 /* We found this uniform name in the program - go ahead and send the data */
111 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
114 checkGLcall("glUniform4fvARB()");
116 constant_entry *constant;
117 if (TRACE_ON(d3d_shader)) {
118 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
120 tmp_loc = constant_locations[i];
122 TRACE("Loading constants %i: %f, %f, %f, %f\n", i,
123 constants[i * 4 + 0], constants[i * 4 + 1],
124 constants[i * 4 + 2], constants[i * 4 + 3]);
128 LIST_FOR_EACH_ENTRY(constant, constant_list, constant_entry, entry) {
130 tmp_loc = constant_locations[i];
132 /* We found this uniform name in the program - go ahead and send the data */
133 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, constants + (i * 4)));
136 checkGLcall("glUniform4fvARB()");
139 /* Load immediate constants */
140 if (TRACE_ON(d3d_shader)) {
141 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
142 tmp_loc = constant_locations[lconst->idx];
144 GLfloat* values = (GLfloat*)lconst->value;
145 TRACE("Loading local constants %i: %f, %f, %f, %f\n", lconst->idx,
146 values[0], values[1], values[2], values[3]);
150 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
151 tmp_loc = constant_locations[lconst->idx];
153 /* We found this uniform name in the program - go ahead and send the data */
154 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, (GLfloat*)lconst->value));
157 checkGLcall("glUniform4fvARB()");
161 * Loads integer constants (aka uniforms) into the currently set GLSL program.
162 * When @constants_set == NULL, it will load all the constants.
164 void shader_glsl_load_constantsI(
165 IWineD3DBaseShaderImpl* This,
166 WineD3D_GL_Info *gl_info,
167 GLhandleARB programId,
168 unsigned max_constants,
170 BOOL* constants_set) {
175 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
176 const char* prefix = is_pshader? "PI":"VI";
179 for (i=0; i<max_constants; ++i) {
180 if (NULL == constants_set || constants_set[i]) {
182 TRACE("Loading constants %i: %i, %i, %i, %i\n",
183 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
185 /* TODO: Benchmark and see if it would be beneficial to store the
186 * locations of the constants to avoid looking up each time */
187 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
188 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
190 /* We found this uniform name in the program - go ahead and send the data */
191 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, &constants[i*4]));
192 checkGLcall("glUniform4ivARB");
197 /* Load immediate constants */
198 ptr = list_head(&This->baseShader.constantsI);
200 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
201 unsigned int idx = lconst->idx;
202 GLint* values = (GLint*) lconst->value;
204 TRACE("Loading local constants %i: %i, %i, %i, %i\n", idx,
205 values[0], values[1], values[2], values[3]);
207 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
208 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
210 /* We found this uniform name in the program - go ahead and send the data */
211 GL_EXTCALL(glUniform4ivARB(tmp_loc, 1, values));
212 checkGLcall("glUniform4ivARB");
214 ptr = list_next(&This->baseShader.constantsI, ptr);
219 * Loads boolean constants (aka uniforms) into the currently set GLSL program.
220 * When @constants_set == NULL, it will load all the constants.
222 void shader_glsl_load_constantsB(
223 IWineD3DBaseShaderImpl* This,
224 WineD3D_GL_Info *gl_info,
225 GLhandleARB programId,
226 unsigned max_constants,
228 BOOL* constants_set) {
233 char is_pshader = shader_is_pshader_version(This->baseShader.hex_version);
234 const char* prefix = is_pshader? "PB":"VB";
237 for (i=0; i<max_constants; ++i) {
238 if (NULL == constants_set || constants_set[i]) {
240 TRACE("Loading constants %i: %i;\n", i, constants[i*4]);
242 /* TODO: Benchmark and see if it would be beneficial to store the
243 * locations of the constants to avoid looking up each time */
244 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
245 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
247 /* We found this uniform name in the program - go ahead and send the data */
248 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i*4]));
249 checkGLcall("glUniform1ivARB");
254 /* Load immediate constants */
255 ptr = list_head(&This->baseShader.constantsB);
257 local_constant* lconst = LIST_ENTRY(ptr, struct local_constant, entry);
258 unsigned int idx = lconst->idx;
259 GLint* values = (GLint*) lconst->value;
261 TRACE("Loading local constants %i: %i\n", idx, values[0]);
263 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
264 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
266 /* We found this uniform name in the program - go ahead and send the data */
267 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
268 checkGLcall("glUniform1ivARB");
270 ptr = list_next(&This->baseShader.constantsB, ptr);
277 * Loads the app-supplied constants into the currently set GLSL program.
279 void shader_glsl_load_constants(
280 IWineD3DDevice* device,
282 char useVertexShader) {
284 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
285 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
286 WineD3D_GL_Info *gl_info = &((IWineD3DImpl*) deviceImpl->wineD3D)->gl_info;
288 GLhandleARB *constant_locations;
289 struct list *constant_list;
290 GLhandleARB programId;
292 if (!stateBlock->glsl_program) {
293 /* No GLSL program set - nothing to do. */
296 programId = stateBlock->glsl_program->programId;
298 if (useVertexShader) {
299 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
300 IWineD3DVertexShaderImpl* vshader_impl = (IWineD3DVertexShaderImpl*) vshader;
303 IWineD3DVertexDeclarationImpl* vertexDeclaration =
304 (IWineD3DVertexDeclarationImpl*) vshader_impl->vertexDeclaration;
306 constant_locations = stateBlock->glsl_program->vuniformF_locations;
307 constant_list = &stateBlock->set_vconstantsF;
309 if (NULL != vertexDeclaration && NULL != vertexDeclaration->constants) {
310 /* Load DirectX 8 float constants/uniforms for vertex shader */
311 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
312 vertexDeclaration->constants, constant_locations, NULL);
315 /* Load DirectX 9 float constants/uniforms for vertex shader */
316 shader_glsl_load_constantsF(vshader, gl_info, GL_LIMITS(vshader_constantsF),
317 stateBlock->vertexShaderConstantF, constant_locations, constant_list);
319 /* Load DirectX 9 integer constants/uniforms for vertex shader */
320 shader_glsl_load_constantsI(vshader, gl_info, programId, MAX_CONST_I,
321 stateBlock->vertexShaderConstantI,
322 stateBlock->set.vertexShaderConstantsI);
324 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
325 shader_glsl_load_constantsB(vshader, gl_info, programId, MAX_CONST_B,
326 stateBlock->vertexShaderConstantB,
327 stateBlock->set.vertexShaderConstantsB);
329 /* Upload the position fixup params */
330 pos = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
331 checkGLcall("glGetUniformLocationARB");
332 GL_EXTCALL(glUniform4fvARB(pos, 1, &deviceImpl->posFixup[0]));
333 checkGLcall("glUniform4fvARB");
336 if (usePixelShader) {
338 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
340 constant_locations = stateBlock->glsl_program->puniformF_locations;
341 constant_list = &stateBlock->set_pconstantsF;
343 /* Load pixel shader samplers */
344 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*) stateBlock);
346 /* Load DirectX 9 float constants/uniforms for pixel shader */
347 shader_glsl_load_constantsF(pshader, gl_info, GL_LIMITS(pshader_constantsF),
348 stateBlock->pixelShaderConstantF, constant_locations, constant_list);
350 /* Load DirectX 9 integer constants/uniforms for pixel shader */
351 shader_glsl_load_constantsI(pshader, gl_info, programId, MAX_CONST_I,
352 stateBlock->pixelShaderConstantI,
353 stateBlock->set.pixelShaderConstantsI);
355 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
356 shader_glsl_load_constantsB(pshader, gl_info, programId, MAX_CONST_B,
357 stateBlock->pixelShaderConstantB,
358 stateBlock->set.pixelShaderConstantsB);
362 /** Generate the variable & register declarations for the GLSL output target */
363 void shader_generate_glsl_declarations(
364 IWineD3DBaseShader *iface,
365 shader_reg_maps* reg_maps,
366 SHADER_BUFFER* buffer,
367 WineD3D_GL_Info* gl_info) {
369 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
372 /* There are some minor differences between pixel and vertex shaders */
373 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
374 char prefix = pshader ? 'P' : 'V';
376 /* Prototype the subroutines */
377 for (i = 0; i < This->baseShader.limits.label; i++) {
378 if (reg_maps->labels[i])
379 shader_addline(buffer, "void subroutine%lu();\n", i);
382 /* Declare the constants (aka uniforms) */
383 if (This->baseShader.limits.constant_float > 0) {
384 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
385 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
386 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
389 if (This->baseShader.limits.constant_int > 0)
390 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
392 if (This->baseShader.limits.constant_bool > 0)
393 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
396 shader_addline(buffer, "uniform vec4 posFixup;\n");
398 /* Declare texture samplers */
399 for (i = 0; i < This->baseShader.limits.sampler; i++) {
400 if (reg_maps->samplers[i]) {
402 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
406 shader_addline(buffer, "uniform sampler1D %csampler%lu;\n", prefix, i);
409 shader_addline(buffer, "uniform sampler2D %csampler%lu;\n", prefix, i);
411 case WINED3DSTT_CUBE:
412 shader_addline(buffer, "uniform samplerCube %csampler%lu;\n", prefix, i);
414 case WINED3DSTT_VOLUME:
415 shader_addline(buffer, "uniform sampler3D %csampler%lu;\n", prefix, i);
418 shader_addline(buffer, "uniform unsupported_sampler %csampler%lu;\n", prefix, i);
419 FIXME("Unrecognized sampler type: %#x\n", stype);
425 /* Declare address variables */
426 for (i = 0; i < This->baseShader.limits.address; i++) {
427 if (reg_maps->address[i])
428 shader_addline(buffer, "ivec4 A%d;\n", i);
431 /* Declare texture coordinate temporaries and initialize them */
432 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
433 if (reg_maps->texcoord[i])
434 shader_addline(buffer, "vec4 T%lu = gl_TexCoord[%lu];\n", i, i);
437 /* Declare input register temporaries */
438 for (i=0; i < This->baseShader.limits.packed_input; i++) {
439 if (reg_maps->packed_input[i])
440 shader_addline(buffer, "vec4 IN%lu;\n", i);
443 /* Declare output register temporaries */
444 for (i = 0; i < This->baseShader.limits.packed_output; i++) {
445 if (reg_maps->packed_output[i])
446 shader_addline(buffer, "vec4 OUT%lu;\n", i);
449 /* Declare temporary variables */
450 for(i = 0; i < This->baseShader.limits.temporary; i++) {
451 if (reg_maps->temporary[i])
452 shader_addline(buffer, "vec4 R%lu;\n", i);
455 /* Declare attributes */
456 for (i = 0; i < This->baseShader.limits.attributes; i++) {
457 if (reg_maps->attributes[i])
458 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
461 /* Declare loop register aL */
462 if (reg_maps->loop) {
463 shader_addline(buffer, "int aL;\n");
464 shader_addline(buffer, "int tmpInt;\n");
467 /* Temporary variables for matrix operations */
468 shader_addline(buffer, "vec4 tmp0;\n");
469 shader_addline(buffer, "vec4 tmp1;\n");
471 /* Start the main program */
472 shader_addline(buffer, "void main() {\n");
475 /*****************************************************************************
476 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
478 * For more information, see http://wiki.winehq.org/DirectX-Shaders
479 ****************************************************************************/
482 static void shader_glsl_add_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[] = {
494 "2.0 * ", /* 1 (x2) */
495 "4.0 * ", /* 2 (x4) */
496 "8.0 * ", /* 3 (x8) */
497 "16.0 * ", /* 4 (x16) */
498 "32.0 * ", /* 5 (x32) */
505 "0.0625 * ", /* 12 (d16) */
506 "0.125 * ", /* 13 (d8) */
507 "0.25 * ", /* 14 (d4) */
508 "0.5 * " /* 15 (d2) */
511 /** Print the beginning of the generated GLSL string. example: "reg_name.xyzw = vec4("
512 * Will also change the reg_mask if necessary (not all register types are equal in DX vs GL) */
513 static void shader_glsl_add_dst_old(DWORD param, const char* reg_name, char* reg_mask, char* outStr) {
515 int shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
518 if ((shader_get_regtype(param) == WINED3DSPR_RASTOUT)
519 && ((param & WINED3DSP_REGNUM_MASK) != 0)) {
520 /* gl_FogFragCoord or glPointSize - both floats */
521 strcpy(cast, "float");
522 strcpy(reg_mask, "");
524 } else if (reg_name[0] == 'A') {
525 /* Address register for vertex shaders (ivec4) */
526 strcpy(cast, "ivec4");
529 /* Everything else should be a 4 component float vector */
530 strcpy(cast, "vec4");
533 sprintf(outStr, "%s%s = %s%s(", reg_name, reg_mask, shift_glsl_tab[shift], cast);
536 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
537 static void shader_glsl_gen_modifier (
540 const char *in_regswizzle,
545 if (instr == WINED3DSIO_TEXKILL)
548 switch (instr & WINED3DSP_SRCMOD_MASK) {
549 case WINED3DSPSM_NONE:
550 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
552 case WINED3DSPSM_NEG:
553 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
555 case WINED3DSPSM_NOT:
556 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
558 case WINED3DSPSM_BIAS:
559 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
561 case WINED3DSPSM_BIASNEG:
562 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
564 case WINED3DSPSM_SIGN:
565 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
567 case WINED3DSPSM_SIGNNEG:
568 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
570 case WINED3DSPSM_COMP:
571 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
574 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
576 case WINED3DSPSM_X2NEG:
577 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
579 case WINED3DSPSM_DZ: /* reg1_db = { reg1.r/b, reg1.g/b, ...} The g & a components are undefined, so we'll leave them alone */
580 sprintf(out_str, "vec4(%s.r / %s.b, %s.g / %s.b, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
583 sprintf(out_str, "vec4(%s.r / %s.a, %s.g / %s.a, %s.b, %s.a)", in_reg, in_reg, in_reg, in_reg, in_reg, in_reg);
585 case WINED3DSPSM_ABS:
586 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
588 case WINED3DSPSM_ABSNEG:
589 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
592 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
593 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
597 /** Writes the GLSL variable name that corresponds to the register that the
598 * DX opcode parameter is trying to access */
599 static void shader_glsl_get_register_name(
601 const DWORD addr_token,
604 SHADER_OPCODE_ARG* arg) {
606 /* oPos, oFog and oPts in D3D */
607 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
609 DWORD reg = param & WINED3DSP_REGNUM_MASK;
610 DWORD regtype = shader_get_regtype(param);
611 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
612 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
613 WineD3D_GL_Info* gl_info = &((IWineD3DImpl*)deviceImpl->wineD3D)->gl_info;
615 char pshader = shader_is_pshader_version(This->baseShader.hex_version);
621 case WINED3DSPR_TEMP:
622 sprintf(tmpStr, "R%u", reg);
624 case WINED3DSPR_INPUT:
626 /* Pixel shaders >= 3.0 */
627 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
628 sprintf(tmpStr, "IN%u", reg);
631 strcpy(tmpStr, "gl_Color");
633 strcpy(tmpStr, "gl_SecondaryColor");
636 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
638 sprintf(tmpStr, "attrib%u", reg);
641 case WINED3DSPR_CONST:
643 const char* prefix = pshader? "PC":"VC";
645 /* Relative addressing */
646 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
648 /* Relative addressing on shaders 2.0+ have a relative address token,
649 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
650 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 2) {
651 char relStr[100], relReg[50], relMask[6];
652 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, relReg, relMask, relStr);
653 sprintf(tmpStr, "%s[%s + %u]", prefix, relStr, reg);
655 sprintf(tmpStr, "%s[A0.x + %u]", prefix, reg);
658 sprintf(tmpStr, "%s[%u]", prefix, reg);
662 case WINED3DSPR_CONSTINT:
664 sprintf(tmpStr, "PI[%u]", reg);
666 sprintf(tmpStr, "VI[%u]", reg);
668 case WINED3DSPR_CONSTBOOL:
670 sprintf(tmpStr, "PB[%u]", reg);
672 sprintf(tmpStr, "VB[%u]", reg);
674 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
676 sprintf(tmpStr, "T%u", reg);
678 sprintf(tmpStr, "A%u", reg);
681 case WINED3DSPR_LOOP:
682 sprintf(tmpStr, "aL");
684 case WINED3DSPR_SAMPLER:
686 sprintf(tmpStr, "Psampler%u", reg);
688 sprintf(tmpStr, "Vsampler%u", reg);
690 case WINED3DSPR_COLOROUT:
691 if (reg >= GL_LIMITS(buffers)) {
692 WARN("Write to render target %u, only %d supported\n", reg, 4);
694 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
695 sprintf(tmpStr, "gl_FragData[%u]", reg);
696 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
697 sprintf(tmpStr, "gl_FragColor");
700 case WINED3DSPR_RASTOUT:
701 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
703 case WINED3DSPR_DEPTHOUT:
704 sprintf(tmpStr, "gl_FragDepth");
706 case WINED3DSPR_ATTROUT:
708 sprintf(tmpStr, "gl_FrontColor");
710 sprintf(tmpStr, "gl_FrontSecondaryColor");
713 case WINED3DSPR_TEXCRDOUT:
714 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
715 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) >= 3)
716 sprintf(tmpStr, "OUT%u", reg);
718 sprintf(tmpStr, "gl_TexCoord[%u]", reg);
721 FIXME("Unhandled register name Type(%d)\n", regtype);
722 sprintf(tmpStr, "unrecognized_register");
726 strcat(regstr, tmpStr);
729 /* Get the GLSL write mask for the destination register */
730 static 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;
738 if (mask != WINED3DSP_WRITEMASK_ALL) {
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';
752 static size_t shader_glsl_get_write_mask_size(DWORD write_mask) {
755 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
756 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
757 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
758 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
763 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
764 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
765 * but addressed as "rgba". To fix this we need to swap the register's x
766 * and z components. */
767 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
768 char *ptr = swizzle_str;
770 /* swizzle bits fields: wwzzyyxx */
771 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
772 DWORD swizzle_x = swizzle & 0x03;
773 DWORD swizzle_y = (swizzle >> 2) & 0x03;
774 DWORD swizzle_z = (swizzle >> 4) & 0x03;
775 DWORD swizzle_w = (swizzle >> 6) & 0x03;
777 /* FIXME: This is here to keep shader_glsl_add_src_param_old happy.
778 * As soon as that is removed, this needs to go as well. */
780 /* If the swizzle is the default swizzle (ie, "xyzw"), we don't need to
781 * generate a swizzle string. Unless we need to our own swizzling. */
782 if ((WINED3DSP_NOSWIZZLE >> WINED3DSP_SWIZZLE_SHIFT) != swizzle || fixup) {
784 if (swizzle_x == swizzle_y && swizzle_x == swizzle_z && swizzle_x == swizzle_w) {
785 *ptr++ = swizzle_chars[swizzle_x];
787 *ptr++ = swizzle_chars[swizzle_x];
788 *ptr++ = swizzle_chars[swizzle_y];
789 *ptr++ = swizzle_chars[swizzle_z];
790 *ptr++ = swizzle_chars[swizzle_w];
795 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle_x];
796 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[swizzle_y];
797 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[swizzle_z];
798 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[swizzle_w];
804 /* From a given parameter token, generate the corresponding GLSL string.
805 * Also, return the actual register name and swizzle in case the
806 * caller needs this information as well. */
807 static void shader_glsl_add_src_param(SHADER_OPCODE_ARG* arg, const DWORD param,
808 const DWORD addr_token, DWORD mask, char *reg_name, char *swizzle, char *out_str) {
809 BOOL is_color = FALSE;
810 swizzle[0] = reg_name[0] = out_str[0] = 0;
812 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
814 shader_glsl_get_swizzle(param, is_color, mask, swizzle);
815 shader_glsl_gen_modifier(param, reg_name, swizzle, out_str);
818 /* From a given parameter token, generate the corresponding GLSL string.
819 * Also, return the actual register name and swizzle in case the
820 * caller needs this information as well. */
821 static DWORD shader_glsl_add_dst_param(SHADER_OPCODE_ARG* arg, const DWORD param,
822 const DWORD addr_token, char *reg_name, char *write_mask, char *out_str) {
823 BOOL is_color = FALSE;
825 write_mask[0] = reg_name[0] = out_str[0] = 0;
827 shader_glsl_get_register_name(param, addr_token, reg_name, &is_color, arg);
829 mask = shader_glsl_get_write_mask(param, write_mask);
830 sprintf(out_str, "%s%s", reg_name, write_mask);
835 /* Append the destination part of the instruction to the buffer, return the effective write mask */
836 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, SHADER_OPCODE_ARG *arg) {
837 char reg_name[50], write_mask[6], reg_str[100];
841 shift = (arg->dst & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
842 mask = shader_glsl_add_dst_param(arg, arg->dst, arg->dst_addr, reg_name, write_mask, reg_str);
843 shader_addline(buffer, "%s = %s(", reg_str, shift_glsl_tab[shift]);
848 /** Process GLSL instruction modifiers */
849 void shader_glsl_add_instruction_modifiers(SHADER_OPCODE_ARG* arg) {
851 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
853 if (arg->opcode->dst_token && mask != 0) {
858 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
860 if (mask & WINED3DSPDM_SATURATE) {
861 /* _SAT means to clamp the value of the register to between 0 and 1 */
862 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_reg, dst_mask, dst_reg, dst_mask);
864 if (mask & WINED3DSPDM_MSAMPCENTROID) {
865 FIXME("_centroid modifier not handled\n");
867 if (mask & WINED3DSPDM_PARTIALPRECISION) {
868 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
873 static inline const char* shader_get_comp_op(
874 const DWORD opcode) {
876 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
878 case COMPARISON_GT: return ">";
879 case COMPARISON_EQ: return "==";
880 case COMPARISON_GE: return ">=";
881 case COMPARISON_LT: return "<";
882 case COMPARISON_NE: return "!=";
883 case COMPARISON_LE: return "<=";
885 FIXME("Unrecognized comparison value: %u\n", op);
890 static void shader_glsl_sample(SHADER_OPCODE_ARG* arg, DWORD sampler_idx, const char *dst_str, const char *coord_reg) {
891 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
892 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
893 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
894 const char sampler_prefix = shader_is_pshader_version(This->baseShader.hex_version) ? 'P' : 'V';
895 SHADER_BUFFER* buffer = arg->buffer;
897 if(deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS] & WINED3DTTFF_PROJECTED) {
898 /* Note that there's no such thing as a projected cube texture. */
899 switch(sampler_type) {
901 shader_addline(buffer, "%s = texture2DProj(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
903 case WINED3DSTT_VOLUME:
904 shader_addline(buffer, "%s = texture3DProj(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
907 shader_addline(buffer, "%s = unrecognized_stype(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
908 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
912 switch(sampler_type) {
914 shader_addline(buffer, "%s = texture2D(%csampler%u, %s.xy);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
916 case WINED3DSTT_CUBE:
917 shader_addline(buffer, "%s = textureCube(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
919 case WINED3DSTT_VOLUME:
920 shader_addline(buffer, "%s = texture3D(%csampler%u, %s.xyz);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
923 shader_addline(buffer, "%s = unrecognized_stype(%csampler%u, %s);\n", dst_str, sampler_prefix, sampler_idx, coord_reg);
924 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
931 /*****************************************************************************
933 * Begin processing individual instruction opcodes
935 ****************************************************************************/
937 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
938 void shader_glsl_arith(SHADER_OPCODE_ARG* arg) {
939 CONST SHADER_OPCODE* curOpcode = arg->opcode;
940 SHADER_BUFFER* buffer = arg->buffer;
941 char src0_reg[50], src1_reg[50];
942 char src0_mask[6], src1_mask[6];
943 char src0_str[100], src1_str[100];
947 /* Determine the GLSL operator to use based on the opcode */
948 switch (curOpcode->opcode) {
949 case WINED3DSIO_MUL: op = '*'; break;
950 case WINED3DSIO_ADD: op = '+'; break;
951 case WINED3DSIO_SUB: op = '-'; break;
954 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
958 write_mask = shader_glsl_append_dst(buffer, arg);
959 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
960 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, src1_reg, src1_mask, src1_str);
961 shader_addline(buffer, "%s %c %s);\n", src0_str, op, src1_str);
964 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
965 void shader_glsl_mov(SHADER_OPCODE_ARG* arg) {
967 SHADER_BUFFER* buffer = arg->buffer;
969 char dst_str[100], src0_str[100];
970 char dst_reg[50], src0_reg[50];
971 char dst_mask[6], src0_mask[6];
973 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
974 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
975 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
976 shader_addline(buffer, "%s%s)%s;\n", tmpLine, src0_str, dst_mask);
979 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
980 void shader_glsl_dot(SHADER_OPCODE_ARG* arg) {
981 CONST SHADER_OPCODE* curOpcode = arg->opcode;
982 SHADER_BUFFER* buffer = arg->buffer;
983 char src0_str[100], src1_str[100];
984 char src0_reg[50], src1_reg[50];
985 char src0_mask[6], src1_mask[6];
986 DWORD dst_write_mask, src_write_mask;
989 dst_write_mask = shader_glsl_append_dst(buffer, arg);
990 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
992 /* dp3 works on vec3, dp4 on vec4 */
993 if (curOpcode->opcode == WINED3DSIO_DP4) {
994 src_write_mask = WINED3DSP_WRITEMASK_ALL;
996 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
999 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, src0_reg, src0_mask, src0_str);
1000 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, src1_reg, src1_mask, src1_str);
1003 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_str, src1_str);
1005 shader_addline(buffer, "dot(%s, %s));\n", src0_str, src1_str);
1009 /* Note that this instruction has some restrictions. The destination write mask
1010 * can't contain the w component, and the source swizzles have to be .xyzw */
1011 void shader_glsl_cross(SHADER_OPCODE_ARG *arg) {
1012 char src0_reg[50], src0_mask[6], src0_str[100];
1013 char src1_reg[50], src1_mask[6], src1_str[100];
1014 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1017 shader_glsl_get_write_mask(arg->dst, dst_mask);
1018 shader_glsl_append_dst(arg->buffer, arg);
1019 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, src0_reg, src0_mask, src0_str);
1020 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, src1_reg, src1_mask, src1_str);
1021 shader_addline(arg->buffer, "cross(%s, %s).%s);\n", src0_str, src1_str, dst_mask);
1024 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1025 void shader_glsl_map2gl(SHADER_OPCODE_ARG* arg) {
1026 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1027 SHADER_BUFFER* buffer = arg->buffer;
1028 const char *instruction;
1029 char arguments[256];
1036 /* Determine the GLSL function to use based on the opcode */
1037 /* TODO: Possibly make this a table for faster lookups */
1038 switch (curOpcode->opcode) {
1039 case WINED3DSIO_MIN: instruction = "min"; break;
1040 case WINED3DSIO_MAX: instruction = "max"; break;
1041 case WINED3DSIO_RSQ: instruction = "inversesqrt"; break;
1042 case WINED3DSIO_ABS: instruction = "abs"; break;
1043 case WINED3DSIO_FRC: instruction = "fract"; break;
1044 case WINED3DSIO_POW: instruction = "pow"; break;
1045 case WINED3DSIO_NRM: instruction = "normalize"; break;
1046 case WINED3DSIO_LOGP:
1047 case WINED3DSIO_LOG: instruction = "log2"; break;
1048 case WINED3DSIO_EXP: instruction = "exp2"; break;
1049 case WINED3DSIO_SGN: instruction = "sign"; break;
1050 default: instruction = "";
1051 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1055 write_mask = shader_glsl_append_dst(buffer, arg);
1057 arguments[0] = '\0';
1058 if (curOpcode->num_params > 0) {
1059 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src_reg, src_mask, src_str);
1060 strcat(arguments, src_str);
1061 for (i = 2; i < curOpcode->num_params; ++i) {
1062 strcat(arguments, ", ");
1063 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, src_reg, src_mask, src_str);
1064 strcat(arguments, src_str);
1068 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1071 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1072 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1073 * dst.x = 2^(floor(src))
1074 * dst.y = src - floor(src)
1075 * dst.z = 2^src (partial precision is allowed, but optional)
1077 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1078 * dst = 2^src; (partial precision is allowed, but optional)
1080 void shader_glsl_expp(SHADER_OPCODE_ARG* arg) {
1083 char dst_str[100], src_str[100];
1084 char dst_reg[50], src_reg[50];
1085 char dst_mask[6], src_mask[6];
1086 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1087 DWORD hex_version = This->baseShader.hex_version;
1089 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1090 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src_reg, src_mask, src_str);
1091 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1093 if (hex_version < WINED3DPS_VERSION(2,0)) {
1094 shader_addline(arg->buffer, "tmp0.x = vec4(exp2(floor(%s))).x;\n", src_str);
1095 shader_addline(arg->buffer, "tmp0.y = vec4(%s - floor(%s)).y;\n", src_str, src_str);
1096 shader_addline(arg->buffer, "tmp0.z = vec4(exp2(%s)).x;\n", src_str);
1097 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1098 shader_addline(arg->buffer, "%svec4(tmp0))%s;\n", tmpLine, dst_mask);
1100 shader_addline(arg->buffer, "%svec4(exp2(%s)))%s;\n", tmpLine, src_str, dst_mask);
1104 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1105 void shader_glsl_rcp(SHADER_OPCODE_ARG* arg) {
1108 char dst_str[100], src_str[100];
1109 char dst_reg[50], src_reg[50];
1110 char dst_mask[6], src_mask[6];
1112 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1113 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src_reg, src_mask, src_str);
1114 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1115 strcat(tmpLine, "1.0 / ");
1116 shader_addline(arg->buffer, "%s%s)%s;\n", tmpLine, src_str, dst_mask);
1119 /** Process signed comparison opcodes in GLSL. */
1120 void shader_glsl_compare(SHADER_OPCODE_ARG* arg) {
1121 char src0_str[100], src1_str[100];
1122 char src0_reg[50], src1_reg[50];
1123 char src0_mask[6], src1_mask[6];
1127 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1128 mask_size = shader_glsl_get_write_mask_size(write_mask);
1129 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
1130 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, src1_reg, src1_mask, src1_str);
1132 if (mask_size > 1) {
1133 const char *compare;
1135 switch(arg->opcode->opcode) {
1136 case WINED3DSIO_SLT: compare = "lessThan"; break;
1137 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1138 default: compare = "";
1139 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1142 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare, src0_str, src1_str);
1144 const char *compare;
1146 switch(arg->opcode->opcode) {
1147 case WINED3DSIO_SLT: compare = "<"; break;
1148 case WINED3DSIO_SGE: compare = ">="; break;
1149 default: compare = "";
1150 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1153 shader_addline(arg->buffer, "(%s %s %s) ? 1.0 : 0.0);\n", src0_str, compare, src1_str);
1157 /** Process CMP instruction in GLSL (dst = src0.x > 0.0 ? src1.x : src2.x), per channel */
1158 void shader_glsl_cmp(SHADER_OPCODE_ARG* arg) {
1161 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1162 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1163 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1165 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1166 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1167 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1168 shader_glsl_add_src_param_old(arg, arg->src[2], arg->src_addr[2], src2_reg, src2_mask, src2_str);
1170 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1171 shader_addline(arg->buffer, "%smix(vec4(%s), vec4(%s), vec4(lessThan(vec4(%s), vec4(0.0)))))%s;\n",
1172 tmpLine, src1_str, src2_str, src0_str, dst_mask);
1175 /** Process the CND opcode in GLSL (dst = (src0 < 0.5) ? src1 : src2) */
1176 void shader_glsl_cnd(SHADER_OPCODE_ARG* arg) {
1179 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1180 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1181 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1183 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1184 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1185 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1186 shader_glsl_add_src_param_old(arg, arg->src[2], arg->src_addr[2], src2_reg, src2_mask, src2_str);
1187 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1188 shader_addline(arg->buffer, "%s(%s < 0.5) ? %s : %s)%s;\n",
1189 tmpLine, src0_str, src1_str, src2_str, dst_mask);
1192 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1193 void shader_glsl_mad(SHADER_OPCODE_ARG* arg) {
1194 char src0_str[100], src1_str[100], src2_str[100];
1195 char src0_reg[50], src1_reg[50], src2_reg[50];
1196 char src0_mask[6], src1_mask[6], src2_mask[6];
1199 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1200 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, src0_reg, src0_mask, src0_str);
1201 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, src1_reg, src1_mask, src1_str);
1202 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, src2_reg, src2_mask, src2_str);
1203 shader_addline(arg->buffer, "(%s * %s) + %s);\n", src0_str, src1_str, src2_str);
1206 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1207 Vertex shaders to GLSL codes */
1208 void shader_glsl_mnxn(SHADER_OPCODE_ARG* arg) {
1210 int nComponents = 0;
1211 SHADER_OPCODE_ARG tmpArg;
1213 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1215 /* Set constants for the temporary argument */
1216 tmpArg.shader = arg->shader;
1217 tmpArg.buffer = arg->buffer;
1218 tmpArg.src[0] = arg->src[0];
1219 tmpArg.src_addr[0] = arg->src_addr[0];
1220 tmpArg.src_addr[1] = arg->src_addr[1];
1221 tmpArg.reg_maps = arg->reg_maps;
1223 switch(arg->opcode->opcode) {
1224 case WINED3DSIO_M4x4:
1226 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1228 case WINED3DSIO_M4x3:
1230 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP4);
1232 case WINED3DSIO_M3x4:
1234 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1236 case WINED3DSIO_M3x3:
1238 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1240 case WINED3DSIO_M3x2:
1242 tmpArg.opcode = shader_get_opcode(arg->shader, WINED3DSIO_DP3);
1248 for (i = 0; i < nComponents; i++) {
1249 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1250 tmpArg.src[1] = arg->src[1]+i;
1251 shader_glsl_dot(&tmpArg);
1256 The LRP instruction performs a component-wise linear interpolation
1257 between the second and third operands using the first operand as the
1258 blend factor. Equation: (dst = src2 * (src1 - src0) + src0)
1260 void shader_glsl_lrp(SHADER_OPCODE_ARG* arg) {
1263 char dst_str[100], src0_str[100], src1_str[100], src2_str[100];
1264 char dst_reg[50], src0_reg[50], src1_reg[50], src2_reg[50];
1265 char dst_mask[6], src0_mask[6], src1_mask[6], src2_mask[6];
1267 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1268 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1269 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1270 shader_glsl_add_src_param_old(arg, arg->src[2], arg->src_addr[2], src2_reg, src2_mask, src2_str);
1272 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1274 shader_addline(arg->buffer, "%s%s + %s * (%s - %s))%s;\n",
1275 tmpLine, src2_str, src0_str, src1_str, src2_str, dst_mask);
1278 /** Process the WINED3DSIO_LIT instruction in GLSL:
1279 * dst.x = dst.w = 1.0
1280 * dst.y = (src0.x > 0) ? src0.x
1281 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1282 * where src.w is clamped at +- 128
1284 void shader_glsl_lit(SHADER_OPCODE_ARG* arg) {
1286 char dst_str[100], src0_str[100];
1287 char dst_reg[50], src0_reg[50];
1288 char dst_mask[6], src0_mask[6];
1290 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1291 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1293 shader_addline(arg->buffer,
1294 "%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",
1295 dst_str, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, src0_reg, dst_mask);
1298 /** Process the WINED3DSIO_DST instruction in GLSL:
1300 * dst.y = src0.x * src0.y
1304 void shader_glsl_dst(SHADER_OPCODE_ARG* arg) {
1306 char dst_str[100], src0_str[100], src1_str[100];
1307 char dst_reg[50], src0_reg[50], src1_reg[50];
1308 char dst_mask[6], src0_mask[6], src1_mask[6];
1310 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1311 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1312 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1314 shader_addline(arg->buffer, "%s = vec4(1.0, %s.x * %s.y, %s.z, %s.w)%s;\n",
1315 dst_str, src0_reg, src1_reg, src0_reg, src1_reg, dst_mask);
1318 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
1319 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
1320 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
1322 * dst.x = cos(src0.?)
1323 * dst.y = sin(src0.?)
1327 void shader_glsl_sincos(SHADER_OPCODE_ARG* arg) {
1333 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1334 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, src0_reg, src0_mask, src0_str);
1336 switch (write_mask) {
1337 case WINED3DSP_WRITEMASK_0:
1338 shader_addline(arg->buffer, "cos(%s));\n", src0_str);
1341 case WINED3DSP_WRITEMASK_1:
1342 shader_addline(arg->buffer, "sin(%s));\n", src0_str);
1345 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
1346 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_str, src0_str);
1350 ERR("Write mask should be .x, .y or .xy\n");
1355 /** Process the WINED3DSIO_LOOP instruction in GLSL:
1356 * Start a for() loop where src0.y is the initial value of aL,
1357 * increment aL by src0.z for a total of src0.x iterations.
1358 * Need to use a temporary variable for this operation.
1360 void shader_glsl_loop(SHADER_OPCODE_ARG* arg) {
1366 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1368 shader_addline(arg->buffer, "for (tmpInt = 0, aL = %s.y; tmpInt < %s.x; tmpInt++, aL += %s.z) {\n",
1369 src1_reg, src1_reg, src1_reg);
1372 void shader_glsl_end(SHADER_OPCODE_ARG* arg) {
1373 shader_addline(arg->buffer, "}\n");
1376 void shader_glsl_rep(SHADER_OPCODE_ARG* arg) {
1382 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1383 shader_addline(arg->buffer, "for (tmpInt = 0; tmpInt < %s.x; tmpInt++) {\n", src0_reg);
1386 void shader_glsl_if(SHADER_OPCODE_ARG* arg) {
1392 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1393 shader_addline(arg->buffer, "if (%s) {\n", src0_str);
1396 void shader_glsl_ifc(SHADER_OPCODE_ARG* arg) {
1398 char src0_str[100], src1_str[100];
1399 char src0_reg[50], src1_reg[50];
1400 char src0_mask[6], src1_mask[6];
1402 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1403 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1405 shader_addline(arg->buffer, "if (%s %s %s) {\n",
1406 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1409 void shader_glsl_else(SHADER_OPCODE_ARG* arg) {
1410 shader_addline(arg->buffer, "} else {\n");
1413 void shader_glsl_break(SHADER_OPCODE_ARG* arg) {
1414 shader_addline(arg->buffer, "break;\n");
1417 void shader_glsl_breakc(SHADER_OPCODE_ARG* arg) {
1419 char src0_str[100], src1_str[100];
1420 char src0_reg[50], src1_reg[50];
1421 char src0_mask[6], src1_mask[6];
1423 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1424 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1426 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
1427 src0_str, shader_get_comp_op(arg->opcode_token), src1_str);
1430 void shader_glsl_label(SHADER_OPCODE_ARG* arg) {
1432 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1433 shader_addline(arg->buffer, "}\n");
1434 shader_addline(arg->buffer, "void subroutine%lu () {\n", snum);
1437 void shader_glsl_call(SHADER_OPCODE_ARG* arg) {
1438 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1439 shader_addline(arg->buffer, "subroutine%lu();\n", snum);
1442 void shader_glsl_callnz(SHADER_OPCODE_ARG* arg) {
1448 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
1449 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_reg, src1_mask, src1_str);
1450 shader_addline(arg->buffer, "if (%s) subroutine%lu();\n", src1_str, snum);
1453 /*********************************************
1454 * Pixel Shader Specific Code begins here
1455 ********************************************/
1456 void pshader_glsl_tex(SHADER_OPCODE_ARG* arg) {
1457 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1459 DWORD hex_version = This->baseShader.hex_version;
1461 char dst_str[100], dst_reg[50], dst_mask[6];
1462 char coord_str[100], coord_reg[50], coord_mask[6];
1464 /* All versions have a destination register */
1465 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1467 /* 1.0-1.3: Use destination register as coordinate source.
1468 1.4+: Use provided coordinate source register. */
1469 if (hex_version < WINED3DPS_VERSION(1,4))
1470 strcpy(coord_reg, dst_reg);
1472 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], coord_reg, coord_mask, coord_str);
1474 /* 1.0-1.4: Use destination register as sampler source.
1475 * 2.0+: Use provided sampler source. */
1476 if (hex_version < WINED3DPS_VERSION(2,0)) {
1477 shader_glsl_sample(arg, arg->dst & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1479 shader_glsl_sample(arg, arg->src[1] & WINED3DSP_REGNUM_MASK, dst_str, coord_reg);
1484 void pshader_glsl_texcoord(SHADER_OPCODE_ARG* arg) {
1486 /* FIXME: Make this work for more than just 2D textures */
1488 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1489 SHADER_BUFFER* buffer = arg->buffer;
1490 DWORD hex_version = This->baseShader.hex_version;
1497 shader_glsl_add_dst_param(arg, arg->dst, 0, tmpReg, tmpMask, tmpStr);
1499 if (hex_version != WINED3DPS_VERSION(1,4)) {
1500 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1501 shader_addline(buffer, "%s = clamp(gl_TexCoord[%u], 0.0, 1.0);\n", tmpReg, reg);
1503 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1504 shader_addline(buffer, "%s = gl_TexCoord[%u]%s;\n", tmpStr, reg2, tmpMask);
1508 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
1509 * Take a 3-component dot product of the TexCoord[dstreg] and src,
1510 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
1511 void pshader_glsl_texdp3tex(SHADER_OPCODE_ARG* arg) {
1513 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1514 char src0_str[100], dst_str[100];
1515 char src0_name[50], dst_name[50];
1516 char src0_mask[6], dst_mask[6];
1518 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1519 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1521 shader_addline(arg->buffer, "tmp0.x = dot(vec3(gl_TexCoord[%u]), vec3(%s));\n", dstreg, src0_str);
1522 shader_addline(arg->buffer, "%s = vec4(texture1D(Psampler%u, tmp0.x))%s;\n", dst_str, dstreg, dst_mask);
1525 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
1526 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
1527 void pshader_glsl_texdp3(SHADER_OPCODE_ARG* arg) {
1529 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1530 char src0_str[100], dst_str[100];
1531 char src0_name[50], dst_name[50];
1532 char src0_mask[6], dst_mask[6];
1534 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1535 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1537 shader_addline(arg->buffer, "%s = vec4(dot(vec3(T%u), vec3(%s)))%s;\n",
1538 dst_str, dstreg, src0_str, dst_mask);
1541 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
1542 * Calculate the depth as dst.x / dst.y */
1543 void pshader_glsl_texdepth(SHADER_OPCODE_ARG* arg) {
1549 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1551 shader_addline(arg->buffer, "gl_FragDepth = %s.x / %s.y;\n", dst_reg, dst_reg);
1554 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
1555 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
1556 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
1557 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
1559 void pshader_glsl_texm3x2depth(SHADER_OPCODE_ARG* arg) {
1561 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1562 char src0_str[100], dst_str[100];
1563 char src0_name[50], dst_name[50];
1564 char src0_mask[6], dst_mask[6];
1566 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1567 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1569 shader_addline(arg->buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", dstreg, src0_str);
1570 shader_addline(arg->buffer, "gl_FragDepth = vec4((tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y)%s;\n", dst_str, dst_name);
1573 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
1574 * Calculate the 1st of a 2-row matrix multiplication. */
1575 void pshader_glsl_texm3x2pad(SHADER_OPCODE_ARG* arg) {
1577 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1578 SHADER_BUFFER* buffer = arg->buffer;
1583 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1584 shader_addline(buffer, "tmp0.x = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1587 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
1588 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
1589 void pshader_glsl_texm3x3pad(SHADER_OPCODE_ARG* arg) {
1591 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1592 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1593 SHADER_BUFFER* buffer = arg->buffer;
1594 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1599 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1600 shader_addline(buffer, "tmp0.%c = dot(vec3(T%u), vec3(%s));\n", 'x' + current_state->current_row, reg, src0_str);
1601 current_state->texcoord_w[current_state->current_row++] = reg;
1604 void pshader_glsl_texm3x2tex(SHADER_OPCODE_ARG* arg) {
1606 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1607 SHADER_BUFFER* buffer = arg->buffer;
1612 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1613 shader_addline(buffer, "tmp0.y = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1615 /* Sample the texture using the calculated coordinates */
1616 sprintf(dst_str, "T%u", reg);
1617 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1620 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
1621 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
1622 void pshader_glsl_texm3x3tex(SHADER_OPCODE_ARG* arg) {
1627 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1628 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1629 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1631 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1632 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1634 /* Sample the texture using the calculated coordinates */
1635 sprintf(dst_str, "T%u", reg);
1636 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1637 current_state->current_row = 0;
1640 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
1641 * Perform the 3rd row of a 3x3 matrix multiply */
1642 void pshader_glsl_texm3x3(SHADER_OPCODE_ARG* arg) {
1647 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1648 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
1649 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
1651 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1653 shader_addline(arg->buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1654 shader_addline(arg->buffer, "T%u = vec4(tmp0.x, tmp0.y, tmp0.z, 1.0);\n", reg);
1655 current_state->current_row = 0;
1658 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
1659 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1660 void pshader_glsl_texm3x3spec(SHADER_OPCODE_ARG* arg) {
1662 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1663 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1666 char src0_str[100], src0_name[50], src0_mask[6];
1667 char src1_str[100], src1_name[50], src1_mask[6];
1668 SHADER_BUFFER* buffer = arg->buffer;
1669 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1670 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
1673 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1674 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1675 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1677 strcpy(dimensions, "");
1678 FIXME("Unrecognized sampler type: %#x\n", stype);
1682 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1683 shader_glsl_add_src_param_old(arg, arg->src[1], arg->src_addr[1], src1_name, src1_mask, src1_str);
1685 /* Perform the last matrix multiply operation */
1686 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1688 /* Calculate reflection vector */
1689 shader_addline(buffer, "tmp0.xyz = reflect(-vec3(%s), vec3(tmp0));\n", src1_str);
1691 /* Sample the texture */
1692 sprintf(dst_str, "T%u", reg);
1693 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1694 current_state->current_row = 0;
1697 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
1698 * Peform the final texture lookup based on the previous 2 3x3 matrix multiplies */
1699 void pshader_glsl_texm3x3vspec(SHADER_OPCODE_ARG* arg) {
1701 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
1702 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
1703 SHADER_BUFFER* buffer = arg->buffer;
1704 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
1706 char src0_str[100], src0_name[50], src0_mask[6];
1708 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_name, src0_mask, src0_str);
1710 /* Perform the last matrix multiply operation */
1711 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_str);
1713 /* Construct the eye-ray vector from w coordinates */
1714 shader_addline(buffer, "tmp1.x = gl_TexCoord[%u].w;\n", current_state->texcoord_w[0]);
1715 shader_addline(buffer, "tmp1.y = gl_TexCoord[%u].w;\n", current_state->texcoord_w[1]);
1716 shader_addline(buffer, "tmp1.z = gl_TexCoord[%u].w;\n", reg);
1718 /* Calculate reflection vector (Assume normal is normalized): RF = 2*(N.E)*N -E */
1719 shader_addline(buffer, "tmp0.x = dot(vec3(tmp0), vec3(tmp1));\n");
1720 shader_addline(buffer, "tmp0 = tmp0.w * tmp0;\n");
1721 shader_addline(buffer, "tmp0 = (2.0 * tmp0) - tmp1;\n");
1723 /* Sample the texture using the calculated coordinates */
1724 sprintf(dst_str, "T%u", reg);
1725 shader_glsl_sample(arg, reg, dst_str, "tmp0");
1726 current_state->current_row = 0;
1729 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
1730 * Apply a fake bump map transform.
1731 * FIXME: Should apply the BUMPMAPENV matrix. For now, just sample the texture */
1732 void pshader_glsl_texbem(SHADER_OPCODE_ARG* arg) {
1734 DWORD reg1 = arg->dst & WINED3DSP_REGNUM_MASK;
1735 DWORD reg2 = arg->src[0] & WINED3DSP_REGNUM_MASK;
1737 FIXME("Not applying the BUMPMAPENV matrix for pixel shader instruction texbem.\n");
1738 shader_addline(arg->buffer, "T%u = texture2D(Psampler%u, gl_TexCoord[%u].xy + T%u.xy);\n",
1739 reg1, reg1, reg1, reg2);
1742 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
1743 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
1744 void pshader_glsl_texreg2ar(SHADER_OPCODE_ARG* arg) {
1747 char dst_str[100], src0_str[100];
1748 char dst_reg[50], src0_reg[50];
1749 char dst_mask[6], src0_mask[6];
1750 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1752 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1753 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1755 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1756 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1757 tmpLine, src0_regnum, dst_reg, dst_mask);
1760 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
1761 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
1762 void pshader_glsl_texreg2gb(SHADER_OPCODE_ARG* arg) {
1765 char dst_str[100], src0_str[100];
1766 char dst_reg[50], src0_reg[50];
1767 char dst_mask[6], src0_mask[6];
1768 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1770 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1771 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1773 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1774 shader_addline(arg->buffer, "%stexture2D(Psampler%u, %s.yz))%s;\n",
1775 tmpLine, src0_regnum, dst_reg, dst_mask);
1778 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
1779 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
1780 void pshader_glsl_texreg2rgb(SHADER_OPCODE_ARG* arg) {
1783 char dst_str[100], src0_str[100];
1784 char dst_reg[50], src0_reg[50];
1785 char dst_mask[6], src0_mask[6];
1787 DWORD src0_regnum = arg->src[0] & WINED3DSP_REGNUM_MASK;
1788 DWORD stype = arg->reg_maps->samplers[src0_regnum] & WINED3DSP_TEXTURETYPE_MASK;
1790 case WINED3DSTT_2D: strcpy(dimensions, "2D"); break;
1791 case WINED3DSTT_CUBE: strcpy(dimensions, "Cube"); break;
1792 case WINED3DSTT_VOLUME: strcpy(dimensions, "3D"); break;
1794 strcpy(dimensions, "");
1795 FIXME("Unrecognized sampler type: %#x\n", stype);
1799 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_reg, dst_mask, dst_str);
1800 shader_glsl_add_src_param_old(arg, arg->src[0], arg->src_addr[0], src0_reg, src0_mask, src0_str);
1802 shader_glsl_add_dst_old(arg->dst, dst_reg, dst_mask, tmpLine);
1803 shader_addline(arg->buffer, "%stexture%s(Psampler%u, %s.%s))%s;\n",
1804 tmpLine, dimensions, src0_regnum, dst_reg, (stype == WINED3DSTT_2D) ? "xy" : "xyz", dst_mask);
1807 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
1808 * If any of the first 3 components are < 0, discard this pixel */
1809 void pshader_glsl_texkill(SHADER_OPCODE_ARG* arg) {
1811 char dst_str[100], dst_name[50], dst_mask[6];
1813 shader_glsl_add_dst_param(arg, arg->dst, 0, dst_name, dst_mask, dst_str);
1814 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_name);
1817 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
1818 * dst = dot2(src0, src1) + src2 */
1819 void pshader_glsl_dp2add(SHADER_OPCODE_ARG* arg) {
1820 char src0_str[100], src1_str[100], src2_str[100];
1821 char src0_reg[50], src1_reg[50], src2_reg[50];
1822 char src0_mask[6], src1_mask[6], src2_mask[6];
1826 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1827 mask_size = shader_glsl_get_write_mask_size(write_mask);
1829 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, src0_reg, src0_mask, src0_str);
1830 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, src1_reg, src1_mask, src1_str);
1831 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, src2_reg, src2_mask, src2_str);
1833 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_str, src1_str, src2_str);
1836 void pshader_glsl_input_pack(
1837 SHADER_BUFFER* buffer,
1838 semantic* semantics_in) {
1842 for (i = 0; i < MAX_REG_INPUT; i++) {
1844 DWORD usage_token = semantics_in[i].usage;
1845 DWORD register_token = semantics_in[i].reg;
1846 DWORD usage, usage_idx;
1850 if (!usage_token) continue;
1851 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1852 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1853 shader_glsl_get_write_mask(register_token, reg_mask);
1857 case D3DDECLUSAGE_COLOR:
1859 shader_addline(buffer, "IN%u%s = vec4(gl_Color)%s;\n",
1860 i, reg_mask, reg_mask);
1861 else if (usage_idx == 1)
1862 shader_addline(buffer, "IN%u%s = vec4(gl_SecondaryColor)%s;\n",
1863 i, reg_mask, reg_mask);
1865 shader_addline(buffer, "IN%u%s = vec4(unsupported_color_input)%s;\n",
1866 i, reg_mask, reg_mask);
1869 case D3DDECLUSAGE_TEXCOORD:
1870 shader_addline(buffer, "IN%u%s = vec4(gl_TexCoord[%u])%s;\n",
1871 i, reg_mask, usage_idx, reg_mask );
1874 case D3DDECLUSAGE_FOG:
1875 shader_addline(buffer, "IN%u%s = vec4(gl_FogFragCoord)%s;\n",
1876 i, reg_mask, reg_mask);
1880 shader_addline(buffer, "IN%u%s = vec4(unsupported_input)%s;\n",
1881 i, reg_mask, reg_mask);
1886 /*********************************************
1887 * Vertex Shader Specific Code begins here
1888 ********************************************/
1890 void vshader_glsl_output_unpack(
1891 SHADER_BUFFER* buffer,
1892 semantic* semantics_out) {
1896 for (i = 0; i < MAX_REG_OUTPUT; i++) {
1898 DWORD usage_token = semantics_out[i].usage;
1899 DWORD register_token = semantics_out[i].reg;
1900 DWORD usage, usage_idx;
1904 if (!usage_token) continue;
1906 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
1907 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
1908 shader_glsl_get_write_mask(register_token, reg_mask);
1912 case D3DDECLUSAGE_COLOR:
1914 shader_addline(buffer, "gl_FrontColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1915 else if (usage_idx == 1)
1916 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1918 shader_addline(buffer, "unsupported_color_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1921 case D3DDECLUSAGE_POSITION:
1922 shader_addline(buffer, "gl_Position%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1925 case D3DDECLUSAGE_TEXCOORD:
1926 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT%u%s;\n",
1927 usage_idx, reg_mask, i, reg_mask);
1930 case WINED3DSHADERDECLUSAGE_PSIZE:
1931 shader_addline(buffer, "gl_PointSize = OUT%u.x;\n", i);
1934 case WINED3DSHADERDECLUSAGE_FOG:
1935 shader_addline(buffer, "gl_FogFragCoord%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1939 shader_addline(buffer, "unsupported_output%s = OUT%u%s;\n", reg_mask, i, reg_mask);
1944 /** Attach a GLSL pixel or vertex shader object to the shader program */
1945 static void attach_glsl_shader(IWineD3DDevice *iface, IWineD3DBaseShader* shader) {
1946 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1947 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1948 GLhandleARB shaderObj = ((IWineD3DBaseShaderImpl*)shader)->baseShader.prgId;
1949 if (This->stateBlock->glsl_program && shaderObj != 0) {
1950 TRACE("Attaching GLSL shader object %u to program %u\n", shaderObj, This->stateBlock->glsl_program->programId);
1951 GL_EXTCALL(glAttachObjectARB(This->stateBlock->glsl_program->programId, shaderObj));
1952 checkGLcall("glAttachObjectARB");
1956 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
1957 * It sets the programId on the current StateBlock (because it should be called
1958 * inside of the DrawPrimitive() part of the render loop).
1960 * If a program for the given combination does not exist, create one, and store
1961 * the program in the list. If it creates a program, it will link the given
1964 * We keep the shader programs around on a list because linking
1965 * shader objects together is an expensive operation. It's much
1966 * faster to loop through a list of pre-compiled & linked programs
1967 * each time that the application sets a new pixel or vertex shader
1968 * than it is to re-link them together at that time.
1970 * The list will be deleted in IWineD3DDevice::Release().
1972 static void set_glsl_shader_program(IWineD3DDevice *iface) {
1973 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
1974 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
1975 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
1976 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
1977 struct glsl_shader_prog_link *curLink = NULL;
1978 struct glsl_shader_prog_link *newLink = NULL;
1979 struct list *ptr = NULL;
1980 GLhandleARB programId = 0;
1984 ptr = list_head( &This->glsl_shader_progs );
1986 /* At least one program exists - see if it matches our ps/vs combination */
1987 curLink = LIST_ENTRY( ptr, struct glsl_shader_prog_link, entry );
1988 if (vshader == curLink->vertexShader && pshader == curLink->pixelShader) {
1989 /* Existing Program found, use it */
1990 TRACE("Found existing program (%u) for this vertex/pixel shader combination\n",
1991 curLink->programId);
1992 This->stateBlock->glsl_program = curLink;
1995 /* This isn't the entry we need - try the next one */
1996 ptr = list_next( &This->glsl_shader_progs, ptr );
1999 /* If we get to this point, then no matching program exists, so we create one */
2000 programId = GL_EXTCALL(glCreateProgramObjectARB());
2001 TRACE("Created new GLSL shader program %u\n", programId);
2003 /* Allocate a new link for the list of programs */
2004 newLink = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
2005 newLink->programId = programId;
2006 This->stateBlock->glsl_program = newLink;
2008 /* Attach GLSL vshader */
2009 if (NULL != vshader && This->vs_selected_mode == SHADER_GLSL) {
2011 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
2014 TRACE("Attaching vertex shader to GLSL program\n");
2015 attach_glsl_shader(iface, (IWineD3DBaseShader*)vshader);
2017 /* Bind vertex attributes to a corresponding index number to match
2018 * the same index numbers as ARB_vertex_programs (makes loading
2019 * vertex attributes simpler). With this method, we can use the
2020 * exact same code to load the attributes later for both ARB and
2023 * We have to do this here because we need to know the Program ID
2024 * in order to make the bindings work, and it has to be done prior
2025 * to linking the GLSL program. */
2026 for (i = 0; i < max_attribs; ++i) {
2027 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
2028 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
2030 checkGLcall("glBindAttribLocationARB");
2031 newLink->vertexShader = vshader;
2034 /* Attach GLSL pshader */
2035 if (NULL != pshader && This->ps_selected_mode == SHADER_GLSL) {
2036 TRACE("Attaching pixel shader to GLSL program\n");
2037 attach_glsl_shader(iface, (IWineD3DBaseShader*)pshader);
2038 newLink->pixelShader = pshader;
2041 /* Link the program */
2042 TRACE("Linking GLSL shader program %u\n", programId);
2043 GL_EXTCALL(glLinkProgramARB(programId));
2044 print_glsl_info_log(&GLINFO_LOCATION, programId);
2045 list_add_head( &This->glsl_shader_progs, &newLink->entry);
2047 newLink->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
2048 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
2049 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
2050 newLink->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2052 newLink->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
2053 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
2054 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
2055 newLink->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
2061 static GLhandleARB create_glsl_blt_shader(WineD3D_GL_Info *gl_info) {
2062 GLhandleARB program_id;
2063 GLhandleARB vshader_id, pshader_id;
2064 const char *blt_vshader[] = {
2067 " gl_Position = gl_Vertex;\n"
2068 " gl_FrontColor = vec4(1.0);\n"
2069 " gl_TexCoord[0].x = (gl_Vertex.x * 0.5) + 0.5;\n"
2070 " gl_TexCoord[0].y = (-gl_Vertex.y * 0.5) + 0.5;\n"
2074 const char *blt_pshader[] = {
2075 "uniform sampler2D sampler;\n"
2078 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
2082 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
2083 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
2084 GL_EXTCALL(glCompileShaderARB(vshader_id));
2086 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
2087 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, blt_pshader, NULL));
2088 GL_EXTCALL(glCompileShaderARB(pshader_id));
2090 program_id = GL_EXTCALL(glCreateProgramObjectARB());
2091 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
2092 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
2093 GL_EXTCALL(glLinkProgramARB(program_id));
2095 print_glsl_info_log(&GLINFO_LOCATION, program_id);
2100 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
2101 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2102 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2103 GLhandleARB program_id = 0;
2105 if (useVS || usePS) set_glsl_shader_program(iface);
2106 else This->stateBlock->glsl_program = NULL;
2108 program_id = This->stateBlock->glsl_program ? This->stateBlock->glsl_program->programId : 0;
2109 if (program_id) TRACE("Using GLSL program %u\n", program_id);
2110 GL_EXTCALL(glUseProgramObjectARB(program_id));
2111 checkGLcall("glUseProgramObjectARB");
2114 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface) {
2115 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
2116 WineD3D_GL_Info *gl_info = &((IWineD3DImpl *)(This->wineD3D))->gl_info;
2117 static GLhandleARB program_id = 0;
2118 static GLhandleARB loc = -1;
2121 program_id = create_glsl_blt_shader(gl_info);
2122 loc = GL_EXTCALL(glGetUniformLocationARB(program_id, "sampler"));
2125 GL_EXTCALL(glUseProgramObjectARB(program_id));
2126 GL_EXTCALL(glUniform1iARB(loc, 0));
2129 static void shader_glsl_cleanup(BOOL usePS, BOOL useVS) {
2133 const shader_backend_t glsl_shader_backend = {
2134 &shader_glsl_select,
2135 &shader_glsl_select_depth_blt,
2136 &shader_glsl_load_constants,
2137 &shader_glsl_cleanup