VirtualBox

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

Last change on this file since 22321 was 22321, checked in by vboxsync, 16 years ago

crOpenGL: wine proper change for snprintf/_snprintf etc.

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

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