VirtualBox

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

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

wddm/3d: fix random black lines with projected textures

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

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