VirtualBox

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

Last change on this file since 32461 was 32461, checked in by vboxsync, 14 years ago

wddm/3d: wine multi-swapchain fixes

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

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