cryptui: Add a header bitmap to CryptUIWizImport's interior pages.
[wine/wine64.git] / dlls / wined3d / glsl_shader.c
blob2aceb9068b9f003ee552deed590d23561d9856aa
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * D3D shader asm has swizzles on source parameters, and write masks for
25 * destination parameters. GLSL uses swizzles for both. The result of this is
26 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
27 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
28 * mask for the destination parameter into account.
31 #include "config.h"
32 #include <limits.h>
33 #include <stdio.h>
34 #include "wined3d_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
37 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d);
41 #define GLINFO_LOCATION (*gl_info)
43 typedef struct {
44 char reg_name[150];
45 char mask_str[6];
46 } glsl_dst_param_t;
48 typedef struct {
49 char reg_name[150];
50 char param_str[100];
51 } glsl_src_param_t;
53 typedef struct {
54 const char *name;
55 DWORD coord_mask;
56 } glsl_sample_function_t;
58 enum heap_node_op
60 HEAP_NODE_TRAVERSE_LEFT,
61 HEAP_NODE_TRAVERSE_RIGHT,
62 HEAP_NODE_POP,
65 struct constant_entry
67 unsigned int idx;
68 unsigned int version;
71 struct constant_heap
73 struct constant_entry *entries;
74 unsigned int *positions;
75 unsigned int size;
78 /* GLSL shader private data */
79 struct shader_glsl_priv {
80 struct hash_table_t *glsl_program_lookup;
81 struct glsl_shader_prog_link *glsl_program;
82 struct constant_heap vconst_heap;
83 struct constant_heap pconst_heap;
84 unsigned char *stack;
85 GLhandleARB depth_blt_program[tex_type_count];
86 UINT next_constant_version;
89 /* Struct to maintain data about a linked GLSL program */
90 struct glsl_shader_prog_link {
91 struct list vshader_entry;
92 struct list pshader_entry;
93 GLhandleARB programId;
94 GLhandleARB *vuniformF_locations;
95 GLhandleARB *puniformF_locations;
96 GLhandleARB vuniformI_locations[MAX_CONST_I];
97 GLhandleARB puniformI_locations[MAX_CONST_I];
98 GLhandleARB posFixup_location;
99 GLhandleARB bumpenvmat_location[MAX_TEXTURES];
100 GLhandleARB luminancescale_location[MAX_TEXTURES];
101 GLhandleARB luminanceoffset_location[MAX_TEXTURES];
102 GLhandleARB ycorrection_location;
103 GLenum vertex_color_clamp;
104 GLhandleARB vshader;
105 IWineD3DPixelShader *pshader;
106 struct ps_compile_args ps_args;
107 UINT constant_version;
110 typedef struct {
111 GLhandleARB vshader;
112 IWineD3DPixelShader *pshader;
113 struct ps_compile_args ps_args;
114 } glsl_program_key_t;
117 /** Prints the GLSL info log which will contain error messages if they exist */
118 static void print_glsl_info_log(const WineD3D_GL_Info *gl_info, GLhandleARB obj)
120 int infologLength = 0;
121 char *infoLog;
122 unsigned int i;
123 BOOL is_spam;
125 static const char * const spam[] =
127 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
128 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
129 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
130 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
131 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
132 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
133 "Fragment shader was successfully compiled to run on hardware.\nWARNING: 0:1: extension 'GL_ARB_draw_buffers' is not supported",
134 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
135 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
136 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
139 GL_EXTCALL(glGetObjectParameterivARB(obj,
140 GL_OBJECT_INFO_LOG_LENGTH_ARB,
141 &infologLength));
143 /* A size of 1 is just a null-terminated string, so the log should be bigger than
144 * that if there are errors. */
145 if (infologLength > 1)
147 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
148 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
150 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
151 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
152 is_spam = FALSE;
154 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
155 if(strcmp(infoLog, spam[i]) == 0) {
156 is_spam = TRUE;
157 break;
160 if(is_spam) {
161 TRACE("Spam received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
162 } else {
163 FIXME("Error received from GLSL shader #%u: %s\n", obj, debugstr_a(infoLog));
165 HeapFree(GetProcessHeap(), 0, infoLog);
170 * Loads (pixel shader) samplers
172 static void shader_glsl_load_psamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
174 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
175 GLhandleARB name_loc;
176 int i;
177 char sampler_name[20];
179 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
180 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
181 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
182 if (name_loc != -1) {
183 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[i];
184 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(fragment_samplers)) {
185 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
186 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
187 checkGLcall("glUniform1iARB");
188 } else {
189 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
195 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info *gl_info, IWineD3DStateBlock *iface, GLhandleARB programId)
197 IWineD3DStateBlockImpl* stateBlock = (IWineD3DStateBlockImpl*) iface;
198 GLhandleARB name_loc;
199 char sampler_name[20];
200 int i;
202 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
203 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
204 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
205 if (name_loc != -1) {
206 int mapped_unit = stateBlock->wineD3DDevice->texUnitMap[MAX_FRAGMENT_SAMPLERS + i];
207 if (mapped_unit != -1 && mapped_unit < GL_LIMITS(combined_samplers)) {
208 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
209 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
210 checkGLcall("glUniform1iARB");
211 } else {
212 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
218 static inline void walk_constant_heap(const WineD3D_GL_Info *gl_info, const float *constants,
219 const GLhandleARB *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
221 int stack_idx = 0;
222 unsigned int heap_idx = 1;
223 unsigned int idx;
225 if (heap->entries[heap_idx].version <= version) return;
227 idx = heap->entries[heap_idx].idx;
228 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
229 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
231 while (stack_idx >= 0)
233 /* Note that we fall through to the next case statement. */
234 switch(stack[stack_idx])
236 case HEAP_NODE_TRAVERSE_LEFT:
238 unsigned int left_idx = heap_idx << 1;
239 if (left_idx < heap->size && heap->entries[left_idx].version > version)
241 heap_idx = left_idx;
242 idx = heap->entries[heap_idx].idx;
243 if (constant_locations[idx] != -1)
244 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
246 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
247 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
248 break;
252 case HEAP_NODE_TRAVERSE_RIGHT:
254 unsigned int right_idx = (heap_idx << 1) + 1;
255 if (right_idx < heap->size && heap->entries[right_idx].version > version)
257 heap_idx = right_idx;
258 idx = heap->entries[heap_idx].idx;
259 if (constant_locations[idx] != -1)
260 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
262 stack[stack_idx++] = HEAP_NODE_POP;
263 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
264 break;
268 case HEAP_NODE_POP:
270 heap_idx >>= 1;
271 --stack_idx;
272 break;
276 checkGLcall("walk_constant_heap()");
279 static inline void apply_clamped_constant(const WineD3D_GL_Info *gl_info, GLint location, const GLfloat *data)
281 GLfloat clamped_constant[4];
283 if (location == -1) return;
285 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0 ? 1.0 : data[0];
286 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0 ? 1.0 : data[1];
287 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0 ? 1.0 : data[2];
288 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0 ? 1.0 : data[3];
290 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
293 static inline void walk_constant_heap_clamped(const WineD3D_GL_Info *gl_info, const float *constants,
294 const GLhandleARB *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
296 int stack_idx = 0;
297 unsigned int heap_idx = 1;
298 unsigned int idx;
300 if (heap->entries[heap_idx].version <= version) return;
302 idx = heap->entries[heap_idx].idx;
303 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
304 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
306 while (stack_idx >= 0)
308 /* Note that we fall through to the next case statement. */
309 switch(stack[stack_idx])
311 case HEAP_NODE_TRAVERSE_LEFT:
313 unsigned int left_idx = heap_idx << 1;
314 if (left_idx < heap->size && heap->entries[left_idx].version > version)
316 heap_idx = left_idx;
317 idx = heap->entries[heap_idx].idx;
318 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
320 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
321 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
322 break;
326 case HEAP_NODE_TRAVERSE_RIGHT:
328 unsigned int right_idx = (heap_idx << 1) + 1;
329 if (right_idx < heap->size && heap->entries[right_idx].version > version)
331 heap_idx = right_idx;
332 idx = heap->entries[heap_idx].idx;
333 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
335 stack[stack_idx++] = HEAP_NODE_POP;
336 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
337 break;
341 case HEAP_NODE_POP:
343 heap_idx >>= 1;
344 --stack_idx;
345 break;
349 checkGLcall("walk_constant_heap_clamped()");
352 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
353 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
354 const float *constants, const GLhandleARB *constant_locations, const struct constant_heap *heap,
355 unsigned char *stack, UINT version)
357 const local_constant *lconst;
359 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
360 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.reg_maps.shader_version) == 1
361 && shader_is_pshader_version(This->baseShader.reg_maps.shader_version))
362 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
363 else
364 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
366 if (!This->baseShader.load_local_constsF)
368 TRACE("No need to load local float constants for this shader\n");
369 return;
372 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
373 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry)
375 GLhandleARB location = constant_locations[lconst->idx];
376 /* We found this uniform name in the program - go ahead and send the data */
377 if (location != -1) GL_EXTCALL(glUniform4fvARB(location, 1, (const GLfloat *)lconst->value));
379 checkGLcall("glUniform4fvARB()");
382 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
383 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
384 const GLhandleARB locations[MAX_CONST_I], const int *constants, WORD constants_set)
386 unsigned int i;
387 struct list* ptr;
389 for (i = 0; constants_set; constants_set >>= 1, ++i)
391 if (!(constants_set & 1)) continue;
393 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
394 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
396 /* We found this uniform name in the program - go ahead and send the data */
397 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
398 checkGLcall("glUniform4ivARB");
401 /* Load immediate constants */
402 ptr = list_head(&This->baseShader.constantsI);
403 while (ptr) {
404 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
405 unsigned int idx = lconst->idx;
406 const GLint *values = (const GLint *)lconst->value;
408 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
409 values[0], values[1], values[2], values[3]);
411 /* We found this uniform name in the program - go ahead and send the data */
412 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
413 checkGLcall("glUniform4ivARB");
414 ptr = list_next(&This->baseShader.constantsI, ptr);
418 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
419 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const WineD3D_GL_Info *gl_info,
420 GLhandleARB programId, const BOOL *constants, WORD constants_set)
422 GLhandleARB tmp_loc;
423 unsigned int i;
424 char tmp_name[8];
425 char is_pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version);
426 const char* prefix = is_pshader? "PB":"VB";
427 struct list* ptr;
429 /* TODO: Benchmark and see if it would be beneficial to store the
430 * locations of the constants to avoid looking up each time */
431 for (i = 0; constants_set; constants_set >>= 1, ++i)
433 if (!(constants_set & 1)) continue;
435 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
437 /* TODO: Benchmark and see if it would be beneficial to store the
438 * locations of the constants to avoid looking up each time */
439 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
440 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
441 if (tmp_loc != -1)
443 /* We found this uniform name in the program - go ahead and send the data */
444 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
445 checkGLcall("glUniform1ivARB");
449 /* Load immediate constants */
450 ptr = list_head(&This->baseShader.constantsB);
451 while (ptr) {
452 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
453 unsigned int idx = lconst->idx;
454 const GLint *values = (const GLint *)lconst->value;
456 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
458 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
459 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
460 if (tmp_loc != -1) {
461 /* We found this uniform name in the program - go ahead and send the data */
462 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
463 checkGLcall("glUniform1ivARB");
465 ptr = list_next(&This->baseShader.constantsB, ptr);
469 static void reset_program_constant_version(void *value, void *context)
471 struct glsl_shader_prog_link *entry = (struct glsl_shader_prog_link *)value;
472 entry->constant_version = 0;
476 * Loads the app-supplied constants into the currently set GLSL program.
478 static void shader_glsl_load_constants(
479 IWineD3DDevice* device,
480 char usePixelShader,
481 char useVertexShader) {
483 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) device;
484 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)deviceImpl->shader_priv;
485 IWineD3DStateBlockImpl* stateBlock = deviceImpl->stateBlock;
486 const WineD3D_GL_Info *gl_info = &deviceImpl->adapter->gl_info;
488 GLhandleARB programId;
489 struct glsl_shader_prog_link *prog = priv->glsl_program;
490 UINT constant_version;
491 int i;
493 if (!prog) {
494 /* No GLSL program set - nothing to do. */
495 return;
497 programId = prog->programId;
498 constant_version = prog->constant_version;
500 if (useVertexShader) {
501 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
503 /* Load DirectX 9 float constants/uniforms for vertex shader */
504 shader_glsl_load_constantsF(vshader, gl_info, stateBlock->vertexShaderConstantF,
505 prog->vuniformF_locations, &priv->vconst_heap, priv->stack, constant_version);
507 /* Load DirectX 9 integer constants/uniforms for vertex shader */
508 if(vshader->baseShader.uses_int_consts) {
509 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations,
510 stateBlock->vertexShaderConstantI, stateBlock->changed.vertexShaderConstantsI);
513 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
514 if(vshader->baseShader.uses_bool_consts) {
515 shader_glsl_load_constantsB(vshader, gl_info, programId,
516 stateBlock->vertexShaderConstantB, stateBlock->changed.vertexShaderConstantsB);
519 /* Upload the position fixup params */
520 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &deviceImpl->posFixup[0]));
521 checkGLcall("glUniform4fvARB");
524 if (usePixelShader) {
526 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
528 /* Load DirectX 9 float constants/uniforms for pixel shader */
529 shader_glsl_load_constantsF(pshader, gl_info, stateBlock->pixelShaderConstantF,
530 prog->puniformF_locations, &priv->pconst_heap, priv->stack, constant_version);
532 /* Load DirectX 9 integer constants/uniforms for pixel shader */
533 if(pshader->baseShader.uses_int_consts) {
534 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations,
535 stateBlock->pixelShaderConstantI, stateBlock->changed.pixelShaderConstantsI);
538 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
539 if(pshader->baseShader.uses_bool_consts) {
540 shader_glsl_load_constantsB(pshader, gl_info, programId,
541 stateBlock->pixelShaderConstantB, stateBlock->changed.pixelShaderConstantsB);
544 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
545 * It can't be 0 for a valid texbem instruction.
547 for(i = 0; i < ((IWineD3DPixelShaderImpl *) pshader)->numbumpenvmatconsts; i++) {
548 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pshader;
549 int stage = ps->luminanceconst[i].texunit;
551 const float *data = (const float *)&stateBlock->textureState[(int)ps->bumpenvmatconst[i].texunit][WINED3DTSS_BUMPENVMAT00];
552 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
553 checkGLcall("glUniformMatrix2fvARB");
555 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
556 * is set too, so we can check that in the needsbumpmat check
558 if(ps->baseShader.reg_maps.luminanceparams[stage]) {
559 const GLfloat *scale = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLSCALE];
560 const GLfloat *offset = (const GLfloat *)&stateBlock->textureState[stage][WINED3DTSS_BUMPENVLOFFSET];
562 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
563 checkGLcall("glUniform1fvARB");
564 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
565 checkGLcall("glUniform1fvARB");
569 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
570 float correction_params[4];
571 if(deviceImpl->render_offscreen) {
572 correction_params[0] = 0.0;
573 correction_params[1] = 1.0;
574 } else {
575 /* position is window relative, not viewport relative */
576 correction_params[0] = ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height;
577 correction_params[1] = -1.0;
579 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
583 if (priv->next_constant_version == UINT_MAX)
585 TRACE("Max constant version reached, resetting to 0.\n");
586 hash_table_for_each_entry(priv->glsl_program_lookup, reset_program_constant_version, NULL);
587 priv->next_constant_version = 1;
589 else
591 prog->constant_version = priv->next_constant_version++;
595 static inline void update_heap_entry(struct constant_heap *heap, unsigned int idx,
596 unsigned int heap_idx, DWORD new_version)
598 struct constant_entry *entries = heap->entries;
599 unsigned int *positions = heap->positions;
600 unsigned int parent_idx;
602 while (heap_idx > 1)
604 parent_idx = heap_idx >> 1;
606 if (new_version <= entries[parent_idx].version) break;
608 entries[heap_idx] = entries[parent_idx];
609 positions[entries[parent_idx].idx] = heap_idx;
610 heap_idx = parent_idx;
613 entries[heap_idx].version = new_version;
614 entries[heap_idx].idx = idx;
615 positions[idx] = heap_idx;
618 static void shader_glsl_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
620 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
621 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
622 struct constant_heap *heap = &priv->vconst_heap;
623 UINT i;
625 for (i = start; i < count + start; ++i)
627 if (!This->stateBlock->changed.vertexShaderConstantsF[i])
628 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
629 else
630 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
634 static void shader_glsl_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
636 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
637 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
638 struct constant_heap *heap = &priv->pconst_heap;
639 UINT i;
641 for (i = start; i < count + start; ++i)
643 if (!This->stateBlock->changed.pixelShaderConstantsF[i])
644 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
645 else
646 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
650 /** Generate the variable & register declarations for the GLSL output target */
651 static void shader_generate_glsl_declarations(IWineD3DBaseShader *iface, const shader_reg_maps *reg_maps,
652 SHADER_BUFFER *buffer, const WineD3D_GL_Info *gl_info,
653 const struct ps_compile_args *ps_args)
655 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
656 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
657 DWORD shader_version = reg_maps->shader_version;
658 unsigned int i, extra_constants_needed = 0;
659 const local_constant *lconst;
661 /* There are some minor differences between pixel and vertex shaders */
662 char pshader = shader_is_pshader_version(shader_version);
663 char prefix = pshader ? 'P' : 'V';
665 /* Prototype the subroutines */
666 for (i = 0; i < This->baseShader.limits.label; i++) {
667 if (reg_maps->labels[i])
668 shader_addline(buffer, "void subroutine%u();\n", i);
671 /* Declare the constants (aka uniforms) */
672 if (This->baseShader.limits.constant_float > 0) {
673 unsigned max_constantsF = min(This->baseShader.limits.constant_float,
674 (pshader ? GL_LIMITS(pshader_constantsF) : GL_LIMITS(vshader_constantsF)));
675 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
678 if (This->baseShader.limits.constant_int > 0)
679 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
681 if (This->baseShader.limits.constant_bool > 0)
682 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
684 if(!pshader) {
685 shader_addline(buffer, "uniform vec4 posFixup;\n");
686 /* Predeclaration; This function is added at link time based on the pixel shader.
687 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
688 * that. We know the input to the reorder function at vertex shader compile time, so
689 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
690 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
691 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
692 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
693 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
694 * inout.
696 if (shader_version >= WINED3DVS_VERSION(3, 0))
698 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
699 } else {
700 shader_addline(buffer, "void order_ps_input();\n");
702 } else {
703 IWineD3DPixelShaderImpl *ps_impl = (IWineD3DPixelShaderImpl *) This;
705 ps_impl->numbumpenvmatconsts = 0;
706 for(i = 0; i < (sizeof(reg_maps->bumpmat) / sizeof(reg_maps->bumpmat[0])); i++) {
707 if(!reg_maps->bumpmat[i]) {
708 continue;
711 ps_impl->bumpenvmatconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
712 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
714 if(reg_maps->luminanceparams) {
715 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = i;
716 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
717 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
718 extra_constants_needed++;
719 } else {
720 ps_impl->luminanceconst[(int) ps_impl->numbumpenvmatconsts].texunit = -1;
723 extra_constants_needed++;
724 ps_impl->numbumpenvmatconsts++;
727 if(ps_args->srgb_correction) {
728 shader_addline(buffer, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
729 srgb_mul_low, srgb_mul_low, srgb_mul_low, srgb_mul_low);
730 shader_addline(buffer, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
731 srgb_cmp, srgb_cmp, srgb_cmp, srgb_cmp);
733 if(reg_maps->vpos || reg_maps->usesdsy) {
734 if(This->baseShader.limits.constant_float + extra_constants_needed + 1 < GL_LIMITS(pshader_constantsF)) {
735 shader_addline(buffer, "uniform vec4 ycorrection;\n");
736 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
737 extra_constants_needed++;
738 } else {
739 /* This happens because we do not have proper tracking of the constant registers that are
740 * actually used, only the max limit of the shader version
742 FIXME("Cannot find a free uniform for vpos correction params\n");
743 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
744 device->render_offscreen ? 0.0 : ((IWineD3DSurfaceImpl *) device->render_targets[0])->currentDesc.Height,
745 device->render_offscreen ? 1.0 : -1.0);
747 shader_addline(buffer, "vec4 vpos;\n");
751 /* Declare texture samplers */
752 for (i = 0; i < This->baseShader.limits.sampler; i++) {
753 if (reg_maps->samplers[i]) {
755 DWORD stype = reg_maps->samplers[i] & WINED3DSP_TEXTURETYPE_MASK;
756 switch (stype) {
758 case WINED3DSTT_1D:
759 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
760 break;
761 case WINED3DSTT_2D:
762 if(device->stateBlock->textures[i] &&
763 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
764 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
765 } else {
766 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
768 break;
769 case WINED3DSTT_CUBE:
770 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
771 break;
772 case WINED3DSTT_VOLUME:
773 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
774 break;
775 default:
776 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
777 FIXME("Unrecognized sampler type: %#x\n", stype);
778 break;
783 /* Declare address variables */
784 for (i = 0; i < This->baseShader.limits.address; i++) {
785 if (reg_maps->address[i])
786 shader_addline(buffer, "ivec4 A%d;\n", i);
789 /* Declare texture coordinate temporaries and initialize them */
790 for (i = 0; i < This->baseShader.limits.texcoord; i++) {
791 if (reg_maps->texcoord[i])
792 shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
795 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
796 * helper function shader that is linked in at link time
798 if (pshader && shader_version >= WINED3DPS_VERSION(3, 0))
800 if(use_vs(device)) {
801 shader_addline(buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
802 } else {
803 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
804 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
805 * pixel shader that reads the fixed function color into the packed input registers.
807 shader_addline(buffer, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
811 /* Declare output register temporaries */
812 if(This->baseShader.limits.packed_output) {
813 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
816 /* Declare temporary variables */
817 for(i = 0; i < This->baseShader.limits.temporary; i++) {
818 if (reg_maps->temporary[i])
819 shader_addline(buffer, "vec4 R%u;\n", i);
822 /* Declare attributes */
823 for (i = 0; i < This->baseShader.limits.attributes; i++) {
824 if (reg_maps->attributes[i])
825 shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
828 /* Declare loop registers aLx */
829 for (i = 0; i < reg_maps->loop_depth; i++) {
830 shader_addline(buffer, "int aL%u;\n", i);
831 shader_addline(buffer, "int tmpInt%u;\n", i);
834 /* Temporary variables for matrix operations */
835 shader_addline(buffer, "vec4 tmp0;\n");
836 shader_addline(buffer, "vec4 tmp1;\n");
838 /* Local constants use a different name so they can be loaded once at shader link time
839 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
840 * float -> string conversion can cause precision loss.
842 if(!This->baseShader.load_local_constsF) {
843 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
844 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
848 /* Start the main program */
849 shader_addline(buffer, "void main() {\n");
850 if(pshader && reg_maps->vpos) {
851 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
852 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
853 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
854 * precision troubles when we just substract 0.5.
856 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
858 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
860 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
861 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
862 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
863 * correctly on drivers that returns integer values.
865 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
869 /*****************************************************************************
870 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
872 * For more information, see http://wiki.winehq.org/DirectX-Shaders
873 ****************************************************************************/
875 /* Prototypes */
876 static void shader_glsl_add_src_param(const SHADER_OPCODE_ARG *arg, const DWORD param,
877 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param);
879 /** Used for opcode modifiers - They multiply the result by the specified amount */
880 static const char * const shift_glsl_tab[] = {
881 "", /* 0 (none) */
882 "2.0 * ", /* 1 (x2) */
883 "4.0 * ", /* 2 (x4) */
884 "8.0 * ", /* 3 (x8) */
885 "16.0 * ", /* 4 (x16) */
886 "32.0 * ", /* 5 (x32) */
887 "", /* 6 (x64) */
888 "", /* 7 (x128) */
889 "", /* 8 (d256) */
890 "", /* 9 (d128) */
891 "", /* 10 (d64) */
892 "", /* 11 (d32) */
893 "0.0625 * ", /* 12 (d16) */
894 "0.125 * ", /* 13 (d8) */
895 "0.25 * ", /* 14 (d4) */
896 "0.5 * " /* 15 (d2) */
899 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
900 static void shader_glsl_gen_modifier (
901 const DWORD instr,
902 const char *in_reg,
903 const char *in_regswizzle,
904 char *out_str) {
906 out_str[0] = 0;
908 if (instr == WINED3DSIO_TEXKILL)
909 return;
911 switch (instr & WINED3DSP_SRCMOD_MASK) {
912 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
913 case WINED3DSPSM_DW:
914 case WINED3DSPSM_NONE:
915 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
916 break;
917 case WINED3DSPSM_NEG:
918 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
919 break;
920 case WINED3DSPSM_NOT:
921 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
922 break;
923 case WINED3DSPSM_BIAS:
924 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
925 break;
926 case WINED3DSPSM_BIASNEG:
927 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
928 break;
929 case WINED3DSPSM_SIGN:
930 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
931 break;
932 case WINED3DSPSM_SIGNNEG:
933 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
934 break;
935 case WINED3DSPSM_COMP:
936 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
937 break;
938 case WINED3DSPSM_X2:
939 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
940 break;
941 case WINED3DSPSM_X2NEG:
942 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
943 break;
944 case WINED3DSPSM_ABS:
945 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
946 break;
947 case WINED3DSPSM_ABSNEG:
948 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
949 break;
950 default:
951 FIXME("Unhandled modifier %u\n", (instr & WINED3DSP_SRCMOD_MASK));
952 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
956 /** Writes the GLSL variable name that corresponds to the register that the
957 * DX opcode parameter is trying to access */
958 static void shader_glsl_get_register_name(const DWORD param, const DWORD addr_token,
959 char *regstr, BOOL *is_color, const SHADER_OPCODE_ARG *arg)
961 /* oPos, oFog and oPts in D3D */
962 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
964 DWORD reg = param & WINED3DSP_REGNUM_MASK;
965 DWORD regtype = shader_get_regtype(param);
966 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) arg->shader;
967 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
968 const WineD3D_GL_Info* gl_info = &deviceImpl->adapter->gl_info;
969 DWORD shader_version = This->baseShader.reg_maps.shader_version;
970 char pshader = shader_is_pshader_version(shader_version);
971 char tmpStr[150];
973 *is_color = FALSE;
975 switch (regtype) {
976 case WINED3DSPR_TEMP:
977 sprintf(tmpStr, "R%u", reg);
978 break;
979 case WINED3DSPR_INPUT:
980 if (pshader) {
981 /* Pixel shaders >= 3.0 */
982 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3)
984 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
986 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
987 glsl_src_param_t rel_param;
988 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
990 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
991 * operation there
993 if(((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg]) {
994 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
995 sprintf(tmpStr, "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
996 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count - 1,
997 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg], in_count,
998 rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
999 } else {
1000 sprintf(tmpStr, "IN[%s + %u]", rel_param.param_str, ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg]);
1002 } else {
1003 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count) {
1004 sprintf(tmpStr, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1005 rel_param.param_str, in_count - 1,
1006 rel_param.param_str, in_count,
1007 rel_param.param_str);
1008 } else {
1009 sprintf(tmpStr, "IN[%s]", rel_param.param_str);
1012 } else {
1013 DWORD idx = ((IWineD3DPixelShaderImpl *) This)->input_reg_map[reg];
1014 if (idx == in_count) {
1015 sprintf(tmpStr, "gl_Color");
1016 } else if (idx == in_count + 1) {
1017 sprintf(tmpStr, "gl_SecondaryColor");
1018 } else {
1019 sprintf(tmpStr, "IN[%u]", idx);
1022 } else {
1023 if (reg==0)
1024 strcpy(tmpStr, "gl_Color");
1025 else
1026 strcpy(tmpStr, "gl_SecondaryColor");
1028 } else {
1029 if (vshader_input_is_color((IWineD3DVertexShader*) This, reg))
1030 *is_color = TRUE;
1031 sprintf(tmpStr, "attrib%u", reg);
1033 break;
1034 case WINED3DSPR_CONST:
1036 const char prefix = pshader? 'P':'V';
1038 /* Relative addressing */
1039 if (param & WINED3DSHADER_ADDRMODE_RELATIVE) {
1041 /* Relative addressing on shaders 2.0+ have a relative address token,
1042 * prior to that, it was hard-coded as "A0.x" because there's only 1 register */
1043 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 2)
1045 glsl_src_param_t rel_param;
1046 shader_glsl_add_src_param(arg, addr_token, 0, WINED3DSP_WRITEMASK_0, &rel_param);
1047 if(reg) {
1048 sprintf(tmpStr, "%cC[%s + %u]", prefix, rel_param.param_str, reg);
1049 } else {
1050 sprintf(tmpStr, "%cC[%s]", prefix, rel_param.param_str);
1052 } else {
1053 if(reg) {
1054 sprintf(tmpStr, "%cC[A0.x + %u]", prefix, reg);
1055 } else {
1056 sprintf(tmpStr, "%cC[A0.x]", prefix);
1060 } else {
1061 if(shader_constant_is_local(This, reg)) {
1062 sprintf(tmpStr, "%cLC%u", prefix, reg);
1063 } else {
1064 sprintf(tmpStr, "%cC[%u]", prefix, reg);
1068 break;
1070 case WINED3DSPR_CONSTINT:
1071 if (pshader)
1072 sprintf(tmpStr, "PI[%u]", reg);
1073 else
1074 sprintf(tmpStr, "VI[%u]", reg);
1075 break;
1076 case WINED3DSPR_CONSTBOOL:
1077 if (pshader)
1078 sprintf(tmpStr, "PB[%u]", reg);
1079 else
1080 sprintf(tmpStr, "VB[%u]", reg);
1081 break;
1082 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1083 if (pshader) {
1084 sprintf(tmpStr, "T%u", reg);
1085 } else {
1086 sprintf(tmpStr, "A%u", reg);
1088 break;
1089 case WINED3DSPR_LOOP:
1090 sprintf(tmpStr, "aL%u", This->baseShader.cur_loop_regno - 1);
1091 break;
1092 case WINED3DSPR_SAMPLER:
1093 if (pshader)
1094 sprintf(tmpStr, "Psampler%u", reg);
1095 else
1096 sprintf(tmpStr, "Vsampler%u", reg);
1097 break;
1098 case WINED3DSPR_COLOROUT:
1099 if (reg >= GL_LIMITS(buffers)) {
1100 WARN("Write to render target %u, only %d supported\n", reg, 4);
1102 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
1103 sprintf(tmpStr, "gl_FragData[%u]", reg);
1104 } else { /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
1105 sprintf(tmpStr, "gl_FragColor");
1107 break;
1108 case WINED3DSPR_RASTOUT:
1109 sprintf(tmpStr, "%s", hwrastout_reg_names[reg]);
1110 break;
1111 case WINED3DSPR_DEPTHOUT:
1112 sprintf(tmpStr, "gl_FragDepth");
1113 break;
1114 case WINED3DSPR_ATTROUT:
1115 if (reg == 0) {
1116 sprintf(tmpStr, "gl_FrontColor");
1117 } else {
1118 sprintf(tmpStr, "gl_FrontSecondaryColor");
1120 break;
1121 case WINED3DSPR_TEXCRDOUT:
1122 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1123 if (WINED3DSHADER_VERSION_MAJOR(shader_version) >= 3) sprintf(tmpStr, "OUT[%u]", reg);
1124 else sprintf(tmpStr, "gl_TexCoord[%u]", reg);
1125 break;
1126 case WINED3DSPR_MISCTYPE:
1127 if (reg == 0) {
1128 /* vPos */
1129 sprintf(tmpStr, "vpos");
1130 } else if (reg == 1){
1131 /* Note that gl_FrontFacing is a bool, while vFace is
1132 * a float for which the sign determines front/back
1134 sprintf(tmpStr, "(gl_FrontFacing ? 1.0 : -1.0)");
1135 } else {
1136 FIXME("Unhandled misctype register %d\n", reg);
1137 sprintf(tmpStr, "unrecognized_register");
1139 break;
1140 default:
1141 FIXME("Unhandled register name Type(%d)\n", regtype);
1142 sprintf(tmpStr, "unrecognized_register");
1143 break;
1146 strcat(regstr, tmpStr);
1149 /* Get the GLSL write mask for the destination register */
1150 static DWORD shader_glsl_get_write_mask(const DWORD param, char *write_mask) {
1151 char *ptr = write_mask;
1152 DWORD mask = param & WINED3DSP_WRITEMASK_ALL;
1154 if (shader_is_scalar(param)) {
1155 mask = WINED3DSP_WRITEMASK_0;
1156 } else {
1157 *ptr++ = '.';
1158 if (param & WINED3DSP_WRITEMASK_0) *ptr++ = 'x';
1159 if (param & WINED3DSP_WRITEMASK_1) *ptr++ = 'y';
1160 if (param & WINED3DSP_WRITEMASK_2) *ptr++ = 'z';
1161 if (param & WINED3DSP_WRITEMASK_3) *ptr++ = 'w';
1164 *ptr = '\0';
1166 return mask;
1169 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1170 unsigned int size = 0;
1172 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1173 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1174 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1175 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1177 return size;
1180 static void shader_glsl_get_swizzle(const DWORD param, BOOL fixup, DWORD mask, char *swizzle_str) {
1181 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1182 * but addressed as "rgba". To fix this we need to swap the register's x
1183 * and z components. */
1184 DWORD swizzle = (param & WINED3DSP_SWIZZLE_MASK) >> WINED3DSP_SWIZZLE_SHIFT;
1185 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1186 char *ptr = swizzle_str;
1188 if (!shader_is_scalar(param)) {
1189 *ptr++ = '.';
1190 /* swizzle bits fields: wwzzyyxx */
1191 if (mask & WINED3DSP_WRITEMASK_0) *ptr++ = swizzle_chars[swizzle & 0x03];
1192 if (mask & WINED3DSP_WRITEMASK_1) *ptr++ = swizzle_chars[(swizzle >> 2) & 0x03];
1193 if (mask & WINED3DSP_WRITEMASK_2) *ptr++ = swizzle_chars[(swizzle >> 4) & 0x03];
1194 if (mask & WINED3DSP_WRITEMASK_3) *ptr++ = swizzle_chars[(swizzle >> 6) & 0x03];
1197 *ptr = '\0';
1200 /* From a given parameter token, generate the corresponding GLSL string.
1201 * Also, return the actual register name and swizzle in case the
1202 * caller needs this information as well. */
1203 static void shader_glsl_add_src_param(const SHADER_OPCODE_ARG *arg, const DWORD param,
1204 const DWORD addr_token, DWORD mask, glsl_src_param_t *src_param)
1206 BOOL is_color = FALSE;
1207 char swizzle_str[6];
1209 src_param->reg_name[0] = '\0';
1210 src_param->param_str[0] = '\0';
1211 swizzle_str[0] = '\0';
1213 shader_glsl_get_register_name(param, addr_token, src_param->reg_name, &is_color, arg);
1215 shader_glsl_get_swizzle(param, is_color, mask, swizzle_str);
1216 shader_glsl_gen_modifier(param, src_param->reg_name, swizzle_str, src_param->param_str);
1219 /* From a given parameter token, generate the corresponding GLSL string.
1220 * Also, return the actual register name and swizzle in case the
1221 * caller needs this information as well. */
1222 static DWORD shader_glsl_add_dst_param(const SHADER_OPCODE_ARG* arg, const DWORD param,
1223 const DWORD addr_token, glsl_dst_param_t *dst_param)
1225 BOOL is_color = FALSE;
1227 dst_param->mask_str[0] = '\0';
1228 dst_param->reg_name[0] = '\0';
1230 shader_glsl_get_register_name(param, addr_token, dst_param->reg_name, &is_color, arg);
1231 return shader_glsl_get_write_mask(param, dst_param->mask_str);
1234 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1235 static DWORD shader_glsl_append_dst_ext(SHADER_BUFFER *buffer, const SHADER_OPCODE_ARG *arg, const DWORD param)
1237 glsl_dst_param_t dst_param;
1238 DWORD mask;
1239 int shift;
1241 mask = shader_glsl_add_dst_param(arg, param, arg->dst_addr, &dst_param);
1243 if(mask) {
1244 shift = (param & WINED3DSP_DSTSHIFT_MASK) >> WINED3DSP_DSTSHIFT_SHIFT;
1245 shader_addline(buffer, "%s%s = %s(", dst_param.reg_name, dst_param.mask_str, shift_glsl_tab[shift]);
1248 return mask;
1251 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1252 static DWORD shader_glsl_append_dst(SHADER_BUFFER *buffer, const SHADER_OPCODE_ARG *arg)
1254 return shader_glsl_append_dst_ext(buffer, arg, arg->dst);
1257 /** Process GLSL instruction modifiers */
1258 void shader_glsl_add_instruction_modifiers(const SHADER_OPCODE_ARG* arg)
1260 DWORD mask = arg->dst & WINED3DSP_DSTMOD_MASK;
1262 if (arg->opcode->dst_token && mask != 0) {
1263 glsl_dst_param_t dst_param;
1265 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
1267 if (mask & WINED3DSPDM_SATURATE) {
1268 /* _SAT means to clamp the value of the register to between 0 and 1 */
1269 shader_addline(arg->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1270 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1272 if (mask & WINED3DSPDM_MSAMPCENTROID) {
1273 FIXME("_centroid modifier not handled\n");
1275 if (mask & WINED3DSPDM_PARTIALPRECISION) {
1276 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1281 static inline const char* shader_get_comp_op(
1282 const DWORD opcode) {
1284 DWORD op = (opcode & INST_CONTROLS_MASK) >> INST_CONTROLS_SHIFT;
1285 switch (op) {
1286 case COMPARISON_GT: return ">";
1287 case COMPARISON_EQ: return "==";
1288 case COMPARISON_GE: return ">=";
1289 case COMPARISON_LT: return "<";
1290 case COMPARISON_NE: return "!=";
1291 case COMPARISON_LE: return "<=";
1292 default:
1293 FIXME("Unrecognized comparison value: %u\n", op);
1294 return "(\?\?)";
1298 static void shader_glsl_get_sample_function(DWORD sampler_type, BOOL projected, BOOL texrect, glsl_sample_function_t *sample_function) {
1299 /* Note that there's no such thing as a projected cube texture. */
1300 switch(sampler_type) {
1301 case WINED3DSTT_1D:
1302 sample_function->name = projected ? "texture1DProj" : "texture1D";
1303 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1304 break;
1305 case WINED3DSTT_2D:
1306 if(texrect) {
1307 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1308 } else {
1309 sample_function->name = projected ? "texture2DProj" : "texture2D";
1311 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1312 break;
1313 case WINED3DSTT_CUBE:
1314 sample_function->name = "textureCube";
1315 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1316 break;
1317 case WINED3DSTT_VOLUME:
1318 sample_function->name = projected ? "texture3DProj" : "texture3D";
1319 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1320 break;
1321 default:
1322 sample_function->name = "";
1323 sample_function->coord_mask = 0;
1324 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1325 break;
1329 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1330 BOOL sign_fixup, enum fixup_channel_source channel_source)
1332 switch(channel_source)
1334 case CHANNEL_SOURCE_ZERO:
1335 strcat(arguments, "0.0");
1336 break;
1338 case CHANNEL_SOURCE_ONE:
1339 strcat(arguments, "1.0");
1340 break;
1342 case CHANNEL_SOURCE_X:
1343 strcat(arguments, reg_name);
1344 strcat(arguments, ".x");
1345 break;
1347 case CHANNEL_SOURCE_Y:
1348 strcat(arguments, reg_name);
1349 strcat(arguments, ".y");
1350 break;
1352 case CHANNEL_SOURCE_Z:
1353 strcat(arguments, reg_name);
1354 strcat(arguments, ".z");
1355 break;
1357 case CHANNEL_SOURCE_W:
1358 strcat(arguments, reg_name);
1359 strcat(arguments, ".w");
1360 break;
1362 default:
1363 FIXME("Unhandled channel source %#x\n", channel_source);
1364 strcat(arguments, "undefined");
1365 break;
1368 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1371 static void shader_glsl_color_correction(const struct SHADER_OPCODE_ARG *arg, struct color_fixup_desc fixup)
1373 unsigned int mask_size, remaining;
1374 glsl_dst_param_t dst_param;
1375 char arguments[256];
1376 DWORD mask;
1377 BOOL dummy;
1379 mask = 0;
1380 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1381 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1382 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1383 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1384 mask &= arg->dst;
1386 if (!mask) return; /* Nothing to do */
1388 if (is_yuv_fixup(fixup))
1390 enum yuv_fixup yuv_fixup = get_yuv_fixup(fixup);
1391 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup);
1392 return;
1395 mask_size = shader_glsl_get_write_mask_size(mask);
1397 dst_param.mask_str[0] = '\0';
1398 shader_glsl_get_write_mask(mask, dst_param.mask_str);
1400 dst_param.reg_name[0] = '\0';
1401 shader_glsl_get_register_name(arg->dst, arg->dst_addr, dst_param.reg_name, &dummy, arg);
1403 arguments[0] = '\0';
1404 remaining = mask_size;
1405 if (mask & WINED3DSP_WRITEMASK_0)
1407 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1408 if (--remaining) strcat(arguments, ", ");
1410 if (mask & WINED3DSP_WRITEMASK_1)
1412 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1413 if (--remaining) strcat(arguments, ", ");
1415 if (mask & WINED3DSP_WRITEMASK_2)
1417 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1418 if (--remaining) strcat(arguments, ", ");
1420 if (mask & WINED3DSP_WRITEMASK_3)
1422 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1423 if (--remaining) strcat(arguments, ", ");
1426 if (mask_size > 1)
1428 shader_addline(arg->buffer, "%s%s = vec%u(%s);\n",
1429 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1431 else
1433 shader_addline(arg->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
1437 /*****************************************************************************
1439 * Begin processing individual instruction opcodes
1441 ****************************************************************************/
1443 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1444 static void shader_glsl_arith(const SHADER_OPCODE_ARG *arg)
1446 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1447 SHADER_BUFFER* buffer = arg->buffer;
1448 glsl_src_param_t src0_param;
1449 glsl_src_param_t src1_param;
1450 DWORD write_mask;
1451 char op;
1453 /* Determine the GLSL operator to use based on the opcode */
1454 switch (curOpcode->opcode) {
1455 case WINED3DSIO_MUL: op = '*'; break;
1456 case WINED3DSIO_ADD: op = '+'; break;
1457 case WINED3DSIO_SUB: op = '-'; break;
1458 default:
1459 op = ' ';
1460 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1461 break;
1464 write_mask = shader_glsl_append_dst(buffer, arg);
1465 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1466 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1467 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1470 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1471 static void shader_glsl_mov(const SHADER_OPCODE_ARG *arg)
1473 SHADER_BUFFER* buffer = arg->buffer;
1474 glsl_src_param_t src0_param;
1475 DWORD write_mask;
1477 write_mask = shader_glsl_append_dst(buffer, arg);
1478 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1480 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1481 * shader versions WINED3DSIO_MOVA is used for this. */
1482 if ((WINED3DSHADER_VERSION_MAJOR(arg->reg_maps->shader_version) == 1
1483 && !shader_is_pshader_version(arg->reg_maps->shader_version)
1484 && shader_get_regtype(arg->dst) == WINED3DSPR_ADDR))
1486 /* This is a simple floor() */
1487 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1488 if (mask_size > 1) {
1489 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
1490 } else {
1491 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
1493 } else if(arg->opcode->opcode == WINED3DSIO_MOVA) {
1494 /* We need to *round* to the nearest int here. */
1495 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1496 if (mask_size > 1) {
1497 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n", mask_size, src0_param.param_str, mask_size, src0_param.param_str);
1498 } else {
1499 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param.param_str, src0_param.param_str);
1501 } else {
1502 shader_addline(buffer, "%s);\n", src0_param.param_str);
1506 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1507 static void shader_glsl_dot(const SHADER_OPCODE_ARG *arg)
1509 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1510 SHADER_BUFFER* buffer = arg->buffer;
1511 glsl_src_param_t src0_param;
1512 glsl_src_param_t src1_param;
1513 DWORD dst_write_mask, src_write_mask;
1514 unsigned int dst_size = 0;
1516 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1517 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1519 /* dp3 works on vec3, dp4 on vec4 */
1520 if (curOpcode->opcode == WINED3DSIO_DP4) {
1521 src_write_mask = WINED3DSP_WRITEMASK_ALL;
1522 } else {
1523 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1526 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_write_mask, &src0_param);
1527 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_write_mask, &src1_param);
1529 if (dst_size > 1) {
1530 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1531 } else {
1532 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
1536 /* Note that this instruction has some restrictions. The destination write mask
1537 * can't contain the w component, and the source swizzles have to be .xyzw */
1538 static void shader_glsl_cross(const SHADER_OPCODE_ARG *arg)
1540 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1541 glsl_src_param_t src0_param;
1542 glsl_src_param_t src1_param;
1543 char dst_mask[6];
1545 shader_glsl_get_write_mask(arg->dst, dst_mask);
1546 shader_glsl_append_dst(arg->buffer, arg);
1547 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
1548 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
1549 shader_addline(arg->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
1552 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1553 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1554 * GLSL uses the value as-is. */
1555 static void shader_glsl_pow(const SHADER_OPCODE_ARG *arg)
1557 SHADER_BUFFER *buffer = arg->buffer;
1558 glsl_src_param_t src0_param;
1559 glsl_src_param_t src1_param;
1560 DWORD dst_write_mask;
1561 unsigned int dst_size;
1563 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1564 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1566 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1567 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
1569 if (dst_size > 1) {
1570 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
1571 } else {
1572 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
1576 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1577 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1578 * GLSL uses the value as-is. */
1579 static void shader_glsl_log(const SHADER_OPCODE_ARG *arg)
1581 SHADER_BUFFER *buffer = arg->buffer;
1582 glsl_src_param_t src0_param;
1583 DWORD dst_write_mask;
1584 unsigned int dst_size;
1586 dst_write_mask = shader_glsl_append_dst(buffer, arg);
1587 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
1589 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1591 if (dst_size > 1) {
1592 shader_addline(buffer, "vec%d(log2(abs(%s))));\n", dst_size, src0_param.param_str);
1593 } else {
1594 shader_addline(buffer, "log2(abs(%s)));\n", src0_param.param_str);
1598 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1599 static void shader_glsl_map2gl(const SHADER_OPCODE_ARG *arg)
1601 CONST SHADER_OPCODE* curOpcode = arg->opcode;
1602 SHADER_BUFFER* buffer = arg->buffer;
1603 glsl_src_param_t src_param;
1604 const char *instruction;
1605 char arguments[256];
1606 DWORD write_mask;
1607 unsigned i;
1609 /* Determine the GLSL function to use based on the opcode */
1610 /* TODO: Possibly make this a table for faster lookups */
1611 switch (curOpcode->opcode) {
1612 case WINED3DSIO_MIN: instruction = "min"; break;
1613 case WINED3DSIO_MAX: instruction = "max"; break;
1614 case WINED3DSIO_ABS: instruction = "abs"; break;
1615 case WINED3DSIO_FRC: instruction = "fract"; break;
1616 case WINED3DSIO_NRM: instruction = "normalize"; break;
1617 case WINED3DSIO_EXP: instruction = "exp2"; break;
1618 case WINED3DSIO_SGN: instruction = "sign"; break;
1619 case WINED3DSIO_DSX: instruction = "dFdx"; break;
1620 case WINED3DSIO_DSY: instruction = "ycorrection.y * dFdy"; break;
1621 default: instruction = "";
1622 FIXME("Opcode %s not yet handled in GLSL\n", curOpcode->name);
1623 break;
1626 write_mask = shader_glsl_append_dst(buffer, arg);
1628 arguments[0] = '\0';
1629 if (curOpcode->num_params > 0) {
1630 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src_param);
1631 strcat(arguments, src_param.param_str);
1632 for (i = 2; i < curOpcode->num_params; ++i) {
1633 strcat(arguments, ", ");
1634 shader_glsl_add_src_param(arg, arg->src[i-1], arg->src_addr[i-1], write_mask, &src_param);
1635 strcat(arguments, src_param.param_str);
1639 shader_addline(buffer, "%s(%s));\n", instruction, arguments);
1642 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1643 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1644 * dst.x = 2^(floor(src))
1645 * dst.y = src - floor(src)
1646 * dst.z = 2^src (partial precision is allowed, but optional)
1647 * dst.w = 1.0;
1648 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1649 * dst = 2^src; (partial precision is allowed, but optional)
1651 static void shader_glsl_expp(const SHADER_OPCODE_ARG *arg)
1653 glsl_src_param_t src_param;
1655 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src_param);
1657 if (arg->reg_maps->shader_version < WINED3DPS_VERSION(2,0))
1659 char dst_mask[6];
1661 shader_addline(arg->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
1662 shader_addline(arg->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
1663 shader_addline(arg->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
1664 shader_addline(arg->buffer, "tmp0.w = 1.0;\n");
1666 shader_glsl_append_dst(arg->buffer, arg);
1667 shader_glsl_get_write_mask(arg->dst, dst_mask);
1668 shader_addline(arg->buffer, "tmp0%s);\n", dst_mask);
1669 } else {
1670 DWORD write_mask;
1671 unsigned int mask_size;
1673 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1674 mask_size = shader_glsl_get_write_mask_size(write_mask);
1676 if (mask_size > 1) {
1677 shader_addline(arg->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
1678 } else {
1679 shader_addline(arg->buffer, "exp2(%s));\n", src_param.param_str);
1684 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1685 static void shader_glsl_rcp(const SHADER_OPCODE_ARG *arg)
1687 glsl_src_param_t src_param;
1688 DWORD write_mask;
1689 unsigned int mask_size;
1691 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1692 mask_size = shader_glsl_get_write_mask_size(write_mask);
1693 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1695 if (mask_size > 1) {
1696 shader_addline(arg->buffer, "vec%d(1.0 / %s));\n", mask_size, src_param.param_str);
1697 } else {
1698 shader_addline(arg->buffer, "1.0 / %s);\n", src_param.param_str);
1702 static void shader_glsl_rsq(const SHADER_OPCODE_ARG *arg)
1704 SHADER_BUFFER* buffer = arg->buffer;
1705 glsl_src_param_t src_param;
1706 DWORD write_mask;
1707 unsigned int mask_size;
1709 write_mask = shader_glsl_append_dst(buffer, arg);
1710 mask_size = shader_glsl_get_write_mask_size(write_mask);
1712 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src_param);
1714 if (mask_size > 1) {
1715 shader_addline(buffer, "vec%d(inversesqrt(%s)));\n", mask_size, src_param.param_str);
1716 } else {
1717 shader_addline(buffer, "inversesqrt(%s));\n", src_param.param_str);
1721 /** Process signed comparison opcodes in GLSL. */
1722 static void shader_glsl_compare(const SHADER_OPCODE_ARG *arg)
1724 glsl_src_param_t src0_param;
1725 glsl_src_param_t src1_param;
1726 DWORD write_mask;
1727 unsigned int mask_size;
1729 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1730 mask_size = shader_glsl_get_write_mask_size(write_mask);
1731 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1732 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1734 if (mask_size > 1) {
1735 const char *compare;
1737 switch(arg->opcode->opcode) {
1738 case WINED3DSIO_SLT: compare = "lessThan"; break;
1739 case WINED3DSIO_SGE: compare = "greaterThanEqual"; break;
1740 default: compare = "";
1741 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1744 shader_addline(arg->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
1745 src0_param.param_str, src1_param.param_str);
1746 } else {
1747 switch(arg->opcode->opcode) {
1748 case WINED3DSIO_SLT:
1749 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1750 * to return 0.0 but step returns 1.0 because step is not < x
1751 * An alternative is a bvec compare padded with an unused second component.
1752 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1753 * issue. Playing with not() is not possible either because not() does not accept
1754 * a scalar.
1756 shader_addline(arg->buffer, "(%s < %s) ? 1.0 : 0.0);\n", src0_param.param_str, src1_param.param_str);
1757 break;
1758 case WINED3DSIO_SGE:
1759 /* Here we can use the step() function and safe a conditional */
1760 shader_addline(arg->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
1761 break;
1762 default:
1763 FIXME("Can't handle opcode %s\n", arg->opcode->name);
1769 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1770 static void shader_glsl_cmp(const SHADER_OPCODE_ARG *arg)
1772 glsl_src_param_t src0_param;
1773 glsl_src_param_t src1_param;
1774 glsl_src_param_t src2_param;
1775 DWORD write_mask, cmp_channel = 0;
1776 unsigned int i, j;
1777 char mask_char[6];
1778 BOOL temp_destination = FALSE;
1780 if(shader_is_scalar(arg->src[0])) {
1781 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1783 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
1784 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1785 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1787 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1788 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1789 } else {
1790 DWORD src0reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
1791 DWORD src1reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
1792 DWORD src2reg = arg->src[2] & WINED3DSP_REGNUM_MASK;
1793 DWORD src0regtype = shader_get_regtype(arg->src[0]);
1794 DWORD src1regtype = shader_get_regtype(arg->src[1]);
1795 DWORD src2regtype = shader_get_regtype(arg->src[2]);
1796 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
1797 DWORD dstregtype = shader_get_regtype(arg->dst);
1799 /* Cycle through all source0 channels */
1800 for (i=0; i<4; i++) {
1801 write_mask = 0;
1802 /* Find the destination channels which use the current source0 channel */
1803 for (j=0; j<4; j++) {
1804 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1805 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1806 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1810 /* Splitting the cmp instruction up in multiple lines imposes a problem:
1811 * The first lines may overwrite source parameters of the following lines.
1812 * Deal with that by using a temporary destination register if needed
1814 if((src0reg == dstreg && src0regtype == dstregtype) ||
1815 (src1reg == dstreg && src1regtype == dstregtype) ||
1816 (src2reg == dstreg && src2regtype == dstregtype)) {
1818 write_mask = shader_glsl_get_write_mask(arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask), mask_char);
1819 if (!write_mask) continue;
1820 shader_addline(arg->buffer, "tmp0%s = (", mask_char);
1821 temp_destination = TRUE;
1822 } else {
1823 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1824 if (!write_mask) continue;
1827 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1828 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1829 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1831 shader_addline(arg->buffer, "%s >= 0.0 ? %s : %s);\n",
1832 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1835 if(temp_destination) {
1836 shader_glsl_get_write_mask(arg->dst, mask_char);
1837 shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst);
1838 shader_addline(arg->buffer, "tmp0%s);\n", mask_char);
1844 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
1845 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
1846 * the compare is done per component of src0. */
1847 static void shader_glsl_cnd(const SHADER_OPCODE_ARG *arg)
1849 glsl_src_param_t src0_param;
1850 glsl_src_param_t src1_param;
1851 glsl_src_param_t src2_param;
1852 DWORD write_mask, cmp_channel = 0;
1853 unsigned int i, j;
1855 if (arg->reg_maps->shader_version < WINED3DPS_VERSION(1, 4))
1857 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1858 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
1859 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1860 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1862 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
1863 if(arg->opcode_token & WINED3DSI_COISSUE) {
1864 shader_addline(arg->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
1865 } else {
1866 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1867 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1869 return;
1871 /* Cycle through all source0 channels */
1872 for (i=0; i<4; i++) {
1873 write_mask = 0;
1874 /* Find the destination channels which use the current source0 channel */
1875 for (j=0; j<4; j++) {
1876 if ( ((arg->src[0] >> (WINED3DSP_SWIZZLE_SHIFT + 2*j)) & 0x3) == i ) {
1877 write_mask |= WINED3DSP_WRITEMASK_0 << j;
1878 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
1881 write_mask = shader_glsl_append_dst_ext(arg->buffer, arg, arg->dst & (~WINED3DSP_SWIZZLE_MASK | write_mask));
1882 if (!write_mask) continue;
1884 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], cmp_channel, &src0_param);
1885 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1886 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1888 shader_addline(arg->buffer, "%s > 0.5 ? %s : %s);\n",
1889 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1893 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
1894 static void shader_glsl_mad(const SHADER_OPCODE_ARG *arg)
1896 glsl_src_param_t src0_param;
1897 glsl_src_param_t src1_param;
1898 glsl_src_param_t src2_param;
1899 DWORD write_mask;
1901 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1902 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1903 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1904 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1905 shader_addline(arg->buffer, "(%s * %s) + %s);\n",
1906 src0_param.param_str, src1_param.param_str, src2_param.param_str);
1909 /** Handles transforming all WINED3DSIO_M?x? opcodes for
1910 Vertex shaders to GLSL codes */
1911 static void shader_glsl_mnxn(const SHADER_OPCODE_ARG *arg)
1913 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)arg->shader;
1914 const SHADER_OPCODE *opcode_table = shader->baseShader.shader_ins;
1915 DWORD shader_version = arg->reg_maps->shader_version;
1916 int i;
1917 int nComponents = 0;
1918 SHADER_OPCODE_ARG tmpArg;
1920 memset(&tmpArg, 0, sizeof(SHADER_OPCODE_ARG));
1922 /* Set constants for the temporary argument */
1923 tmpArg.shader = arg->shader;
1924 tmpArg.buffer = arg->buffer;
1925 tmpArg.src[0] = arg->src[0];
1926 tmpArg.src_addr[0] = arg->src_addr[0];
1927 tmpArg.src_addr[1] = arg->src_addr[1];
1928 tmpArg.reg_maps = arg->reg_maps;
1930 switch(arg->opcode->opcode) {
1931 case WINED3DSIO_M4x4:
1932 nComponents = 4;
1933 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1934 break;
1935 case WINED3DSIO_M4x3:
1936 nComponents = 3;
1937 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP4);
1938 break;
1939 case WINED3DSIO_M3x4:
1940 nComponents = 4;
1941 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1942 break;
1943 case WINED3DSIO_M3x3:
1944 nComponents = 3;
1945 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1946 break;
1947 case WINED3DSIO_M3x2:
1948 nComponents = 2;
1949 tmpArg.opcode = shader_get_opcode(opcode_table, shader_version, WINED3DSIO_DP3);
1950 break;
1951 default:
1952 break;
1955 for (i = 0; i < nComponents; i++) {
1956 tmpArg.dst = ((arg->dst) & ~WINED3DSP_WRITEMASK_ALL)|(WINED3DSP_WRITEMASK_0<<i);
1957 tmpArg.src[1] = arg->src[1]+i;
1958 shader_glsl_dot(&tmpArg);
1963 The LRP instruction performs a component-wise linear interpolation
1964 between the second and third operands using the first operand as the
1965 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
1966 This is equivalent to mix(src2, src1, src0);
1968 static void shader_glsl_lrp(const SHADER_OPCODE_ARG *arg)
1970 glsl_src_param_t src0_param;
1971 glsl_src_param_t src1_param;
1972 glsl_src_param_t src2_param;
1973 DWORD write_mask;
1975 write_mask = shader_glsl_append_dst(arg->buffer, arg);
1977 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], write_mask, &src0_param);
1978 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], write_mask, &src1_param);
1979 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], write_mask, &src2_param);
1981 shader_addline(arg->buffer, "mix(%s, %s, %s));\n",
1982 src2_param.param_str, src1_param.param_str, src0_param.param_str);
1985 /** Process the WINED3DSIO_LIT instruction in GLSL:
1986 * dst.x = dst.w = 1.0
1987 * dst.y = (src0.x > 0) ? src0.x
1988 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
1989 * where src.w is clamped at +- 128
1991 static void shader_glsl_lit(const SHADER_OPCODE_ARG *arg)
1993 glsl_src_param_t src0_param;
1994 glsl_src_param_t src1_param;
1995 glsl_src_param_t src3_param;
1996 char dst_mask[6];
1998 shader_glsl_append_dst(arg->buffer, arg);
1999 shader_glsl_get_write_mask(arg->dst, dst_mask);
2001 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2002 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src1_param);
2003 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &src3_param);
2005 /* The sdk specifies the instruction like this
2006 * dst.x = 1.0;
2007 * if(src.x > 0.0) dst.y = src.x
2008 * else dst.y = 0.0.
2009 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2010 * else dst.z = 0.0;
2011 * dst.w = 1.0;
2013 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2014 * dst.x = 1.0 ... No further explanation needed
2015 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2016 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2017 * dst.w = 1.0. ... Nothing fancy.
2019 * So we still have one conditional in there. So do this:
2020 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2022 * step(0.0, x) will return 1 if src.x > 0.0, and 0 otherwise. So if y is 0 we get pow(0.0 * 1.0, power),
2023 * which sets dst.z to 0. If y > 0, but x = 0.0, we get pow(y * 0.0, power), which results in 0 too.
2024 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
2026 shader_addline(arg->buffer, "vec4(1.0, max(%s, 0.0), pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
2027 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
2030 /** Process the WINED3DSIO_DST instruction in GLSL:
2031 * dst.x = 1.0
2032 * dst.y = src0.x * src0.y
2033 * dst.z = src0.z
2034 * dst.w = src1.w
2036 static void shader_glsl_dst(const SHADER_OPCODE_ARG *arg)
2038 glsl_src_param_t src0y_param;
2039 glsl_src_param_t src0z_param;
2040 glsl_src_param_t src1y_param;
2041 glsl_src_param_t src1w_param;
2042 char dst_mask[6];
2044 shader_glsl_append_dst(arg->buffer, arg);
2045 shader_glsl_get_write_mask(arg->dst, dst_mask);
2047 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_1, &src0y_param);
2048 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &src0z_param);
2049 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_1, &src1y_param);
2050 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_3, &src1w_param);
2052 shader_addline(arg->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2053 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
2056 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2057 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2058 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2060 * dst.x = cos(src0.?)
2061 * dst.y = sin(src0.?)
2062 * dst.z = dst.z
2063 * dst.w = dst.w
2065 static void shader_glsl_sincos(const SHADER_OPCODE_ARG *arg)
2067 glsl_src_param_t src0_param;
2068 DWORD write_mask;
2070 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2071 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2073 switch (write_mask) {
2074 case WINED3DSP_WRITEMASK_0:
2075 shader_addline(arg->buffer, "cos(%s));\n", src0_param.param_str);
2076 break;
2078 case WINED3DSP_WRITEMASK_1:
2079 shader_addline(arg->buffer, "sin(%s));\n", src0_param.param_str);
2080 break;
2082 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2083 shader_addline(arg->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2084 break;
2086 default:
2087 ERR("Write mask should be .x, .y or .xy\n");
2088 break;
2092 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2093 * Start a for() loop where src1.y is the initial value of aL,
2094 * increment aL by src1.z for a total of src1.x iterations.
2095 * Need to use a temporary variable for this operation.
2097 /* FIXME: I don't think nested loops will work correctly this way. */
2098 static void shader_glsl_loop(const SHADER_OPCODE_ARG *arg)
2100 glsl_src_param_t src1_param;
2101 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2102 DWORD regtype = shader_get_regtype(arg->src[1]);
2103 DWORD reg = arg->src[1] & WINED3DSP_REGNUM_MASK;
2104 const DWORD *control_values = NULL;
2105 const local_constant *constant;
2107 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2109 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2110 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2111 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2112 * addressing.
2114 if(regtype == WINED3DSPR_CONSTINT) {
2115 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2116 if(constant->idx == reg) {
2117 control_values = constant->value;
2118 break;
2123 if(control_values) {
2124 if(control_values[2] > 0) {
2125 shader_addline(arg->buffer, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2126 shader->baseShader.cur_loop_depth, control_values[1],
2127 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2128 shader->baseShader.cur_loop_depth, control_values[2]);
2129 } else if(control_values[2] == 0) {
2130 shader_addline(arg->buffer, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2131 shader->baseShader.cur_loop_depth, control_values[1], shader->baseShader.cur_loop_depth,
2132 shader->baseShader.cur_loop_depth, control_values[0],
2133 shader->baseShader.cur_loop_depth);
2134 } else {
2135 shader_addline(arg->buffer, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2136 shader->baseShader.cur_loop_depth, control_values[1],
2137 shader->baseShader.cur_loop_depth, control_values[0], control_values[2], control_values[1],
2138 shader->baseShader.cur_loop_depth, control_values[2]);
2140 } else {
2141 shader_addline(arg->buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2142 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2143 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2144 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2147 shader->baseShader.cur_loop_depth++;
2148 shader->baseShader.cur_loop_regno++;
2151 static void shader_glsl_end(const SHADER_OPCODE_ARG *arg)
2153 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2155 shader_addline(arg->buffer, "}\n");
2157 if(arg->opcode->opcode == WINED3DSIO_ENDLOOP) {
2158 shader->baseShader.cur_loop_depth--;
2159 shader->baseShader.cur_loop_regno--;
2161 if(arg->opcode->opcode == WINED3DSIO_ENDREP) {
2162 shader->baseShader.cur_loop_depth--;
2166 static void shader_glsl_rep(const SHADER_OPCODE_ARG *arg)
2168 IWineD3DBaseShaderImpl* shader = (IWineD3DBaseShaderImpl*) arg->shader;
2169 glsl_src_param_t src0_param;
2171 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2172 shader_addline(arg->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2173 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2174 src0_param.param_str, shader->baseShader.cur_loop_depth);
2175 shader->baseShader.cur_loop_depth++;
2178 static void shader_glsl_if(const SHADER_OPCODE_ARG *arg)
2180 glsl_src_param_t src0_param;
2182 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2183 shader_addline(arg->buffer, "if (%s) {\n", src0_param.param_str);
2186 static void shader_glsl_ifc(const SHADER_OPCODE_ARG *arg)
2188 glsl_src_param_t src0_param;
2189 glsl_src_param_t src1_param;
2191 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2192 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2194 shader_addline(arg->buffer, "if (%s %s %s) {\n",
2195 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2198 static void shader_glsl_else(const SHADER_OPCODE_ARG *arg)
2200 shader_addline(arg->buffer, "} else {\n");
2203 static void shader_glsl_break(const SHADER_OPCODE_ARG *arg)
2205 shader_addline(arg->buffer, "break;\n");
2208 /* FIXME: According to MSDN the compare is done per component. */
2209 static void shader_glsl_breakc(const SHADER_OPCODE_ARG *arg)
2211 glsl_src_param_t src0_param;
2212 glsl_src_param_t src1_param;
2214 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0, &src0_param);
2215 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2217 shader_addline(arg->buffer, "if (%s %s %s) break;\n",
2218 src0_param.param_str, shader_get_comp_op(arg->opcode_token), src1_param.param_str);
2221 static void shader_glsl_label(const SHADER_OPCODE_ARG *arg)
2224 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2225 shader_addline(arg->buffer, "}\n");
2226 shader_addline(arg->buffer, "void subroutine%u () {\n", snum);
2229 static void shader_glsl_call(const SHADER_OPCODE_ARG *arg)
2231 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2232 shader_addline(arg->buffer, "subroutine%u();\n", snum);
2235 static void shader_glsl_callnz(const SHADER_OPCODE_ARG *arg)
2237 glsl_src_param_t src1_param;
2239 DWORD snum = (arg->src[0]) & WINED3DSP_REGNUM_MASK;
2240 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0, &src1_param);
2241 shader_addline(arg->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, snum);
2244 /*********************************************
2245 * Pixel Shader Specific Code begins here
2246 ********************************************/
2247 static void pshader_glsl_tex(const SHADER_OPCODE_ARG *arg)
2249 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2250 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2251 DWORD shader_version = arg->reg_maps->shader_version;
2252 char dst_swizzle[6];
2253 glsl_sample_function_t sample_function;
2254 DWORD sampler_type;
2255 DWORD sampler_idx;
2256 BOOL projected, texrect = FALSE;
2257 DWORD mask = 0;
2259 /* All versions have a destination register */
2260 shader_glsl_append_dst(arg->buffer, arg);
2262 /* 1.0-1.4: Use destination register as sampler source.
2263 * 2.0+: Use provided sampler source. */
2264 if (shader_version < WINED3DPS_VERSION(2,0)) sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2265 else sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2266 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2268 if (shader_version < WINED3DPS_VERSION(1,4))
2270 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2272 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2273 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2274 projected = TRUE;
2275 switch (flags & ~WINED3DTTFF_PROJECTED) {
2276 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2277 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2278 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2279 case WINED3DTTFF_COUNT4:
2280 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2282 } else {
2283 projected = FALSE;
2286 else if (shader_version < WINED3DPS_VERSION(2,0))
2288 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2290 if (src_mod == WINED3DSPSM_DZ) {
2291 projected = TRUE;
2292 mask = WINED3DSP_WRITEMASK_2;
2293 } else if (src_mod == WINED3DSPSM_DW) {
2294 projected = TRUE;
2295 mask = WINED3DSP_WRITEMASK_3;
2296 } else {
2297 projected = FALSE;
2299 } else {
2300 if(arg->opcode_token & WINED3DSI_TEXLD_PROJECT) {
2301 /* ps 2.0 texldp instruction always divides by the fourth component. */
2302 projected = TRUE;
2303 mask = WINED3DSP_WRITEMASK_3;
2304 } else {
2305 projected = FALSE;
2309 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2310 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2311 texrect = TRUE;
2314 shader_glsl_get_sample_function(sampler_type, projected, texrect, &sample_function);
2315 mask |= sample_function.coord_mask;
2317 if (shader_version < WINED3DPS_VERSION(2,0)) shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2318 else shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2320 /* 1.0-1.3: Use destination register as coordinate source.
2321 1.4+: Use provided coordinate source register. */
2322 if (shader_version < WINED3DPS_VERSION(1,4))
2324 char coord_mask[6];
2325 shader_glsl_get_write_mask(mask, coord_mask);
2326 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s)%s);\n",
2327 sample_function.name, sampler_idx, sampler_idx, coord_mask, dst_swizzle);
2328 } else {
2329 glsl_src_param_t coord_param;
2330 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], mask, &coord_param);
2331 if(arg->opcode_token & WINED3DSI_TEXLD_BIAS) {
2332 glsl_src_param_t bias;
2333 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &bias);
2335 shader_addline(arg->buffer, "%s(Psampler%u, %s, %s)%s);\n",
2336 sample_function.name, sampler_idx, coord_param.param_str,
2337 bias.param_str, dst_swizzle);
2338 } else {
2339 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n",
2340 sample_function.name, sampler_idx, coord_param.param_str, dst_swizzle);
2345 static void shader_glsl_texldl(const SHADER_OPCODE_ARG *arg)
2347 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*)arg->shader;
2348 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2349 glsl_sample_function_t sample_function;
2350 glsl_src_param_t coord_param, lod_param;
2351 char dst_swizzle[6];
2352 DWORD sampler_type;
2353 DWORD sampler_idx;
2354 BOOL texrect = FALSE;
2356 shader_glsl_append_dst(arg->buffer, arg);
2357 shader_glsl_get_swizzle(arg->src[1], FALSE, arg->dst, dst_swizzle);
2359 sampler_idx = arg->src[1] & WINED3DSP_REGNUM_MASK;
2360 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2361 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2362 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2363 texrect = TRUE;
2365 shader_glsl_get_sample_function(sampler_type, FALSE, texrect, &sample_function); shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &coord_param);
2367 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &lod_param);
2369 if (shader_is_pshader_version(arg->reg_maps->shader_version))
2371 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2372 * However, they seem to work just fine in fragment shaders as well. */
2373 WARN("Using %sLod in fragment shader.\n", sample_function.name);
2374 shader_addline(arg->buffer, "%sLod(Psampler%u, %s, %s)%s);\n",
2375 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2376 } else {
2377 shader_addline(arg->buffer, "%sLod(Vsampler%u, %s, %s)%s);\n",
2378 sample_function.name, sampler_idx, coord_param.param_str, lod_param.param_str, dst_swizzle);
2382 static void pshader_glsl_texcoord(const SHADER_OPCODE_ARG *arg)
2384 /* FIXME: Make this work for more than just 2D textures */
2385 SHADER_BUFFER* buffer = arg->buffer;
2386 DWORD write_mask;
2387 char dst_mask[6];
2389 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2390 shader_glsl_get_write_mask(write_mask, dst_mask);
2392 if (arg->reg_maps->shader_version != WINED3DPS_VERSION(1,4))
2394 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2395 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n", reg, dst_mask);
2396 } else {
2397 DWORD reg = arg->src[0] & WINED3DSP_REGNUM_MASK;
2398 DWORD src_mod = arg->src[0] & WINED3DSP_SRCMOD_MASK;
2399 char dst_swizzle[6];
2401 shader_glsl_get_swizzle(arg->src[0], FALSE, write_mask, dst_swizzle);
2403 if (src_mod == WINED3DSPSM_DZ) {
2404 glsl_src_param_t div_param;
2405 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2406 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &div_param);
2408 if (mask_size > 1) {
2409 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2410 } else {
2411 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2413 } else if (src_mod == WINED3DSPSM_DW) {
2414 glsl_src_param_t div_param;
2415 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2416 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_3, &div_param);
2418 if (mask_size > 1) {
2419 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
2420 } else {
2421 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
2423 } else {
2424 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
2429 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2430 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2431 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2432 static void pshader_glsl_texdp3tex(const SHADER_OPCODE_ARG *arg)
2434 glsl_src_param_t src0_param;
2435 char dst_mask[6];
2436 glsl_sample_function_t sample_function;
2437 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2438 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2439 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2441 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2443 shader_glsl_append_dst(arg->buffer, arg);
2444 shader_glsl_get_write_mask(arg->dst, dst_mask);
2446 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2447 * scalar, and projected sampling would require 4.
2449 * It is a dependent read - not valid with conditional NP2 textures
2451 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2453 switch(count_bits(sample_function.coord_mask)) {
2454 case 1:
2455 shader_addline(arg->buffer, "%s(Psampler%u, dot(gl_TexCoord[%u].xyz, %s))%s);\n",
2456 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2457 break;
2459 case 2:
2460 shader_addline(arg->buffer, "%s(Psampler%u, vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0))%s);\n",
2461 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2462 break;
2464 case 3:
2465 shader_addline(arg->buffer, "%s(Psampler%u, vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0))%s);\n",
2466 sample_function.name, sampler_idx, sampler_idx, src0_param.param_str, dst_mask);
2467 break;
2468 default:
2469 FIXME("Unexpected mask bitcount %d\n", count_bits(sample_function.coord_mask));
2473 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2474 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2475 static void pshader_glsl_texdp3(const SHADER_OPCODE_ARG *arg)
2477 glsl_src_param_t src0_param;
2478 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2479 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2480 DWORD dst_mask;
2481 unsigned int mask_size;
2483 dst_mask = shader_glsl_append_dst(arg->buffer, arg);
2484 mask_size = shader_glsl_get_write_mask_size(dst_mask);
2485 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2487 if (mask_size > 1) {
2488 shader_addline(arg->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
2489 } else {
2490 shader_addline(arg->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
2494 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2495 * Calculate the depth as dst.x / dst.y */
2496 static void pshader_glsl_texdepth(const SHADER_OPCODE_ARG *arg)
2498 glsl_dst_param_t dst_param;
2500 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2502 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2503 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2504 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2505 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2506 * >= 1.0 or < 0.0
2508 shader_addline(arg->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n", dst_param.reg_name, dst_param.reg_name);
2511 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2512 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2513 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2514 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2516 static void pshader_glsl_texm3x2depth(const SHADER_OPCODE_ARG *arg)
2518 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2519 DWORD dstreg = arg->dst & WINED3DSP_REGNUM_MASK;
2520 glsl_src_param_t src0_param;
2522 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2524 shader_addline(arg->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
2525 shader_addline(arg->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2528 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2529 * Calculate the 1st of a 2-row matrix multiplication. */
2530 static void pshader_glsl_texm3x2pad(const SHADER_OPCODE_ARG *arg)
2532 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2533 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2534 SHADER_BUFFER* buffer = arg->buffer;
2535 glsl_src_param_t src0_param;
2537 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2538 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2541 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2542 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2543 static void pshader_glsl_texm3x3pad(const SHADER_OPCODE_ARG* arg)
2545 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2546 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2547 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2548 SHADER_BUFFER* buffer = arg->buffer;
2549 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2550 glsl_src_param_t src0_param;
2552 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2553 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
2554 current_state->texcoord_w[current_state->current_row++] = reg;
2557 static void pshader_glsl_texm3x2tex(const SHADER_OPCODE_ARG *arg)
2559 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2560 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2561 SHADER_BUFFER* buffer = arg->buffer;
2562 glsl_src_param_t src0_param;
2563 char dst_mask[6];
2565 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2566 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2568 shader_glsl_append_dst(buffer, arg);
2569 shader_glsl_get_write_mask(arg->dst, dst_mask);
2571 /* Sample the texture using the calculated coordinates */
2572 shader_addline(buffer, "texture2D(Psampler%u, tmp0.xy)%s);\n", reg, dst_mask);
2575 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2576 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2577 static void pshader_glsl_texm3x3tex(const SHADER_OPCODE_ARG *arg)
2579 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2580 glsl_src_param_t src0_param;
2581 char dst_mask[6];
2582 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2583 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2584 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2585 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2586 glsl_sample_function_t sample_function;
2588 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2589 shader_addline(arg->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2591 shader_glsl_append_dst(arg->buffer, arg);
2592 shader_glsl_get_write_mask(arg->dst, dst_mask);
2593 /* Dependent read, not valid with conditional NP2 */
2594 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2596 /* Sample the texture using the calculated coordinates */
2597 shader_addline(arg->buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2599 current_state->current_row = 0;
2602 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2603 * Perform the 3rd row of a 3x3 matrix multiply */
2604 static void pshader_glsl_texm3x3(const SHADER_OPCODE_ARG *arg)
2606 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2607 glsl_src_param_t src0_param;
2608 char dst_mask[6];
2609 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2610 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2611 SHADER_PARSE_STATE* current_state = &This->baseShader.parse_state;
2613 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2615 shader_glsl_append_dst(arg->buffer, arg);
2616 shader_glsl_get_write_mask(arg->dst, dst_mask);
2617 shader_addline(arg->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
2619 current_state->current_row = 0;
2622 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2623 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2624 static void pshader_glsl_texm3x3spec(const SHADER_OPCODE_ARG *arg)
2626 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2627 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2628 glsl_src_param_t src0_param;
2629 glsl_src_param_t src1_param;
2630 char dst_mask[6];
2631 SHADER_BUFFER* buffer = arg->buffer;
2632 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2633 DWORD stype = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2634 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2635 glsl_sample_function_t sample_function;
2637 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2638 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], src_mask, &src1_param);
2640 /* Perform the last matrix multiply operation */
2641 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
2642 /* Reflection calculation */
2643 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
2645 shader_glsl_append_dst(buffer, arg);
2646 shader_glsl_get_write_mask(arg->dst, dst_mask);
2647 /* Dependent read, not valid with conditional NP2 */
2648 shader_glsl_get_sample_function(stype, FALSE, FALSE, &sample_function);
2650 /* Sample the texture */
2651 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2653 current_state->current_row = 0;
2656 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2657 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2658 static void pshader_glsl_texm3x3vspec(const SHADER_OPCODE_ARG *arg)
2660 IWineD3DPixelShaderImpl* shader = (IWineD3DPixelShaderImpl*) arg->shader;
2661 DWORD reg = arg->dst & WINED3DSP_REGNUM_MASK;
2662 SHADER_BUFFER* buffer = arg->buffer;
2663 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
2664 glsl_src_param_t src0_param;
2665 char dst_mask[6];
2666 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2667 DWORD sampler_type = arg->reg_maps->samplers[reg] & WINED3DSP_TEXTURETYPE_MASK;
2668 glsl_sample_function_t sample_function;
2670 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], src_mask, &src0_param);
2672 /* Perform the last matrix multiply operation */
2673 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
2675 /* Construct the eye-ray vector from w coordinates */
2676 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2677 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
2678 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2680 shader_glsl_append_dst(buffer, arg);
2681 shader_glsl_get_write_mask(arg->dst, dst_mask);
2682 /* Dependent read, not valid with conditional NP2 */
2683 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2685 /* Sample the texture using the calculated coordinates */
2686 shader_addline(buffer, "%s(Psampler%u, tmp0.xyz)%s);\n", sample_function.name, reg, dst_mask);
2688 current_state->current_row = 0;
2691 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2692 * Apply a fake bump map transform.
2693 * texbem is pshader <= 1.3 only, this saves a few version checks
2695 static void pshader_glsl_texbem(const SHADER_OPCODE_ARG *arg)
2697 IWineD3DPixelShaderImpl* This = (IWineD3DPixelShaderImpl*) arg->shader;
2698 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2699 char dst_swizzle[6];
2700 glsl_sample_function_t sample_function;
2701 glsl_src_param_t coord_param;
2702 DWORD sampler_type;
2703 DWORD sampler_idx;
2704 DWORD mask;
2705 DWORD flags;
2706 char coord_mask[6];
2708 sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2709 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2711 sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2712 /* Dependent read, not valid with conditional NP2 */
2713 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2714 mask = sample_function.coord_mask;
2716 shader_glsl_get_write_mask(arg->dst, dst_swizzle);
2718 shader_glsl_get_write_mask(mask, coord_mask);
2720 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2721 * so we can't let the GL handle this.
2723 if (flags & WINED3DTTFF_PROJECTED) {
2724 DWORD div_mask=0;
2725 char coord_div_mask[3];
2726 switch (flags & ~WINED3DTTFF_PROJECTED) {
2727 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2728 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
2729 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
2730 case WINED3DTTFF_COUNT4:
2731 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
2733 shader_glsl_get_write_mask(div_mask, coord_div_mask);
2734 shader_addline(arg->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
2737 shader_glsl_append_dst(arg->buffer, arg);
2738 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &coord_param);
2739 if(arg->opcode->opcode == WINED3DSIO_TEXBEML) {
2740 glsl_src_param_t luminance_param;
2741 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_2, &luminance_param);
2742 shader_addline(arg->buffer, "(%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )*(%s * luminancescale%d + luminanceoffset%d))%s);\n",
2743 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask,
2744 luminance_param.param_str, sampler_idx, sampler_idx, dst_swizzle);
2745 } else {
2746 shader_addline(arg->buffer, "%s(Psampler%u, T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s )%s);\n",
2747 sample_function.name, sampler_idx, sampler_idx, coord_mask, sampler_idx, coord_param.param_str, coord_mask, dst_swizzle);
2751 static void pshader_glsl_bem(const SHADER_OPCODE_ARG *arg)
2753 glsl_src_param_t src0_param, src1_param;
2754 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2756 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src0_param);
2757 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0|WINED3DSP_WRITEMASK_1, &src1_param);
2759 shader_glsl_append_dst(arg->buffer, arg);
2760 shader_addline(arg->buffer, "%s + bumpenvmat%d * %s);\n",
2761 src0_param.param_str, sampler_idx, src1_param.param_str);
2764 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
2765 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
2766 static void pshader_glsl_texreg2ar(const SHADER_OPCODE_ARG *arg)
2768 glsl_src_param_t src0_param;
2769 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2770 char dst_mask[6];
2772 shader_glsl_append_dst(arg->buffer, arg);
2773 shader_glsl_get_write_mask(arg->dst, dst_mask);
2774 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2776 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.wx)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2779 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
2780 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
2781 static void pshader_glsl_texreg2gb(const SHADER_OPCODE_ARG *arg)
2783 glsl_src_param_t src0_param;
2784 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2785 char dst_mask[6];
2787 shader_glsl_append_dst(arg->buffer, arg);
2788 shader_glsl_get_write_mask(arg->dst, dst_mask);
2789 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2791 shader_addline(arg->buffer, "texture2D(Psampler%u, %s.yz)%s);\n", sampler_idx, src0_param.reg_name, dst_mask);
2794 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
2795 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
2796 static void pshader_glsl_texreg2rgb(const SHADER_OPCODE_ARG *arg)
2798 glsl_src_param_t src0_param;
2799 char dst_mask[6];
2800 DWORD sampler_idx = arg->dst & WINED3DSP_REGNUM_MASK;
2801 DWORD sampler_type = arg->reg_maps->samplers[sampler_idx] & WINED3DSP_TEXTURETYPE_MASK;
2802 glsl_sample_function_t sample_function;
2804 shader_glsl_append_dst(arg->buffer, arg);
2805 shader_glsl_get_write_mask(arg->dst, dst_mask);
2806 /* Dependent read, not valid with conditional NP2 */
2807 shader_glsl_get_sample_function(sampler_type, FALSE, FALSE, &sample_function);
2808 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], sample_function.coord_mask, &src0_param);
2810 shader_addline(arg->buffer, "%s(Psampler%u, %s)%s);\n", sample_function.name, sampler_idx, src0_param.param_str, dst_mask);
2813 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
2814 * If any of the first 3 components are < 0, discard this pixel */
2815 static void pshader_glsl_texkill(const SHADER_OPCODE_ARG *arg)
2817 glsl_dst_param_t dst_param;
2819 /* The argument is a destination parameter, and no writemasks are allowed */
2820 shader_glsl_add_dst_param(arg, arg->dst, 0, &dst_param);
2821 if ((arg->reg_maps->shader_version >= WINED3DPS_VERSION(2,0)))
2823 /* 2.0 shaders compare all 4 components in texkill */
2824 shader_addline(arg->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
2825 } else {
2826 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
2827 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
2828 * 4 components are defined, only the first 3 are used
2830 shader_addline(arg->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
2834 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
2835 * dst = dot2(src0, src1) + src2 */
2836 static void pshader_glsl_dp2add(const SHADER_OPCODE_ARG *arg)
2838 glsl_src_param_t src0_param;
2839 glsl_src_param_t src1_param;
2840 glsl_src_param_t src2_param;
2841 DWORD write_mask;
2842 unsigned int mask_size;
2844 write_mask = shader_glsl_append_dst(arg->buffer, arg);
2845 mask_size = shader_glsl_get_write_mask_size(write_mask);
2847 shader_glsl_add_src_param(arg, arg->src[0], arg->src_addr[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
2848 shader_glsl_add_src_param(arg, arg->src[1], arg->src_addr[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
2849 shader_glsl_add_src_param(arg, arg->src[2], arg->src_addr[2], WINED3DSP_WRITEMASK_0, &src2_param);
2851 if (mask_size > 1) {
2852 shader_addline(arg->buffer, "vec%d(dot(%s, %s) + %s));\n", mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
2853 } else {
2854 shader_addline(arg->buffer, "dot(%s, %s) + %s);\n", src0_param.param_str, src1_param.param_str, src2_param.param_str);
2858 static void pshader_glsl_input_pack(SHADER_BUFFER* buffer, const struct semantic* semantics_in,
2859 IWineD3DPixelShader *iface, enum vertexprocessing_mode vertexprocessing)
2861 unsigned int i;
2862 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *) iface;
2864 for (i = 0; i < MAX_REG_INPUT; i++) {
2866 DWORD usage_token = semantics_in[i].usage;
2867 DWORD register_token = semantics_in[i].reg;
2868 DWORD usage, usage_idx;
2869 char reg_mask[6];
2871 /* Uninitialized */
2872 if (!usage_token) continue;
2873 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2874 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2875 shader_glsl_get_write_mask(register_token, reg_mask);
2877 switch(usage) {
2879 case WINED3DDECLUSAGE_TEXCOORD:
2880 if(usage_idx < 8 && vertexprocessing == pretransformed) {
2881 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
2882 This->input_reg_map[i], reg_mask, usage_idx, reg_mask);
2883 } else {
2884 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2885 This->input_reg_map[i], reg_mask, reg_mask);
2887 break;
2889 case WINED3DDECLUSAGE_COLOR:
2890 if (usage_idx == 0)
2891 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
2892 This->input_reg_map[i], reg_mask, reg_mask);
2893 else if (usage_idx == 1)
2894 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
2895 This->input_reg_map[i], reg_mask, reg_mask);
2896 else
2897 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2898 This->input_reg_map[i], reg_mask, reg_mask);
2899 break;
2901 default:
2902 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
2903 This->input_reg_map[i], reg_mask, reg_mask);
2908 /*********************************************
2909 * Vertex Shader Specific Code begins here
2910 ********************************************/
2912 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
2913 glsl_program_key_t *key;
2915 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2916 key->vshader = entry->vshader;
2917 key->pshader = entry->pshader;
2918 key->ps_args = entry->ps_args;
2920 hash_table_put(priv->glsl_program_lookup, key, entry);
2923 static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
2924 GLhandleARB vshader, IWineD3DPixelShader *pshader, struct ps_compile_args *ps_args) {
2925 glsl_program_key_t key;
2927 key.vshader = vshader;
2928 key.pshader = pshader;
2929 key.ps_args = *ps_args;
2931 return (struct glsl_shader_prog_link *)hash_table_get(priv->glsl_program_lookup, &key);
2934 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const WineD3D_GL_Info *gl_info,
2935 struct glsl_shader_prog_link *entry)
2937 glsl_program_key_t *key;
2939 key = HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t));
2940 key->vshader = entry->vshader;
2941 key->pshader = entry->pshader;
2942 key->ps_args = entry->ps_args;
2943 hash_table_remove(priv->glsl_program_lookup, key);
2945 GL_EXTCALL(glDeleteObjectARB(entry->programId));
2946 if (entry->vshader) list_remove(&entry->vshader_entry);
2947 if (entry->pshader) list_remove(&entry->pshader_entry);
2948 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
2949 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
2950 HeapFree(GetProcessHeap(), 0, entry);
2953 static void handle_ps3_input(SHADER_BUFFER *buffer, const struct semantic *semantics_in,
2954 const struct semantic *semantics_out, const WineD3D_GL_Info *gl_info, const DWORD *map)
2956 unsigned int i, j;
2957 DWORD usage_token, usage_token_out;
2958 DWORD register_token, register_token_out;
2959 DWORD usage, usage_idx, usage_out, usage_idx_out;
2960 DWORD *set;
2961 DWORD in_idx;
2962 DWORD in_count = GL_LIMITS(glsl_varyings) / 4;
2963 char reg_mask[6], reg_mask_out[6];
2964 char destination[50];
2966 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
2968 if (!semantics_out) {
2969 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
2970 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
2971 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
2974 for(i = 0; i < MAX_REG_INPUT; i++) {
2975 usage_token = semantics_in[i].usage;
2976 if (!usage_token) continue;
2978 in_idx = map[i];
2979 if (in_idx >= (in_count + 2)) {
2980 FIXME("More input varyings declared than supported, expect issues\n");
2981 continue;
2982 } else if(map[i] == -1) {
2983 /* Declared, but not read register */
2984 continue;
2987 if (in_idx == in_count) {
2988 sprintf(destination, "gl_FrontColor");
2989 } else if (in_idx == in_count + 1) {
2990 sprintf(destination, "gl_FrontSecondaryColor");
2991 } else {
2992 sprintf(destination, "IN[%u]", in_idx);
2995 register_token = semantics_in[i].reg;
2997 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
2998 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
2999 set[map[i]] = shader_glsl_get_write_mask(register_token, reg_mask);
3001 if(!semantics_out) {
3002 switch(usage) {
3003 case WINED3DDECLUSAGE_COLOR:
3004 if (usage_idx == 0)
3005 shader_addline(buffer, "%s%s = front_color%s;\n",
3006 destination, reg_mask, reg_mask);
3007 else if (usage_idx == 1)
3008 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
3009 destination, reg_mask, reg_mask);
3010 else
3011 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3012 destination, reg_mask, reg_mask);
3013 break;
3015 case WINED3DDECLUSAGE_TEXCOORD:
3016 if (usage_idx < 8) {
3017 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
3018 destination, reg_mask, usage_idx, reg_mask);
3019 } else {
3020 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3021 destination, reg_mask, reg_mask);
3023 break;
3025 case WINED3DDECLUSAGE_FOG:
3026 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3027 destination, reg_mask, reg_mask);
3028 break;
3030 default:
3031 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3032 destination, reg_mask, reg_mask);
3034 } else {
3035 BOOL found = FALSE;
3036 for(j = 0; j < MAX_REG_OUTPUT; j++) {
3037 usage_token_out = semantics_out[j].usage;
3038 if (!usage_token_out) continue;
3039 register_token_out = semantics_out[j].reg;
3041 usage_out = (usage_token_out & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3042 usage_idx_out = (usage_token_out & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3043 shader_glsl_get_write_mask(register_token_out, reg_mask_out);
3045 if(usage == usage_out &&
3046 usage_idx == usage_idx_out) {
3047 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3048 destination, reg_mask, j, reg_mask);
3049 found = TRUE;
3052 if(!found) {
3053 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3054 destination, reg_mask, reg_mask);
3059 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3060 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3061 * input varyings are assigned above, if the optimizer works properly.
3063 for(i = 0; i < in_count + 2; i++) {
3064 if(set[i] != WINED3DSP_WRITEMASK_ALL) {
3065 unsigned int size = 0;
3066 memset(reg_mask, 0, sizeof(reg_mask));
3067 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
3068 reg_mask[size] = 'x';
3069 size++;
3071 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
3072 reg_mask[size] = 'y';
3073 size++;
3075 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
3076 reg_mask[size] = 'z';
3077 size++;
3079 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
3080 reg_mask[size] = 'w';
3081 size++;
3084 if (i == in_count) {
3085 sprintf(destination, "gl_FrontColor");
3086 } else if (i == in_count + 1) {
3087 sprintf(destination, "gl_FrontSecondaryColor");
3088 } else {
3089 sprintf(destination, "IN[%u]", i);
3092 if (size == 1) {
3093 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3094 } else {
3095 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3100 HeapFree(GetProcessHeap(), 0, set);
3103 static GLhandleARB generate_param_reorder_function(IWineD3DVertexShader *vertexshader,
3104 IWineD3DPixelShader *pixelshader, const WineD3D_GL_Info *gl_info)
3106 GLhandleARB ret = 0;
3107 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3108 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3109 IWineD3DDeviceImpl *device;
3110 DWORD vs_major = WINED3DSHADER_VERSION_MAJOR(vs->baseShader.reg_maps.shader_version);
3111 DWORD ps_major = ps ? WINED3DSHADER_VERSION_MAJOR(ps->baseShader.reg_maps.shader_version) : 0;
3112 unsigned int i;
3113 SHADER_BUFFER buffer;
3114 DWORD usage_token;
3115 DWORD register_token;
3116 DWORD usage, usage_idx, writemask;
3117 char reg_mask[6];
3118 const struct semantic *semantics_out, *semantics_in;
3120 shader_buffer_init(&buffer);
3122 shader_addline(&buffer, "#version 120\n");
3124 if(vs_major < 3 && ps_major < 3) {
3125 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3126 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3128 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3129 if((GLINFO_LOCATION).set_texcoord_w && ps_major == 0 && vs_major > 0 &&
3130 !device->frag_pipe->ffp_proj_control) {
3131 shader_addline(&buffer, "void order_ps_input() {\n");
3132 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3133 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3134 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3135 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3138 shader_addline(&buffer, "}\n");
3139 } else {
3140 shader_addline(&buffer, "void order_ps_input() { /* do nothing */ }\n");
3142 } else if(ps_major < 3 && vs_major >= 3) {
3143 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3144 semantics_out = vs->semantics_out;
3146 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3147 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3148 usage_token = semantics_out[i].usage;
3149 if (!usage_token) continue;
3150 register_token = semantics_out[i].reg;
3152 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3153 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3154 writemask = shader_glsl_get_write_mask(register_token, reg_mask);
3156 switch(usage) {
3157 case WINED3DDECLUSAGE_COLOR:
3158 if (usage_idx == 0)
3159 shader_addline(&buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3160 else if (usage_idx == 1)
3161 shader_addline(&buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3162 break;
3164 case WINED3DDECLUSAGE_POSITION:
3165 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3166 break;
3168 case WINED3DDECLUSAGE_TEXCOORD:
3169 if (usage_idx < 8) {
3170 if(!(GLINFO_LOCATION).set_texcoord_w || ps_major > 0) writemask |= WINED3DSP_WRITEMASK_3;
3172 shader_addline(&buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3173 usage_idx, reg_mask, i, reg_mask);
3174 if(!(writemask & WINED3DSP_WRITEMASK_3)) {
3175 shader_addline(&buffer, "gl_TexCoord[%u].w = 1.0;\n", usage_idx);
3178 break;
3180 case WINED3DDECLUSAGE_PSIZE:
3181 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3182 break;
3184 case WINED3DDECLUSAGE_FOG:
3185 shader_addline(&buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3186 break;
3188 default:
3189 break;
3192 shader_addline(&buffer, "}\n");
3194 } else if(ps_major >= 3 && vs_major >= 3) {
3195 semantics_out = vs->semantics_out;
3196 semantics_in = ps->semantics_in;
3198 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3199 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3200 shader_addline(&buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3202 /* First, sort out position and point size. Those are not passed to the pixel shader */
3203 for(i = 0; i < MAX_REG_OUTPUT; i++) {
3204 usage_token = semantics_out[i].usage;
3205 if (!usage_token) continue;
3206 register_token = semantics_out[i].reg;
3208 usage = (usage_token & WINED3DSP_DCL_USAGE_MASK) >> WINED3DSP_DCL_USAGE_SHIFT;
3209 usage_idx = (usage_token & WINED3DSP_DCL_USAGEINDEX_MASK) >> WINED3DSP_DCL_USAGEINDEX_SHIFT;
3210 shader_glsl_get_write_mask(register_token, reg_mask);
3212 switch(usage) {
3213 case WINED3DDECLUSAGE_POSITION:
3214 shader_addline(&buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3215 break;
3217 case WINED3DDECLUSAGE_PSIZE:
3218 shader_addline(&buffer, "gl_PointSize = OUT[%u].x;\n", i);
3219 break;
3221 default:
3222 break;
3226 /* Then, fix the pixel shader input */
3227 handle_ps3_input(&buffer, semantics_in, semantics_out, gl_info, ps->input_reg_map);
3229 shader_addline(&buffer, "}\n");
3230 } else if(ps_major >= 3 && vs_major < 3) {
3231 semantics_in = ps->semantics_in;
3233 shader_addline(&buffer, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings) / 4);
3234 shader_addline(&buffer, "void order_ps_input() {\n");
3235 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3236 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3237 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3239 handle_ps3_input(&buffer, semantics_in, NULL, gl_info, ps->input_reg_map);
3240 shader_addline(&buffer, "}\n");
3241 } else {
3242 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3245 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3246 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3247 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL));
3248 checkGLcall("glShaderSourceARB(ret, 1, (const char**)&buffer.buffer, NULL)");
3249 GL_EXTCALL(glCompileShaderARB(ret));
3250 checkGLcall("glCompileShaderARB(ret)");
3252 shader_buffer_free(&buffer);
3253 return ret;
3256 static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const WineD3D_GL_Info *gl_info,
3257 GLhandleARB programId, char prefix)
3259 const local_constant *lconst;
3260 GLuint tmp_loc;
3261 const float *value;
3262 char glsl_name[8];
3264 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3265 value = (const float *)lconst->value;
3266 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3267 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3268 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3270 checkGLcall("Hardcoding local constants\n");
3273 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3274 * It sets the programId on the current StateBlock (because it should be called
3275 * inside of the DrawPrimitive() part of the render loop).
3277 * If a program for the given combination does not exist, create one, and store
3278 * the program in the hash table. If it creates a program, it will link the
3279 * given objects, too.
3281 static void set_glsl_shader_program(IWineD3DDevice *iface, BOOL use_ps, BOOL use_vs) {
3282 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3283 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3284 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3285 IWineD3DPixelShader *pshader = This->stateBlock->pixelShader;
3286 IWineD3DVertexShader *vshader = This->stateBlock->vertexShader;
3287 struct glsl_shader_prog_link *entry = NULL;
3288 GLhandleARB programId = 0;
3289 GLhandleARB reorder_shader_id = 0;
3290 int i;
3291 char glsl_name[8];
3292 GLhandleARB vshader_id, pshader_id;
3293 struct ps_compile_args compile_args;
3295 if(use_vs) {
3296 IWineD3DVertexShaderImpl_CompileShader(vshader);
3297 vshader_id = ((IWineD3DVertexShaderImpl*)vshader)->prgId;
3298 } else {
3299 vshader_id = 0;
3301 if(use_ps) {
3302 find_ps_compile_args((IWineD3DPixelShaderImpl*)This->stateBlock->pixelShader, This->stateBlock, &compile_args);
3303 } else {
3304 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3305 memset(&compile_args, 0, sizeof(compile_args));
3307 entry = get_glsl_program_entry(priv, vshader_id, pshader, &compile_args);
3308 if (entry) {
3309 priv->glsl_program = entry;
3310 return;
3313 /* If we get to this point, then no matching program exists, so we create one */
3314 programId = GL_EXTCALL(glCreateProgramObjectARB());
3315 TRACE("Created new GLSL shader program %u\n", programId);
3317 /* Create the entry */
3318 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
3319 entry->programId = programId;
3320 entry->vshader = vshader_id;
3321 entry->pshader = pshader;
3322 entry->ps_args = compile_args;
3323 entry->constant_version = 0;
3324 /* Add the hash table entry */
3325 add_glsl_program_entry(priv, entry);
3327 /* Set the current program */
3328 priv->glsl_program = entry;
3330 /* Attach GLSL vshader */
3331 if (vshader_id) {
3332 int max_attribs = 16; /* TODO: Will this always be the case? It is at the moment... */
3333 char tmp_name[10];
3335 reorder_shader_id = generate_param_reorder_function(vshader, pshader, gl_info);
3336 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
3337 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
3338 checkGLcall("glAttachObjectARB");
3339 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3340 * is destroyed
3342 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
3344 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
3345 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
3346 checkGLcall("glAttachObjectARB");
3348 /* Bind vertex attributes to a corresponding index number to match
3349 * the same index numbers as ARB_vertex_programs (makes loading
3350 * vertex attributes simpler). With this method, we can use the
3351 * exact same code to load the attributes later for both ARB and
3352 * GLSL shaders.
3354 * We have to do this here because we need to know the Program ID
3355 * in order to make the bindings work, and it has to be done prior
3356 * to linking the GLSL program. */
3357 for (i = 0; i < max_attribs; ++i) {
3358 if (((IWineD3DBaseShaderImpl*)vshader)->baseShader.reg_maps.attributes[i]) {
3359 snprintf(tmp_name, sizeof(tmp_name), "attrib%i", i);
3360 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
3363 checkGLcall("glBindAttribLocationARB");
3365 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
3368 if(use_ps) {
3369 pshader_id = find_gl_pshader((IWineD3DPixelShaderImpl *) pshader, &compile_args);
3370 } else {
3371 pshader_id = 0;
3374 /* Attach GLSL pshader */
3375 if (pshader_id) {
3376 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
3377 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
3378 checkGLcall("glAttachObjectARB");
3380 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
3383 /* Link the program */
3384 TRACE("Linking GLSL shader program %u\n", programId);
3385 GL_EXTCALL(glLinkProgramARB(programId));
3386 print_glsl_info_log(&GLINFO_LOCATION, programId);
3388 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(vshader_constantsF));
3389 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
3390 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
3391 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3393 for (i = 0; i < MAX_CONST_I; ++i) {
3394 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
3395 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3397 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB) * GL_LIMITS(pshader_constantsF));
3398 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
3399 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
3400 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3402 for (i = 0; i < MAX_CONST_I; ++i) {
3403 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
3404 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3407 if(pshader) {
3408 for(i = 0; i < ((IWineD3DPixelShaderImpl*)pshader)->numbumpenvmatconsts; i++) {
3409 char name[32];
3410 sprintf(name, "bumpenvmat%d", ((IWineD3DPixelShaderImpl*)pshader)->bumpenvmatconst[i].texunit);
3411 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3412 sprintf(name, "luminancescale%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3413 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3414 sprintf(name, "luminanceoffset%d", ((IWineD3DPixelShaderImpl*)pshader)->luminanceconst[i].texunit);
3415 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
3420 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
3421 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
3422 checkGLcall("Find glsl program uniform locations");
3424 if (pshader
3425 && WINED3DSHADER_VERSION_MAJOR(((IWineD3DPixelShaderImpl *)pshader)->baseShader.reg_maps.shader_version) >= 3
3426 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > GL_LIMITS(glsl_varyings) / 4)
3428 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
3429 entry->vertex_color_clamp = GL_FALSE;
3430 } else {
3431 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
3434 /* Set the shader to allow uniform loading on it */
3435 GL_EXTCALL(glUseProgramObjectARB(programId));
3436 checkGLcall("glUseProgramObjectARB(programId)");
3438 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3439 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3440 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3441 * vertex shader with fixed function pixel processing is used we make sure that the card
3442 * supports enough samplers to allow the max number of vertex samplers with all possible
3443 * fixed function fragment processing setups. So once the program is linked these samplers
3444 * won't change.
3446 if(vshader_id) {
3447 /* Load vertex shader samplers */
3448 shader_glsl_load_vsamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3450 if(pshader_id) {
3451 /* Load pixel shader samplers */
3452 shader_glsl_load_psamplers(gl_info, (IWineD3DStateBlock*)This->stateBlock, programId);
3455 /* If the local constants do not have to be loaded with the environment constants,
3456 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3457 * later
3459 if(pshader && !((IWineD3DPixelShaderImpl*)pshader)->baseShader.load_local_constsF) {
3460 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
3462 if(vshader && !((IWineD3DVertexShaderImpl*)vshader)->baseShader.load_local_constsF) {
3463 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
3467 static GLhandleARB create_glsl_blt_shader(const WineD3D_GL_Info *gl_info, enum tex_types tex_type)
3469 GLhandleARB program_id;
3470 GLhandleARB vshader_id, pshader_id;
3471 static const char *blt_vshader[] =
3473 "#version 120\n"
3474 "void main(void)\n"
3475 "{\n"
3476 " gl_Position = gl_Vertex;\n"
3477 " gl_FrontColor = vec4(1.0);\n"
3478 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3479 "}\n"
3482 static const char *blt_pshaders[tex_type_count] =
3484 /* tex_1d */
3485 NULL,
3486 /* tex_2d */
3487 "#version 120\n"
3488 "uniform sampler2D sampler;\n"
3489 "void main(void)\n"
3490 "{\n"
3491 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3492 "}\n",
3493 /* tex_3d */
3494 NULL,
3495 /* tex_cube */
3496 "#version 120\n"
3497 "uniform samplerCube sampler;\n"
3498 "void main(void)\n"
3499 "{\n"
3500 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3501 "}\n",
3502 /* tex_rect */
3503 "#version 120\n"
3504 "#extension GL_ARB_texture_rectangle : enable\n"
3505 "uniform sampler2DRect sampler;\n"
3506 "void main(void)\n"
3507 "{\n"
3508 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3509 "}\n",
3512 if (!blt_pshaders[tex_type])
3514 FIXME("tex_type %#x not supported\n", tex_type);
3515 tex_type = tex_2d;
3518 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3519 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
3520 GL_EXTCALL(glCompileShaderARB(vshader_id));
3522 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3523 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
3524 GL_EXTCALL(glCompileShaderARB(pshader_id));
3526 program_id = GL_EXTCALL(glCreateProgramObjectARB());
3527 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
3528 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
3529 GL_EXTCALL(glLinkProgramARB(program_id));
3531 print_glsl_info_log(&GLINFO_LOCATION, program_id);
3533 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3534 * is destroyed
3536 GL_EXTCALL(glDeleteObjectARB(vshader_id));
3537 GL_EXTCALL(glDeleteObjectARB(pshader_id));
3538 return program_id;
3541 static void shader_glsl_select(IWineD3DDevice *iface, BOOL usePS, BOOL useVS) {
3542 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3543 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3544 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3545 GLhandleARB program_id = 0;
3546 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
3548 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3550 if (useVS || usePS) set_glsl_shader_program(iface, usePS, useVS);
3551 else priv->glsl_program = NULL;
3553 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
3555 if (old_vertex_color_clamp != current_vertex_color_clamp) {
3556 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT)) {
3557 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
3558 checkGLcall("glClampColorARB");
3559 } else {
3560 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3564 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3565 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3566 GL_EXTCALL(glUseProgramObjectARB(program_id));
3567 checkGLcall("glUseProgramObjectARB");
3570 static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
3571 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3572 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3573 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3574 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
3576 if (!*blt_program) {
3577 GLhandleARB loc;
3578 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
3579 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
3580 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3581 GL_EXTCALL(glUniform1iARB(loc, 0));
3582 } else {
3583 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
3587 static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
3588 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3589 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3590 struct shader_glsl_priv *priv = (struct shader_glsl_priv *) This->shader_priv;
3591 GLhandleARB program_id;
3593 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
3594 if (program_id) TRACE("Using GLSL program %u\n", program_id);
3596 GL_EXTCALL(glUseProgramObjectARB(program_id));
3597 checkGLcall("glUseProgramObjectARB");
3600 static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
3601 const struct list *linked_programs;
3602 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
3603 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
3604 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)device->shader_priv;
3605 const WineD3D_GL_Info *gl_info = &device->adapter->gl_info;
3606 IWineD3DPixelShaderImpl *ps = NULL;
3607 IWineD3DVertexShaderImpl *vs = NULL;
3609 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3610 * can be called from IWineD3DBaseShader::Release
3612 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version);
3614 if(pshader) {
3615 ps = (IWineD3DPixelShaderImpl *) This;
3616 if(ps->num_gl_shaders == 0) return;
3617 } else {
3618 vs = (IWineD3DVertexShaderImpl *) This;
3619 if(vs->prgId == 0) return;
3622 linked_programs = &This->baseShader.linked_programs;
3624 TRACE("Deleting linked programs\n");
3625 if (linked_programs->next) {
3626 struct glsl_shader_prog_link *entry, *entry2;
3628 if(pshader) {
3629 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
3630 delete_glsl_program_entry(priv, gl_info, entry);
3632 } else {
3633 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
3634 delete_glsl_program_entry(priv, gl_info, entry);
3639 if(pshader) {
3640 UINT i;
3642 ENTER_GL();
3643 for(i = 0; i < ps->num_gl_shaders; i++) {
3644 TRACE("deleting pshader %u\n", ps->gl_shaders[i].prgId);
3645 GL_EXTCALL(glDeleteObjectARB(ps->gl_shaders[i].prgId));
3646 checkGLcall("glDeleteObjectARB");
3648 LEAVE_GL();
3649 HeapFree(GetProcessHeap(), 0, ps->gl_shaders);
3650 ps->gl_shaders = NULL;
3651 ps->num_gl_shaders = 0;
3652 } else {
3653 TRACE("Deleting shader object %u\n", vs->prgId);
3654 ENTER_GL();
3655 GL_EXTCALL(glDeleteObjectARB(vs->prgId));
3656 checkGLcall("glDeleteObjectARB");
3657 LEAVE_GL();
3658 vs->prgId = 0;
3659 vs->baseShader.is_compiled = FALSE;
3663 static unsigned int glsl_program_key_hash(const void *key)
3665 const glsl_program_key_t *k = (const glsl_program_key_t *)key;
3667 unsigned int hash = k->vshader | ((DWORD_PTR) k->pshader) << 16;
3668 hash += ~(hash << 15);
3669 hash ^= (hash >> 10);
3670 hash += (hash << 3);
3671 hash ^= (hash >> 6);
3672 hash += ~(hash << 11);
3673 hash ^= (hash >> 16);
3675 return hash;
3678 static BOOL glsl_program_key_compare(const void *keya, const void *keyb)
3680 const glsl_program_key_t *ka = (const glsl_program_key_t *)keya;
3681 const glsl_program_key_t *kb = (const glsl_program_key_t *)keyb;
3683 return ka->vshader == kb->vshader && ka->pshader == kb->pshader &&
3684 (memcmp(&ka->ps_args, &kb->ps_args, sizeof(kb->ps_args)) == 0);
3687 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
3689 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
3690 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
3692 if (!mem)
3694 ERR("Failed to allocate memory\n");
3695 return FALSE;
3698 heap->entries = mem;
3699 heap->entries[1].version = 0;
3700 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
3701 heap->size = 1;
3703 return TRUE;
3706 static void constant_heap_free(struct constant_heap *heap)
3708 HeapFree(GetProcessHeap(), 0, heap->entries);
3711 static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
3712 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3713 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3714 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
3715 SIZE_T stack_size = wined3d_log2i(max(GL_LIMITS(vshader_constantsF), GL_LIMITS(pshader_constantsF))) + 1;
3717 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
3718 if (!priv->stack)
3720 ERR("Failed to allocate memory.\n");
3721 HeapFree(GetProcessHeap(), 0, priv);
3722 return E_OUTOFMEMORY;
3725 if (!constant_heap_init(&priv->vconst_heap, GL_LIMITS(vshader_constantsF)))
3727 ERR("Failed to initialize vertex shader constant heap\n");
3728 HeapFree(GetProcessHeap(), 0, priv->stack);
3729 HeapFree(GetProcessHeap(), 0, priv);
3730 return E_OUTOFMEMORY;
3733 if (!constant_heap_init(&priv->pconst_heap, GL_LIMITS(pshader_constantsF)))
3735 ERR("Failed to initialize pixel shader constant heap\n");
3736 constant_heap_free(&priv->vconst_heap);
3737 HeapFree(GetProcessHeap(), 0, priv->stack);
3738 HeapFree(GetProcessHeap(), 0, priv);
3739 return E_OUTOFMEMORY;
3742 priv->glsl_program_lookup = hash_table_create(glsl_program_key_hash, glsl_program_key_compare);
3743 priv->next_constant_version = 1;
3745 This->shader_priv = priv;
3746 return WINED3D_OK;
3749 static void shader_glsl_free(IWineD3DDevice *iface) {
3750 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
3751 const WineD3D_GL_Info *gl_info = &This->adapter->gl_info;
3752 struct shader_glsl_priv *priv = (struct shader_glsl_priv *)This->shader_priv;
3753 int i;
3755 for (i = 0; i < tex_type_count; ++i)
3757 if (priv->depth_blt_program[i])
3759 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
3763 hash_table_destroy(priv->glsl_program_lookup, NULL, NULL);
3764 constant_heap_free(&priv->pconst_heap);
3765 constant_heap_free(&priv->vconst_heap);
3767 HeapFree(GetProcessHeap(), 0, This->shader_priv);
3768 This->shader_priv = NULL;
3771 static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
3772 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
3773 return FALSE;
3776 static GLuint shader_glsl_generate_pshader(IWineD3DPixelShader *iface, SHADER_BUFFER *buffer, const struct ps_compile_args *args) {
3777 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3778 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3779 CONST DWORD *function = This->baseShader.function;
3780 const char *fragcolor;
3781 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3783 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3784 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3786 shader_addline(buffer, "#version 120\n");
3788 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3789 shader_addline(buffer, "#extension GL_ARB_draw_buffers : enable\n");
3791 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3792 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3793 * drivers write a warning if we don't do so
3795 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3798 /* Base Declarations */
3799 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, args);
3801 /* Pack 3.0 inputs */
3802 if (reg_maps->shader_version >= WINED3DPS_VERSION(3,0) && args->vp_mode != vertexshader) {
3803 pshader_glsl_input_pack(buffer, This->semantics_in, iface, args->vp_mode);
3806 /* Base Shader Body */
3807 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3809 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3810 if (reg_maps->shader_version < WINED3DPS_VERSION(2,0))
3812 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3813 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
3814 shader_addline(buffer, "gl_FragData[0] = R0;\n");
3815 else
3816 shader_addline(buffer, "gl_FragColor = R0;\n");
3819 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
3820 fragcolor = "gl_FragData[0]";
3821 } else {
3822 fragcolor = "gl_FragColor";
3824 if(args->srgb_correction) {
3825 shader_addline(buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3826 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
3827 srgb_sub_high, srgb_sub_high, srgb_sub_high);
3828 shader_addline(buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
3829 shader_addline(buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
3830 shader_addline(buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
3831 shader_addline(buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
3832 shader_addline(buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
3834 /* Pixel shader < 3.0 do not replace the fog stage.
3835 * This implements linear fog computation and blending.
3836 * TODO: non linear fog
3837 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3838 * -1/(e-s) and e/(e-s) respectively.
3840 if(reg_maps->shader_version < WINED3DPS_VERSION(3,0)) {
3841 switch(args->fog) {
3842 case FOG_OFF: break;
3843 case FOG_LINEAR:
3844 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
3845 shader_addline(buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
3846 break;
3847 case FOG_EXP:
3848 FIXME("Implement EXP fog in glsl\n");
3849 break;
3850 case FOG_EXP2:
3851 FIXME("Implement EXP2 fog in glsl\n");
3852 break;
3856 shader_addline(buffer, "}\n");
3858 TRACE("Compiling shader object %u\n", shader_obj);
3859 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3860 GL_EXTCALL(glCompileShaderARB(shader_obj));
3861 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3863 /* Store the shader object */
3864 return shader_obj;
3867 static void shader_glsl_generate_vshader(IWineD3DVertexShader *iface, SHADER_BUFFER *buffer) {
3868 IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
3869 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3870 CONST DWORD *function = This->baseShader.function;
3871 const WineD3D_GL_Info *gl_info = &((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info;
3873 /* Create the hw GLSL shader program and assign it as the shader->prgId */
3874 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3876 shader_addline(buffer, "#version 120\n");
3878 /* Base Declarations */
3879 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, buffer, &GLINFO_LOCATION, NULL);
3881 /* Base Shader Body */
3882 shader_generate_main( (IWineD3DBaseShader*) This, buffer, reg_maps, function);
3884 /* Unpack 3.0 outputs */
3885 if (reg_maps->shader_version >= WINED3DVS_VERSION(3,0)) shader_addline(buffer, "order_ps_input(OUT);\n");
3886 else shader_addline(buffer, "order_ps_input();\n");
3888 /* If this shader doesn't use fog copy the z coord to the fog coord so that we can use table fog */
3889 if (!reg_maps->fog)
3890 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
3892 /* Write the final position.
3894 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3895 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3896 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3897 * contains 1.0 to allow a mad.
3899 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3900 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3902 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3904 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3905 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3906 * which is the same as z = z * 2 - w.
3908 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3910 shader_addline(buffer, "}\n");
3912 TRACE("Compiling shader object %u\n", shader_obj);
3913 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
3914 GL_EXTCALL(glCompileShaderARB(shader_obj));
3915 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
3917 /* Store the shader object */
3918 This->prgId = shader_obj;
3921 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype, const WineD3D_GL_Info *gl_info, struct shader_caps *pCaps)
3923 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
3924 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
3925 * vs_nv_version which is based on NV_vertex_program.
3926 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
3927 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
3928 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
3929 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
3931 if((GLINFO_LOCATION.vs_nv_version == VS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3932 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
3933 else
3934 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
3935 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
3936 pCaps->MaxVertexShaderConst = GL_LIMITS(vshader_constantsF);
3938 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
3939 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
3940 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
3941 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
3942 * in max native instructions. Intel and others also offer the info in this extension but they
3943 * don't support GLSL (at least on Windows).
3945 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
3946 * of instructions is 512 or less we have to do with ps2.0 hardware.
3947 * NOTE: ps3.0 hardware requires 512 or more instructions but ati and nvidia offer 'enough' (1024 vs 4096) on their most basic ps3.0 hardware.
3949 if((GLINFO_LOCATION.ps_nv_version == PS_VERSION_20) || (GLINFO_LOCATION.ps_arb_max_instructions <= 512))
3950 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
3951 else
3952 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
3954 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
3955 * Direct3D minimum requirement.
3957 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
3958 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
3960 * The problem is that the refrast clamps temporary results in the shader to
3961 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
3962 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
3963 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
3964 * offer a way to query this.
3966 pCaps->PixelShader1xMaxValue = 8.0;
3967 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
3970 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
3972 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
3974 TRACE("Checking support for fixup:\n");
3975 dump_color_fixup_desc(fixup);
3978 /* We support everything except YUV conversions. */
3979 if (!is_yuv_fixup(fixup))
3981 TRACE("[OK]\n");
3982 return TRUE;
3985 TRACE("[FAILED]\n");
3986 return FALSE;
3989 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
3991 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
3992 /* WINED3DSIH_ADD */ shader_glsl_arith,
3993 /* WINED3DSIH_BEM */ pshader_glsl_bem,
3994 /* WINED3DSIH_BREAK */ shader_glsl_break,
3995 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
3996 /* WINED3DSIH_BREAKP */ NULL,
3997 /* WINED3DSIH_CALL */ shader_glsl_call,
3998 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
3999 /* WINED3DSIH_CMP */ shader_glsl_cmp,
4000 /* WINED3DSIH_CND */ shader_glsl_cnd,
4001 /* WINED3DSIH_CRS */ shader_glsl_cross,
4002 /* WINED3DSIH_DCL */ NULL,
4003 /* WINED3DSIH_DEF */ NULL,
4004 /* WINED3DSIH_DEFB */ NULL,
4005 /* WINED3DSIH_DEFI */ NULL,
4006 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add,
4007 /* WINED3DSIH_DP3 */ shader_glsl_dot,
4008 /* WINED3DSIH_DP4 */ shader_glsl_dot,
4009 /* WINED3DSIH_DST */ shader_glsl_dst,
4010 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
4011 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
4012 /* WINED3DSIH_ELSE */ shader_glsl_else,
4013 /* WINED3DSIH_ENDIF */ shader_glsl_end,
4014 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
4015 /* WINED3DSIH_ENDREP */ shader_glsl_end,
4016 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
4017 /* WINED3DSIH_EXPP */ shader_glsl_expp,
4018 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
4019 /* WINED3DSIH_IF */ shader_glsl_if,
4020 /* WINED3DSIH_IFC */ shader_glsl_ifc,
4021 /* WINED3DSIH_LABEL */ shader_glsl_label,
4022 /* WINED3DSIH_LIT */ shader_glsl_lit,
4023 /* WINED3DSIH_LOG */ shader_glsl_log,
4024 /* WINED3DSIH_LOGP */ shader_glsl_log,
4025 /* WINED3DSIH_LOOP */ shader_glsl_loop,
4026 /* WINED3DSIH_LRP */ shader_glsl_lrp,
4027 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
4028 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
4029 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
4030 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
4031 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
4032 /* WINED3DSIH_MAD */ shader_glsl_mad,
4033 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
4034 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
4035 /* WINED3DSIH_MOV */ shader_glsl_mov,
4036 /* WINED3DSIH_MOVA */ shader_glsl_mov,
4037 /* WINED3DSIH_MUL */ shader_glsl_arith,
4038 /* WINED3DSIH_NOP */ NULL,
4039 /* WINED3DSIH_NRM */ shader_glsl_map2gl,
4040 /* WINED3DSIH_PHASE */ NULL,
4041 /* WINED3DSIH_POW */ shader_glsl_pow,
4042 /* WINED3DSIH_RCP */ shader_glsl_rcp,
4043 /* WINED3DSIH_REP */ shader_glsl_rep,
4044 /* WINED3DSIH_RET */ NULL,
4045 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
4046 /* WINED3DSIH_SETP */ NULL,
4047 /* WINED3DSIH_SGE */ shader_glsl_compare,
4048 /* WINED3DSIH_SGN */ shader_glsl_map2gl,
4049 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
4050 /* WINED3DSIH_SLT */ shader_glsl_compare,
4051 /* WINED3DSIH_SUB */ shader_glsl_arith,
4052 /* WINED3DSIH_TEX */ pshader_glsl_tex,
4053 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem,
4054 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem,
4055 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord,
4056 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth,
4057 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3,
4058 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex,
4059 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill,
4060 /* WINED3DSIH_TEXLDD */ NULL,
4061 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
4062 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth,
4063 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad,
4064 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex,
4065 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3,
4066 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
4067 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad,
4068 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec,
4069 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex,
4070 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec,
4071 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar,
4072 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb,
4073 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb,
4076 const shader_backend_t glsl_shader_backend = {
4077 shader_glsl_instruction_handler_table,
4078 shader_glsl_select,
4079 shader_glsl_select_depth_blt,
4080 shader_glsl_deselect_depth_blt,
4081 shader_glsl_update_float_vertex_constants,
4082 shader_glsl_update_float_pixel_constants,
4083 shader_glsl_load_constants,
4084 shader_glsl_color_correction,
4085 shader_glsl_destroy,
4086 shader_glsl_alloc,
4087 shader_glsl_free,
4088 shader_glsl_dirty_const,
4089 shader_glsl_generate_pshader,
4090 shader_glsl_generate_vshader,
4091 shader_glsl_get_caps,
4092 shader_glsl_color_fixup_supported,