VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/glsl_shader.c@ 30585

Last change on this file since 30585 was 28475, checked in by vboxsync, 15 years ago

crOpenGL: update to wine 1.1.43

  • Property svn:eol-style set to native
File size: 202.5 KB
Line 
1/*
2 * GLSL pixel and vertex shader implementation
3 *
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
7 * Copyright 2009 Henri Verbeet for CodeWeavers
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24/*
25 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
26 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
27 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
28 * a choice of LGPL license versions is made available with the language indicating
29 * that LGPLv2 or any later version may be used, or where a choice of which version
30 * of the LGPL is applied is otherwise unspecified.
31 */
32
33/*
34 * D3D shader asm has swizzles on source parameters, and write masks for
35 * destination parameters. GLSL uses swizzles for both. The result of this is
36 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
37 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
38 * mask for the destination parameter into account.
39 */
40
41#include "config.h"
42#include "wine/port.h"
43#include <limits.h>
44#include <stdio.h>
45#include "wined3d_private.h"
46
47WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
48WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
49WINE_DECLARE_DEBUG_CHANNEL(d3d_caps);
50WINE_DECLARE_DEBUG_CHANNEL(d3d);
51
52#define GLINFO_LOCATION (*gl_info)
53
54#define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
55#define WINED3D_GLSL_SAMPLE_RECT 0x2
56#define WINED3D_GLSL_SAMPLE_LOD 0x4
57#define WINED3D_GLSL_SAMPLE_GRAD 0x8
58
59typedef struct {
60 char reg_name[150];
61 char mask_str[6];
62} glsl_dst_param_t;
63
64typedef struct {
65 char reg_name[150];
66 char param_str[200];
67} glsl_src_param_t;
68
69typedef struct {
70 const char *name;
71 DWORD coord_mask;
72} glsl_sample_function_t;
73
74enum heap_node_op
75{
76 HEAP_NODE_TRAVERSE_LEFT,
77 HEAP_NODE_TRAVERSE_RIGHT,
78 HEAP_NODE_POP,
79};
80
81struct constant_entry
82{
83 unsigned int idx;
84 unsigned int version;
85};
86
87struct constant_heap
88{
89 struct constant_entry *entries;
90 unsigned int *positions;
91 unsigned int size;
92};
93
94/* GLSL shader private data */
95struct shader_glsl_priv {
96 struct wined3d_shader_buffer shader_buffer;
97 struct wine_rb_tree program_lookup;
98 struct glsl_shader_prog_link *glsl_program;
99 struct constant_heap vconst_heap;
100 struct constant_heap pconst_heap;
101 unsigned char *stack;
102 GLhandleARB depth_blt_program[tex_type_count];
103 UINT next_constant_version;
104};
105
106/* Struct to maintain data about a linked GLSL program */
107struct glsl_shader_prog_link {
108 struct wine_rb_entry program_lookup_entry;
109 struct list vshader_entry;
110 struct list pshader_entry;
111 GLhandleARB programId;
112 GLint *vuniformF_locations;
113 GLint *puniformF_locations;
114 GLint vuniformI_locations[MAX_CONST_I];
115 GLint puniformI_locations[MAX_CONST_I];
116 GLint posFixup_location;
117 GLint np2Fixup_location;
118 GLint bumpenvmat_location[MAX_TEXTURES];
119 GLint luminancescale_location[MAX_TEXTURES];
120 GLint luminanceoffset_location[MAX_TEXTURES];
121 GLint ycorrection_location;
122 GLenum vertex_color_clamp;
123 IWineD3DVertexShader *vshader;
124 IWineD3DPixelShader *pshader;
125 struct vs_compile_args vs_args;
126 struct ps_compile_args ps_args;
127 UINT constant_version;
128 const struct ps_np2fixup_info *np2Fixup_info;
129};
130
131typedef struct {
132 IWineD3DVertexShader *vshader;
133 IWineD3DPixelShader *pshader;
134 struct ps_compile_args ps_args;
135 struct vs_compile_args vs_args;
136} glsl_program_key_t;
137
138struct shader_glsl_ctx_priv {
139 const struct vs_compile_args *cur_vs_args;
140 const struct ps_compile_args *cur_ps_args;
141 struct ps_np2fixup_info *cur_np2fixup_info;
142};
143
144struct glsl_ps_compiled_shader
145{
146 struct ps_compile_args args;
147 struct ps_np2fixup_info np2fixup;
148 GLhandleARB prgId;
149};
150
151struct glsl_pshader_private
152{
153 struct glsl_ps_compiled_shader *gl_shaders;
154 UINT num_gl_shaders, shader_array_size;
155};
156
157struct glsl_vs_compiled_shader
158{
159 struct vs_compile_args args;
160 GLhandleARB prgId;
161};
162
163struct glsl_vshader_private
164{
165 struct glsl_vs_compiled_shader *gl_shaders;
166 UINT num_gl_shaders, shader_array_size;
167};
168
169static const char *debug_gl_shader_type(GLenum type)
170{
171 switch (type)
172 {
173#define WINED3D_TO_STR(u) case u: return #u
174 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
175 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
176 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
177#undef WINED3D_TO_STR
178 default:
179 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
180 }
181}
182
183/* Extract a line from the info log.
184 * Note that this modifies the source string. */
185static char *get_info_log_line(char **ptr)
186{
187 char *p, *q;
188
189 p = *ptr;
190 if (!(q = strstr(p, "\n")))
191 {
192 if (!*p) return NULL;
193 *ptr += strlen(p);
194 return p;
195 }
196 *q = '\0';
197 *ptr = q + 1;
198
199 return p;
200}
201
202/** Prints the GLSL info log which will contain error messages if they exist */
203/* GL locking is done by the caller */
204static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
205{
206 int infologLength = 0;
207 char *infoLog;
208 unsigned int i;
209 BOOL is_spam;
210
211 static const char * const spam[] =
212 {
213 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
214 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx, with \n */
215 "Fragment shader was successfully compiled to run on hardware.", /* fglrx, no \n */
216 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
217 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
218 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
219 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
220 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
221 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
222 };
223
224 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
225
226 GL_EXTCALL(glGetObjectParameterivARB(obj,
227 GL_OBJECT_INFO_LOG_LENGTH_ARB,
228 &infologLength));
229
230 /* A size of 1 is just a null-terminated string, so the log should be bigger than
231 * that if there are errors. */
232 if (infologLength > 1)
233 {
234 char *ptr, *line;
235
236 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
237 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
238 */
239 infoLog = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, infologLength);
240 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
241 is_spam = FALSE;
242
243 for(i = 0; i < sizeof(spam) / sizeof(spam[0]); i++) {
244 if(strcmp(infoLog, spam[i]) == 0) {
245 is_spam = TRUE;
246 break;
247 }
248 }
249
250 ptr = infoLog;
251 if (is_spam)
252 {
253 TRACE("Spam received from GLSL shader #%u:\n", obj);
254 while ((line = get_info_log_line(&ptr))) TRACE(" %s\n", line);
255 }
256 else
257 {
258 FIXME("Error received from GLSL shader #%u:\n", obj);
259 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
260 }
261 HeapFree(GetProcessHeap(), 0, infoLog);
262 }
263}
264
265/* GL locking is done by the caller. */
266static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
267{
268 GLint i, object_count, source_size;
269 GLhandleARB *objects;
270 char *source = NULL;
271
272 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
273 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
274 if (!objects)
275 {
276 ERR("Failed to allocate object array memory.\n");
277 return;
278 }
279
280 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
281 for (i = 0; i < object_count; ++i)
282 {
283 char *ptr, *line;
284 GLint tmp;
285
286 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
287
288 if (!source || source_size < tmp)
289 {
290 HeapFree(GetProcessHeap(), 0, source);
291
292 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
293 if (!source)
294 {
295 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
296 HeapFree(GetProcessHeap(), 0, objects);
297 return;
298 }
299 source_size = tmp;
300 }
301
302 FIXME("Object %u:\n", objects[i]);
303 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
304 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
305 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
306 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
307 FIXME("\n");
308
309 ptr = source;
310 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
311 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
312 FIXME("\n");
313 }
314
315 HeapFree(GetProcessHeap(), 0, source);
316 HeapFree(GetProcessHeap(), 0, objects);
317}
318
319/* GL locking is done by the caller. */
320static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
321{
322 GLint tmp;
323
324 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
325
326 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
327 if (tmp == GL_PROGRAM_OBJECT_ARB)
328 {
329 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
330 if (!tmp)
331 {
332 FIXME("Program %u link status invalid.\n", program);
333 shader_glsl_dump_program_source(gl_info, program);
334 }
335 }
336
337 print_glsl_info_log(gl_info, program);
338}
339
340/**
341 * Loads (pixel shader) samplers
342 */
343/* GL locking is done by the caller */
344static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
345 DWORD *tex_unit_map, GLhandleARB programId)
346{
347 GLint name_loc;
348 int i;
349 char sampler_name[20];
350
351 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i) {
352 snprintf(sampler_name, sizeof(sampler_name), "Psampler%d", i);
353 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
354 if (name_loc != -1) {
355 DWORD mapped_unit = tex_unit_map[i];
356 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
357 {
358 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
359 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
360 checkGLcall("glUniform1iARB");
361 } else {
362 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
363 }
364 }
365 }
366}
367
368/* GL locking is done by the caller */
369static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
370 DWORD *tex_unit_map, GLhandleARB programId)
371{
372 GLint name_loc;
373 char sampler_name[20];
374 int i;
375
376 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i) {
377 snprintf(sampler_name, sizeof(sampler_name), "Vsampler%d", i);
378 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
379 if (name_loc != -1) {
380 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
381 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
382 {
383 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
384 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
385 checkGLcall("glUniform1iARB");
386 } else {
387 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
388 }
389 }
390 }
391}
392
393/* GL locking is done by the caller */
394static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
395 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
396{
397 int stack_idx = 0;
398 unsigned int heap_idx = 1;
399 unsigned int idx;
400
401 if (heap->entries[heap_idx].version <= version) return;
402
403 idx = heap->entries[heap_idx].idx;
404 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
405 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
406
407 while (stack_idx >= 0)
408 {
409 /* Note that we fall through to the next case statement. */
410 switch(stack[stack_idx])
411 {
412 case HEAP_NODE_TRAVERSE_LEFT:
413 {
414 unsigned int left_idx = heap_idx << 1;
415 if (left_idx < heap->size && heap->entries[left_idx].version > version)
416 {
417 heap_idx = left_idx;
418 idx = heap->entries[heap_idx].idx;
419 if (constant_locations[idx] != -1)
420 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
421
422 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
423 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
424 break;
425 }
426 }
427
428 case HEAP_NODE_TRAVERSE_RIGHT:
429 {
430 unsigned int right_idx = (heap_idx << 1) + 1;
431 if (right_idx < heap->size && heap->entries[right_idx].version > version)
432 {
433 heap_idx = right_idx;
434 idx = heap->entries[heap_idx].idx;
435 if (constant_locations[idx] != -1)
436 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
437
438 stack[stack_idx++] = HEAP_NODE_POP;
439 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
440 break;
441 }
442 }
443
444 case HEAP_NODE_POP:
445 {
446 heap_idx >>= 1;
447 --stack_idx;
448 break;
449 }
450 }
451 }
452 checkGLcall("walk_constant_heap()");
453}
454
455/* GL locking is done by the caller */
456static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
457{
458 GLfloat clamped_constant[4];
459
460 if (location == -1) return;
461
462 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
463 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
464 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
465 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
466
467 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
468}
469
470/* GL locking is done by the caller */
471static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
472 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
473{
474 int stack_idx = 0;
475 unsigned int heap_idx = 1;
476 unsigned int idx;
477
478 if (heap->entries[heap_idx].version <= version) return;
479
480 idx = heap->entries[heap_idx].idx;
481 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
482 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
483
484 while (stack_idx >= 0)
485 {
486 /* Note that we fall through to the next case statement. */
487 switch(stack[stack_idx])
488 {
489 case HEAP_NODE_TRAVERSE_LEFT:
490 {
491 unsigned int left_idx = heap_idx << 1;
492 if (left_idx < heap->size && heap->entries[left_idx].version > version)
493 {
494 heap_idx = left_idx;
495 idx = heap->entries[heap_idx].idx;
496 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
497
498 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
499 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
500 break;
501 }
502 }
503
504 case HEAP_NODE_TRAVERSE_RIGHT:
505 {
506 unsigned int right_idx = (heap_idx << 1) + 1;
507 if (right_idx < heap->size && heap->entries[right_idx].version > version)
508 {
509 heap_idx = right_idx;
510 idx = heap->entries[heap_idx].idx;
511 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
512
513 stack[stack_idx++] = HEAP_NODE_POP;
514 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
515 break;
516 }
517 }
518
519 case HEAP_NODE_POP:
520 {
521 heap_idx >>= 1;
522 --stack_idx;
523 break;
524 }
525 }
526 }
527 checkGLcall("walk_constant_heap_clamped()");
528}
529
530/* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
531/* GL locking is done by the caller */
532static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl *This, const struct wined3d_gl_info *gl_info,
533 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
534 unsigned char *stack, UINT version)
535{
536 const local_constant *lconst;
537
538 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
539 if (This->baseShader.reg_maps.shader_version.major == 1
540 && shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type))
541 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
542 else
543 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
544
545 if (!This->baseShader.load_local_constsF)
546 {
547 TRACE("No need to load local float constants for this shader\n");
548 return;
549 }
550
551 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
552 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry)
553 {
554 GLint location = constant_locations[lconst->idx];
555 /* We found this uniform name in the program - go ahead and send the data */
556 if (location != -1) GL_EXTCALL(glUniform4fvARB(location, 1, (const GLfloat *)lconst->value));
557 }
558 checkGLcall("glUniform4fvARB()");
559}
560
561/* Loads integer constants (aka uniforms) into the currently set GLSL program. */
562/* GL locking is done by the caller */
563static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl *This, const struct wined3d_gl_info *gl_info,
564 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
565{
566 unsigned int i;
567 struct list* ptr;
568
569 for (i = 0; constants_set; constants_set >>= 1, ++i)
570 {
571 if (!(constants_set & 1)) continue;
572
573 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
574 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
575
576 /* We found this uniform name in the program - go ahead and send the data */
577 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
578 checkGLcall("glUniform4ivARB");
579 }
580
581 /* Load immediate constants */
582 ptr = list_head(&This->baseShader.constantsI);
583 while (ptr) {
584 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
585 unsigned int idx = lconst->idx;
586 const GLint *values = (const GLint *)lconst->value;
587
588 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
589 values[0], values[1], values[2], values[3]);
590
591 /* We found this uniform name in the program - go ahead and send the data */
592 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
593 checkGLcall("glUniform4ivARB");
594 ptr = list_next(&This->baseShader.constantsI, ptr);
595 }
596}
597
598/* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
599/* GL locking is done by the caller */
600static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl *This, const struct wined3d_gl_info *gl_info,
601 GLhandleARB programId, const BOOL *constants, WORD constants_set)
602{
603 GLint tmp_loc;
604 unsigned int i;
605 char tmp_name[8];
606 const char *prefix;
607 struct list* ptr;
608
609 switch (This->baseShader.reg_maps.shader_version.type)
610 {
611 case WINED3D_SHADER_TYPE_VERTEX:
612 prefix = "VB";
613 break;
614
615 case WINED3D_SHADER_TYPE_GEOMETRY:
616 prefix = "GB";
617 break;
618
619 case WINED3D_SHADER_TYPE_PIXEL:
620 prefix = "PB";
621 break;
622
623 default:
624 FIXME("Unknown shader type %#x.\n",
625 This->baseShader.reg_maps.shader_version.type);
626 prefix = "UB";
627 break;
628 }
629
630 /* TODO: Benchmark and see if it would be beneficial to store the
631 * locations of the constants to avoid looking up each time */
632 for (i = 0; constants_set; constants_set >>= 1, ++i)
633 {
634 if (!(constants_set & 1)) continue;
635
636 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
637
638 /* TODO: Benchmark and see if it would be beneficial to store the
639 * locations of the constants to avoid looking up each time */
640 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, i);
641 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
642 if (tmp_loc != -1)
643 {
644 /* We found this uniform name in the program - go ahead and send the data */
645 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
646 checkGLcall("glUniform1ivARB");
647 }
648 }
649
650 /* Load immediate constants */
651 ptr = list_head(&This->baseShader.constantsB);
652 while (ptr) {
653 const struct local_constant *lconst = LIST_ENTRY(ptr, const struct local_constant, entry);
654 unsigned int idx = lconst->idx;
655 const GLint *values = (const GLint *)lconst->value;
656
657 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
658
659 snprintf(tmp_name, sizeof(tmp_name), "%s[%i]", prefix, idx);
660 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
661 if (tmp_loc != -1) {
662 /* We found this uniform name in the program - go ahead and send the data */
663 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
664 checkGLcall("glUniform1ivARB");
665 }
666 ptr = list_next(&This->baseShader.constantsB, ptr);
667 }
668}
669
670static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
671{
672 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
673}
674
675/**
676 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
677 */
678/* GL locking is done by the caller (state handler) */
679static void shader_glsl_load_np2fixup_constants(
680 IWineD3DDevice* device,
681 char usePixelShader,
682 char useVertexShader) {
683
684 const IWineD3DDeviceImpl* deviceImpl = (const IWineD3DDeviceImpl*) device;
685 const struct glsl_shader_prog_link* prog = ((struct shader_glsl_priv *)(deviceImpl->shader_priv))->glsl_program;
686
687 if (!prog) {
688 /* No GLSL program set - nothing to do. */
689 return;
690 }
691
692 if (!usePixelShader) {
693 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
694 return;
695 }
696
697 if (prog->ps_args.np2_fixup && -1 != prog->np2Fixup_location) {
698 const struct wined3d_gl_info *gl_info = &deviceImpl->adapter->gl_info;
699 const IWineD3DStateBlockImpl* stateBlock = (const IWineD3DStateBlockImpl*) deviceImpl->stateBlock;
700 UINT i;
701 UINT fixup = prog->ps_args.np2_fixup;
702 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
703
704 for (i = 0; fixup; fixup >>= 1, ++i) {
705 const unsigned char idx = prog->np2Fixup_info->idx[i];
706 const IWineD3DBaseTextureImpl* const tex = (const IWineD3DBaseTextureImpl*) stateBlock->textures[i];
707 GLfloat* tex_dim = &np2fixup_constants[(idx >> 1) * 4];
708
709 if (!tex) {
710 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
711 continue;
712 }
713
714 if (idx % 2) {
715 tex_dim[2] = tex->baseTexture.pow2Matrix[0]; tex_dim[3] = tex->baseTexture.pow2Matrix[5];
716 } else {
717 tex_dim[0] = tex->baseTexture.pow2Matrix[0]; tex_dim[1] = tex->baseTexture.pow2Matrix[5];
718 }
719 }
720
721 GL_EXTCALL(glUniform4fvARB(prog->np2Fixup_location, prog->np2Fixup_info->num_consts, np2fixup_constants));
722 }
723}
724
725/**
726 * Loads the app-supplied constants into the currently set GLSL program.
727 */
728/* GL locking is done by the caller (state handler) */
729static void shader_glsl_load_constants(const struct wined3d_context *context,
730 char usePixelShader, char useVertexShader)
731{
732 const struct wined3d_gl_info *gl_info = context->gl_info;
733 IWineD3DDeviceImpl *device = context->swapchain->device;
734 IWineD3DStateBlockImpl* stateBlock = device->stateBlock;
735 struct shader_glsl_priv *priv = device->shader_priv;
736
737 GLhandleARB programId;
738 struct glsl_shader_prog_link *prog = priv->glsl_program;
739 UINT constant_version;
740 int i;
741
742 if (!prog) {
743 /* No GLSL program set - nothing to do. */
744 return;
745 }
746 programId = prog->programId;
747 constant_version = prog->constant_version;
748
749 if (useVertexShader) {
750 IWineD3DBaseShaderImpl* vshader = (IWineD3DBaseShaderImpl*) stateBlock->vertexShader;
751
752 /* Load DirectX 9 float constants/uniforms for vertex shader */
753 shader_glsl_load_constantsF(vshader, gl_info, stateBlock->vertexShaderConstantF,
754 prog->vuniformF_locations, &priv->vconst_heap, priv->stack, constant_version);
755
756 /* Load DirectX 9 integer constants/uniforms for vertex shader */
757 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations, stateBlock->vertexShaderConstantI,
758 stateBlock->changed.vertexShaderConstantsI & vshader->baseShader.reg_maps.integer_constants);
759
760 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
761 shader_glsl_load_constantsB(vshader, gl_info, programId, stateBlock->vertexShaderConstantB,
762 stateBlock->changed.vertexShaderConstantsB & vshader->baseShader.reg_maps.boolean_constants);
763
764 /* Upload the position fixup params */
765 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, &device->posFixup[0]));
766 checkGLcall("glUniform4fvARB");
767 }
768
769 if (usePixelShader) {
770
771 IWineD3DBaseShaderImpl* pshader = (IWineD3DBaseShaderImpl*) stateBlock->pixelShader;
772
773 /* Load DirectX 9 float constants/uniforms for pixel shader */
774 shader_glsl_load_constantsF(pshader, gl_info, stateBlock->pixelShaderConstantF,
775 prog->puniformF_locations, &priv->pconst_heap, priv->stack, constant_version);
776
777 /* Load DirectX 9 integer constants/uniforms for pixel shader */
778 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations, stateBlock->pixelShaderConstantI,
779 stateBlock->changed.pixelShaderConstantsI & pshader->baseShader.reg_maps.integer_constants);
780
781 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
782 shader_glsl_load_constantsB(pshader, gl_info, programId, stateBlock->pixelShaderConstantB,
783 stateBlock->changed.pixelShaderConstantsB & pshader->baseShader.reg_maps.boolean_constants);
784
785 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
786 * It can't be 0 for a valid texbem instruction.
787 */
788 for(i = 0; i < MAX_TEXTURES; i++) {
789 const float *data;
790
791 if(prog->bumpenvmat_location[i] == -1) continue;
792
793 data = (const float *)&stateBlock->textureState[i][WINED3DTSS_BUMPENVMAT00];
794 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
795 checkGLcall("glUniformMatrix2fvARB");
796
797 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
798 * is set too, so we can check that in the needsbumpmat check
799 */
800 if(prog->luminancescale_location[i] != -1) {
801 const GLfloat *scale = (const GLfloat *)&stateBlock->textureState[i][WINED3DTSS_BUMPENVLSCALE];
802 const GLfloat *offset = (const GLfloat *)&stateBlock->textureState[i][WINED3DTSS_BUMPENVLOFFSET];
803
804 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
805 checkGLcall("glUniform1fvARB");
806 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
807 checkGLcall("glUniform1fvARB");
808 }
809 }
810
811 if(((IWineD3DPixelShaderImpl *) pshader)->vpos_uniform) {
812 float correction_params[4];
813
814 if (context->render_offscreen)
815 {
816 correction_params[0] = 0.0f;
817 correction_params[1] = 1.0f;
818 } else {
819 /* position is window relative, not viewport relative */
820 correction_params[0] = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Height;
821 correction_params[1] = -1.0f;
822 }
823 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
824 }
825 }
826
827 if (priv->next_constant_version == UINT_MAX)
828 {
829 TRACE("Max constant version reached, resetting to 0.\n");
830 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
831 priv->next_constant_version = 1;
832 }
833 else
834 {
835 prog->constant_version = priv->next_constant_version++;
836 }
837}
838
839static inline void update_heap_entry(struct constant_heap *heap, unsigned int idx,
840 unsigned int heap_idx, DWORD new_version)
841{
842 struct constant_entry *entries = heap->entries;
843 unsigned int *positions = heap->positions;
844 unsigned int parent_idx;
845
846 while (heap_idx > 1)
847 {
848 parent_idx = heap_idx >> 1;
849
850 if (new_version <= entries[parent_idx].version) break;
851
852 entries[heap_idx] = entries[parent_idx];
853 positions[entries[parent_idx].idx] = heap_idx;
854 heap_idx = parent_idx;
855 }
856
857 entries[heap_idx].version = new_version;
858 entries[heap_idx].idx = idx;
859 positions[idx] = heap_idx;
860}
861
862static void shader_glsl_update_float_vertex_constants(IWineD3DDevice *iface, UINT start, UINT count)
863{
864 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
865 struct shader_glsl_priv *priv = This->shader_priv;
866 struct constant_heap *heap = &priv->vconst_heap;
867 UINT i;
868
869 for (i = start; i < count + start; ++i)
870 {
871 if (!This->stateBlock->changed.vertexShaderConstantsF[i])
872 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
873 else
874 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
875 }
876}
877
878static void shader_glsl_update_float_pixel_constants(IWineD3DDevice *iface, UINT start, UINT count)
879{
880 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
881 struct shader_glsl_priv *priv = This->shader_priv;
882 struct constant_heap *heap = &priv->pconst_heap;
883 UINT i;
884
885 for (i = start; i < count + start; ++i)
886 {
887 if (!This->stateBlock->changed.pixelShaderConstantsF[i])
888 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
889 else
890 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
891 }
892}
893
894static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
895{
896 unsigned int ret = gl_info->limits.glsl_varyings / 4;
897 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
898 if(shader_major > 3) return ret;
899
900 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
901 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
902 return ret;
903}
904
905/** Generate the variable & register declarations for the GLSL output target */
906static void shader_generate_glsl_declarations(const struct wined3d_context *context,
907 struct wined3d_shader_buffer *buffer, IWineD3DBaseShader *iface,
908 const shader_reg_maps *reg_maps, struct shader_glsl_ctx_priv *ctx_priv)
909{
910 IWineD3DBaseShaderImpl* This = (IWineD3DBaseShaderImpl*) iface;
911 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->baseShader.device;
912 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
913 const struct wined3d_gl_info *gl_info = context->gl_info;
914 unsigned int i, extra_constants_needed = 0;
915 const local_constant *lconst;
916 DWORD map;
917
918 /* There are some minor differences between pixel and vertex shaders */
919 char pshader = shader_is_pshader_version(reg_maps->shader_version.type);
920 char prefix = pshader ? 'P' : 'V';
921
922 /* Prototype the subroutines */
923 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
924 {
925 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
926 }
927
928 /* Declare the constants (aka uniforms) */
929 if (This->baseShader.limits.constant_float > 0) {
930 unsigned max_constantsF;
931 /* Unless the shader uses indirect addressing, always declare the maximum array size and ignore that we need some
932 * uniforms privately. E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup and immediate values, still
933 * declare VC[256]. If the shader needs more uniforms than we have it won't work in any case. If it uses less, the
934 * compiler will figure out which uniforms are really used and strip them out. This allows a shader to use c255 on
935 * a dx9 card, as long as it doesn't also use all the other constants.
936 *
937 * If the shader uses indirect addressing the compiler must assume that all declared uniforms are used. In this case,
938 * declare only the amount that we're assured to have.
939 *
940 * Thus we run into problems in these two cases:
941 * 1) The shader really uses more uniforms than supported
942 * 2) The shader uses indirect addressing, less constants than supported, but uses a constant index > #supported consts
943 */
944 if (pshader)
945 {
946 /* No indirect addressing here. */
947 max_constantsF = gl_info->limits.glsl_ps_float_constants;
948 }
949 else
950 {
951 if(This->baseShader.reg_maps.usesrelconstF) {
952 /* Subtract the other potential uniforms from the max available (bools, ints, and 1 row of projection matrix).
953 * Subtract another uniform for immediate values, which have to be loaded via uniform by the driver as well.
954 * The shader code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex shader code, so one vec4 should be enough
955 * (Unfortunately the Nvidia driver doesn't store 128 and -128 in one float).
956 *
957 * Writing gl_ClipVertex requires one uniform for each clipplane as well.
958 */
959 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
960 if(ctx_priv->cur_vs_args->clip_enabled)
961 {
962 max_constantsF -= gl_info->limits.clipplanes;
963 }
964 max_constantsF -= count_bits(This->baseShader.reg_maps.integer_constants);
965 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
966 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
967 * for now take this into account when calculating the number of available constants
968 */
969 max_constantsF -= count_bits(This->baseShader.reg_maps.boolean_constants);
970 /* Set by driver quirks in directx.c */
971 max_constantsF -= gl_info->reserved_glsl_constants;
972 }
973 else
974 {
975 max_constantsF = gl_info->limits.glsl_vs_float_constants;
976 }
977 }
978 max_constantsF = min(This->baseShader.limits.constant_float, max_constantsF);
979 shader_addline(buffer, "uniform vec4 %cC[%u];\n", prefix, max_constantsF);
980 }
981
982 /* Always declare the full set of constants, the compiler can remove the unused ones because d3d doesn't(yet)
983 * support indirect int and bool constant addressing. This avoids problems if the app uses e.g. i0 and i9.
984 */
985 if (This->baseShader.limits.constant_int > 0 && This->baseShader.reg_maps.integer_constants)
986 shader_addline(buffer, "uniform ivec4 %cI[%u];\n", prefix, This->baseShader.limits.constant_int);
987
988 if (This->baseShader.limits.constant_bool > 0 && This->baseShader.reg_maps.boolean_constants)
989 shader_addline(buffer, "uniform bool %cB[%u];\n", prefix, This->baseShader.limits.constant_bool);
990
991 if(!pshader) {
992 shader_addline(buffer, "uniform vec4 posFixup;\n");
993 /* Predeclaration; This function is added at link time based on the pixel shader.
994 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
995 * that. We know the input to the reorder function at vertex shader compile time, so
996 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
997 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
998 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
999 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
1000 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
1001 * inout.
1002 */
1003 if (reg_maps->shader_version.major >= 3)
1004 {
1005 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT);
1006 } else {
1007 shader_addline(buffer, "void order_ps_input();\n");
1008 }
1009 } else {
1010 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1011 {
1012 if (!(map & 1)) continue;
1013
1014 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
1015
1016 if (reg_maps->luminanceparams & (1 << i))
1017 {
1018 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
1019 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
1020 extra_constants_needed++;
1021 }
1022
1023 extra_constants_needed++;
1024 }
1025
1026 if (ps_args->srgb_correction)
1027 {
1028 shader_addline(buffer, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1029 srgb_pow, srgb_mul_high, srgb_sub_high, srgb_mul_low);
1030 shader_addline(buffer, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
1031 srgb_cmp);
1032 }
1033 if (reg_maps->vpos || reg_maps->usesdsy)
1034 {
1035 if (This->baseShader.limits.constant_float + extra_constants_needed
1036 + 1 < gl_info->limits.glsl_ps_float_constants)
1037 {
1038 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1039 ((IWineD3DPixelShaderImpl *) This)->vpos_uniform = 1;
1040 extra_constants_needed++;
1041 } else {
1042 /* This happens because we do not have proper tracking of the constant registers that are
1043 * actually used, only the max limit of the shader version
1044 */
1045 FIXME("Cannot find a free uniform for vpos correction params\n");
1046 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
1047 context->render_offscreen ? 0.0f : ((IWineD3DSurfaceImpl *)device->render_targets[0])->currentDesc.Height,
1048 context->render_offscreen ? 1.0f : -1.0f);
1049 }
1050 shader_addline(buffer, "vec4 vpos;\n");
1051 }
1052 }
1053
1054 /* Declare texture samplers */
1055 for (i = 0; i < This->baseShader.limits.sampler; i++) {
1056 if (reg_maps->sampler_type[i])
1057 {
1058 switch (reg_maps->sampler_type[i])
1059 {
1060 case WINED3DSTT_1D:
1061 shader_addline(buffer, "uniform sampler1D %csampler%u;\n", prefix, i);
1062 break;
1063 case WINED3DSTT_2D:
1064 if(device->stateBlock->textures[i] &&
1065 IWineD3DBaseTexture_GetTextureDimensions(device->stateBlock->textures[i]) == GL_TEXTURE_RECTANGLE_ARB) {
1066 shader_addline(buffer, "uniform sampler2DRect %csampler%u;\n", prefix, i);
1067 } else {
1068 shader_addline(buffer, "uniform sampler2D %csampler%u;\n", prefix, i);
1069 }
1070 break;
1071 case WINED3DSTT_CUBE:
1072 shader_addline(buffer, "uniform samplerCube %csampler%u;\n", prefix, i);
1073 break;
1074 case WINED3DSTT_VOLUME:
1075 shader_addline(buffer, "uniform sampler3D %csampler%u;\n", prefix, i);
1076 break;
1077 default:
1078 shader_addline(buffer, "uniform unsupported_sampler %csampler%u;\n", prefix, i);
1079 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1080 break;
1081 }
1082 }
1083 }
1084
1085 /* Declare uniforms for NP2 texcoord fixup:
1086 * This is NOT done inside the loop that declares the texture samplers since the NP2 fixup code
1087 * is currently only used for the GeforceFX series and when forcing the ARB_npot extension off.
1088 * Modern cards just skip the code anyway, so put it inside a separate loop. */
1089 if (pshader && ps_args->np2_fixup) {
1090
1091 struct ps_np2fixup_info* const fixup = ctx_priv->cur_np2fixup_info;
1092 UINT cur = 0;
1093
1094 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1095 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1096 * samplerNP2Fixup stores texture dimensions and is updated through
1097 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1098
1099 for (i = 0; i < This->baseShader.limits.sampler; ++i) {
1100 if (reg_maps->sampler_type[i]) {
1101 if (!(ps_args->np2_fixup & (1 << i))) continue;
1102
1103 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1104 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1105 continue;
1106 }
1107
1108 fixup->idx[i] = cur++;
1109 }
1110 }
1111
1112 fixup->num_consts = (cur + 1) >> 1;
1113 shader_addline(buffer, "uniform vec4 %csamplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1114 }
1115
1116 /* Declare address variables */
1117 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1118 {
1119 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1120 }
1121
1122 /* Declare texture coordinate temporaries and initialize them */
1123 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1124 {
1125 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1126 }
1127
1128 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
1129 * helper function shader that is linked in at link time
1130 */
1131 if (pshader && reg_maps->shader_version.major >= 3)
1132 {
1133 if (use_vs(device->stateBlock))
1134 {
1135 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(reg_maps->shader_version.major, gl_info));
1136 } else {
1137 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
1138 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
1139 * pixel shader that reads the fixed function color into the packed input registers.
1140 */
1141 shader_addline(buffer, "vec4 IN[%u];\n", vec4_varyings(reg_maps->shader_version.major, gl_info));
1142 }
1143 }
1144
1145 /* Declare output register temporaries */
1146 if(This->baseShader.limits.packed_output) {
1147 shader_addline(buffer, "vec4 OUT[%u];\n", This->baseShader.limits.packed_output);
1148 }
1149
1150 /* Declare temporary variables */
1151 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1152 {
1153 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1154 }
1155
1156 /* Declare attributes */
1157 if (reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX)
1158 {
1159 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1160 {
1161 if (map & 1) shader_addline(buffer, "attribute vec4 attrib%i;\n", i);
1162 }
1163 }
1164
1165 /* Declare loop registers aLx */
1166 for (i = 0; i < reg_maps->loop_depth; i++) {
1167 shader_addline(buffer, "int aL%u;\n", i);
1168 shader_addline(buffer, "int tmpInt%u;\n", i);
1169 }
1170
1171 /* Temporary variables for matrix operations */
1172 shader_addline(buffer, "vec4 tmp0;\n");
1173 shader_addline(buffer, "vec4 tmp1;\n");
1174
1175 /* Local constants use a different name so they can be loaded once at shader link time
1176 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
1177 * float -> string conversion can cause precision loss.
1178 */
1179 if(!This->baseShader.load_local_constsF) {
1180 LIST_FOR_EACH_ENTRY(lconst, &This->baseShader.constantsF, local_constant, entry) {
1181 shader_addline(buffer, "uniform vec4 %cLC%u;\n", prefix, lconst->idx);
1182 }
1183 }
1184
1185 shader_addline(buffer, "const float FLT_MAX = 1e38;\n");
1186
1187 /* Start the main program */
1188 shader_addline(buffer, "void main() {\n");
1189 if(pshader && reg_maps->vpos) {
1190 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
1191 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
1192 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
1193 * precision troubles when we just substract 0.5.
1194 *
1195 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
1196 *
1197 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
1198 *
1199 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
1200 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
1201 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
1202 * correctly on drivers that returns integer values.
1203 */
1204 shader_addline(buffer, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1205 }
1206}
1207
1208/*****************************************************************************
1209 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1210 *
1211 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1212 ****************************************************************************/
1213
1214/* Prototypes */
1215static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1216 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, glsl_src_param_t *glsl_src);
1217
1218/** Used for opcode modifiers - They multiply the result by the specified amount */
1219static const char * const shift_glsl_tab[] = {
1220 "", /* 0 (none) */
1221 "2.0 * ", /* 1 (x2) */
1222 "4.0 * ", /* 2 (x4) */
1223 "8.0 * ", /* 3 (x8) */
1224 "16.0 * ", /* 4 (x16) */
1225 "32.0 * ", /* 5 (x32) */
1226 "", /* 6 (x64) */
1227 "", /* 7 (x128) */
1228 "", /* 8 (d256) */
1229 "", /* 9 (d128) */
1230 "", /* 10 (d64) */
1231 "", /* 11 (d32) */
1232 "0.0625 * ", /* 12 (d16) */
1233 "0.125 * ", /* 13 (d8) */
1234 "0.25 * ", /* 14 (d4) */
1235 "0.5 * " /* 15 (d2) */
1236};
1237
1238/* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1239static void shader_glsl_gen_modifier(DWORD src_modifier, const char *in_reg, const char *in_regswizzle, char *out_str)
1240{
1241 out_str[0] = 0;
1242
1243 switch (src_modifier)
1244 {
1245 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1246 case WINED3DSPSM_DW:
1247 case WINED3DSPSM_NONE:
1248 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1249 break;
1250 case WINED3DSPSM_NEG:
1251 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1252 break;
1253 case WINED3DSPSM_NOT:
1254 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1255 break;
1256 case WINED3DSPSM_BIAS:
1257 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1258 break;
1259 case WINED3DSPSM_BIASNEG:
1260 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1261 break;
1262 case WINED3DSPSM_SIGN:
1263 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1264 break;
1265 case WINED3DSPSM_SIGNNEG:
1266 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1267 break;
1268 case WINED3DSPSM_COMP:
1269 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1270 break;
1271 case WINED3DSPSM_X2:
1272 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1273 break;
1274 case WINED3DSPSM_X2NEG:
1275 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1276 break;
1277 case WINED3DSPSM_ABS:
1278 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1279 break;
1280 case WINED3DSPSM_ABSNEG:
1281 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1282 break;
1283 default:
1284 FIXME("Unhandled modifier %u\n", src_modifier);
1285 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1286 }
1287}
1288
1289/** Writes the GLSL variable name that corresponds to the register that the
1290 * DX opcode parameter is trying to access */
1291static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1292 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1293{
1294 /* oPos, oFog and oPts in D3D */
1295 static const char * const hwrastout_reg_names[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
1296
1297 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
1298 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1299 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
1300
1301 *is_color = FALSE;
1302
1303 switch (reg->type)
1304 {
1305 case WINED3DSPR_TEMP:
1306 sprintf(register_name, "R%u", reg->idx);
1307 break;
1308
1309 case WINED3DSPR_INPUT:
1310 /* vertex shaders */
1311 if (!pshader)
1312 {
1313 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1314 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx)) *is_color = TRUE;
1315 sprintf(register_name, "attrib%u", reg->idx);
1316 break;
1317 }
1318
1319 /* pixel shaders >= 3.0 */
1320 if (This->baseShader.reg_maps.shader_version.major >= 3)
1321 {
1322 DWORD idx = ((IWineD3DPixelShaderImpl *)This)->input_reg_map[reg->idx];
1323 unsigned int in_count = vec4_varyings(This->baseShader.reg_maps.shader_version.major, gl_info);
1324
1325 if (reg->rel_addr)
1326 {
1327 glsl_src_param_t rel_param;
1328
1329 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1330
1331 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1332 * operation there */
1333 if (idx)
1334 {
1335 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count)
1336 {
1337 sprintf(register_name,
1338 "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1339 rel_param.param_str, idx, in_count - 1, rel_param.param_str, idx, in_count,
1340 rel_param.param_str, idx);
1341 }
1342 else
1343 {
1344 sprintf(register_name, "IN[%s + %u]", rel_param.param_str, idx);
1345 }
1346 }
1347 else
1348 {
1349 if (((IWineD3DPixelShaderImpl *)This)->declared_in_count > in_count)
1350 {
1351 sprintf(register_name, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1352 rel_param.param_str, in_count - 1, rel_param.param_str, in_count,
1353 rel_param.param_str);
1354 }
1355 else
1356 {
1357 sprintf(register_name, "IN[%s]", rel_param.param_str);
1358 }
1359 }
1360 }
1361 else
1362 {
1363 if (idx == in_count) sprintf(register_name, "gl_Color");
1364 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1365 else sprintf(register_name, "IN[%u]", idx);
1366 }
1367 }
1368 else
1369 {
1370 if (reg->idx == 0) strcpy(register_name, "gl_Color");
1371 else strcpy(register_name, "gl_SecondaryColor");
1372 break;
1373 }
1374 break;
1375
1376 case WINED3DSPR_CONST:
1377 {
1378 const char prefix = pshader ? 'P' : 'V';
1379
1380 /* Relative addressing */
1381 if (reg->rel_addr)
1382 {
1383 glsl_src_param_t rel_param;
1384 shader_glsl_add_src_param(ins, reg->rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1385 if (reg->idx) sprintf(register_name, "%cC[%s + %u]", prefix, rel_param.param_str, reg->idx);
1386 else sprintf(register_name, "%cC[%s]", prefix, rel_param.param_str);
1387 }
1388 else
1389 {
1390 if (shader_constant_is_local(This, reg->idx))
1391 sprintf(register_name, "%cLC%u", prefix, reg->idx);
1392 else
1393 sprintf(register_name, "%cC[%u]", prefix, reg->idx);
1394 }
1395 }
1396 break;
1397
1398 case WINED3DSPR_CONSTINT:
1399 if (pshader) sprintf(register_name, "PI[%u]", reg->idx);
1400 else sprintf(register_name, "VI[%u]", reg->idx);
1401 break;
1402
1403 case WINED3DSPR_CONSTBOOL:
1404 if (pshader) sprintf(register_name, "PB[%u]", reg->idx);
1405 else sprintf(register_name, "VB[%u]", reg->idx);
1406 break;
1407
1408 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1409 if (pshader) sprintf(register_name, "T%u", reg->idx);
1410 else sprintf(register_name, "A%u", reg->idx);
1411 break;
1412
1413 case WINED3DSPR_LOOP:
1414 sprintf(register_name, "aL%u", This->baseShader.cur_loop_regno - 1);
1415 break;
1416
1417 case WINED3DSPR_SAMPLER:
1418 if (pshader) sprintf(register_name, "Psampler%u", reg->idx);
1419 else sprintf(register_name, "Vsampler%u", reg->idx);
1420 break;
1421
1422 case WINED3DSPR_COLOROUT:
1423 if (reg->idx >= gl_info->limits.buffers)
1424 WARN("Write to render target %u, only %d supported.\n", reg->idx, gl_info->limits.buffers);
1425
1426 sprintf(register_name, "gl_FragData[%u]", reg->idx);
1427 break;
1428
1429 case WINED3DSPR_RASTOUT:
1430 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx]);
1431 break;
1432
1433 case WINED3DSPR_DEPTHOUT:
1434 sprintf(register_name, "gl_FragDepth");
1435 break;
1436
1437 case WINED3DSPR_ATTROUT:
1438 if (reg->idx == 0) sprintf(register_name, "gl_FrontColor");
1439 else sprintf(register_name, "gl_FrontSecondaryColor");
1440 break;
1441
1442 case WINED3DSPR_TEXCRDOUT:
1443 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1444 if (This->baseShader.reg_maps.shader_version.major >= 3) sprintf(register_name, "OUT[%u]", reg->idx);
1445 else sprintf(register_name, "gl_TexCoord[%u]", reg->idx);
1446 break;
1447
1448 case WINED3DSPR_MISCTYPE:
1449 if (reg->idx == 0)
1450 {
1451 /* vPos */
1452 sprintf(register_name, "vpos");
1453 }
1454 else if (reg->idx == 1)
1455 {
1456 /* Note that gl_FrontFacing is a bool, while vFace is
1457 * a float for which the sign determines front/back */
1458 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1459 }
1460 else
1461 {
1462 FIXME("Unhandled misctype register %d\n", reg->idx);
1463 sprintf(register_name, "unrecognized_register");
1464 }
1465 break;
1466
1467 case WINED3DSPR_IMMCONST:
1468 switch (reg->immconst_type)
1469 {
1470 case WINED3D_IMMCONST_FLOAT:
1471 sprintf(register_name, "%.8e", *(const float *)reg->immconst_data);
1472 break;
1473
1474 case WINED3D_IMMCONST_FLOAT4:
1475 sprintf(register_name, "vec4(%.8e, %.8e, %.8e, %.8e)",
1476 *(const float *)&reg->immconst_data[0], *(const float *)&reg->immconst_data[1],
1477 *(const float *)&reg->immconst_data[2], *(const float *)&reg->immconst_data[3]);
1478 break;
1479
1480 default:
1481 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1482 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1483 }
1484 break;
1485
1486 default:
1487 FIXME("Unhandled register name Type(%d)\n", reg->type);
1488 sprintf(register_name, "unrecognized_register");
1489 break;
1490 }
1491}
1492
1493static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1494{
1495 *str++ = '.';
1496 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1497 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1498 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1499 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1500 *str = '\0';
1501}
1502
1503/* Get the GLSL write mask for the destination register */
1504static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1505{
1506 DWORD mask = param->write_mask;
1507
1508 if (shader_is_scalar(&param->reg))
1509 {
1510 mask = WINED3DSP_WRITEMASK_0;
1511 *write_mask = '\0';
1512 }
1513 else
1514 {
1515 shader_glsl_write_mask_to_str(mask, write_mask);
1516 }
1517
1518 return mask;
1519}
1520
1521static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1522 unsigned int size = 0;
1523
1524 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1525 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1526 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1527 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1528
1529 return size;
1530}
1531
1532static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1533{
1534 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1535 * but addressed as "rgba". To fix this we need to swap the register's x
1536 * and z components. */
1537 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1538
1539 *str++ = '.';
1540 /* swizzle bits fields: wwzzyyxx */
1541 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1542 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1543 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1544 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1545 *str = '\0';
1546}
1547
1548static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1549 BOOL fixup, DWORD mask, char *swizzle_str)
1550{
1551 if (shader_is_scalar(&param->reg))
1552 *swizzle_str = '\0';
1553 else
1554 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1555}
1556
1557/* From a given parameter token, generate the corresponding GLSL string.
1558 * Also, return the actual register name and swizzle in case the
1559 * caller needs this information as well. */
1560static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1561 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, glsl_src_param_t *glsl_src)
1562{
1563 BOOL is_color = FALSE;
1564 char swizzle_str[6];
1565
1566 glsl_src->reg_name[0] = '\0';
1567 glsl_src->param_str[0] = '\0';
1568 swizzle_str[0] = '\0';
1569
1570 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1571 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1572 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1573}
1574
1575/* From a given parameter token, generate the corresponding GLSL string.
1576 * Also, return the actual register name and swizzle in case the
1577 * caller needs this information as well. */
1578static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1579 const struct wined3d_shader_dst_param *wined3d_dst, glsl_dst_param_t *glsl_dst)
1580{
1581 BOOL is_color = FALSE;
1582
1583 glsl_dst->mask_str[0] = '\0';
1584 glsl_dst->reg_name[0] = '\0';
1585
1586 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1587 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1588}
1589
1590/* Append the destination part of the instruction to the buffer, return the effective write mask */
1591static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1592 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1593{
1594 glsl_dst_param_t glsl_dst;
1595 DWORD mask;
1596
1597 mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst);
1598 if (mask) shader_addline(buffer, "%s%s = %s(", glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1599
1600 return mask;
1601}
1602
1603/* Append the destination part of the instruction to the buffer, return the effective write mask */
1604static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1605{
1606 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1607}
1608
1609/** Process GLSL instruction modifiers */
1610static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1611{
1612 glsl_dst_param_t dst_param;
1613 DWORD modifiers;
1614
1615 if (!ins->dst_count) return;
1616
1617 modifiers = ins->dst[0].modifiers;
1618 if (!modifiers) return;
1619
1620 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1621
1622 if (modifiers & WINED3DSPDM_SATURATE)
1623 {
1624 /* _SAT means to clamp the value of the register to between 0 and 1 */
1625 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1626 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1627 }
1628
1629 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1630 {
1631 FIXME("_centroid modifier not handled\n");
1632 }
1633
1634 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1635 {
1636 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1637 }
1638}
1639
1640static inline const char *shader_get_comp_op(DWORD op)
1641{
1642 switch (op) {
1643 case COMPARISON_GT: return ">";
1644 case COMPARISON_EQ: return "==";
1645 case COMPARISON_GE: return ">=";
1646 case COMPARISON_LT: return "<";
1647 case COMPARISON_NE: return "!=";
1648 case COMPARISON_LE: return "<=";
1649 default:
1650 FIXME("Unrecognized comparison value: %u\n", op);
1651 return "(\?\?)";
1652 }
1653}
1654
1655static void shader_glsl_get_sample_function(const struct wined3d_gl_info *gl_info,
1656 DWORD sampler_type, DWORD flags, glsl_sample_function_t *sample_function)
1657{
1658 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1659 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_RECT;
1660 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1661 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1662
1663 /* Note that there's no such thing as a projected cube texture. */
1664 switch(sampler_type) {
1665 case WINED3DSTT_1D:
1666 if(lod) {
1667 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1668 }
1669 else if (grad)
1670 {
1671 if (gl_info->supported[EXT_GPU_SHADER4])
1672 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1673 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1674 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1675 else
1676 {
1677 FIXME("Unsupported 1D grad function.\n");
1678 sample_function->name = "unsupported1DGrad";
1679 }
1680 }
1681 else
1682 {
1683 sample_function->name = projected ? "texture1DProj" : "texture1D";
1684 }
1685 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1686 break;
1687 case WINED3DSTT_2D:
1688 if(texrect) {
1689 if(lod) {
1690 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1691 }
1692 else if (grad)
1693 {
1694 if (gl_info->supported[EXT_GPU_SHADER4])
1695 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
1696 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1697 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
1698 else
1699 {
1700 FIXME("Unsupported RECT grad function.\n");
1701 sample_function->name = "unsupported2DRectGrad";
1702 }
1703 }
1704 else
1705 {
1706 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1707 }
1708 } else {
1709 if(lod) {
1710 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
1711 }
1712 else if (grad)
1713 {
1714 if (gl_info->supported[EXT_GPU_SHADER4])
1715 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
1716 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1717 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
1718 else
1719 {
1720 FIXME("Unsupported 2D grad function.\n");
1721 sample_function->name = "unsupported2DGrad";
1722 }
1723 }
1724 else
1725 {
1726 sample_function->name = projected ? "texture2DProj" : "texture2D";
1727 }
1728 }
1729 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1730 break;
1731 case WINED3DSTT_CUBE:
1732 if(lod) {
1733 sample_function->name = "textureCubeLod";
1734 }
1735 else if (grad)
1736 {
1737 if (gl_info->supported[EXT_GPU_SHADER4])
1738 sample_function->name = "textureCubeGrad";
1739 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1740 sample_function->name = "textureCubeGradARB";
1741 else
1742 {
1743 FIXME("Unsupported Cube grad function.\n");
1744 sample_function->name = "unsupportedCubeGrad";
1745 }
1746 }
1747 else
1748 {
1749 sample_function->name = "textureCube";
1750 }
1751 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1752 break;
1753 case WINED3DSTT_VOLUME:
1754 if(lod) {
1755 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
1756 }
1757 else if (grad)
1758 {
1759 if (gl_info->supported[EXT_GPU_SHADER4])
1760 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
1761 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1762 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
1763 else
1764 {
1765 FIXME("Unsupported 3D grad function.\n");
1766 sample_function->name = "unsupported3DGrad";
1767 }
1768 }
1769 else
1770 {
1771 sample_function->name = projected ? "texture3DProj" : "texture3D";
1772 }
1773 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1774 break;
1775 default:
1776 sample_function->name = "";
1777 sample_function->coord_mask = 0;
1778 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
1779 break;
1780 }
1781}
1782
1783static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
1784 BOOL sign_fixup, enum fixup_channel_source channel_source)
1785{
1786 switch(channel_source)
1787 {
1788 case CHANNEL_SOURCE_ZERO:
1789 strcat(arguments, "0.0");
1790 break;
1791
1792 case CHANNEL_SOURCE_ONE:
1793 strcat(arguments, "1.0");
1794 break;
1795
1796 case CHANNEL_SOURCE_X:
1797 strcat(arguments, reg_name);
1798 strcat(arguments, ".x");
1799 break;
1800
1801 case CHANNEL_SOURCE_Y:
1802 strcat(arguments, reg_name);
1803 strcat(arguments, ".y");
1804 break;
1805
1806 case CHANNEL_SOURCE_Z:
1807 strcat(arguments, reg_name);
1808 strcat(arguments, ".z");
1809 break;
1810
1811 case CHANNEL_SOURCE_W:
1812 strcat(arguments, reg_name);
1813 strcat(arguments, ".w");
1814 break;
1815
1816 default:
1817 FIXME("Unhandled channel source %#x\n", channel_source);
1818 strcat(arguments, "undefined");
1819 break;
1820 }
1821
1822 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
1823}
1824
1825static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
1826{
1827 struct wined3d_shader_dst_param dst;
1828 unsigned int mask_size, remaining;
1829 glsl_dst_param_t dst_param;
1830 char arguments[256];
1831 DWORD mask;
1832
1833 mask = 0;
1834 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
1835 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
1836 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
1837 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
1838 mask &= ins->dst[0].write_mask;
1839
1840 if (!mask) return; /* Nothing to do */
1841
1842 if (is_complex_fixup(fixup))
1843 {
1844 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
1845 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
1846 return;
1847 }
1848
1849 mask_size = shader_glsl_get_write_mask_size(mask);
1850
1851 dst = ins->dst[0];
1852 dst.write_mask = mask;
1853 shader_glsl_add_dst_param(ins, &dst, &dst_param);
1854
1855 arguments[0] = '\0';
1856 remaining = mask_size;
1857 if (mask & WINED3DSP_WRITEMASK_0)
1858 {
1859 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
1860 if (--remaining) strcat(arguments, ", ");
1861 }
1862 if (mask & WINED3DSP_WRITEMASK_1)
1863 {
1864 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
1865 if (--remaining) strcat(arguments, ", ");
1866 }
1867 if (mask & WINED3DSP_WRITEMASK_2)
1868 {
1869 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
1870 if (--remaining) strcat(arguments, ", ");
1871 }
1872 if (mask & WINED3DSP_WRITEMASK_3)
1873 {
1874 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
1875 if (--remaining) strcat(arguments, ", ");
1876 }
1877
1878 if (mask_size > 1)
1879 {
1880 shader_addline(ins->ctx->buffer, "%s%s = vec%u(%s);\n",
1881 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
1882 }
1883 else
1884 {
1885 shader_addline(ins->ctx->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
1886 }
1887}
1888
1889static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
1890 DWORD sampler, const glsl_sample_function_t *sample_function, DWORD swizzle,
1891 const char *dx, const char *dy,
1892 const char *bias, const char *coord_reg_fmt, ...)
1893{
1894 const char *sampler_base;
1895 char dst_swizzle[6];
1896 struct color_fixup_desc fixup;
1897 BOOL np2_fixup = FALSE;
1898 va_list args;
1899
1900 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
1901
1902 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
1903 {
1904 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1905 fixup = priv->cur_ps_args->color_fixup[sampler];
1906 sampler_base = "Psampler";
1907
1908 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
1909 if(bias) {
1910 FIXME("Biased sampling from NP2 textures is unsupported\n");
1911 } else {
1912 np2_fixup = TRUE;
1913 }
1914 }
1915 } else {
1916 sampler_base = "Vsampler";
1917 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
1918 }
1919
1920 shader_glsl_append_dst(ins->ctx->buffer, ins);
1921
1922 shader_addline(ins->ctx->buffer, "%s(%s%u, ", sample_function->name, sampler_base, sampler);
1923
1924 va_start(args, coord_reg_fmt);
1925 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
1926 va_end(args);
1927
1928 if(bias) {
1929 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
1930 } else {
1931 if (np2_fixup) {
1932 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1933 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
1934
1935 shader_addline(ins->ctx->buffer, " * PsamplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
1936 (idx % 2) ? "zw" : "xy", dst_swizzle);
1937 } else if(dx && dy) {
1938 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
1939 } else {
1940 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
1941 }
1942 }
1943
1944 if(!is_identity_fixup(fixup)) {
1945 shader_glsl_color_correction(ins, fixup);
1946 }
1947}
1948
1949/*****************************************************************************
1950 * Begin processing individual instruction opcodes
1951 ****************************************************************************/
1952
1953/* Generate GLSL arithmetic functions (dst = src1 + src2) */
1954static void shader_glsl_arith(const struct wined3d_shader_instruction *ins)
1955{
1956 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1957 glsl_src_param_t src0_param;
1958 glsl_src_param_t src1_param;
1959 DWORD write_mask;
1960 char op;
1961
1962 /* Determine the GLSL operator to use based on the opcode */
1963 switch (ins->handler_idx)
1964 {
1965 case WINED3DSIH_MUL: op = '*'; break;
1966 case WINED3DSIH_ADD: op = '+'; break;
1967 case WINED3DSIH_SUB: op = '-'; break;
1968 default:
1969 op = ' ';
1970 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
1971 break;
1972 }
1973
1974 write_mask = shader_glsl_append_dst(buffer, ins);
1975 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
1976 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
1977 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
1978}
1979
1980/* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1981static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
1982{
1983 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1984 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
1985 glsl_src_param_t src0_param;
1986 DWORD write_mask;
1987
1988 write_mask = shader_glsl_append_dst(buffer, ins);
1989 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
1990
1991 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1992 * shader versions WINED3DSIO_MOVA is used for this. */
1993 if (ins->ctx->reg_maps->shader_version.major == 1
1994 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)
1995 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
1996 {
1997 /* This is a simple floor() */
1998 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
1999 if (mask_size > 1) {
2000 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2001 } else {
2002 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2003 }
2004 }
2005 else if(ins->handler_idx == WINED3DSIH_MOVA)
2006 {
2007 /* We need to *round* to the nearest int here. */
2008 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2009
2010 if (gl_info->supported[EXT_GPU_SHADER4])
2011 {
2012 if (mask_size > 1)
2013 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2014 else
2015 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2016 }
2017 else
2018 {
2019 if (mask_size > 1)
2020 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2021 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2022 else
2023 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2024 src0_param.param_str, src0_param.param_str);
2025 }
2026 }
2027 else
2028 {
2029 shader_addline(buffer, "%s);\n", src0_param.param_str);
2030 }
2031}
2032
2033/* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2034static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2035{
2036 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2037 glsl_src_param_t src0_param;
2038 glsl_src_param_t src1_param;
2039 DWORD dst_write_mask, src_write_mask;
2040 unsigned int dst_size = 0;
2041
2042 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2043 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2044
2045 /* dp3 works on vec3, dp4 on vec4 */
2046 if (ins->handler_idx == WINED3DSIH_DP4)
2047 {
2048 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2049 } else {
2050 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2051 }
2052
2053 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2054 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2055
2056 if (dst_size > 1) {
2057 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2058 } else {
2059 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2060 }
2061}
2062
2063/* Note that this instruction has some restrictions. The destination write mask
2064 * can't contain the w component, and the source swizzles have to be .xyzw */
2065static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2066{
2067 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2068 glsl_src_param_t src0_param;
2069 glsl_src_param_t src1_param;
2070 char dst_mask[6];
2071
2072 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2073 shader_glsl_append_dst(ins->ctx->buffer, ins);
2074 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2075 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2076 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2077}
2078
2079/* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2080 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2081 * GLSL uses the value as-is. */
2082static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2083{
2084 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2085 glsl_src_param_t src0_param;
2086 glsl_src_param_t src1_param;
2087 DWORD dst_write_mask;
2088 unsigned int dst_size;
2089
2090 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2091 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2092
2093 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2094 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2095
2096 if (dst_size > 1) {
2097 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2098 } else {
2099 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
2100 }
2101}
2102
2103/* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2104 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2105 * GLSL uses the value as-is. */
2106static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2107{
2108 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2109 glsl_src_param_t src0_param;
2110 DWORD dst_write_mask;
2111 unsigned int dst_size;
2112
2113 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2114 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2115
2116 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2117
2118 if (dst_size > 1)
2119 {
2120 shader_addline(buffer, "vec%d(%s == 0.0 ? -FLT_MAX : log2(abs(%s))));\n",
2121 dst_size, src0_param.param_str, src0_param.param_str);
2122 }
2123 else
2124 {
2125 shader_addline(buffer, "%s == 0.0 ? -FLT_MAX : log2(abs(%s)));\n",
2126 src0_param.param_str, src0_param.param_str);
2127 }
2128}
2129
2130/* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2131static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2132{
2133 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2134 glsl_src_param_t src_param;
2135 const char *instruction;
2136 DWORD write_mask;
2137 unsigned i;
2138
2139 /* Determine the GLSL function to use based on the opcode */
2140 /* TODO: Possibly make this a table for faster lookups */
2141 switch (ins->handler_idx)
2142 {
2143 case WINED3DSIH_MIN: instruction = "min"; break;
2144 case WINED3DSIH_MAX: instruction = "max"; break;
2145 case WINED3DSIH_ABS: instruction = "abs"; break;
2146 case WINED3DSIH_FRC: instruction = "fract"; break;
2147 case WINED3DSIH_EXP: instruction = "exp2"; break;
2148 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2149 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2150 default: instruction = "";
2151 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2152 break;
2153 }
2154
2155 write_mask = shader_glsl_append_dst(buffer, ins);
2156
2157 shader_addline(buffer, "%s(", instruction);
2158
2159 if (ins->src_count)
2160 {
2161 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2162 shader_addline(buffer, "%s", src_param.param_str);
2163 for (i = 1; i < ins->src_count; ++i)
2164 {
2165 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2166 shader_addline(buffer, ", %s", src_param.param_str);
2167 }
2168 }
2169
2170 shader_addline(buffer, "));\n");
2171}
2172
2173static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2174{
2175 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2176 glsl_src_param_t src_param;
2177 unsigned int mask_size;
2178 DWORD write_mask;
2179 char dst_mask[6];
2180
2181 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2182 mask_size = shader_glsl_get_write_mask_size(write_mask);
2183 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2184
2185 shader_addline(buffer, "tmp0.x = length(%s);\n", src_param.param_str);
2186 shader_glsl_append_dst(buffer, ins);
2187 if (mask_size > 1)
2188 {
2189 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s / tmp0.x));\n",
2190 mask_size, src_param.param_str);
2191 }
2192 else
2193 {
2194 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s / tmp0.x));\n",
2195 src_param.param_str);
2196 }
2197}
2198
2199/** Process the WINED3DSIO_EXPP instruction in GLSL:
2200 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2201 * dst.x = 2^(floor(src))
2202 * dst.y = src - floor(src)
2203 * dst.z = 2^src (partial precision is allowed, but optional)
2204 * dst.w = 1.0;
2205 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2206 * dst = 2^src; (partial precision is allowed, but optional)
2207 */
2208static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2209{
2210 glsl_src_param_t src_param;
2211
2212 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2213
2214 if (ins->ctx->reg_maps->shader_version.major < 2)
2215 {
2216 char dst_mask[6];
2217
2218 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2219 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2220 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2221 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2222
2223 shader_glsl_append_dst(ins->ctx->buffer, ins);
2224 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2225 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2226 } else {
2227 DWORD write_mask;
2228 unsigned int mask_size;
2229
2230 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2231 mask_size = shader_glsl_get_write_mask_size(write_mask);
2232
2233 if (mask_size > 1) {
2234 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2235 } else {
2236 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2237 }
2238 }
2239}
2240
2241/** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2242static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2243{
2244 glsl_src_param_t src_param;
2245 DWORD write_mask;
2246 unsigned int mask_size;
2247
2248 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2249 mask_size = shader_glsl_get_write_mask_size(write_mask);
2250 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2251
2252 if (mask_size > 1)
2253 {
2254 shader_addline(ins->ctx->buffer, "vec%d(%s == 0.0 ? FLT_MAX : 1.0 / %s));\n",
2255 mask_size, src_param.param_str, src_param.param_str);
2256 }
2257 else
2258 {
2259 shader_addline(ins->ctx->buffer, "%s == 0.0 ? FLT_MAX : 1.0 / %s);\n",
2260 src_param.param_str, src_param.param_str);
2261 }
2262}
2263
2264static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2265{
2266 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2267 glsl_src_param_t src_param;
2268 DWORD write_mask;
2269 unsigned int mask_size;
2270
2271 write_mask = shader_glsl_append_dst(buffer, ins);
2272 mask_size = shader_glsl_get_write_mask_size(write_mask);
2273
2274 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2275
2276 if (mask_size > 1)
2277 {
2278 shader_addline(buffer, "vec%d(%s == 0.0 ? FLT_MAX : inversesqrt(abs(%s))));\n",
2279 mask_size, src_param.param_str, src_param.param_str);
2280 }
2281 else
2282 {
2283 shader_addline(buffer, "%s == 0.0 ? FLT_MAX : inversesqrt(abs(%s)));\n",
2284 src_param.param_str, src_param.param_str);
2285 }
2286}
2287
2288/** Process signed comparison opcodes in GLSL. */
2289static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2290{
2291 glsl_src_param_t src0_param;
2292 glsl_src_param_t src1_param;
2293 DWORD write_mask;
2294 unsigned int mask_size;
2295
2296 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2297 mask_size = shader_glsl_get_write_mask_size(write_mask);
2298 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2299 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2300
2301 if (mask_size > 1) {
2302 const char *compare;
2303
2304 switch(ins->handler_idx)
2305 {
2306 case WINED3DSIH_SLT: compare = "lessThan"; break;
2307 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2308 default: compare = "";
2309 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2310 }
2311
2312 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2313 src0_param.param_str, src1_param.param_str);
2314 } else {
2315 switch(ins->handler_idx)
2316 {
2317 case WINED3DSIH_SLT:
2318 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2319 * to return 0.0 but step returns 1.0 because step is not < x
2320 * An alternative is a bvec compare padded with an unused second component.
2321 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2322 * issue. Playing with not() is not possible either because not() does not accept
2323 * a scalar.
2324 */
2325 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2326 src0_param.param_str, src1_param.param_str);
2327 break;
2328 case WINED3DSIH_SGE:
2329 /* Here we can use the step() function and safe a conditional */
2330 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2331 break;
2332 default:
2333 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2334 }
2335
2336 }
2337}
2338
2339/** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
2340static void shader_glsl_cmp(const struct wined3d_shader_instruction *ins)
2341{
2342 glsl_src_param_t src0_param;
2343 glsl_src_param_t src1_param;
2344 glsl_src_param_t src2_param;
2345 DWORD write_mask, cmp_channel = 0;
2346 unsigned int i, j;
2347 char mask_char[6];
2348 BOOL temp_destination = FALSE;
2349
2350 if (shader_is_scalar(&ins->src[0].reg))
2351 {
2352 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2353
2354 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2355 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2356 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2357
2358 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2359 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2360 } else {
2361 DWORD dst_mask = ins->dst[0].write_mask;
2362 struct wined3d_shader_dst_param dst = ins->dst[0];
2363
2364 /* Cycle through all source0 channels */
2365 for (i=0; i<4; i++) {
2366 write_mask = 0;
2367 /* Find the destination channels which use the current source0 channel */
2368 for (j=0; j<4; j++) {
2369 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2370 {
2371 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2372 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2373 }
2374 }
2375 dst.write_mask = dst_mask & write_mask;
2376
2377 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2378 * The first lines may overwrite source parameters of the following lines.
2379 * Deal with that by using a temporary destination register if needed
2380 */
2381 if ((ins->src[0].reg.idx == ins->dst[0].reg.idx
2382 && ins->src[0].reg.type == ins->dst[0].reg.type)
2383 || (ins->src[1].reg.idx == ins->dst[0].reg.idx
2384 && ins->src[1].reg.type == ins->dst[0].reg.type)
2385 || (ins->src[2].reg.idx == ins->dst[0].reg.idx
2386 && ins->src[2].reg.type == ins->dst[0].reg.type))
2387 {
2388 write_mask = shader_glsl_get_write_mask(&dst, mask_char);
2389 if (!write_mask) continue;
2390 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2391 temp_destination = TRUE;
2392 } else {
2393 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2394 if (!write_mask) continue;
2395 }
2396
2397 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2398 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2399 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2400
2401 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2402 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2403 }
2404
2405 if(temp_destination) {
2406 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2407 shader_glsl_append_dst(ins->ctx->buffer, ins);
2408 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2409 }
2410 }
2411
2412}
2413
2414/** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2415/* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2416 * the compare is done per component of src0. */
2417static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2418{
2419 struct wined3d_shader_dst_param dst;
2420 glsl_src_param_t src0_param;
2421 glsl_src_param_t src1_param;
2422 glsl_src_param_t src2_param;
2423 DWORD write_mask, cmp_channel = 0;
2424 unsigned int i, j;
2425 DWORD dst_mask;
2426 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2427 ins->ctx->reg_maps->shader_version.minor);
2428
2429 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2430 {
2431 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2432 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2433 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2434 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2435
2436 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2437 if (ins->coissue)
2438 {
2439 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2440 } else {
2441 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2442 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2443 }
2444 return;
2445 }
2446 /* Cycle through all source0 channels */
2447 dst_mask = ins->dst[0].write_mask;
2448 dst = ins->dst[0];
2449 for (i=0; i<4; i++) {
2450 write_mask = 0;
2451 /* Find the destination channels which use the current source0 channel */
2452 for (j=0; j<4; j++) {
2453 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2454 {
2455 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2456 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2457 }
2458 }
2459
2460 dst.write_mask = dst_mask & write_mask;
2461 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2462 if (!write_mask) continue;
2463
2464 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2465 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2466 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2467
2468 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2469 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2470 }
2471}
2472
2473/** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2474static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2475{
2476 glsl_src_param_t src0_param;
2477 glsl_src_param_t src1_param;
2478 glsl_src_param_t src2_param;
2479 DWORD write_mask;
2480
2481 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2482 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2483 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2484 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2485 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2486 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2487}
2488
2489/* Handles transforming all WINED3DSIO_M?x? opcodes for
2490 Vertex shaders to GLSL codes */
2491static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2492{
2493 int i;
2494 int nComponents = 0;
2495 struct wined3d_shader_dst_param tmp_dst = {{0}};
2496 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2497 struct wined3d_shader_instruction tmp_ins;
2498
2499 memset(&tmp_ins, 0, sizeof(tmp_ins));
2500
2501 /* Set constants for the temporary argument */
2502 tmp_ins.ctx = ins->ctx;
2503 tmp_ins.dst_count = 1;
2504 tmp_ins.dst = &tmp_dst;
2505 tmp_ins.src_count = 2;
2506 tmp_ins.src = tmp_src;
2507
2508 switch(ins->handler_idx)
2509 {
2510 case WINED3DSIH_M4x4:
2511 nComponents = 4;
2512 tmp_ins.handler_idx = WINED3DSIH_DP4;
2513 break;
2514 case WINED3DSIH_M4x3:
2515 nComponents = 3;
2516 tmp_ins.handler_idx = WINED3DSIH_DP4;
2517 break;
2518 case WINED3DSIH_M3x4:
2519 nComponents = 4;
2520 tmp_ins.handler_idx = WINED3DSIH_DP3;
2521 break;
2522 case WINED3DSIH_M3x3:
2523 nComponents = 3;
2524 tmp_ins.handler_idx = WINED3DSIH_DP3;
2525 break;
2526 case WINED3DSIH_M3x2:
2527 nComponents = 2;
2528 tmp_ins.handler_idx = WINED3DSIH_DP3;
2529 break;
2530 default:
2531 break;
2532 }
2533
2534 tmp_dst = ins->dst[0];
2535 tmp_src[0] = ins->src[0];
2536 tmp_src[1] = ins->src[1];
2537 for (i = 0; i < nComponents; ++i)
2538 {
2539 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2540 shader_glsl_dot(&tmp_ins);
2541 ++tmp_src[1].reg.idx;
2542 }
2543}
2544
2545/**
2546 The LRP instruction performs a component-wise linear interpolation
2547 between the second and third operands using the first operand as the
2548 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2549 This is equivalent to mix(src2, src1, src0);
2550*/
2551static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
2552{
2553 glsl_src_param_t src0_param;
2554 glsl_src_param_t src1_param;
2555 glsl_src_param_t src2_param;
2556 DWORD write_mask;
2557
2558 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2559
2560 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2561 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2562 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2563
2564 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
2565 src2_param.param_str, src1_param.param_str, src0_param.param_str);
2566}
2567
2568/** Process the WINED3DSIO_LIT instruction in GLSL:
2569 * dst.x = dst.w = 1.0
2570 * dst.y = (src0.x > 0) ? src0.x
2571 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2572 * where src.w is clamped at +- 128
2573 */
2574static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
2575{
2576 glsl_src_param_t src0_param;
2577 glsl_src_param_t src1_param;
2578 glsl_src_param_t src3_param;
2579 char dst_mask[6];
2580
2581 shader_glsl_append_dst(ins->ctx->buffer, ins);
2582 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2583
2584 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2585 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
2586 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
2587
2588 /* The sdk specifies the instruction like this
2589 * dst.x = 1.0;
2590 * if(src.x > 0.0) dst.y = src.x
2591 * else dst.y = 0.0.
2592 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2593 * else dst.z = 0.0;
2594 * dst.w = 1.0;
2595 *
2596 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2597 * dst.x = 1.0 ... No further explanation needed
2598 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2599 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2600 * dst.w = 1.0. ... Nothing fancy.
2601 *
2602 * So we still have one conditional in there. So do this:
2603 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2604 *
2605 * 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),
2606 * 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.
2607 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
2608 */
2609 shader_addline(ins->ctx->buffer,
2610 "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",
2611 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
2612}
2613
2614/** Process the WINED3DSIO_DST instruction in GLSL:
2615 * dst.x = 1.0
2616 * dst.y = src0.x * src0.y
2617 * dst.z = src0.z
2618 * dst.w = src1.w
2619 */
2620static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
2621{
2622 glsl_src_param_t src0y_param;
2623 glsl_src_param_t src0z_param;
2624 glsl_src_param_t src1y_param;
2625 glsl_src_param_t src1w_param;
2626 char dst_mask[6];
2627
2628 shader_glsl_append_dst(ins->ctx->buffer, ins);
2629 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2630
2631 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
2632 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
2633 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
2634 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
2635
2636 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2637 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
2638}
2639
2640/** Process the WINED3DSIO_SINCOS instruction in GLSL:
2641 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2642 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2643 *
2644 * dst.x = cos(src0.?)
2645 * dst.y = sin(src0.?)
2646 * dst.z = dst.z
2647 * dst.w = dst.w
2648 */
2649static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
2650{
2651 glsl_src_param_t src0_param;
2652 DWORD write_mask;
2653
2654 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2655 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2656
2657 switch (write_mask) {
2658 case WINED3DSP_WRITEMASK_0:
2659 shader_addline(ins->ctx->buffer, "cos(%s));\n", src0_param.param_str);
2660 break;
2661
2662 case WINED3DSP_WRITEMASK_1:
2663 shader_addline(ins->ctx->buffer, "sin(%s));\n", src0_param.param_str);
2664 break;
2665
2666 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2667 shader_addline(ins->ctx->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2668 break;
2669
2670 default:
2671 ERR("Write mask should be .x, .y or .xy\n");
2672 break;
2673 }
2674}
2675
2676/* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
2677 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
2678 * generate invalid code
2679 */
2680static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
2681{
2682 glsl_src_param_t src0_param;
2683 DWORD write_mask;
2684
2685 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2686 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2687
2688 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
2689}
2690
2691/** Process the WINED3DSIO_LOOP instruction in GLSL:
2692 * Start a for() loop where src1.y is the initial value of aL,
2693 * increment aL by src1.z for a total of src1.x iterations.
2694 * Need to use a temporary variable for this operation.
2695 */
2696/* FIXME: I don't think nested loops will work correctly this way. */
2697static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
2698{
2699 glsl_src_param_t src1_param;
2700 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2701 const DWORD *control_values = NULL;
2702 const local_constant *constant;
2703
2704 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2705
2706 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2707 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2708 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2709 * addressing.
2710 */
2711 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
2712 {
2713 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2714 if (constant->idx == ins->src[1].reg.idx)
2715 {
2716 control_values = constant->value;
2717 break;
2718 }
2719 }
2720 }
2721
2722 if (control_values)
2723 {
2724 struct wined3d_shader_loop_control loop_control;
2725 loop_control.count = control_values[0];
2726 loop_control.start = control_values[1];
2727 loop_control.step = (int)control_values[2];
2728
2729 if (loop_control.step > 0)
2730 {
2731 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d) {\n",
2732 shader->baseShader.cur_loop_depth, loop_control.start,
2733 shader->baseShader.cur_loop_depth, loop_control.count, loop_control.step, loop_control.start,
2734 shader->baseShader.cur_loop_depth, loop_control.step);
2735 }
2736 else if (loop_control.step < 0)
2737 {
2738 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d) {\n",
2739 shader->baseShader.cur_loop_depth, loop_control.start,
2740 shader->baseShader.cur_loop_depth, loop_control.count, loop_control.step, loop_control.start,
2741 shader->baseShader.cur_loop_depth, loop_control.step);
2742 }
2743 else
2744 {
2745 shader_addline(ins->ctx->buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++) {\n",
2746 shader->baseShader.cur_loop_depth, loop_control.start, shader->baseShader.cur_loop_depth,
2747 shader->baseShader.cur_loop_depth, loop_control.count,
2748 shader->baseShader.cur_loop_depth);
2749 }
2750 } else {
2751 shader_addline(ins->ctx->buffer,
2752 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2753 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2754 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2755 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2756 }
2757
2758 shader->baseShader.cur_loop_depth++;
2759 shader->baseShader.cur_loop_regno++;
2760}
2761
2762static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
2763{
2764 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2765
2766 shader_addline(ins->ctx->buffer, "}\n");
2767
2768 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
2769 {
2770 shader->baseShader.cur_loop_depth--;
2771 shader->baseShader.cur_loop_regno--;
2772 }
2773
2774 if (ins->handler_idx == WINED3DSIH_ENDREP)
2775 {
2776 shader->baseShader.cur_loop_depth--;
2777 }
2778}
2779
2780static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
2781{
2782 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2783 glsl_src_param_t src0_param;
2784 const DWORD *control_values = NULL;
2785 const local_constant *constant;
2786
2787 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2788 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
2789 {
2790 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry)
2791 {
2792 if (constant->idx == ins->src[0].reg.idx)
2793 {
2794 control_values = constant->value;
2795 break;
2796 }
2797 }
2798 }
2799
2800 if(control_values) {
2801 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2802 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2803 control_values[0], shader->baseShader.cur_loop_depth);
2804 } else {
2805 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2806 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2807 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2808 src0_param.param_str, shader->baseShader.cur_loop_depth);
2809 }
2810 shader->baseShader.cur_loop_depth++;
2811}
2812
2813static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
2814{
2815 glsl_src_param_t src0_param;
2816
2817 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2818 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
2819}
2820
2821static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
2822{
2823 glsl_src_param_t src0_param;
2824 glsl_src_param_t src1_param;
2825
2826 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2827 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2828
2829 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
2830 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2831}
2832
2833static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
2834{
2835 shader_addline(ins->ctx->buffer, "} else {\n");
2836}
2837
2838static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
2839{
2840 shader_addline(ins->ctx->buffer, "break;\n");
2841}
2842
2843/* FIXME: According to MSDN the compare is done per component. */
2844static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
2845{
2846 glsl_src_param_t src0_param;
2847 glsl_src_param_t src1_param;
2848
2849 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2850 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2851
2852 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
2853 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2854}
2855
2856static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
2857{
2858 shader_addline(ins->ctx->buffer, "}\n");
2859 shader_addline(ins->ctx->buffer, "void subroutine%u () {\n", ins->src[0].reg.idx);
2860}
2861
2862static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
2863{
2864 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx);
2865}
2866
2867static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
2868{
2869 glsl_src_param_t src1_param;
2870
2871 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2872 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, ins->src[0].reg.idx);
2873}
2874
2875static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
2876{
2877 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
2878 * function only suppresses the unhandled instruction warning
2879 */
2880}
2881
2882/*********************************************
2883 * Pixel Shader Specific Code begins here
2884 ********************************************/
2885static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
2886{
2887 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2888 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *)shader->baseShader.device;
2889 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2890 ins->ctx->reg_maps->shader_version.minor);
2891 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2892 glsl_sample_function_t sample_function;
2893 DWORD sample_flags = 0;
2894 WINED3DSAMPLER_TEXTURE_TYPE sampler_type;
2895 DWORD sampler_idx;
2896 DWORD mask = 0, swizzle;
2897
2898 /* 1.0-1.4: Use destination register as sampler source.
2899 * 2.0+: Use provided sampler source. */
2900 if (shader_version < WINED3D_SHADER_VERSION(2,0)) sampler_idx = ins->dst[0].reg.idx;
2901 else sampler_idx = ins->src[1].reg.idx;
2902 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
2903
2904 if (shader_version < WINED3D_SHADER_VERSION(1,4))
2905 {
2906 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2907
2908 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2909 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2910 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2911 switch (flags & ~WINED3DTTFF_PROJECTED) {
2912 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2913 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2914 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2915 case WINED3DTTFF_COUNT4:
2916 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
2917 }
2918 }
2919 }
2920 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
2921 {
2922 DWORD src_mod = ins->src[0].modifiers;
2923
2924 if (src_mod == WINED3DSPSM_DZ) {
2925 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2926 mask = WINED3DSP_WRITEMASK_2;
2927 } else if (src_mod == WINED3DSPSM_DW) {
2928 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2929 mask = WINED3DSP_WRITEMASK_3;
2930 }
2931 } else {
2932 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
2933 {
2934 /* ps 2.0 texldp instruction always divides by the fourth component. */
2935 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2936 mask = WINED3DSP_WRITEMASK_3;
2937 }
2938 }
2939
2940 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2941 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2942 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
2943 }
2944
2945 shader_glsl_get_sample_function(gl_info, sampler_type, sample_flags, &sample_function);
2946 mask |= sample_function.coord_mask;
2947
2948 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
2949 else swizzle = ins->src[1].swizzle;
2950
2951 /* 1.0-1.3: Use destination register as coordinate source.
2952 1.4+: Use provided coordinate source register. */
2953 if (shader_version < WINED3D_SHADER_VERSION(1,4))
2954 {
2955 char coord_mask[6];
2956 shader_glsl_write_mask_to_str(mask, coord_mask);
2957 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
2958 "T%u%s", sampler_idx, coord_mask);
2959 } else {
2960 glsl_src_param_t coord_param;
2961 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
2962 if (ins->flags & WINED3DSI_TEXLD_BIAS)
2963 {
2964 glsl_src_param_t bias;
2965 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
2966 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
2967 "%s", coord_param.param_str);
2968 } else {
2969 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
2970 "%s", coord_param.param_str);
2971 }
2972 }
2973}
2974
2975static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
2976{
2977 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2978 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
2979 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2980 glsl_sample_function_t sample_function;
2981 glsl_src_param_t coord_param, dx_param, dy_param;
2982 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
2983 DWORD sampler_type;
2984 DWORD sampler_idx;
2985 DWORD swizzle = ins->src[1].swizzle;
2986
2987 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
2988 {
2989 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
2990 return shader_glsl_tex(ins);
2991 }
2992
2993 sampler_idx = ins->src[1].reg.idx;
2994 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
2995 if(deviceImpl->stateBlock->textures[sampler_idx] &&
2996 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
2997 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
2998 }
2999
3000 shader_glsl_get_sample_function(gl_info, sampler_type, sample_flags, &sample_function);
3001 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3002 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3003 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3004
3005 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3006 "%s", coord_param.param_str);
3007}
3008
3009static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3010{
3011 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3012 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
3013 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3014 glsl_sample_function_t sample_function;
3015 glsl_src_param_t coord_param, lod_param;
3016 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3017 DWORD sampler_type;
3018 DWORD sampler_idx;
3019 DWORD swizzle = ins->src[1].swizzle;
3020
3021 sampler_idx = ins->src[1].reg.idx;
3022 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3023 if(deviceImpl->stateBlock->textures[sampler_idx] &&
3024 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
3025 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3026 }
3027 shader_glsl_get_sample_function(gl_info, sampler_type, sample_flags, &sample_function);
3028 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3029
3030 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3031
3032 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3033 && shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
3034 {
3035 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
3036 * However, they seem to work just fine in fragment shaders as well. */
3037 WARN("Using %s in fragment shader.\n", sample_function.name);
3038 }
3039 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3040 "%s", coord_param.param_str);
3041}
3042
3043static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3044{
3045 /* FIXME: Make this work for more than just 2D textures */
3046 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3047 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3048
3049 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3050 {
3051 char dst_mask[6];
3052
3053 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3054 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3055 ins->dst[0].reg.idx, dst_mask);
3056 } else {
3057 DWORD reg = ins->src[0].reg.idx;
3058 DWORD src_mod = ins->src[0].modifiers;
3059 char dst_swizzle[6];
3060
3061 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3062
3063 if (src_mod == WINED3DSPSM_DZ) {
3064 glsl_src_param_t div_param;
3065 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3066 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3067
3068 if (mask_size > 1) {
3069 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3070 } else {
3071 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3072 }
3073 } else if (src_mod == WINED3DSPSM_DW) {
3074 glsl_src_param_t div_param;
3075 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3076 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3077
3078 if (mask_size > 1) {
3079 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3080 } else {
3081 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3082 }
3083 } else {
3084 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3085 }
3086 }
3087}
3088
3089/** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3090 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3091 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3092static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3093{
3094 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3095 glsl_src_param_t src0_param;
3096 glsl_sample_function_t sample_function;
3097 DWORD sampler_idx = ins->dst[0].reg.idx;
3098 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3099 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3100 UINT mask_size;
3101
3102 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3103
3104 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3105 * scalar, and projected sampling would require 4.
3106 *
3107 * It is a dependent read - not valid with conditional NP2 textures
3108 */
3109 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3110 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3111
3112 switch(mask_size)
3113 {
3114 case 1:
3115 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3116 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3117 break;
3118
3119 case 2:
3120 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3121 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3122 break;
3123
3124 case 3:
3125 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3126 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3127 break;
3128
3129 default:
3130 FIXME("Unexpected mask size %u\n", mask_size);
3131 break;
3132 }
3133}
3134
3135/** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3136 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3137static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3138{
3139 glsl_src_param_t src0_param;
3140 DWORD dstreg = ins->dst[0].reg.idx;
3141 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3142 DWORD dst_mask;
3143 unsigned int mask_size;
3144
3145 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3146 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3147 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3148
3149 if (mask_size > 1) {
3150 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3151 } else {
3152 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3153 }
3154}
3155
3156/** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3157 * Calculate the depth as dst.x / dst.y */
3158static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3159{
3160 glsl_dst_param_t dst_param;
3161
3162 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3163
3164 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3165 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3166 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3167 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3168 * >= 1.0 or < 0.0
3169 */
3170 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3171 dst_param.reg_name, dst_param.reg_name);
3172}
3173
3174/** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3175 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3176 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3177 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3178 */
3179static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3180{
3181 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3182 DWORD dstreg = ins->dst[0].reg.idx;
3183 glsl_src_param_t src0_param;
3184
3185 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3186
3187 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3188 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3189}
3190
3191/** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3192 * Calculate the 1st of a 2-row matrix multiplication. */
3193static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3194{
3195 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3196 DWORD reg = ins->dst[0].reg.idx;
3197 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3198 glsl_src_param_t src0_param;
3199
3200 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3201 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3202}
3203
3204/** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3205 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3206static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3207{
3208 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3209 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3210 DWORD reg = ins->dst[0].reg.idx;
3211 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3212 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
3213 glsl_src_param_t src0_param;
3214
3215 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3216 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
3217 current_state->texcoord_w[current_state->current_row++] = reg;
3218}
3219
3220static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3221{
3222 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3223 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3224 DWORD reg = ins->dst[0].reg.idx;
3225 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3226 glsl_src_param_t src0_param;
3227 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
3228 glsl_sample_function_t sample_function;
3229
3230 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3231 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3232
3233 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3234
3235 /* Sample the texture using the calculated coordinates */
3236 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3237}
3238
3239/** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3240 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3241static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3242{
3243 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3244 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3245 SHADER_PARSE_STATE *current_state = &shader->baseShader.parse_state;
3246 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3247 glsl_src_param_t src0_param;
3248 DWORD reg = ins->dst[0].reg.idx;
3249 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
3250 glsl_sample_function_t sample_function;
3251
3252 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3253 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3254
3255 /* Dependent read, not valid with conditional NP2 */
3256 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3257
3258 /* Sample the texture using the calculated coordinates */
3259 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3260
3261 current_state->current_row = 0;
3262}
3263
3264/** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3265 * Perform the 3rd row of a 3x3 matrix multiply */
3266static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3267{
3268 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3269 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3270 SHADER_PARSE_STATE *current_state = &shader->baseShader.parse_state;
3271 glsl_src_param_t src0_param;
3272 char dst_mask[6];
3273 DWORD reg = ins->dst[0].reg.idx;
3274
3275 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3276
3277 shader_glsl_append_dst(ins->ctx->buffer, ins);
3278 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3279 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3280
3281 current_state->current_row = 0;
3282}
3283
3284/* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3285 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3286static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3287{
3288 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3289 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3290 DWORD reg = ins->dst[0].reg.idx;
3291 glsl_src_param_t src0_param;
3292 glsl_src_param_t src1_param;
3293 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3294 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
3295 WINED3DSAMPLER_TEXTURE_TYPE stype = ins->ctx->reg_maps->sampler_type[reg];
3296 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3297 glsl_sample_function_t sample_function;
3298
3299 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3300 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3301
3302 /* Perform the last matrix multiply operation */
3303 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3304 /* Reflection calculation */
3305 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3306
3307 /* Dependent read, not valid with conditional NP2 */
3308 shader_glsl_get_sample_function(gl_info, stype, 0, &sample_function);
3309
3310 /* Sample the texture */
3311 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3312
3313 current_state->current_row = 0;
3314}
3315
3316/* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3317 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3318static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3319{
3320 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3321 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3322 DWORD reg = ins->dst[0].reg.idx;
3323 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3324 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
3325 glsl_src_param_t src0_param;
3326 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3327 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
3328 glsl_sample_function_t sample_function;
3329
3330 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3331
3332 /* Perform the last matrix multiply operation */
3333 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3334
3335 /* Construct the eye-ray vector from w coordinates */
3336 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3337 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
3338 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3339
3340 /* Dependent read, not valid with conditional NP2 */
3341 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3342
3343 /* Sample the texture using the calculated coordinates */
3344 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3345
3346 current_state->current_row = 0;
3347}
3348
3349/** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3350 * Apply a fake bump map transform.
3351 * texbem is pshader <= 1.3 only, this saves a few version checks
3352 */
3353static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3354{
3355 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3356 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *)shader->baseShader.device;
3357 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3358 glsl_sample_function_t sample_function;
3359 glsl_src_param_t coord_param;
3360 WINED3DSAMPLER_TEXTURE_TYPE sampler_type;
3361 DWORD sampler_idx;
3362 DWORD mask;
3363 DWORD flags;
3364 char coord_mask[6];
3365
3366 sampler_idx = ins->dst[0].reg.idx;
3367 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
3368
3369 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3370 /* Dependent read, not valid with conditional NP2 */
3371 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3372 mask = sample_function.coord_mask;
3373
3374 shader_glsl_write_mask_to_str(mask, coord_mask);
3375
3376 /* with projective textures, texbem only divides the static texture coord, not the displacement,
3377 * so we can't let the GL handle this.
3378 */
3379 if (flags & WINED3DTTFF_PROJECTED) {
3380 DWORD div_mask=0;
3381 char coord_div_mask[3];
3382 switch (flags & ~WINED3DTTFF_PROJECTED) {
3383 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3384 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
3385 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
3386 case WINED3DTTFF_COUNT4:
3387 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
3388 }
3389 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3390 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3391 }
3392
3393 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3394
3395 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3396 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3397 coord_param.param_str, coord_mask);
3398
3399 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3400 {
3401 glsl_src_param_t luminance_param;
3402 glsl_dst_param_t dst_param;
3403
3404 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3405 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3406
3407 shader_addline(ins->ctx->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3408 dst_param.reg_name, dst_param.mask_str,
3409 luminance_param.param_str, sampler_idx, sampler_idx);
3410 }
3411}
3412
3413static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3414{
3415 glsl_src_param_t src0_param, src1_param;
3416 DWORD sampler_idx = ins->dst[0].reg.idx;
3417
3418 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3419 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3420
3421 shader_glsl_append_dst(ins->ctx->buffer, ins);
3422 shader_addline(ins->ctx->buffer, "%s + bumpenvmat%d * %s);\n",
3423 src0_param.param_str, sampler_idx, src1_param.param_str);
3424}
3425
3426/** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3427 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3428static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
3429{
3430 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3431 glsl_src_param_t src0_param;
3432 DWORD sampler_idx = ins->dst[0].reg.idx;
3433 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3434 glsl_sample_function_t sample_function;
3435
3436 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3437
3438 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3439 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3440 "%s.wx", src0_param.reg_name);
3441}
3442
3443/** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3444 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3445static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
3446{
3447 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3448 glsl_src_param_t src0_param;
3449 DWORD sampler_idx = ins->dst[0].reg.idx;
3450 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3451 glsl_sample_function_t sample_function;
3452
3453 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3454
3455 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3456 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3457 "%s.yz", src0_param.reg_name);
3458}
3459
3460/** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3461 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3462static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
3463{
3464 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3465 glsl_src_param_t src0_param;
3466 DWORD sampler_idx = ins->dst[0].reg.idx;
3467 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3468 glsl_sample_function_t sample_function;
3469
3470 /* Dependent read, not valid with conditional NP2 */
3471 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3472 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
3473
3474 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3475 "%s", src0_param.param_str);
3476}
3477
3478/** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3479 * If any of the first 3 components are < 0, discard this pixel */
3480static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
3481{
3482 glsl_dst_param_t dst_param;
3483
3484 /* The argument is a destination parameter, and no writemasks are allowed */
3485 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3486 if (ins->ctx->reg_maps->shader_version.major >= 2)
3487 {
3488 /* 2.0 shaders compare all 4 components in texkill */
3489 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
3490 } else {
3491 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3492 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3493 * 4 components are defined, only the first 3 are used
3494 */
3495 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
3496 }
3497}
3498
3499/** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3500 * dst = dot2(src0, src1) + src2 */
3501static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
3502{
3503 glsl_src_param_t src0_param;
3504 glsl_src_param_t src1_param;
3505 glsl_src_param_t src2_param;
3506 DWORD write_mask;
3507 unsigned int mask_size;
3508
3509 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3510 mask_size = shader_glsl_get_write_mask_size(write_mask);
3511
3512 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3513 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3514 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
3515
3516 if (mask_size > 1) {
3517 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
3518 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
3519 } else {
3520 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
3521 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3522 }
3523}
3524
3525static void shader_glsl_input_pack(IWineD3DPixelShader *iface, struct wined3d_shader_buffer *buffer,
3526 const struct wined3d_shader_signature_element *input_signature, const struct shader_reg_maps *reg_maps,
3527 enum vertexprocessing_mode vertexprocessing)
3528{
3529 unsigned int i;
3530 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3531 WORD map = reg_maps->input_registers;
3532
3533 for (i = 0; map; map >>= 1, ++i)
3534 {
3535 const char *semantic_name;
3536 UINT semantic_idx;
3537 char reg_mask[6];
3538
3539 /* Unused */
3540 if (!(map & 1)) continue;
3541
3542 semantic_name = input_signature[i].semantic_name;
3543 semantic_idx = input_signature[i].semantic_idx;
3544 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
3545
3546 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3547 {
3548 if (semantic_idx < 8 && vertexprocessing == pretransformed)
3549 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3550 This->input_reg_map[i], reg_mask, semantic_idx, reg_mask);
3551 else
3552 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3553 This->input_reg_map[i], reg_mask, reg_mask);
3554 }
3555 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3556 {
3557 if (semantic_idx == 0)
3558 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
3559 This->input_reg_map[i], reg_mask, reg_mask);
3560 else if (semantic_idx == 1)
3561 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3562 This->input_reg_map[i], reg_mask, reg_mask);
3563 else
3564 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3565 This->input_reg_map[i], reg_mask, reg_mask);
3566 }
3567 else
3568 {
3569 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3570 This->input_reg_map[i], reg_mask, reg_mask);
3571 }
3572 }
3573}
3574
3575/*********************************************
3576 * Vertex Shader Specific Code begins here
3577 ********************************************/
3578
3579static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
3580 glsl_program_key_t key;
3581
3582 key.vshader = entry->vshader;
3583 key.pshader = entry->pshader;
3584 key.vs_args = entry->vs_args;
3585 key.ps_args = entry->ps_args;
3586
3587 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
3588 {
3589 ERR("Failed to insert program entry.\n");
3590 }
3591}
3592
3593static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
3594 IWineD3DVertexShader *vshader, IWineD3DPixelShader *pshader, struct vs_compile_args *vs_args,
3595 struct ps_compile_args *ps_args) {
3596 struct wine_rb_entry *entry;
3597 glsl_program_key_t key;
3598
3599 key.vshader = vshader;
3600 key.pshader = pshader;
3601 key.vs_args = *vs_args;
3602 key.ps_args = *ps_args;
3603
3604 entry = wine_rb_get(&priv->program_lookup, &key);
3605 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
3606}
3607
3608/* GL locking is done by the caller */
3609static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
3610 struct glsl_shader_prog_link *entry)
3611{
3612 glsl_program_key_t key;
3613
3614 key.vshader = entry->vshader;
3615 key.pshader = entry->pshader;
3616 key.vs_args = entry->vs_args;
3617 key.ps_args = entry->ps_args;
3618 wine_rb_remove(&priv->program_lookup, &key);
3619
3620 GL_EXTCALL(glDeleteObjectARB(entry->programId));
3621 if (entry->vshader) list_remove(&entry->vshader_entry);
3622 if (entry->pshader) list_remove(&entry->pshader_entry);
3623 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
3624 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
3625 HeapFree(GetProcessHeap(), 0, entry);
3626}
3627
3628static void handle_ps3_input(struct wined3d_shader_buffer *buffer, const struct wined3d_gl_info *gl_info, const DWORD *map,
3629 const struct wined3d_shader_signature_element *input_signature, const struct shader_reg_maps *reg_maps_in,
3630 const struct wined3d_shader_signature_element *output_signature, const struct shader_reg_maps *reg_maps_out)
3631{
3632 unsigned int i, j;
3633 const char *semantic_name_in, *semantic_name_out;
3634 UINT semantic_idx_in, semantic_idx_out;
3635 DWORD *set;
3636 DWORD in_idx;
3637 unsigned int in_count = vec4_varyings(3, gl_info);
3638 char reg_mask[6], reg_mask_out[6];
3639 char destination[50];
3640 WORD input_map, output_map;
3641
3642 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
3643
3644 if (!output_signature)
3645 {
3646 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
3647 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
3648 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
3649 }
3650
3651 input_map = reg_maps_in->input_registers;
3652 for (i = 0; input_map; input_map >>= 1, ++i)
3653 {
3654 if (!(input_map & 1)) continue;
3655
3656 in_idx = map[i];
3657 if (in_idx >= (in_count + 2)) {
3658 FIXME("More input varyings declared than supported, expect issues\n");
3659 continue;
3660 }
3661 else if (map[i] == ~0U)
3662 {
3663 /* Declared, but not read register */
3664 continue;
3665 }
3666
3667 if (in_idx == in_count) {
3668 sprintf(destination, "gl_FrontColor");
3669 } else if (in_idx == in_count + 1) {
3670 sprintf(destination, "gl_FrontSecondaryColor");
3671 } else {
3672 sprintf(destination, "IN[%u]", in_idx);
3673 }
3674
3675 semantic_name_in = input_signature[i].semantic_name;
3676 semantic_idx_in = input_signature[i].semantic_idx;
3677 set[map[i]] = input_signature[i].mask;
3678 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
3679
3680 if (!output_signature)
3681 {
3682 if (shader_match_semantic(semantic_name_in, WINED3DDECLUSAGE_COLOR))
3683 {
3684 if (semantic_idx_in == 0)
3685 shader_addline(buffer, "%s%s = front_color%s;\n",
3686 destination, reg_mask, reg_mask);
3687 else if (semantic_idx_in == 1)
3688 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
3689 destination, reg_mask, reg_mask);
3690 else
3691 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3692 destination, reg_mask, reg_mask);
3693 }
3694 else if (shader_match_semantic(semantic_name_in, WINED3DDECLUSAGE_TEXCOORD))
3695 {
3696 if (semantic_idx_in < 8)
3697 {
3698 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
3699 destination, reg_mask, semantic_idx_in, reg_mask);
3700 }
3701 else
3702 {
3703 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3704 destination, reg_mask, reg_mask);
3705 }
3706 }
3707 else if (shader_match_semantic(semantic_name_in, WINED3DDECLUSAGE_FOG))
3708 {
3709 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3710 destination, reg_mask, reg_mask);
3711 }
3712 else
3713 {
3714 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3715 destination, reg_mask, reg_mask);
3716 }
3717 } else {
3718 BOOL found = FALSE;
3719
3720 output_map = reg_maps_out->output_registers;
3721 for (j = 0; output_map; output_map >>= 1, ++j)
3722 {
3723 if (!(output_map & 1)) continue;
3724
3725 semantic_name_out = output_signature[j].semantic_name;
3726 semantic_idx_out = output_signature[j].semantic_idx;
3727 shader_glsl_write_mask_to_str(output_signature[j].mask, reg_mask_out);
3728
3729 if (semantic_idx_in == semantic_idx_out
3730 && !strcmp(semantic_name_in, semantic_name_out))
3731 {
3732 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3733 destination, reg_mask, j, reg_mask);
3734 found = TRUE;
3735 }
3736 }
3737 if(!found) {
3738 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3739 destination, reg_mask, reg_mask);
3740 }
3741 }
3742 }
3743
3744 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3745 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3746 * input varyings are assigned above, if the optimizer works properly.
3747 */
3748 for(i = 0; i < in_count + 2; i++) {
3749 if (set[i] && set[i] != WINED3DSP_WRITEMASK_ALL)
3750 {
3751 unsigned int size = 0;
3752 memset(reg_mask, 0, sizeof(reg_mask));
3753 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
3754 reg_mask[size] = 'x';
3755 size++;
3756 }
3757 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
3758 reg_mask[size] = 'y';
3759 size++;
3760 }
3761 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
3762 reg_mask[size] = 'z';
3763 size++;
3764 }
3765 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
3766 reg_mask[size] = 'w';
3767 size++;
3768 }
3769
3770 if (i == in_count) {
3771 sprintf(destination, "gl_FrontColor");
3772 } else if (i == in_count + 1) {
3773 sprintf(destination, "gl_FrontSecondaryColor");
3774 } else {
3775 sprintf(destination, "IN[%u]", i);
3776 }
3777
3778 if (size == 1) {
3779 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3780 } else {
3781 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3782 }
3783 }
3784 }
3785
3786 HeapFree(GetProcessHeap(), 0, set);
3787}
3788
3789/* GL locking is done by the caller */
3790static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
3791 IWineD3DVertexShader *vertexshader, IWineD3DPixelShader *pixelshader, const struct wined3d_gl_info *gl_info)
3792{
3793 GLhandleARB ret = 0;
3794 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3795 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3796 IWineD3DDeviceImpl *device;
3797 DWORD vs_major = vs->baseShader.reg_maps.shader_version.major;
3798 DWORD ps_major = ps ? ps->baseShader.reg_maps.shader_version.major : 0;
3799 unsigned int i;
3800 const char *semantic_name;
3801 UINT semantic_idx;
3802 char reg_mask[6];
3803 const struct wined3d_shader_signature_element *output_signature;
3804
3805 shader_buffer_clear(buffer);
3806
3807 shader_addline(buffer, "#version 120\n");
3808
3809 if(vs_major < 3 && ps_major < 3) {
3810 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3811 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3812 */
3813 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3814 if ((gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W)
3815 && ps_major == 0 && vs_major > 0 && !device->frag_pipe->ffp_proj_control)
3816 {
3817 shader_addline(buffer, "void order_ps_input() {\n");
3818 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3819 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3820 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3821 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3822 }
3823 }
3824 shader_addline(buffer, "}\n");
3825 } else {
3826 shader_addline(buffer, "void order_ps_input() { /* do nothing */ }\n");
3827 }
3828 } else if(ps_major < 3 && vs_major >= 3) {
3829 WORD map = vs->baseShader.reg_maps.output_registers;
3830
3831 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3832 output_signature = vs->baseShader.output_signature;
3833
3834 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3835 for (i = 0; map; map >>= 1, ++i)
3836 {
3837 DWORD write_mask;
3838
3839 if (!(map & 1)) continue;
3840
3841 semantic_name = output_signature[i].semantic_name;
3842 semantic_idx = output_signature[i].semantic_idx;
3843 write_mask = output_signature[i].mask;
3844 shader_glsl_write_mask_to_str(write_mask, reg_mask);
3845
3846 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3847 {
3848 if (semantic_idx == 0)
3849 shader_addline(buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3850 else if (semantic_idx == 1)
3851 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3852 }
3853 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3854 {
3855 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3856 }
3857 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3858 {
3859 if (semantic_idx < 8)
3860 {
3861 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
3862 write_mask |= WINED3DSP_WRITEMASK_3;
3863
3864 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3865 semantic_idx, reg_mask, i, reg_mask);
3866 if (!(write_mask & WINED3DSP_WRITEMASK_3))
3867 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
3868 }
3869 }
3870 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3871 {
3872 shader_addline(buffer, "gl_PointSize = OUT[%u].x;\n", i);
3873 }
3874 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3875 {
3876 shader_addline(buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3877 }
3878 }
3879 shader_addline(buffer, "}\n");
3880
3881 } else if(ps_major >= 3 && vs_major >= 3) {
3882 WORD map = vs->baseShader.reg_maps.output_registers;
3883
3884 output_signature = vs->baseShader.output_signature;
3885
3886 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3887 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info));
3888 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3889
3890 /* First, sort out position and point size. Those are not passed to the pixel shader */
3891 for (i = 0; map; map >>= 1, ++i)
3892 {
3893 if (!(map & 1)) continue;
3894
3895 semantic_name = output_signature[i].semantic_name;
3896 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
3897
3898 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3899 {
3900 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3901 }
3902 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3903 {
3904 shader_addline(buffer, "gl_PointSize = OUT[%u].x;\n", i);
3905 }
3906 }
3907
3908 /* Then, fix the pixel shader input */
3909 handle_ps3_input(buffer, gl_info, ps->input_reg_map, ps->baseShader.input_signature,
3910 &ps->baseShader.reg_maps, output_signature, &vs->baseShader.reg_maps);
3911
3912 shader_addline(buffer, "}\n");
3913 } else if(ps_major >= 3 && vs_major < 3) {
3914 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info));
3915 shader_addline(buffer, "void order_ps_input() {\n");
3916 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3917 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3918 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3919 */
3920 handle_ps3_input(buffer, gl_info, ps->input_reg_map, ps->baseShader.input_signature,
3921 &ps->baseShader.reg_maps, NULL, NULL);
3922 shader_addline(buffer, "}\n");
3923 } else {
3924 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
3925 }
3926
3927 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
3928 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3929 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer->buffer, NULL));
3930 checkGLcall("glShaderSourceARB(ret, 1, &buffer->buffer, NULL)");
3931 GL_EXTCALL(glCompileShaderARB(ret));
3932 checkGLcall("glCompileShaderARB(ret)");
3933
3934 return ret;
3935}
3936
3937/* GL locking is done by the caller */
3938static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const struct wined3d_gl_info *gl_info,
3939 GLhandleARB programId, char prefix)
3940{
3941 const local_constant *lconst;
3942 GLint tmp_loc;
3943 const float *value;
3944 char glsl_name[8];
3945
3946 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
3947 value = (const float *)lconst->value;
3948 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
3949 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
3950 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
3951 }
3952 checkGLcall("Hardcoding local constants");
3953}
3954
3955/* GL locking is done by the caller */
3956static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
3957 struct wined3d_shader_buffer *buffer, IWineD3DPixelShaderImpl *This,
3958 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
3959{
3960 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
3961 const struct wined3d_gl_info *gl_info = context->gl_info;
3962 CONST DWORD *function = This->baseShader.function;
3963 struct shader_glsl_ctx_priv priv_ctx;
3964
3965 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3966 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
3967
3968 memset(&priv_ctx, 0, sizeof(priv_ctx));
3969 priv_ctx.cur_ps_args = args;
3970 priv_ctx.cur_np2fixup_info = np2fixup_info;
3971
3972 shader_addline(buffer, "#version 120\n");
3973
3974 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] && reg_maps->usestexldd)
3975 {
3976 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
3977 }
3978 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
3979 {
3980 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3981 * drivers write a warning if we don't do so
3982 */
3983 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
3984 }
3985 if (gl_info->supported[EXT_GPU_SHADER4])
3986 {
3987 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
3988 }
3989
3990 /* Base Declarations */
3991 shader_generate_glsl_declarations(context, buffer, (IWineD3DBaseShader *)This, reg_maps, &priv_ctx);
3992
3993 /* Pack 3.0 inputs */
3994 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
3995 {
3996 shader_glsl_input_pack((IWineD3DPixelShader *) This, buffer,
3997 This->baseShader.input_signature, reg_maps, args->vp_mode);
3998 }
3999
4000 /* Base Shader Body */
4001 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function, &priv_ctx);
4002
4003 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4004 if (reg_maps->shader_version.major < 2)
4005 {
4006 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4007 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4008 }
4009
4010 if (args->srgb_correction)
4011 {
4012 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4013 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4014 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4015 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4016 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4017 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4018 }
4019 /* Pixel shader < 3.0 do not replace the fog stage.
4020 * This implements linear fog computation and blending.
4021 * TODO: non linear fog
4022 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4023 * -1/(e-s) and e/(e-s) respectively.
4024 */
4025 if (reg_maps->shader_version.major < 3)
4026 {
4027 switch(args->fog) {
4028 case FOG_OFF: break;
4029 case FOG_LINEAR:
4030 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4031 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4032 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4033 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4034 break;
4035 case FOG_EXP:
4036 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4037 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4038 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4039 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4040 break;
4041 case FOG_EXP2:
4042 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4043 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4044 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4045 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4046 break;
4047 }
4048 }
4049
4050 shader_addline(buffer, "}\n");
4051
4052 TRACE("Compiling shader object %u\n", shader_obj);
4053 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4054 GL_EXTCALL(glCompileShaderARB(shader_obj));
4055 print_glsl_info_log(gl_info, shader_obj);
4056
4057 /* Store the shader object */
4058 return shader_obj;
4059}
4060
4061/* GL locking is done by the caller */
4062static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4063 struct wined3d_shader_buffer *buffer, IWineD3DVertexShaderImpl *This,
4064 const struct vs_compile_args *args)
4065{
4066 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
4067 const struct wined3d_gl_info *gl_info = context->gl_info;
4068 CONST DWORD *function = This->baseShader.function;
4069 struct shader_glsl_ctx_priv priv_ctx;
4070
4071 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4072 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4073
4074 shader_addline(buffer, "#version 120\n");
4075
4076 if (gl_info->supported[EXT_GPU_SHADER4])
4077 {
4078 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4079 }
4080
4081 memset(&priv_ctx, 0, sizeof(priv_ctx));
4082 priv_ctx.cur_vs_args = args;
4083
4084 /* Base Declarations */
4085 shader_generate_glsl_declarations(context, buffer, (IWineD3DBaseShader *)This, reg_maps, &priv_ctx);
4086
4087 /* Base Shader Body */
4088 shader_generate_main((IWineD3DBaseShader*)This, buffer, reg_maps, function, &priv_ctx);
4089
4090 /* Unpack 3.0 outputs */
4091 if (reg_maps->shader_version.major >= 3) shader_addline(buffer, "order_ps_input(OUT);\n");
4092 else shader_addline(buffer, "order_ps_input();\n");
4093
4094 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4095 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4096 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4097 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4098 */
4099 if(args->fog_src == VS_FOG_Z) {
4100 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4101 } else if (!reg_maps->fog) {
4102 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4103 }
4104
4105 /* Write the final position.
4106 *
4107 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4108 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4109 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4110 * contains 1.0 to allow a mad.
4111 */
4112 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4113 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4114 if(args->clip_enabled) {
4115 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4116 }
4117
4118 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4119 *
4120 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4121 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4122 * which is the same as z = z * 2 - w.
4123 */
4124 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4125
4126 shader_addline(buffer, "}\n");
4127
4128 TRACE("Compiling shader object %u\n", shader_obj);
4129 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4130 GL_EXTCALL(glCompileShaderARB(shader_obj));
4131 print_glsl_info_log(gl_info, shader_obj);
4132
4133 return shader_obj;
4134}
4135
4136static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4137 struct wined3d_shader_buffer *buffer, IWineD3DPixelShaderImpl *shader,
4138 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4139{
4140 UINT i;
4141 DWORD new_size;
4142 struct glsl_ps_compiled_shader *new_array;
4143 struct glsl_pshader_private *shader_data;
4144 struct ps_np2fixup_info *np2fixup = NULL;
4145 GLhandleARB ret;
4146
4147 if (!shader->baseShader.backend_data)
4148 {
4149 shader->baseShader.backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4150 if (!shader->baseShader.backend_data)
4151 {
4152 ERR("Failed to allocate backend data.\n");
4153 return 0;
4154 }
4155 }
4156 shader_data = shader->baseShader.backend_data;
4157
4158 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4159 * so a linear search is more performant than a hashmap or a binary search
4160 * (cache coherency etc)
4161 */
4162 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4163 if(memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)) == 0) {
4164 if(args->np2_fixup) *np2fixup_info = &shader_data->gl_shaders[i].np2fixup;
4165 return shader_data->gl_shaders[i].prgId;
4166 }
4167 }
4168
4169 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4170 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4171 if (shader_data->num_gl_shaders)
4172 {
4173 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4174 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4175 new_size * sizeof(*shader_data->gl_shaders));
4176 } else {
4177 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4178 new_size = 1;
4179 }
4180
4181 if(!new_array) {
4182 ERR("Out of memory\n");
4183 return 0;
4184 }
4185 shader_data->gl_shaders = new_array;
4186 shader_data->shader_array_size = new_size;
4187 }
4188
4189 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4190
4191 memset(&shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup, 0, sizeof(struct ps_np2fixup_info));
4192 if (args->np2_fixup) np2fixup = &shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup;
4193
4194 pixelshader_update_samplers(&shader->baseShader.reg_maps,
4195 ((IWineD3DDeviceImpl *)shader->baseShader.device)->stateBlock->textures);
4196
4197 shader_buffer_clear(buffer);
4198 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4199 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4200 *np2fixup_info = np2fixup;
4201
4202 return ret;
4203}
4204
4205static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4206 const DWORD use_map) {
4207 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4208 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4209 return stored->fog_src == new->fog_src;
4210}
4211
4212static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4213 struct wined3d_shader_buffer *buffer, IWineD3DVertexShaderImpl *shader,
4214 const struct vs_compile_args *args)
4215{
4216 UINT i;
4217 DWORD new_size;
4218 struct glsl_vs_compiled_shader *new_array;
4219 DWORD use_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.use_map;
4220 struct glsl_vshader_private *shader_data;
4221 GLhandleARB ret;
4222
4223 if (!shader->baseShader.backend_data)
4224 {
4225 shader->baseShader.backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4226 if (!shader->baseShader.backend_data)
4227 {
4228 ERR("Failed to allocate backend data.\n");
4229 return 0;
4230 }
4231 }
4232 shader_data = shader->baseShader.backend_data;
4233
4234 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4235 * so a linear search is more performant than a hashmap or a binary search
4236 * (cache coherency etc)
4237 */
4238 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4239 if(vs_args_equal(&shader_data->gl_shaders[i].args, args, use_map)) {
4240 return shader_data->gl_shaders[i].prgId;
4241 }
4242 }
4243
4244 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4245
4246 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4247 if (shader_data->num_gl_shaders)
4248 {
4249 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4250 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4251 new_size * sizeof(*shader_data->gl_shaders));
4252 } else {
4253 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4254 new_size = 1;
4255 }
4256
4257 if(!new_array) {
4258 ERR("Out of memory\n");
4259 return 0;
4260 }
4261 shader_data->gl_shaders = new_array;
4262 shader_data->shader_array_size = new_size;
4263 }
4264
4265 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4266
4267 shader_buffer_clear(buffer);
4268 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4269 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4270
4271 return ret;
4272}
4273
4274/** Sets the GLSL program ID for the given pixel and vertex shader combination.
4275 * It sets the programId on the current StateBlock (because it should be called
4276 * inside of the DrawPrimitive() part of the render loop).
4277 *
4278 * If a program for the given combination does not exist, create one, and store
4279 * the program in the hash table. If it creates a program, it will link the
4280 * given objects, too.
4281 */
4282
4283/* GL locking is done by the caller */
4284static void set_glsl_shader_program(const struct wined3d_context *context,
4285 IWineD3DDeviceImpl *device, BOOL use_ps, BOOL use_vs)
4286{
4287 IWineD3DVertexShader *vshader = use_vs ? device->stateBlock->vertexShader : NULL;
4288 IWineD3DPixelShader *pshader = use_ps ? device->stateBlock->pixelShader : NULL;
4289 const struct wined3d_gl_info *gl_info = context->gl_info;
4290 struct shader_glsl_priv *priv = device->shader_priv;
4291 struct glsl_shader_prog_link *entry = NULL;
4292 GLhandleARB programId = 0;
4293 GLhandleARB reorder_shader_id = 0;
4294 unsigned int i;
4295 char glsl_name[8];
4296 struct ps_compile_args ps_compile_args;
4297 struct vs_compile_args vs_compile_args;
4298
4299 if (vshader) find_vs_compile_args((IWineD3DVertexShaderImpl *)vshader, device->stateBlock, &vs_compile_args);
4300 if (pshader) find_ps_compile_args((IWineD3DPixelShaderImpl *)pshader, device->stateBlock, &ps_compile_args);
4301
4302 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args);
4303 if (entry) {
4304 priv->glsl_program = entry;
4305 return;
4306 }
4307
4308 /* If we get to this point, then no matching program exists, so we create one */
4309 programId = GL_EXTCALL(glCreateProgramObjectARB());
4310 TRACE("Created new GLSL shader program %u\n", programId);
4311
4312 /* Create the entry */
4313 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
4314 entry->programId = programId;
4315 entry->vshader = vshader;
4316 entry->pshader = pshader;
4317 entry->vs_args = vs_compile_args;
4318 entry->ps_args = ps_compile_args;
4319 entry->constant_version = 0;
4320 entry->np2Fixup_info = NULL;
4321 /* Add the hash table entry */
4322 add_glsl_program_entry(priv, entry);
4323
4324 /* Set the current program */
4325 priv->glsl_program = entry;
4326
4327 /* Attach GLSL vshader */
4328 if (vshader)
4329 {
4330 GLhandleARB vshader_id = find_glsl_vshader(context, &priv->shader_buffer,
4331 (IWineD3DVertexShaderImpl *)vshader, &vs_compile_args);
4332 WORD map = ((IWineD3DBaseShaderImpl *)vshader)->baseShader.reg_maps.input_registers;
4333 char tmp_name[10];
4334
4335 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
4336 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
4337 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
4338 checkGLcall("glAttachObjectARB");
4339 /* Flag the reorder function for deletion, then it will be freed automatically when the program
4340 * is destroyed
4341 */
4342 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
4343
4344 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
4345 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
4346 checkGLcall("glAttachObjectARB");
4347
4348 /* Bind vertex attributes to a corresponding index number to match
4349 * the same index numbers as ARB_vertex_programs (makes loading
4350 * vertex attributes simpler). With this method, we can use the
4351 * exact same code to load the attributes later for both ARB and
4352 * GLSL shaders.
4353 *
4354 * We have to do this here because we need to know the Program ID
4355 * in order to make the bindings work, and it has to be done prior
4356 * to linking the GLSL program. */
4357 for (i = 0; map; map >>= 1, ++i)
4358 {
4359 if (!(map & 1)) continue;
4360
4361 snprintf(tmp_name, sizeof(tmp_name), "attrib%u", i);
4362 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
4363 }
4364 checkGLcall("glBindAttribLocationARB");
4365
4366 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
4367 }
4368
4369 /* Attach GLSL pshader */
4370 if (pshader)
4371 {
4372 GLhandleARB pshader_id = find_glsl_pshader(context, &priv->shader_buffer,
4373 (IWineD3DPixelShaderImpl *)pshader, &ps_compile_args, &entry->np2Fixup_info);
4374 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
4375 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
4376 checkGLcall("glAttachObjectARB");
4377
4378 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
4379 }
4380
4381 /* Link the program */
4382 TRACE("Linking GLSL shader program %u\n", programId);
4383 GL_EXTCALL(glLinkProgramARB(programId));
4384 shader_glsl_validate_link(gl_info, programId);
4385
4386 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4387 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
4388 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
4389 {
4390 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
4391 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4392 }
4393 for (i = 0; i < MAX_CONST_I; ++i)
4394 {
4395 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
4396 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4397 }
4398 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4399 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
4400 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
4401 {
4402 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
4403 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4404 }
4405 for (i = 0; i < MAX_CONST_I; ++i)
4406 {
4407 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
4408 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4409 }
4410
4411 if(pshader) {
4412 char name[32];
4413
4414 for(i = 0; i < MAX_TEXTURES; i++) {
4415 sprintf(name, "bumpenvmat%u", i);
4416 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4417 sprintf(name, "luminancescale%u", i);
4418 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4419 sprintf(name, "luminanceoffset%u", i);
4420 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4421 }
4422
4423 if (ps_compile_args.np2_fixup) {
4424 if (entry->np2Fixup_info) {
4425 entry->np2Fixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "PsamplerNP2Fixup"));
4426 } else {
4427 FIXME("NP2 texcoord fixup needed for this pixelshader, but no fixup uniform found.\n");
4428 }
4429 }
4430 }
4431
4432 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
4433 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
4434 checkGLcall("Find glsl program uniform locations");
4435
4436 if (pshader
4437 && ((IWineD3DPixelShaderImpl *)pshader)->baseShader.reg_maps.shader_version.major >= 3
4438 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > vec4_varyings(3, gl_info))
4439 {
4440 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
4441 entry->vertex_color_clamp = GL_FALSE;
4442 } else {
4443 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
4444 }
4445
4446 /* Set the shader to allow uniform loading on it */
4447 GL_EXTCALL(glUseProgramObjectARB(programId));
4448 checkGLcall("glUseProgramObjectARB(programId)");
4449
4450 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4451 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4452 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4453 * vertex shader with fixed function pixel processing is used we make sure that the card
4454 * supports enough samplers to allow the max number of vertex samplers with all possible
4455 * fixed function fragment processing setups. So once the program is linked these samplers
4456 * won't change.
4457 */
4458 if (vshader) shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
4459 if (pshader) shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
4460
4461 /* If the local constants do not have to be loaded with the environment constants,
4462 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4463 * later
4464 */
4465 if (pshader && !((IWineD3DBaseShaderImpl *)pshader)->baseShader.load_local_constsF)
4466 {
4467 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
4468 }
4469 if (vshader && !((IWineD3DBaseShaderImpl *)vshader)->baseShader.load_local_constsF)
4470 {
4471 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
4472 }
4473}
4474
4475/* GL locking is done by the caller */
4476static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type)
4477{
4478 GLhandleARB program_id;
4479 GLhandleARB vshader_id, pshader_id;
4480 static const char *blt_vshader[] =
4481 {
4482 "#version 120\n"
4483 "void main(void)\n"
4484 "{\n"
4485 " gl_Position = gl_Vertex;\n"
4486 " gl_FrontColor = vec4(1.0);\n"
4487 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4488 "}\n"
4489 };
4490
4491 static const char *blt_pshaders[tex_type_count] =
4492 {
4493 /* tex_1d */
4494 NULL,
4495 /* tex_2d */
4496 "#version 120\n"
4497 "uniform sampler2D sampler;\n"
4498 "void main(void)\n"
4499 "{\n"
4500 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4501 "}\n",
4502 /* tex_3d */
4503 NULL,
4504 /* tex_cube */
4505 "#version 120\n"
4506 "uniform samplerCube sampler;\n"
4507 "void main(void)\n"
4508 "{\n"
4509 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4510 "}\n",
4511 /* tex_rect */
4512 "#version 120\n"
4513 "#extension GL_ARB_texture_rectangle : enable\n"
4514 "uniform sampler2DRect sampler;\n"
4515 "void main(void)\n"
4516 "{\n"
4517 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4518 "}\n",
4519 };
4520
4521 if (!blt_pshaders[tex_type])
4522 {
4523 FIXME("tex_type %#x not supported\n", tex_type);
4524 tex_type = tex_2d;
4525 }
4526
4527 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4528 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
4529 GL_EXTCALL(glCompileShaderARB(vshader_id));
4530
4531 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4532 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
4533 GL_EXTCALL(glCompileShaderARB(pshader_id));
4534
4535 program_id = GL_EXTCALL(glCreateProgramObjectARB());
4536 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
4537 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
4538 GL_EXTCALL(glLinkProgramARB(program_id));
4539
4540 shader_glsl_validate_link(gl_info, program_id);
4541
4542 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4543 * is destroyed
4544 */
4545 GL_EXTCALL(glDeleteObjectARB(vshader_id));
4546 GL_EXTCALL(glDeleteObjectARB(pshader_id));
4547 return program_id;
4548}
4549
4550/* GL locking is done by the caller */
4551static void shader_glsl_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
4552{
4553 const struct wined3d_gl_info *gl_info = context->gl_info;
4554 IWineD3DDeviceImpl *device = context->swapchain->device;
4555 struct shader_glsl_priv *priv = device->shader_priv;
4556 GLhandleARB program_id = 0;
4557 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
4558
4559 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4560
4561 if (useVS || usePS) set_glsl_shader_program(context, device, usePS, useVS);
4562 else priv->glsl_program = NULL;
4563
4564 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4565
4566 if (old_vertex_color_clamp != current_vertex_color_clamp)
4567 {
4568 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
4569 {
4570 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
4571 checkGLcall("glClampColorARB");
4572 }
4573 else
4574 {
4575 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4576 }
4577 }
4578
4579 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4580 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4581 GL_EXTCALL(glUseProgramObjectARB(program_id));
4582 checkGLcall("glUseProgramObjectARB");
4583
4584 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
4585 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
4586 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
4587 if (priv->glsl_program && priv->glsl_program->np2Fixup_info)
4588 {
4589 shader_glsl_load_np2fixup_constants((IWineD3DDevice *)device, usePS, useVS);
4590 }
4591}
4592
4593/* GL locking is done by the caller */
4594static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
4595 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4596 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4597 struct shader_glsl_priv *priv = This->shader_priv;
4598 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
4599
4600 if (!*blt_program) {
4601 GLint loc;
4602 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
4603 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
4604 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4605 GL_EXTCALL(glUniform1iARB(loc, 0));
4606 } else {
4607 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4608 }
4609}
4610
4611/* GL locking is done by the caller */
4612static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
4613 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4614 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4615 struct shader_glsl_priv *priv = This->shader_priv;
4616 GLhandleARB program_id;
4617
4618 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4619 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4620
4621 GL_EXTCALL(glUseProgramObjectARB(program_id));
4622 checkGLcall("glUseProgramObjectARB");
4623}
4624
4625static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
4626 const struct list *linked_programs;
4627 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
4628 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
4629 struct shader_glsl_priv *priv = device->shader_priv;
4630 const struct wined3d_gl_info *gl_info;
4631 struct wined3d_context *context;
4632
4633 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
4634 * can be called from IWineD3DBaseShader::Release
4635 */
4636 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
4637
4638 if(pshader) {
4639 struct glsl_pshader_private *shader_data;
4640 shader_data = This->baseShader.backend_data;
4641 if(!shader_data || shader_data->num_gl_shaders == 0)
4642 {
4643 HeapFree(GetProcessHeap(), 0, shader_data);
4644 This->baseShader.backend_data = NULL;
4645 return;
4646 }
4647
4648 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4649 gl_info = context->gl_info;
4650
4651 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->pshader == iface)
4652 {
4653 ENTER_GL();
4654 shader_glsl_select(context, FALSE, FALSE);
4655 LEAVE_GL();
4656 }
4657 } else {
4658 struct glsl_vshader_private *shader_data;
4659 shader_data = This->baseShader.backend_data;
4660 if(!shader_data || shader_data->num_gl_shaders == 0)
4661 {
4662 HeapFree(GetProcessHeap(), 0, shader_data);
4663 This->baseShader.backend_data = NULL;
4664 return;
4665 }
4666
4667 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4668 gl_info = context->gl_info;
4669
4670 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->vshader == iface)
4671 {
4672 ENTER_GL();
4673 shader_glsl_select(context, FALSE, FALSE);
4674 LEAVE_GL();
4675 }
4676 }
4677
4678 linked_programs = &This->baseShader.linked_programs;
4679
4680 TRACE("Deleting linked programs\n");
4681 if (linked_programs->next) {
4682 struct glsl_shader_prog_link *entry, *entry2;
4683
4684 ENTER_GL();
4685 if(pshader) {
4686 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
4687 delete_glsl_program_entry(priv, gl_info, entry);
4688 }
4689 } else {
4690 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
4691 delete_glsl_program_entry(priv, gl_info, entry);
4692 }
4693 }
4694 LEAVE_GL();
4695 }
4696
4697 if(pshader) {
4698 UINT i;
4699 struct glsl_pshader_private *shader_data = This->baseShader.backend_data;
4700
4701 ENTER_GL();
4702 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4703 TRACE("deleting pshader %u\n", shader_data->gl_shaders[i].prgId);
4704 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4705 checkGLcall("glDeleteObjectARB");
4706 }
4707 LEAVE_GL();
4708 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4709 }
4710 else
4711 {
4712 UINT i;
4713 struct glsl_vshader_private *shader_data = This->baseShader.backend_data;
4714
4715 ENTER_GL();
4716 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4717 TRACE("deleting vshader %u\n", shader_data->gl_shaders[i].prgId);
4718 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4719 checkGLcall("glDeleteObjectARB");
4720 }
4721 LEAVE_GL();
4722 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4723 }
4724
4725 HeapFree(GetProcessHeap(), 0, This->baseShader.backend_data);
4726 This->baseShader.backend_data = NULL;
4727
4728 context_release(context);
4729}
4730
4731static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
4732{
4733 const glsl_program_key_t *k = key;
4734 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
4735 const struct glsl_shader_prog_link, program_lookup_entry);
4736 int cmp;
4737
4738 if (k->vshader > prog->vshader) return 1;
4739 else if (k->vshader < prog->vshader) return -1;
4740
4741 if (k->pshader > prog->pshader) return 1;
4742 else if (k->pshader < prog->pshader) return -1;
4743
4744 if (k->vshader && (cmp = memcmp(&k->vs_args, &prog->vs_args, sizeof(prog->vs_args)))) return cmp;
4745 if (k->pshader && (cmp = memcmp(&k->ps_args, &prog->ps_args, sizeof(prog->ps_args)))) return cmp;
4746
4747 return 0;
4748}
4749
4750static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
4751{
4752 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
4753 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
4754
4755 if (!mem)
4756 {
4757 ERR("Failed to allocate memory\n");
4758 return FALSE;
4759 }
4760
4761 heap->entries = mem;
4762 heap->entries[1].version = 0;
4763 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
4764 heap->size = 1;
4765
4766 return TRUE;
4767}
4768
4769static void constant_heap_free(struct constant_heap *heap)
4770{
4771 HeapFree(GetProcessHeap(), 0, heap->entries);
4772}
4773
4774static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
4775{
4776 wined3d_rb_alloc,
4777 wined3d_rb_realloc,
4778 wined3d_rb_free,
4779 glsl_program_key_compare,
4780};
4781
4782static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
4783 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4784 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4785 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
4786 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
4787 gl_info->limits.glsl_ps_float_constants)) + 1;
4788
4789 if (!shader_buffer_init(&priv->shader_buffer))
4790 {
4791 ERR("Failed to initialize shader buffer.\n");
4792 goto fail;
4793 }
4794
4795 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
4796 if (!priv->stack)
4797 {
4798 ERR("Failed to allocate memory.\n");
4799 goto fail;
4800 }
4801
4802 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
4803 {
4804 ERR("Failed to initialize vertex shader constant heap\n");
4805 goto fail;
4806 }
4807
4808 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
4809 {
4810 ERR("Failed to initialize pixel shader constant heap\n");
4811 goto fail;
4812 }
4813
4814 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
4815 {
4816 ERR("Failed to initialize rbtree.\n");
4817 goto fail;
4818 }
4819
4820 priv->next_constant_version = 1;
4821
4822 This->shader_priv = priv;
4823 return WINED3D_OK;
4824
4825fail:
4826 constant_heap_free(&priv->pconst_heap);
4827 constant_heap_free(&priv->vconst_heap);
4828 HeapFree(GetProcessHeap(), 0, priv->stack);
4829 shader_buffer_free(&priv->shader_buffer);
4830 HeapFree(GetProcessHeap(), 0, priv);
4831 return E_OUTOFMEMORY;
4832}
4833
4834/* Context activation is done by the caller. */
4835static void shader_glsl_free(IWineD3DDevice *iface) {
4836 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4837 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4838 struct shader_glsl_priv *priv = This->shader_priv;
4839 int i;
4840
4841 ENTER_GL();
4842 for (i = 0; i < tex_type_count; ++i)
4843 {
4844 if (priv->depth_blt_program[i])
4845 {
4846 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
4847 }
4848 }
4849 LEAVE_GL();
4850
4851 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
4852 constant_heap_free(&priv->pconst_heap);
4853 constant_heap_free(&priv->vconst_heap);
4854 HeapFree(GetProcessHeap(), 0, priv->stack);
4855 shader_buffer_free(&priv->shader_buffer);
4856
4857 HeapFree(GetProcessHeap(), 0, This->shader_priv);
4858 This->shader_priv = NULL;
4859}
4860
4861static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
4862 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
4863 return FALSE;
4864}
4865
4866static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *pCaps)
4867{
4868 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
4869 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support based
4870 * on the version of NV_vertex_program.
4871 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
4872 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
4873 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
4874 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
4875 */
4876 if ((gl_info->supported[NV_VERTEX_PROGRAM2] && !gl_info->supported[NV_VERTEX_PROGRAM3])
4877 || gl_info->limits.arb_ps_instructions <= 512)
4878 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
4879 else
4880 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
4881 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
4882 pCaps->MaxVertexShaderConst = gl_info->limits.glsl_vs_float_constants;
4883
4884 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
4885 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
4886 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
4887 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
4888 * in max native instructions. Intel and others also offer the info in this extension but they
4889 * don't support GLSL (at least on Windows).
4890 *
4891 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
4892 * of instructions is 512 or less we have to do with ps2.0 hardware.
4893 * 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.
4894 */
4895 if ((gl_info->supported[NV_FRAGMENT_PROGRAM] && !gl_info->supported[NV_FRAGMENT_PROGRAM2])
4896 || gl_info->limits.arb_ps_instructions <= 512)
4897 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
4898 else
4899 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
4900
4901 pCaps->MaxPixelShaderConst = gl_info->limits.glsl_ps_float_constants;
4902
4903 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4904 * Direct3D minimum requirement.
4905 *
4906 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4907 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4908 *
4909 * The problem is that the refrast clamps temporary results in the shader to
4910 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4911 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4912 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4913 * offer a way to query this.
4914 */
4915 pCaps->PixelShader1xMaxValue = 8.0;
4916 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
4917
4918 pCaps->VSClipping = TRUE;
4919}
4920
4921static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
4922{
4923 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
4924 {
4925 TRACE("Checking support for fixup:\n");
4926 dump_color_fixup_desc(fixup);
4927 }
4928
4929 /* We support everything except YUV conversions. */
4930 if (!is_complex_fixup(fixup))
4931 {
4932 TRACE("[OK]\n");
4933 return TRUE;
4934 }
4935
4936 TRACE("[FAILED]\n");
4937 return FALSE;
4938}
4939
4940static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
4941{
4942 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
4943 /* WINED3DSIH_ADD */ shader_glsl_arith,
4944 /* WINED3DSIH_BEM */ shader_glsl_bem,
4945 /* WINED3DSIH_BREAK */ shader_glsl_break,
4946 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
4947 /* WINED3DSIH_BREAKP */ NULL,
4948 /* WINED3DSIH_CALL */ shader_glsl_call,
4949 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
4950 /* WINED3DSIH_CMP */ shader_glsl_cmp,
4951 /* WINED3DSIH_CND */ shader_glsl_cnd,
4952 /* WINED3DSIH_CRS */ shader_glsl_cross,
4953 /* WINED3DSIH_CUT */ NULL,
4954 /* WINED3DSIH_DCL */ NULL,
4955 /* WINED3DSIH_DEF */ NULL,
4956 /* WINED3DSIH_DEFB */ NULL,
4957 /* WINED3DSIH_DEFI */ NULL,
4958 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
4959 /* WINED3DSIH_DP3 */ shader_glsl_dot,
4960 /* WINED3DSIH_DP4 */ shader_glsl_dot,
4961 /* WINED3DSIH_DST */ shader_glsl_dst,
4962 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
4963 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
4964 /* WINED3DSIH_ELSE */ shader_glsl_else,
4965 /* WINED3DSIH_EMIT */ NULL,
4966 /* WINED3DSIH_ENDIF */ shader_glsl_end,
4967 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
4968 /* WINED3DSIH_ENDREP */ shader_glsl_end,
4969 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
4970 /* WINED3DSIH_EXPP */ shader_glsl_expp,
4971 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
4972 /* WINED3DSIH_IADD */ NULL,
4973 /* WINED3DSIH_IF */ shader_glsl_if,
4974 /* WINED3DSIH_IFC */ shader_glsl_ifc,
4975 /* WINED3DSIH_IGE */ NULL,
4976 /* WINED3DSIH_LABEL */ shader_glsl_label,
4977 /* WINED3DSIH_LIT */ shader_glsl_lit,
4978 /* WINED3DSIH_LOG */ shader_glsl_log,
4979 /* WINED3DSIH_LOGP */ shader_glsl_log,
4980 /* WINED3DSIH_LOOP */ shader_glsl_loop,
4981 /* WINED3DSIH_LRP */ shader_glsl_lrp,
4982 /* WINED3DSIH_LT */ NULL,
4983 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
4984 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
4985 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
4986 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
4987 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
4988 /* WINED3DSIH_MAD */ shader_glsl_mad,
4989 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
4990 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
4991 /* WINED3DSIH_MOV */ shader_glsl_mov,
4992 /* WINED3DSIH_MOVA */ shader_glsl_mov,
4993 /* WINED3DSIH_MUL */ shader_glsl_arith,
4994 /* WINED3DSIH_NOP */ NULL,
4995 /* WINED3DSIH_NRM */ shader_glsl_nrm,
4996 /* WINED3DSIH_PHASE */ NULL,
4997 /* WINED3DSIH_POW */ shader_glsl_pow,
4998 /* WINED3DSIH_RCP */ shader_glsl_rcp,
4999 /* WINED3DSIH_REP */ shader_glsl_rep,
5000 /* WINED3DSIH_RET */ shader_glsl_ret,
5001 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
5002 /* WINED3DSIH_SETP */ NULL,
5003 /* WINED3DSIH_SGE */ shader_glsl_compare,
5004 /* WINED3DSIH_SGN */ shader_glsl_sgn,
5005 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
5006 /* WINED3DSIH_SLT */ shader_glsl_compare,
5007 /* WINED3DSIH_SUB */ shader_glsl_arith,
5008 /* WINED3DSIH_TEX */ shader_glsl_tex,
5009 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
5010 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
5011 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
5012 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
5013 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
5014 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
5015 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
5016 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
5017 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
5018 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
5019 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
5020 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
5021 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
5022 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
5023 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
5024 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
5025 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
5026 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
5027 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
5028 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
5029 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
5030};
5031
5032static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
5033 SHADER_HANDLER hw_fct;
5034
5035 /* Select handler */
5036 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
5037
5038 /* Unhandled opcode */
5039 if (!hw_fct)
5040 {
5041 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
5042 return;
5043 }
5044 hw_fct(ins);
5045
5046 shader_glsl_add_instruction_modifiers(ins);
5047}
5048
5049const shader_backend_t glsl_shader_backend = {
5050 shader_glsl_handle_instruction,
5051 shader_glsl_select,
5052 shader_glsl_select_depth_blt,
5053 shader_glsl_deselect_depth_blt,
5054 shader_glsl_update_float_vertex_constants,
5055 shader_glsl_update_float_pixel_constants,
5056 shader_glsl_load_constants,
5057 shader_glsl_load_np2fixup_constants,
5058 shader_glsl_destroy,
5059 shader_glsl_alloc,
5060 shader_glsl_free,
5061 shader_glsl_dirty_const,
5062 shader_glsl_get_caps,
5063 shader_glsl_color_fixup_supported,
5064};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette