VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/shaderlib/glsl_shader.c@ 53277

Last change on this file since 53277 was 53277, checked in by vboxsync, 10 years ago

glsl_shader.c: Corrected return value of shader_glsl_generate_vshader and shader_glsl_generate_pshader. Corrected WINEFIXUPINFO_NOINDEX and WINEFIXUPINFO_INIT (both made sure the NOINDEX stuff never worked). Fixed printf format warnings on logging.

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

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