VirtualBox

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

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

crOpenGL/wddm: some texture mirroring code

  • Property svn:eol-style set to native
File size: 205.7 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 VBOX_WITH_WDDM
129 UINT inp2Fixup_info;
130#else
131 const struct ps_np2fixup_info *np2Fixup_info;
132#endif
133};
134
135#ifdef VBOX_WITH_WDDM
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 VBOX_WITH_WDDM
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 VBOX_WITH_WDDM
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 BOOL tmirror_tmp_reg = FALSE;
1945 va_list args;
1946
1947 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
1948
1949 if (shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
1950 {
1951 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1952 fixup = priv->cur_ps_args->color_fixup[sampler];
1953 sampler_base = "Psampler";
1954
1955 if (priv->cur_ps_args->np2_fixup & (1 << sampler)) {
1956 if(bias) {
1957 FIXME("Biased sampling from NP2 textures is unsupported\n");
1958 } else {
1959 np2_fixup = TRUE;
1960 }
1961 }
1962
1963 if (priv->cur_ps_args->t_mirror & (1 << sampler))
1964 {
1965 if (ins->ctx->reg_maps->sampler_type[sampler]==WINED3DSTT_2D)
1966 {
1967 if (sample_function->coord_mask & WINED3DSP_WRITEMASK_1)
1968 {
1969 glsl_src_param_t coord_param;
1970 shader_glsl_add_src_param(ins, &ins->src[0], sample_function->coord_mask, &coord_param);
1971
1972 if (ins->src[0].reg.type != WINED3DSPR_INPUT)
1973 {
1974 shader_addline(ins->ctx->buffer, "%s.y=1.0-%s.y;\n",
1975 coord_param.reg_name, coord_param.reg_name);
1976 }
1977 else
1978 {
1979 tmirror_tmp_reg = TRUE;
1980 shader_addline(ins->ctx->buffer, "tmp0.xy=vec2(%s.x, 1.0-%s.y).xy;\n",
1981 coord_param.reg_name, coord_param.reg_name);
1982 }
1983 }
1984 else
1985 {
1986 DebugBreak();
1987 FIXME("Unexpected coord_mask with t_mirror\n");
1988 }
1989 }
1990 }
1991 } else {
1992 sampler_base = "Vsampler";
1993 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
1994 }
1995
1996 shader_glsl_append_dst(ins->ctx->buffer, ins);
1997
1998 shader_addline(ins->ctx->buffer, "%s(%s%u, ", sample_function->name, sampler_base, sampler);
1999
2000 if (tmirror_tmp_reg)
2001 {
2002 shader_addline(ins->ctx->buffer, "%s", "tmp0.xy");
2003 }
2004 else
2005 {
2006 va_start(args, coord_reg_fmt);
2007 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2008 va_end(args);
2009 }
2010
2011 if(bias) {
2012 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2013 } else {
2014 if (np2_fixup) {
2015 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2016 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2017
2018 shader_addline(ins->ctx->buffer, " * PsamplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2019 (idx % 2) ? "zw" : "xy", dst_swizzle);
2020 } else if(dx && dy) {
2021 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2022 } else {
2023 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2024 }
2025 }
2026
2027 if(!is_identity_fixup(fixup)) {
2028 shader_glsl_color_correction(ins, fixup);
2029 }
2030}
2031
2032/*****************************************************************************
2033 * Begin processing individual instruction opcodes
2034 ****************************************************************************/
2035
2036/* Generate GLSL arithmetic functions (dst = src1 + src2) */
2037static void shader_glsl_arith(const struct wined3d_shader_instruction *ins)
2038{
2039 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2040 glsl_src_param_t src0_param;
2041 glsl_src_param_t src1_param;
2042 DWORD write_mask;
2043 char op;
2044
2045 /* Determine the GLSL operator to use based on the opcode */
2046 switch (ins->handler_idx)
2047 {
2048 case WINED3DSIH_MUL: op = '*'; break;
2049 case WINED3DSIH_ADD: op = '+'; break;
2050 case WINED3DSIH_SUB: op = '-'; break;
2051 default:
2052 op = ' ';
2053 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2054 break;
2055 }
2056
2057 write_mask = shader_glsl_append_dst(buffer, ins);
2058 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2059 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2060 shader_addline(buffer, "%s %c %s);\n", src0_param.param_str, op, src1_param.param_str);
2061}
2062
2063/* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2064static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2065{
2066 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2067 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2068 glsl_src_param_t src0_param;
2069 DWORD write_mask;
2070
2071 write_mask = shader_glsl_append_dst(buffer, ins);
2072 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2073
2074 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2075 * shader versions WINED3DSIO_MOVA is used for this. */
2076 if (ins->ctx->reg_maps->shader_version.major == 1
2077 && !shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type)
2078 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2079 {
2080 /* This is a simple floor() */
2081 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2082 if (mask_size > 1) {
2083 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2084 } else {
2085 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2086 }
2087 }
2088 else if(ins->handler_idx == WINED3DSIH_MOVA)
2089 {
2090 /* We need to *round* to the nearest int here. */
2091 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2092
2093 if (gl_info->supported[EXT_GPU_SHADER4])
2094 {
2095 if (mask_size > 1)
2096 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2097 else
2098 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2099 }
2100 else
2101 {
2102 if (mask_size > 1)
2103 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2104 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2105 else
2106 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2107 src0_param.param_str, src0_param.param_str);
2108 }
2109 }
2110 else
2111 {
2112 shader_addline(buffer, "%s);\n", src0_param.param_str);
2113 }
2114}
2115
2116/* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2117static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2118{
2119 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2120 glsl_src_param_t src0_param;
2121 glsl_src_param_t src1_param;
2122 DWORD dst_write_mask, src_write_mask;
2123 unsigned int dst_size = 0;
2124
2125 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2126 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2127
2128 /* dp3 works on vec3, dp4 on vec4 */
2129 if (ins->handler_idx == WINED3DSIH_DP4)
2130 {
2131 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2132 } else {
2133 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2134 }
2135
2136 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2137 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2138
2139 if (dst_size > 1) {
2140 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2141 } else {
2142 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2143 }
2144}
2145
2146/* Note that this instruction has some restrictions. The destination write mask
2147 * can't contain the w component, and the source swizzles have to be .xyzw */
2148static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2149{
2150 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2151 glsl_src_param_t src0_param;
2152 glsl_src_param_t src1_param;
2153 char dst_mask[6];
2154
2155 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2156 shader_glsl_append_dst(ins->ctx->buffer, ins);
2157 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2158 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2159 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2160}
2161
2162/* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2163 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2164 * GLSL uses the value as-is. */
2165static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2166{
2167 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2168 glsl_src_param_t src0_param;
2169 glsl_src_param_t src1_param;
2170 DWORD dst_write_mask;
2171 unsigned int dst_size;
2172
2173 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2174 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2175
2176 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2177 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2178
2179 if (dst_size > 1) {
2180 shader_addline(buffer, "vec%d(pow(abs(%s), %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2181 } else {
2182 shader_addline(buffer, "pow(abs(%s), %s));\n", src0_param.param_str, src1_param.param_str);
2183 }
2184}
2185
2186/* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2187 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2188 * GLSL uses the value as-is. */
2189static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2190{
2191 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2192 glsl_src_param_t src0_param;
2193 DWORD dst_write_mask;
2194 unsigned int dst_size;
2195
2196 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2197 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2198
2199 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2200
2201 if (dst_size > 1)
2202 {
2203 shader_addline(buffer, "vec%d(%s == 0.0 ? -FLT_MAX : log2(abs(%s))));\n",
2204 dst_size, src0_param.param_str, src0_param.param_str);
2205 }
2206 else
2207 {
2208 shader_addline(buffer, "%s == 0.0 ? -FLT_MAX : log2(abs(%s)));\n",
2209 src0_param.param_str, src0_param.param_str);
2210 }
2211}
2212
2213/* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2214static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2215{
2216 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2217 glsl_src_param_t src_param;
2218 const char *instruction;
2219 DWORD write_mask;
2220 unsigned i;
2221
2222 /* Determine the GLSL function to use based on the opcode */
2223 /* TODO: Possibly make this a table for faster lookups */
2224 switch (ins->handler_idx)
2225 {
2226 case WINED3DSIH_MIN: instruction = "min"; break;
2227 case WINED3DSIH_MAX: instruction = "max"; break;
2228 case WINED3DSIH_ABS: instruction = "abs"; break;
2229 case WINED3DSIH_FRC: instruction = "fract"; break;
2230 case WINED3DSIH_EXP: instruction = "exp2"; break;
2231 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2232 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2233 default: instruction = "";
2234 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2235 break;
2236 }
2237
2238 write_mask = shader_glsl_append_dst(buffer, ins);
2239
2240 shader_addline(buffer, "%s(", instruction);
2241
2242 if (ins->src_count)
2243 {
2244 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2245 shader_addline(buffer, "%s", src_param.param_str);
2246 for (i = 1; i < ins->src_count; ++i)
2247 {
2248 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2249 shader_addline(buffer, ", %s", src_param.param_str);
2250 }
2251 }
2252
2253 shader_addline(buffer, "));\n");
2254}
2255
2256static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2257{
2258 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2259 glsl_src_param_t src_param;
2260 unsigned int mask_size;
2261 DWORD write_mask;
2262 char dst_mask[6];
2263
2264 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2265 mask_size = shader_glsl_get_write_mask_size(write_mask);
2266 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2267
2268 shader_addline(buffer, "tmp0.x = length(%s);\n", src_param.param_str);
2269 shader_glsl_append_dst(buffer, ins);
2270 if (mask_size > 1)
2271 {
2272 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s / tmp0.x));\n",
2273 mask_size, src_param.param_str);
2274 }
2275 else
2276 {
2277 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s / tmp0.x));\n",
2278 src_param.param_str);
2279 }
2280}
2281
2282/** Process the WINED3DSIO_EXPP instruction in GLSL:
2283 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2284 * dst.x = 2^(floor(src))
2285 * dst.y = src - floor(src)
2286 * dst.z = 2^src (partial precision is allowed, but optional)
2287 * dst.w = 1.0;
2288 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2289 * dst = 2^src; (partial precision is allowed, but optional)
2290 */
2291static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2292{
2293 glsl_src_param_t src_param;
2294
2295 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2296
2297 if (ins->ctx->reg_maps->shader_version.major < 2)
2298 {
2299 char dst_mask[6];
2300
2301 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2302 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2303 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2304 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2305
2306 shader_glsl_append_dst(ins->ctx->buffer, ins);
2307 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2308 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2309 } else {
2310 DWORD write_mask;
2311 unsigned int mask_size;
2312
2313 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2314 mask_size = shader_glsl_get_write_mask_size(write_mask);
2315
2316 if (mask_size > 1) {
2317 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2318 } else {
2319 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2320 }
2321 }
2322}
2323
2324/** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2325static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2326{
2327 glsl_src_param_t src_param;
2328 DWORD write_mask;
2329 unsigned int mask_size;
2330
2331 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2332 mask_size = shader_glsl_get_write_mask_size(write_mask);
2333 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2334
2335 if (mask_size > 1)
2336 {
2337 shader_addline(ins->ctx->buffer, "vec%d(%s == 0.0 ? FLT_MAX : 1.0 / %s));\n",
2338 mask_size, src_param.param_str, src_param.param_str);
2339 }
2340 else
2341 {
2342 shader_addline(ins->ctx->buffer, "%s == 0.0 ? FLT_MAX : 1.0 / %s);\n",
2343 src_param.param_str, src_param.param_str);
2344 }
2345}
2346
2347static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2348{
2349 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2350 glsl_src_param_t src_param;
2351 DWORD write_mask;
2352 unsigned int mask_size;
2353
2354 write_mask = shader_glsl_append_dst(buffer, ins);
2355 mask_size = shader_glsl_get_write_mask_size(write_mask);
2356
2357 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2358
2359 if (mask_size > 1)
2360 {
2361 shader_addline(buffer, "vec%d(%s == 0.0 ? FLT_MAX : inversesqrt(abs(%s))));\n",
2362 mask_size, src_param.param_str, src_param.param_str);
2363 }
2364 else
2365 {
2366 shader_addline(buffer, "%s == 0.0 ? FLT_MAX : inversesqrt(abs(%s)));\n",
2367 src_param.param_str, src_param.param_str);
2368 }
2369}
2370
2371/** Process signed comparison opcodes in GLSL. */
2372static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2373{
2374 glsl_src_param_t src0_param;
2375 glsl_src_param_t src1_param;
2376 DWORD write_mask;
2377 unsigned int mask_size;
2378
2379 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2380 mask_size = shader_glsl_get_write_mask_size(write_mask);
2381 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2382 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2383
2384 if (mask_size > 1) {
2385 const char *compare;
2386
2387 switch(ins->handler_idx)
2388 {
2389 case WINED3DSIH_SLT: compare = "lessThan"; break;
2390 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2391 default: compare = "";
2392 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2393 }
2394
2395 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2396 src0_param.param_str, src1_param.param_str);
2397 } else {
2398 switch(ins->handler_idx)
2399 {
2400 case WINED3DSIH_SLT:
2401 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2402 * to return 0.0 but step returns 1.0 because step is not < x
2403 * An alternative is a bvec compare padded with an unused second component.
2404 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2405 * issue. Playing with not() is not possible either because not() does not accept
2406 * a scalar.
2407 */
2408 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2409 src0_param.param_str, src1_param.param_str);
2410 break;
2411 case WINED3DSIH_SGE:
2412 /* Here we can use the step() function and safe a conditional */
2413 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2414 break;
2415 default:
2416 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2417 }
2418
2419 }
2420}
2421
2422/** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
2423static void shader_glsl_cmp(const struct wined3d_shader_instruction *ins)
2424{
2425 glsl_src_param_t src0_param;
2426 glsl_src_param_t src1_param;
2427 glsl_src_param_t src2_param;
2428 DWORD write_mask, cmp_channel = 0;
2429 unsigned int i, j;
2430 char mask_char[6];
2431 BOOL temp_destination = FALSE;
2432
2433 if (shader_is_scalar(&ins->src[0].reg))
2434 {
2435 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2436
2437 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
2438 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2439 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2440
2441 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2442 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2443 } else {
2444 DWORD dst_mask = ins->dst[0].write_mask;
2445 struct wined3d_shader_dst_param dst = ins->dst[0];
2446
2447 /* Cycle through all source0 channels */
2448 for (i=0; i<4; i++) {
2449 write_mask = 0;
2450 /* Find the destination channels which use the current source0 channel */
2451 for (j=0; j<4; j++) {
2452 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2453 {
2454 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2455 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2456 }
2457 }
2458 dst.write_mask = dst_mask & write_mask;
2459
2460 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2461 * The first lines may overwrite source parameters of the following lines.
2462 * Deal with that by using a temporary destination register if needed
2463 */
2464 if ((ins->src[0].reg.idx == ins->dst[0].reg.idx
2465 && ins->src[0].reg.type == ins->dst[0].reg.type)
2466 || (ins->src[1].reg.idx == ins->dst[0].reg.idx
2467 && ins->src[1].reg.type == ins->dst[0].reg.type)
2468 || (ins->src[2].reg.idx == ins->dst[0].reg.idx
2469 && ins->src[2].reg.type == ins->dst[0].reg.type))
2470 {
2471 write_mask = shader_glsl_get_write_mask(&dst, mask_char);
2472 if (!write_mask) continue;
2473 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2474 temp_destination = TRUE;
2475 } else {
2476 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2477 if (!write_mask) continue;
2478 }
2479
2480 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2481 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2482 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2483
2484 shader_addline(ins->ctx->buffer, "%s >= 0.0 ? %s : %s);\n",
2485 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2486 }
2487
2488 if(temp_destination) {
2489 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2490 shader_glsl_append_dst(ins->ctx->buffer, ins);
2491 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2492 }
2493 }
2494
2495}
2496
2497/** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2498/* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2499 * the compare is done per component of src0. */
2500static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2501{
2502 struct wined3d_shader_dst_param dst;
2503 glsl_src_param_t src0_param;
2504 glsl_src_param_t src1_param;
2505 glsl_src_param_t src2_param;
2506 DWORD write_mask, cmp_channel = 0;
2507 unsigned int i, j;
2508 DWORD dst_mask;
2509 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2510 ins->ctx->reg_maps->shader_version.minor);
2511
2512 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2513 {
2514 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2515 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2516 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2517 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2518
2519 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2520 if (ins->coissue)
2521 {
2522 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2523 } else {
2524 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2525 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2526 }
2527 return;
2528 }
2529 /* Cycle through all source0 channels */
2530 dst_mask = ins->dst[0].write_mask;
2531 dst = ins->dst[0];
2532 for (i=0; i<4; i++) {
2533 write_mask = 0;
2534 /* Find the destination channels which use the current source0 channel */
2535 for (j=0; j<4; j++) {
2536 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2537 {
2538 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2539 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2540 }
2541 }
2542
2543 dst.write_mask = dst_mask & write_mask;
2544 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst);
2545 if (!write_mask) continue;
2546
2547 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2548 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2549 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2550
2551 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2552 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2553 }
2554}
2555
2556/** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2557static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2558{
2559 glsl_src_param_t src0_param;
2560 glsl_src_param_t src1_param;
2561 glsl_src_param_t src2_param;
2562 DWORD write_mask;
2563
2564 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2565 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2566 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2567 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2568 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2569 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2570}
2571
2572/* Handles transforming all WINED3DSIO_M?x? opcodes for
2573 Vertex shaders to GLSL codes */
2574static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2575{
2576 int i;
2577 int nComponents = 0;
2578 struct wined3d_shader_dst_param tmp_dst = {{0}};
2579 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2580 struct wined3d_shader_instruction tmp_ins;
2581
2582 memset(&tmp_ins, 0, sizeof(tmp_ins));
2583
2584 /* Set constants for the temporary argument */
2585 tmp_ins.ctx = ins->ctx;
2586 tmp_ins.dst_count = 1;
2587 tmp_ins.dst = &tmp_dst;
2588 tmp_ins.src_count = 2;
2589 tmp_ins.src = tmp_src;
2590
2591 switch(ins->handler_idx)
2592 {
2593 case WINED3DSIH_M4x4:
2594 nComponents = 4;
2595 tmp_ins.handler_idx = WINED3DSIH_DP4;
2596 break;
2597 case WINED3DSIH_M4x3:
2598 nComponents = 3;
2599 tmp_ins.handler_idx = WINED3DSIH_DP4;
2600 break;
2601 case WINED3DSIH_M3x4:
2602 nComponents = 4;
2603 tmp_ins.handler_idx = WINED3DSIH_DP3;
2604 break;
2605 case WINED3DSIH_M3x3:
2606 nComponents = 3;
2607 tmp_ins.handler_idx = WINED3DSIH_DP3;
2608 break;
2609 case WINED3DSIH_M3x2:
2610 nComponents = 2;
2611 tmp_ins.handler_idx = WINED3DSIH_DP3;
2612 break;
2613 default:
2614 break;
2615 }
2616
2617 tmp_dst = ins->dst[0];
2618 tmp_src[0] = ins->src[0];
2619 tmp_src[1] = ins->src[1];
2620 for (i = 0; i < nComponents; ++i)
2621 {
2622 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2623 shader_glsl_dot(&tmp_ins);
2624 ++tmp_src[1].reg.idx;
2625 }
2626}
2627
2628/**
2629 The LRP instruction performs a component-wise linear interpolation
2630 between the second and third operands using the first operand as the
2631 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2632 This is equivalent to mix(src2, src1, src0);
2633*/
2634static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
2635{
2636 glsl_src_param_t src0_param;
2637 glsl_src_param_t src1_param;
2638 glsl_src_param_t src2_param;
2639 DWORD write_mask;
2640
2641 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2642
2643 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2644 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2645 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2646
2647 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
2648 src2_param.param_str, src1_param.param_str, src0_param.param_str);
2649}
2650
2651/** Process the WINED3DSIO_LIT instruction in GLSL:
2652 * dst.x = dst.w = 1.0
2653 * dst.y = (src0.x > 0) ? src0.x
2654 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2655 * where src.w is clamped at +- 128
2656 */
2657static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
2658{
2659 glsl_src_param_t src0_param;
2660 glsl_src_param_t src1_param;
2661 glsl_src_param_t src3_param;
2662 char dst_mask[6];
2663
2664 shader_glsl_append_dst(ins->ctx->buffer, ins);
2665 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2666
2667 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2668 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
2669 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
2670
2671 /* The sdk specifies the instruction like this
2672 * dst.x = 1.0;
2673 * if(src.x > 0.0) dst.y = src.x
2674 * else dst.y = 0.0.
2675 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2676 * else dst.z = 0.0;
2677 * dst.w = 1.0;
2678 *
2679 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2680 * dst.x = 1.0 ... No further explanation needed
2681 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2682 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2683 * dst.w = 1.0. ... Nothing fancy.
2684 *
2685 * So we still have one conditional in there. So do this:
2686 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2687 *
2688 * 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),
2689 * 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.
2690 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
2691 */
2692 shader_addline(ins->ctx->buffer,
2693 "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",
2694 src0_param.param_str, src1_param.param_str, src0_param.param_str, src3_param.param_str, dst_mask);
2695}
2696
2697/** Process the WINED3DSIO_DST instruction in GLSL:
2698 * dst.x = 1.0
2699 * dst.y = src0.x * src0.y
2700 * dst.z = src0.z
2701 * dst.w = src1.w
2702 */
2703static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
2704{
2705 glsl_src_param_t src0y_param;
2706 glsl_src_param_t src0z_param;
2707 glsl_src_param_t src1y_param;
2708 glsl_src_param_t src1w_param;
2709 char dst_mask[6];
2710
2711 shader_glsl_append_dst(ins->ctx->buffer, ins);
2712 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2713
2714 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
2715 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
2716 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
2717 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
2718
2719 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2720 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
2721}
2722
2723/** Process the WINED3DSIO_SINCOS instruction in GLSL:
2724 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2725 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2726 *
2727 * dst.x = cos(src0.?)
2728 * dst.y = sin(src0.?)
2729 * dst.z = dst.z
2730 * dst.w = dst.w
2731 */
2732static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
2733{
2734 glsl_src_param_t src0_param;
2735 DWORD write_mask;
2736
2737 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2738 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2739
2740 switch (write_mask) {
2741 case WINED3DSP_WRITEMASK_0:
2742 shader_addline(ins->ctx->buffer, "cos(%s));\n", src0_param.param_str);
2743 break;
2744
2745 case WINED3DSP_WRITEMASK_1:
2746 shader_addline(ins->ctx->buffer, "sin(%s));\n", src0_param.param_str);
2747 break;
2748
2749 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
2750 shader_addline(ins->ctx->buffer, "vec2(cos(%s), sin(%s)));\n", src0_param.param_str, src0_param.param_str);
2751 break;
2752
2753 default:
2754 ERR("Write mask should be .x, .y or .xy\n");
2755 break;
2756 }
2757}
2758
2759/* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
2760 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
2761 * generate invalid code
2762 */
2763static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
2764{
2765 glsl_src_param_t src0_param;
2766 DWORD write_mask;
2767
2768 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2769 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2770
2771 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
2772}
2773
2774/** Process the WINED3DSIO_LOOP instruction in GLSL:
2775 * Start a for() loop where src1.y is the initial value of aL,
2776 * increment aL by src1.z for a total of src1.x iterations.
2777 * Need to use a temporary variable for this operation.
2778 */
2779/* FIXME: I don't think nested loops will work correctly this way. */
2780static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
2781{
2782 glsl_src_param_t src1_param;
2783 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2784 const DWORD *control_values = NULL;
2785 const local_constant *constant;
2786
2787 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
2788
2789 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2790 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2791 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2792 * addressing.
2793 */
2794 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
2795 {
2796 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry) {
2797 if (constant->idx == ins->src[1].reg.idx)
2798 {
2799 control_values = constant->value;
2800 break;
2801 }
2802 }
2803 }
2804
2805 if (control_values)
2806 {
2807 struct wined3d_shader_loop_control loop_control;
2808 loop_control.count = control_values[0];
2809 loop_control.start = control_values[1];
2810 loop_control.step = (int)control_values[2];
2811
2812 if (loop_control.step > 0)
2813 {
2814 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d) {\n",
2815 shader->baseShader.cur_loop_depth, loop_control.start,
2816 shader->baseShader.cur_loop_depth, loop_control.count, loop_control.step, loop_control.start,
2817 shader->baseShader.cur_loop_depth, loop_control.step);
2818 }
2819 else if (loop_control.step < 0)
2820 {
2821 shader_addline(ins->ctx->buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d) {\n",
2822 shader->baseShader.cur_loop_depth, loop_control.start,
2823 shader->baseShader.cur_loop_depth, loop_control.count, loop_control.step, loop_control.start,
2824 shader->baseShader.cur_loop_depth, loop_control.step);
2825 }
2826 else
2827 {
2828 shader_addline(ins->ctx->buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++) {\n",
2829 shader->baseShader.cur_loop_depth, loop_control.start, shader->baseShader.cur_loop_depth,
2830 shader->baseShader.cur_loop_depth, loop_control.count,
2831 shader->baseShader.cur_loop_depth);
2832 }
2833 } else {
2834 shader_addline(ins->ctx->buffer,
2835 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2836 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno,
2837 src1_param.reg_name, shader->baseShader.cur_loop_depth, src1_param.reg_name,
2838 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_regno, src1_param.reg_name);
2839 }
2840
2841 shader->baseShader.cur_loop_depth++;
2842 shader->baseShader.cur_loop_regno++;
2843}
2844
2845static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
2846{
2847 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2848
2849 shader_addline(ins->ctx->buffer, "}\n");
2850
2851 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
2852 {
2853 shader->baseShader.cur_loop_depth--;
2854 shader->baseShader.cur_loop_regno--;
2855 }
2856
2857 if (ins->handler_idx == WINED3DSIH_ENDREP)
2858 {
2859 shader->baseShader.cur_loop_depth--;
2860 }
2861}
2862
2863static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
2864{
2865 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2866 glsl_src_param_t src0_param;
2867 const DWORD *control_values = NULL;
2868 const local_constant *constant;
2869
2870 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2871 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
2872 {
2873 LIST_FOR_EACH_ENTRY(constant, &shader->baseShader.constantsI, local_constant, entry)
2874 {
2875 if (constant->idx == ins->src[0].reg.idx)
2876 {
2877 control_values = constant->value;
2878 break;
2879 }
2880 }
2881 }
2882
2883 if(control_values) {
2884 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2885 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2886 control_values[0], shader->baseShader.cur_loop_depth);
2887 } else {
2888 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2889 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2890 shader->baseShader.cur_loop_depth, shader->baseShader.cur_loop_depth,
2891 src0_param.param_str, shader->baseShader.cur_loop_depth);
2892 }
2893 shader->baseShader.cur_loop_depth++;
2894}
2895
2896static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
2897{
2898 glsl_src_param_t src0_param;
2899
2900 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2901 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
2902}
2903
2904static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
2905{
2906 glsl_src_param_t src0_param;
2907 glsl_src_param_t src1_param;
2908
2909 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2910 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2911
2912 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
2913 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2914}
2915
2916static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
2917{
2918 shader_addline(ins->ctx->buffer, "} else {\n");
2919}
2920
2921static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
2922{
2923 shader_addline(ins->ctx->buffer, "break;\n");
2924}
2925
2926/* FIXME: According to MSDN the compare is done per component. */
2927static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
2928{
2929 glsl_src_param_t src0_param;
2930 glsl_src_param_t src1_param;
2931
2932 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2933 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2934
2935 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
2936 src0_param.param_str, shader_get_comp_op(ins->flags), src1_param.param_str);
2937}
2938
2939static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
2940{
2941 shader_addline(ins->ctx->buffer, "}\n");
2942 shader_addline(ins->ctx->buffer, "void subroutine%u () {\n", ins->src[0].reg.idx);
2943}
2944
2945static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
2946{
2947 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx);
2948}
2949
2950static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
2951{
2952 glsl_src_param_t src1_param;
2953
2954 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2955 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n", src1_param.param_str, ins->src[0].reg.idx);
2956}
2957
2958static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
2959{
2960 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
2961 * function only suppresses the unhandled instruction warning
2962 */
2963}
2964
2965/*********************************************
2966 * Pixel Shader Specific Code begins here
2967 ********************************************/
2968static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
2969{
2970 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
2971 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *)shader->baseShader.device;
2972 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2973 ins->ctx->reg_maps->shader_version.minor);
2974 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2975 glsl_sample_function_t sample_function;
2976 DWORD sample_flags = 0;
2977 WINED3DSAMPLER_TEXTURE_TYPE sampler_type;
2978 DWORD sampler_idx;
2979 DWORD mask = 0, swizzle;
2980
2981 /* 1.0-1.4: Use destination register as sampler source.
2982 * 2.0+: Use provided sampler source. */
2983 if (shader_version < WINED3D_SHADER_VERSION(2,0)) sampler_idx = ins->dst[0].reg.idx;
2984 else sampler_idx = ins->src[1].reg.idx;
2985 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
2986
2987 if (shader_version < WINED3D_SHADER_VERSION(1,4))
2988 {
2989 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2990
2991 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2992 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2993 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2994 switch (flags & ~WINED3DTTFF_PROJECTED) {
2995 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2996 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2997 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2998 case WINED3DTTFF_COUNT4:
2999 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
3000 }
3001 }
3002 }
3003 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3004 {
3005 DWORD src_mod = ins->src[0].modifiers;
3006
3007 if (src_mod == WINED3DSPSM_DZ) {
3008 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3009 mask = WINED3DSP_WRITEMASK_2;
3010 } else if (src_mod == WINED3DSPSM_DW) {
3011 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3012 mask = WINED3DSP_WRITEMASK_3;
3013 }
3014 } else {
3015 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
3016 {
3017 /* ps 2.0 texldp instruction always divides by the fourth component. */
3018 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3019 mask = WINED3DSP_WRITEMASK_3;
3020 }
3021 }
3022
3023 if(deviceImpl->stateBlock->textures[sampler_idx] &&
3024 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
3025 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3026 }
3027
3028 shader_glsl_get_sample_function(gl_info, sampler_type, sample_flags, &sample_function);
3029 mask |= sample_function.coord_mask;
3030
3031 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3032 else swizzle = ins->src[1].swizzle;
3033
3034 /* 1.0-1.3: Use destination register as coordinate source.
3035 1.4+: Use provided coordinate source register. */
3036 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3037 {
3038 char coord_mask[6];
3039 shader_glsl_write_mask_to_str(mask, coord_mask);
3040 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3041 "T%u%s", sampler_idx, coord_mask);
3042 } else {
3043 glsl_src_param_t coord_param;
3044 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3045 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3046 {
3047 glsl_src_param_t bias;
3048 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3049 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3050 "%s", coord_param.param_str);
3051 } else {
3052 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3053 "%s", coord_param.param_str);
3054 }
3055 }
3056}
3057
3058static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3059{
3060 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3061 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
3062 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3063 glsl_sample_function_t sample_function;
3064 glsl_src_param_t coord_param, dx_param, dy_param;
3065 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3066 DWORD sampler_type;
3067 DWORD sampler_idx;
3068 DWORD swizzle = ins->src[1].swizzle;
3069
3070 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3071 {
3072 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3073 return shader_glsl_tex(ins);
3074 }
3075
3076 sampler_idx = ins->src[1].reg.idx;
3077 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3078 if(deviceImpl->stateBlock->textures[sampler_idx] &&
3079 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
3080 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3081 }
3082
3083 shader_glsl_get_sample_function(gl_info, sampler_type, sample_flags, &sample_function);
3084 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3085 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3086 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3087
3088 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3089 "%s", coord_param.param_str);
3090}
3091
3092static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3093{
3094 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3095 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
3096 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3097 glsl_sample_function_t sample_function;
3098 glsl_src_param_t coord_param, lod_param;
3099 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3100 DWORD sampler_type;
3101 DWORD sampler_idx;
3102 DWORD swizzle = ins->src[1].swizzle;
3103
3104 sampler_idx = ins->src[1].reg.idx;
3105 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3106 if(deviceImpl->stateBlock->textures[sampler_idx] &&
3107 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
3108 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3109 }
3110 shader_glsl_get_sample_function(gl_info, sampler_type, sample_flags, &sample_function);
3111 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3112
3113 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3114
3115 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3116 && shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
3117 {
3118 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
3119 * However, they seem to work just fine in fragment shaders as well. */
3120 WARN("Using %s in fragment shader.\n", sample_function.name);
3121 }
3122 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3123 "%s", coord_param.param_str);
3124}
3125
3126static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3127{
3128 /* FIXME: Make this work for more than just 2D textures */
3129 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3130 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3131
3132 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3133 {
3134 char dst_mask[6];
3135
3136 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3137 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3138 ins->dst[0].reg.idx, dst_mask);
3139 } else {
3140 DWORD reg = ins->src[0].reg.idx;
3141 DWORD src_mod = ins->src[0].modifiers;
3142 char dst_swizzle[6];
3143
3144 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3145
3146 if (src_mod == WINED3DSPSM_DZ) {
3147 glsl_src_param_t div_param;
3148 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3149 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3150
3151 if (mask_size > 1) {
3152 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3153 } else {
3154 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3155 }
3156 } else if (src_mod == WINED3DSPSM_DW) {
3157 glsl_src_param_t div_param;
3158 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3159 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3160
3161 if (mask_size > 1) {
3162 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3163 } else {
3164 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3165 }
3166 } else {
3167 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3168 }
3169 }
3170}
3171
3172/** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3173 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3174 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3175static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3176{
3177 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3178 glsl_src_param_t src0_param;
3179 glsl_sample_function_t sample_function;
3180 DWORD sampler_idx = ins->dst[0].reg.idx;
3181 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3182 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3183 UINT mask_size;
3184
3185 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3186
3187 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3188 * scalar, and projected sampling would require 4.
3189 *
3190 * It is a dependent read - not valid with conditional NP2 textures
3191 */
3192 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3193 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3194
3195 switch(mask_size)
3196 {
3197 case 1:
3198 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3199 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3200 break;
3201
3202 case 2:
3203 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3204 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3205 break;
3206
3207 case 3:
3208 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3209 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3210 break;
3211
3212 default:
3213 FIXME("Unexpected mask size %u\n", mask_size);
3214 break;
3215 }
3216}
3217
3218/** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3219 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3220static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3221{
3222 glsl_src_param_t src0_param;
3223 DWORD dstreg = ins->dst[0].reg.idx;
3224 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3225 DWORD dst_mask;
3226 unsigned int mask_size;
3227
3228 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3229 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3230 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3231
3232 if (mask_size > 1) {
3233 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3234 } else {
3235 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3236 }
3237}
3238
3239/** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3240 * Calculate the depth as dst.x / dst.y */
3241static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3242{
3243 glsl_dst_param_t dst_param;
3244
3245 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3246
3247 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3248 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3249 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3250 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3251 * >= 1.0 or < 0.0
3252 */
3253 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3254 dst_param.reg_name, dst_param.reg_name);
3255}
3256
3257/** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3258 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3259 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3260 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3261 */
3262static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3263{
3264 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3265 DWORD dstreg = ins->dst[0].reg.idx;
3266 glsl_src_param_t src0_param;
3267
3268 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3269
3270 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3271 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3272}
3273
3274/** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3275 * Calculate the 1st of a 2-row matrix multiplication. */
3276static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3277{
3278 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3279 DWORD reg = ins->dst[0].reg.idx;
3280 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3281 glsl_src_param_t src0_param;
3282
3283 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3284 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3285}
3286
3287/** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3288 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3289static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3290{
3291 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3292 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3293 DWORD reg = ins->dst[0].reg.idx;
3294 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3295 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
3296 glsl_src_param_t src0_param;
3297
3298 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3299 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
3300 current_state->texcoord_w[current_state->current_row++] = reg;
3301}
3302
3303static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3304{
3305 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3306 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3307 DWORD reg = ins->dst[0].reg.idx;
3308 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3309 glsl_src_param_t src0_param;
3310 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
3311 glsl_sample_function_t sample_function;
3312
3313 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3314 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3315
3316 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3317
3318 /* Sample the texture using the calculated coordinates */
3319 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3320}
3321
3322/** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3323 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3324static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3325{
3326 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3327 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3328 SHADER_PARSE_STATE *current_state = &shader->baseShader.parse_state;
3329 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3330 glsl_src_param_t src0_param;
3331 DWORD reg = ins->dst[0].reg.idx;
3332 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
3333 glsl_sample_function_t sample_function;
3334
3335 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3336 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3337
3338 /* Dependent read, not valid with conditional NP2 */
3339 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3340
3341 /* Sample the texture using the calculated coordinates */
3342 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3343
3344 current_state->current_row = 0;
3345}
3346
3347/** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3348 * Perform the 3rd row of a 3x3 matrix multiply */
3349static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3350{
3351 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3352 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3353 SHADER_PARSE_STATE *current_state = &shader->baseShader.parse_state;
3354 glsl_src_param_t src0_param;
3355 char dst_mask[6];
3356 DWORD reg = ins->dst[0].reg.idx;
3357
3358 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3359
3360 shader_glsl_append_dst(ins->ctx->buffer, ins);
3361 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3362 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3363
3364 current_state->current_row = 0;
3365}
3366
3367/* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3368 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3369static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3370{
3371 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3372 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3373 DWORD reg = ins->dst[0].reg.idx;
3374 glsl_src_param_t src0_param;
3375 glsl_src_param_t src1_param;
3376 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3377 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
3378 WINED3DSAMPLER_TEXTURE_TYPE stype = ins->ctx->reg_maps->sampler_type[reg];
3379 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3380 glsl_sample_function_t sample_function;
3381
3382 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3383 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3384
3385 /* Perform the last matrix multiply operation */
3386 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3387 /* Reflection calculation */
3388 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3389
3390 /* Dependent read, not valid with conditional NP2 */
3391 shader_glsl_get_sample_function(gl_info, stype, 0, &sample_function);
3392
3393 /* Sample the texture */
3394 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3395
3396 current_state->current_row = 0;
3397}
3398
3399/* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3400 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3401static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3402{
3403 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3404 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3405 DWORD reg = ins->dst[0].reg.idx;
3406 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3407 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
3408 glsl_src_param_t src0_param;
3409 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3410 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
3411 glsl_sample_function_t sample_function;
3412
3413 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3414
3415 /* Perform the last matrix multiply operation */
3416 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3417
3418 /* Construct the eye-ray vector from w coordinates */
3419 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3420 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
3421 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3422
3423 /* Dependent read, not valid with conditional NP2 */
3424 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3425
3426 /* Sample the texture using the calculated coordinates */
3427 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3428
3429 current_state->current_row = 0;
3430}
3431
3432/** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3433 * Apply a fake bump map transform.
3434 * texbem is pshader <= 1.3 only, this saves a few version checks
3435 */
3436static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3437{
3438 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3439 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *)shader->baseShader.device;
3440 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3441 glsl_sample_function_t sample_function;
3442 glsl_src_param_t coord_param;
3443 WINED3DSAMPLER_TEXTURE_TYPE sampler_type;
3444 DWORD sampler_idx;
3445 DWORD mask;
3446 DWORD flags;
3447 char coord_mask[6];
3448
3449 sampler_idx = ins->dst[0].reg.idx;
3450 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
3451
3452 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3453 /* Dependent read, not valid with conditional NP2 */
3454 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3455 mask = sample_function.coord_mask;
3456
3457 shader_glsl_write_mask_to_str(mask, coord_mask);
3458
3459 /* with projective textures, texbem only divides the static texture coord, not the displacement,
3460 * so we can't let the GL handle this.
3461 */
3462 if (flags & WINED3DTTFF_PROJECTED) {
3463 DWORD div_mask=0;
3464 char coord_div_mask[3];
3465 switch (flags & ~WINED3DTTFF_PROJECTED) {
3466 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3467 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
3468 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
3469 case WINED3DTTFF_COUNT4:
3470 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
3471 }
3472 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3473 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3474 }
3475
3476 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3477
3478 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3479 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3480 coord_param.param_str, coord_mask);
3481
3482 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3483 {
3484 glsl_src_param_t luminance_param;
3485 glsl_dst_param_t dst_param;
3486
3487 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3488 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3489
3490 shader_addline(ins->ctx->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3491 dst_param.reg_name, dst_param.mask_str,
3492 luminance_param.param_str, sampler_idx, sampler_idx);
3493 }
3494}
3495
3496static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3497{
3498 glsl_src_param_t src0_param, src1_param;
3499 DWORD sampler_idx = ins->dst[0].reg.idx;
3500
3501 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3502 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3503
3504 shader_glsl_append_dst(ins->ctx->buffer, ins);
3505 shader_addline(ins->ctx->buffer, "%s + bumpenvmat%d * %s);\n",
3506 src0_param.param_str, sampler_idx, src1_param.param_str);
3507}
3508
3509/** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3510 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3511static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
3512{
3513 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3514 glsl_src_param_t src0_param;
3515 DWORD sampler_idx = ins->dst[0].reg.idx;
3516 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3517 glsl_sample_function_t sample_function;
3518
3519 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3520
3521 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3522 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3523 "%s.wx", src0_param.reg_name);
3524}
3525
3526/** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3527 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3528static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
3529{
3530 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3531 glsl_src_param_t src0_param;
3532 DWORD sampler_idx = ins->dst[0].reg.idx;
3533 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3534 glsl_sample_function_t sample_function;
3535
3536 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3537
3538 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3539 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3540 "%s.yz", src0_param.reg_name);
3541}
3542
3543/** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3544 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3545static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
3546{
3547 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3548 glsl_src_param_t src0_param;
3549 DWORD sampler_idx = ins->dst[0].reg.idx;
3550 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3551 glsl_sample_function_t sample_function;
3552
3553 /* Dependent read, not valid with conditional NP2 */
3554 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3555 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
3556
3557 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3558 "%s", src0_param.param_str);
3559}
3560
3561/** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3562 * If any of the first 3 components are < 0, discard this pixel */
3563static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
3564{
3565 glsl_dst_param_t dst_param;
3566
3567 /* The argument is a destination parameter, and no writemasks are allowed */
3568 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3569 if (ins->ctx->reg_maps->shader_version.major >= 2)
3570 {
3571 /* 2.0 shaders compare all 4 components in texkill */
3572 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
3573 } else {
3574 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3575 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3576 * 4 components are defined, only the first 3 are used
3577 */
3578 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
3579 }
3580}
3581
3582/** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3583 * dst = dot2(src0, src1) + src2 */
3584static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
3585{
3586 glsl_src_param_t src0_param;
3587 glsl_src_param_t src1_param;
3588 glsl_src_param_t src2_param;
3589 DWORD write_mask;
3590 unsigned int mask_size;
3591
3592 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3593 mask_size = shader_glsl_get_write_mask_size(write_mask);
3594
3595 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3596 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3597 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
3598
3599 if (mask_size > 1) {
3600 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
3601 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
3602 } else {
3603 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
3604 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3605 }
3606}
3607
3608static void shader_glsl_input_pack(IWineD3DPixelShader *iface, struct wined3d_shader_buffer *buffer,
3609 const struct wined3d_shader_signature_element *input_signature, const struct shader_reg_maps *reg_maps,
3610 enum vertexprocessing_mode vertexprocessing)
3611{
3612 unsigned int i;
3613 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3614 WORD map = reg_maps->input_registers;
3615
3616 for (i = 0; map; map >>= 1, ++i)
3617 {
3618 const char *semantic_name;
3619 UINT semantic_idx;
3620 char reg_mask[6];
3621
3622 /* Unused */
3623 if (!(map & 1)) continue;
3624
3625 semantic_name = input_signature[i].semantic_name;
3626 semantic_idx = input_signature[i].semantic_idx;
3627 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
3628
3629 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3630 {
3631 if (semantic_idx < 8 && vertexprocessing == pretransformed)
3632 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3633 This->input_reg_map[i], reg_mask, semantic_idx, reg_mask);
3634 else
3635 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3636 This->input_reg_map[i], reg_mask, reg_mask);
3637 }
3638 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3639 {
3640 if (semantic_idx == 0)
3641 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
3642 This->input_reg_map[i], reg_mask, reg_mask);
3643 else if (semantic_idx == 1)
3644 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3645 This->input_reg_map[i], reg_mask, reg_mask);
3646 else
3647 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3648 This->input_reg_map[i], reg_mask, reg_mask);
3649 }
3650 else
3651 {
3652 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3653 This->input_reg_map[i], reg_mask, reg_mask);
3654 }
3655 }
3656}
3657
3658/*********************************************
3659 * Vertex Shader Specific Code begins here
3660 ********************************************/
3661
3662static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
3663 glsl_program_key_t key;
3664
3665 key.vshader = entry->vshader;
3666 key.pshader = entry->pshader;
3667 key.vs_args = entry->vs_args;
3668 key.ps_args = entry->ps_args;
3669
3670 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
3671 {
3672 ERR("Failed to insert program entry.\n");
3673 }
3674}
3675
3676static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
3677 IWineD3DVertexShader *vshader, IWineD3DPixelShader *pshader, struct vs_compile_args *vs_args,
3678 struct ps_compile_args *ps_args) {
3679 struct wine_rb_entry *entry;
3680 glsl_program_key_t key;
3681
3682 key.vshader = vshader;
3683 key.pshader = pshader;
3684 key.vs_args = *vs_args;
3685 key.ps_args = *ps_args;
3686
3687 entry = wine_rb_get(&priv->program_lookup, &key);
3688 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
3689}
3690
3691/* GL locking is done by the caller */
3692static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
3693 struct glsl_shader_prog_link *entry)
3694{
3695 glsl_program_key_t key;
3696
3697 key.vshader = entry->vshader;
3698 key.pshader = entry->pshader;
3699 key.vs_args = entry->vs_args;
3700 key.ps_args = entry->ps_args;
3701 wine_rb_remove(&priv->program_lookup, &key);
3702
3703 GL_EXTCALL(glDeleteObjectARB(entry->programId));
3704 if (entry->vshader) list_remove(&entry->vshader_entry);
3705 if (entry->pshader) list_remove(&entry->pshader_entry);
3706 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
3707 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
3708 HeapFree(GetProcessHeap(), 0, entry);
3709}
3710
3711static void handle_ps3_input(struct wined3d_shader_buffer *buffer, const struct wined3d_gl_info *gl_info, const DWORD *map,
3712 const struct wined3d_shader_signature_element *input_signature, const struct shader_reg_maps *reg_maps_in,
3713 const struct wined3d_shader_signature_element *output_signature, const struct shader_reg_maps *reg_maps_out)
3714{
3715 unsigned int i, j;
3716 const char *semantic_name_in, *semantic_name_out;
3717 UINT semantic_idx_in, semantic_idx_out;
3718 DWORD *set;
3719 DWORD in_idx;
3720 unsigned int in_count = vec4_varyings(3, gl_info);
3721 char reg_mask[6], reg_mask_out[6];
3722 char destination[50];
3723 WORD input_map, output_map;
3724
3725 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
3726
3727 if (!output_signature)
3728 {
3729 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
3730 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
3731 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
3732 }
3733
3734 input_map = reg_maps_in->input_registers;
3735 for (i = 0; input_map; input_map >>= 1, ++i)
3736 {
3737 if (!(input_map & 1)) continue;
3738
3739 in_idx = map[i];
3740 if (in_idx >= (in_count + 2)) {
3741 FIXME("More input varyings declared than supported, expect issues\n");
3742 continue;
3743 }
3744 else if (map[i] == ~0U)
3745 {
3746 /* Declared, but not read register */
3747 continue;
3748 }
3749
3750 if (in_idx == in_count) {
3751 sprintf(destination, "gl_FrontColor");
3752 } else if (in_idx == in_count + 1) {
3753 sprintf(destination, "gl_FrontSecondaryColor");
3754 } else {
3755 sprintf(destination, "IN[%u]", in_idx);
3756 }
3757
3758 semantic_name_in = input_signature[i].semantic_name;
3759 semantic_idx_in = input_signature[i].semantic_idx;
3760 set[map[i]] = input_signature[i].mask;
3761 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
3762
3763 if (!output_signature)
3764 {
3765 if (shader_match_semantic(semantic_name_in, WINED3DDECLUSAGE_COLOR))
3766 {
3767 if (semantic_idx_in == 0)
3768 shader_addline(buffer, "%s%s = front_color%s;\n",
3769 destination, reg_mask, reg_mask);
3770 else if (semantic_idx_in == 1)
3771 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
3772 destination, reg_mask, reg_mask);
3773 else
3774 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3775 destination, reg_mask, reg_mask);
3776 }
3777 else if (shader_match_semantic(semantic_name_in, WINED3DDECLUSAGE_TEXCOORD))
3778 {
3779 if (semantic_idx_in < 8)
3780 {
3781 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
3782 destination, reg_mask, semantic_idx_in, reg_mask);
3783 }
3784 else
3785 {
3786 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3787 destination, reg_mask, reg_mask);
3788 }
3789 }
3790 else if (shader_match_semantic(semantic_name_in, WINED3DDECLUSAGE_FOG))
3791 {
3792 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3793 destination, reg_mask, reg_mask);
3794 }
3795 else
3796 {
3797 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3798 destination, reg_mask, reg_mask);
3799 }
3800 } else {
3801 BOOL found = FALSE;
3802
3803 output_map = reg_maps_out->output_registers;
3804 for (j = 0; output_map; output_map >>= 1, ++j)
3805 {
3806 if (!(output_map & 1)) continue;
3807
3808 semantic_name_out = output_signature[j].semantic_name;
3809 semantic_idx_out = output_signature[j].semantic_idx;
3810 shader_glsl_write_mask_to_str(output_signature[j].mask, reg_mask_out);
3811
3812 if (semantic_idx_in == semantic_idx_out
3813 && !strcmp(semantic_name_in, semantic_name_out))
3814 {
3815 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3816 destination, reg_mask, j, reg_mask);
3817 found = TRUE;
3818 }
3819 }
3820 if(!found) {
3821 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3822 destination, reg_mask, reg_mask);
3823 }
3824 }
3825 }
3826
3827 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3828 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3829 * input varyings are assigned above, if the optimizer works properly.
3830 */
3831 for(i = 0; i < in_count + 2; i++) {
3832 if (set[i] && set[i] != WINED3DSP_WRITEMASK_ALL)
3833 {
3834 unsigned int size = 0;
3835 memset(reg_mask, 0, sizeof(reg_mask));
3836 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
3837 reg_mask[size] = 'x';
3838 size++;
3839 }
3840 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
3841 reg_mask[size] = 'y';
3842 size++;
3843 }
3844 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
3845 reg_mask[size] = 'z';
3846 size++;
3847 }
3848 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
3849 reg_mask[size] = 'w';
3850 size++;
3851 }
3852
3853 if (i == in_count) {
3854 sprintf(destination, "gl_FrontColor");
3855 } else if (i == in_count + 1) {
3856 sprintf(destination, "gl_FrontSecondaryColor");
3857 } else {
3858 sprintf(destination, "IN[%u]", i);
3859 }
3860
3861 if (size == 1) {
3862 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3863 } else {
3864 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3865 }
3866 }
3867 }
3868
3869 HeapFree(GetProcessHeap(), 0, set);
3870}
3871
3872/* GL locking is done by the caller */
3873static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
3874 IWineD3DVertexShader *vertexshader, IWineD3DPixelShader *pixelshader, const struct wined3d_gl_info *gl_info)
3875{
3876 GLhandleARB ret = 0;
3877 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3878 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3879 IWineD3DDeviceImpl *device;
3880 DWORD vs_major = vs->baseShader.reg_maps.shader_version.major;
3881 DWORD ps_major = ps ? ps->baseShader.reg_maps.shader_version.major : 0;
3882 unsigned int i;
3883 const char *semantic_name;
3884 UINT semantic_idx;
3885 char reg_mask[6];
3886 const struct wined3d_shader_signature_element *output_signature;
3887
3888 shader_buffer_clear(buffer);
3889
3890 shader_addline(buffer, "#version 120\n");
3891
3892 if(vs_major < 3 && ps_major < 3) {
3893 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3894 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3895 */
3896 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3897 if ((gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W)
3898 && ps_major == 0 && vs_major > 0 && !device->frag_pipe->ffp_proj_control)
3899 {
3900 shader_addline(buffer, "void order_ps_input() {\n");
3901 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3902 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3903 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3904 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3905 }
3906 }
3907 shader_addline(buffer, "}\n");
3908 } else {
3909 shader_addline(buffer, "void order_ps_input() { /* do nothing */ }\n");
3910 }
3911 } else if(ps_major < 3 && vs_major >= 3) {
3912 WORD map = vs->baseShader.reg_maps.output_registers;
3913
3914 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3915 output_signature = vs->baseShader.output_signature;
3916
3917 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3918 for (i = 0; map; map >>= 1, ++i)
3919 {
3920 DWORD write_mask;
3921
3922 if (!(map & 1)) continue;
3923
3924 semantic_name = output_signature[i].semantic_name;
3925 semantic_idx = output_signature[i].semantic_idx;
3926 write_mask = output_signature[i].mask;
3927 shader_glsl_write_mask_to_str(write_mask, reg_mask);
3928
3929 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3930 {
3931 if (semantic_idx == 0)
3932 shader_addline(buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3933 else if (semantic_idx == 1)
3934 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3935 }
3936 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3937 {
3938 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3939 }
3940 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3941 {
3942 if (semantic_idx < 8)
3943 {
3944 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
3945 write_mask |= WINED3DSP_WRITEMASK_3;
3946
3947 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3948 semantic_idx, reg_mask, i, reg_mask);
3949 if (!(write_mask & WINED3DSP_WRITEMASK_3))
3950 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
3951 }
3952 }
3953 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3954 {
3955 shader_addline(buffer, "gl_PointSize = OUT[%u].x;\n", i);
3956 }
3957 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3958 {
3959 shader_addline(buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3960 }
3961 }
3962 shader_addline(buffer, "}\n");
3963
3964 } else if(ps_major >= 3 && vs_major >= 3) {
3965 WORD map = vs->baseShader.reg_maps.output_registers;
3966
3967 output_signature = vs->baseShader.output_signature;
3968
3969 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3970 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info));
3971 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3972
3973 /* First, sort out position and point size. Those are not passed to the pixel shader */
3974 for (i = 0; map; map >>= 1, ++i)
3975 {
3976 if (!(map & 1)) continue;
3977
3978 semantic_name = output_signature[i].semantic_name;
3979 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
3980
3981 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3982 {
3983 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3984 }
3985 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3986 {
3987 shader_addline(buffer, "gl_PointSize = OUT[%u].x;\n", i);
3988 }
3989 }
3990
3991 /* Then, fix the pixel shader input */
3992 handle_ps3_input(buffer, gl_info, ps->input_reg_map, ps->baseShader.input_signature,
3993 &ps->baseShader.reg_maps, output_signature, &vs->baseShader.reg_maps);
3994
3995 shader_addline(buffer, "}\n");
3996 } else if(ps_major >= 3 && vs_major < 3) {
3997 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info));
3998 shader_addline(buffer, "void order_ps_input() {\n");
3999 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
4000 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
4001 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
4002 */
4003 handle_ps3_input(buffer, gl_info, ps->input_reg_map, ps->baseShader.input_signature,
4004 &ps->baseShader.reg_maps, NULL, NULL);
4005 shader_addline(buffer, "}\n");
4006 } else {
4007 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
4008 }
4009
4010 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4011 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4012 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer->buffer, NULL));
4013 checkGLcall("glShaderSourceARB(ret, 1, &buffer->buffer, NULL)");
4014 GL_EXTCALL(glCompileShaderARB(ret));
4015 checkGLcall("glCompileShaderARB(ret)");
4016
4017 return ret;
4018}
4019
4020/* GL locking is done by the caller */
4021static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const struct wined3d_gl_info *gl_info,
4022 GLhandleARB programId, char prefix)
4023{
4024 const local_constant *lconst;
4025 GLint tmp_loc;
4026 const float *value;
4027 char glsl_name[8];
4028
4029 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
4030 value = (const float *)lconst->value;
4031 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
4032 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4033 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
4034 }
4035 checkGLcall("Hardcoding local constants");
4036}
4037
4038/* GL locking is done by the caller */
4039static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4040 struct wined3d_shader_buffer *buffer, IWineD3DPixelShaderImpl *This,
4041 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4042{
4043 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
4044 const struct wined3d_gl_info *gl_info = context->gl_info;
4045 CONST DWORD *function = This->baseShader.function;
4046 struct shader_glsl_ctx_priv priv_ctx;
4047
4048 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4049 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4050
4051 memset(&priv_ctx, 0, sizeof(priv_ctx));
4052 priv_ctx.cur_ps_args = args;
4053 priv_ctx.cur_np2fixup_info = np2fixup_info;
4054
4055 shader_addline(buffer, "#version 120\n");
4056
4057 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] && reg_maps->usestexldd)
4058 {
4059 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4060 }
4061 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4062 {
4063 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
4064 * drivers write a warning if we don't do so
4065 */
4066 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4067 }
4068 if (gl_info->supported[EXT_GPU_SHADER4])
4069 {
4070 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4071 }
4072
4073 /* Base Declarations */
4074 shader_generate_glsl_declarations(context, buffer, (IWineD3DBaseShader *)This, reg_maps, &priv_ctx);
4075
4076 /* Pack 3.0 inputs */
4077 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4078 {
4079 shader_glsl_input_pack((IWineD3DPixelShader *) This, buffer,
4080 This->baseShader.input_signature, reg_maps, args->vp_mode);
4081 }
4082
4083 /* Base Shader Body */
4084 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function, &priv_ctx);
4085
4086 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4087 if (reg_maps->shader_version.major < 2)
4088 {
4089 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4090 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4091 }
4092
4093 if (args->srgb_correction)
4094 {
4095 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4096 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4097 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4098 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4099 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4100 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4101 }
4102 /* Pixel shader < 3.0 do not replace the fog stage.
4103 * This implements linear fog computation and blending.
4104 * TODO: non linear fog
4105 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4106 * -1/(e-s) and e/(e-s) respectively.
4107 */
4108 if (reg_maps->shader_version.major < 3)
4109 {
4110 switch(args->fog) {
4111 case FOG_OFF: break;
4112 case FOG_LINEAR:
4113 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4114 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4115 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4116 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4117 break;
4118 case FOG_EXP:
4119 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4120 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4121 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4122 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4123 break;
4124 case FOG_EXP2:
4125 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4126 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4127 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4128 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4129 break;
4130 }
4131 }
4132
4133 shader_addline(buffer, "}\n");
4134
4135 TRACE("Compiling shader object %u\n", shader_obj);
4136 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4137 GL_EXTCALL(glCompileShaderARB(shader_obj));
4138 print_glsl_info_log(gl_info, shader_obj);
4139
4140 /* Store the shader object */
4141 return shader_obj;
4142}
4143
4144/* GL locking is done by the caller */
4145static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4146 struct wined3d_shader_buffer *buffer, IWineD3DVertexShaderImpl *This,
4147 const struct vs_compile_args *args)
4148{
4149 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
4150 const struct wined3d_gl_info *gl_info = context->gl_info;
4151 CONST DWORD *function = This->baseShader.function;
4152 struct shader_glsl_ctx_priv priv_ctx;
4153
4154 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4155 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4156
4157 shader_addline(buffer, "#version 120\n");
4158
4159 if (gl_info->supported[EXT_GPU_SHADER4])
4160 {
4161 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4162 }
4163
4164 memset(&priv_ctx, 0, sizeof(priv_ctx));
4165 priv_ctx.cur_vs_args = args;
4166
4167 /* Base Declarations */
4168 shader_generate_glsl_declarations(context, buffer, (IWineD3DBaseShader *)This, reg_maps, &priv_ctx);
4169
4170 /* Base Shader Body */
4171 shader_generate_main((IWineD3DBaseShader*)This, buffer, reg_maps, function, &priv_ctx);
4172
4173 /* Unpack 3.0 outputs */
4174 if (reg_maps->shader_version.major >= 3) shader_addline(buffer, "order_ps_input(OUT);\n");
4175 else shader_addline(buffer, "order_ps_input();\n");
4176
4177 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4178 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4179 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4180 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4181 */
4182 if(args->fog_src == VS_FOG_Z) {
4183 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4184 } else if (!reg_maps->fog) {
4185 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4186 }
4187
4188 /* Write the final position.
4189 *
4190 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4191 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4192 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4193 * contains 1.0 to allow a mad.
4194 */
4195 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4196 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4197 if(args->clip_enabled) {
4198 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4199 }
4200
4201 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4202 *
4203 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4204 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4205 * which is the same as z = z * 2 - w.
4206 */
4207 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4208
4209 shader_addline(buffer, "}\n");
4210
4211 TRACE("Compiling shader object %u\n", shader_obj);
4212 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4213 GL_EXTCALL(glCompileShaderARB(shader_obj));
4214 print_glsl_info_log(gl_info, shader_obj);
4215
4216 return shader_obj;
4217}
4218
4219static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4220 struct wined3d_shader_buffer *buffer, IWineD3DPixelShaderImpl *shader,
4221 const struct ps_compile_args *args,
4222#ifdef VBOX_WITH_WDDM
4223 UINT *inp2fixup_info
4224#else
4225 const struct ps_np2fixup_info **np2fixup_info
4226#endif
4227 )
4228{
4229 UINT i;
4230 DWORD new_size;
4231 struct glsl_ps_compiled_shader *new_array;
4232 struct glsl_pshader_private *shader_data;
4233 struct ps_np2fixup_info *np2fixup = NULL;
4234 GLhandleARB ret;
4235
4236 if (!shader->baseShader.backend_data)
4237 {
4238 shader->baseShader.backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4239 if (!shader->baseShader.backend_data)
4240 {
4241 ERR("Failed to allocate backend data.\n");
4242 return 0;
4243 }
4244 }
4245 shader_data = shader->baseShader.backend_data;
4246
4247 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4248 * so a linear search is more performant than a hashmap or a binary search
4249 * (cache coherency etc)
4250 */
4251 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4252 if(memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)) == 0) {
4253 if(args->np2_fixup) {
4254#ifdef VBOX_WITH_WDDM
4255 *inp2fixup_info = i;
4256#else
4257 *np2fixup_info = &shader_data->gl_shaders[i].np2fixup;
4258#endif
4259 }
4260 return shader_data->gl_shaders[i].prgId;
4261 }
4262 }
4263
4264 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4265 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4266 if (shader_data->num_gl_shaders)
4267 {
4268 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4269 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4270 new_size * sizeof(*shader_data->gl_shaders));
4271 } else {
4272 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4273 new_size = 1;
4274 }
4275
4276 if(!new_array) {
4277 ERR("Out of memory\n");
4278 return 0;
4279 }
4280 shader_data->gl_shaders = new_array;
4281 shader_data->shader_array_size = new_size;
4282 }
4283
4284 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4285
4286 memset(&shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup, 0, sizeof(struct ps_np2fixup_info));
4287 if (args->np2_fixup) np2fixup = &shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup;
4288
4289 pixelshader_update_samplers(&shader->baseShader.reg_maps,
4290 ((IWineD3DDeviceImpl *)shader->baseShader.device)->stateBlock->textures);
4291
4292 shader_buffer_clear(buffer);
4293 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4294#ifdef VBOX_WITH_WDDM
4295 *inp2fixup_info = shader_data->num_gl_shaders;
4296#else
4297 *np2fixup_info = np2fixup;
4298#endif
4299 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4300
4301 return ret;
4302}
4303
4304static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4305 const DWORD use_map) {
4306 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4307 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4308 return stored->fog_src == new->fog_src;
4309}
4310
4311static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4312 struct wined3d_shader_buffer *buffer, IWineD3DVertexShaderImpl *shader,
4313 const struct vs_compile_args *args)
4314{
4315 UINT i;
4316 DWORD new_size;
4317 struct glsl_vs_compiled_shader *new_array;
4318 DWORD use_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.use_map;
4319 struct glsl_vshader_private *shader_data;
4320 GLhandleARB ret;
4321
4322 if (!shader->baseShader.backend_data)
4323 {
4324 shader->baseShader.backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4325 if (!shader->baseShader.backend_data)
4326 {
4327 ERR("Failed to allocate backend data.\n");
4328 return 0;
4329 }
4330 }
4331 shader_data = shader->baseShader.backend_data;
4332
4333 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4334 * so a linear search is more performant than a hashmap or a binary search
4335 * (cache coherency etc)
4336 */
4337 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4338 if(vs_args_equal(&shader_data->gl_shaders[i].args, args, use_map)) {
4339 return shader_data->gl_shaders[i].prgId;
4340 }
4341 }
4342
4343 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4344
4345 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4346 if (shader_data->num_gl_shaders)
4347 {
4348 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4349 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4350 new_size * sizeof(*shader_data->gl_shaders));
4351 } else {
4352 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4353 new_size = 1;
4354 }
4355
4356 if(!new_array) {
4357 ERR("Out of memory\n");
4358 return 0;
4359 }
4360 shader_data->gl_shaders = new_array;
4361 shader_data->shader_array_size = new_size;
4362 }
4363
4364 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4365
4366 shader_buffer_clear(buffer);
4367 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4368 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4369
4370 return ret;
4371}
4372
4373/** Sets the GLSL program ID for the given pixel and vertex shader combination.
4374 * It sets the programId on the current StateBlock (because it should be called
4375 * inside of the DrawPrimitive() part of the render loop).
4376 *
4377 * If a program for the given combination does not exist, create one, and store
4378 * the program in the hash table. If it creates a program, it will link the
4379 * given objects, too.
4380 */
4381
4382/* GL locking is done by the caller */
4383static void set_glsl_shader_program(const struct wined3d_context *context,
4384 IWineD3DDeviceImpl *device, BOOL use_ps, BOOL use_vs)
4385{
4386 IWineD3DVertexShader *vshader = use_vs ? device->stateBlock->vertexShader : NULL;
4387 IWineD3DPixelShader *pshader = use_ps ? device->stateBlock->pixelShader : NULL;
4388 const struct wined3d_gl_info *gl_info = context->gl_info;
4389 struct shader_glsl_priv *priv = device->shader_priv;
4390 struct glsl_shader_prog_link *entry = NULL;
4391 GLhandleARB programId = 0;
4392 GLhandleARB reorder_shader_id = 0;
4393 unsigned int i;
4394 char glsl_name[8];
4395 struct ps_compile_args ps_compile_args;
4396 struct vs_compile_args vs_compile_args;
4397
4398 if (vshader) find_vs_compile_args((IWineD3DVertexShaderImpl *)vshader, device->stateBlock, &vs_compile_args);
4399 if (pshader) find_ps_compile_args((IWineD3DPixelShaderImpl *)pshader, device->stateBlock, &ps_compile_args);
4400
4401 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args);
4402 if (entry) {
4403 priv->glsl_program = entry;
4404 return;
4405 }
4406
4407 /* If we get to this point, then no matching program exists, so we create one */
4408 programId = GL_EXTCALL(glCreateProgramObjectARB());
4409 TRACE("Created new GLSL shader program %u\n", programId);
4410
4411 /* Create the entry */
4412 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
4413 entry->programId = programId;
4414 entry->vshader = vshader;
4415 entry->pshader = pshader;
4416 entry->vs_args = vs_compile_args;
4417 entry->ps_args = ps_compile_args;
4418 entry->constant_version = 0;
4419 WINEFIXUPINFO_INIT(entry);
4420 /* Add the hash table entry */
4421 add_glsl_program_entry(priv, entry);
4422
4423 /* Set the current program */
4424 priv->glsl_program = entry;
4425
4426 /* Attach GLSL vshader */
4427 if (vshader)
4428 {
4429 GLhandleARB vshader_id = find_glsl_vshader(context, &priv->shader_buffer,
4430 (IWineD3DVertexShaderImpl *)vshader, &vs_compile_args);
4431 WORD map = ((IWineD3DBaseShaderImpl *)vshader)->baseShader.reg_maps.input_registers;
4432 char tmp_name[10];
4433
4434 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
4435 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
4436 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
4437 checkGLcall("glAttachObjectARB");
4438 /* Flag the reorder function for deletion, then it will be freed automatically when the program
4439 * is destroyed
4440 */
4441 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
4442
4443 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
4444 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
4445 checkGLcall("glAttachObjectARB");
4446
4447 /* Bind vertex attributes to a corresponding index number to match
4448 * the same index numbers as ARB_vertex_programs (makes loading
4449 * vertex attributes simpler). With this method, we can use the
4450 * exact same code to load the attributes later for both ARB and
4451 * GLSL shaders.
4452 *
4453 * We have to do this here because we need to know the Program ID
4454 * in order to make the bindings work, and it has to be done prior
4455 * to linking the GLSL program. */
4456 for (i = 0; map; map >>= 1, ++i)
4457 {
4458 if (!(map & 1)) continue;
4459
4460 snprintf(tmp_name, sizeof(tmp_name), "attrib%u", i);
4461 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
4462 }
4463 checkGLcall("glBindAttribLocationARB");
4464
4465 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
4466 }
4467
4468 /* Attach GLSL pshader */
4469 if (pshader)
4470 {
4471 GLhandleARB pshader_id = find_glsl_pshader(context, &priv->shader_buffer,
4472 (IWineD3DPixelShaderImpl *)pshader, &ps_compile_args,
4473#ifdef VBOX_WITH_WDDM
4474 &entry->inp2Fixup_info
4475#else
4476 &entry->np2Fixup_info
4477#endif
4478 );
4479 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
4480 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
4481 checkGLcall("glAttachObjectARB");
4482
4483 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
4484 }
4485
4486 /* Link the program */
4487 TRACE("Linking GLSL shader program %u\n", programId);
4488 GL_EXTCALL(glLinkProgramARB(programId));
4489 shader_glsl_validate_link(gl_info, programId);
4490
4491 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4492 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
4493 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
4494 {
4495 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
4496 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4497 }
4498 for (i = 0; i < MAX_CONST_I; ++i)
4499 {
4500 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
4501 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4502 }
4503 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4504 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
4505 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
4506 {
4507 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
4508 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4509 }
4510 for (i = 0; i < MAX_CONST_I; ++i)
4511 {
4512 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
4513 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4514 }
4515
4516 if(pshader) {
4517 char name[32];
4518
4519 for(i = 0; i < MAX_TEXTURES; i++) {
4520 sprintf(name, "bumpenvmat%u", i);
4521 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4522 sprintf(name, "luminancescale%u", i);
4523 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4524 sprintf(name, "luminanceoffset%u", i);
4525 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4526 }
4527
4528 if (ps_compile_args.np2_fixup) {
4529 if (WINEFIXUPINFO_ISVALID(entry)) {
4530 entry->np2Fixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "PsamplerNP2Fixup"));
4531 } else {
4532 FIXME("NP2 texcoord fixup needed for this pixelshader, but no fixup uniform found.\n");
4533 }
4534 }
4535 }
4536
4537 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
4538 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
4539 checkGLcall("Find glsl program uniform locations");
4540
4541 if (pshader
4542 && ((IWineD3DPixelShaderImpl *)pshader)->baseShader.reg_maps.shader_version.major >= 3
4543 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > vec4_varyings(3, gl_info))
4544 {
4545 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
4546 entry->vertex_color_clamp = GL_FALSE;
4547 } else {
4548 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
4549 }
4550
4551 /* Set the shader to allow uniform loading on it */
4552 GL_EXTCALL(glUseProgramObjectARB(programId));
4553 checkGLcall("glUseProgramObjectARB(programId)");
4554
4555 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4556 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4557 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4558 * vertex shader with fixed function pixel processing is used we make sure that the card
4559 * supports enough samplers to allow the max number of vertex samplers with all possible
4560 * fixed function fragment processing setups. So once the program is linked these samplers
4561 * won't change.
4562 */
4563 if (vshader) shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
4564 if (pshader) shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
4565
4566 /* If the local constants do not have to be loaded with the environment constants,
4567 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4568 * later
4569 */
4570 if (pshader && !((IWineD3DBaseShaderImpl *)pshader)->baseShader.load_local_constsF)
4571 {
4572 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
4573 }
4574 if (vshader && !((IWineD3DBaseShaderImpl *)vshader)->baseShader.load_local_constsF)
4575 {
4576 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
4577 }
4578}
4579
4580/* GL locking is done by the caller */
4581static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type)
4582{
4583 GLhandleARB program_id;
4584 GLhandleARB vshader_id, pshader_id;
4585 static const char *blt_vshader[] =
4586 {
4587 "#version 120\n"
4588 "void main(void)\n"
4589 "{\n"
4590 " gl_Position = gl_Vertex;\n"
4591 " gl_FrontColor = vec4(1.0);\n"
4592 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4593 "}\n"
4594 };
4595
4596 static const char *blt_pshaders[tex_type_count] =
4597 {
4598 /* tex_1d */
4599 NULL,
4600 /* tex_2d */
4601 "#version 120\n"
4602 "uniform sampler2D sampler;\n"
4603 "void main(void)\n"
4604 "{\n"
4605 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4606 "}\n",
4607 /* tex_3d */
4608 NULL,
4609 /* tex_cube */
4610 "#version 120\n"
4611 "uniform samplerCube sampler;\n"
4612 "void main(void)\n"
4613 "{\n"
4614 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4615 "}\n",
4616 /* tex_rect */
4617 "#version 120\n"
4618 "#extension GL_ARB_texture_rectangle : enable\n"
4619 "uniform sampler2DRect sampler;\n"
4620 "void main(void)\n"
4621 "{\n"
4622 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4623 "}\n",
4624 };
4625
4626 if (!blt_pshaders[tex_type])
4627 {
4628 FIXME("tex_type %#x not supported\n", tex_type);
4629 tex_type = tex_2d;
4630 }
4631
4632 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4633 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
4634 GL_EXTCALL(glCompileShaderARB(vshader_id));
4635
4636 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4637 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
4638 GL_EXTCALL(glCompileShaderARB(pshader_id));
4639
4640 program_id = GL_EXTCALL(glCreateProgramObjectARB());
4641 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
4642 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
4643 GL_EXTCALL(glLinkProgramARB(program_id));
4644
4645 shader_glsl_validate_link(gl_info, program_id);
4646
4647 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4648 * is destroyed
4649 */
4650 GL_EXTCALL(glDeleteObjectARB(vshader_id));
4651 GL_EXTCALL(glDeleteObjectARB(pshader_id));
4652 return program_id;
4653}
4654
4655/* GL locking is done by the caller */
4656static void shader_glsl_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
4657{
4658 const struct wined3d_gl_info *gl_info = context->gl_info;
4659#ifdef VBOX_WITH_WDDM
4660 IWineD3DDeviceImpl *device = context->device;
4661#else
4662 IWineD3DDeviceImpl *device = context->swapchain->device;
4663#endif
4664 struct shader_glsl_priv *priv = device->shader_priv;
4665 GLhandleARB program_id = 0;
4666 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
4667
4668 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4669
4670 if (useVS || usePS) set_glsl_shader_program(context, device, usePS, useVS);
4671 else priv->glsl_program = NULL;
4672
4673 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4674
4675 if (old_vertex_color_clamp != current_vertex_color_clamp)
4676 {
4677 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
4678 {
4679 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
4680 checkGLcall("glClampColorARB");
4681 }
4682 else
4683 {
4684 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4685 }
4686 }
4687
4688 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4689 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4690 GL_EXTCALL(glUseProgramObjectARB(program_id));
4691 checkGLcall("glUseProgramObjectARB");
4692
4693 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
4694 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
4695 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
4696 if (priv->glsl_program && WINEFIXUPINFO_ISVALID(priv->glsl_program))
4697 {
4698 shader_glsl_load_np2fixup_constants((IWineD3DDevice *)device, usePS, useVS);
4699 }
4700}
4701
4702/* GL locking is done by the caller */
4703static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
4704 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4705 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4706 struct shader_glsl_priv *priv = This->shader_priv;
4707 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
4708
4709 if (!*blt_program) {
4710 GLint loc;
4711 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
4712 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
4713 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4714 GL_EXTCALL(glUniform1iARB(loc, 0));
4715 } else {
4716 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4717 }
4718}
4719
4720/* GL locking is done by the caller */
4721static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
4722 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4723 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4724 struct shader_glsl_priv *priv = This->shader_priv;
4725 GLhandleARB program_id;
4726
4727 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4728 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4729
4730 GL_EXTCALL(glUseProgramObjectARB(program_id));
4731 checkGLcall("glUseProgramObjectARB");
4732}
4733
4734static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
4735 const struct list *linked_programs;
4736 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
4737 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
4738 struct shader_glsl_priv *priv = device->shader_priv;
4739 const struct wined3d_gl_info *gl_info;
4740 struct wined3d_context *context;
4741
4742 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
4743 * can be called from IWineD3DBaseShader::Release
4744 */
4745 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
4746
4747 if(pshader) {
4748 struct glsl_pshader_private *shader_data;
4749 shader_data = This->baseShader.backend_data;
4750 if(!shader_data || shader_data->num_gl_shaders == 0)
4751 {
4752 HeapFree(GetProcessHeap(), 0, shader_data);
4753 This->baseShader.backend_data = NULL;
4754 return;
4755 }
4756
4757 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4758 gl_info = context->gl_info;
4759
4760 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->pshader == iface)
4761 {
4762 ENTER_GL();
4763 shader_glsl_select(context, FALSE, FALSE);
4764 LEAVE_GL();
4765 }
4766 } else {
4767 struct glsl_vshader_private *shader_data;
4768 shader_data = This->baseShader.backend_data;
4769 if(!shader_data || shader_data->num_gl_shaders == 0)
4770 {
4771 HeapFree(GetProcessHeap(), 0, shader_data);
4772 This->baseShader.backend_data = NULL;
4773 return;
4774 }
4775
4776 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4777 gl_info = context->gl_info;
4778
4779 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->vshader == iface)
4780 {
4781 ENTER_GL();
4782 shader_glsl_select(context, FALSE, FALSE);
4783 LEAVE_GL();
4784 }
4785 }
4786
4787 linked_programs = &This->baseShader.linked_programs;
4788
4789 TRACE("Deleting linked programs\n");
4790 if (linked_programs->next) {
4791 struct glsl_shader_prog_link *entry, *entry2;
4792
4793 ENTER_GL();
4794 if(pshader) {
4795 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
4796 delete_glsl_program_entry(priv, gl_info, entry);
4797 }
4798 } else {
4799 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
4800 delete_glsl_program_entry(priv, gl_info, entry);
4801 }
4802 }
4803 LEAVE_GL();
4804 }
4805
4806 if(pshader) {
4807 UINT i;
4808 struct glsl_pshader_private *shader_data = This->baseShader.backend_data;
4809
4810 ENTER_GL();
4811 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4812 TRACE("deleting pshader %u\n", shader_data->gl_shaders[i].prgId);
4813 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4814 checkGLcall("glDeleteObjectARB");
4815 }
4816 LEAVE_GL();
4817 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4818 }
4819 else
4820 {
4821 UINT i;
4822 struct glsl_vshader_private *shader_data = This->baseShader.backend_data;
4823
4824 ENTER_GL();
4825 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4826 TRACE("deleting vshader %u\n", shader_data->gl_shaders[i].prgId);
4827 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4828 checkGLcall("glDeleteObjectARB");
4829 }
4830 LEAVE_GL();
4831 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4832 }
4833
4834 HeapFree(GetProcessHeap(), 0, This->baseShader.backend_data);
4835 This->baseShader.backend_data = NULL;
4836
4837 context_release(context);
4838}
4839
4840static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
4841{
4842 const glsl_program_key_t *k = key;
4843 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
4844 const struct glsl_shader_prog_link, program_lookup_entry);
4845 int cmp;
4846
4847 if (k->vshader > prog->vshader) return 1;
4848 else if (k->vshader < prog->vshader) return -1;
4849
4850 if (k->pshader > prog->pshader) return 1;
4851 else if (k->pshader < prog->pshader) return -1;
4852
4853 if (k->vshader && (cmp = memcmp(&k->vs_args, &prog->vs_args, sizeof(prog->vs_args)))) return cmp;
4854 if (k->pshader && (cmp = memcmp(&k->ps_args, &prog->ps_args, sizeof(prog->ps_args)))) return cmp;
4855
4856 return 0;
4857}
4858
4859static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
4860{
4861 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
4862 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
4863
4864 if (!mem)
4865 {
4866 ERR("Failed to allocate memory\n");
4867 return FALSE;
4868 }
4869
4870 heap->entries = mem;
4871 heap->entries[1].version = 0;
4872 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
4873 heap->size = 1;
4874
4875 return TRUE;
4876}
4877
4878static void constant_heap_free(struct constant_heap *heap)
4879{
4880 HeapFree(GetProcessHeap(), 0, heap->entries);
4881}
4882
4883static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
4884{
4885 wined3d_rb_alloc,
4886 wined3d_rb_realloc,
4887 wined3d_rb_free,
4888 glsl_program_key_compare,
4889};
4890
4891static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
4892 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4893 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4894 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
4895 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
4896 gl_info->limits.glsl_ps_float_constants)) + 1;
4897
4898 if (!shader_buffer_init(&priv->shader_buffer))
4899 {
4900 ERR("Failed to initialize shader buffer.\n");
4901 goto fail;
4902 }
4903
4904 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
4905 if (!priv->stack)
4906 {
4907 ERR("Failed to allocate memory.\n");
4908 goto fail;
4909 }
4910
4911 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
4912 {
4913 ERR("Failed to initialize vertex shader constant heap\n");
4914 goto fail;
4915 }
4916
4917 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
4918 {
4919 ERR("Failed to initialize pixel shader constant heap\n");
4920 goto fail;
4921 }
4922
4923 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
4924 {
4925 ERR("Failed to initialize rbtree.\n");
4926 goto fail;
4927 }
4928
4929 priv->next_constant_version = 1;
4930
4931 This->shader_priv = priv;
4932 return WINED3D_OK;
4933
4934fail:
4935 constant_heap_free(&priv->pconst_heap);
4936 constant_heap_free(&priv->vconst_heap);
4937 HeapFree(GetProcessHeap(), 0, priv->stack);
4938 shader_buffer_free(&priv->shader_buffer);
4939 HeapFree(GetProcessHeap(), 0, priv);
4940 return E_OUTOFMEMORY;
4941}
4942
4943/* Context activation is done by the caller. */
4944static void shader_glsl_free(IWineD3DDevice *iface) {
4945 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4946 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4947 struct shader_glsl_priv *priv = This->shader_priv;
4948 int i;
4949
4950 ENTER_GL();
4951 for (i = 0; i < tex_type_count; ++i)
4952 {
4953 if (priv->depth_blt_program[i])
4954 {
4955 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
4956 }
4957 }
4958 LEAVE_GL();
4959
4960 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
4961 constant_heap_free(&priv->pconst_heap);
4962 constant_heap_free(&priv->vconst_heap);
4963 HeapFree(GetProcessHeap(), 0, priv->stack);
4964 shader_buffer_free(&priv->shader_buffer);
4965
4966 HeapFree(GetProcessHeap(), 0, This->shader_priv);
4967 This->shader_priv = NULL;
4968}
4969
4970static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
4971 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
4972 return FALSE;
4973}
4974
4975static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *pCaps)
4976{
4977 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
4978 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support based
4979 * on the version of NV_vertex_program.
4980 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
4981 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
4982 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
4983 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
4984 */
4985 if ((gl_info->supported[NV_VERTEX_PROGRAM2] && !gl_info->supported[NV_VERTEX_PROGRAM3])
4986 || gl_info->limits.arb_ps_instructions <= 512)
4987 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
4988 else
4989 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
4990 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
4991 pCaps->MaxVertexShaderConst = gl_info->limits.glsl_vs_float_constants;
4992
4993 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
4994 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
4995 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
4996 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
4997 * in max native instructions. Intel and others also offer the info in this extension but they
4998 * don't support GLSL (at least on Windows).
4999 *
5000 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
5001 * of instructions is 512 or less we have to do with ps2.0 hardware.
5002 * 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.
5003 */
5004 if ((gl_info->supported[NV_FRAGMENT_PROGRAM] && !gl_info->supported[NV_FRAGMENT_PROGRAM2])
5005 || gl_info->limits.arb_ps_instructions <= 512)
5006 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
5007 else
5008 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
5009
5010 pCaps->MaxPixelShaderConst = gl_info->limits.glsl_ps_float_constants;
5011
5012 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
5013 * Direct3D minimum requirement.
5014 *
5015 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
5016 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
5017 *
5018 * The problem is that the refrast clamps temporary results in the shader to
5019 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
5020 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
5021 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
5022 * offer a way to query this.
5023 */
5024 pCaps->PixelShader1xMaxValue = 8.0;
5025 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
5026
5027 pCaps->VSClipping = TRUE;
5028}
5029
5030static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
5031{
5032 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
5033 {
5034 TRACE("Checking support for fixup:\n");
5035 dump_color_fixup_desc(fixup);
5036 }
5037
5038 /* We support everything except YUV conversions. */
5039 if (!is_complex_fixup(fixup))
5040 {
5041 TRACE("[OK]\n");
5042 return TRUE;
5043 }
5044
5045 TRACE("[FAILED]\n");
5046 return FALSE;
5047}
5048
5049static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
5050{
5051 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
5052 /* WINED3DSIH_ADD */ shader_glsl_arith,
5053 /* WINED3DSIH_BEM */ shader_glsl_bem,
5054 /* WINED3DSIH_BREAK */ shader_glsl_break,
5055 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
5056 /* WINED3DSIH_BREAKP */ NULL,
5057 /* WINED3DSIH_CALL */ shader_glsl_call,
5058 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
5059 /* WINED3DSIH_CMP */ shader_glsl_cmp,
5060 /* WINED3DSIH_CND */ shader_glsl_cnd,
5061 /* WINED3DSIH_CRS */ shader_glsl_cross,
5062 /* WINED3DSIH_CUT */ NULL,
5063 /* WINED3DSIH_DCL */ NULL,
5064 /* WINED3DSIH_DEF */ NULL,
5065 /* WINED3DSIH_DEFB */ NULL,
5066 /* WINED3DSIH_DEFI */ NULL,
5067 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
5068 /* WINED3DSIH_DP3 */ shader_glsl_dot,
5069 /* WINED3DSIH_DP4 */ shader_glsl_dot,
5070 /* WINED3DSIH_DST */ shader_glsl_dst,
5071 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
5072 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
5073 /* WINED3DSIH_ELSE */ shader_glsl_else,
5074 /* WINED3DSIH_EMIT */ NULL,
5075 /* WINED3DSIH_ENDIF */ shader_glsl_end,
5076 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
5077 /* WINED3DSIH_ENDREP */ shader_glsl_end,
5078 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
5079 /* WINED3DSIH_EXPP */ shader_glsl_expp,
5080 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
5081 /* WINED3DSIH_IADD */ NULL,
5082 /* WINED3DSIH_IF */ shader_glsl_if,
5083 /* WINED3DSIH_IFC */ shader_glsl_ifc,
5084 /* WINED3DSIH_IGE */ NULL,
5085 /* WINED3DSIH_LABEL */ shader_glsl_label,
5086 /* WINED3DSIH_LIT */ shader_glsl_lit,
5087 /* WINED3DSIH_LOG */ shader_glsl_log,
5088 /* WINED3DSIH_LOGP */ shader_glsl_log,
5089 /* WINED3DSIH_LOOP */ shader_glsl_loop,
5090 /* WINED3DSIH_LRP */ shader_glsl_lrp,
5091 /* WINED3DSIH_LT */ NULL,
5092 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
5093 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
5094 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
5095 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
5096 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
5097 /* WINED3DSIH_MAD */ shader_glsl_mad,
5098 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
5099 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
5100 /* WINED3DSIH_MOV */ shader_glsl_mov,
5101 /* WINED3DSIH_MOVA */ shader_glsl_mov,
5102 /* WINED3DSIH_MUL */ shader_glsl_arith,
5103 /* WINED3DSIH_NOP */ NULL,
5104 /* WINED3DSIH_NRM */ shader_glsl_nrm,
5105 /* WINED3DSIH_PHASE */ NULL,
5106 /* WINED3DSIH_POW */ shader_glsl_pow,
5107 /* WINED3DSIH_RCP */ shader_glsl_rcp,
5108 /* WINED3DSIH_REP */ shader_glsl_rep,
5109 /* WINED3DSIH_RET */ shader_glsl_ret,
5110 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
5111 /* WINED3DSIH_SETP */ NULL,
5112 /* WINED3DSIH_SGE */ shader_glsl_compare,
5113 /* WINED3DSIH_SGN */ shader_glsl_sgn,
5114 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
5115 /* WINED3DSIH_SLT */ shader_glsl_compare,
5116 /* WINED3DSIH_SUB */ shader_glsl_arith,
5117 /* WINED3DSIH_TEX */ shader_glsl_tex,
5118 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
5119 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
5120 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
5121 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
5122 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
5123 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
5124 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
5125 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
5126 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
5127 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
5128 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
5129 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
5130 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
5131 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
5132 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
5133 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
5134 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
5135 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
5136 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
5137 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
5138 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
5139};
5140
5141static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
5142 SHADER_HANDLER hw_fct;
5143
5144 /* Select handler */
5145 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
5146
5147 /* Unhandled opcode */
5148 if (!hw_fct)
5149 {
5150 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
5151 return;
5152 }
5153 hw_fct(ins);
5154
5155 shader_glsl_add_instruction_modifiers(ins);
5156}
5157
5158const shader_backend_t glsl_shader_backend = {
5159 shader_glsl_handle_instruction,
5160 shader_glsl_select,
5161 shader_glsl_select_depth_blt,
5162 shader_glsl_deselect_depth_blt,
5163 shader_glsl_update_float_vertex_constants,
5164 shader_glsl_update_float_pixel_constants,
5165 shader_glsl_load_constants,
5166 shader_glsl_load_np2fixup_constants,
5167 shader_glsl_destroy,
5168 shader_glsl_alloc,
5169 shader_glsl_free,
5170 shader_glsl_dirty_const,
5171 shader_glsl_get_caps,
5172 shader_glsl_color_fixup_supported,
5173};
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