VirtualBox

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

最後變更 在這個檔案從35825是 35825,由 vboxsync 提交於 14 年 前

wined3d: bit less ifdefs

  • 屬性 svn:eol-style 設為 native
檔案大小: 207.3 KB
 
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 DWORD flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
2990
2991 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2992 if (flags & WINED3DTTFF_PROJECTED && sampler_type != WINED3DSTT_CUBE) {
2993 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
2994 switch (flags & ~WINED3DTTFF_PROJECTED) {
2995 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2996 case WINED3DTTFF_COUNT2: mask = WINED3DSP_WRITEMASK_1; break;
2997 case WINED3DTTFF_COUNT3: mask = WINED3DSP_WRITEMASK_2; break;
2998 case WINED3DTTFF_COUNT4:
2999 case WINED3DTTFF_DISABLE: mask = WINED3DSP_WRITEMASK_3; break;
3000 }
3001 }
3002 }
3003 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3004 {
3005 DWORD src_mod = ins->src[0].modifiers;
3006
3007 if (src_mod == WINED3DSPSM_DZ) {
3008 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3009 mask = WINED3DSP_WRITEMASK_2;
3010 } else if (src_mod == WINED3DSPSM_DW) {
3011 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3012 mask = WINED3DSP_WRITEMASK_3;
3013 }
3014 } else {
3015 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
3016 {
3017 /* ps 2.0 texldp instruction always divides by the fourth component. */
3018 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3019 mask = WINED3DSP_WRITEMASK_3;
3020 }
3021 }
3022
3023 if(deviceImpl->stateBlock->textures[sampler_idx] &&
3024 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
3025 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3026 }
3027
3028 shader_glsl_get_sample_function(gl_info, sampler_type, sample_flags, &sample_function);
3029 mask |= sample_function.coord_mask;
3030
3031 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3032 else swizzle = ins->src[1].swizzle;
3033
3034 /* 1.0-1.3: Use destination register as coordinate source.
3035 1.4+: Use provided coordinate source register. */
3036 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3037 {
3038 char coord_mask[6];
3039 shader_glsl_write_mask_to_str(mask, coord_mask);
3040 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3041 "T%u%s", sampler_idx, coord_mask);
3042 } else {
3043 glsl_src_param_t coord_param;
3044 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3045 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3046 {
3047 glsl_src_param_t bias;
3048 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3049 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3050 "%s", coord_param.param_str);
3051 } else {
3052 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3053 "%s", coord_param.param_str);
3054 }
3055 }
3056}
3057
3058static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3059{
3060 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3061 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
3062 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3063 glsl_sample_function_t sample_function;
3064 glsl_src_param_t coord_param, dx_param, dy_param;
3065 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3066 DWORD sampler_type;
3067 DWORD sampler_idx;
3068 DWORD swizzle = ins->src[1].swizzle;
3069
3070 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3071 {
3072 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3073 return shader_glsl_tex(ins);
3074 }
3075
3076 sampler_idx = ins->src[1].reg.idx;
3077 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3078 if(deviceImpl->stateBlock->textures[sampler_idx] &&
3079 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
3080 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3081 }
3082
3083 shader_glsl_get_sample_function(gl_info, sampler_type, sample_flags, &sample_function);
3084 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3085 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3086 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3087
3088 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3089 "%s", coord_param.param_str);
3090}
3091
3092static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3093{
3094 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3095 IWineD3DDeviceImpl* deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
3096 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3097 glsl_sample_function_t sample_function;
3098 glsl_src_param_t coord_param, lod_param;
3099 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3100 DWORD sampler_type;
3101 DWORD sampler_idx;
3102 DWORD swizzle = ins->src[1].swizzle;
3103
3104 sampler_idx = ins->src[1].reg.idx;
3105 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3106 if(deviceImpl->stateBlock->textures[sampler_idx] &&
3107 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl->stateBlock->textures[sampler_idx]) == GL_TEXTURE_RECTANGLE_ARB) {
3108 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3109 }
3110 shader_glsl_get_sample_function(gl_info, sampler_type, sample_flags, &sample_function);
3111 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3112
3113 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3114
3115 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3116 && shader_is_pshader_version(ins->ctx->reg_maps->shader_version.type))
3117 {
3118 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
3119 * However, they seem to work just fine in fragment shaders as well. */
3120 WARN("Using %s in fragment shader.\n", sample_function.name);
3121 }
3122 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3123 "%s", coord_param.param_str);
3124}
3125
3126static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3127{
3128 /* FIXME: Make this work for more than just 2D textures */
3129 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3130 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3131
3132 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3133 {
3134 char dst_mask[6];
3135
3136 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3137 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3138 ins->dst[0].reg.idx, dst_mask);
3139 } else {
3140 DWORD reg = ins->src[0].reg.idx;
3141 DWORD src_mod = ins->src[0].modifiers;
3142 char dst_swizzle[6];
3143
3144 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3145
3146 if (src_mod == WINED3DSPSM_DZ) {
3147 glsl_src_param_t div_param;
3148 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3149 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3150
3151 if (mask_size > 1) {
3152 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3153 } else {
3154 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3155 }
3156 } else if (src_mod == WINED3DSPSM_DW) {
3157 glsl_src_param_t div_param;
3158 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3159 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3160
3161 if (mask_size > 1) {
3162 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3163 } else {
3164 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3165 }
3166 } else {
3167 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3168 }
3169 }
3170}
3171
3172/** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3173 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3174 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3175static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3176{
3177 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3178 glsl_src_param_t src0_param;
3179 glsl_sample_function_t sample_function;
3180 DWORD sampler_idx = ins->dst[0].reg.idx;
3181 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3182 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3183 UINT mask_size;
3184
3185 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3186
3187 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3188 * scalar, and projected sampling would require 4.
3189 *
3190 * It is a dependent read - not valid with conditional NP2 textures
3191 */
3192 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3193 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3194
3195 switch(mask_size)
3196 {
3197 case 1:
3198 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3199 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3200 break;
3201
3202 case 2:
3203 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3204 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3205 break;
3206
3207 case 3:
3208 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3209 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3210 break;
3211
3212 default:
3213 FIXME("Unexpected mask size %u\n", mask_size);
3214 break;
3215 }
3216}
3217
3218/** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3219 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3220static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3221{
3222 glsl_src_param_t src0_param;
3223 DWORD dstreg = ins->dst[0].reg.idx;
3224 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3225 DWORD dst_mask;
3226 unsigned int mask_size;
3227
3228 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3229 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3230 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3231
3232 if (mask_size > 1) {
3233 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3234 } else {
3235 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3236 }
3237}
3238
3239/** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3240 * Calculate the depth as dst.x / dst.y */
3241static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3242{
3243 glsl_dst_param_t dst_param;
3244
3245 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3246
3247 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3248 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3249 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3250 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3251 * >= 1.0 or < 0.0
3252 */
3253 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3254 dst_param.reg_name, dst_param.reg_name);
3255}
3256
3257/** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3258 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3259 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3260 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3261 */
3262static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3263{
3264 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3265 DWORD dstreg = ins->dst[0].reg.idx;
3266 glsl_src_param_t src0_param;
3267
3268 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3269
3270 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3271 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3272}
3273
3274/** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3275 * Calculate the 1st of a 2-row matrix multiplication. */
3276static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3277{
3278 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3279 DWORD reg = ins->dst[0].reg.idx;
3280 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3281 glsl_src_param_t src0_param;
3282
3283 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3284 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3285}
3286
3287/** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3288 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3289static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3290{
3291 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3292 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3293 DWORD reg = ins->dst[0].reg.idx;
3294 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3295 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
3296 glsl_src_param_t src0_param;
3297
3298 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3299 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state->current_row, reg, src0_param.param_str);
3300 current_state->texcoord_w[current_state->current_row++] = reg;
3301}
3302
3303static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3304{
3305 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3306 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3307 DWORD reg = ins->dst[0].reg.idx;
3308 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3309 glsl_src_param_t src0_param;
3310 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
3311 glsl_sample_function_t sample_function;
3312
3313 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3314 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3315
3316 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3317
3318 /* Sample the texture using the calculated coordinates */
3319 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3320}
3321
3322/** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3323 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3324static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3325{
3326 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3327 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3328 SHADER_PARSE_STATE *current_state = &shader->baseShader.parse_state;
3329 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3330 glsl_src_param_t src0_param;
3331 DWORD reg = ins->dst[0].reg.idx;
3332 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
3333 glsl_sample_function_t sample_function;
3334
3335 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3336 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3337
3338 /* Dependent read, not valid with conditional NP2 */
3339 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3340
3341 /* Sample the texture using the calculated coordinates */
3342 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3343
3344 current_state->current_row = 0;
3345}
3346
3347/** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3348 * Perform the 3rd row of a 3x3 matrix multiply */
3349static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3350{
3351 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3352 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3353 SHADER_PARSE_STATE *current_state = &shader->baseShader.parse_state;
3354 glsl_src_param_t src0_param;
3355 char dst_mask[6];
3356 DWORD reg = ins->dst[0].reg.idx;
3357
3358 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3359
3360 shader_glsl_append_dst(ins->ctx->buffer, ins);
3361 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3362 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3363
3364 current_state->current_row = 0;
3365}
3366
3367/* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3368 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3369static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3370{
3371 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3372 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3373 DWORD reg = ins->dst[0].reg.idx;
3374 glsl_src_param_t src0_param;
3375 glsl_src_param_t src1_param;
3376 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3377 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
3378 WINED3DSAMPLER_TEXTURE_TYPE stype = ins->ctx->reg_maps->sampler_type[reg];
3379 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3380 glsl_sample_function_t sample_function;
3381
3382 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3383 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3384
3385 /* Perform the last matrix multiply operation */
3386 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3387 /* Reflection calculation */
3388 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3389
3390 /* Dependent read, not valid with conditional NP2 */
3391 shader_glsl_get_sample_function(gl_info, stype, 0, &sample_function);
3392
3393 /* Sample the texture */
3394 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3395
3396 current_state->current_row = 0;
3397}
3398
3399/* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3400 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3401static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3402{
3403 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3404 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3405 DWORD reg = ins->dst[0].reg.idx;
3406 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3407 SHADER_PARSE_STATE* current_state = &shader->baseShader.parse_state;
3408 glsl_src_param_t src0_param;
3409 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3410 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[reg];
3411 glsl_sample_function_t sample_function;
3412
3413 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3414
3415 /* Perform the last matrix multiply operation */
3416 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3417
3418 /* Construct the eye-ray vector from w coordinates */
3419 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3420 current_state->texcoord_w[0], current_state->texcoord_w[1], reg);
3421 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3422
3423 /* Dependent read, not valid with conditional NP2 */
3424 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3425
3426 /* Sample the texture using the calculated coordinates */
3427 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3428
3429 current_state->current_row = 0;
3430}
3431
3432/** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3433 * Apply a fake bump map transform.
3434 * texbem is pshader <= 1.3 only, this saves a few version checks
3435 */
3436static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3437{
3438 IWineD3DBaseShaderImpl *shader = (IWineD3DBaseShaderImpl *)ins->ctx->shader;
3439 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *)shader->baseShader.device;
3440 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3441 glsl_sample_function_t sample_function;
3442 glsl_src_param_t coord_param;
3443 WINED3DSAMPLER_TEXTURE_TYPE sampler_type;
3444 DWORD sampler_idx;
3445 DWORD mask;
3446 DWORD flags;
3447 char coord_mask[6];
3448
3449 sampler_idx = ins->dst[0].reg.idx;
3450 flags = deviceImpl->stateBlock->textureState[sampler_idx][WINED3DTSS_TEXTURETRANSFORMFLAGS];
3451
3452 sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3453 /* Dependent read, not valid with conditional NP2 */
3454 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3455 mask = sample_function.coord_mask;
3456
3457 shader_glsl_write_mask_to_str(mask, coord_mask);
3458
3459 /* with projective textures, texbem only divides the static texture coord, not the displacement,
3460 * so we can't let the GL handle this.
3461 */
3462 if (flags & WINED3DTTFF_PROJECTED) {
3463 DWORD div_mask=0;
3464 char coord_div_mask[3];
3465 switch (flags & ~WINED3DTTFF_PROJECTED) {
3466 case WINED3DTTFF_COUNT1: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3467 case WINED3DTTFF_COUNT2: div_mask = WINED3DSP_WRITEMASK_1; break;
3468 case WINED3DTTFF_COUNT3: div_mask = WINED3DSP_WRITEMASK_2; break;
3469 case WINED3DTTFF_COUNT4:
3470 case WINED3DTTFF_DISABLE: div_mask = WINED3DSP_WRITEMASK_3; break;
3471 }
3472 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3473 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3474 }
3475
3476 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3477
3478 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3479 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3480 coord_param.param_str, coord_mask);
3481
3482 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3483 {
3484 glsl_src_param_t luminance_param;
3485 glsl_dst_param_t dst_param;
3486
3487 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3488 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3489
3490 shader_addline(ins->ctx->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3491 dst_param.reg_name, dst_param.mask_str,
3492 luminance_param.param_str, sampler_idx, sampler_idx);
3493 }
3494}
3495
3496static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3497{
3498 glsl_src_param_t src0_param, src1_param;
3499 DWORD sampler_idx = ins->dst[0].reg.idx;
3500
3501 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3502 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3503
3504 shader_glsl_append_dst(ins->ctx->buffer, ins);
3505 shader_addline(ins->ctx->buffer, "%s + bumpenvmat%d * %s);\n",
3506 src0_param.param_str, sampler_idx, src1_param.param_str);
3507}
3508
3509/** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3510 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3511static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
3512{
3513 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3514 glsl_src_param_t src0_param;
3515 DWORD sampler_idx = ins->dst[0].reg.idx;
3516 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3517 glsl_sample_function_t sample_function;
3518
3519 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3520
3521 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3522 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3523 "%s.wx", src0_param.reg_name);
3524}
3525
3526/** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3527 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3528static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
3529{
3530 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3531 glsl_src_param_t src0_param;
3532 DWORD sampler_idx = ins->dst[0].reg.idx;
3533 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3534 glsl_sample_function_t sample_function;
3535
3536 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3537
3538 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3539 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3540 "%s.yz", src0_param.reg_name);
3541}
3542
3543/** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3544 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3545static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
3546{
3547 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3548 glsl_src_param_t src0_param;
3549 DWORD sampler_idx = ins->dst[0].reg.idx;
3550 WINED3DSAMPLER_TEXTURE_TYPE sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3551 glsl_sample_function_t sample_function;
3552
3553 /* Dependent read, not valid with conditional NP2 */
3554 shader_glsl_get_sample_function(gl_info, sampler_type, 0, &sample_function);
3555 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
3556
3557 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3558 "%s", src0_param.param_str);
3559}
3560
3561/** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3562 * If any of the first 3 components are < 0, discard this pixel */
3563static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
3564{
3565 glsl_dst_param_t dst_param;
3566
3567 /* The argument is a destination parameter, and no writemasks are allowed */
3568 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3569 if (ins->ctx->reg_maps->shader_version.major >= 2)
3570 {
3571 /* 2.0 shaders compare all 4 components in texkill */
3572 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
3573 } else {
3574 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3575 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3576 * 4 components are defined, only the first 3 are used
3577 */
3578 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
3579 }
3580}
3581
3582/** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3583 * dst = dot2(src0, src1) + src2 */
3584static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
3585{
3586 glsl_src_param_t src0_param;
3587 glsl_src_param_t src1_param;
3588 glsl_src_param_t src2_param;
3589 DWORD write_mask;
3590 unsigned int mask_size;
3591
3592 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3593 mask_size = shader_glsl_get_write_mask_size(write_mask);
3594
3595 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3596 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3597 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
3598
3599 if (mask_size > 1) {
3600 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
3601 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
3602 } else {
3603 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
3604 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3605 }
3606}
3607
3608static void shader_glsl_input_pack(IWineD3DPixelShader *iface, struct wined3d_shader_buffer *buffer,
3609 const struct wined3d_shader_signature_element *input_signature, const struct shader_reg_maps *reg_maps,
3610 enum vertexprocessing_mode vertexprocessing)
3611{
3612 unsigned int i;
3613 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
3614 WORD map = reg_maps->input_registers;
3615
3616 for (i = 0; map; map >>= 1, ++i)
3617 {
3618 const char *semantic_name;
3619 UINT semantic_idx;
3620 char reg_mask[6];
3621
3622 /* Unused */
3623 if (!(map & 1)) continue;
3624
3625 semantic_name = input_signature[i].semantic_name;
3626 semantic_idx = input_signature[i].semantic_idx;
3627 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
3628
3629 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3630 {
3631 if (semantic_idx < 8 && vertexprocessing == pretransformed)
3632 shader_addline(buffer, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3633 This->input_reg_map[i], reg_mask, semantic_idx, reg_mask);
3634 else
3635 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3636 This->input_reg_map[i], reg_mask, reg_mask);
3637 }
3638 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3639 {
3640 if (semantic_idx == 0)
3641 shader_addline(buffer, "IN[%u]%s = vec4(gl_Color)%s;\n",
3642 This->input_reg_map[i], reg_mask, reg_mask);
3643 else if (semantic_idx == 1)
3644 shader_addline(buffer, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3645 This->input_reg_map[i], reg_mask, reg_mask);
3646 else
3647 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3648 This->input_reg_map[i], reg_mask, reg_mask);
3649 }
3650 else
3651 {
3652 shader_addline(buffer, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3653 This->input_reg_map[i], reg_mask, reg_mask);
3654 }
3655 }
3656}
3657
3658/*********************************************
3659 * Vertex Shader Specific Code begins here
3660 ********************************************/
3661
3662static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry) {
3663 glsl_program_key_t key;
3664
3665 key.vshader = entry->vshader;
3666 key.pshader = entry->pshader;
3667 key.vs_args = entry->vs_args;
3668 key.ps_args = entry->ps_args;
3669 key.context = entry->context;
3670
3671 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
3672 {
3673 ERR("Failed to insert program entry.\n");
3674 }
3675}
3676
3677static struct glsl_shader_prog_link *get_glsl_program_entry(struct shader_glsl_priv *priv,
3678 IWineD3DVertexShader *vshader, IWineD3DPixelShader *pshader, struct vs_compile_args *vs_args,
3679 struct ps_compile_args *ps_args, const struct wined3d_context *context) {
3680 struct wine_rb_entry *entry;
3681 glsl_program_key_t key;
3682
3683 key.vshader = vshader;
3684 key.pshader = pshader;
3685 key.vs_args = *vs_args;
3686 key.ps_args = *ps_args;
3687 key.context = context;
3688
3689 entry = wine_rb_get(&priv->program_lookup, &key);
3690 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
3691}
3692
3693/* GL locking is done by the caller */
3694static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
3695 struct glsl_shader_prog_link *entry)
3696{
3697 glsl_program_key_t key;
3698
3699 key.vshader = entry->vshader;
3700 key.pshader = entry->pshader;
3701 key.vs_args = entry->vs_args;
3702 key.ps_args = entry->ps_args;
3703 key.context = entry->context;
3704 wine_rb_remove(&priv->program_lookup, &key);
3705
3706 if (context_get_current() == entry->context)
3707 {
3708 TRACE("deleting program %u\n", entry->programId);
3709 GL_EXTCALL(glDeleteObjectARB(entry->programId));
3710 checkGLcall("glDeleteObjectARB");
3711 }
3712 else
3713 {
3714 WARN("Attempting to delete program %u created in ctx %p from ctx %p\n", entry->programId, entry->context, context_get_current());
3715 }
3716
3717 if (entry->vshader) list_remove(&entry->vshader_entry);
3718 if (entry->pshader) list_remove(&entry->pshader_entry);
3719 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
3720 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
3721 HeapFree(GetProcessHeap(), 0, entry);
3722}
3723
3724static void handle_ps3_input(struct wined3d_shader_buffer *buffer, const struct wined3d_gl_info *gl_info, const DWORD *map,
3725 const struct wined3d_shader_signature_element *input_signature, const struct shader_reg_maps *reg_maps_in,
3726 const struct wined3d_shader_signature_element *output_signature, const struct shader_reg_maps *reg_maps_out)
3727{
3728 unsigned int i, j;
3729 const char *semantic_name_in, *semantic_name_out;
3730 UINT semantic_idx_in, semantic_idx_out;
3731 DWORD *set;
3732 DWORD in_idx;
3733 unsigned int in_count = vec4_varyings(3, gl_info);
3734 char reg_mask[6], reg_mask_out[6];
3735 char destination[50];
3736 WORD input_map, output_map;
3737
3738 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
3739
3740 if (!output_signature)
3741 {
3742 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
3743 shader_addline(buffer, "vec4 front_color = gl_FrontColor;\n");
3744 shader_addline(buffer, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
3745 }
3746
3747 input_map = reg_maps_in->input_registers;
3748 for (i = 0; input_map; input_map >>= 1, ++i)
3749 {
3750 if (!(input_map & 1)) continue;
3751
3752 in_idx = map[i];
3753 if (in_idx >= (in_count + 2)) {
3754 FIXME("More input varyings declared than supported, expect issues\n");
3755 continue;
3756 }
3757 else if (map[i] == ~0U)
3758 {
3759 /* Declared, but not read register */
3760 continue;
3761 }
3762
3763 if (in_idx == in_count) {
3764 sprintf(destination, "gl_FrontColor");
3765 } else if (in_idx == in_count + 1) {
3766 sprintf(destination, "gl_FrontSecondaryColor");
3767 } else {
3768 sprintf(destination, "IN[%u]", in_idx);
3769 }
3770
3771 semantic_name_in = input_signature[i].semantic_name;
3772 semantic_idx_in = input_signature[i].semantic_idx;
3773 set[map[i]] = input_signature[i].mask;
3774 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
3775
3776 if (!output_signature)
3777 {
3778 if (shader_match_semantic(semantic_name_in, WINED3DDECLUSAGE_COLOR))
3779 {
3780 if (semantic_idx_in == 0)
3781 shader_addline(buffer, "%s%s = front_color%s;\n",
3782 destination, reg_mask, reg_mask);
3783 else if (semantic_idx_in == 1)
3784 shader_addline(buffer, "%s%s = front_secondary_color%s;\n",
3785 destination, reg_mask, reg_mask);
3786 else
3787 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3788 destination, reg_mask, reg_mask);
3789 }
3790 else if (shader_match_semantic(semantic_name_in, WINED3DDECLUSAGE_TEXCOORD))
3791 {
3792 if (semantic_idx_in < 8)
3793 {
3794 shader_addline(buffer, "%s%s = gl_TexCoord[%u]%s;\n",
3795 destination, reg_mask, semantic_idx_in, reg_mask);
3796 }
3797 else
3798 {
3799 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3800 destination, reg_mask, reg_mask);
3801 }
3802 }
3803 else if (shader_match_semantic(semantic_name_in, WINED3DDECLUSAGE_FOG))
3804 {
3805 shader_addline(buffer, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3806 destination, reg_mask, reg_mask);
3807 }
3808 else
3809 {
3810 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3811 destination, reg_mask, reg_mask);
3812 }
3813 } else {
3814 BOOL found = FALSE;
3815
3816 output_map = reg_maps_out->output_registers;
3817 for (j = 0; output_map; output_map >>= 1, ++j)
3818 {
3819 if (!(output_map & 1)) continue;
3820
3821 semantic_name_out = output_signature[j].semantic_name;
3822 semantic_idx_out = output_signature[j].semantic_idx;
3823 shader_glsl_write_mask_to_str(output_signature[j].mask, reg_mask_out);
3824
3825 if (semantic_idx_in == semantic_idx_out
3826 && !strcmp(semantic_name_in, semantic_name_out))
3827 {
3828 shader_addline(buffer, "%s%s = OUT[%u]%s;\n",
3829 destination, reg_mask, j, reg_mask);
3830 found = TRUE;
3831 }
3832 }
3833 if(!found) {
3834 shader_addline(buffer, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3835 destination, reg_mask, reg_mask);
3836 }
3837 }
3838 }
3839
3840 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3841 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3842 * input varyings are assigned above, if the optimizer works properly.
3843 */
3844 for(i = 0; i < in_count + 2; i++) {
3845 if (set[i] && set[i] != WINED3DSP_WRITEMASK_ALL)
3846 {
3847 unsigned int size = 0;
3848 memset(reg_mask, 0, sizeof(reg_mask));
3849 if(!(set[i] & WINED3DSP_WRITEMASK_0)) {
3850 reg_mask[size] = 'x';
3851 size++;
3852 }
3853 if(!(set[i] & WINED3DSP_WRITEMASK_1)) {
3854 reg_mask[size] = 'y';
3855 size++;
3856 }
3857 if(!(set[i] & WINED3DSP_WRITEMASK_2)) {
3858 reg_mask[size] = 'z';
3859 size++;
3860 }
3861 if(!(set[i] & WINED3DSP_WRITEMASK_3)) {
3862 reg_mask[size] = 'w';
3863 size++;
3864 }
3865
3866 if (i == in_count) {
3867 sprintf(destination, "gl_FrontColor");
3868 } else if (i == in_count + 1) {
3869 sprintf(destination, "gl_FrontSecondaryColor");
3870 } else {
3871 sprintf(destination, "IN[%u]", i);
3872 }
3873
3874 if (size == 1) {
3875 shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
3876 } else {
3877 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
3878 }
3879 }
3880 }
3881
3882 HeapFree(GetProcessHeap(), 0, set);
3883}
3884
3885/* GL locking is done by the caller */
3886static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
3887 IWineD3DVertexShader *vertexshader, IWineD3DPixelShader *pixelshader, const struct wined3d_gl_info *gl_info)
3888{
3889 GLhandleARB ret = 0;
3890 IWineD3DVertexShaderImpl *vs = (IWineD3DVertexShaderImpl *) vertexshader;
3891 IWineD3DPixelShaderImpl *ps = (IWineD3DPixelShaderImpl *) pixelshader;
3892 IWineD3DDeviceImpl *device;
3893 DWORD vs_major = vs->baseShader.reg_maps.shader_version.major;
3894 DWORD ps_major = ps ? ps->baseShader.reg_maps.shader_version.major : 0;
3895 unsigned int i;
3896 const char *semantic_name;
3897 UINT semantic_idx;
3898 char reg_mask[6];
3899 const struct wined3d_shader_signature_element *output_signature;
3900
3901 shader_buffer_clear(buffer);
3902
3903 shader_addline(buffer, "#version 120\n");
3904
3905 if(vs_major < 3 && ps_major < 3) {
3906 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3907 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3908 */
3909 device = (IWineD3DDeviceImpl *) vs->baseShader.device;
3910 if ((gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W)
3911 && ps_major == 0 && vs_major > 0 && !device->frag_pipe->ffp_proj_control)
3912 {
3913 shader_addline(buffer, "void order_ps_input() {\n");
3914 for(i = 0; i < min(8, MAX_REG_TEXCRD); i++) {
3915 if(vs->baseShader.reg_maps.texcoord_mask[i] != 0 &&
3916 vs->baseShader.reg_maps.texcoord_mask[i] != WINED3DSP_WRITEMASK_ALL) {
3917 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", i);
3918 }
3919 }
3920 shader_addline(buffer, "}\n");
3921 } else {
3922 shader_addline(buffer, "void order_ps_input() { /* do nothing */ }\n");
3923 }
3924 } else if(ps_major < 3 && vs_major >= 3) {
3925 WORD map = vs->baseShader.reg_maps.output_registers;
3926
3927 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3928 output_signature = vs->baseShader.output_signature;
3929
3930 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3931 for (i = 0; map; map >>= 1, ++i)
3932 {
3933 DWORD write_mask;
3934
3935 if (!(map & 1)) continue;
3936
3937 semantic_name = output_signature[i].semantic_name;
3938 semantic_idx = output_signature[i].semantic_idx;
3939 write_mask = output_signature[i].mask;
3940 shader_glsl_write_mask_to_str(write_mask, reg_mask);
3941
3942 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_COLOR))
3943 {
3944 if (semantic_idx == 0)
3945 shader_addline(buffer, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3946 else if (semantic_idx == 1)
3947 shader_addline(buffer, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3948 }
3949 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3950 {
3951 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3952 }
3953 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_TEXCOORD))
3954 {
3955 if (semantic_idx < 8)
3956 {
3957 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
3958 write_mask |= WINED3DSP_WRITEMASK_3;
3959
3960 shader_addline(buffer, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3961 semantic_idx, reg_mask, i, reg_mask);
3962 if (!(write_mask & WINED3DSP_WRITEMASK_3))
3963 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
3964 }
3965 }
3966 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3967 {
3968 shader_addline(buffer, "gl_PointSize = OUT[%u].x;\n", i);
3969 }
3970 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_FOG))
3971 {
3972 shader_addline(buffer, "gl_FogFragCoord = OUT[%u].%c;\n", i, reg_mask[1]);
3973 }
3974 }
3975 shader_addline(buffer, "}\n");
3976
3977 } else if(ps_major >= 3 && vs_major >= 3) {
3978 WORD map = vs->baseShader.reg_maps.output_registers;
3979
3980 output_signature = vs->baseShader.output_signature;
3981
3982 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3983 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info));
3984 shader_addline(buffer, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT);
3985
3986 /* First, sort out position and point size. Those are not passed to the pixel shader */
3987 for (i = 0; map; map >>= 1, ++i)
3988 {
3989 if (!(map & 1)) continue;
3990
3991 semantic_name = output_signature[i].semantic_name;
3992 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
3993
3994 if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_POSITION))
3995 {
3996 shader_addline(buffer, "gl_Position%s = OUT[%u]%s;\n", reg_mask, i, reg_mask);
3997 }
3998 else if (shader_match_semantic(semantic_name, WINED3DDECLUSAGE_PSIZE))
3999 {
4000 shader_addline(buffer, "gl_PointSize = OUT[%u].x;\n", i);
4001 }
4002 }
4003
4004 /* Then, fix the pixel shader input */
4005 handle_ps3_input(buffer, gl_info, ps->input_reg_map, ps->baseShader.input_signature,
4006 &ps->baseShader.reg_maps, output_signature, &vs->baseShader.reg_maps);
4007
4008 shader_addline(buffer, "}\n");
4009 } else if(ps_major >= 3 && vs_major < 3) {
4010 shader_addline(buffer, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info));
4011 shader_addline(buffer, "void order_ps_input() {\n");
4012 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
4013 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
4014 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
4015 */
4016 handle_ps3_input(buffer, gl_info, ps->input_reg_map, ps->baseShader.input_signature,
4017 &ps->baseShader.reg_maps, NULL, NULL);
4018 shader_addline(buffer, "}\n");
4019 } else {
4020 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major, ps_major);
4021 }
4022
4023 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4024 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4025 GL_EXTCALL(glShaderSourceARB(ret, 1, (const char**)&buffer->buffer, NULL));
4026 checkGLcall("glShaderSourceARB(ret, 1, &buffer->buffer, NULL)");
4027 GL_EXTCALL(glCompileShaderARB(ret));
4028 checkGLcall("glCompileShaderARB(ret)");
4029
4030 return ret;
4031}
4032
4033/* GL locking is done by the caller */
4034static void hardcode_local_constants(IWineD3DBaseShaderImpl *shader, const struct wined3d_gl_info *gl_info,
4035 GLhandleARB programId, char prefix)
4036{
4037 const local_constant *lconst;
4038 GLint tmp_loc;
4039 const float *value;
4040 char glsl_name[8];
4041
4042 LIST_FOR_EACH_ENTRY(lconst, &shader->baseShader.constantsF, local_constant, entry) {
4043 value = (const float *)lconst->value;
4044 snprintf(glsl_name, sizeof(glsl_name), "%cLC%u", prefix, lconst->idx);
4045 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4046 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
4047 }
4048 checkGLcall("Hardcoding local constants");
4049}
4050
4051/* GL locking is done by the caller */
4052static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4053 struct wined3d_shader_buffer *buffer, IWineD3DPixelShaderImpl *This,
4054 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4055{
4056 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
4057 const struct wined3d_gl_info *gl_info = context->gl_info;
4058 CONST DWORD *function = This->baseShader.function;
4059 struct shader_glsl_ctx_priv priv_ctx;
4060
4061 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4062 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4063
4064 memset(&priv_ctx, 0, sizeof(priv_ctx));
4065 priv_ctx.cur_ps_args = args;
4066 priv_ctx.cur_np2fixup_info = np2fixup_info;
4067
4068 shader_addline(buffer, "#version 120\n");
4069
4070 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] && reg_maps->usestexldd)
4071 {
4072 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4073 }
4074 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4075 {
4076 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
4077 * drivers write a warning if we don't do so
4078 */
4079 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4080 }
4081 if (gl_info->supported[EXT_GPU_SHADER4])
4082 {
4083 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4084 }
4085
4086 /* Base Declarations */
4087 shader_generate_glsl_declarations(context, buffer, (IWineD3DBaseShader *)This, reg_maps, &priv_ctx);
4088
4089 /* Pack 3.0 inputs */
4090 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4091 {
4092 shader_glsl_input_pack((IWineD3DPixelShader *) This, buffer,
4093 This->baseShader.input_signature, reg_maps, args->vp_mode);
4094 }
4095
4096 /* Base Shader Body */
4097 shader_generate_main((IWineD3DBaseShader *)This, buffer, reg_maps, function, &priv_ctx);
4098
4099 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4100 if (reg_maps->shader_version.major < 2)
4101 {
4102 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4103 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4104 }
4105
4106 if (args->srgb_correction)
4107 {
4108 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4109 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4110 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4111 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4112 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4113 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4114 }
4115 /* Pixel shader < 3.0 do not replace the fog stage.
4116 * This implements linear fog computation and blending.
4117 * TODO: non linear fog
4118 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4119 * -1/(e-s) and e/(e-s) respectively.
4120 */
4121 if (reg_maps->shader_version.major < 3)
4122 {
4123 switch(args->fog) {
4124 case FOG_OFF: break;
4125 case FOG_LINEAR:
4126 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4127 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4128 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4129 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4130 break;
4131 case FOG_EXP:
4132 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4133 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4134 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4135 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4136 break;
4137 case FOG_EXP2:
4138 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4139 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4140 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4141 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4142 break;
4143 }
4144 }
4145
4146 shader_addline(buffer, "}\n");
4147
4148 TRACE("Compiling shader object %u\n", shader_obj);
4149 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4150 GL_EXTCALL(glCompileShaderARB(shader_obj));
4151 print_glsl_info_log(gl_info, shader_obj);
4152
4153 /* Store the shader object */
4154 return shader_obj;
4155}
4156
4157/* GL locking is done by the caller */
4158static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4159 struct wined3d_shader_buffer *buffer, IWineD3DVertexShaderImpl *This,
4160 const struct vs_compile_args *args)
4161{
4162 const struct shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
4163 const struct wined3d_gl_info *gl_info = context->gl_info;
4164 CONST DWORD *function = This->baseShader.function;
4165 struct shader_glsl_ctx_priv priv_ctx;
4166
4167 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4168 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4169
4170 shader_addline(buffer, "#version 120\n");
4171
4172 if (gl_info->supported[EXT_GPU_SHADER4])
4173 {
4174 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4175 }
4176
4177 memset(&priv_ctx, 0, sizeof(priv_ctx));
4178 priv_ctx.cur_vs_args = args;
4179
4180 /* Base Declarations */
4181 shader_generate_glsl_declarations(context, buffer, (IWineD3DBaseShader *)This, reg_maps, &priv_ctx);
4182
4183 /* Base Shader Body */
4184 shader_generate_main((IWineD3DBaseShader*)This, buffer, reg_maps, function, &priv_ctx);
4185
4186 /* Unpack 3.0 outputs */
4187 if (reg_maps->shader_version.major >= 3) shader_addline(buffer, "order_ps_input(OUT);\n");
4188 else shader_addline(buffer, "order_ps_input();\n");
4189
4190 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4191 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4192 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4193 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4194 */
4195 if(args->fog_src == VS_FOG_Z) {
4196 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4197 } else if (!reg_maps->fog) {
4198 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4199 }
4200
4201 /* Write the final position.
4202 *
4203 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4204 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4205 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4206 * contains 1.0 to allow a mad.
4207 */
4208 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4209 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4210 if(args->clip_enabled) {
4211 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4212 }
4213
4214 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4215 *
4216 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4217 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4218 * which is the same as z = z * 2 - w.
4219 */
4220 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4221
4222 shader_addline(buffer, "}\n");
4223
4224 TRACE("Compiling shader object %u\n", shader_obj);
4225 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer->buffer, NULL));
4226 GL_EXTCALL(glCompileShaderARB(shader_obj));
4227 print_glsl_info_log(gl_info, shader_obj);
4228
4229 return shader_obj;
4230}
4231
4232static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4233 struct wined3d_shader_buffer *buffer, IWineD3DPixelShaderImpl *shader,
4234 const struct ps_compile_args *args,
4235#ifdef VBOX_WITH_WDDM
4236 UINT *inp2fixup_info
4237#else
4238 const struct ps_np2fixup_info **np2fixup_info
4239#endif
4240 )
4241{
4242 UINT i;
4243 DWORD new_size;
4244 struct glsl_ps_compiled_shader *new_array;
4245 struct glsl_pshader_private *shader_data;
4246 struct ps_np2fixup_info *np2fixup = NULL;
4247 GLhandleARB ret;
4248
4249 if (!shader->baseShader.backend_data)
4250 {
4251 shader->baseShader.backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4252 if (!shader->baseShader.backend_data)
4253 {
4254 ERR("Failed to allocate backend data.\n");
4255 return 0;
4256 }
4257 }
4258 shader_data = shader->baseShader.backend_data;
4259
4260 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4261 * so a linear search is more performant than a hashmap or a binary search
4262 * (cache coherency etc)
4263 */
4264 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4265 if(shader_data->gl_shaders[i].context==context
4266 && memcmp(&shader_data->gl_shaders[i].args, args, sizeof(*args)) == 0) {
4267 if(args->np2_fixup) {
4268#ifdef VBOX_WITH_WDDM
4269 *inp2fixup_info = i;
4270#else
4271 *np2fixup_info = &shader_data->gl_shaders[i].np2fixup;
4272#endif
4273 }
4274 return shader_data->gl_shaders[i].prgId;
4275 }
4276 }
4277
4278 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4279 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4280 if (shader_data->num_gl_shaders)
4281 {
4282 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4283 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4284 new_size * sizeof(*shader_data->gl_shaders));
4285 } else {
4286 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4287 new_size = 1;
4288 }
4289
4290 if(!new_array) {
4291 ERR("Out of memory\n");
4292 return 0;
4293 }
4294 shader_data->gl_shaders = new_array;
4295 shader_data->shader_array_size = new_size;
4296 }
4297
4298 shader_data->gl_shaders[shader_data->num_gl_shaders].context = context;
4299 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4300
4301 memset(&shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup, 0, sizeof(struct ps_np2fixup_info));
4302 if (args->np2_fixup) np2fixup = &shader_data->gl_shaders[shader_data->num_gl_shaders].np2fixup;
4303
4304 pixelshader_update_samplers(&shader->baseShader.reg_maps,
4305 ((IWineD3DDeviceImpl *)shader->baseShader.device)->stateBlock->textures);
4306
4307 shader_buffer_clear(buffer);
4308 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4309#ifdef VBOX_WITH_WDDM
4310 *inp2fixup_info = shader_data->num_gl_shaders;
4311#else
4312 *np2fixup_info = np2fixup;
4313#endif
4314 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4315
4316 return ret;
4317}
4318
4319static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4320 const DWORD use_map) {
4321 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4322 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4323 return stored->fog_src == new->fog_src;
4324}
4325
4326static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4327 struct wined3d_shader_buffer *buffer, IWineD3DVertexShaderImpl *shader,
4328 const struct vs_compile_args *args)
4329{
4330 UINT i;
4331 DWORD new_size;
4332 struct glsl_vs_compiled_shader *new_array;
4333 DWORD use_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.use_map;
4334 struct glsl_vshader_private *shader_data;
4335 GLhandleARB ret;
4336
4337 if (!shader->baseShader.backend_data)
4338 {
4339 shader->baseShader.backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4340 if (!shader->baseShader.backend_data)
4341 {
4342 ERR("Failed to allocate backend data.\n");
4343 return 0;
4344 }
4345 }
4346 shader_data = shader->baseShader.backend_data;
4347
4348 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4349 * so a linear search is more performant than a hashmap or a binary search
4350 * (cache coherency etc)
4351 */
4352 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4353 if(shader_data->gl_shaders[i].context==context
4354 && vs_args_equal(&shader_data->gl_shaders[i].args, args, use_map)) {
4355 return shader_data->gl_shaders[i].prgId;
4356 }
4357 }
4358
4359 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4360
4361 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4362 if (shader_data->num_gl_shaders)
4363 {
4364 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4365 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders,
4366 new_size * sizeof(*shader_data->gl_shaders));
4367 } else {
4368 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data->gl_shaders));
4369 new_size = 1;
4370 }
4371
4372 if(!new_array) {
4373 ERR("Out of memory\n");
4374 return 0;
4375 }
4376 shader_data->gl_shaders = new_array;
4377 shader_data->shader_array_size = new_size;
4378 }
4379
4380 shader_data->gl_shaders[shader_data->num_gl_shaders].context = context;
4381 shader_data->gl_shaders[shader_data->num_gl_shaders].args = *args;
4382
4383 shader_buffer_clear(buffer);
4384 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4385 shader_data->gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4386
4387 return ret;
4388}
4389
4390/** Sets the GLSL program ID for the given pixel and vertex shader combination.
4391 * It sets the programId on the current StateBlock (because it should be called
4392 * inside of the DrawPrimitive() part of the render loop).
4393 *
4394 * If a program for the given combination does not exist, create one, and store
4395 * the program in the hash table. If it creates a program, it will link the
4396 * given objects, too.
4397 */
4398
4399/* GL locking is done by the caller */
4400static void set_glsl_shader_program(const struct wined3d_context *context,
4401 IWineD3DDeviceImpl *device, BOOL use_ps, BOOL use_vs)
4402{
4403 IWineD3DVertexShader *vshader = use_vs ? device->stateBlock->vertexShader : NULL;
4404 IWineD3DPixelShader *pshader = use_ps ? device->stateBlock->pixelShader : NULL;
4405 const struct wined3d_gl_info *gl_info = context->gl_info;
4406 struct shader_glsl_priv *priv = device->shader_priv;
4407 struct glsl_shader_prog_link *entry = NULL;
4408 GLhandleARB programId = 0;
4409 GLhandleARB reorder_shader_id = 0;
4410 unsigned int i;
4411 char glsl_name[8];
4412 struct ps_compile_args ps_compile_args;
4413 struct vs_compile_args vs_compile_args;
4414
4415 if (vshader) find_vs_compile_args((IWineD3DVertexShaderImpl *)vshader, device->stateBlock, &vs_compile_args);
4416 if (pshader) find_ps_compile_args((IWineD3DPixelShaderImpl *)pshader, device->stateBlock, &ps_compile_args);
4417
4418 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args, context);
4419 if (entry) {
4420 priv->glsl_program = entry;
4421 return;
4422 }
4423
4424 /* If we get to this point, then no matching program exists, so we create one */
4425 programId = GL_EXTCALL(glCreateProgramObjectARB());
4426 TRACE("Created new GLSL shader program %u\n", programId);
4427
4428 /* Create the entry */
4429 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
4430 entry->context = context;
4431 entry->programId = programId;
4432 entry->vshader = vshader;
4433 entry->pshader = pshader;
4434 entry->vs_args = vs_compile_args;
4435 entry->ps_args = ps_compile_args;
4436 entry->constant_version = 0;
4437 WINEFIXUPINFO_INIT(entry);
4438 /* Add the hash table entry */
4439 add_glsl_program_entry(priv, entry);
4440
4441 /* Set the current program */
4442 priv->glsl_program = entry;
4443
4444 /* Attach GLSL vshader */
4445 if (vshader)
4446 {
4447 GLhandleARB vshader_id = find_glsl_vshader(context, &priv->shader_buffer,
4448 (IWineD3DVertexShaderImpl *)vshader, &vs_compile_args);
4449 WORD map = ((IWineD3DBaseShaderImpl *)vshader)->baseShader.reg_maps.input_registers;
4450 char tmp_name[10];
4451
4452 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
4453 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
4454 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
4455 checkGLcall("glAttachObjectARB");
4456 /* Flag the reorder function for deletion, then it will be freed automatically when the program
4457 * is destroyed
4458 */
4459 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
4460
4461 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
4462 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
4463 checkGLcall("glAttachObjectARB");
4464
4465 /* Bind vertex attributes to a corresponding index number to match
4466 * the same index numbers as ARB_vertex_programs (makes loading
4467 * vertex attributes simpler). With this method, we can use the
4468 * exact same code to load the attributes later for both ARB and
4469 * GLSL shaders.
4470 *
4471 * We have to do this here because we need to know the Program ID
4472 * in order to make the bindings work, and it has to be done prior
4473 * to linking the GLSL program. */
4474 for (i = 0; map; map >>= 1, ++i)
4475 {
4476 if (!(map & 1)) continue;
4477
4478 snprintf(tmp_name, sizeof(tmp_name), "attrib%u", i);
4479 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
4480 }
4481 checkGLcall("glBindAttribLocationARB");
4482
4483 list_add_head(&((IWineD3DBaseShaderImpl *)vshader)->baseShader.linked_programs, &entry->vshader_entry);
4484 }
4485
4486 /* Attach GLSL pshader */
4487 if (pshader)
4488 {
4489 GLhandleARB pshader_id = find_glsl_pshader(context, &priv->shader_buffer,
4490 (IWineD3DPixelShaderImpl *)pshader, &ps_compile_args,
4491#ifdef VBOX_WITH_WDDM
4492 &entry->inp2Fixup_info
4493#else
4494 &entry->np2Fixup_info
4495#endif
4496 );
4497 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
4498 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
4499 checkGLcall("glAttachObjectARB");
4500
4501 list_add_head(&((IWineD3DBaseShaderImpl *)pshader)->baseShader.linked_programs, &entry->pshader_entry);
4502 }
4503
4504 /* Link the program */
4505 TRACE("Linking GLSL shader program %u\n", programId);
4506 GL_EXTCALL(glLinkProgramARB(programId));
4507 shader_glsl_validate_link(gl_info, programId);
4508
4509 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4510 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
4511 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
4512 {
4513 snprintf(glsl_name, sizeof(glsl_name), "VC[%i]", i);
4514 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4515 }
4516 for (i = 0; i < MAX_CONST_I; ++i)
4517 {
4518 snprintf(glsl_name, sizeof(glsl_name), "VI[%i]", i);
4519 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4520 }
4521 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4522 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
4523 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
4524 {
4525 snprintf(glsl_name, sizeof(glsl_name), "PC[%i]", i);
4526 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4527 }
4528 for (i = 0; i < MAX_CONST_I; ++i)
4529 {
4530 snprintf(glsl_name, sizeof(glsl_name), "PI[%i]", i);
4531 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4532 }
4533
4534 if(pshader) {
4535 char name[32];
4536
4537 for(i = 0; i < MAX_TEXTURES; i++) {
4538 sprintf(name, "bumpenvmat%u", i);
4539 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4540 sprintf(name, "luminancescale%u", i);
4541 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4542 sprintf(name, "luminanceoffset%u", i);
4543 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4544 }
4545
4546 if (ps_compile_args.np2_fixup) {
4547 if (WINEFIXUPINFO_ISVALID(entry)) {
4548 entry->np2Fixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "PsamplerNP2Fixup"));
4549 } else {
4550 FIXME("NP2 texcoord fixup needed for this pixelshader, but no fixup uniform found.\n");
4551 }
4552 }
4553 }
4554
4555 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
4556 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
4557 checkGLcall("Find glsl program uniform locations");
4558
4559 if (pshader
4560 && ((IWineD3DPixelShaderImpl *)pshader)->baseShader.reg_maps.shader_version.major >= 3
4561 && ((IWineD3DPixelShaderImpl *)pshader)->declared_in_count > vec4_varyings(3, gl_info))
4562 {
4563 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
4564 entry->vertex_color_clamp = GL_FALSE;
4565 } else {
4566 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
4567 }
4568
4569 /* Set the shader to allow uniform loading on it */
4570 GL_EXTCALL(glUseProgramObjectARB(programId));
4571 checkGLcall("glUseProgramObjectARB(programId)");
4572
4573 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4574 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4575 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4576 * vertex shader with fixed function pixel processing is used we make sure that the card
4577 * supports enough samplers to allow the max number of vertex samplers with all possible
4578 * fixed function fragment processing setups. So once the program is linked these samplers
4579 * won't change.
4580 */
4581 if (vshader) shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
4582 if (pshader) shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
4583
4584 /* If the local constants do not have to be loaded with the environment constants,
4585 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4586 * later
4587 */
4588 if (pshader && !((IWineD3DBaseShaderImpl *)pshader)->baseShader.load_local_constsF)
4589 {
4590 hardcode_local_constants((IWineD3DBaseShaderImpl *) pshader, gl_info, programId, 'P');
4591 }
4592 if (vshader && !((IWineD3DBaseShaderImpl *)vshader)->baseShader.load_local_constsF)
4593 {
4594 hardcode_local_constants((IWineD3DBaseShaderImpl *) vshader, gl_info, programId, 'V');
4595 }
4596}
4597
4598/* GL locking is done by the caller */
4599static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type)
4600{
4601 GLhandleARB program_id;
4602 GLhandleARB vshader_id, pshader_id;
4603 static const char *blt_vshader[] =
4604 {
4605 "#version 120\n"
4606 "void main(void)\n"
4607 "{\n"
4608 " gl_Position = gl_Vertex;\n"
4609 " gl_FrontColor = vec4(1.0);\n"
4610 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4611 "}\n"
4612 };
4613
4614 static const char *blt_pshaders[tex_type_count] =
4615 {
4616 /* tex_1d */
4617 NULL,
4618 /* tex_2d */
4619 "#version 120\n"
4620 "uniform sampler2D sampler;\n"
4621 "void main(void)\n"
4622 "{\n"
4623 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4624 "}\n",
4625 /* tex_3d */
4626 NULL,
4627 /* tex_cube */
4628 "#version 120\n"
4629 "uniform samplerCube sampler;\n"
4630 "void main(void)\n"
4631 "{\n"
4632 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4633 "}\n",
4634 /* tex_rect */
4635 "#version 120\n"
4636 "#extension GL_ARB_texture_rectangle : enable\n"
4637 "uniform sampler2DRect sampler;\n"
4638 "void main(void)\n"
4639 "{\n"
4640 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4641 "}\n",
4642 };
4643
4644 if (!blt_pshaders[tex_type])
4645 {
4646 FIXME("tex_type %#x not supported\n", tex_type);
4647 tex_type = tex_2d;
4648 }
4649
4650 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4651 GL_EXTCALL(glShaderSourceARB(vshader_id, 1, blt_vshader, NULL));
4652 GL_EXTCALL(glCompileShaderARB(vshader_id));
4653
4654 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4655 GL_EXTCALL(glShaderSourceARB(pshader_id, 1, &blt_pshaders[tex_type], NULL));
4656 GL_EXTCALL(glCompileShaderARB(pshader_id));
4657
4658 program_id = GL_EXTCALL(glCreateProgramObjectARB());
4659 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
4660 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
4661 GL_EXTCALL(glLinkProgramARB(program_id));
4662
4663 shader_glsl_validate_link(gl_info, program_id);
4664
4665 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4666 * is destroyed
4667 */
4668 GL_EXTCALL(glDeleteObjectARB(vshader_id));
4669 GL_EXTCALL(glDeleteObjectARB(pshader_id));
4670 return program_id;
4671}
4672
4673/* GL locking is done by the caller */
4674static void shader_glsl_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
4675{
4676 const struct wined3d_gl_info *gl_info = context->gl_info;
4677 IWineD3DDeviceImpl *device = context_get_device(context);
4678 struct shader_glsl_priv *priv = device->shader_priv;
4679 GLhandleARB program_id = 0;
4680 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
4681
4682 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4683
4684 if (useVS || usePS) set_glsl_shader_program(context, device, usePS, useVS);
4685 else priv->glsl_program = NULL;
4686
4687 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
4688
4689 if (old_vertex_color_clamp != current_vertex_color_clamp)
4690 {
4691 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
4692 {
4693 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
4694 checkGLcall("glClampColorARB");
4695 }
4696 else
4697 {
4698 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4699 }
4700 }
4701
4702 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4703 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4704 GL_EXTCALL(glUseProgramObjectARB(program_id));
4705 checkGLcall("glUseProgramObjectARB");
4706
4707 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
4708 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
4709 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
4710 if (priv->glsl_program && WINEFIXUPINFO_ISVALID(priv->glsl_program))
4711 {
4712 shader_glsl_load_np2fixup_constants((IWineD3DDevice *)device, usePS, useVS);
4713 }
4714}
4715
4716/* GL locking is done by the caller */
4717static void shader_glsl_select_depth_blt(IWineD3DDevice *iface, enum tex_types tex_type) {
4718 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4719 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4720 struct shader_glsl_priv *priv = This->shader_priv;
4721 GLhandleARB *blt_program = &priv->depth_blt_program[tex_type];
4722
4723 if (!*blt_program) {
4724 GLint loc;
4725 *blt_program = create_glsl_blt_shader(gl_info, tex_type);
4726 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
4727 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4728 GL_EXTCALL(glUniform1iARB(loc, 0));
4729 } else {
4730 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
4731 }
4732}
4733
4734/* GL locking is done by the caller */
4735static void shader_glsl_deselect_depth_blt(IWineD3DDevice *iface) {
4736 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4737 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4738 struct shader_glsl_priv *priv = This->shader_priv;
4739 GLhandleARB program_id;
4740
4741 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
4742 if (program_id) TRACE("Using GLSL program %u\n", program_id);
4743
4744 GL_EXTCALL(glUseProgramObjectARB(program_id));
4745 checkGLcall("glUseProgramObjectARB");
4746}
4747
4748static void shader_glsl_destroy(IWineD3DBaseShader *iface) {
4749 const struct list *linked_programs;
4750 IWineD3DBaseShaderImpl *This = (IWineD3DBaseShaderImpl *) iface;
4751 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)This->baseShader.device;
4752 struct shader_glsl_priv *priv = device->shader_priv;
4753 const struct wined3d_gl_info *gl_info;
4754 struct wined3d_context *context;
4755
4756 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
4757 * can be called from IWineD3DBaseShader::Release
4758 */
4759 char pshader = shader_is_pshader_version(This->baseShader.reg_maps.shader_version.type);
4760
4761 if(pshader) {
4762 struct glsl_pshader_private *shader_data;
4763 shader_data = This->baseShader.backend_data;
4764 if(!shader_data || shader_data->num_gl_shaders == 0)
4765 {
4766 HeapFree(GetProcessHeap(), 0, shader_data);
4767 This->baseShader.backend_data = NULL;
4768 return;
4769 }
4770
4771 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4772 gl_info = context->gl_info;
4773
4774 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->pshader == iface)
4775 {
4776 ENTER_GL();
4777 shader_glsl_select(context, FALSE, FALSE);
4778 LEAVE_GL();
4779 }
4780 } else {
4781 struct glsl_vshader_private *shader_data;
4782 shader_data = This->baseShader.backend_data;
4783 if(!shader_data || shader_data->num_gl_shaders == 0)
4784 {
4785 HeapFree(GetProcessHeap(), 0, shader_data);
4786 This->baseShader.backend_data = NULL;
4787 return;
4788 }
4789
4790 context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
4791 gl_info = context->gl_info;
4792
4793 if (priv->glsl_program && (IWineD3DBaseShader *)priv->glsl_program->vshader == iface)
4794 {
4795 ENTER_GL();
4796 shader_glsl_select(context, FALSE, FALSE);
4797 LEAVE_GL();
4798 }
4799 }
4800
4801 linked_programs = &This->baseShader.linked_programs;
4802
4803 TRACE("Deleting linked programs\n");
4804 if (linked_programs->next) {
4805 struct glsl_shader_prog_link *entry, *entry2;
4806
4807 ENTER_GL();
4808 if(pshader) {
4809 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
4810 delete_glsl_program_entry(priv, gl_info, entry);
4811 }
4812 } else {
4813 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, vshader_entry) {
4814 delete_glsl_program_entry(priv, gl_info, entry);
4815 }
4816 }
4817 LEAVE_GL();
4818 }
4819
4820 if(pshader) {
4821 UINT i;
4822 struct glsl_pshader_private *shader_data = This->baseShader.backend_data;
4823
4824 ENTER_GL();
4825 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4826 if (shader_data->gl_shaders[i].context==context_get_current())
4827 {
4828 TRACE("deleting pshader %u\n", shader_data->gl_shaders[i].prgId);
4829 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4830 checkGLcall("glDeleteObjectARB");
4831 }
4832 else
4833 {
4834 WARN("Attempting to delete pshader %u created in ctx %p from ctx %p\n",
4835 shader_data->gl_shaders[i].prgId, shader_data->gl_shaders[i].context, context_get_current());
4836 }
4837 }
4838 LEAVE_GL();
4839 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4840 }
4841 else
4842 {
4843 UINT i;
4844 struct glsl_vshader_private *shader_data = This->baseShader.backend_data;
4845
4846 ENTER_GL();
4847 for(i = 0; i < shader_data->num_gl_shaders; i++) {
4848 if (shader_data->gl_shaders[i].context==context_get_current())
4849 {
4850 TRACE("deleting vshader %u\n", shader_data->gl_shaders[i].prgId);
4851 GL_EXTCALL(glDeleteObjectARB(shader_data->gl_shaders[i].prgId));
4852 checkGLcall("glDeleteObjectARB");
4853 }
4854 else
4855 {
4856 WARN("Attempting to delete vshader %u created in ctx %p from ctx %p\n",
4857 shader_data->gl_shaders[i].prgId, shader_data->gl_shaders[i].context, context_get_current());
4858 }
4859 }
4860 LEAVE_GL();
4861 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders);
4862 }
4863
4864 HeapFree(GetProcessHeap(), 0, This->baseShader.backend_data);
4865 This->baseShader.backend_data = NULL;
4866
4867 context_release(context);
4868}
4869
4870static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
4871{
4872 const glsl_program_key_t *k = key;
4873 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
4874 const struct glsl_shader_prog_link, program_lookup_entry);
4875 int cmp;
4876
4877 if (k->context > prog->context) return 1;
4878 else if (k->context < prog->context) return -1;
4879
4880 if (k->vshader > prog->vshader) return 1;
4881 else if (k->vshader < prog->vshader) return -1;
4882
4883 if (k->pshader > prog->pshader) return 1;
4884 else if (k->pshader < prog->pshader) return -1;
4885
4886 if (k->vshader && (cmp = memcmp(&k->vs_args, &prog->vs_args, sizeof(prog->vs_args)))) return cmp;
4887 if (k->pshader && (cmp = memcmp(&k->ps_args, &prog->ps_args, sizeof(prog->ps_args)))) return cmp;
4888
4889 return 0;
4890}
4891
4892static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
4893{
4894 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
4895 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
4896
4897 if (!mem)
4898 {
4899 ERR("Failed to allocate memory\n");
4900 return FALSE;
4901 }
4902
4903 heap->entries = mem;
4904 heap->entries[1].version = 0;
4905 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
4906 heap->size = 1;
4907
4908 return TRUE;
4909}
4910
4911static void constant_heap_free(struct constant_heap *heap)
4912{
4913 HeapFree(GetProcessHeap(), 0, heap->entries);
4914}
4915
4916static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
4917{
4918 wined3d_rb_alloc,
4919 wined3d_rb_realloc,
4920 wined3d_rb_free,
4921 glsl_program_key_compare,
4922};
4923
4924static HRESULT shader_glsl_alloc(IWineD3DDevice *iface) {
4925 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4926 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4927 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
4928 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
4929 gl_info->limits.glsl_ps_float_constants)) + 1;
4930
4931 if (!shader_buffer_init(&priv->shader_buffer))
4932 {
4933 ERR("Failed to initialize shader buffer.\n");
4934 goto fail;
4935 }
4936
4937 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
4938 if (!priv->stack)
4939 {
4940 ERR("Failed to allocate memory.\n");
4941 goto fail;
4942 }
4943
4944 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
4945 {
4946 ERR("Failed to initialize vertex shader constant heap\n");
4947 goto fail;
4948 }
4949
4950 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
4951 {
4952 ERR("Failed to initialize pixel shader constant heap\n");
4953 goto fail;
4954 }
4955
4956 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
4957 {
4958 ERR("Failed to initialize rbtree.\n");
4959 goto fail;
4960 }
4961
4962 priv->next_constant_version = 1;
4963
4964 This->shader_priv = priv;
4965 return WINED3D_OK;
4966
4967fail:
4968 constant_heap_free(&priv->pconst_heap);
4969 constant_heap_free(&priv->vconst_heap);
4970 HeapFree(GetProcessHeap(), 0, priv->stack);
4971 shader_buffer_free(&priv->shader_buffer);
4972 HeapFree(GetProcessHeap(), 0, priv);
4973 return E_OUTOFMEMORY;
4974}
4975
4976/* Context activation is done by the caller. */
4977static void shader_glsl_free(IWineD3DDevice *iface) {
4978 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
4979 const struct wined3d_gl_info *gl_info = &This->adapter->gl_info;
4980 struct shader_glsl_priv *priv = This->shader_priv;
4981 int i;
4982
4983 ENTER_GL();
4984 for (i = 0; i < tex_type_count; ++i)
4985 {
4986 if (priv->depth_blt_program[i])
4987 {
4988 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program[i]));
4989 }
4990 }
4991 LEAVE_GL();
4992
4993 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
4994 constant_heap_free(&priv->pconst_heap);
4995 constant_heap_free(&priv->vconst_heap);
4996 HeapFree(GetProcessHeap(), 0, priv->stack);
4997 shader_buffer_free(&priv->shader_buffer);
4998
4999 HeapFree(GetProcessHeap(), 0, This->shader_priv);
5000 This->shader_priv = NULL;
5001}
5002
5003static BOOL shader_glsl_dirty_const(IWineD3DDevice *iface) {
5004 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
5005 return FALSE;
5006}
5007
5008static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *pCaps)
5009{
5010 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
5011 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support based
5012 * on the version of NV_vertex_program.
5013 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
5014 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
5015 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
5016 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
5017 */
5018 if ((gl_info->supported[NV_VERTEX_PROGRAM2] && !gl_info->supported[NV_VERTEX_PROGRAM3])
5019 || gl_info->limits.arb_ps_instructions <= 512)
5020 pCaps->VertexShaderVersion = WINED3DVS_VERSION(2,0);
5021 else
5022 pCaps->VertexShaderVersion = WINED3DVS_VERSION(3,0);
5023 TRACE_(d3d_caps)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps->VertexShaderVersion >> 8) & 0xff, pCaps->VertexShaderVersion & 0xff);
5024 pCaps->MaxVertexShaderConst = gl_info->limits.glsl_vs_float_constants;
5025
5026 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
5027 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
5028 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
5029 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
5030 * in max native instructions. Intel and others also offer the info in this extension but they
5031 * don't support GLSL (at least on Windows).
5032 *
5033 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
5034 * of instructions is 512 or less we have to do with ps2.0 hardware.
5035 * 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.
5036 */
5037 if ((gl_info->supported[NV_FRAGMENT_PROGRAM] && !gl_info->supported[NV_FRAGMENT_PROGRAM2])
5038 || gl_info->limits.arb_ps_instructions <= 512)
5039 pCaps->PixelShaderVersion = WINED3DPS_VERSION(2,0);
5040 else
5041 pCaps->PixelShaderVersion = WINED3DPS_VERSION(3,0);
5042
5043 pCaps->MaxPixelShaderConst = gl_info->limits.glsl_ps_float_constants;
5044
5045 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
5046 * Direct3D minimum requirement.
5047 *
5048 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
5049 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
5050 *
5051 * The problem is that the refrast clamps temporary results in the shader to
5052 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
5053 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
5054 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
5055 * offer a way to query this.
5056 */
5057 pCaps->PixelShader1xMaxValue = 8.0;
5058 TRACE_(d3d_caps)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps->PixelShaderVersion >> 8) & 0xff, pCaps->PixelShaderVersion & 0xff);
5059
5060 pCaps->VSClipping = TRUE;
5061}
5062
5063static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
5064{
5065 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
5066 {
5067 TRACE("Checking support for fixup:\n");
5068 dump_color_fixup_desc(fixup);
5069 }
5070
5071 /* We support everything except YUV conversions. */
5072 if (!is_complex_fixup(fixup))
5073 {
5074 TRACE("[OK]\n");
5075 return TRUE;
5076 }
5077
5078 TRACE("[FAILED]\n");
5079 return FALSE;
5080}
5081
5082static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
5083{
5084 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
5085 /* WINED3DSIH_ADD */ shader_glsl_arith,
5086 /* WINED3DSIH_BEM */ shader_glsl_bem,
5087 /* WINED3DSIH_BREAK */ shader_glsl_break,
5088 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
5089 /* WINED3DSIH_BREAKP */ NULL,
5090 /* WINED3DSIH_CALL */ shader_glsl_call,
5091 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
5092 /* WINED3DSIH_CMP */ shader_glsl_cmp,
5093 /* WINED3DSIH_CND */ shader_glsl_cnd,
5094 /* WINED3DSIH_CRS */ shader_glsl_cross,
5095 /* WINED3DSIH_CUT */ NULL,
5096 /* WINED3DSIH_DCL */ NULL,
5097 /* WINED3DSIH_DEF */ NULL,
5098 /* WINED3DSIH_DEFB */ NULL,
5099 /* WINED3DSIH_DEFI */ NULL,
5100 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
5101 /* WINED3DSIH_DP3 */ shader_glsl_dot,
5102 /* WINED3DSIH_DP4 */ shader_glsl_dot,
5103 /* WINED3DSIH_DST */ shader_glsl_dst,
5104 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
5105 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
5106 /* WINED3DSIH_ELSE */ shader_glsl_else,
5107 /* WINED3DSIH_EMIT */ NULL,
5108 /* WINED3DSIH_ENDIF */ shader_glsl_end,
5109 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
5110 /* WINED3DSIH_ENDREP */ shader_glsl_end,
5111 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
5112 /* WINED3DSIH_EXPP */ shader_glsl_expp,
5113 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
5114 /* WINED3DSIH_IADD */ NULL,
5115 /* WINED3DSIH_IF */ shader_glsl_if,
5116 /* WINED3DSIH_IFC */ shader_glsl_ifc,
5117 /* WINED3DSIH_IGE */ NULL,
5118 /* WINED3DSIH_LABEL */ shader_glsl_label,
5119 /* WINED3DSIH_LIT */ shader_glsl_lit,
5120 /* WINED3DSIH_LOG */ shader_glsl_log,
5121 /* WINED3DSIH_LOGP */ shader_glsl_log,
5122 /* WINED3DSIH_LOOP */ shader_glsl_loop,
5123 /* WINED3DSIH_LRP */ shader_glsl_lrp,
5124 /* WINED3DSIH_LT */ NULL,
5125 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
5126 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
5127 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
5128 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
5129 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
5130 /* WINED3DSIH_MAD */ shader_glsl_mad,
5131 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
5132 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
5133 /* WINED3DSIH_MOV */ shader_glsl_mov,
5134 /* WINED3DSIH_MOVA */ shader_glsl_mov,
5135 /* WINED3DSIH_MUL */ shader_glsl_arith,
5136 /* WINED3DSIH_NOP */ NULL,
5137 /* WINED3DSIH_NRM */ shader_glsl_nrm,
5138 /* WINED3DSIH_PHASE */ NULL,
5139 /* WINED3DSIH_POW */ shader_glsl_pow,
5140 /* WINED3DSIH_RCP */ shader_glsl_rcp,
5141 /* WINED3DSIH_REP */ shader_glsl_rep,
5142 /* WINED3DSIH_RET */ shader_glsl_ret,
5143 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
5144 /* WINED3DSIH_SETP */ NULL,
5145 /* WINED3DSIH_SGE */ shader_glsl_compare,
5146 /* WINED3DSIH_SGN */ shader_glsl_sgn,
5147 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
5148 /* WINED3DSIH_SLT */ shader_glsl_compare,
5149 /* WINED3DSIH_SUB */ shader_glsl_arith,
5150 /* WINED3DSIH_TEX */ shader_glsl_tex,
5151 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
5152 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
5153 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
5154 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
5155 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
5156 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
5157 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
5158 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
5159 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
5160 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
5161 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
5162 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
5163 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
5164 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
5165 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
5166 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
5167 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
5168 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
5169 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
5170 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
5171 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
5172};
5173
5174static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
5175 SHADER_HANDLER hw_fct;
5176
5177 /* Select handler */
5178 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
5179
5180 /* Unhandled opcode */
5181 if (!hw_fct)
5182 {
5183 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
5184 return;
5185 }
5186 hw_fct(ins);
5187
5188 shader_glsl_add_instruction_modifiers(ins);
5189}
5190
5191const shader_backend_t glsl_shader_backend = {
5192 shader_glsl_handle_instruction,
5193 shader_glsl_select,
5194 shader_glsl_select_depth_blt,
5195 shader_glsl_deselect_depth_blt,
5196 shader_glsl_update_float_vertex_constants,
5197 shader_glsl_update_float_pixel_constants,
5198 shader_glsl_load_constants,
5199 shader_glsl_load_np2fixup_constants,
5200 shader_glsl_destroy,
5201 shader_glsl_alloc,
5202 shader_glsl_free,
5203 shader_glsl_dirty_const,
5204 shader_glsl_get_caps,
5205 shader_glsl_color_fixup_supported,
5206};
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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