[DIRECT3D]
[reactos.git] / reactos / dll / directx / wine / wined3d / glsl_shader.c
1 /*
2 * GLSL pixel and vertex shader implementation
3 *
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 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 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
30 */
31
32 #include "wined3d_private.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
35 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
38
39 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
40 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
41 #define WINED3D_GLSL_SAMPLE_LOD 0x4
42 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
43
44 struct glsl_dst_param
45 {
46 char reg_name[150];
47 char mask_str[6];
48 };
49
50 struct glsl_src_param
51 {
52 char reg_name[150];
53 char param_str[200];
54 };
55
56 struct glsl_sample_function
57 {
58 const char *name;
59 DWORD coord_mask;
60 };
61
62 enum heap_node_op
63 {
64 HEAP_NODE_TRAVERSE_LEFT,
65 HEAP_NODE_TRAVERSE_RIGHT,
66 HEAP_NODE_POP,
67 };
68
69 struct constant_entry
70 {
71 unsigned int idx;
72 unsigned int version;
73 };
74
75 struct constant_heap
76 {
77 struct constant_entry *entries;
78 BOOL *contained;
79 unsigned int *positions;
80 unsigned int size;
81 };
82
83 /* GLSL shader private data */
84 struct shader_glsl_priv {
85 struct wined3d_shader_buffer shader_buffer;
86 struct wine_rb_tree program_lookup;
87 struct constant_heap vconst_heap;
88 struct constant_heap pconst_heap;
89 unsigned char *stack;
90 GLhandleARB depth_blt_program_full[tex_type_count];
91 GLhandleARB depth_blt_program_masked[tex_type_count];
92 UINT next_constant_version;
93
94 const struct wined3d_vertex_pipe_ops *vertex_pipe;
95 const struct fragment_pipeline *fragment_pipe;
96 struct wine_rb_tree ffp_vertex_shaders;
97 struct wine_rb_tree ffp_fragment_shaders;
98 BOOL ffp_proj_control;
99 };
100
101 struct glsl_vs_program
102 {
103 struct list shader_entry;
104 GLhandleARB id;
105 GLenum vertex_color_clamp;
106 GLint *uniform_f_locations;
107 GLint uniform_i_locations[MAX_CONST_I];
108 GLint pos_fixup_location;
109 };
110
111 struct glsl_gs_program
112 {
113 struct list shader_entry;
114 GLhandleARB id;
115 };
116
117 struct glsl_ps_program
118 {
119 struct list shader_entry;
120 GLhandleARB id;
121 GLint *uniform_f_locations;
122 GLint uniform_i_locations[MAX_CONST_I];
123 GLint bumpenv_mat_location[MAX_TEXTURES];
124 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
125 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
126 GLint tex_factor_location;
127 GLint specular_enable_location;
128 GLint ycorrection_location;
129 GLint np2_fixup_location;
130 const struct ps_np2fixup_info *np2_fixup_info;
131 };
132
133 /* Struct to maintain data about a linked GLSL program */
134 struct glsl_shader_prog_link
135 {
136 struct wine_rb_entry program_lookup_entry;
137 struct glsl_vs_program vs;
138 struct glsl_gs_program gs;
139 struct glsl_ps_program ps;
140 GLhandleARB programId;
141 DWORD constant_update_mask;
142 UINT constant_version;
143 };
144
145 struct glsl_program_key
146 {
147 GLhandleARB vs_id;
148 GLhandleARB gs_id;
149 GLhandleARB ps_id;
150 };
151
152 struct shader_glsl_ctx_priv {
153 const struct vs_compile_args *cur_vs_args;
154 const struct ps_compile_args *cur_ps_args;
155 struct ps_np2fixup_info *cur_np2fixup_info;
156 };
157
158 struct glsl_context_data
159 {
160 struct glsl_shader_prog_link *glsl_program;
161 };
162
163 struct glsl_ps_compiled_shader
164 {
165 struct ps_compile_args args;
166 struct ps_np2fixup_info np2fixup;
167 GLhandleARB prgId;
168 };
169
170 struct glsl_vs_compiled_shader
171 {
172 struct vs_compile_args args;
173 GLhandleARB prgId;
174 };
175
176 struct glsl_gs_compiled_shader
177 {
178 GLhandleARB id;
179 };
180
181 struct glsl_shader_private
182 {
183 union
184 {
185 struct glsl_vs_compiled_shader *vs;
186 struct glsl_gs_compiled_shader *gs;
187 struct glsl_ps_compiled_shader *ps;
188 } gl_shaders;
189 UINT num_gl_shaders, shader_array_size;
190 };
191
192 struct glsl_ffp_vertex_shader
193 {
194 struct wined3d_ffp_vs_desc desc;
195 GLhandleARB id;
196 struct list linked_programs;
197 };
198
199 struct glsl_ffp_fragment_shader
200 {
201 struct ffp_frag_desc entry;
202 GLhandleARB id;
203 struct list linked_programs;
204 };
205
206 struct glsl_ffp_destroy_ctx
207 {
208 struct shader_glsl_priv *priv;
209 const struct wined3d_gl_info *gl_info;
210 };
211
212 static const char *debug_gl_shader_type(GLenum type)
213 {
214 switch (type)
215 {
216 #define WINED3D_TO_STR(u) case u: return #u
217 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
218 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
219 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
220 #undef WINED3D_TO_STR
221 default:
222 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
223 }
224 }
225
226 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
227 {
228 switch (type)
229 {
230 case WINED3D_SHADER_TYPE_VERTEX:
231 return "vs";
232
233 case WINED3D_SHADER_TYPE_GEOMETRY:
234 return "gs";
235
236 case WINED3D_SHADER_TYPE_PIXEL:
237 return "ps";
238
239 default:
240 FIXME("Unhandled shader type %#x.\n", type);
241 return "unknown";
242 }
243 }
244
245 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
246 {
247 char str[4][17];
248
249 wined3d_ftoa(values[0], str[0]);
250 wined3d_ftoa(values[1], str[1]);
251 wined3d_ftoa(values[2], str[2]);
252 wined3d_ftoa(values[3], str[3]);
253 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
254 }
255
256 /* Extract a line from the info log.
257 * Note that this modifies the source string. */
258 static char *get_info_log_line(char **ptr)
259 {
260 char *p, *q;
261
262 p = *ptr;
263 if (!(q = strstr(p, "\n")))
264 {
265 if (!*p) return NULL;
266 *ptr += strlen(p);
267 return p;
268 }
269 *q = '\0';
270 *ptr = q + 1;
271
272 return p;
273 }
274
275 /* Context activation is done by the caller. */
276 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
277 {
278 int infologLength = 0;
279 char *infoLog;
280
281 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
282 return;
283
284 GL_EXTCALL(glGetObjectParameterivARB(obj,
285 GL_OBJECT_INFO_LOG_LENGTH_ARB,
286 &infologLength));
287
288 /* A size of 1 is just a null-terminated string, so the log should be bigger than
289 * that if there are errors. */
290 if (infologLength > 1)
291 {
292 char *ptr, *line;
293
294 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
295 /* The info log is supposed to be zero-terminated, but at least some
296 * versions of fglrx don't terminate the string properly. The reported
297 * length does include the terminator, so explicitly set it to zero
298 * here. */
299 infoLog[infologLength - 1] = 0;
300 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
301
302 ptr = infoLog;
303 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
304 {
305 WARN("Info log received from GLSL shader #%u:\n", obj);
306 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
307 }
308 else
309 {
310 FIXME("Info log received from GLSL shader #%u:\n", obj);
311 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
312 }
313 HeapFree(GetProcessHeap(), 0, infoLog);
314 }
315 }
316
317 /* Context activation is done by the caller. */
318 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
319 {
320 TRACE("Compiling shader object %u.\n", shader);
321 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
322 checkGLcall("glShaderSourceARB");
323 GL_EXTCALL(glCompileShaderARB(shader));
324 checkGLcall("glCompileShaderARB");
325 print_glsl_info_log(gl_info, shader);
326 }
327
328 /* Context activation is done by the caller. */
329 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
330 {
331 GLint i, object_count, source_size = -1;
332 GLhandleARB *objects;
333 char *source = NULL;
334
335 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
336 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
337 if (!objects)
338 {
339 ERR("Failed to allocate object array memory.\n");
340 return;
341 }
342
343 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
344 for (i = 0; i < object_count; ++i)
345 {
346 char *ptr, *line;
347 GLint tmp;
348
349 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
350
351 if (source_size < tmp)
352 {
353 HeapFree(GetProcessHeap(), 0, source);
354
355 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
356 if (!source)
357 {
358 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
359 HeapFree(GetProcessHeap(), 0, objects);
360 return;
361 }
362 source_size = tmp;
363 }
364
365 FIXME("Object %u:\n", objects[i]);
366 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
367 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
368 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
369 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
370 FIXME("\n");
371
372 ptr = source;
373 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
374 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
375 FIXME("\n");
376 }
377
378 HeapFree(GetProcessHeap(), 0, source);
379 HeapFree(GetProcessHeap(), 0, objects);
380 }
381
382 /* Context activation is done by the caller. */
383 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
384 {
385 GLint tmp;
386
387 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
388
389 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
390 if (tmp == GL_PROGRAM_OBJECT_ARB)
391 {
392 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
393 if (!tmp)
394 {
395 FIXME("Program %u link status invalid.\n", program);
396 shader_glsl_dump_program_source(gl_info, program);
397 }
398 }
399
400 print_glsl_info_log(gl_info, program);
401 }
402
403 /* Context activation is done by the caller. */
404 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
405 const DWORD *tex_unit_map, GLhandleARB programId)
406 {
407 GLint name_loc;
408 char sampler_name[20];
409 unsigned int i;
410
411 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
412 {
413 snprintf(sampler_name, sizeof(sampler_name), "ps_sampler%u", i);
414 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
415 if (name_loc != -1) {
416 DWORD mapped_unit = tex_unit_map[i];
417 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
418 {
419 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
420 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
421 checkGLcall("glUniform1iARB");
422 } else {
423 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
424 }
425 }
426 }
427 }
428
429 /* Context activation is done by the caller. */
430 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
431 const DWORD *tex_unit_map, GLhandleARB programId)
432 {
433 GLint name_loc;
434 char sampler_name[20];
435 unsigned int i;
436
437 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
438 {
439 snprintf(sampler_name, sizeof(sampler_name), "vs_sampler%u", i);
440 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
441 if (name_loc != -1) {
442 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
443 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
444 {
445 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
446 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
447 checkGLcall("glUniform1iARB");
448 } else {
449 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
450 }
451 }
452 }
453 }
454
455 /* Context activation is done by the caller. */
456 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
457 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
458 {
459 unsigned int start = ~0U, end = 0;
460 int stack_idx = 0;
461 unsigned int heap_idx = 1;
462 unsigned int idx;
463
464 if (heap->entries[heap_idx].version <= version) return;
465
466 idx = heap->entries[heap_idx].idx;
467 if (constant_locations[idx] != -1)
468 start = end = idx;
469 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
470
471 while (stack_idx >= 0)
472 {
473 /* Note that we fall through to the next case statement. */
474 switch(stack[stack_idx])
475 {
476 case HEAP_NODE_TRAVERSE_LEFT:
477 {
478 unsigned int left_idx = heap_idx << 1;
479 if (left_idx < heap->size && heap->entries[left_idx].version > version)
480 {
481 heap_idx = left_idx;
482 idx = heap->entries[heap_idx].idx;
483 if (constant_locations[idx] != -1)
484 {
485 if (start > idx)
486 start = idx;
487 if (end < idx)
488 end = idx;
489 }
490
491 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
492 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
493 break;
494 }
495 }
496
497 case HEAP_NODE_TRAVERSE_RIGHT:
498 {
499 unsigned int right_idx = (heap_idx << 1) + 1;
500 if (right_idx < heap->size && heap->entries[right_idx].version > version)
501 {
502 heap_idx = right_idx;
503 idx = heap->entries[heap_idx].idx;
504 if (constant_locations[idx] != -1)
505 {
506 if (start > idx)
507 start = idx;
508 if (end < idx)
509 end = idx;
510 }
511
512 stack[stack_idx++] = HEAP_NODE_POP;
513 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
514 break;
515 }
516 }
517
518 case HEAP_NODE_POP:
519 heap_idx >>= 1;
520 --stack_idx;
521 break;
522 }
523 }
524 if (start <= end)
525 GL_EXTCALL(glUniform4fvARB(constant_locations[start], end - start + 1, &constants[start * 4]));
526 checkGLcall("walk_constant_heap()");
527 }
528
529 /* Context activation is done by the caller. */
530 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
531 {
532 GLfloat clamped_constant[4];
533
534 if (location == -1) return;
535
536 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
537 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
538 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
539 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
540
541 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
542 }
543
544 /* Context activation is done by the caller. */
545 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
546 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
547 {
548 int stack_idx = 0;
549 unsigned int heap_idx = 1;
550 unsigned int idx;
551
552 if (heap->entries[heap_idx].version <= version) return;
553
554 idx = heap->entries[heap_idx].idx;
555 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
556 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
557
558 while (stack_idx >= 0)
559 {
560 /* Note that we fall through to the next case statement. */
561 switch(stack[stack_idx])
562 {
563 case HEAP_NODE_TRAVERSE_LEFT:
564 {
565 unsigned int left_idx = heap_idx << 1;
566 if (left_idx < heap->size && heap->entries[left_idx].version > version)
567 {
568 heap_idx = left_idx;
569 idx = heap->entries[heap_idx].idx;
570 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
571
572 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
573 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
574 break;
575 }
576 }
577
578 case HEAP_NODE_TRAVERSE_RIGHT:
579 {
580 unsigned int right_idx = (heap_idx << 1) + 1;
581 if (right_idx < heap->size && heap->entries[right_idx].version > version)
582 {
583 heap_idx = right_idx;
584 idx = heap->entries[heap_idx].idx;
585 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
586
587 stack[stack_idx++] = HEAP_NODE_POP;
588 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
589 break;
590 }
591 }
592
593 case HEAP_NODE_POP:
594 heap_idx >>= 1;
595 --stack_idx;
596 break;
597 }
598 }
599 checkGLcall("walk_constant_heap_clamped()");
600 }
601
602 /* Context activation is done by the caller. */
603 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
604 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
605 unsigned char *stack, UINT version)
606 {
607 const struct wined3d_shader_lconst *lconst;
608
609 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
610 if (shader->reg_maps.shader_version.major == 1
611 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
612 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
613 else
614 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
615
616 if (!shader->load_local_constsF)
617 {
618 TRACE("No need to load local float constants for this shader\n");
619 return;
620 }
621
622 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
623 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
624 {
625 GL_EXTCALL(glUniform4fvARB(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
626 }
627 checkGLcall("glUniform4fvARB()");
628 }
629
630 /* Context activation is done by the caller. */
631 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
632 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
633 {
634 unsigned int i;
635 struct list* ptr;
636
637 for (i = 0; constants_set; constants_set >>= 1, ++i)
638 {
639 if (!(constants_set & 1)) continue;
640
641 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
642 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
643
644 /* We found this uniform name in the program - go ahead and send the data */
645 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
646 checkGLcall("glUniform4ivARB");
647 }
648
649 /* Load immediate constants */
650 ptr = list_head(&shader->constantsI);
651 while (ptr)
652 {
653 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, 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, %i, %i, %i\n", idx,
658 values[0], values[1], values[2], values[3]);
659
660 /* We found this uniform name in the program - go ahead and send the data */
661 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
662 checkGLcall("glUniform4ivARB");
663 ptr = list_next(&shader->constantsI, ptr);
664 }
665 }
666
667 /* Context activation is done by the caller. */
668 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
669 GLhandleARB programId, const BOOL *constants, WORD constants_set)
670 {
671 GLint tmp_loc;
672 unsigned int i;
673 char tmp_name[10];
674 const char *prefix;
675 struct list* ptr;
676
677 prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
678
679 /* TODO: Benchmark and see if it would be beneficial to store the
680 * locations of the constants to avoid looking up each time */
681 for (i = 0; constants_set; constants_set >>= 1, ++i)
682 {
683 if (!(constants_set & 1)) continue;
684
685 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
686
687 /* TODO: Benchmark and see if it would be beneficial to store the
688 * locations of the constants to avoid looking up each time */
689 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, i);
690 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
691 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
692 }
693
694 /* Load immediate constants */
695 ptr = list_head(&shader->constantsB);
696 while (ptr)
697 {
698 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
699 unsigned int idx = lconst->idx;
700 const GLint *values = (const GLint *)lconst->value;
701
702 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
703
704 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, idx);
705 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
706 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
707 ptr = list_next(&shader->constantsB, ptr);
708 }
709
710 checkGLcall("shader_glsl_load_constantsB()");
711 }
712
713 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
714 {
715 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
716 }
717
718 /* Context activation is done by the caller (state handler). */
719 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
720 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
721 {
722 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
723 UINT fixup = ps->np2_fixup_info->active;
724 UINT i;
725
726 for (i = 0; fixup; fixup >>= 1, ++i)
727 {
728 const struct wined3d_texture *tex = state->textures[i];
729 unsigned char idx = ps->np2_fixup_info->idx[i];
730 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
731
732 if (!tex)
733 {
734 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
735 continue;
736 }
737
738 if (idx % 2)
739 {
740 tex_dim[2] = tex->pow2_matrix[0];
741 tex_dim[3] = tex->pow2_matrix[5];
742 }
743 else
744 {
745 tex_dim[0] = tex->pow2_matrix[0];
746 tex_dim[1] = tex->pow2_matrix[5];
747 }
748 }
749
750 GL_EXTCALL(glUniform4fvARB(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
751 }
752
753 /* Context activation is done by the caller (state handler). */
754 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
755 const struct wined3d_state *state)
756 {
757 const struct glsl_context_data *ctx_data = context->shader_backend_data;
758 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
759 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
760 const struct wined3d_gl_info *gl_info = context->gl_info;
761 struct shader_glsl_priv *priv = shader_priv;
762 float position_fixup[4];
763 DWORD update_mask = 0;
764
765 GLhandleARB programId;
766 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
767 UINT constant_version;
768 int i;
769
770 if (!prog) {
771 /* No GLSL program set - nothing to do. */
772 return;
773 }
774 programId = prog->programId;
775 constant_version = prog->constant_version;
776 update_mask = context->constant_update_mask & prog->constant_update_mask;
777
778 if (update_mask & WINED3D_SHADER_CONST_VS_F)
779 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
780 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
781
782 if (update_mask & WINED3D_SHADER_CONST_VS_I)
783 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
784 vshader->reg_maps.integer_constants);
785
786 if (update_mask & WINED3D_SHADER_CONST_VS_B)
787 shader_glsl_load_constantsB(vshader, gl_info, programId, state->vs_consts_b,
788 vshader->reg_maps.boolean_constants);
789
790 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
791 {
792 shader_get_position_fixup(context, state, position_fixup);
793 GL_EXTCALL(glUniform4fvARB(prog->vs.pos_fixup_location, 1, position_fixup));
794 checkGLcall("glUniform4fvARB");
795 }
796
797 if (update_mask & WINED3D_SHADER_CONST_PS_F)
798 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
799 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
800
801 if (update_mask & WINED3D_SHADER_CONST_PS_I)
802 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
803 pshader->reg_maps.integer_constants);
804
805 if (update_mask & WINED3D_SHADER_CONST_PS_B)
806 shader_glsl_load_constantsB(pshader, gl_info, programId, state->ps_consts_b,
807 pshader->reg_maps.boolean_constants);
808
809 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
810 {
811 for (i = 0; i < MAX_TEXTURES; ++i)
812 {
813 if (prog->ps.bumpenv_mat_location[i] == -1)
814 continue;
815
816 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0,
817 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
818
819 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
820 {
821 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_scale_location[i], 1,
822 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
823 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_offset_location[i], 1,
824 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
825 }
826 }
827
828 checkGLcall("bump env uniforms");
829 }
830
831 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
832 {
833 float correction_params[4];
834
835 if (context->render_offscreen)
836 {
837 correction_params[0] = 0.0f;
838 correction_params[1] = 1.0f;
839 } else {
840 /* position is window relative, not viewport relative */
841 correction_params[0] = (float) context->current_rt->resource.height;
842 correction_params[1] = -1.0f;
843 }
844 GL_EXTCALL(glUniform4fvARB(prog->ps.ycorrection_location, 1, correction_params));
845 }
846
847 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
848 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
849
850 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
851 {
852 float col[4];
853
854 if (prog->ps.tex_factor_location != -1)
855 {
856 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
857 GL_EXTCALL(glUniform4fvARB(prog->ps.tex_factor_location, 1, col));
858 }
859
860 if (state->render_states[WINED3D_RS_SPECULARENABLE])
861 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
862 else
863 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
864
865 checkGLcall("fixed function uniforms");
866 }
867
868 if (priv->next_constant_version == UINT_MAX)
869 {
870 TRACE("Max constant version reached, resetting to 0.\n");
871 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
872 priv->next_constant_version = 1;
873 }
874 else
875 {
876 prog->constant_version = priv->next_constant_version++;
877 }
878 }
879
880 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
881 {
882 struct constant_entry *entries = heap->entries;
883 unsigned int *positions = heap->positions;
884 unsigned int heap_idx, parent_idx;
885
886 if (!heap->contained[idx])
887 {
888 heap_idx = heap->size++;
889 heap->contained[idx] = TRUE;
890 }
891 else
892 {
893 heap_idx = positions[idx];
894 }
895
896 while (heap_idx > 1)
897 {
898 parent_idx = heap_idx >> 1;
899
900 if (new_version <= entries[parent_idx].version) break;
901
902 entries[heap_idx] = entries[parent_idx];
903 positions[entries[parent_idx].idx] = heap_idx;
904 heap_idx = parent_idx;
905 }
906
907 entries[heap_idx].version = new_version;
908 entries[heap_idx].idx = idx;
909 positions[idx] = heap_idx;
910 }
911
912 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
913 {
914 struct shader_glsl_priv *priv = device->shader_priv;
915 struct constant_heap *heap = &priv->vconst_heap;
916 UINT i;
917
918 for (i = start; i < count + start; ++i)
919 {
920 update_heap_entry(heap, i, priv->next_constant_version);
921 }
922
923 for (i = 0; i < device->context_count; ++i)
924 {
925 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
926 }
927 }
928
929 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
930 {
931 struct shader_glsl_priv *priv = device->shader_priv;
932 struct constant_heap *heap = &priv->pconst_heap;
933 UINT i;
934
935 for (i = start; i < count + start; ++i)
936 {
937 update_heap_entry(heap, i, priv->next_constant_version);
938 }
939
940 for (i = 0; i < device->context_count; ++i)
941 {
942 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
943 }
944 }
945
946 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
947 {
948 unsigned int ret = gl_info->limits.glsl_varyings / 4;
949 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
950 if(shader_major > 3) return ret;
951
952 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
953 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
954 return ret;
955 }
956
957 /** Generate the variable & register declarations for the GLSL output target */
958 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
959 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
960 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
961 {
962 const struct wined3d_shader_version *version = &reg_maps->shader_version;
963 const struct wined3d_state *state = &shader->device->state;
964 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
965 const struct wined3d_gl_info *gl_info = context->gl_info;
966 const struct wined3d_fb_state *fb = &shader->device->fb;
967 unsigned int i, extra_constants_needed = 0;
968 const struct wined3d_shader_lconst *lconst;
969 const char *prefix;
970 DWORD map;
971
972 prefix = shader_glsl_get_prefix(version->type);
973
974 /* Prototype the subroutines */
975 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
976 {
977 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
978 }
979
980 /* Declare the constants (aka uniforms) */
981 if (shader->limits.constant_float > 0)
982 {
983 unsigned max_constantsF;
984
985 /* Unless the shader uses indirect addressing, always declare the
986 * maximum array size and ignore that we need some uniforms privately.
987 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
988 * and immediate values, still declare VC[256]. If the shader needs
989 * more uniforms than we have it won't work in any case. If it uses
990 * less, the compiler will figure out which uniforms are really used
991 * and strip them out. This allows a shader to use c255 on a dx9 card,
992 * as long as it doesn't also use all the other constants.
993 *
994 * If the shader uses indirect addressing the compiler must assume
995 * that all declared uniforms are used. In this case, declare only the
996 * amount that we're assured to have.
997 *
998 * Thus we run into problems in these two cases:
999 * 1) The shader really uses more uniforms than supported.
1000 * 2) The shader uses indirect addressing, less constants than
1001 * supported, but uses a constant index > #supported consts. */
1002 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1003 {
1004 /* No indirect addressing here. */
1005 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1006 }
1007 else
1008 {
1009 if (reg_maps->usesrelconstF)
1010 {
1011 /* Subtract the other potential uniforms from the max
1012 * available (bools, ints, and 1 row of projection matrix).
1013 * Subtract another uniform for immediate values, which have
1014 * to be loaded via uniform by the driver as well. The shader
1015 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1016 * shader code, so one vec4 should be enough. (Unfortunately
1017 * the Nvidia driver doesn't store 128 and -128 in one float).
1018 *
1019 * Writing gl_ClipVertex requires one uniform for each
1020 * clipplane as well. */
1021 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1022 if(ctx_priv->cur_vs_args->clip_enabled)
1023 {
1024 max_constantsF -= gl_info->limits.clipplanes;
1025 }
1026 max_constantsF -= count_bits(reg_maps->integer_constants);
1027 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1028 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1029 * for now take this into account when calculating the number of available constants
1030 */
1031 max_constantsF -= count_bits(reg_maps->boolean_constants);
1032 /* Set by driver quirks in directx.c */
1033 max_constantsF -= gl_info->reserved_glsl_constants;
1034
1035 if (max_constantsF < shader->limits.constant_float)
1036 {
1037 static unsigned int once;
1038
1039 if (!once++)
1040 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1041 " it may not render correctly.\n");
1042 else
1043 WARN("The hardware does not support enough uniform components to run this shader.\n");
1044 }
1045 }
1046 else
1047 {
1048 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1049 }
1050 }
1051 max_constantsF = min(shader->limits.constant_float, max_constantsF);
1052 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1053 }
1054
1055 /* Always declare the full set of constants, the compiler can remove the
1056 * unused ones because d3d doesn't (yet) support indirect int and bool
1057 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1058 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
1059 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits.constant_int);
1060
1061 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
1062 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits.constant_bool);
1063
1064 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1065 {
1066 if (reg_maps->cb_sizes[i])
1067 shader_addline(buffer, "uniform vec4 %s_cb%u[%u];\n", prefix, i, reg_maps->cb_sizes[i]);
1068 }
1069
1070 /* Declare texture samplers */
1071 for (i = 0; i < shader->limits.sampler; ++i)
1072 {
1073 if (reg_maps->sampler_type[i])
1074 {
1075 BOOL shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1076 BOOL tex_rect;
1077
1078 switch (reg_maps->sampler_type[i])
1079 {
1080 case WINED3DSTT_1D:
1081 if (shadow_sampler)
1082 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1083 else
1084 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1085 break;
1086 case WINED3DSTT_2D:
1087 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->np2_fixup & (1 << i));
1088 tex_rect = tex_rect && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1089 if (shadow_sampler)
1090 {
1091 if (tex_rect)
1092 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1093 else
1094 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1095 }
1096 else
1097 {
1098 if (tex_rect)
1099 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1100 else
1101 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1102 }
1103 break;
1104 case WINED3DSTT_CUBE:
1105 if (shadow_sampler)
1106 FIXME("Unsupported Cube shadow sampler.\n");
1107 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1108 break;
1109 case WINED3DSTT_VOLUME:
1110 if (shadow_sampler)
1111 FIXME("Unsupported 3D shadow sampler.\n");
1112 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1113 break;
1114 default:
1115 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1116 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1117 break;
1118 }
1119 }
1120 }
1121
1122 /* Declare uniforms for NP2 texcoord fixup:
1123 * This is NOT done inside the loop that declares the texture samplers
1124 * since the NP2 fixup code is currently only used for the GeforceFX
1125 * series and when forcing the ARB_npot extension off. Modern cards just
1126 * skip the code anyway, so put it inside a separate loop. */
1127 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1128 {
1129 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1130 UINT cur = 0;
1131
1132 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1133 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1134 * samplerNP2Fixup stores texture dimensions and is updated through
1135 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1136
1137 for (i = 0; i < shader->limits.sampler; ++i)
1138 {
1139 if (reg_maps->sampler_type[i])
1140 {
1141 if (!(ps_args->np2_fixup & (1 << i))) continue;
1142
1143 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1144 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1145 continue;
1146 }
1147
1148 fixup->idx[i] = cur++;
1149 }
1150 }
1151
1152 fixup->num_consts = (cur + 1) >> 1;
1153 fixup->active = ps_args->np2_fixup;
1154 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1155 }
1156
1157 /* Declare address variables */
1158 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1159 {
1160 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1161 }
1162
1163 /* Declare texture coordinate temporaries and initialize them */
1164 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1165 {
1166 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1167 }
1168
1169 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1170 {
1171 /* Declare attributes. */
1172 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1173 {
1174 if (map & 1)
1175 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1176 }
1177
1178 shader_addline(buffer, "uniform vec4 posFixup;\n");
1179 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits.packed_output);
1180 }
1181 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1182 {
1183 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits.packed_input);
1184 }
1185 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1186 {
1187 if (version->major >= 3)
1188 {
1189 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits.packed_input);
1190
1191 if (use_vs(state))
1192 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1193 else
1194 /* TODO: Write a replacement shader for the fixed function
1195 * vertex pipeline, so this isn't needed. For fixed function
1196 * vertex processing + 3.0 pixel shader we need a separate
1197 * function in the pixel shader that reads the fixed function
1198 * color into the packed input registers. */
1199 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1200 }
1201
1202 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1203 {
1204 if (!(map & 1))
1205 continue;
1206
1207 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1208
1209 if (reg_maps->luminanceparams & (1 << i))
1210 {
1211 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1212 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1213 extra_constants_needed++;
1214 }
1215
1216 extra_constants_needed++;
1217 }
1218
1219 if (ps_args->srgb_correction)
1220 {
1221 shader_addline(buffer, "const vec4 srgb_const0 = ");
1222 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1223 shader_addline(buffer, ";\n");
1224 shader_addline(buffer, "const vec4 srgb_const1 = ");
1225 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1226 shader_addline(buffer, ";\n");
1227 }
1228 if (reg_maps->vpos || reg_maps->usesdsy)
1229 {
1230 if (shader->limits.constant_float + extra_constants_needed
1231 + 1 < gl_info->limits.glsl_ps_float_constants)
1232 {
1233 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1234 extra_constants_needed++;
1235 }
1236 else
1237 {
1238 float ycorrection[] =
1239 {
1240 context->render_offscreen ? 0.0f : fb->render_targets[0]->resource.height,
1241 context->render_offscreen ? 1.0f : -1.0f,
1242 0.0f,
1243 0.0f,
1244 };
1245
1246 /* This happens because we do not have proper tracking of the
1247 * constant registers that are actually used, only the max
1248 * limit of the shader version. */
1249 FIXME("Cannot find a free uniform for vpos correction params\n");
1250 shader_addline(buffer, "const vec4 ycorrection = ");
1251 shader_glsl_append_imm_vec4(buffer, ycorrection);
1252 shader_addline(buffer, ";\n");
1253 }
1254 shader_addline(buffer, "vec4 vpos;\n");
1255 }
1256 }
1257
1258 /* Declare output register temporaries */
1259 if (shader->limits.packed_output)
1260 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits.packed_output);
1261
1262 /* Declare temporary variables */
1263 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1264 {
1265 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1266 }
1267
1268 /* Declare loop registers aLx */
1269 if (version->major < 4)
1270 {
1271 for (i = 0; i < reg_maps->loop_depth; ++i)
1272 {
1273 shader_addline(buffer, "int aL%u;\n", i);
1274 shader_addline(buffer, "int tmpInt%u;\n", i);
1275 }
1276 }
1277
1278 /* Temporary variables for matrix operations */
1279 shader_addline(buffer, "vec4 tmp0;\n");
1280 shader_addline(buffer, "vec4 tmp1;\n");
1281
1282 if (!shader->load_local_constsF)
1283 {
1284 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1285 {
1286 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1287 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1288 shader_addline(buffer, ";\n");
1289 }
1290 }
1291
1292 /* Start the main program. */
1293 shader_addline(buffer, "void main()\n{\n");
1294
1295 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1296 * add approximately 0.5. This causes off-by-one problems as spotted by
1297 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1298 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1299 * causes precision troubles when we just subtract 0.5.
1300 *
1301 * To deal with that, just floor() the position. This will eliminate the
1302 * fraction on all cards.
1303 *
1304 * TODO: Test how this behaves with multisampling.
1305 *
1306 * An advantage of floor is that it works even if the driver doesn't add
1307 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1308 * to return in gl_FragCoord, even though coordinates specify the pixel
1309 * centers instead of the pixel corners. This code will behave correctly
1310 * on drivers that returns integer values. */
1311 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1312 shader_addline(buffer,
1313 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1314 }
1315
1316 /*****************************************************************************
1317 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1318 *
1319 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1320 ****************************************************************************/
1321
1322 /* Prototypes */
1323 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1324 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1325
1326 /** Used for opcode modifiers - They multiply the result by the specified amount */
1327 static const char * const shift_glsl_tab[] = {
1328 "", /* 0 (none) */
1329 "2.0 * ", /* 1 (x2) */
1330 "4.0 * ", /* 2 (x4) */
1331 "8.0 * ", /* 3 (x8) */
1332 "16.0 * ", /* 4 (x16) */
1333 "32.0 * ", /* 5 (x32) */
1334 "", /* 6 (x64) */
1335 "", /* 7 (x128) */
1336 "", /* 8 (d256) */
1337 "", /* 9 (d128) */
1338 "", /* 10 (d64) */
1339 "", /* 11 (d32) */
1340 "0.0625 * ", /* 12 (d16) */
1341 "0.125 * ", /* 13 (d8) */
1342 "0.25 * ", /* 14 (d4) */
1343 "0.5 * " /* 15 (d2) */
1344 };
1345
1346 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1347 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1348 const char *in_reg, const char *in_regswizzle, char *out_str)
1349 {
1350 out_str[0] = 0;
1351
1352 switch (src_modifier)
1353 {
1354 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1355 case WINED3DSPSM_DW:
1356 case WINED3DSPSM_NONE:
1357 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1358 break;
1359 case WINED3DSPSM_NEG:
1360 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1361 break;
1362 case WINED3DSPSM_NOT:
1363 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1364 break;
1365 case WINED3DSPSM_BIAS:
1366 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1367 break;
1368 case WINED3DSPSM_BIASNEG:
1369 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1370 break;
1371 case WINED3DSPSM_SIGN:
1372 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1373 break;
1374 case WINED3DSPSM_SIGNNEG:
1375 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1376 break;
1377 case WINED3DSPSM_COMP:
1378 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1379 break;
1380 case WINED3DSPSM_X2:
1381 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1382 break;
1383 case WINED3DSPSM_X2NEG:
1384 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1385 break;
1386 case WINED3DSPSM_ABS:
1387 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1388 break;
1389 case WINED3DSPSM_ABSNEG:
1390 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1391 break;
1392 default:
1393 FIXME("Unhandled modifier %u\n", src_modifier);
1394 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1395 }
1396 }
1397
1398 /** Writes the GLSL variable name that corresponds to the register that the
1399 * DX opcode parameter is trying to access */
1400 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1401 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1402 {
1403 /* oPos, oFog and oPts in D3D */
1404 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1405
1406 const struct wined3d_shader *shader = ins->ctx->shader;
1407 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1408 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1409 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1410 const char *prefix = shader_glsl_get_prefix(version->type);
1411 struct glsl_src_param rel_param0, rel_param1;
1412 char imm_str[4][17];
1413
1414 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1415 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1416 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1417 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1418 *is_color = FALSE;
1419
1420 switch (reg->type)
1421 {
1422 case WINED3DSPR_TEMP:
1423 sprintf(register_name, "R%u", reg->idx[0].offset);
1424 break;
1425
1426 case WINED3DSPR_INPUT:
1427 /* vertex shaders */
1428 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1429 {
1430 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1431 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1432 *is_color = TRUE;
1433 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1434 break;
1435 }
1436
1437 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1438 {
1439 if (reg->idx[0].rel_addr)
1440 {
1441 if (reg->idx[1].rel_addr)
1442 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1443 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1444 else
1445 sprintf(register_name, "gs_in[%s + %u][%u]",
1446 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1447 }
1448 else if (reg->idx[1].rel_addr)
1449 sprintf(register_name, "gs_in[%u][%s + %u]",
1450 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1451 else
1452 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1453 break;
1454 }
1455
1456 /* pixel shaders >= 3.0 */
1457 if (version->major >= 3)
1458 {
1459 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1460 unsigned int in_count = vec4_varyings(version->major, gl_info);
1461
1462 if (reg->idx[0].rel_addr)
1463 {
1464 /* Removing a + 0 would be an obvious optimization, but
1465 * OS X doesn't see the NOP operation there. */
1466 if (idx)
1467 {
1468 if (shader->u.ps.declared_in_count > in_count)
1469 {
1470 sprintf(register_name,
1471 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1472 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1473 prefix, rel_param0.param_str, idx);
1474 }
1475 else
1476 {
1477 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1478 }
1479 }
1480 else
1481 {
1482 if (shader->u.ps.declared_in_count > in_count)
1483 {
1484 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1485 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1486 prefix, rel_param0.param_str);
1487 }
1488 else
1489 {
1490 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1491 }
1492 }
1493 }
1494 else
1495 {
1496 if (idx == in_count) sprintf(register_name, "gl_Color");
1497 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1498 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1499 }
1500 }
1501 else
1502 {
1503 if (!reg->idx[0].offset)
1504 strcpy(register_name, "gl_Color");
1505 else
1506 strcpy(register_name, "gl_SecondaryColor");
1507 break;
1508 }
1509 break;
1510
1511 case WINED3DSPR_CONST:
1512 {
1513 /* Relative addressing */
1514 if (reg->idx[0].rel_addr)
1515 {
1516 if (reg->idx[0].offset)
1517 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1518 else
1519 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1520 }
1521 else
1522 {
1523 if (shader_constant_is_local(shader, reg->idx[0].offset))
1524 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1525 else
1526 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1527 }
1528 }
1529 break;
1530
1531 case WINED3DSPR_CONSTINT:
1532 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1533 break;
1534
1535 case WINED3DSPR_CONSTBOOL:
1536 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1537 break;
1538
1539 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1540 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1541 sprintf(register_name, "T%u", reg->idx[0].offset);
1542 else
1543 sprintf(register_name, "A%u", reg->idx[0].offset);
1544 break;
1545
1546 case WINED3DSPR_LOOP:
1547 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1548 break;
1549
1550 case WINED3DSPR_SAMPLER:
1551 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1552 break;
1553
1554 case WINED3DSPR_COLOROUT:
1555 if (reg->idx[0].offset >= gl_info->limits.buffers)
1556 WARN("Write to render target %u, only %d supported.\n",
1557 reg->idx[0].offset, gl_info->limits.buffers);
1558
1559 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1560 break;
1561
1562 case WINED3DSPR_RASTOUT:
1563 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1564 break;
1565
1566 case WINED3DSPR_DEPTHOUT:
1567 sprintf(register_name, "gl_FragDepth");
1568 break;
1569
1570 case WINED3DSPR_ATTROUT:
1571 if (!reg->idx[0].offset)
1572 sprintf(register_name, "%s_out[8]", prefix);
1573 else
1574 sprintf(register_name, "%s_out[9]", prefix);
1575 break;
1576
1577 case WINED3DSPR_TEXCRDOUT:
1578 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1579 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1580 break;
1581
1582 case WINED3DSPR_MISCTYPE:
1583 if (!reg->idx[0].offset)
1584 {
1585 /* vPos */
1586 sprintf(register_name, "vpos");
1587 }
1588 else if (reg->idx[0].offset == 1)
1589 {
1590 /* Note that gl_FrontFacing is a bool, while vFace is
1591 * a float for which the sign determines front/back */
1592 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1593 }
1594 else
1595 {
1596 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1597 sprintf(register_name, "unrecognized_register");
1598 }
1599 break;
1600
1601 case WINED3DSPR_IMMCONST:
1602 switch (reg->immconst_type)
1603 {
1604 case WINED3D_IMMCONST_SCALAR:
1605 switch (reg->data_type)
1606 {
1607 case WINED3D_DATA_FLOAT:
1608 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1609 break;
1610 case WINED3D_DATA_INT:
1611 sprintf(register_name, "%#x", reg->immconst_data[0]);
1612 break;
1613 case WINED3D_DATA_RESOURCE:
1614 case WINED3D_DATA_SAMPLER:
1615 case WINED3D_DATA_UINT:
1616 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1617 break;
1618 default:
1619 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1620 break;
1621 }
1622 break;
1623
1624 case WINED3D_IMMCONST_VEC4:
1625 switch (reg->data_type)
1626 {
1627 case WINED3D_DATA_FLOAT:
1628 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1629 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1630 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1631 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1632 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1633 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1634 break;
1635 case WINED3D_DATA_INT:
1636 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1637 reg->immconst_data[0], reg->immconst_data[1],
1638 reg->immconst_data[2], reg->immconst_data[3]);
1639 break;
1640 case WINED3D_DATA_RESOURCE:
1641 case WINED3D_DATA_SAMPLER:
1642 case WINED3D_DATA_UINT:
1643 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1644 reg->immconst_data[0], reg->immconst_data[1],
1645 reg->immconst_data[2], reg->immconst_data[3]);
1646 break;
1647 default:
1648 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1649 break;
1650 }
1651 break;
1652
1653 default:
1654 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1655 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1656 }
1657 break;
1658
1659 case WINED3DSPR_CONSTBUFFER:
1660 if (reg->idx[1].rel_addr)
1661 sprintf(register_name, "%s_cb%u[%s + %u]",
1662 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1663 else
1664 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1665 break;
1666
1667 case WINED3DSPR_PRIMID:
1668 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1669 break;
1670
1671 default:
1672 FIXME("Unhandled register type %#x.\n", reg->type);
1673 sprintf(register_name, "unrecognized_register");
1674 break;
1675 }
1676 }
1677
1678 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1679 {
1680 *str++ = '.';
1681 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1682 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1683 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1684 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1685 *str = '\0';
1686 }
1687
1688 /* Get the GLSL write mask for the destination register */
1689 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1690 {
1691 DWORD mask = param->write_mask;
1692
1693 if (shader_is_scalar(&param->reg))
1694 {
1695 mask = WINED3DSP_WRITEMASK_0;
1696 *write_mask = '\0';
1697 }
1698 else
1699 {
1700 shader_glsl_write_mask_to_str(mask, write_mask);
1701 }
1702
1703 return mask;
1704 }
1705
1706 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1707 unsigned int size = 0;
1708
1709 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1710 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1711 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1712 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1713
1714 return size;
1715 }
1716
1717 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1718 {
1719 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1720 * but addressed as "rgba". To fix this we need to swap the register's x
1721 * and z components. */
1722 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1723
1724 *str++ = '.';
1725 /* swizzle bits fields: wwzzyyxx */
1726 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1727 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1728 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1729 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1730 *str = '\0';
1731 }
1732
1733 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1734 BOOL fixup, DWORD mask, char *swizzle_str)
1735 {
1736 if (shader_is_scalar(&param->reg))
1737 *swizzle_str = '\0';
1738 else
1739 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1740 }
1741
1742 /* From a given parameter token, generate the corresponding GLSL string.
1743 * Also, return the actual register name and swizzle in case the
1744 * caller needs this information as well. */
1745 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1746 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1747 {
1748 BOOL is_color = FALSE;
1749 char swizzle_str[6];
1750
1751 glsl_src->reg_name[0] = '\0';
1752 glsl_src->param_str[0] = '\0';
1753 swizzle_str[0] = '\0';
1754
1755 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1756 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1757
1758 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1759 {
1760 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1761 }
1762 else
1763 {
1764 char param_str[200];
1765
1766 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1767
1768 switch (wined3d_src->reg.data_type)
1769 {
1770 case WINED3D_DATA_FLOAT:
1771 sprintf(glsl_src->param_str, "%s", param_str);
1772 break;
1773 case WINED3D_DATA_INT:
1774 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1775 break;
1776 case WINED3D_DATA_RESOURCE:
1777 case WINED3D_DATA_SAMPLER:
1778 case WINED3D_DATA_UINT:
1779 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1780 break;
1781 default:
1782 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1783 sprintf(glsl_src->param_str, "%s", param_str);
1784 break;
1785 }
1786 }
1787 }
1788
1789 /* From a given parameter token, generate the corresponding GLSL string.
1790 * Also, return the actual register name and swizzle in case the
1791 * caller needs this information as well. */
1792 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1793 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1794 {
1795 BOOL is_color = FALSE;
1796
1797 glsl_dst->mask_str[0] = '\0';
1798 glsl_dst->reg_name[0] = '\0';
1799
1800 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1801 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1802 }
1803
1804 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1805 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1806 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1807 {
1808 struct glsl_dst_param glsl_dst;
1809 DWORD mask;
1810
1811 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1812 {
1813 switch (dst->reg.data_type)
1814 {
1815 case WINED3D_DATA_FLOAT:
1816 shader_addline(buffer, "%s%s = %s(",
1817 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1818 break;
1819 case WINED3D_DATA_INT:
1820 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1821 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1822 break;
1823 case WINED3D_DATA_RESOURCE:
1824 case WINED3D_DATA_SAMPLER:
1825 case WINED3D_DATA_UINT:
1826 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1827 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1828 break;
1829 default:
1830 FIXME("Unhandled data type %#x.\n", dst->reg.data_type);
1831 shader_addline(buffer, "%s%s = %s(",
1832 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1833 break;
1834 }
1835 }
1836
1837 return mask;
1838 }
1839
1840 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1841 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1842 {
1843 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1844 }
1845
1846 /** Process GLSL instruction modifiers */
1847 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1848 {
1849 struct glsl_dst_param dst_param;
1850 DWORD modifiers;
1851
1852 if (!ins->dst_count) return;
1853
1854 modifiers = ins->dst[0].modifiers;
1855 if (!modifiers) return;
1856
1857 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1858
1859 if (modifiers & WINED3DSPDM_SATURATE)
1860 {
1861 /* _SAT means to clamp the value of the register to between 0 and 1 */
1862 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1863 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1864 }
1865
1866 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1867 {
1868 FIXME("_centroid modifier not handled\n");
1869 }
1870
1871 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1872 {
1873 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1874 }
1875 }
1876
1877 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1878 {
1879 switch (op)
1880 {
1881 case WINED3D_SHADER_REL_OP_GT: return ">";
1882 case WINED3D_SHADER_REL_OP_EQ: return "==";
1883 case WINED3D_SHADER_REL_OP_GE: return ">=";
1884 case WINED3D_SHADER_REL_OP_LT: return "<";
1885 case WINED3D_SHADER_REL_OP_NE: return "!=";
1886 case WINED3D_SHADER_REL_OP_LE: return "<=";
1887 default:
1888 FIXME("Unrecognized operator %#x.\n", op);
1889 return "(\?\?)";
1890 }
1891 }
1892
1893 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1894 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1895 {
1896 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1897 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1898 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1899 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1900 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1901 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1902 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1903 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1904
1905 /* Note that there's no such thing as a projected cube texture. */
1906 switch(sampler_type) {
1907 case WINED3DSTT_1D:
1908 if (shadow)
1909 {
1910 if (lod)
1911 {
1912 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1913 }
1914 else if (grad)
1915 {
1916 if (gl_info->supported[EXT_GPU_SHADER4])
1917 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1918 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1919 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1920 else
1921 {
1922 FIXME("Unsupported 1D shadow grad function.\n");
1923 sample_function->name = "unsupported1DGrad";
1924 }
1925 }
1926 else
1927 {
1928 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1929 }
1930 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1931 }
1932 else
1933 {
1934 if (lod)
1935 {
1936 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1937 }
1938 else if (grad)
1939 {
1940 if (gl_info->supported[EXT_GPU_SHADER4])
1941 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1942 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1943 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1944 else
1945 {
1946 FIXME("Unsupported 1D grad function.\n");
1947 sample_function->name = "unsupported1DGrad";
1948 }
1949 }
1950 else
1951 {
1952 sample_function->name = projected ? "texture1DProj" : "texture1D";
1953 }
1954 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1955 }
1956 break;
1957
1958 case WINED3DSTT_2D:
1959 if (shadow)
1960 {
1961 if (texrect)
1962 {
1963 if (lod)
1964 {
1965 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1966 }
1967 else if (grad)
1968 {
1969 if (gl_info->supported[EXT_GPU_SHADER4])
1970 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1971 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1972 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1973 else
1974 {
1975 FIXME("Unsupported RECT shadow grad function.\n");
1976 sample_function->name = "unsupported2DRectGrad";
1977 }
1978 }
1979 else
1980 {
1981 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1982 }
1983 }
1984 else
1985 {
1986 if (lod)
1987 {
1988 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1989 }
1990 else if (grad)
1991 {
1992 if (gl_info->supported[EXT_GPU_SHADER4])
1993 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1994 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1995 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1996 else
1997 {
1998 FIXME("Unsupported 2D shadow grad function.\n");
1999 sample_function->name = "unsupported2DGrad";
2000 }
2001 }
2002 else
2003 {
2004 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2005 }
2006 }
2007 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2008 }
2009 else
2010 {
2011 if (texrect)
2012 {
2013 if (lod)
2014 {
2015 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2016 }
2017 else if (grad)
2018 {
2019 if (gl_info->supported[EXT_GPU_SHADER4])
2020 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2021 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2022 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2023 else
2024 {
2025 FIXME("Unsupported RECT grad function.\n");
2026 sample_function->name = "unsupported2DRectGrad";
2027 }
2028 }
2029 else
2030 {
2031 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2032 }
2033 }
2034 else
2035 {
2036 if (lod)
2037 {
2038 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2039 }
2040 else if (grad)
2041 {
2042 if (gl_info->supported[EXT_GPU_SHADER4])
2043 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2044 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2045 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2046 else
2047 {
2048 FIXME("Unsupported 2D grad function.\n");
2049 sample_function->name = "unsupported2DGrad";
2050 }
2051 }
2052 else
2053 {
2054 sample_function->name = projected ? "texture2DProj" : "texture2D";
2055 }
2056 }
2057 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2058 }
2059 break;
2060
2061 case WINED3DSTT_CUBE:
2062 if (shadow)
2063 {
2064 FIXME("Unsupported Cube shadow function.\n");
2065 sample_function->name = "unsupportedCubeShadow";
2066 sample_function->coord_mask = 0;
2067 }
2068 else
2069 {
2070 if (lod)
2071 {
2072 sample_function->name = "textureCubeLod";
2073 }
2074 else if (grad)
2075 {
2076 if (gl_info->supported[EXT_GPU_SHADER4])
2077 sample_function->name = "textureCubeGrad";
2078 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2079 sample_function->name = "textureCubeGradARB";
2080 else
2081 {
2082 FIXME("Unsupported Cube grad function.\n");
2083 sample_function->name = "unsupportedCubeGrad";
2084 }
2085 }
2086 else
2087 {
2088 sample_function->name = "textureCube";
2089 }
2090 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2091 }
2092 break;
2093
2094 case WINED3DSTT_VOLUME:
2095 if (shadow)
2096 {
2097 FIXME("Unsupported 3D shadow function.\n");
2098 sample_function->name = "unsupported3DShadow";
2099 sample_function->coord_mask = 0;
2100 }
2101 else
2102 {
2103 if (lod)
2104 {
2105 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2106 }
2107 else if (grad)
2108 {
2109 if (gl_info->supported[EXT_GPU_SHADER4])
2110 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2111 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2112 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2113 else
2114 {
2115 FIXME("Unsupported 3D grad function.\n");
2116 sample_function->name = "unsupported3DGrad";
2117 }
2118 }
2119 else
2120 {
2121 sample_function->name = projected ? "texture3DProj" : "texture3D";
2122 }
2123 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2124 }
2125 break;
2126
2127 default:
2128 sample_function->name = "";
2129 sample_function->coord_mask = 0;
2130 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
2131 break;
2132 }
2133 }
2134
2135 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2136 BOOL sign_fixup, enum fixup_channel_source channel_source)
2137 {
2138 switch(channel_source)
2139 {
2140 case CHANNEL_SOURCE_ZERO:
2141 strcat(arguments, "0.0");
2142 break;
2143
2144 case CHANNEL_SOURCE_ONE:
2145 strcat(arguments, "1.0");
2146 break;
2147
2148 case CHANNEL_SOURCE_X:
2149 strcat(arguments, reg_name);
2150 strcat(arguments, ".x");
2151 break;
2152
2153 case CHANNEL_SOURCE_Y:
2154 strcat(arguments, reg_name);
2155 strcat(arguments, ".y");
2156 break;
2157
2158 case CHANNEL_SOURCE_Z:
2159 strcat(arguments, reg_name);
2160 strcat(arguments, ".z");
2161 break;
2162
2163 case CHANNEL_SOURCE_W:
2164 strcat(arguments, reg_name);
2165 strcat(arguments, ".w");
2166 break;
2167
2168 default:
2169 FIXME("Unhandled channel source %#x\n", channel_source);
2170 strcat(arguments, "undefined");
2171 break;
2172 }
2173
2174 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2175 }
2176
2177 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2178 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2179 {
2180 unsigned int mask_size, remaining;
2181 DWORD fixup_mask = 0;
2182 char arguments[256];
2183 char mask_str[6];
2184
2185 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2186 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2187 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2188 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2189 if (!(mask &= fixup_mask))
2190 return;
2191
2192 if (is_complex_fixup(fixup))
2193 {
2194 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2195 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2196 return;
2197 }
2198
2199 shader_glsl_write_mask_to_str(mask, mask_str);
2200 mask_size = shader_glsl_get_write_mask_size(mask);
2201
2202 arguments[0] = '\0';
2203 remaining = mask_size;
2204 if (mask & WINED3DSP_WRITEMASK_0)
2205 {
2206 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2207 if (--remaining) strcat(arguments, ", ");
2208 }
2209 if (mask & WINED3DSP_WRITEMASK_1)
2210 {
2211 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2212 if (--remaining) strcat(arguments, ", ");
2213 }
2214 if (mask & WINED3DSP_WRITEMASK_2)
2215 {
2216 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2217 if (--remaining) strcat(arguments, ", ");
2218 }
2219 if (mask & WINED3DSP_WRITEMASK_3)
2220 {
2221 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2222 if (--remaining) strcat(arguments, ", ");
2223 }
2224
2225 if (mask_size > 1)
2226 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2227 else
2228 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2229 }
2230
2231 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2232 {
2233 char reg_name[256];
2234 BOOL is_color;
2235
2236 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2237 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2238 }
2239
2240 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2241 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2242 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2243 {
2244 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2245 char dst_swizzle[6];
2246 struct color_fixup_desc fixup;
2247 BOOL np2_fixup = FALSE;
2248 va_list args;
2249
2250 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2251
2252 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2253 {
2254 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2255 fixup = priv->cur_ps_args->color_fixup[sampler];
2256
2257 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2258 if(bias) {
2259 FIXME("Biased sampling from NP2 textures is unsupported\n");
2260 } else {
2261 np2_fixup = TRUE;
2262 }
2263 }
2264 }
2265 else
2266 {
2267 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2268 }
2269
2270 shader_glsl_append_dst(ins->ctx->buffer, ins);
2271
2272 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2273 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2274
2275 va_start(args, coord_reg_fmt);
2276 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2277 va_end(args);
2278
2279 if(bias) {
2280 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2281 } else {
2282 if (np2_fixup) {
2283 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2284 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2285
2286 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2287 (idx % 2) ? "zw" : "xy", dst_swizzle);
2288 } else if(dx && dy) {
2289 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2290 } else {
2291 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2292 }
2293 }
2294
2295 if(!is_identity_fixup(fixup)) {
2296 shader_glsl_color_correction(ins, fixup);
2297 }
2298 }
2299
2300 /*****************************************************************************
2301 * Begin processing individual instruction opcodes
2302 ****************************************************************************/
2303
2304 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2305 {
2306 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2307 struct glsl_src_param src0_param;
2308 struct glsl_src_param src1_param;
2309 DWORD write_mask;
2310 const char *op;
2311
2312 /* Determine the GLSL operator to use based on the opcode */
2313 switch (ins->handler_idx)
2314 {
2315 case WINED3DSIH_ADD: op = "+"; break;
2316 case WINED3DSIH_AND: op = "&"; break;
2317 case WINED3DSIH_DIV: op = "/"; break;
2318 case WINED3DSIH_IADD: op = "+"; break;
2319 case WINED3DSIH_MUL: op = "*"; break;
2320 case WINED3DSIH_SUB: op = "-"; break;
2321 case WINED3DSIH_USHR: op = ">>"; break;
2322 case WINED3DSIH_XOR: op = "^"; break;
2323 default:
2324 op = "<unhandled operator>";
2325 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2326 break;
2327 }
2328
2329 write_mask = shader_glsl_append_dst(buffer, ins);
2330 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2331 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2332 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2333 }
2334
2335 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2336 {
2337 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2338 struct glsl_src_param src0_param;
2339 struct glsl_src_param src1_param;
2340 unsigned int mask_size;
2341 DWORD write_mask;
2342 const char *op;
2343
2344 write_mask = shader_glsl_append_dst(buffer, ins);
2345 mask_size = shader_glsl_get_write_mask_size(write_mask);
2346 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2347 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2348
2349 if (mask_size > 1)
2350 {
2351 switch (ins->handler_idx)
2352 {
2353 case WINED3DSIH_EQ: op = "equal"; break;
2354 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2355 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2356 case WINED3DSIH_LT: op = "lessThan"; break;
2357 default:
2358 op = "<unhandled operator>";
2359 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2360 break;
2361 }
2362
2363 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2364 mask_size, op, src0_param.param_str, src1_param.param_str);
2365 }
2366 else
2367 {
2368 switch (ins->handler_idx)
2369 {
2370 case WINED3DSIH_EQ: op = "=="; break;
2371 case WINED3DSIH_GE: op = ">="; break;
2372 case WINED3DSIH_IGE: op = ">="; break;
2373 case WINED3DSIH_LT: op = "<"; break;
2374 default:
2375 op = "<unhandled operator>";
2376 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2377 break;
2378 }
2379
2380 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2381 src0_param.param_str, op, src1_param.param_str);
2382 }
2383 }
2384
2385 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2386 {
2387 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2388 struct glsl_src_param src0_param;
2389 struct glsl_src_param src1_param;
2390 DWORD write_mask;
2391
2392 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2393 * not, we can emulate it. */
2394 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2395 FIXME("64-bit integer multiplies not implemented.\n");
2396
2397 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2398 {
2399 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2400 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2401 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2402
2403 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2404 src0_param.param_str, src1_param.param_str);
2405 }
2406 }
2407
2408 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2409 {
2410 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2411 struct glsl_src_param src0_param, src1_param;
2412 DWORD write_mask;
2413
2414 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2415 {
2416
2417 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2418 {
2419 char dst_mask[6];
2420
2421 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2422 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2423 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2424 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2425 dst_mask, src0_param.param_str, src1_param.param_str);
2426
2427 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2428 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2429 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2430 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2431
2432 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2433 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2434 }
2435 else
2436 {
2437 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2438 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2439 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2440 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2441 }
2442 }
2443 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2444 {
2445 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2446 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2447 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2448 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2449 }
2450 }
2451
2452 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2453 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2454 {
2455 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2456 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2457 struct glsl_src_param src0_param;
2458 DWORD write_mask;
2459
2460 write_mask = shader_glsl_append_dst(buffer, ins);
2461 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2462
2463 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2464 * shader versions WINED3DSIO_MOVA is used for this. */
2465 if (ins->ctx->reg_maps->shader_version.major == 1
2466 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2467 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2468 {
2469 /* This is a simple floor() */
2470 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2471 if (mask_size > 1) {
2472 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2473 } else {
2474 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2475 }
2476 }
2477 else if(ins->handler_idx == WINED3DSIH_MOVA)
2478 {
2479 /* We need to *round* to the nearest int here. */
2480 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2481
2482 if (gl_info->supported[EXT_GPU_SHADER4])
2483 {
2484 if (mask_size > 1)
2485 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2486 else
2487 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2488 }
2489 else
2490 {
2491 if (mask_size > 1)
2492 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2493 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2494 else
2495 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2496 src0_param.param_str, src0_param.param_str);
2497 }
2498 }
2499 else
2500 {
2501 shader_addline(buffer, "%s);\n", src0_param.param_str);
2502 }
2503 }
2504
2505 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2506 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2507 {
2508 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2509 struct glsl_src_param src0_param;
2510 struct glsl_src_param src1_param;
2511 DWORD dst_write_mask, src_write_mask;
2512 unsigned int dst_size = 0;
2513
2514 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2515 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2516
2517 /* dp3 works on vec3, dp4 on vec4 */
2518 if (ins->handler_idx == WINED3DSIH_DP4)
2519 {
2520 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2521 } else {
2522 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2523 }
2524
2525 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2526 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2527
2528 if (dst_size > 1) {
2529 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2530 } else {
2531 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2532 }
2533 }
2534
2535 /* Note that this instruction has some restrictions. The destination write mask
2536 * can't contain the w component, and the source swizzles have to be .xyzw */
2537 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2538 {
2539 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2540 struct glsl_src_param src0_param;
2541 struct glsl_src_param src1_param;
2542 char dst_mask[6];
2543
2544 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2545 shader_glsl_append_dst(ins->ctx->buffer, ins);
2546 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2547 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2548 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2549 }
2550
2551 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2552 {
2553 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2554 }
2555
2556 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2557 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2558 * GLSL uses the value as-is. */
2559 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2560 {
2561 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2562 struct glsl_src_param src0_param;
2563 struct glsl_src_param src1_param;
2564 DWORD dst_write_mask;
2565 unsigned int dst_size;
2566
2567 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2568 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2569
2570 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2571 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2572
2573 if (dst_size > 1)
2574 {
2575 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2576 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2577 }
2578 else
2579 {
2580 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2581 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2582 }
2583 }
2584
2585 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2586 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2587 {
2588 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2589 struct glsl_src_param src_param;
2590 const char *instruction;
2591 DWORD write_mask;
2592 unsigned i;
2593
2594 /* Determine the GLSL function to use based on the opcode */
2595 /* TODO: Possibly make this a table for faster lookups */
2596 switch (ins->handler_idx)
2597 {
2598 case WINED3DSIH_MIN: instruction = "min"; break;
2599 case WINED3DSIH_MAX: instruction = "max"; break;
2600 case WINED3DSIH_ABS: instruction = "abs"; break;
2601 case WINED3DSIH_FRC: instruction = "fract"; break;
2602 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2603 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2604 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2605 default: instruction = "";
2606 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2607 break;
2608 }
2609
2610 write_mask = shader_glsl_append_dst(buffer, ins);
2611
2612 shader_addline(buffer, "%s(", instruction);
2613
2614 if (ins->src_count)
2615 {
2616 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2617 shader_addline(buffer, "%s", src_param.param_str);
2618 for (i = 1; i < ins->src_count; ++i)
2619 {
2620 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2621 shader_addline(buffer, ", %s", src_param.param_str);
2622 }
2623 }
2624
2625 shader_addline(buffer, "));\n");
2626 }
2627
2628 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2629
2630 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2631 {
2632 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2633 struct glsl_src_param src_param;
2634 unsigned int mask_size;
2635 DWORD write_mask;
2636 char dst_mask[6];
2637
2638 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2639 mask_size = shader_glsl_get_write_mask_size(write_mask);
2640 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2641
2642 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2643 src_param.param_str, src_param.param_str);
2644 shader_glsl_append_dst(buffer, ins);
2645
2646 if (mask_size > 1)
2647 {
2648 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2649 mask_size, src_param.param_str);
2650 }
2651 else
2652 {
2653 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2654 src_param.param_str);
2655 }
2656 }
2657
2658 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2659 {
2660 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2661 struct glsl_src_param src0_param;
2662 const char *prefix, *suffix;
2663 unsigned int dst_size;
2664 DWORD dst_write_mask;
2665
2666 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2667 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2668
2669 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2670
2671 switch (ins->handler_idx)
2672 {
2673 case WINED3DSIH_EXP:
2674 case WINED3DSIH_EXPP:
2675 prefix = "exp2(";
2676 suffix = ")";
2677 break;
2678
2679 case WINED3DSIH_LOG:
2680 case WINED3DSIH_LOGP:
2681 prefix = "log2(abs(";
2682 suffix = "))";
2683 break;
2684
2685 case WINED3DSIH_RCP:
2686 prefix = "1.0 / ";
2687 suffix = "";
2688 break;
2689
2690 case WINED3DSIH_RSQ:
2691 prefix = "inversesqrt(abs(";
2692 suffix = "))";
2693 break;
2694
2695 default:
2696 prefix = "";
2697 suffix = "";
2698 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2699 break;
2700 }
2701
2702 if (dst_size > 1)
2703 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2704 else
2705 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2706 }
2707
2708 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2709 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2710 * dst.x = 2^(floor(src))
2711 * dst.y = src - floor(src)
2712 * dst.z = 2^src (partial precision is allowed, but optional)
2713 * dst.w = 1.0;
2714 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2715 * dst = 2^src; (partial precision is allowed, but optional)
2716 */
2717 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2718 {
2719 if (ins->ctx->reg_maps->shader_version.major < 2)
2720 {
2721 struct glsl_src_param src_param;
2722 char dst_mask[6];
2723
2724 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2725
2726 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2727 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2728 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2729 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2730
2731 shader_glsl_append_dst(ins->ctx->buffer, ins);
2732 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2733 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2734 return;
2735 }
2736
2737 shader_glsl_scalar_op(ins);
2738 }
2739
2740 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2741 {
2742 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2743 struct glsl_src_param src_param;
2744 unsigned int mask_size;
2745 DWORD write_mask;
2746
2747 write_mask = shader_glsl_append_dst(buffer, ins);
2748 mask_size = shader_glsl_get_write_mask_size(write_mask);
2749 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2750
2751 if (mask_size > 1)
2752 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2753 else
2754 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2755 }
2756
2757 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2758 {
2759 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2760 struct glsl_src_param src_param;
2761 unsigned int mask_size;
2762 DWORD write_mask;
2763
2764 write_mask = shader_glsl_append_dst(buffer, ins);
2765 mask_size = shader_glsl_get_write_mask_size(write_mask);
2766 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2767
2768 if (mask_size > 1)
2769 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2770 else
2771 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2772 }
2773
2774 /** Process signed comparison opcodes in GLSL. */
2775 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2776 {
2777 struct glsl_src_param src0_param;
2778 struct glsl_src_param src1_param;
2779 DWORD write_mask;
2780 unsigned int mask_size;
2781
2782 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2783 mask_size = shader_glsl_get_write_mask_size(write_mask);
2784 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2785 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2786
2787 if (mask_size > 1) {
2788 const char *compare;
2789
2790 switch(ins->handler_idx)
2791 {
2792 case WINED3DSIH_SLT: compare = "lessThan"; break;
2793 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2794 default: compare = "";
2795 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2796 }
2797
2798 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2799 src0_param.param_str, src1_param.param_str);
2800 } else {
2801 switch(ins->handler_idx)
2802 {
2803 case WINED3DSIH_SLT:
2804 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2805 * to return 0.0 but step returns 1.0 because step is not < x
2806 * An alternative is a bvec compare padded with an unused second component.
2807 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2808 * issue. Playing with not() is not possible either because not() does not accept
2809 * a scalar.
2810 */
2811 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2812 src0_param.param_str, src1_param.param_str);
2813 break;
2814 case WINED3DSIH_SGE:
2815 /* Here we can use the step() function and safe a conditional */
2816 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2817 break;
2818 default:
2819 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2820 }
2821
2822 }
2823 }
2824
2825 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2826 {
2827 const char *condition_prefix, *condition_suffix;
2828 struct wined3d_shader_dst_param dst;
2829 struct glsl_src_param src0_param;
2830 struct glsl_src_param src1_param;
2831 struct glsl_src_param src2_param;
2832 BOOL temp_destination = FALSE;
2833 DWORD cmp_channel = 0;
2834 unsigned int i, j;
2835 char mask_char[6];
2836 DWORD write_mask;
2837
2838 switch (ins->handler_idx)
2839 {
2840 case WINED3DSIH_CMP:
2841 condition_prefix = "";
2842 condition_suffix = " >= 0.0";
2843 break;
2844
2845 case WINED3DSIH_CND:
2846 condition_prefix = "";
2847 condition_suffix = " > 0.5";
2848 break;
2849
2850 case WINED3DSIH_MOVC:
2851 condition_prefix = "bool(";
2852 condition_suffix = ")";
2853 break;
2854
2855 default:
2856 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2857 condition_prefix = "<unhandled prefix>";
2858 condition_suffix = "<unhandled suffix>";
2859 break;
2860 }
2861
2862 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2863 {
2864 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2865 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2866 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2867 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2868
2869 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2870 condition_prefix, src0_param.param_str, condition_suffix,
2871 src1_param.param_str, src2_param.param_str);
2872 return;
2873 }
2874
2875 dst = ins->dst[0];
2876
2877 /* Splitting the instruction up in multiple lines imposes a problem:
2878 * The first lines may overwrite source parameters of the following lines.
2879 * Deal with that by using a temporary destination register if needed. */
2880 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2881 && ins->src[0].reg.type == dst.reg.type)
2882 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2883 && ins->src[1].reg.type == dst.reg.type)
2884 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2885 && ins->src[2].reg.type == dst.reg.type))
2886 temp_destination = TRUE;
2887
2888 /* Cycle through all source0 channels. */
2889 for (i = 0; i < 4; ++i)
2890 {
2891 write_mask = 0;
2892 /* Find the destination channels which use the current source0 channel. */
2893 for (j = 0; j < 4; ++j)
2894 {
2895 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2896 {
2897 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2898 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2899 }
2900 }
2901 dst.write_mask = ins->dst[0].write_mask & write_mask;
2902
2903 if (temp_destination)
2904 {
2905 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2906 continue;
2907 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2908 }
2909 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst)))
2910 continue;
2911
2912 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2913 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2914 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2915
2916 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2917 condition_prefix, src0_param.param_str, condition_suffix,
2918 src1_param.param_str, src2_param.param_str);
2919 }
2920
2921 if (temp_destination)
2922 {
2923 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2924 shader_glsl_append_dst(ins->ctx->buffer, ins);
2925 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2926 }
2927 }
2928
2929 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2930 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2931 * the compare is done per component of src0. */
2932 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2933 {
2934 struct glsl_src_param src0_param;
2935 struct glsl_src_param src1_param;
2936 struct glsl_src_param src2_param;
2937 DWORD write_mask;
2938 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2939 ins->ctx->reg_maps->shader_version.minor);
2940
2941 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2942 {
2943 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2944 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2945 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2946 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2947
2948 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
2949 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2950 else
2951 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2952 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2953 return;
2954 }
2955
2956 shader_glsl_conditional_move(ins);
2957 }
2958
2959 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2960 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2961 {
2962 struct glsl_src_param src0_param;
2963 struct glsl_src_param src1_param;
2964 struct glsl_src_param src2_param;
2965 DWORD write_mask;
2966
2967 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2968 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2969 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2970 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2971 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2972 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2973 }
2974
2975 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2976 Vertex shaders to GLSL codes */
2977 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2978 {
2979 int i;
2980 int nComponents = 0;
2981 struct wined3d_shader_dst_param tmp_dst = {{0}};
2982 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2983 struct wined3d_shader_instruction tmp_ins;
2984
2985 memset(&tmp_ins, 0, sizeof(tmp_ins));
2986
2987 /* Set constants for the temporary argument */
2988 tmp_ins.ctx = ins->ctx;
2989 tmp_ins.dst_count = 1;
2990 tmp_ins.dst = &tmp_dst;
2991 tmp_ins.src_count = 2;
2992 tmp_ins.src = tmp_src;
2993
2994 switch(ins->handler_idx)
2995 {
2996 case WINED3DSIH_M4x4:
2997 nComponents = 4;
2998 tmp_ins.handler_idx = WINED3DSIH_DP4;
2999 break;
3000 case WINED3DSIH_M4x3:
3001 nComponents = 3;
3002 tmp_ins.handler_idx = WINED3DSIH_DP4;
3003 break;
3004 case WINED3DSIH_M3x4:
3005 nComponents = 4;
3006 tmp_ins.handler_idx = WINED3DSIH_DP3;
3007 break;
3008 case WINED3DSIH_M3x3:
3009 nComponents = 3;
3010 tmp_ins.handler_idx = WINED3DSIH_DP3;
3011 break;
3012 case WINED3DSIH_M3x2:
3013 nComponents = 2;
3014 tmp_ins.handler_idx = WINED3DSIH_DP3;
3015 break;
3016 default:
3017 break;
3018 }
3019
3020 tmp_dst = ins->dst[0];
3021 tmp_src[0] = ins->src[0];
3022 tmp_src[1] = ins->src[1];
3023 for (i = 0; i < nComponents; ++i)
3024 {
3025 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3026 shader_glsl_dot(&tmp_ins);
3027 ++tmp_src[1].reg.idx[0].offset;
3028 }
3029 }
3030
3031 /**
3032 The LRP instruction performs a component-wise linear interpolation
3033 between the second and third operands using the first operand as the
3034 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3035 This is equivalent to mix(src2, src1, src0);
3036 */
3037 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3038 {
3039 struct glsl_src_param src0_param;
3040 struct glsl_src_param src1_param;
3041 struct glsl_src_param src2_param;
3042 DWORD write_mask;
3043
3044 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3045
3046 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3047 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3048 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3049
3050 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3051 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3052 }
3053
3054 /** Process the WINED3DSIO_LIT instruction in GLSL:
3055 * dst.x = dst.w = 1.0
3056 * dst.y = (src0.x > 0) ? src0.x
3057 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3058 * where src.w is clamped at +- 128
3059 */
3060 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3061 {
3062 struct glsl_src_param src0_param;
3063 struct glsl_src_param src1_param;
3064 struct glsl_src_param src3_param;
3065 char dst_mask[6];
3066
3067 shader_glsl_append_dst(ins->ctx->buffer, ins);
3068 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3069
3070 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3071 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3072 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3073
3074 /* The sdk specifies the instruction like this
3075 * dst.x = 1.0;
3076 * if(src.x > 0.0) dst.y = src.x
3077 * else dst.y = 0.0.
3078 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3079 * else dst.z = 0.0;
3080 * dst.w = 1.0;
3081 * (where power = src.w clamped between -128 and 128)
3082 *
3083 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3084 * dst.x = 1.0 ... No further explanation needed
3085 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3086 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3087 * dst.w = 1.0. ... Nothing fancy.
3088 *
3089 * So we still have one conditional in there. So do this:
3090 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3091 *
3092 * 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),
3093 * 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.
3094 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3095 *
3096 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3097 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3098 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3099 */
3100 shader_addline(ins->ctx->buffer,
3101 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3102 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3103 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3104 src0_param.param_str, src3_param.param_str, dst_mask);
3105 }
3106
3107 /** Process the WINED3DSIO_DST instruction in GLSL:
3108 * dst.x = 1.0
3109 * dst.y = src0.x * src0.y
3110 * dst.z = src0.z
3111 * dst.w = src1.w
3112 */
3113 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3114 {
3115 struct glsl_src_param src0y_param;
3116 struct glsl_src_param src0z_param;
3117 struct glsl_src_param src1y_param;
3118 struct glsl_src_param src1w_param;
3119 char dst_mask[6];
3120
3121 shader_glsl_append_dst(ins->ctx->buffer, ins);
3122 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3123
3124 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3125 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3126 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3127 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3128
3129 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3130 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3131 }
3132
3133 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3134 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3135 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3136 *
3137 * dst.x = cos(src0.?)
3138 * dst.y = sin(src0.?)
3139 * dst.z = dst.z
3140 * dst.w = dst.w
3141 */
3142 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3143 {
3144 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3145 struct glsl_src_param src0_param;
3146 DWORD write_mask;
3147
3148 if (ins->ctx->reg_maps->shader_version.major < 4)
3149 {
3150 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3151
3152 write_mask = shader_glsl_append_dst(buffer, ins);
3153 switch (write_mask)
3154 {
3155 case WINED3DSP_WRITEMASK_0:
3156 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3157 break;
3158
3159 case WINED3DSP_WRITEMASK_1:
3160 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3161 break;
3162
3163 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3164 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3165 src0_param.param_str, src0_param.param_str);
3166 break;
3167
3168 default:
3169 ERR("Write mask should be .x, .y or .xy\n");
3170 break;
3171 }
3172
3173 return;
3174 }
3175
3176 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3177 {
3178
3179 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3180 {
3181 char dst_mask[6];
3182
3183 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3184 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3185 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3186
3187 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3188 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3189 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3190
3191 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3192 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3193 }
3194 else
3195 {
3196 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3197 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3198 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3199 }
3200 }
3201 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3202 {
3203 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3204 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3205 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3206 }
3207 }
3208
3209 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3210 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3211 * generate invalid code
3212 */
3213 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3214 {
3215 struct glsl_src_param src0_param;
3216 DWORD write_mask;
3217
3218 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3219 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3220
3221 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3222 }
3223
3224 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3225 * Start a for() loop where src1.y is the initial value of aL,
3226 * increment aL by src1.z for a total of src1.x iterations.
3227 * Need to use a temporary variable for this operation.
3228 */
3229 /* FIXME: I don't think nested loops will work correctly this way. */
3230 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3231 {
3232 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3233 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3234 const struct wined3d_shader *shader = ins->ctx->shader;
3235 const struct wined3d_shader_lconst *constant;
3236 struct glsl_src_param src1_param;
3237 const DWORD *control_values = NULL;
3238
3239 if (ins->ctx->reg_maps->shader_version.major < 4)
3240 {
3241 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3242
3243 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3244 * class hardware doesn't support real varying indexing, but Microsoft
3245 * designed this feature for Shader model 2.x+. If the loop control is
3246 * known at compile time, the GLSL compiler can unroll the loop, and
3247 * replace indirect addressing with direct addressing. */
3248 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3249 {
3250 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3251 {
3252 if (constant->idx == ins->src[1].reg.idx[0].offset)
3253 {
3254 control_values = constant->value;
3255 break;
3256 }
3257 }
3258 }
3259
3260 if (control_values)
3261 {
3262 struct wined3d_shader_loop_control loop_control;
3263 loop_control.count = control_values[0];
3264 loop_control.start = control_values[1];
3265 loop_control.step = (int)control_values[2];
3266
3267 if (loop_control.step > 0)
3268 {
3269 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3270 loop_state->current_depth, loop_control.start,
3271 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3272 loop_state->current_depth, loop_control.step);
3273 }
3274 else if (loop_control.step < 0)
3275 {
3276 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3277 loop_state->current_depth, loop_control.start,
3278 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3279 loop_state->current_depth, loop_control.step);
3280 }
3281 else
3282 {
3283 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3284 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3285 loop_state->current_depth, loop_control.count,
3286 loop_state->current_depth);
3287 }
3288 }
3289 else
3290 {
3291 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3292 loop_state->current_depth, loop_state->current_reg,
3293 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3294 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3295 }
3296
3297 ++loop_state->current_reg;
3298 }
3299 else
3300 {
3301 shader_addline(buffer, "for (;;)\n{\n");
3302 }
3303
3304 ++loop_state->current_depth;
3305 }
3306
3307 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3308 {
3309 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3310
3311 shader_addline(ins->ctx->buffer, "}\n");
3312
3313 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3314 {
3315 --loop_state->current_depth;
3316 --loop_state->current_reg;
3317 }
3318
3319 if (ins->handler_idx == WINED3DSIH_ENDREP)
3320 {
3321 --loop_state->current_depth;
3322 }
3323 }
3324
3325 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3326 {
3327 const struct wined3d_shader *shader = ins->ctx->shader;
3328 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3329 const struct wined3d_shader_lconst *constant;
3330 struct glsl_src_param src0_param;
3331 const DWORD *control_values = NULL;
3332
3333 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3334 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3335 {
3336 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3337 {
3338 if (constant->idx == ins->src[0].reg.idx[0].offset)
3339 {
3340 control_values = constant->value;
3341 break;
3342 }
3343 }
3344 }
3345
3346 if (control_values)
3347 {
3348 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3349 loop_state->current_depth, loop_state->current_depth,
3350 control_values[0], loop_state->current_depth);
3351 }
3352 else
3353 {
3354 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3355 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3356 loop_state->current_depth, loop_state->current_depth,
3357 src0_param.param_str, loop_state->current_depth);
3358 }
3359
3360 ++loop_state->current_depth;
3361 }
3362
3363 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3364 {
3365 struct glsl_src_param src0_param;
3366
3367 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3368 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
3369 }
3370
3371 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3372 {
3373 struct glsl_src_param src0_param;
3374 struct glsl_src_param src1_param;
3375
3376 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3377 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3378
3379 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3380 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3381 }
3382
3383 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3384 {
3385 shader_addline(ins->ctx->buffer, "} else {\n");
3386 }
3387
3388 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3389 {
3390 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3391 }
3392
3393 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3394 {
3395 shader_addline(ins->ctx->buffer, "break;\n");
3396 }
3397
3398 /* FIXME: According to MSDN the compare is done per component. */
3399 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3400 {
3401 struct glsl_src_param src0_param;
3402 struct glsl_src_param src1_param;
3403
3404 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3405 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3406
3407 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3408 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3409 }
3410
3411 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3412 {
3413 struct glsl_src_param src_param;
3414
3415 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3416 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3417 }
3418
3419 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3420 {
3421 shader_addline(ins->ctx->buffer, "}\n");
3422 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3423 }
3424
3425 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3426 {
3427 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3428 }
3429
3430 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3431 {
3432 struct glsl_src_param src1_param;
3433
3434 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3435 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3436 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3437 }
3438
3439 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3440 {
3441 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3442 * function only suppresses the unhandled instruction warning
3443 */
3444 }
3445
3446 /*********************************************
3447 * Pixel Shader Specific Code begins here
3448 ********************************************/
3449 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3450 {
3451 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3452 ins->ctx->reg_maps->shader_version.minor);
3453 struct glsl_sample_function sample_function;
3454 DWORD sample_flags = 0;
3455 DWORD sampler_idx;
3456 DWORD mask = 0, swizzle;
3457 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3458
3459 /* 1.0-1.4: Use destination register as sampler source.
3460 * 2.0+: Use provided sampler source. */
3461 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3462 sampler_idx = ins->dst[0].reg.idx[0].offset;
3463 else
3464 sampler_idx = ins->src[1].reg.idx[0].offset;
3465
3466 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3467 {
3468 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3469 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3470 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3471
3472 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3473 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3474 {
3475 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3476 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3477 {
3478 case WINED3D_TTFF_COUNT1:
3479 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3480 break;
3481 case WINED3D_TTFF_COUNT2:
3482 mask = WINED3DSP_WRITEMASK_1;
3483 break;
3484 case WINED3D_TTFF_COUNT3:
3485 mask = WINED3DSP_WRITEMASK_2;
3486 break;
3487 case WINED3D_TTFF_COUNT4:
3488 case WINED3D_TTFF_DISABLE:
3489 mask = WINED3DSP_WRITEMASK_3;
3490 break;
3491 }
3492 }
3493 }
3494 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3495 {
3496 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3497
3498 if (src_mod == WINED3DSPSM_DZ) {
3499 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3500 mask = WINED3DSP_WRITEMASK_2;
3501 } else if (src_mod == WINED3DSPSM_DW) {
3502 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3503 mask = WINED3DSP_WRITEMASK_3;
3504 }
3505 }
3506 else
3507 {
3508 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3509 && ins->ctx->reg_maps->sampler_type[sampler_idx] != WINED3DSTT_CUBE)
3510 {
3511 /* ps 2.0 texldp instruction always divides by the fourth component. */
3512 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3513 mask = WINED3DSP_WRITEMASK_3;
3514 }
3515 }
3516
3517 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3518 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3519
3520 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3521 mask |= sample_function.coord_mask;
3522
3523 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3524 else swizzle = ins->src[1].swizzle;
3525
3526 /* 1.0-1.3: Use destination register as coordinate source.
3527 1.4+: Use provided coordinate source register. */
3528 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3529 {
3530 char coord_mask[6];
3531 shader_glsl_write_mask_to_str(mask, coord_mask);
3532 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3533 "T%u%s", sampler_idx, coord_mask);
3534 }
3535 else
3536 {
3537 struct glsl_src_param coord_param;
3538 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3539 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3540 {
3541 struct glsl_src_param bias;
3542 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3543 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3544 "%s", coord_param.param_str);
3545 } else {
3546 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3547 "%s", coord_param.param_str);
3548 }
3549 }
3550 }
3551
3552 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3553 {
3554 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3555 struct glsl_src_param coord_param, dx_param, dy_param;
3556 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3557 struct glsl_sample_function sample_function;
3558 DWORD sampler_idx;
3559 DWORD swizzle = ins->src[1].swizzle;
3560 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3561
3562 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3563 {
3564 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3565 shader_glsl_tex(ins);
3566 return;
3567 }
3568
3569 sampler_idx = ins->src[1].reg.idx[0].offset;
3570 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3571 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3572
3573 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3574 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3575 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3576 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3577
3578 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3579 "%s", coord_param.param_str);
3580 }
3581
3582 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3583 {
3584 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3585 struct glsl_src_param coord_param, lod_param;
3586 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3587 struct glsl_sample_function sample_function;
3588 DWORD sampler_idx;
3589 DWORD swizzle = ins->src[1].swizzle;
3590 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3591
3592 sampler_idx = ins->src[1].reg.idx[0].offset;
3593 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3594 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3595 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3596
3597 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3598 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3599
3600 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3601
3602 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3603 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3604 {
3605 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3606 * However, the NVIDIA drivers allow them in fragment shaders as well,
3607 * even without the appropriate extension. */
3608 WARN("Using %s in fragment shader.\n", sample_function.name);
3609 }
3610 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3611 "%s", coord_param.param_str);
3612 }
3613
3614 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3615 {
3616 /* FIXME: Make this work for more than just 2D textures */
3617 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3618 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3619
3620 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3621 {
3622 char dst_mask[6];
3623
3624 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3625 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3626 ins->dst[0].reg.idx[0].offset, dst_mask);
3627 }
3628 else
3629 {
3630 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3631 DWORD reg = ins->src[0].reg.idx[0].offset;
3632 char dst_swizzle[6];
3633
3634 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3635
3636 if (src_mod == WINED3DSPSM_DZ)
3637 {
3638 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3639 struct glsl_src_param div_param;
3640
3641 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3642
3643 if (mask_size > 1) {
3644 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3645 } else {
3646 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3647 }
3648 }
3649 else if (src_mod == WINED3DSPSM_DW)
3650 {
3651 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3652 struct glsl_src_param div_param;
3653
3654 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3655
3656 if (mask_size > 1) {
3657 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3658 } else {
3659 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3660 }
3661 } else {
3662 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3663 }
3664 }
3665 }
3666
3667 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3668 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3669 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3670 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3671 {
3672 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3673 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3674 struct glsl_sample_function sample_function;
3675 struct glsl_src_param src0_param;
3676 UINT mask_size;
3677
3678 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3679
3680 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3681 * scalar, and projected sampling would require 4.
3682 *
3683 * It is a dependent read - not valid with conditional NP2 textures
3684 */
3685 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3686 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3687
3688 switch(mask_size)
3689 {
3690 case 1:
3691 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3692 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3693 break;
3694
3695 case 2:
3696 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3697 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3698 break;
3699
3700 case 3:
3701 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3702 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3703 break;
3704
3705 default:
3706 FIXME("Unexpected mask size %u\n", mask_size);
3707 break;
3708 }
3709 }
3710
3711 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3712 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3713 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3714 {
3715 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3716 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3717 struct glsl_src_param src0_param;
3718 DWORD dst_mask;
3719 unsigned int mask_size;
3720
3721 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3722 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3723 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3724
3725 if (mask_size > 1) {
3726 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3727 } else {
3728 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3729 }
3730 }
3731
3732 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3733 * Calculate the depth as dst.x / dst.y */
3734 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3735 {
3736 struct glsl_dst_param dst_param;
3737
3738 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3739
3740 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3741 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3742 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3743 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3744 * >= 1.0 or < 0.0
3745 */
3746 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3747 dst_param.reg_name, dst_param.reg_name);
3748 }
3749
3750 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3751 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3752 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3753 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3754 */
3755 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3756 {
3757 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3758 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3759 struct glsl_src_param src0_param;
3760
3761 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3762
3763 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3764 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3765 }
3766
3767 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3768 * Calculate the 1st of a 2-row matrix multiplication. */
3769 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3770 {
3771 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3772 DWORD reg = ins->dst[0].reg.idx[0].offset;
3773 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3774 struct glsl_src_param src0_param;
3775
3776 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3777 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3778 }
3779
3780 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3781 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3782 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3783 {
3784 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3785 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3786 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3787 DWORD reg = ins->dst[0].reg.idx[0].offset;
3788 struct glsl_src_param src0_param;
3789
3790 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3791 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3792 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3793 }
3794
3795 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3796 {
3797 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3798 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3799 struct glsl_sample_function sample_function;
3800 DWORD reg = ins->dst[0].reg.idx[0].offset;
3801 struct glsl_src_param src0_param;
3802
3803 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3804 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3805
3806 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3807
3808 /* Sample the texture using the calculated coordinates */
3809 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3810 }
3811
3812 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3813 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3814 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3815 {
3816 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3817 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3818 struct glsl_sample_function sample_function;
3819 DWORD reg = ins->dst[0].reg.idx[0].offset;
3820 struct glsl_src_param src0_param;
3821
3822 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3823 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3824
3825 /* Dependent read, not valid with conditional NP2 */
3826 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3827
3828 /* Sample the texture using the calculated coordinates */
3829 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3830
3831 tex_mx->current_row = 0;
3832 }
3833
3834 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3835 * Perform the 3rd row of a 3x3 matrix multiply */
3836 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3837 {
3838 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3839 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3840 DWORD reg = ins->dst[0].reg.idx[0].offset;
3841 struct glsl_src_param src0_param;
3842 char dst_mask[6];
3843
3844 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3845
3846 shader_glsl_append_dst(ins->ctx->buffer, ins);
3847 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3848 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3849
3850 tex_mx->current_row = 0;
3851 }
3852
3853 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3854 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3855 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3856 {
3857 struct glsl_src_param src0_param;
3858 struct glsl_src_param src1_param;
3859 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3860 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3861 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3862 struct glsl_sample_function sample_function;
3863 DWORD reg = ins->dst[0].reg.idx[0].offset;
3864 char coord_mask[6];
3865
3866 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3867 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3868
3869 /* Perform the last matrix multiply operation */
3870 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3871 /* Reflection calculation */
3872 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3873
3874 /* Dependent read, not valid with conditional NP2 */
3875 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3876 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3877
3878 /* Sample the texture */
3879 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3880 NULL, NULL, NULL, "tmp0%s", coord_mask);
3881
3882 tex_mx->current_row = 0;
3883 }
3884
3885 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3886 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3887 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3888 {
3889 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3890 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3891 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3892 struct glsl_sample_function sample_function;
3893 DWORD reg = ins->dst[0].reg.idx[0].offset;
3894 struct glsl_src_param src0_param;
3895 char coord_mask[6];
3896
3897 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3898
3899 /* Perform the last matrix multiply operation */
3900 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3901
3902 /* Construct the eye-ray vector from w coordinates */
3903 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3904 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3905 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3906
3907 /* Dependent read, not valid with conditional NP2 */
3908 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3909 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3910
3911 /* Sample the texture using the calculated coordinates */
3912 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3913 NULL, NULL, NULL, "tmp0%s", coord_mask);
3914
3915 tex_mx->current_row = 0;
3916 }
3917
3918 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3919 * Apply a fake bump map transform.
3920 * texbem is pshader <= 1.3 only, this saves a few version checks
3921 */
3922 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3923 {
3924 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3925 struct glsl_sample_function sample_function;
3926 struct glsl_src_param coord_param;
3927 DWORD sampler_idx;
3928 DWORD mask;
3929 DWORD flags;
3930 char coord_mask[6];
3931
3932 sampler_idx = ins->dst[0].reg.idx[0].offset;
3933 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3934 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3935
3936 /* Dependent read, not valid with conditional NP2 */
3937 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3938 mask = sample_function.coord_mask;
3939
3940 shader_glsl_write_mask_to_str(mask, coord_mask);
3941
3942 /* With projected textures, texbem only divides the static texture coord,
3943 * not the displacement, so we can't let GL handle this. */
3944 if (flags & WINED3D_PSARGS_PROJECTED)
3945 {
3946 DWORD div_mask=0;
3947 char coord_div_mask[3];
3948 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3949 {
3950 case WINED3D_TTFF_COUNT1:
3951 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3952 break;
3953 case WINED3D_TTFF_COUNT2:
3954 div_mask = WINED3DSP_WRITEMASK_1;
3955 break;
3956 case WINED3D_TTFF_COUNT3:
3957 div_mask = WINED3DSP_WRITEMASK_2;
3958 break;
3959 case WINED3D_TTFF_COUNT4:
3960 case WINED3D_TTFF_DISABLE:
3961 div_mask = WINED3DSP_WRITEMASK_3;
3962 break;
3963 }
3964 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3965 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3966 }
3967
3968 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3969
3970 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3971 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3972 coord_param.param_str, coord_mask);
3973
3974 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3975 {
3976 struct glsl_src_param luminance_param;
3977 struct glsl_dst_param dst_param;
3978
3979 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3980 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3981
3982 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
3983 dst_param.reg_name, dst_param.mask_str,
3984 luminance_param.param_str, sampler_idx, sampler_idx);
3985 }
3986 }
3987
3988 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3989 {
3990 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3991 struct glsl_src_param src0_param, src1_param;
3992
3993 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3994 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3995
3996 shader_glsl_append_dst(ins->ctx->buffer, ins);
3997 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
3998 src0_param.param_str, sampler_idx, src1_param.param_str);
3999 }
4000
4001 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4002 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4003 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4004 {
4005 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4006 struct glsl_sample_function sample_function;
4007 struct glsl_src_param src0_param;
4008
4009 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4010
4011 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4012 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4013 "%s.wx", src0_param.reg_name);
4014 }
4015
4016 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4017 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4018 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4019 {
4020 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4021 struct glsl_sample_function sample_function;
4022 struct glsl_src_param src0_param;
4023
4024 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4025
4026 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4027 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4028 "%s.yz", src0_param.reg_name);
4029 }
4030
4031 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4032 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4033 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4034 {
4035 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4036 struct glsl_sample_function sample_function;
4037 struct glsl_src_param src0_param;
4038
4039 /* Dependent read, not valid with conditional NP2 */
4040 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4041 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4042
4043 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4044 "%s", src0_param.param_str);
4045 }
4046
4047 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4048 * If any of the first 3 components are < 0, discard this pixel */
4049 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4050 {
4051 struct glsl_dst_param dst_param;
4052
4053 /* The argument is a destination parameter, and no writemasks are allowed */
4054 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4055 if (ins->ctx->reg_maps->shader_version.major >= 2)
4056 {
4057 /* 2.0 shaders compare all 4 components in texkill */
4058 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4059 } else {
4060 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4061 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4062 * 4 components are defined, only the first 3 are used
4063 */
4064 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4065 }
4066 }
4067
4068 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4069 * dst = dot2(src0, src1) + src2 */
4070 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4071 {
4072 struct glsl_src_param src0_param;
4073 struct glsl_src_param src1_param;
4074 struct glsl_src_param src2_param;
4075 DWORD write_mask;
4076 unsigned int mask_size;
4077
4078 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4079 mask_size = shader_glsl_get_write_mask_size(write_mask);
4080
4081 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4082 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4083 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4084
4085 if (mask_size > 1) {
4086 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4087 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4088 } else {
4089 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4090 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4091 }
4092 }
4093
4094 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4095 const struct wined3d_shader_signature_element *input_signature,
4096 const struct wined3d_shader_reg_maps *reg_maps,
4097 enum vertexprocessing_mode vertexprocessing)
4098 {
4099 WORD map = reg_maps->input_registers;
4100 unsigned int i;
4101
4102 for (i = 0; map; map >>= 1, ++i)
4103 {
4104 const char *semantic_name;
4105 UINT semantic_idx;
4106 char reg_mask[6];
4107
4108 /* Unused */
4109 if (!(map & 1)) continue;
4110
4111 semantic_name = input_signature[i].semantic_name;
4112 semantic_idx = input_signature[i].semantic_idx;
4113 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4114
4115 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4116 {
4117 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4118 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4119 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4120 else
4121 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4122 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4123 }
4124 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4125 {
4126 if (!semantic_idx)
4127 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4128 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4129 else if (semantic_idx == 1)
4130 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4131 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4132 else
4133 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4134 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4135 }
4136 else
4137 {
4138 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4139 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4140 }
4141 }
4142 }
4143
4144 /*********************************************
4145 * Vertex Shader Specific Code begins here
4146 ********************************************/
4147
4148 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4149 {
4150 struct glsl_program_key key;
4151
4152 key.vs_id = entry->vs.id;
4153 key.gs_id = entry->gs.id;
4154 key.ps_id = entry->ps.id;
4155
4156 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4157 {
4158 ERR("Failed to insert program entry.\n");
4159 }
4160 }
4161
4162 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4163 GLhandleARB vs_id, GLhandleARB gs_id, GLhandleARB ps_id)
4164 {
4165 struct wine_rb_entry *entry;
4166 struct glsl_program_key key;
4167
4168 key.vs_id = vs_id;
4169 key.gs_id = gs_id;
4170 key.ps_id = ps_id;
4171
4172 entry = wine_rb_get(&priv->program_lookup, &key);
4173 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4174 }
4175
4176 /* Context activation is done by the caller. */
4177 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4178 struct glsl_shader_prog_link *entry)
4179 {
4180 struct glsl_program_key key;
4181
4182 key.vs_id = entry->vs.id;
4183 key.gs_id = entry->gs.id;
4184 key.ps_id = entry->ps.id;
4185 wine_rb_remove(&priv->program_lookup, &key);
4186
4187 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4188 if (entry->vs.id)
4189 list_remove(&entry->vs.shader_entry);
4190 if (entry->gs.id)
4191 list_remove(&entry->gs.shader_entry);
4192 if (entry->ps.id)
4193 list_remove(&entry->ps.shader_entry);
4194 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4195 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4196 HeapFree(GetProcessHeap(), 0, entry);
4197 }
4198
4199 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4200 const struct wined3d_gl_info *gl_info, const DWORD *map,
4201 const struct wined3d_shader_signature_element *input_signature,
4202 const struct wined3d_shader_reg_maps *reg_maps_in,
4203 const struct wined3d_shader_signature_element *output_signature,
4204 const struct wined3d_shader_reg_maps *reg_maps_out)
4205 {
4206 unsigned int i, j;
4207 const char *semantic_name_in;
4208 UINT semantic_idx_in;
4209 DWORD *set;
4210 DWORD in_idx;
4211 unsigned int in_count = vec4_varyings(3, gl_info);
4212 char reg_mask[6];
4213 char destination[50];
4214 WORD input_map, output_map;
4215
4216 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4217
4218 input_map = reg_maps_in->input_registers;
4219 for (i = 0; input_map; input_map >>= 1, ++i)
4220 {
4221 if (!(input_map & 1)) continue;
4222
4223 in_idx = map[i];
4224 /* Declared, but not read register */
4225 if (in_idx == ~0U) continue;
4226 if (in_idx >= (in_count + 2))
4227 {
4228 FIXME("More input varyings declared than supported, expect issues.\n");
4229 continue;
4230 }
4231
4232 if (in_idx == in_count)
4233 sprintf(destination, "gl_FrontColor");
4234 else if (in_idx == in_count + 1)
4235 sprintf(destination, "gl_FrontSecondaryColor");
4236 else
4237 sprintf(destination, "ps_in[%u]", in_idx);
4238
4239 semantic_name_in = input_signature[i].semantic_name;
4240 semantic_idx_in = input_signature[i].semantic_idx;
4241 set[in_idx] = ~0U;
4242
4243 output_map = reg_maps_out->output_registers;
4244 for (j = 0; output_map; output_map >>= 1, ++j)
4245 {
4246 DWORD mask;
4247
4248 if (!(output_map & 1)
4249 || semantic_idx_in != output_signature[j].semantic_idx
4250 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4251 || !(mask = input_signature[i].mask & output_signature[j].mask))
4252 continue;
4253
4254 set[in_idx] = mask;
4255 shader_glsl_write_mask_to_str(mask, reg_mask);
4256
4257 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4258 destination, reg_mask, j, reg_mask);
4259 }
4260 }
4261
4262 for (i = 0; i < in_count + 2; ++i)
4263 {
4264 unsigned int size;
4265
4266 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4267 continue;
4268
4269 if (set[i] == ~0U) set[i] = 0;
4270
4271 size = 0;
4272 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4273 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4274 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4275 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4276 reg_mask[size] = '\0';
4277
4278 if (i == in_count)
4279 sprintf(destination, "gl_FrontColor");
4280 else if (i == in_count + 1)
4281 sprintf(destination, "gl_FrontSecondaryColor");
4282 else
4283 sprintf(destination, "ps_in[%u]", i);
4284
4285 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4286 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4287 }
4288
4289 HeapFree(GetProcessHeap(), 0, set);
4290 }
4291
4292 /* Context activation is done by the caller. */
4293 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4294 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4295 const struct wined3d_gl_info *gl_info)
4296 {
4297 GLhandleARB ret = 0;
4298 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4299 unsigned int i;
4300 const char *semantic_name;
4301 UINT semantic_idx;
4302 char reg_mask[6];
4303 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4304 WORD map = vs->reg_maps.output_registers;
4305
4306 shader_buffer_clear(buffer);
4307
4308 shader_addline(buffer, "#version 120\n");
4309
4310 if (ps_major < 3)
4311 {
4312 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4313
4314 for (i = 0; map; map >>= 1, ++i)
4315 {
4316 DWORD write_mask;
4317
4318 if (!(map & 1)) continue;
4319
4320 semantic_name = output_signature[i].semantic_name;
4321 semantic_idx = output_signature[i].semantic_idx;
4322 write_mask = output_signature[i].mask;
4323 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4324
4325 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4326 {
4327 if (!semantic_idx)
4328 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4329 reg_mask, i, reg_mask);
4330 else if (semantic_idx == 1)
4331 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4332 reg_mask, i, reg_mask);
4333 }
4334 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4335 {
4336 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4337 reg_mask, i, reg_mask);
4338 }
4339 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4340 {
4341 if (semantic_idx < 8)
4342 {
4343 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4344 write_mask |= WINED3DSP_WRITEMASK_3;
4345
4346 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4347 semantic_idx, reg_mask, i, reg_mask);
4348 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4349 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4350 }
4351 }
4352 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4353 {
4354 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4355 }
4356 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4357 {
4358 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4359 }
4360 }
4361 shader_addline(buffer, "}\n");
4362 }
4363 else
4364 {
4365 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
4366 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4367 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4368 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4369
4370 /* First, sort out position and point size. Those are not passed to the pixel shader */
4371 for (i = 0; map; map >>= 1, ++i)
4372 {
4373 if (!(map & 1)) continue;
4374
4375 semantic_name = output_signature[i].semantic_name;
4376 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4377
4378 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4379 {
4380 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4381 reg_mask, i, reg_mask);
4382 }
4383 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4384 {
4385 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4386 }
4387 }
4388
4389 /* Then, fix the pixel shader input */
4390 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4391 &ps->reg_maps, output_signature, &vs->reg_maps);
4392
4393 shader_addline(buffer, "}\n");
4394 }
4395
4396 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4397 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4398 shader_glsl_compile(gl_info, ret, buffer->buffer);
4399
4400 return ret;
4401 }
4402
4403 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4404 {
4405 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4406 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4407 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4408 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4409 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4410 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4411 }
4412
4413 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4414 {
4415 switch (mode)
4416 {
4417 case WINED3D_FFP_PS_FOG_OFF:
4418 return;
4419
4420 case WINED3D_FFP_PS_FOG_LINEAR:
4421 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4422 break;
4423
4424 case WINED3D_FFP_PS_FOG_EXP:
4425 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4426 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4427 break;
4428
4429 case WINED3D_FFP_PS_FOG_EXP2:
4430 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4431 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4432 break;
4433
4434 default:
4435 ERR("Invalid fog mode %#x.\n", mode);
4436 return;
4437 }
4438
4439 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4440 }
4441
4442 /* Context activation is done by the caller. */
4443 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4444 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4445 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4446 {
4447 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4448 const struct wined3d_gl_info *gl_info = context->gl_info;
4449 const DWORD *function = shader->function;
4450 struct shader_glsl_ctx_priv priv_ctx;
4451
4452 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4453 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4454
4455 memset(&priv_ctx, 0, sizeof(priv_ctx));
4456 priv_ctx.cur_ps_args = args;
4457 priv_ctx.cur_np2fixup_info = np2fixup_info;
4458
4459 shader_addline(buffer, "#version 120\n");
4460
4461 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4462 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4463 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4464 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4465 /* The spec says that it doesn't have to be explicitly enabled, but the
4466 * nvidia drivers write a warning if we don't do so. */
4467 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4468 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4469 if (gl_info->supported[EXT_GPU_SHADER4])
4470 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4471
4472 /* Base Declarations */
4473 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4474
4475 /* Pack 3.0 inputs */
4476 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4477 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4478
4479 /* Base Shader Body */
4480 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4481
4482 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4483 if (reg_maps->shader_version.major < 2)
4484 {
4485 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4486 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4487 }
4488
4489 if (args->srgb_correction)
4490 shader_glsl_generate_srgb_write_correction(buffer);
4491
4492 /* SM < 3 does not replace the fog stage. */
4493 if (reg_maps->shader_version.major < 3)
4494 shader_glsl_generate_fog_code(buffer, args->fog);
4495
4496 shader_addline(buffer, "}\n");
4497
4498 TRACE("Compiling shader object %u\n", shader_obj);
4499 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4500
4501 /* Store the shader object */
4502 return shader_obj;
4503 }
4504
4505 /* Context activation is done by the caller. */
4506 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4507 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4508 const struct vs_compile_args *args)
4509 {
4510 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4511 const struct wined3d_gl_info *gl_info = context->gl_info;
4512 const DWORD *function = shader->function;
4513 struct shader_glsl_ctx_priv priv_ctx;
4514
4515 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4516 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4517
4518 shader_addline(buffer, "#version 120\n");
4519
4520 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4521 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4522 if (gl_info->supported[EXT_GPU_SHADER4])
4523 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4524
4525 memset(&priv_ctx, 0, sizeof(priv_ctx));
4526 priv_ctx.cur_vs_args = args;
4527
4528 /* Base Declarations */
4529 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4530
4531 /* Base Shader Body */
4532 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4533
4534 /* Unpack outputs */
4535 shader_addline(buffer, "order_ps_input(vs_out);\n");
4536
4537 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4538 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4539 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4540 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4541 */
4542 if (args->fog_src == VS_FOG_Z)
4543 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4544 else if (!reg_maps->fog)
4545 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4546
4547 /* We always store the clipplanes without y inversion */
4548 if (args->clip_enabled)
4549 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4550
4551 /* Write the final position.
4552 *
4553 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4554 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4555 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4556 * contains 1.0 to allow a mad.
4557 */
4558 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4559 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4560
4561 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4562 *
4563 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4564 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4565 * which is the same as z = z * 2 - w.
4566 */
4567 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4568
4569 shader_addline(buffer, "}\n");
4570
4571 TRACE("Compiling shader object %u\n", shader_obj);
4572 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4573
4574 return shader_obj;
4575 }
4576
4577 /* Context activation is done by the caller. */
4578 static GLhandleARB shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4579 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4580 {
4581 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4582 const struct wined3d_gl_info *gl_info = context->gl_info;
4583 const DWORD *function = shader->function;
4584 struct shader_glsl_ctx_priv priv_ctx;
4585 GLhandleARB shader_id;
4586
4587 shader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB));
4588
4589 shader_addline(buffer, "#version 120\n");
4590
4591 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4592 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4593 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4594 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4595 if (gl_info->supported[EXT_GPU_SHADER4])
4596 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4597
4598 memset(&priv_ctx, 0, sizeof(priv_ctx));
4599 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4600 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4601 shader_addline(buffer, "}\n");
4602
4603 TRACE("Compiling shader object %u.\n", shader_id);
4604 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4605
4606 return shader_id;
4607 }
4608
4609 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4610 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4611 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4612 {
4613 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4614 struct glsl_shader_private *shader_data;
4615 struct ps_np2fixup_info *np2fixup;
4616 UINT i;
4617 DWORD new_size;
4618 GLhandleARB ret;
4619
4620 if (!shader->backend_data)
4621 {
4622 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4623 if (!shader->backend_data)
4624 {
4625 ERR("Failed to allocate backend data.\n");
4626 return 0;
4627 }
4628 }
4629 shader_data = shader->backend_data;
4630 gl_shaders = shader_data->gl_shaders.ps;
4631
4632 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4633 * so a linear search is more performant than a hashmap or a binary search
4634 * (cache coherency etc)
4635 */
4636 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4637 {
4638 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4639 {
4640 if (args->np2_fixup)
4641 *np2fixup_info = &gl_shaders[i].np2fixup;
4642 return gl_shaders[i].prgId;
4643 }
4644 }
4645
4646 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4647 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4648 if (shader_data->num_gl_shaders)
4649 {
4650 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4651 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4652 new_size * sizeof(*gl_shaders));
4653 }
4654 else
4655 {
4656 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4657 new_size = 1;
4658 }
4659
4660 if(!new_array) {
4661 ERR("Out of memory\n");
4662 return 0;
4663 }
4664 shader_data->gl_shaders.ps = new_array;
4665 shader_data->shader_array_size = new_size;
4666 gl_shaders = new_array;
4667 }
4668
4669 gl_shaders[shader_data->num_gl_shaders].args = *args;
4670
4671 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4672 memset(np2fixup, 0, sizeof(*np2fixup));
4673 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4674
4675 pixelshader_update_samplers(shader, args->tex_types);
4676
4677 shader_buffer_clear(buffer);
4678 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4679 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4680
4681 return ret;
4682 }
4683
4684 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4685 const DWORD use_map) {
4686 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4687 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4688 return stored->fog_src == new->fog_src;
4689 }
4690
4691 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4692 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4693 const struct vs_compile_args *args)
4694 {
4695 UINT i;
4696 DWORD new_size;
4697 DWORD use_map = context->stream_info.use_map;
4698 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4699 struct glsl_shader_private *shader_data;
4700 GLhandleARB ret;
4701
4702 if (!shader->backend_data)
4703 {
4704 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4705 if (!shader->backend_data)
4706 {
4707 ERR("Failed to allocate backend data.\n");
4708 return 0;
4709 }
4710 }
4711 shader_data = shader->backend_data;
4712 gl_shaders = shader_data->gl_shaders.vs;
4713
4714 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4715 * so a linear search is more performant than a hashmap or a binary search
4716 * (cache coherency etc)
4717 */
4718 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4719 {
4720 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4721 return gl_shaders[i].prgId;
4722 }
4723
4724 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4725
4726 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4727 if (shader_data->num_gl_shaders)
4728 {
4729 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4730 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4731 new_size * sizeof(*gl_shaders));
4732 }
4733 else
4734 {
4735 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4736 new_size = 1;
4737 }
4738
4739 if(!new_array) {
4740 ERR("Out of memory\n");
4741 return 0;
4742 }
4743 shader_data->gl_shaders.vs = new_array;
4744 shader_data->shader_array_size = new_size;
4745 gl_shaders = new_array;
4746 }
4747
4748 gl_shaders[shader_data->num_gl_shaders].args = *args;
4749
4750 shader_buffer_clear(buffer);
4751 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4752 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4753
4754 return ret;
4755 }
4756
4757 static GLhandleARB find_glsl_geometry_shader(const struct wined3d_context *context,
4758 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4759 {
4760 struct glsl_gs_compiled_shader *gl_shaders;
4761 struct glsl_shader_private *shader_data;
4762 GLhandleARB ret;
4763
4764 if (!shader->backend_data)
4765 {
4766 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4767 {
4768 ERR("Failed to allocate backend data.\n");
4769 return 0;
4770 }
4771 }
4772 shader_data = shader->backend_data;
4773 gl_shaders = shader_data->gl_shaders.gs;
4774
4775 if (shader_data->num_gl_shaders)
4776 return gl_shaders[0].id;
4777
4778 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4779
4780 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4781 {
4782 ERR("Failed to allocate GL shader array.\n");
4783 return 0;
4784 }
4785 shader_data->shader_array_size = 1;
4786 gl_shaders = shader_data->gl_shaders.gs;
4787
4788 shader_buffer_clear(buffer);
4789 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4790 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4791
4792 return ret;
4793 }
4794
4795 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4796 {
4797 switch (mcs)
4798 {
4799 case WINED3D_MCS_MATERIAL:
4800 return material;
4801 case WINED3D_MCS_COLOR1:
4802 return "gl_Color";
4803 case WINED3D_MCS_COLOR2:
4804 return "gl_SecondaryColor";
4805 default:
4806 ERR("Invalid material color source %#x.\n", mcs);
4807 return "<invalid>";
4808 }
4809 }
4810
4811 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4812 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4813 {
4814 const char *diffuse, *specular, *emission, *ambient;
4815 enum wined3d_light_type light_type;
4816 unsigned int i;
4817
4818 if (!settings->lighting)
4819 {
4820 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4821 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4822 return;
4823 }
4824
4825 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4826 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4827 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4828 shader_addline(buffer, "vec3 dir, dst;\n");
4829 shader_addline(buffer, "float att, t;\n");
4830
4831 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4832 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4833 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4834 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4835
4836 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4837 {
4838 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4839 switch (light_type)
4840 {
4841 case WINED3D_LIGHT_POINT:
4842 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4843 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4844 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4845 shader_addline(buffer, "dst.x = 1.0;\n");
4846 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4847 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4848 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4849 if (!settings->normal)
4850 break;
4851 shader_addline(buffer, "dir = normalize(dir);\n");
4852 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4853 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4854 if (settings->localviewer)
4855 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4856 else
4857 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4858 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4859 " * gl_LightSource[%u].specular) / att;\n", i);
4860 break;
4861
4862 case WINED3D_LIGHT_SPOT:
4863 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4864 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4865 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4866 shader_addline(buffer, "dst.x = 1.0;\n");
4867 shader_addline(buffer, "dir = normalize(dir);\n");
4868 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4869 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4870 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4871 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4872 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4873 i, i, i, i);
4874 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4875 if (!settings->normal)
4876 break;
4877 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4878 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4879 if (settings->localviewer)
4880 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4881 else
4882 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4883 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4884 " * gl_LightSource[%u].specular) * att;\n", i);
4885 break;
4886
4887 case WINED3D_LIGHT_DIRECTIONAL:
4888 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4889 if (!settings->normal)
4890 break;
4891 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4892 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
4893 " * gl_LightSource[%u].diffuse.xyz;\n", i);
4894 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4895 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4896 " * gl_LightSource[%u].specular;\n", i);
4897 break;
4898
4899 default:
4900 if (light_type)
4901 FIXME("Unhandled light type %#x.\n", light_type);
4902 continue;
4903 }
4904 }
4905
4906 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4907 ambient, diffuse, emission);
4908 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4909 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4910 }
4911
4912 /* Context activation is done by the caller. */
4913 static GLhandleARB shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4914 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4915 {
4916 GLhandleARB shader_obj;
4917 unsigned int i;
4918
4919 shader_buffer_clear(buffer);
4920
4921 shader_addline(buffer, "#version 120\n");
4922 shader_addline(buffer, "\n");
4923 shader_addline(buffer, "void main()\n{\n");
4924 shader_addline(buffer, "float m;\n");
4925 shader_addline(buffer, "vec3 r;\n");
4926
4927 if (settings->transformed)
4928 {
4929 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
4930 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4931 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
4932 }
4933 else
4934 {
4935 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
4936 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4937 if (settings->clipping)
4938 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
4939 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
4940 }
4941
4942 if (!settings->normal)
4943 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
4944 else if (settings->normalize)
4945 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
4946 else
4947 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
4948
4949 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
4950
4951 for (i = 0; i < MAX_TEXTURES; ++i)
4952 {
4953 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
4954 {
4955 case WINED3DTSS_TCI_PASSTHRU:
4956 if (settings->texcoords & (1 << i))
4957 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
4958 i, i, i);
4959 break;
4960
4961 case WINED3DTSS_TCI_CAMERASPACENORMAL:
4962 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
4963 break;
4964
4965 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
4966 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
4967 break;
4968
4969 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
4970 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
4971 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
4972 break;
4973
4974 case WINED3DTSS_TCI_SPHEREMAP:
4975 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
4976 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
4977 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
4978 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
4979 break;
4980
4981 default:
4982 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
4983 break;
4984 }
4985 }
4986
4987 switch (settings->fog_mode)
4988 {
4989 case WINED3D_FFP_VS_FOG_OFF:
4990 break;
4991
4992 case WINED3D_FFP_VS_FOG_FOGCOORD:
4993 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
4994 break;
4995
4996 case WINED3D_FFP_VS_FOG_RANGE:
4997 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
4998 break;
4999
5000 case WINED3D_FFP_VS_FOG_DEPTH:
5001 if (settings->ortho_fog)
5002 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5003 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5004 else
5005 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5006 break;
5007
5008 default:
5009 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5010 break;
5011 }
5012
5013 if (settings->point_size)
5014 {
5015 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5016 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5017 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5018 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5019 }
5020
5021 shader_addline(buffer, "}\n");
5022
5023 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
5024 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5025
5026 return shader_obj;
5027 }
5028
5029 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5030 DWORD argnum, unsigned int stage, DWORD arg)
5031 {
5032 const char *ret;
5033
5034 if (arg == ARG_UNUSED)
5035 return "<unused arg>";
5036
5037 switch (arg & WINED3DTA_SELECTMASK)
5038 {
5039 case WINED3DTA_DIFFUSE:
5040 ret = "gl_Color";
5041 break;
5042
5043 case WINED3DTA_CURRENT:
5044 if (!stage)
5045 ret = "gl_Color";
5046 else
5047 ret = "ret";
5048 break;
5049
5050 case WINED3DTA_TEXTURE:
5051 switch (stage)
5052 {
5053 case 0: ret = "tex0"; break;
5054 case 1: ret = "tex1"; break;
5055 case 2: ret = "tex2"; break;
5056 case 3: ret = "tex3"; break;
5057 case 4: ret = "tex4"; break;
5058 case 5: ret = "tex5"; break;
5059 case 6: ret = "tex6"; break;
5060 case 7: ret = "tex7"; break;
5061 default:
5062 ret = "<invalid texture>";
5063 break;
5064 }
5065 break;
5066
5067 case WINED3DTA_TFACTOR:
5068 ret = "tex_factor";
5069 break;
5070
5071 case WINED3DTA_SPECULAR:
5072 ret = "gl_SecondaryColor";
5073 break;
5074
5075 case WINED3DTA_TEMP:
5076 ret = "temp_reg";
5077 break;
5078
5079 case WINED3DTA_CONSTANT:
5080 FIXME("Per-stage constants not implemented.\n");
5081 switch (stage)
5082 {
5083 case 0: ret = "const0"; break;
5084 case 1: ret = "const1"; break;
5085 case 2: ret = "const2"; break;
5086 case 3: ret = "const3"; break;
5087 case 4: ret = "const4"; break;
5088 case 5: ret = "const5"; break;
5089 case 6: ret = "const6"; break;
5090 case 7: ret = "const7"; break;
5091 default:
5092 ret = "<invalid constant>";
5093 break;
5094 }
5095 break;
5096
5097 default:
5098 return "<unhandled arg>";
5099 }
5100
5101 if (arg & WINED3DTA_COMPLEMENT)
5102 {
5103 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5104 if (argnum == 0)
5105 ret = "arg0";
5106 else if (argnum == 1)
5107 ret = "arg1";
5108 else if (argnum == 2)
5109 ret = "arg2";
5110 }
5111
5112 if (arg & WINED3DTA_ALPHAREPLICATE)
5113 {
5114 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5115 if (argnum == 0)
5116 ret = "arg0";
5117 else if (argnum == 1)
5118 ret = "arg1";
5119 else if (argnum == 2)
5120 ret = "arg2";
5121 }
5122
5123 return ret;
5124 }
5125
5126 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5127 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5128 {
5129 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5130
5131 if (color && alpha)
5132 dstmask = "";
5133 else if (color)
5134 dstmask = ".xyz";
5135 else
5136 dstmask = ".w";
5137
5138 if (dst == tempreg)
5139 dstreg = "temp_reg";
5140 else
5141 dstreg = "ret";
5142
5143 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5144 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5145 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5146
5147 switch (op)
5148 {
5149 case WINED3D_TOP_DISABLE:
5150 if (!stage)
5151 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5152 break;
5153
5154 case WINED3D_TOP_SELECT_ARG1:
5155 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5156 break;
5157
5158 case WINED3D_TOP_SELECT_ARG2:
5159 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5160 break;
5161
5162 case WINED3D_TOP_MODULATE:
5163 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5164 break;
5165
5166 case WINED3D_TOP_MODULATE_4X:
5167 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5168 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5169 break;
5170
5171 case WINED3D_TOP_MODULATE_2X:
5172 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5173 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5174 break;
5175
5176 case WINED3D_TOP_ADD:
5177 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5178 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5179 break;
5180
5181 case WINED3D_TOP_ADD_SIGNED:
5182 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5183 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5184 break;
5185
5186 case WINED3D_TOP_ADD_SIGNED_2X:
5187 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5188 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5189 break;
5190
5191 case WINED3D_TOP_SUBTRACT:
5192 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5193 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5194 break;
5195
5196 case WINED3D_TOP_ADD_SMOOTH:
5197 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5198 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5199 break;
5200
5201 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5202 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5203 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5204 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5205 break;
5206
5207 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5208 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5209 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5210 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5211 break;
5212
5213 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5214 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5215 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5216 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5217 break;
5218
5219 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5220 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5221 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5222 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5223 break;
5224
5225 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5226 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5227 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5228 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5229 break;
5230
5231 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5232 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5233 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5234 break;
5235
5236 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5237 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5238 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5239 break;
5240
5241 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5242 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5243 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5244 break;
5245 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5246 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5247 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5248 break;
5249
5250 case WINED3D_TOP_BUMPENVMAP:
5251 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5252 /* These are handled in the first pass, nothing to do. */
5253 break;
5254
5255 case WINED3D_TOP_DOTPRODUCT3:
5256 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5257 dstreg, dstmask, arg1, arg2, dstmask);
5258 break;
5259
5260 case WINED3D_TOP_MULTIPLY_ADD:
5261 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5262 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5263 break;
5264
5265 case WINED3D_TOP_LERP:
5266 /* MSDN isn't quite right here. */
5267 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5268 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5269 break;
5270
5271 default:
5272 FIXME("Unhandled operation %#x.\n", op);
5273 break;
5274 }
5275 }
5276
5277 /* Context activation is done by the caller. */
5278 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5279 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5280 {
5281 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5282 BYTE lum_map = 0, bump_map = 0, tex_map = 0;
5283 const char *final_combiner_src = "ret";
5284 UINT lowest_disabled_stage;
5285 GLhandleARB shader_obj;
5286 DWORD arg0, arg1, arg2;
5287 unsigned int stage;
5288
5289 shader_buffer_clear(buffer);
5290
5291 /* Find out which textures are read */
5292 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5293 {
5294 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5295 break;
5296
5297 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5298 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5299 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5300
5301 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5302 tex_map |= 1 << stage;
5303 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5304 tfactor_used = TRUE;
5305 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5306 tempreg_used = TRUE;
5307 if (settings->op[stage].dst == tempreg)
5308 tempreg_used = TRUE;
5309
5310 switch (settings->op[stage].cop)
5311 {
5312 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5313 lum_map |= 1 << stage;
5314 /* fall through */
5315 case WINED3D_TOP_BUMPENVMAP:
5316 bump_map |= 1 << stage;
5317 /* fall through */
5318 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5319 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5320 tex_map |= 1 << stage;
5321 break;
5322
5323 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5324 tfactor_used = TRUE;
5325 break;
5326
5327 default:
5328 break;
5329 }
5330
5331 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5332 continue;
5333
5334 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5335 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5336 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5337
5338 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5339 tex_map |= 1 << stage;
5340 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5341 tfactor_used = TRUE;
5342 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5343 tempreg_used = TRUE;
5344 }
5345 lowest_disabled_stage = stage;
5346
5347 shader_addline(buffer, "#version 120\n");
5348
5349 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5350 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5351
5352 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5353 shader_addline(buffer, "vec4 ret;\n");
5354 if (tempreg_used || settings->sRGB_write)
5355 shader_addline(buffer, "vec4 temp_reg;\n");
5356 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5357
5358 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5359 {
5360 if (!(tex_map & (1 << stage)))
5361 continue;
5362
5363 switch (settings->op[stage].tex_type)
5364 {
5365 case tex_1d:
5366 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5367 break;
5368 case tex_2d:
5369 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5370 break;
5371 case tex_3d:
5372 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5373 break;
5374 case tex_cube:
5375 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5376 break;
5377 case tex_rect:
5378 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5379 break;
5380 default:
5381 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5382 break;
5383 }
5384
5385 shader_addline(buffer, "vec4 tex%u;\n", stage);
5386
5387 if (!(bump_map & (1 << stage)))
5388 continue;
5389 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5390
5391 if (!(lum_map & (1 << stage)))
5392 continue;
5393 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5394 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5395 }
5396 if (tfactor_used)
5397 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5398 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5399
5400 if (settings->sRGB_write)
5401 {
5402 shader_addline(buffer, "const vec4 srgb_const0 = ");
5403 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5404 shader_addline(buffer, ";\n");
5405 shader_addline(buffer, "const vec4 srgb_const1 = ");
5406 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5407 shader_addline(buffer, ";\n");
5408 }
5409
5410 shader_addline(buffer, "void main()\n{\n");
5411
5412 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5413 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5414
5415 /* Generate texture sampling instructions) */
5416 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5417 {
5418 const char *texture_function, *coord_mask;
5419 char tex_reg_name[8];
5420 BOOL proj;
5421
5422 if (!(tex_map & (1 << stage)))
5423 continue;
5424
5425 if (settings->op[stage].projected == proj_none)
5426 {
5427 proj = FALSE;
5428 }
5429 else if (settings->op[stage].projected == proj_count4
5430 || settings->op[stage].projected == proj_count3)
5431 {
5432 proj = TRUE;
5433 }
5434 else
5435 {
5436 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5437 proj = TRUE;
5438 }
5439
5440 switch (settings->op[stage].tex_type)
5441 {
5442 case tex_1d:
5443 if (proj)
5444 {
5445 texture_function = "texture1DProj";
5446 coord_mask = "xw";
5447 }
5448 else
5449 {
5450 texture_function = "texture1D";
5451 coord_mask = "x";
5452 }
5453 break;
5454 case tex_2d:
5455 if (proj)
5456 {
5457 texture_function = "texture2DProj";
5458 coord_mask = "xyw";
5459 }
5460 else
5461 {
5462 texture_function = "texture2D";
5463 coord_mask = "xy";
5464 }
5465 break;
5466 case tex_3d:
5467 if (proj)
5468 {
5469 texture_function = "texture3DProj";
5470 coord_mask = "xyzw";
5471 }
5472 else
5473 {
5474 texture_function = "texture3D";
5475 coord_mask = "xyz";
5476 }
5477 break;
5478 case tex_cube:
5479 texture_function = "textureCube";
5480 coord_mask = "xyz";
5481 break;
5482 case tex_rect:
5483 if (proj)
5484 {
5485 texture_function = "texture2DRectProj";
5486 coord_mask = "xyw";
5487 }
5488 else
5489 {
5490 texture_function = "texture2DRect";
5491 coord_mask = "xy";
5492 }
5493 break;
5494 default:
5495 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5496 texture_function = "";
5497 coord_mask = "xyzw";
5498 break;
5499 }
5500
5501 if (stage > 0
5502 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5503 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5504 {
5505 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5506
5507 /* With projective textures, texbem only divides the static
5508 * texture coord, not the displacement, so multiply the
5509 * displacement with the dividing parameter before passing it to
5510 * TXP. */
5511 if (settings->op[stage].projected != proj_none)
5512 {
5513 if (settings->op[stage].projected == proj_count4)
5514 {
5515 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5516 stage, stage);
5517 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5518 }
5519 else
5520 {
5521 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5522 stage, stage);
5523 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5524 }
5525 }
5526 else
5527 {
5528 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5529 }
5530
5531 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5532 stage, texture_function, stage, coord_mask);
5533
5534 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5535 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5536 stage, stage - 1, stage - 1, stage - 1);
5537 }
5538 else if (settings->op[stage].projected == proj_count3)
5539 {
5540 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5541 stage, texture_function, stage, stage);
5542 }
5543 else
5544 {
5545 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5546 stage, texture_function, stage, stage, coord_mask);
5547 }
5548
5549 sprintf(tex_reg_name, "tex%u", stage);
5550 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5551 settings->op[stage].color_fixup);
5552 }
5553
5554 /* Generate the main shader */
5555 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5556 {
5557 BOOL op_equal;
5558
5559 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5560 {
5561 if (!stage)
5562 final_combiner_src = "gl_Color";
5563 break;
5564 }
5565
5566 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5567 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5568 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5569 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5570 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5571 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5572 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5573 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5574 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5575 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5576 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5577 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5578 else
5579 op_equal = settings->op[stage].aop == settings->op[stage].cop
5580 && settings->op[stage].carg0 == settings->op[stage].aarg0
5581 && settings->op[stage].carg1 == settings->op[stage].aarg1
5582 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5583
5584 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5585 {
5586 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5587 settings->op[stage].cop, settings->op[stage].carg0,
5588 settings->op[stage].carg1, settings->op[stage].carg2);
5589 if (!stage)
5590 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5591 }
5592 else if (op_equal)
5593 {
5594 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5595 settings->op[stage].cop, settings->op[stage].carg0,
5596 settings->op[stage].carg1, settings->op[stage].carg2);
5597 }
5598 else
5599 {
5600 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5601 settings->op[stage].cop, settings->op[stage].carg0,
5602 settings->op[stage].carg1, settings->op[stage].carg2);
5603 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5604 settings->op[stage].aop, settings->op[stage].aarg0,
5605 settings->op[stage].aarg1, settings->op[stage].aarg2);
5606 }
5607 }
5608
5609 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5610
5611 if (settings->sRGB_write)
5612 shader_glsl_generate_srgb_write_correction(buffer);
5613
5614 shader_glsl_generate_fog_code(buffer, settings->fog);
5615
5616 shader_addline(buffer, "}\n");
5617
5618 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5619 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5620 return shader_obj;
5621 }
5622
5623 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5624 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5625 {
5626 struct glsl_ffp_vertex_shader *shader;
5627 const struct wine_rb_entry *entry;
5628
5629 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5630 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5631
5632 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5633 return NULL;
5634
5635 shader->desc.settings = *settings;
5636 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5637 list_init(&shader->linked_programs);
5638 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5639 ERR("Failed to insert ffp vertex shader.\n");
5640
5641 return shader;
5642 }
5643
5644 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5645 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5646 {
5647 struct glsl_ffp_fragment_shader *glsl_desc;
5648 const struct ffp_frag_desc *desc;
5649
5650 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5651 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5652
5653 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5654 return NULL;
5655
5656 glsl_desc->entry.settings = *args;
5657 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5658 list_init(&glsl_desc->linked_programs);
5659 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5660
5661 return glsl_desc;
5662 }
5663
5664
5665 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5666 GLhandleARB program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5667 {
5668 unsigned int i;
5669 char name[32];
5670
5671 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5672 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
5673 for (i = 0; i < vs_c_count; ++i)
5674 {
5675 snprintf(name, sizeof(name), "vs_c[%u]", i);
5676 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5677 }
5678 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5679 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLhandleARB));
5680
5681 for (i = 0; i < MAX_CONST_I; ++i)
5682 {
5683 snprintf(name, sizeof(name), "vs_i[%u]", i);
5684 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5685 }
5686
5687 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "posFixup"));
5688 }
5689
5690 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5691 GLhandleARB program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5692 {
5693 unsigned int i;
5694 char name[32];
5695
5696 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5697 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
5698 for (i = 0; i < ps_c_count; ++i)
5699 {
5700 snprintf(name, sizeof(name), "ps_c[%u]", i);
5701 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5702 }
5703 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5704 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLhandleARB));
5705
5706 for (i = 0; i < MAX_CONST_I; ++i)
5707 {
5708 snprintf(name, sizeof(name), "ps_i[%u]", i);
5709 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5710 }
5711
5712 for (i = 0; i < MAX_TEXTURES; ++i)
5713 {
5714 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5715 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5716 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5717 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5718 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5719 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5720 }
5721
5722 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "tex_factor"));
5723 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "specular_enable"));
5724 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ps_samplerNP2Fixup"));
5725 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ycorrection"));
5726 }
5727
5728 /* Context activation is done by the caller. */
5729 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5730 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5731 {
5732 const struct wined3d_gl_info *gl_info = context->gl_info;
5733 const struct ps_np2fixup_info *np2fixup_info = NULL;
5734 struct glsl_shader_prog_link *entry = NULL;
5735 struct wined3d_shader *vshader = NULL;
5736 struct wined3d_shader *gshader = NULL;
5737 struct wined3d_shader *pshader = NULL;
5738 GLhandleARB programId = 0;
5739 GLhandleARB reorder_shader_id = 0;
5740 unsigned int i;
5741 GLhandleARB vs_id = 0;
5742 GLhandleARB gs_id = 0;
5743 GLhandleARB ps_id = 0;
5744 struct list *ps_list, *vs_list;
5745
5746 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5747 {
5748 vs_id = ctx_data->glsl_program->vs.id;
5749 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5750
5751 if (use_vs(state))
5752 {
5753 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5754 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5755
5756 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5757 && ctx_data->glsl_program->gs.id)
5758 gs_id = ctx_data->glsl_program->gs.id;
5759 else if (gshader)
5760 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5761 }
5762 }
5763 else if (use_vs(state))
5764 {
5765 struct vs_compile_args vs_compile_args;
5766 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5767
5768 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
5769 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5770 vs_list = &vshader->linked_programs;
5771
5772 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
5773 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5774 }
5775 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5776 {
5777 struct glsl_ffp_vertex_shader *ffp_shader;
5778 struct wined3d_ffp_vs_settings settings;
5779
5780 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
5781 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5782 vs_id = ffp_shader->id;
5783 vs_list = &ffp_shader->linked_programs;
5784 }
5785
5786 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
5787 {
5788 ps_id = ctx_data->glsl_program->ps.id;
5789 ps_list = &ctx_data->glsl_program->ps.shader_entry;
5790
5791 if (use_ps(state))
5792 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5793 }
5794 else if (use_ps(state))
5795 {
5796 struct ps_compile_args ps_compile_args;
5797 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5798 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
5799 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5800 pshader, &ps_compile_args, &np2fixup_info);
5801 ps_list = &pshader->linked_programs;
5802 }
5803 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5804 {
5805 struct glsl_ffp_fragment_shader *ffp_shader;
5806 struct ffp_frag_settings settings;
5807
5808 gen_ffp_frag_op(context, state, &settings, FALSE);
5809 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5810 ps_id = ffp_shader->id;
5811 ps_list = &ffp_shader->linked_programs;
5812 }
5813
5814 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5815 {
5816 ctx_data->glsl_program = entry;
5817 return;
5818 }
5819
5820 /* If we get to this point, then no matching program exists, so we create one */
5821 programId = GL_EXTCALL(glCreateProgramObjectARB());
5822 TRACE("Created new GLSL shader program %u\n", programId);
5823
5824 /* Create the entry */
5825 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5826 entry->programId = programId;
5827 entry->vs.id = vs_id;
5828 entry->gs.id = gs_id;
5829 entry->ps.id = ps_id;
5830 entry->constant_version = 0;
5831 entry->ps.np2_fixup_info = np2fixup_info;
5832 /* Add the hash table entry */
5833 add_glsl_program_entry(priv, entry);
5834
5835 /* Set the current program */
5836 ctx_data->glsl_program = entry;
5837
5838 /* Attach GLSL vshader */
5839 if (vs_id)
5840 {
5841 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, programId);
5842 GL_EXTCALL(glAttachObjectARB(programId, vs_id));
5843 checkGLcall("glAttachObjectARB");
5844
5845 list_add_head(vs_list, &entry->vs.shader_entry);
5846 }
5847
5848 if (vshader)
5849 {
5850 WORD map = vshader->reg_maps.input_registers;
5851 char tmp_name[10];
5852
5853 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5854 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
5855 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
5856 checkGLcall("glAttachObjectARB");
5857 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5858 * is destroyed
5859 */
5860 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
5861
5862 /* Bind vertex attributes to a corresponding index number to match
5863 * the same index numbers as ARB_vertex_programs (makes loading
5864 * vertex attributes simpler). With this method, we can use the
5865 * exact same code to load the attributes later for both ARB and
5866 * GLSL shaders.
5867 *
5868 * We have to do this here because we need to know the Program ID
5869 * in order to make the bindings work, and it has to be done prior
5870 * to linking the GLSL program. */
5871 for (i = 0; map; map >>= 1, ++i)
5872 {
5873 if (!(map & 1)) continue;
5874
5875 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5876 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
5877 }
5878 checkGLcall("glBindAttribLocationARB");
5879 }
5880
5881 if (gshader)
5882 {
5883 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, programId);
5884 GL_EXTCALL(glAttachObjectARB(programId, gs_id));
5885 checkGLcall("glAttachObjectARB");
5886
5887 TRACE("input type %s, output type %s, vertices out %u.\n",
5888 debug_d3dprimitivetype(gshader->u.gs.input_type),
5889 debug_d3dprimitivetype(gshader->u.gs.output_type),
5890 gshader->u.gs.vertices_out);
5891 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_INPUT_TYPE_ARB,
5892 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5893 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5894 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5895 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_VERTICES_OUT_ARB,
5896 gshader->u.gs.vertices_out));
5897 checkGLcall("glProgramParameteriARB");
5898
5899 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5900 }
5901
5902 /* Attach GLSL pshader */
5903 if (ps_id)
5904 {
5905 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, programId);
5906 GL_EXTCALL(glAttachObjectARB(programId, ps_id));
5907 checkGLcall("glAttachObjectARB");
5908
5909 list_add_head(ps_list, &entry->ps.shader_entry);
5910 }
5911
5912 /* Link the program */
5913 TRACE("Linking GLSL shader program %u\n", programId);
5914 GL_EXTCALL(glLinkProgramARB(programId));
5915 shader_glsl_validate_link(gl_info, programId);
5916
5917 shader_glsl_init_vs_uniform_locations(gl_info, programId, &entry->vs,
5918 vshader ? vshader->limits.constant_float : 0);
5919 shader_glsl_init_ps_uniform_locations(gl_info, programId, &entry->ps,
5920 pshader ? pshader->limits.constant_float : 0);
5921 checkGLcall("Find glsl program uniform locations");
5922
5923 if (pshader && pshader->reg_maps.shader_version.major >= 3
5924 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
5925 {
5926 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
5927 entry->vs.vertex_color_clamp = GL_FALSE;
5928 }
5929 else
5930 {
5931 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
5932 }
5933
5934 /* Set the shader to allow uniform loading on it */
5935 GL_EXTCALL(glUseProgramObjectARB(programId));
5936 checkGLcall("glUseProgramObjectARB(programId)");
5937
5938 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
5939 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
5940 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
5941 * vertex shader with fixed function pixel processing is used we make sure that the card
5942 * supports enough samplers to allow the max number of vertex samplers with all possible
5943 * fixed function fragment processing setups. So once the program is linked these samplers
5944 * won't change.
5945 */
5946 shader_glsl_load_vsamplers(gl_info, context->tex_unit_map, programId);
5947 shader_glsl_load_psamplers(gl_info, context->tex_unit_map, programId);
5948
5949 entry->constant_update_mask = 0;
5950 if (vshader)
5951 {
5952 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
5953 if (vshader->reg_maps.integer_constants)
5954 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
5955 if (vshader->reg_maps.boolean_constants)
5956 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
5957 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
5958 }
5959
5960 if (ps_id)
5961 {
5962 if (pshader)
5963 {
5964 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
5965 if (pshader->reg_maps.integer_constants)
5966 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
5967 if (pshader->reg_maps.boolean_constants)
5968 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
5969 if (entry->ps.ycorrection_location != -1)
5970 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
5971 }
5972 else
5973 {
5974 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
5975 }
5976
5977 for (i = 0; i < MAX_TEXTURES; ++i)
5978 {
5979 if (entry->ps.bumpenv_mat_location[i] != -1)
5980 {
5981 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
5982 break;
5983 }
5984 }
5985
5986 if (entry->ps.np2_fixup_location != -1)
5987 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
5988 }
5989 }
5990
5991 /* Context activation is done by the caller. */
5992 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
5993 {
5994 GLhandleARB program_id;
5995 GLhandleARB vshader_id, pshader_id;
5996 const char *blt_pshader;
5997
5998 static const char *blt_vshader =
5999 "#version 120\n"
6000 "void main(void)\n"
6001 "{\n"
6002 " gl_Position = gl_Vertex;\n"
6003 " gl_FrontColor = vec4(1.0);\n"
6004 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6005 "}\n";
6006
6007 static const char * const blt_pshaders_full[tex_type_count] =
6008 {
6009 /* tex_1d */
6010 NULL,
6011 /* tex_2d */
6012 "#version 120\n"
6013 "uniform sampler2D sampler;\n"
6014 "void main(void)\n"
6015 "{\n"
6016 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6017 "}\n",
6018 /* tex_3d */
6019 NULL,
6020 /* tex_cube */
6021 "#version 120\n"
6022 "uniform samplerCube sampler;\n"
6023 "void main(void)\n"
6024 "{\n"
6025 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6026 "}\n",
6027 /* tex_rect */
6028 "#version 120\n"
6029 "#extension GL_ARB_texture_rectangle : enable\n"
6030 "uniform sampler2DRect sampler;\n"
6031 "void main(void)\n"
6032 "{\n"
6033 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6034 "}\n",
6035 };
6036
6037 static const char * const blt_pshaders_masked[tex_type_count] =
6038 {
6039 /* tex_1d */
6040 NULL,
6041 /* tex_2d */
6042 "#version 120\n"
6043 "uniform sampler2D sampler;\n"
6044 "uniform vec4 mask;\n"
6045 "void main(void)\n"
6046 "{\n"
6047 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6048 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6049 "}\n",
6050 /* tex_3d */
6051 NULL,
6052 /* tex_cube */
6053 "#version 120\n"
6054 "uniform samplerCube sampler;\n"
6055 "uniform vec4 mask;\n"
6056 "void main(void)\n"
6057 "{\n"
6058 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6059 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6060 "}\n",
6061 /* tex_rect */
6062 "#version 120\n"
6063 "#extension GL_ARB_texture_rectangle : enable\n"
6064 "uniform sampler2DRect sampler;\n"
6065 "uniform vec4 mask;\n"
6066 "void main(void)\n"
6067 "{\n"
6068 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6069 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6070 "}\n",
6071 };
6072
6073 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6074 if (!blt_pshader)
6075 {
6076 FIXME("tex_type %#x not supported\n", tex_type);
6077 return 0;
6078 }
6079
6080 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
6081 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6082
6083 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
6084 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6085
6086 program_id = GL_EXTCALL(glCreateProgramObjectARB());
6087 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
6088 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
6089 GL_EXTCALL(glLinkProgramARB(program_id));
6090
6091 shader_glsl_validate_link(gl_info, program_id);
6092
6093 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6094 * is destroyed
6095 */
6096 GL_EXTCALL(glDeleteObjectARB(vshader_id));
6097 GL_EXTCALL(glDeleteObjectARB(pshader_id));
6098 return program_id;
6099 }
6100
6101 /* Context activation is done by the caller. */
6102 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6103 const struct wined3d_state *state)
6104 {
6105 struct glsl_context_data *ctx_data = context->shader_backend_data;
6106 const struct wined3d_gl_info *gl_info = context->gl_info;
6107 struct shader_glsl_priv *priv = shader_priv;
6108 GLhandleARB program_id = 0, prev_id = 0;
6109 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6110
6111 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6112 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6113
6114 if (ctx_data->glsl_program)
6115 {
6116 prev_id = ctx_data->glsl_program->programId;
6117 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6118 }
6119 else
6120 {
6121 prev_id = 0;
6122 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6123 }
6124
6125 set_glsl_shader_program(context, state, priv, ctx_data);
6126
6127 if (ctx_data->glsl_program)
6128 {
6129 program_id = ctx_data->glsl_program->programId;
6130 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6131 }
6132 else
6133 {
6134 program_id = 0;
6135 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6136 }
6137
6138 if (old_vertex_color_clamp != current_vertex_color_clamp)
6139 {
6140 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6141 {
6142 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6143 checkGLcall("glClampColorARB");
6144 }
6145 else
6146 {
6147 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6148 }
6149 }
6150
6151 TRACE("Using GLSL program %u.\n", program_id);
6152
6153 if (prev_id != program_id)
6154 {
6155 GL_EXTCALL(glUseProgramObjectARB(program_id));
6156 checkGLcall("glUseProgramObjectARB");
6157
6158 if (program_id)
6159 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6160 }
6161 }
6162
6163 /* "context" is not necessarily the currently active context. */
6164 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6165 {
6166 struct glsl_context_data *ctx_data = context->shader_backend_data;
6167
6168 ctx_data->glsl_program = NULL;
6169 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6170 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6171 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6172 }
6173
6174 /* Context activation is done by the caller. */
6175 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6176 {
6177 const struct wined3d_gl_info *gl_info = context->gl_info;
6178 struct shader_glsl_priv *priv = shader_priv;
6179
6180 shader_glsl_invalidate_current_program(context);
6181 GL_EXTCALL(glUseProgramObjectARB(0));
6182 checkGLcall("glUseProgramObjectARB");
6183
6184 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6185 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6186
6187 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6188 {
6189 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6190 checkGLcall("glClampColorARB");
6191 }
6192 }
6193
6194 /* Context activation is done by the caller. */
6195 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6196 enum tex_types tex_type, const SIZE *ds_mask_size)
6197 {
6198 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6199 struct shader_glsl_priv *priv = shader_priv;
6200 GLhandleARB *blt_program;
6201 GLint loc;
6202
6203 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6204 if (!*blt_program)
6205 {
6206 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6207 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
6208 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6209 GL_EXTCALL(glUniform1iARB(loc, 0));
6210 }
6211 else
6212 {
6213 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6214 }
6215
6216 if (masked)
6217 {
6218 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
6219 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6220 }
6221 }
6222
6223 /* Context activation is done by the caller. */
6224 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6225 {
6226 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6227 GLhandleARB program_id;
6228
6229 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->programId : 0;
6230 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6231
6232 GL_EXTCALL(glUseProgramObjectARB(program_id));
6233 checkGLcall("glUseProgramObjectARB");
6234 }
6235
6236 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6237 const struct glsl_shader_prog_link *program)
6238 {
6239 const struct glsl_context_data *ctx_data;
6240 struct wined3d_context *context;
6241 unsigned int i;
6242
6243 for (i = 0; i < device->context_count; ++i)
6244 {
6245 context = device->contexts[i];
6246 ctx_data = context->shader_backend_data;
6247
6248 if (ctx_data->glsl_program == program)
6249 shader_glsl_invalidate_current_program(context);
6250 }
6251 }
6252
6253 static void shader_glsl_destroy(struct wined3d_shader *shader)
6254 {
6255 struct glsl_shader_private *shader_data = shader->backend_data;
6256 struct wined3d_device *device = shader->device;
6257 struct shader_glsl_priv *priv = device->shader_priv;
6258 const struct wined3d_gl_info *gl_info;
6259 const struct list *linked_programs;
6260 struct wined3d_context *context;
6261
6262 if (!shader_data || !shader_data->num_gl_shaders)
6263 {
6264 HeapFree(GetProcessHeap(), 0, shader_data);
6265 shader->backend_data = NULL;
6266 return;
6267 }
6268
6269 context = context_acquire(device, NULL);
6270 gl_info = context->gl_info;
6271
6272 TRACE("Deleting linked programs.\n");
6273 linked_programs = &shader->linked_programs;
6274 if (linked_programs->next)
6275 {
6276 struct glsl_shader_prog_link *entry, *entry2;
6277 UINT i;
6278
6279 switch (shader->reg_maps.shader_version.type)
6280 {
6281 case WINED3D_SHADER_TYPE_PIXEL:
6282 {
6283 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6284
6285 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6286 {
6287 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
6288 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6289 checkGLcall("glDeleteObjectARB");
6290 }
6291 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6292
6293 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6294 struct glsl_shader_prog_link, ps.shader_entry)
6295 {
6296 shader_glsl_invalidate_contexts_program(device, entry);
6297 delete_glsl_program_entry(priv, gl_info, entry);
6298 }
6299
6300 break;
6301 }
6302
6303 case WINED3D_SHADER_TYPE_VERTEX:
6304 {
6305 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6306
6307 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6308 {
6309 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
6310 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6311 checkGLcall("glDeleteObjectARB");
6312 }
6313 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6314
6315 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6316 struct glsl_shader_prog_link, vs.shader_entry)
6317 {
6318 shader_glsl_invalidate_contexts_program(device, entry);
6319 delete_glsl_program_entry(priv, gl_info, entry);
6320 }
6321
6322 break;
6323 }
6324
6325 case WINED3D_SHADER_TYPE_GEOMETRY:
6326 {
6327 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6328
6329 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6330 {
6331 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6332 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].id));
6333 checkGLcall("glDeleteObjectARB");
6334 }
6335 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6336
6337 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6338 struct glsl_shader_prog_link, gs.shader_entry)
6339 {
6340 shader_glsl_invalidate_contexts_program(device, entry);
6341 delete_glsl_program_entry(priv, gl_info, entry);
6342 }
6343
6344 break;
6345 }
6346
6347 default:
6348 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6349 break;
6350 }
6351 }
6352
6353 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6354 shader->backend_data = NULL;
6355
6356 context_release(context);
6357 }
6358
6359 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6360 {
6361 const struct glsl_program_key *k = key;
6362 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6363 const struct glsl_shader_prog_link, program_lookup_entry);
6364
6365 if (k->vs_id > prog->vs.id) return 1;
6366 else if (k->vs_id < prog->vs.id) return -1;
6367
6368 if (k->gs_id > prog->gs.id) return 1;
6369 else if (k->gs_id < prog->gs.id) return -1;
6370
6371 if (k->ps_id > prog->ps.id) return 1;
6372 else if (k->ps_id < prog->ps.id) return -1;
6373
6374 return 0;
6375 }
6376
6377 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6378 {
6379 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6380 + constant_count * sizeof(*heap->contained)
6381 + constant_count * sizeof(*heap->positions);
6382 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6383
6384 if (!mem)
6385 {
6386 ERR("Failed to allocate memory\n");
6387 return FALSE;
6388 }
6389
6390 heap->entries = mem;
6391 heap->entries[1].version = 0;
6392 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6393 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6394 heap->positions = (unsigned int *)(heap->contained + constant_count);
6395 heap->size = 1;
6396
6397 return TRUE;
6398 }
6399
6400 static void constant_heap_free(struct constant_heap *heap)
6401 {
6402 HeapFree(GetProcessHeap(), 0, heap->entries);
6403 }
6404
6405 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6406 {
6407 wined3d_rb_alloc,
6408 wined3d_rb_realloc,
6409 wined3d_rb_free,
6410 glsl_program_key_compare,
6411 };
6412
6413 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6414 const struct fragment_pipeline *fragment_pipe)
6415 {
6416 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6417 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6418 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6419 gl_info->limits.glsl_ps_float_constants)) + 1;
6420 struct fragment_caps fragment_caps;
6421 void *vertex_priv, *fragment_priv;
6422
6423 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6424 {
6425 ERR("Failed to initialize vertex pipe.\n");
6426 HeapFree(GetProcessHeap(), 0, priv);
6427 return E_FAIL;
6428 }
6429
6430 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6431 {
6432 ERR("Failed to initialize fragment pipe.\n");
6433 vertex_pipe->vp_free(device);
6434 HeapFree(GetProcessHeap(), 0, priv);
6435 return E_FAIL;
6436 }
6437
6438 if (!shader_buffer_init(&priv->shader_buffer))
6439 {
6440 ERR("Failed to initialize shader buffer.\n");
6441 goto fail;
6442 }
6443
6444 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6445 if (!priv->stack)
6446 {
6447 ERR("Failed to allocate memory.\n");
6448 goto fail;
6449 }
6450
6451 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6452 {
6453 ERR("Failed to initialize vertex shader constant heap\n");
6454 goto fail;
6455 }
6456
6457 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6458 {
6459 ERR("Failed to initialize pixel shader constant heap\n");
6460 goto fail;
6461 }
6462
6463 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6464 {
6465 ERR("Failed to initialize rbtree.\n");
6466 goto fail;
6467 }
6468
6469 priv->next_constant_version = 1;
6470 priv->vertex_pipe = vertex_pipe;
6471 priv->fragment_pipe = fragment_pipe;
6472 fragment_pipe->get_caps(gl_info, &fragment_caps);
6473 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6474
6475 device->vertex_priv = vertex_priv;
6476 device->fragment_priv = fragment_priv;
6477 device->shader_priv = priv;
6478
6479 return WINED3D_OK;
6480
6481 fail:
6482 constant_heap_free(&priv->pconst_heap);
6483 constant_heap_free(&priv->vconst_heap);
6484 HeapFree(GetProcessHeap(), 0, priv->stack);
6485 shader_buffer_free(&priv->shader_buffer);
6486 fragment_pipe->free_private(device);
6487 vertex_pipe->vp_free(device);
6488 HeapFree(GetProcessHeap(), 0, priv);
6489 return E_OUTOFMEMORY;
6490 }
6491
6492 /* Context activation is done by the caller. */
6493 static void shader_glsl_free(struct wined3d_device *device)
6494 {
6495 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6496 struct shader_glsl_priv *priv = device->shader_priv;
6497 int i;
6498
6499 for (i = 0; i < tex_type_count; ++i)
6500 {
6501 if (priv->depth_blt_program_full[i])
6502 {
6503 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
6504 }
6505 if (priv->depth_blt_program_masked[i])
6506 {
6507 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
6508 }
6509 }
6510
6511 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6512 constant_heap_free(&priv->pconst_heap);
6513 constant_heap_free(&priv->vconst_heap);
6514 HeapFree(GetProcessHeap(), 0, priv->stack);
6515 shader_buffer_free(&priv->shader_buffer);
6516 priv->fragment_pipe->free_private(device);
6517 priv->vertex_pipe->vp_free(device);
6518
6519 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6520 device->shader_priv = NULL;
6521 }
6522
6523 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6524 {
6525 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6526 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6527 }
6528
6529 static void shader_glsl_free_context_data(struct wined3d_context *context)
6530 {
6531 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6532 }
6533
6534 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6535 {
6536 UINT shader_model;
6537
6538 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6539 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6540 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED])
6541 shader_model = 4;
6542 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6543 * texldd and texldl instructions. */
6544 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6545 shader_model = 3;
6546 else
6547 shader_model = 2;
6548 TRACE("Shader model %u.\n", shader_model);
6549
6550 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6551 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6552 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6553
6554 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6555 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6556
6557 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6558 * Direct3D minimum requirement.
6559 *
6560 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6561 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6562 *
6563 * The problem is that the refrast clamps temporary results in the shader to
6564 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6565 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6566 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6567 * offer a way to query this.
6568 */
6569 caps->ps_1x_max_value = 8.0;
6570
6571 /* Ideally we'd only set caps like sRGB writes here if supported by both
6572 * the shader backend and the fragment pipe, but we can get called before
6573 * shader_glsl_alloc(). */
6574 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6575 | WINED3D_SHADER_CAP_SRGB_WRITE;
6576 }
6577
6578 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6579 {
6580 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6581 {
6582 TRACE("Checking support for fixup:\n");
6583 dump_color_fixup_desc(fixup);
6584 }
6585
6586 /* We support everything except YUV conversions. */
6587 if (!is_complex_fixup(fixup))
6588 {
6589 TRACE("[OK]\n");
6590 return TRUE;
6591 }
6592
6593 TRACE("[FAILED]\n");
6594 return FALSE;
6595 }
6596
6597 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6598 {
6599 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6600 /* WINED3DSIH_ADD */ shader_glsl_binop,
6601 /* WINED3DSIH_AND */ shader_glsl_binop,
6602 /* WINED3DSIH_BEM */ shader_glsl_bem,
6603 /* WINED3DSIH_BREAK */ shader_glsl_break,
6604 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6605 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6606 /* WINED3DSIH_CALL */ shader_glsl_call,
6607 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6608 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6609 /* WINED3DSIH_CND */ shader_glsl_cnd,
6610 /* WINED3DSIH_CRS */ shader_glsl_cross,
6611 /* WINED3DSIH_CUT */ shader_glsl_cut,
6612 /* WINED3DSIH_DCL */ shader_glsl_nop,
6613 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6614 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6615 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6616 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6617 /* WINED3DSIH_DEF */ shader_glsl_nop,
6618 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6619 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6620 /* WINED3DSIH_DIV */ shader_glsl_binop,
6621 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6622 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6623 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6624 /* WINED3DSIH_DST */ shader_glsl_dst,
6625 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6626 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6627 /* WINED3DSIH_ELSE */ shader_glsl_else,
6628 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6629 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6630 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6631 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6632 /* WINED3DSIH_EQ */ shader_glsl_relop,
6633 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6634 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6635 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6636 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6637 /* WINED3DSIH_GE */ shader_glsl_relop,
6638 /* WINED3DSIH_IADD */ shader_glsl_binop,
6639 /* WINED3DSIH_IEQ */ NULL,
6640 /* WINED3DSIH_IF */ shader_glsl_if,
6641 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6642 /* WINED3DSIH_IGE */ shader_glsl_relop,
6643 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6644 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6645 /* WINED3DSIH_LABEL */ shader_glsl_label,
6646 /* WINED3DSIH_LD */ NULL,
6647 /* WINED3DSIH_LIT */ shader_glsl_lit,
6648 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6649 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6650 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6651 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6652 /* WINED3DSIH_LT */ shader_glsl_relop,
6653 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6654 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6655 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6656 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6657 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6658 /* WINED3DSIH_MAD */ shader_glsl_mad,
6659 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6660 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6661 /* WINED3DSIH_MOV */ shader_glsl_mov,
6662 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6663 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6664 /* WINED3DSIH_MUL */ shader_glsl_binop,
6665 /* WINED3DSIH_NOP */ shader_glsl_nop,
6666 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6667 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6668 /* WINED3DSIH_POW */ shader_glsl_pow,
6669 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6670 /* WINED3DSIH_REP */ shader_glsl_rep,
6671 /* WINED3DSIH_RET */ shader_glsl_ret,
6672 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6673 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6674 /* WINED3DSIH_SAMPLE */ NULL,
6675 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6676 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6677 /* WINED3DSIH_SETP */ NULL,
6678 /* WINED3DSIH_SGE */ shader_glsl_compare,
6679 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6680 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6681 /* WINED3DSIH_SLT */ shader_glsl_compare,
6682 /* WINED3DSIH_SQRT */ NULL,
6683 /* WINED3DSIH_SUB */ shader_glsl_binop,
6684 /* WINED3DSIH_TEX */ shader_glsl_tex,
6685 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6686 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6687 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6688 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6689 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6690 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6691 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6692 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6693 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6694 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6695 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6696 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6697 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6698 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6699 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6700 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6701 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6702 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6703 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6704 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6705 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6706 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6707 /* WINED3DSIH_USHR */ shader_glsl_binop,
6708 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6709 /* WINED3DSIH_XOR */ shader_glsl_binop,
6710 };
6711
6712 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6713 SHADER_HANDLER hw_fct;
6714
6715 /* Select handler */
6716 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6717
6718 /* Unhandled opcode */
6719 if (!hw_fct)
6720 {
6721 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6722 return;
6723 }
6724 hw_fct(ins);
6725
6726 shader_glsl_add_instruction_modifiers(ins);
6727 }
6728
6729 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6730 {
6731 struct shader_glsl_priv *priv = shader_priv;
6732
6733 return priv->ffp_proj_control;
6734 }
6735
6736 const struct wined3d_shader_backend_ops glsl_shader_backend =
6737 {
6738 shader_glsl_handle_instruction,
6739 shader_glsl_select,
6740 shader_glsl_disable,
6741 shader_glsl_select_depth_blt,
6742 shader_glsl_deselect_depth_blt,
6743 shader_glsl_update_float_vertex_constants,
6744 shader_glsl_update_float_pixel_constants,
6745 shader_glsl_load_constants,
6746 shader_glsl_destroy,
6747 shader_glsl_alloc,
6748 shader_glsl_free,
6749 shader_glsl_allocate_context_data,
6750 shader_glsl_free_context_data,
6751 shader_glsl_get_caps,
6752 shader_glsl_color_fixup_supported,
6753 shader_glsl_has_ffp_proj_control,
6754 };
6755
6756 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6757 {
6758 if (enable)
6759 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6760 else
6761 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6762 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6763 }
6764
6765 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6766 {
6767 caps->xyzrhw = TRUE;
6768 caps->max_active_lights = gl_info->limits.lights;
6769 caps->max_vertex_blend_matrices = 1;
6770 caps->max_vertex_blend_matrix_index = 0;
6771 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6772 | WINED3DVTXPCAPS_MATERIALSOURCE7
6773 | WINED3DVTXPCAPS_VERTEXFOG
6774 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6775 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6776 | WINED3DVTXPCAPS_LOCALVIEWER
6777 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6778 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6779 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6780 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6781 }
6782
6783 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6784 {
6785 struct shader_glsl_priv *priv;
6786
6787 if (shader_backend == &glsl_shader_backend)
6788 {
6789 priv = shader_priv;
6790
6791 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6792 {
6793 ERR("Failed to initialize rbtree.\n");
6794 return NULL;
6795 }
6796
6797 return priv;
6798 }
6799
6800 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6801
6802 return NULL;
6803 }
6804
6805 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6806 {
6807 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6808 struct glsl_ffp_vertex_shader, desc.entry);
6809 struct glsl_shader_prog_link *program, *program2;
6810 struct glsl_ffp_destroy_ctx *ctx = context;
6811
6812 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6813 struct glsl_shader_prog_link, vs.shader_entry)
6814 {
6815 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6816 }
6817 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6818 HeapFree(GetProcessHeap(), 0, shader);
6819 }
6820
6821 /* Context activation is done by the caller. */
6822 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6823 {
6824 struct shader_glsl_priv *priv = device->vertex_priv;
6825 struct glsl_ffp_destroy_ctx ctx;
6826
6827 ctx.priv = priv;
6828 ctx.gl_info = &device->adapter->gl_info;
6829 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6830 }
6831
6832 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6833 const struct wined3d_state *state, DWORD state_id)
6834 {
6835 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6836 }
6837
6838 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
6839 const struct wined3d_state *state, DWORD state_id)
6840 {
6841 /* Table fog behavior depends on the projection matrix. */
6842 if (state->render_states[WINED3D_RS_FOGENABLE]
6843 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6844 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6845 transform_projection(context, state, state_id);
6846 }
6847
6848 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6849 {
6850 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6851 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6852 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6853 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6854 /* Clip planes */
6855 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6856 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6857 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6858 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6859 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6860 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6861 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6862 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6863 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6864 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6865 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6866 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6867 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6868 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6869 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6870 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6871 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6872 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6873 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6874 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6875 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6876 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6877 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6878 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6879 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6880 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6881 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6882 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6883 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
6884 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
6885 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
6886 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
6887 /* Lights */
6888 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6889 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
6890 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
6891 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
6892 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
6893 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
6894 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
6895 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
6896 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
6897 /* Viewport */
6898 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
6899 /* Transform states */
6900 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
6901 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
6902 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6903 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6904 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6905 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6906 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6907 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6908 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6909 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6910 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
6911 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6912 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6913 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6914 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6915 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6916 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6917 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6918 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6919 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6920 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6921 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6922 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6923 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6924 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6925 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6926 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6927 /* Fog */
6928 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6929 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6930 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6931 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6932 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
6933 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
6934 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6935 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
6936 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6937 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6938 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6939 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6940 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6941 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6942 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6943 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6944 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6945 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
6946 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
6947 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
6948 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
6949 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
6950 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
6951 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6952 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6953 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6954 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
6955 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
6956 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
6957 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6958 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6959 /* Samplers for NP2 texture matrix adjustions. They are not needed if
6960 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
6961 * handler in that case to get the vertex part of sampler() skipped (VTF
6962 * is handled in the misc states). Otherwise, register
6963 * sampler_texmatrix(), which takes care of updating the texture matrix. */
6964 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6965 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6966 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6967 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6968 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6969 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6970 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6971 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6972 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6973 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6974 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6975 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6976 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6977 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6978 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6979 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6980 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6981 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6982 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6983 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6984 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6985 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
6986 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
6987 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
6988 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6989 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
6990 };
6991
6992 /* TODO:
6993 * - This currently depends on GL fixed function functions to set things
6994 * like light parameters. Ideally we'd use regular uniforms for that.
6995 * - In part because of the previous point, much of this is modelled after
6996 * GL fixed function, and has much of the same limitations. For example,
6997 * D3D spot lights are slightly different from GL spot lights.
6998 * - We can now implement drawing transformed vertices using the GLSL pipe,
6999 * instead of using the immediate mode fallback.
7000 * - Similarly, we don't need the fallback for certain combinations of
7001 * material sources anymore.
7002 * - Implement vertex blending and vertex tweening.
7003 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7004 * attribute arrays in load_tex_coords().
7005 * - Per-vertex point sizes. */
7006 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7007 {
7008 glsl_vertex_pipe_vp_enable,
7009 glsl_vertex_pipe_vp_get_caps,
7010 glsl_vertex_pipe_vp_alloc,
7011 glsl_vertex_pipe_vp_free,
7012 glsl_vertex_pipe_vp_states,
7013 };
7014
7015 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7016 {
7017 /* Nothing to do. */
7018 }
7019
7020 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7021 {
7022 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7023 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7024 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP;
7025 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7026 | WINED3DTEXOPCAPS_SELECTARG1
7027 | WINED3DTEXOPCAPS_SELECTARG2
7028 | WINED3DTEXOPCAPS_MODULATE4X
7029 | WINED3DTEXOPCAPS_MODULATE2X
7030 | WINED3DTEXOPCAPS_MODULATE
7031 | WINED3DTEXOPCAPS_ADDSIGNED2X
7032 | WINED3DTEXOPCAPS_ADDSIGNED
7033 | WINED3DTEXOPCAPS_ADD
7034 | WINED3DTEXOPCAPS_SUBTRACT
7035 | WINED3DTEXOPCAPS_ADDSMOOTH
7036 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7037 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7038 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7039 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7040 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7041 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7042 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7043 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7044 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7045 | WINED3DTEXOPCAPS_DOTPRODUCT3
7046 | WINED3DTEXOPCAPS_MULTIPLYADD
7047 | WINED3DTEXOPCAPS_LERP
7048 | WINED3DTEXOPCAPS_BUMPENVMAP
7049 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7050 caps->MaxTextureBlendStages = 8;
7051 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7052 }
7053
7054 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7055 {
7056 struct shader_glsl_priv *priv;
7057
7058 if (shader_backend == &glsl_shader_backend)
7059 {
7060 priv = shader_priv;
7061
7062 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7063 {
7064 ERR("Failed to initialize rbtree.\n");
7065 return NULL;
7066 }
7067
7068 return priv;
7069 }
7070
7071 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7072
7073 return NULL;
7074 }
7075
7076 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7077 {
7078 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7079 struct glsl_ffp_fragment_shader, entry.entry);
7080 struct glsl_shader_prog_link *program, *program2;
7081 struct glsl_ffp_destroy_ctx *ctx = context;
7082
7083 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7084 struct glsl_shader_prog_link, ps.shader_entry)
7085 {
7086 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7087 }
7088 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
7089 HeapFree(GetProcessHeap(), 0, shader);
7090 }
7091
7092 /* Context activation is done by the caller. */
7093 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7094 {
7095 struct shader_glsl_priv *priv = device->fragment_priv;
7096 struct glsl_ffp_destroy_ctx ctx;
7097
7098 ctx.priv = priv;
7099 ctx.gl_info = &device->adapter->gl_info;
7100 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7101 }
7102
7103 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7104 const struct wined3d_state *state, DWORD state_id)
7105 {
7106 context->last_was_pshader = use_ps(state);
7107
7108 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7109 }
7110
7111 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7112 const struct wined3d_state *state, DWORD state_id)
7113 {
7114 BOOL use_vshader = use_vs(state);
7115 enum fogsource new_source;
7116 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7117 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7118
7119 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7120
7121 if (!state->render_states[WINED3D_RS_FOGENABLE])
7122 return;
7123
7124 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7125 {
7126 if (use_vshader)
7127 new_source = FOGSOURCE_VS;
7128 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7129 new_source = FOGSOURCE_COORD;
7130 else
7131 new_source = FOGSOURCE_FFP;
7132 }
7133 else
7134 {
7135 new_source = FOGSOURCE_FFP;
7136 }
7137
7138 if (new_source != context->fog_source || fogstart == fogend)
7139 {
7140 context->fog_source = new_source;
7141 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7142 }
7143 }
7144
7145 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7146 const struct wined3d_state *state, DWORD state_id)
7147 {
7148 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7149 }
7150
7151 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7152 const struct wined3d_state *state, DWORD state_id)
7153 {
7154 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7155 }
7156
7157 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7158 {
7159 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7160 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7161 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7162 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7163 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7164 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7165 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7166 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7167 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7168 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7169 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7170 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7171 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7172 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7173 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7174 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7175 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7176 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7177 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7178 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7179 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7180 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7181 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7182 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7183 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7184 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7185 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7186 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7187 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7188 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7189 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7190 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7191 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7192 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7193 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7194 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7195 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7196 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7197 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7198 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7199 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7200 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7201 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7202 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7203 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7204 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7205 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7206 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7207 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7208 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7209 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7210 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7211 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7212 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7213 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7214 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7215 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7216 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7217 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7218 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7219 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7220 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7221 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7222 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7223 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7224 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7225 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7226 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7227 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7228 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7229 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7230 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7231 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7232 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7233 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7234 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7235 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7236 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7237 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7238 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7239 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7240 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7241 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7242 {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
7243 {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
7244 {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
7245 {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
7246 {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
7247 {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
7248 {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
7249 {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_fragment_pipe_tex_transform }, WINED3D_GL_EXT_NONE },
7250 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7251 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7252 };
7253
7254 const struct fragment_pipeline glsl_fragment_pipe =
7255 {
7256 glsl_fragment_pipe_enable,
7257 glsl_fragment_pipe_get_caps,
7258 glsl_fragment_pipe_alloc,
7259 glsl_fragment_pipe_free,
7260 shader_glsl_color_fixup_supported,
7261 glsl_fragment_pipe_state_template,
7262 };