VirtualBox

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

Last change on this file since 83176 was 83176, checked in by vboxsync, 5 years ago

Devices/Graphics/shaderlib: workaround for NVidia GLSL compiler quirk.

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