VirtualBox

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

Last change on this file since 103417 was 103417, checked in by vboxsync, 9 months ago

Devices/Graphics,VMM: Fix some unused function warnings, bugref:3409

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