VirtualBox

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

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

wddm/3d: Aero working cleanly!

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