2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
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.
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.
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
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
33 #include "wine/port.h"
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
49 #define WINED3D_GLSL_SAMPLE_LOD 0x4
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
64 struct glsl_sample_function
66 struct wined3d_string_buffer
*name
;
68 enum wined3d_data_type data_type
;
69 BOOL output_single_component
;
74 HEAP_NODE_TRAVERSE_LEFT
,
75 HEAP_NODE_TRAVERSE_RIGHT
,
87 struct constant_entry
*entries
;
89 unsigned int *positions
;
93 /* GLSL shader private data */
94 struct shader_glsl_priv
{
95 struct wined3d_string_buffer shader_buffer
;
96 struct wined3d_string_buffer_list string_buffers
;
97 struct wine_rb_tree program_lookup
;
98 struct constant_heap vconst_heap
;
99 struct constant_heap pconst_heap
;
100 unsigned char *stack
;
101 GLuint depth_blt_program_full
[WINED3D_GL_RES_TYPE_COUNT
];
102 GLuint depth_blt_program_masked
[WINED3D_GL_RES_TYPE_COUNT
];
103 UINT next_constant_version
;
105 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
106 const struct fragment_pipeline
*fragment_pipe
;
107 struct wine_rb_tree ffp_vertex_shaders
;
108 struct wine_rb_tree ffp_fragment_shaders
;
109 BOOL ffp_proj_control
;
110 BOOL legacy_lighting
;
113 struct glsl_vs_program
115 struct list shader_entry
;
117 GLenum vertex_color_clamp
;
118 GLint
*uniform_f_locations
;
119 GLint uniform_i_locations
[MAX_CONST_I
];
120 GLint uniform_b_locations
[MAX_CONST_B
];
121 GLint pos_fixup_location
;
123 GLint modelview_matrix_location
[MAX_VERTEX_BLENDS
];
124 GLint projection_matrix_location
;
125 GLint normal_matrix_location
;
126 GLint texture_matrix_location
[MAX_TEXTURES
];
127 GLint material_ambient_location
;
128 GLint material_diffuse_location
;
129 GLint material_specular_location
;
130 GLint material_emissive_location
;
131 GLint material_shininess_location
;
132 GLint light_ambient_location
;
147 } light_location
[MAX_ACTIVE_LIGHTS
];
148 GLint pointsize_location
;
149 GLint pointsize_min_location
;
150 GLint pointsize_max_location
;
151 GLint pointsize_c_att_location
;
152 GLint pointsize_l_att_location
;
153 GLint pointsize_q_att_location
;
156 struct glsl_gs_program
158 struct list shader_entry
;
162 struct glsl_ps_program
164 struct list shader_entry
;
166 GLint
*uniform_f_locations
;
167 GLint uniform_i_locations
[MAX_CONST_I
];
168 GLint uniform_b_locations
[MAX_CONST_B
];
169 GLint bumpenv_mat_location
[MAX_TEXTURES
];
170 GLint bumpenv_lum_scale_location
[MAX_TEXTURES
];
171 GLint bumpenv_lum_offset_location
[MAX_TEXTURES
];
172 GLint tss_constant_location
[MAX_TEXTURES
];
173 GLint tex_factor_location
;
174 GLint specular_enable_location
;
175 GLint fog_color_location
;
176 GLint fog_density_location
;
177 GLint fog_end_location
;
178 GLint fog_scale_location
;
179 GLint ycorrection_location
;
180 GLint np2_fixup_location
;
181 GLint color_key_location
;
182 const struct ps_np2fixup_info
*np2_fixup_info
;
185 /* Struct to maintain data about a linked GLSL program */
186 struct glsl_shader_prog_link
188 struct wine_rb_entry program_lookup_entry
;
189 struct glsl_vs_program vs
;
190 struct glsl_gs_program gs
;
191 struct glsl_ps_program ps
;
193 DWORD constant_update_mask
;
194 UINT constant_version
;
197 struct glsl_program_key
204 struct shader_glsl_ctx_priv
{
205 const struct vs_compile_args
*cur_vs_args
;
206 const struct ps_compile_args
*cur_ps_args
;
207 struct ps_np2fixup_info
*cur_np2fixup_info
;
208 struct wined3d_string_buffer_list
*string_buffers
;
211 struct glsl_context_data
213 struct glsl_shader_prog_link
*glsl_program
;
216 struct glsl_ps_compiled_shader
218 struct ps_compile_args args
;
219 struct ps_np2fixup_info np2fixup
;
223 struct glsl_vs_compiled_shader
225 struct vs_compile_args args
;
229 struct glsl_gs_compiled_shader
234 struct glsl_shader_private
238 struct glsl_vs_compiled_shader
*vs
;
239 struct glsl_gs_compiled_shader
*gs
;
240 struct glsl_ps_compiled_shader
*ps
;
242 UINT num_gl_shaders
, shader_array_size
;
245 struct glsl_ffp_vertex_shader
247 struct wined3d_ffp_vs_desc desc
;
249 struct list linked_programs
;
252 struct glsl_ffp_fragment_shader
254 struct ffp_frag_desc entry
;
256 struct list linked_programs
;
259 struct glsl_ffp_destroy_ctx
261 struct shader_glsl_priv
*priv
;
262 const struct wined3d_gl_info
*gl_info
;
265 static const char *debug_gl_shader_type(GLenum type
)
269 #define WINED3D_TO_STR(u) case u: return #u
270 WINED3D_TO_STR(GL_VERTEX_SHADER
);
271 WINED3D_TO_STR(GL_GEOMETRY_SHADER
);
272 WINED3D_TO_STR(GL_FRAGMENT_SHADER
);
273 #undef WINED3D_TO_STR
275 return wine_dbg_sprintf("UNKNOWN(%#x)", type
);
279 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type
)
283 case WINED3D_SHADER_TYPE_VERTEX
:
286 case WINED3D_SHADER_TYPE_GEOMETRY
:
289 case WINED3D_SHADER_TYPE_PIXEL
:
293 FIXME("Unhandled shader type %#x.\n", type
);
298 static const char *shader_glsl_get_version(const struct wined3d_gl_info
*gl_info
,
299 const struct wined3d_shader_version
*version
)
301 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
302 return "#version 150";
303 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 30) && version
&& version
->major
>= 4)
304 return "#version 130";
306 return "#version 120";
309 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer
*buffer
, const float *values
)
313 wined3d_ftoa(values
[0], str
[0]);
314 wined3d_ftoa(values
[1], str
[1]);
315 wined3d_ftoa(values
[2], str
[2]);
316 wined3d_ftoa(values
[3], str
[3]);
317 shader_addline(buffer
, "vec4(%s, %s, %s, %s)", str
[0], str
[1], str
[2], str
[3]);
320 static const char *get_info_log_line(const char **ptr
)
325 if (!(q
= strstr(p
, "\n")))
327 if (!*p
) return NULL
;
336 /* Context activation is done by the caller. */
337 void print_glsl_info_log(const struct wined3d_gl_info
*gl_info
, GLuint id
, BOOL program
)
342 if (!WARN_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
346 GL_EXTCALL(glGetProgramiv(id
, GL_INFO_LOG_LENGTH
, &length
));
348 GL_EXTCALL(glGetShaderiv(id
, GL_INFO_LOG_LENGTH
, &length
));
350 /* A size of 1 is just a null-terminated string, so the log should be bigger than
351 * that if there are errors. */
354 const char *ptr
, *line
;
356 log
= HeapAlloc(GetProcessHeap(), 0, length
);
357 /* The info log is supposed to be zero-terminated, but at least some
358 * versions of fglrx don't terminate the string properly. The reported
359 * length does include the terminator, so explicitly set it to zero
363 GL_EXTCALL(glGetProgramInfoLog(id
, length
, NULL
, log
));
365 GL_EXTCALL(glGetShaderInfoLog(id
, length
, NULL
, log
));
368 if (gl_info
->quirks
& WINED3D_QUIRK_INFO_LOG_SPAM
)
370 WARN("Info log received from GLSL shader #%u:\n", id
);
371 while ((line
= get_info_log_line(&ptr
))) WARN(" %.*s", (int)(ptr
- line
), line
);
375 FIXME("Info log received from GLSL shader #%u:\n", id
);
376 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
378 HeapFree(GetProcessHeap(), 0, log
);
382 /* Context activation is done by the caller. */
383 static void shader_glsl_compile(const struct wined3d_gl_info
*gl_info
, GLuint shader
, const char *src
)
385 const char *ptr
, *line
;
387 TRACE("Compiling shader object %u.\n", shader
);
389 if (TRACE_ON(d3d_shader
))
392 while ((line
= get_info_log_line(&ptr
))) TRACE_(d3d_shader
)(" %.*s", (int)(ptr
- line
), line
);
395 GL_EXTCALL(glShaderSource(shader
, 1, &src
, NULL
));
396 checkGLcall("glShaderSource");
397 GL_EXTCALL(glCompileShader(shader
));
398 checkGLcall("glCompileShader");
399 print_glsl_info_log(gl_info
, shader
, FALSE
);
402 /* Context activation is done by the caller. */
403 static void shader_glsl_dump_program_source(const struct wined3d_gl_info
*gl_info
, GLuint program
)
405 GLint i
, shader_count
, source_size
= -1;
409 GL_EXTCALL(glGetProgramiv(program
, GL_ATTACHED_SHADERS
, &shader_count
));
410 shaders
= HeapAlloc(GetProcessHeap(), 0, shader_count
* sizeof(*shaders
));
413 ERR("Failed to allocate shader array memory.\n");
417 GL_EXTCALL(glGetAttachedShaders(program
, shader_count
, NULL
, shaders
));
418 for (i
= 0; i
< shader_count
; ++i
)
420 const char *ptr
, *line
;
423 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_SOURCE_LENGTH
, &tmp
));
425 if (source_size
< tmp
)
427 HeapFree(GetProcessHeap(), 0, source
);
429 source
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, tmp
);
432 ERR("Failed to allocate %d bytes for shader source.\n", tmp
);
433 HeapFree(GetProcessHeap(), 0, shaders
);
439 FIXME("Shader %u:\n", shaders
[i
]);
440 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_TYPE
, &tmp
));
441 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp
));
442 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_COMPILE_STATUS
, &tmp
));
443 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp
);
447 GL_EXTCALL(glGetShaderSource(shaders
[i
], source_size
, NULL
, source
));
448 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
452 HeapFree(GetProcessHeap(), 0, source
);
453 HeapFree(GetProcessHeap(), 0, shaders
);
456 /* Context activation is done by the caller. */
457 void shader_glsl_validate_link(const struct wined3d_gl_info
*gl_info
, GLuint program
)
461 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
464 GL_EXTCALL(glGetProgramiv(program
, GL_LINK_STATUS
, &tmp
));
467 FIXME("Program %u link status invalid.\n", program
);
468 shader_glsl_dump_program_source(gl_info
, program
);
471 print_glsl_info_log(gl_info
, program
, TRUE
);
474 /* Context activation is done by the caller. */
475 static void shader_glsl_load_samplers(const struct wined3d_gl_info
*gl_info
,
476 struct shader_glsl_priv
*priv
, const DWORD
*tex_unit_map
, GLuint program_id
)
478 unsigned int mapped_unit
;
479 struct wined3d_string_buffer
*sampler_name
= string_buffer_get(&priv
->string_buffers
);
486 enum wined3d_shader_type type
;
487 unsigned int base_idx
;
492 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
493 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
496 for (i
= 0; i
< ARRAY_SIZE(sampler_info
); ++i
)
498 prefix
= shader_glsl_get_prefix(sampler_info
[i
].type
);
500 for (j
= 0; j
< sampler_info
[i
].count
; ++j
)
502 string_buffer_sprintf(sampler_name
, "%s_sampler%u", prefix
, j
);
503 name_loc
= GL_EXTCALL(glGetUniformLocation(program_id
, sampler_name
->buffer
));
507 mapped_unit
= tex_unit_map
[sampler_info
[i
].base_idx
+ j
];
508 if (mapped_unit
== WINED3D_UNMAPPED_STAGE
|| mapped_unit
>= gl_info
->limits
.combined_samplers
)
510 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name
->buffer
, mapped_unit
);
514 TRACE("Loading sampler %s on unit %u.\n", sampler_name
->buffer
, mapped_unit
);
515 GL_EXTCALL(glUniform1i(name_loc
, mapped_unit
));
518 checkGLcall("glUniform1i");
519 string_buffer_release(&priv
->string_buffers
, sampler_name
);
522 /* Context activation is done by the caller. */
523 static inline void walk_constant_heap(const struct wined3d_gl_info
*gl_info
, const float *constants
,
524 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
526 unsigned int start
= ~0U, end
= 0;
528 unsigned int heap_idx
= 1;
531 if (heap
->entries
[heap_idx
].version
<= version
) return;
533 idx
= heap
->entries
[heap_idx
].idx
;
534 if (constant_locations
[idx
] != -1)
536 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
538 while (stack_idx
>= 0)
540 /* Note that we fall through to the next case statement. */
541 switch(stack
[stack_idx
])
543 case HEAP_NODE_TRAVERSE_LEFT
:
545 unsigned int left_idx
= heap_idx
<< 1;
546 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
549 idx
= heap
->entries
[heap_idx
].idx
;
550 if (constant_locations
[idx
] != -1)
558 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
559 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
564 case HEAP_NODE_TRAVERSE_RIGHT
:
566 unsigned int right_idx
= (heap_idx
<< 1) + 1;
567 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
569 heap_idx
= right_idx
;
570 idx
= heap
->entries
[heap_idx
].idx
;
571 if (constant_locations
[idx
] != -1)
579 stack
[stack_idx
++] = HEAP_NODE_POP
;
580 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
592 GL_EXTCALL(glUniform4fv(constant_locations
[start
], end
- start
+ 1, &constants
[start
* 4]));
593 checkGLcall("walk_constant_heap()");
596 /* Context activation is done by the caller. */
597 static inline void apply_clamped_constant(const struct wined3d_gl_info
*gl_info
, GLint location
, const GLfloat
*data
)
599 GLfloat clamped_constant
[4];
601 if (location
== -1) return;
603 clamped_constant
[0] = data
[0] < -1.0f
? -1.0f
: data
[0] > 1.0f
? 1.0f
: data
[0];
604 clamped_constant
[1] = data
[1] < -1.0f
? -1.0f
: data
[1] > 1.0f
? 1.0f
: data
[1];
605 clamped_constant
[2] = data
[2] < -1.0f
? -1.0f
: data
[2] > 1.0f
? 1.0f
: data
[2];
606 clamped_constant
[3] = data
[3] < -1.0f
? -1.0f
: data
[3] > 1.0f
? 1.0f
: data
[3];
608 GL_EXTCALL(glUniform4fv(location
, 1, clamped_constant
));
611 /* Context activation is done by the caller. */
612 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info
*gl_info
, const float *constants
,
613 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
616 unsigned int heap_idx
= 1;
619 if (heap
->entries
[heap_idx
].version
<= version
) return;
621 idx
= heap
->entries
[heap_idx
].idx
;
622 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
623 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
625 while (stack_idx
>= 0)
627 /* Note that we fall through to the next case statement. */
628 switch(stack
[stack_idx
])
630 case HEAP_NODE_TRAVERSE_LEFT
:
632 unsigned int left_idx
= heap_idx
<< 1;
633 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
636 idx
= heap
->entries
[heap_idx
].idx
;
637 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
639 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
640 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
645 case HEAP_NODE_TRAVERSE_RIGHT
:
647 unsigned int right_idx
= (heap_idx
<< 1) + 1;
648 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
650 heap_idx
= right_idx
;
651 idx
= heap
->entries
[heap_idx
].idx
;
652 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
654 stack
[stack_idx
++] = HEAP_NODE_POP
;
655 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
666 checkGLcall("walk_constant_heap_clamped()");
669 /* Context activation is done by the caller. */
670 static void shader_glsl_load_constantsF(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
671 const float *constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
672 unsigned char *stack
, UINT version
)
674 const struct wined3d_shader_lconst
*lconst
;
676 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
677 if (shader
->reg_maps
.shader_version
.major
== 1
678 && shader
->reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
679 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
681 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
683 if (!shader
->load_local_constsF
)
685 TRACE("No need to load local float constants for this shader\n");
689 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
690 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
692 GL_EXTCALL(glUniform4fv(constant_locations
[lconst
->idx
], 1, (const GLfloat
*)lconst
->value
));
694 checkGLcall("glUniform4fv()");
697 /* Context activation is done by the caller. */
698 static void shader_glsl_load_constantsI(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
699 const GLint locations
[MAX_CONST_I
], const int *constants
, WORD constants_set
)
704 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
706 if (!(constants_set
& 1)) continue;
708 /* We found this uniform name in the program - go ahead and send the data */
709 GL_EXTCALL(glUniform4iv(locations
[i
], 1, &constants
[i
* 4]));
712 /* Load immediate constants */
713 ptr
= list_head(&shader
->constantsI
);
716 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
717 unsigned int idx
= lconst
->idx
;
718 const GLint
*values
= (const GLint
*)lconst
->value
;
720 /* We found this uniform name in the program - go ahead and send the data */
721 GL_EXTCALL(glUniform4iv(locations
[idx
], 1, values
));
722 ptr
= list_next(&shader
->constantsI
, ptr
);
724 checkGLcall("glUniform4iv()");
727 /* Context activation is done by the caller. */
728 static void shader_glsl_load_constantsB(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
729 const GLint locations
[MAX_CONST_B
], const BOOL
*constants
, WORD constants_set
)
734 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
736 if (!(constants_set
& 1)) continue;
738 GL_EXTCALL(glUniform1iv(locations
[i
], 1, &constants
[i
]));
741 /* Load immediate constants */
742 ptr
= list_head(&shader
->constantsB
);
745 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
746 unsigned int idx
= lconst
->idx
;
747 const GLint
*values
= (const GLint
*)lconst
->value
;
749 GL_EXTCALL(glUniform1iv(locations
[idx
], 1, values
));
750 ptr
= list_next(&shader
->constantsB
, ptr
);
752 checkGLcall("glUniform1iv()");
755 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
757 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
760 /* Context activation is done by the caller (state handler). */
761 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program
*ps
,
762 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
764 GLfloat np2fixup_constants
[4 * MAX_FRAGMENT_SAMPLERS
];
765 UINT fixup
= ps
->np2_fixup_info
->active
;
768 for (i
= 0; fixup
; fixup
>>= 1, ++i
)
770 const struct wined3d_texture
*tex
= state
->textures
[i
];
771 unsigned char idx
= ps
->np2_fixup_info
->idx
[i
];
772 GLfloat
*tex_dim
= &np2fixup_constants
[(idx
>> 1) * 4];
776 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
782 tex_dim
[2] = tex
->pow2_matrix
[0];
783 tex_dim
[3] = tex
->pow2_matrix
[5];
787 tex_dim
[0] = tex
->pow2_matrix
[0];
788 tex_dim
[1] = tex
->pow2_matrix
[5];
792 GL_EXTCALL(glUniform4fv(ps
->np2_fixup_location
, ps
->np2_fixup_info
->num_consts
, np2fixup_constants
));
795 /* Taken and adapted from Mesa. */
796 static BOOL
invert_matrix_3d(struct wined3d_matrix
*out
, const struct wined3d_matrix
*in
)
798 float pos
, neg
, t
, det
;
799 struct wined3d_matrix temp
;
801 /* Calculate the determinant of upper left 3x3 submatrix and
802 * determine if the matrix is singular. */
804 t
= in
->_11
* in
->_22
* in
->_33
;
810 t
= in
->_21
* in
->_32
* in
->_13
;
815 t
= in
->_31
* in
->_12
* in
->_23
;
821 t
= -in
->_31
* in
->_22
* in
->_13
;
826 t
= -in
->_21
* in
->_12
* in
->_33
;
832 t
= -in
->_11
* in
->_32
* in
->_23
;
840 if (fabsf(det
) < 1e-25f
)
844 temp
._11
= (in
->_22
* in
->_33
- in
->_32
* in
->_23
) * det
;
845 temp
._12
= -(in
->_12
* in
->_33
- in
->_32
* in
->_13
) * det
;
846 temp
._13
= (in
->_12
* in
->_23
- in
->_22
* in
->_13
) * det
;
847 temp
._21
= -(in
->_21
* in
->_33
- in
->_31
* in
->_23
) * det
;
848 temp
._22
= (in
->_11
* in
->_33
- in
->_31
* in
->_13
) * det
;
849 temp
._23
= -(in
->_11
* in
->_23
- in
->_21
* in
->_13
) * det
;
850 temp
._31
= (in
->_21
* in
->_32
- in
->_31
* in
->_22
) * det
;
851 temp
._32
= -(in
->_11
* in
->_32
- in
->_31
* in
->_12
) * det
;
852 temp
._33
= (in
->_11
* in
->_22
- in
->_21
* in
->_12
) * det
;
858 static void swap_rows(float **a
, float **b
)
866 static BOOL
invert_matrix(struct wined3d_matrix
*out
, struct wined3d_matrix
*m
)
869 float m0
, m1
, m2
, m3
, s
;
870 float *r0
, *r1
, *r2
, *r3
;
882 r0
[5] = r0
[6] = r0
[7] = 0.0f
;
889 r1
[4] = r1
[6] = r1
[7] = 0.0f
;
896 r2
[4] = r2
[5] = r2
[7] = 0.0f
;
903 r3
[4] = r3
[5] = r3
[6] = 0.0f
;
905 /* Choose pivot - or die. */
906 if (fabsf(r3
[0]) > fabsf(r2
[0]))
908 if (fabsf(r2
[0]) > fabsf(r1
[0]))
910 if (fabsf(r1
[0]) > fabsf(r0
[0]))
915 /* Eliminate first variable. */
916 m1
= r1
[0] / r0
[0]; m2
= r2
[0] / r0
[0]; m3
= r3
[0] / r0
[0];
917 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
918 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
919 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
949 /* Choose pivot - or die. */
950 if (fabsf(r3
[1]) > fabsf(r2
[1]))
952 if (fabsf(r2
[1]) > fabsf(r1
[1]))
957 /* Eliminate second variable. */
958 m2
= r2
[1] / r1
[1]; m3
= r3
[1] / r1
[1];
959 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
960 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
986 /* Choose pivot - or die. */
987 if (fabsf(r3
[2]) > fabsf(r2
[2]))
992 /* Eliminate third variable. */
1004 /* Back substitute row 3. */
1011 /* Back substitute row 2. */
1014 r2
[4] = s
* (r2
[4] - r3
[4] * m2
);
1015 r2
[5] = s
* (r2
[5] - r3
[5] * m2
);
1016 r2
[6] = s
* (r2
[6] - r3
[6] * m2
);
1017 r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
1019 r1
[4] -= r3
[4] * m1
;
1020 r1
[5] -= r3
[5] * m1
;
1021 r1
[6] -= r3
[6] * m1
;
1022 r1
[7] -= r3
[7] * m1
;
1024 r0
[4] -= r3
[4] * m0
;
1025 r0
[5] -= r3
[5] * m0
;
1026 r0
[6] -= r3
[6] * m0
;
1027 r0
[7] -= r3
[7] * m0
;
1029 /* Back substitute row 1. */
1032 r1
[4] = s
* (r1
[4] - r2
[4] * m1
);
1033 r1
[5] = s
* (r1
[5] - r2
[5] * m1
);
1034 r1
[6] = s
* (r1
[6] - r2
[6] * m1
);
1035 r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
1037 r0
[4] -= r2
[4] * m0
;
1038 r0
[5] -= r2
[5] * m0
;
1039 r0
[6] -= r2
[6] * m0
;
1040 r0
[7] -= r2
[7] * m0
;
1042 /* Back substitute row 0. */
1045 r0
[4] = s
* (r0
[4] - r1
[4] * m0
);
1046 r0
[5] = s
* (r0
[5] - r1
[5] * m0
);
1047 r0
[6] = s
* (r0
[6] - r1
[6] * m0
);
1048 r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
1070 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context
*context
,
1071 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1073 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1075 struct wined3d_matrix mv
;
1078 if (prog
->vs
.normal_matrix_location
== -1)
1081 get_modelview_matrix(context
, state
, 0, &mv
);
1082 if (context
->swapchain
->device
->wined3d
->flags
& WINED3D_LEGACY_FFP_LIGHTING
)
1083 invert_matrix_3d(&mv
, &mv
);
1085 invert_matrix(&mv
, &mv
);
1086 /* Tests show that singular modelview matrices are used unchanged as normal
1087 * matrices on D3D3 and older. There seems to be no clearly consistent
1088 * behavior on newer D3D versions so always follow older ddraw behavior. */
1089 for (i
= 0; i
< 3; ++i
)
1090 for (j
= 0; j
< 3; ++j
)
1091 mat
[i
* 3 + j
] = (&mv
._11
)[j
* 4 + i
];
1093 GL_EXTCALL(glUniformMatrix3fv(prog
->vs
.normal_matrix_location
, 1, FALSE
, mat
));
1094 checkGLcall("glUniformMatrix3fv");
1097 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context
*context
,
1098 const struct wined3d_state
*state
, unsigned int tex
, struct glsl_shader_prog_link
*prog
)
1100 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1101 struct wined3d_matrix mat
;
1103 if (tex
>= MAX_TEXTURES
)
1105 if (prog
->vs
.texture_matrix_location
[tex
] == -1)
1108 get_texture_matrix(context
, state
, tex
, &mat
);
1109 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.texture_matrix_location
[tex
], 1, FALSE
, &mat
._11
));
1110 checkGLcall("glUniformMatrix4fv");
1113 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context
*context
,
1114 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1116 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1118 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1120 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, &state
->material
.specular
.r
));
1121 GL_EXTCALL(glUniform1f(prog
->vs
.material_shininess_location
, state
->material
.power
));
1125 static const float black
[] = {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1127 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, black
));
1129 GL_EXTCALL(glUniform4fv(prog
->vs
.material_ambient_location
, 1, &state
->material
.ambient
.r
));
1130 GL_EXTCALL(glUniform4fv(prog
->vs
.material_diffuse_location
, 1, &state
->material
.diffuse
.r
));
1131 GL_EXTCALL(glUniform4fv(prog
->vs
.material_emissive_location
, 1, &state
->material
.emissive
.r
));
1132 checkGLcall("setting FFP material uniforms");
1135 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context
*context
,
1136 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1138 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1141 D3DCOLORTOGLFLOAT4(state
->render_states
[WINED3D_RS_AMBIENT
], col
);
1142 GL_EXTCALL(glUniform3fv(prog
->vs
.light_ambient_location
, 1, col
));
1143 checkGLcall("glUniform3fv");
1146 static void multiply_vector_matrix(struct wined3d_vec4
*dest
, const struct wined3d_vec4
*src1
,
1147 const struct wined3d_matrix
*src2
)
1149 struct wined3d_vec4 temp
;
1151 temp
.x
= (src1
->x
* src2
->_11
) + (src1
->y
* src2
->_21
) + (src1
->z
* src2
->_31
) + (src1
->w
* src2
->_41
);
1152 temp
.y
= (src1
->x
* src2
->_12
) + (src1
->y
* src2
->_22
) + (src1
->z
* src2
->_32
) + (src1
->w
* src2
->_42
);
1153 temp
.z
= (src1
->x
* src2
->_13
) + (src1
->y
* src2
->_23
) + (src1
->z
* src2
->_33
) + (src1
->w
* src2
->_43
);
1154 temp
.w
= (src1
->x
* src2
->_14
) + (src1
->y
* src2
->_24
) + (src1
->z
* src2
->_34
) + (src1
->w
* src2
->_44
);
1159 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context
*context
,
1160 const struct wined3d_state
*state
, unsigned int light
, struct glsl_shader_prog_link
*prog
)
1162 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1163 const struct wined3d_light_info
*light_info
= state
->lights
[light
];
1164 struct wined3d_vec4 vec4
;
1165 const struct wined3d_matrix
*view
= &state
->transforms
[WINED3D_TS_VIEW
];
1170 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].diffuse
, 1, &light_info
->OriginalParms
.diffuse
.r
));
1171 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].specular
, 1, &light_info
->OriginalParms
.specular
.r
));
1172 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].ambient
, 1, &light_info
->OriginalParms
.ambient
.r
));
1174 switch (light_info
->OriginalParms
.type
)
1176 case WINED3D_LIGHT_POINT
:
1177 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1178 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1179 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1180 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1181 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1182 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1185 case WINED3D_LIGHT_SPOT
:
1186 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1187 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1189 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1190 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1192 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1193 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].falloff
, light_info
->OriginalParms
.falloff
));
1194 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1195 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1196 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1197 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_htheta
, cosf(light_info
->OriginalParms
.theta
/ 2.0f
)));
1198 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_hphi
, cosf(light_info
->OriginalParms
.phi
/ 2.0f
)));
1201 case WINED3D_LIGHT_DIRECTIONAL
:
1202 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1203 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1206 case WINED3D_LIGHT_PARALLELPOINT
:
1207 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1208 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1212 FIXME("Unrecognized light type %#x.\n", light_info
->OriginalParms
.type
);
1214 checkGLcall("setting FFP lights uniforms");
1217 static void shader_glsl_pointsize_uniform(const struct wined3d_context
*context
,
1218 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1220 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1224 get_pointsize_minmax(context
, state
, &min
, &max
);
1226 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_min_location
, min
));
1227 checkGLcall("glUniform1f");
1228 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_max_location
, max
));
1229 checkGLcall("glUniform1f");
1231 get_pointsize(context
, state
, &size
, att
);
1233 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_location
, size
));
1234 checkGLcall("glUniform1f");
1235 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_c_att_location
, att
[0]));
1236 checkGLcall("glUniform1f");
1237 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_l_att_location
, att
[1]));
1238 checkGLcall("glUniform1f");
1239 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_q_att_location
, att
[2]));
1240 checkGLcall("glUniform1f");
1243 static void shader_glsl_load_fog_uniform(const struct wined3d_context
*context
,
1244 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1246 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1247 float start
, end
, scale
;
1255 D3DCOLORTOGLFLOAT4(state
->render_states
[WINED3D_RS_FOGCOLOR
], col
);
1256 GL_EXTCALL(glUniform4fv(prog
->ps
.fog_color_location
, 1, col
));
1257 tmpvalue
.d
= state
->render_states
[WINED3D_RS_FOGDENSITY
];
1258 GL_EXTCALL(glUniform1f(prog
->ps
.fog_density_location
, tmpvalue
.f
));
1259 get_fog_start_end(context
, state
, &start
, &end
);
1260 scale
= 1.0f
/ (end
- start
);
1261 GL_EXTCALL(glUniform1f(prog
->ps
.fog_end_location
, end
));
1262 GL_EXTCALL(glUniform1f(prog
->ps
.fog_scale_location
, scale
));
1263 checkGLcall("fog emulation uniforms");
1266 /* Context activation is done by the caller (state handler). */
1267 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program
*ps
,
1268 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
1270 struct wined3d_color float_key
[2];
1271 const struct wined3d_texture
*texture
= state
->textures
[0];
1273 wined3d_format_get_float_color_key(texture
->resource
.format
, &texture
->async
.src_blt_color_key
, float_key
);
1274 GL_EXTCALL(glUniform4fv(ps
->color_key_location
, 2, &float_key
[0].r
));
1277 /* Context activation is done by the caller (state handler). */
1278 static void shader_glsl_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1279 const struct wined3d_state
*state
)
1281 const struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
1282 const struct wined3d_shader
*vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
1283 const struct wined3d_shader
*pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
1284 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1285 struct shader_glsl_priv
*priv
= shader_priv
;
1286 float position_fixup
[4];
1287 DWORD update_mask
= 0;
1289 struct glsl_shader_prog_link
*prog
= ctx_data
->glsl_program
;
1290 UINT constant_version
;
1294 /* No GLSL program set - nothing to do. */
1297 constant_version
= prog
->constant_version
;
1298 update_mask
= context
->constant_update_mask
& prog
->constant_update_mask
;
1300 if (update_mask
& WINED3D_SHADER_CONST_VS_F
)
1301 shader_glsl_load_constantsF(vshader
, gl_info
, state
->vs_consts_f
,
1302 prog
->vs
.uniform_f_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
1304 if (update_mask
& WINED3D_SHADER_CONST_VS_I
)
1305 shader_glsl_load_constantsI(vshader
, gl_info
, prog
->vs
.uniform_i_locations
, state
->vs_consts_i
,
1306 vshader
->reg_maps
.integer_constants
);
1308 if (update_mask
& WINED3D_SHADER_CONST_VS_B
)
1309 shader_glsl_load_constantsB(vshader
, gl_info
, prog
->vs
.uniform_b_locations
, state
->vs_consts_b
,
1310 vshader
->reg_maps
.boolean_constants
);
1312 if (update_mask
& WINED3D_SHADER_CONST_VS_POINTSIZE
)
1313 shader_glsl_pointsize_uniform(context
, state
, prog
);
1315 if (update_mask
& WINED3D_SHADER_CONST_VS_POS_FIXUP
)
1317 shader_get_position_fixup(context
, state
, position_fixup
);
1318 GL_EXTCALL(glUniform4fv(prog
->vs
.pos_fixup_location
, 1, position_fixup
));
1319 checkGLcall("glUniform4fv");
1322 if (update_mask
& WINED3D_SHADER_CONST_FFP_MODELVIEW
)
1324 struct wined3d_matrix mat
;
1326 get_modelview_matrix(context
, state
, 0, &mat
);
1327 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[0], 1, FALSE
, &mat
._11
));
1328 checkGLcall("glUniformMatrix4fv");
1330 shader_glsl_ffp_vertex_normalmatrix_uniform(context
, state
, prog
);
1333 if (update_mask
& WINED3D_SHADER_CONST_FFP_VERTEXBLEND
)
1335 struct wined3d_matrix mat
;
1337 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
1339 if (prog
->vs
.modelview_matrix_location
[i
] == -1)
1342 get_modelview_matrix(context
, state
, i
, &mat
);
1343 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[i
], 1, FALSE
, &mat
._11
));
1344 checkGLcall("glUniformMatrix4fv");
1348 if (update_mask
& WINED3D_SHADER_CONST_FFP_PROJ
)
1350 struct wined3d_matrix projection
;
1352 get_projection_matrix(context
, state
, &projection
);
1353 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.projection_matrix_location
, 1, FALSE
, &projection
._11
));
1354 checkGLcall("glUniformMatrix4fv");
1357 if (update_mask
& WINED3D_SHADER_CONST_FFP_TEXMATRIX
)
1359 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1360 shader_glsl_ffp_vertex_texmatrix_uniform(context
, state
, i
, prog
);
1363 if (update_mask
& WINED3D_SHADER_CONST_FFP_MATERIAL
)
1364 shader_glsl_ffp_vertex_material_uniform(context
, state
, prog
);
1366 if (update_mask
& WINED3D_SHADER_CONST_FFP_LIGHTS
)
1368 shader_glsl_ffp_vertex_lightambient_uniform(context
, state
, prog
);
1369 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
1370 shader_glsl_ffp_vertex_light_uniform(context
, state
, i
, prog
);
1373 if (update_mask
& WINED3D_SHADER_CONST_PS_F
)
1374 shader_glsl_load_constantsF(pshader
, gl_info
, state
->ps_consts_f
,
1375 prog
->ps
.uniform_f_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
1377 if (update_mask
& WINED3D_SHADER_CONST_PS_I
)
1378 shader_glsl_load_constantsI(pshader
, gl_info
, prog
->ps
.uniform_i_locations
, state
->ps_consts_i
,
1379 pshader
->reg_maps
.integer_constants
);
1381 if (update_mask
& WINED3D_SHADER_CONST_PS_B
)
1382 shader_glsl_load_constantsB(pshader
, gl_info
, prog
->ps
.uniform_b_locations
, state
->ps_consts_b
,
1383 pshader
->reg_maps
.boolean_constants
);
1385 if (update_mask
& WINED3D_SHADER_CONST_PS_BUMP_ENV
)
1387 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1389 if (prog
->ps
.bumpenv_mat_location
[i
] == -1)
1392 GL_EXTCALL(glUniformMatrix2fv(prog
->ps
.bumpenv_mat_location
[i
], 1, 0,
1393 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
]));
1395 if (prog
->ps
.bumpenv_lum_scale_location
[i
] != -1)
1397 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_scale_location
[i
], 1,
1398 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
]));
1399 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_offset_location
[i
], 1,
1400 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
]));
1404 checkGLcall("bump env uniforms");
1407 if (update_mask
& WINED3D_SHADER_CONST_PS_Y_CORR
)
1409 float correction_params
[] =
1411 /* position is window relative, not viewport relative */
1412 context
->render_offscreen
? 0.0f
: (float)context
->current_rt
->resource
.height
,
1413 context
->render_offscreen
? 1.0f
: -1.0f
,
1418 GL_EXTCALL(glUniform4fv(prog
->ps
.ycorrection_location
, 1, correction_params
));
1421 if (update_mask
& WINED3D_SHADER_CONST_PS_NP2_FIXUP
)
1422 shader_glsl_load_np2fixup_constants(&prog
->ps
, gl_info
, state
);
1423 if (update_mask
& WINED3D_SHADER_CONST_FFP_COLOR_KEY
)
1424 shader_glsl_load_color_key_constant(&prog
->ps
, gl_info
, state
);
1426 if (update_mask
& WINED3D_SHADER_CONST_FFP_PS
)
1430 if (prog
->ps
.tex_factor_location
!= -1)
1432 D3DCOLORTOGLFLOAT4(state
->render_states
[WINED3D_RS_TEXTUREFACTOR
], col
);
1433 GL_EXTCALL(glUniform4fv(prog
->ps
.tex_factor_location
, 1, col
));
1436 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1437 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 1.0f
, 1.0f
, 1.0f
, 0.0f
));
1439 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
1441 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1443 if (prog
->ps
.tss_constant_location
[i
] == -1)
1446 D3DCOLORTOGLFLOAT4(state
->texture_states
[i
][WINED3D_TSS_CONSTANT
], col
);
1447 GL_EXTCALL(glUniform4fv(prog
->ps
.tss_constant_location
[i
], 1, col
));
1450 checkGLcall("fixed function uniforms");
1453 if (update_mask
& WINED3D_SHADER_CONST_PS_FOG
)
1454 shader_glsl_load_fog_uniform(context
, state
, prog
);
1456 if (priv
->next_constant_version
== UINT_MAX
)
1458 TRACE("Max constant version reached, resetting to 0.\n");
1459 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
1460 priv
->next_constant_version
= 1;
1464 prog
->constant_version
= priv
->next_constant_version
++;
1468 static void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
, DWORD new_version
)
1470 struct constant_entry
*entries
= heap
->entries
;
1471 unsigned int *positions
= heap
->positions
;
1472 unsigned int heap_idx
, parent_idx
;
1474 if (!heap
->contained
[idx
])
1476 heap_idx
= heap
->size
++;
1477 heap
->contained
[idx
] = TRUE
;
1481 heap_idx
= positions
[idx
];
1484 while (heap_idx
> 1)
1486 parent_idx
= heap_idx
>> 1;
1488 if (new_version
<= entries
[parent_idx
].version
) break;
1490 entries
[heap_idx
] = entries
[parent_idx
];
1491 positions
[entries
[parent_idx
].idx
] = heap_idx
;
1492 heap_idx
= parent_idx
;
1495 entries
[heap_idx
].version
= new_version
;
1496 entries
[heap_idx
].idx
= idx
;
1497 positions
[idx
] = heap_idx
;
1500 static void shader_glsl_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1502 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1503 struct constant_heap
*heap
= &priv
->vconst_heap
;
1506 for (i
= start
; i
< count
+ start
; ++i
)
1508 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1511 for (i
= 0; i
< device
->context_count
; ++i
)
1513 device
->contexts
[i
]->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
1517 static void shader_glsl_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1519 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1520 struct constant_heap
*heap
= &priv
->pconst_heap
;
1523 for (i
= start
; i
< count
+ start
; ++i
)
1525 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1528 for (i
= 0; i
< device
->context_count
; ++i
)
1530 device
->contexts
[i
]->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
1534 static unsigned int vec4_varyings(DWORD shader_major
, const struct wined3d_gl_info
*gl_info
)
1536 unsigned int ret
= gl_info
->limits
.glsl_varyings
/ 4;
1537 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1538 if(shader_major
> 3) return ret
;
1540 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1541 if (gl_info
->quirks
& WINED3D_QUIRK_GLSL_CLIP_VARYING
) ret
-= 1;
1545 static BOOL
needs_legacy_glsl_syntax(const struct wined3d_gl_info
*gl_info
)
1547 return gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
1550 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info
*gl_info
,
1551 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1556 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1557 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "in");
1560 va_start(args
, format
);
1561 ret
= shader_vaddline(buffer
, format
, args
);
1565 if (!string_buffer_resize(buffer
, ret
))
1570 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info
*gl_info
,
1571 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1576 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1577 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "out");
1580 va_start(args
, format
);
1581 ret
= shader_vaddline(buffer
, format
, args
);
1585 if (!string_buffer_resize(buffer
, ret
))
1590 static BOOL
glsl_is_color_reg_read(const struct wined3d_shader
*shader
, unsigned int idx
)
1592 const struct wined3d_shader_signature
*input_signature
= &shader
->input_signature
;
1593 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1594 const BOOL
*input_reg_used
= shader
->u
.ps
.input_reg_used
;
1597 if (reg_maps
->shader_version
.major
< 3)
1598 return input_reg_used
[idx
];
1600 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1602 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
1604 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
1607 if (shader_match_semantic(input
->semantic_name
, WINED3D_DECL_USAGE_COLOR
)
1608 && input
->semantic_idx
== idx
)
1610 if (input_reg_used
[input
->register_idx
])
1619 /** Generate the variable & register declarations for the GLSL output target */
1620 static void shader_generate_glsl_declarations(const struct wined3d_context
*context
,
1621 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
1622 const struct wined3d_shader_reg_maps
*reg_maps
, const struct shader_glsl_ctx_priv
*ctx_priv
)
1624 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
1625 const struct wined3d_state
*state
= &shader
->device
->state
;
1626 const struct vs_compile_args
*vs_args
= ctx_priv
->cur_vs_args
;
1627 const struct ps_compile_args
*ps_args
= ctx_priv
->cur_ps_args
;
1628 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1629 const struct wined3d_fb_state
*fb
= &shader
->device
->fb
;
1630 unsigned int i
, extra_constants_needed
= 0;
1631 const struct wined3d_shader_lconst
*lconst
;
1635 prefix
= shader_glsl_get_prefix(version
->type
);
1637 /* Prototype the subroutines */
1638 for (i
= 0, map
= reg_maps
->labels
; map
; map
>>= 1, ++i
)
1640 if (map
& 1) shader_addline(buffer
, "void subroutine%u();\n", i
);
1643 /* Declare the constants (aka uniforms) */
1644 if (shader
->limits
->constant_float
> 0)
1646 unsigned max_constantsF
;
1648 /* Unless the shader uses indirect addressing, always declare the
1649 * maximum array size and ignore that we need some uniforms privately.
1650 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1651 * and immediate values, still declare VC[256]. If the shader needs
1652 * more uniforms than we have it won't work in any case. If it uses
1653 * less, the compiler will figure out which uniforms are really used
1654 * and strip them out. This allows a shader to use c255 on a dx9 card,
1655 * as long as it doesn't also use all the other constants.
1657 * If the shader uses indirect addressing the compiler must assume
1658 * that all declared uniforms are used. In this case, declare only the
1659 * amount that we're assured to have.
1661 * Thus we run into problems in these two cases:
1662 * 1) The shader really uses more uniforms than supported.
1663 * 2) The shader uses indirect addressing, less constants than
1664 * supported, but uses a constant index > #supported consts. */
1665 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1667 /* No indirect addressing here. */
1668 max_constantsF
= gl_info
->limits
.glsl_ps_float_constants
;
1672 if (reg_maps
->usesrelconstF
)
1674 /* Subtract the other potential uniforms from the max
1675 * available (bools, ints, and 1 row of projection matrix).
1676 * Subtract another uniform for immediate values, which have
1677 * to be loaded via uniform by the driver as well. The shader
1678 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1679 * shader code, so one vec4 should be enough. (Unfortunately
1680 * the Nvidia driver doesn't store 128 and -128 in one float).
1682 * Writing gl_ClipVertex requires one uniform for each
1683 * clipplane as well. */
1684 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
- 3;
1685 if(ctx_priv
->cur_vs_args
->clip_enabled
)
1687 max_constantsF
-= gl_info
->limits
.clipplanes
;
1689 max_constantsF
-= count_bits(reg_maps
->integer_constants
);
1690 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1691 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1692 * for now take this into account when calculating the number of available constants
1694 max_constantsF
-= count_bits(reg_maps
->boolean_constants
);
1695 /* Set by driver quirks in directx.c */
1696 max_constantsF
-= gl_info
->reserved_glsl_constants
;
1698 if (max_constantsF
< shader
->limits
->constant_float
)
1700 static unsigned int once
;
1703 ERR_(winediag
)("The hardware does not support enough uniform components to run this shader,"
1704 " it may not render correctly.\n");
1706 WARN("The hardware does not support enough uniform components to run this shader.\n");
1711 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
;
1714 max_constantsF
= min(shader
->limits
->constant_float
, max_constantsF
);
1715 shader_addline(buffer
, "uniform vec4 %s_c[%u];\n", prefix
, max_constantsF
);
1718 /* Always declare the full set of constants, the compiler can remove the
1719 * unused ones because d3d doesn't (yet) support indirect int and bool
1720 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1721 if (shader
->limits
->constant_int
> 0 && reg_maps
->integer_constants
)
1722 shader_addline(buffer
, "uniform ivec4 %s_i[%u];\n", prefix
, shader
->limits
->constant_int
);
1724 if (shader
->limits
->constant_bool
> 0 && reg_maps
->boolean_constants
)
1725 shader_addline(buffer
, "uniform bool %s_b[%u];\n", prefix
, shader
->limits
->constant_bool
);
1727 for (i
= 0; i
< WINED3D_MAX_CBS
; ++i
)
1729 if (reg_maps
->cb_sizes
[i
])
1730 shader_addline(buffer
, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1731 prefix
, i
, prefix
, i
, reg_maps
->cb_sizes
[i
]);
1734 /* Declare texture samplers */
1735 for (i
= 0; i
< reg_maps
->sampler_map
.count
; ++i
)
1737 struct wined3d_shader_sampler_map_entry
*entry
;
1738 BOOL shadow_sampler
, tex_rect
;
1739 const char *sampler_type
;
1741 entry
= ®_maps
->sampler_map
.entries
[i
];
1743 if (entry
->resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
1745 ERR("Invalid resource index %u.\n", entry
->resource_idx
);
1749 shadow_sampler
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& (ps_args
->shadow
& (1u << entry
->sampler_idx
));
1750 switch (reg_maps
->resource_info
[entry
->resource_idx
].type
)
1752 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
1754 sampler_type
= "sampler1DShadow";
1756 sampler_type
= "sampler1D";
1759 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
1760 tex_rect
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
1761 && (ps_args
->np2_fixup
& (1u << entry
->resource_idx
))
1762 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
1766 sampler_type
= "sampler2DRectShadow";
1768 sampler_type
= "sampler2DShadow";
1773 sampler_type
= "sampler2DRect";
1775 sampler_type
= "sampler2D";
1779 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
1781 FIXME("Unsupported 3D shadow sampler.\n");
1782 sampler_type
= "sampler3D";
1785 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
1787 FIXME("Unsupported Cube shadow sampler.\n");
1788 sampler_type
= "samplerCube";
1792 sampler_type
= "unsupported_sampler";
1793 FIXME("Unhandled resource type %#x.\n", reg_maps
->resource_info
[i
].type
);
1796 shader_addline(buffer
, "uniform %s %s_sampler%u;\n", sampler_type
, prefix
, entry
->bind_idx
);
1799 /* Declare uniforms for NP2 texcoord fixup:
1800 * This is NOT done inside the loop that declares the texture samplers
1801 * since the NP2 fixup code is currently only used for the GeforceFX
1802 * series and when forcing the ARB_npot extension off. Modern cards just
1803 * skip the code anyway, so put it inside a separate loop. */
1804 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& ps_args
->np2_fixup
)
1806 struct ps_np2fixup_info
*fixup
= ctx_priv
->cur_np2fixup_info
;
1809 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1810 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1811 * samplerNP2Fixup stores texture dimensions and is updated through
1812 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1814 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
1816 if (!reg_maps
->resource_info
[i
].type
|| !(ps_args
->np2_fixup
& (1u << i
)))
1819 if (reg_maps
->resource_info
[i
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_2D
)
1821 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1825 fixup
->idx
[i
] = cur
++;
1828 fixup
->num_consts
= (cur
+ 1) >> 1;
1829 fixup
->active
= ps_args
->np2_fixup
;
1830 shader_addline(buffer
, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix
, fixup
->num_consts
);
1833 /* Declare address variables */
1834 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
1836 if (map
& 1) shader_addline(buffer
, "ivec4 A%u;\n", i
);
1839 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
1841 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
1843 const struct wined3d_shader_signature_element
*e
= &shader
->input_signature
.elements
[i
];
1844 if (e
->sysval_semantic
== WINED3D_SV_INSTANCEID
)
1845 shader_addline(buffer
, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1846 prefix
, e
->register_idx
);
1848 shader_addline(buffer
, "attribute vec4 %s_in%u;\n", prefix
, e
->register_idx
);
1851 if (vs_args
->point_size
&& !vs_args
->per_vertex_point_size
)
1853 shader_addline(buffer
, "uniform struct\n{\n");
1854 shader_addline(buffer
, " float size;\n");
1855 shader_addline(buffer
, " float size_min;\n");
1856 shader_addline(buffer
, " float size_max;\n");
1857 shader_addline(buffer
, "} ffp_point;\n");
1860 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && version
->major
< 3)
1862 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_diffuse;\n");
1863 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_specular;\n");
1864 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
1865 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
1868 shader_addline(buffer
, "uniform vec4 posFixup;\n");
1869 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", shader
->limits
->packed_output
);
1871 else if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
1873 shader_addline(buffer
, "varying in vec4 gs_in[][%u];\n", shader
->limits
->packed_input
);
1875 else if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1877 if (version
->major
< 3 || ps_args
->vp_mode
!= vertexshader
)
1879 shader_addline(buffer
, "uniform struct\n{\n");
1880 shader_addline(buffer
, " vec4 color;\n");
1881 shader_addline(buffer
, " float density;\n");
1882 shader_addline(buffer
, " float end;\n");
1883 shader_addline(buffer
, " float scale;\n");
1884 shader_addline(buffer
, "} ffp_fog;\n");
1886 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
1888 if (glsl_is_color_reg_read(shader
, 0))
1889 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
1890 if (glsl_is_color_reg_read(shader
, 1))
1891 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
1892 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
1893 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
1897 if (glsl_is_color_reg_read(shader
, 0))
1898 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_diffuse;\n");
1899 if (glsl_is_color_reg_read(shader
, 1))
1900 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_specular;\n");
1901 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
1902 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
1903 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
1907 if (version
->major
>= 3)
1909 UINT in_count
= min(vec4_varyings(version
->major
, gl_info
), shader
->limits
->packed_input
);
1912 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 %s_link[%u];\n", prefix
, in_count
);
1913 shader_addline(buffer
, "vec4 %s_in[%u];\n", prefix
, in_count
);
1916 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
1921 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", i
);
1923 if (reg_maps
->luminanceparams
& (1u << i
))
1925 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", i
);
1926 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", i
);
1927 extra_constants_needed
++;
1930 extra_constants_needed
++;
1933 if (ps_args
->srgb_correction
)
1935 shader_addline(buffer
, "const vec4 srgb_const0 = ");
1936 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
1937 shader_addline(buffer
, ";\n");
1938 shader_addline(buffer
, "const vec4 srgb_const1 = ");
1939 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
1940 shader_addline(buffer
, ";\n");
1942 if (reg_maps
->vpos
|| reg_maps
->usesdsy
)
1944 if (shader
->limits
->constant_float
+ extra_constants_needed
1945 + 1 < gl_info
->limits
.glsl_ps_float_constants
)
1947 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
1948 extra_constants_needed
++;
1952 float ycorrection
[] =
1954 context
->render_offscreen
? 0.0f
: fb
->render_targets
[0]->height
,
1955 context
->render_offscreen
? 1.0f
: -1.0f
,
1960 /* This happens because we do not have proper tracking of the
1961 * constant registers that are actually used, only the max
1962 * limit of the shader version. */
1963 FIXME("Cannot find a free uniform for vpos correction params\n");
1964 shader_addline(buffer
, "const vec4 ycorrection = ");
1965 shader_glsl_append_imm_vec4(buffer
, ycorrection
);
1966 shader_addline(buffer
, ";\n");
1968 shader_addline(buffer
, "vec4 vpos;\n");
1972 /* Declare output register temporaries */
1973 if (shader
->limits
->packed_output
)
1974 shader_addline(buffer
, "vec4 %s_out[%u];\n", prefix
, shader
->limits
->packed_output
);
1976 /* Declare temporary variables */
1977 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
1979 if (map
& 1) shader_addline(buffer
, "vec4 R%u;\n", i
);
1982 /* Declare loop registers aLx */
1983 if (version
->major
< 4)
1985 for (i
= 0; i
< reg_maps
->loop_depth
; ++i
)
1987 shader_addline(buffer
, "int aL%u;\n", i
);
1988 shader_addline(buffer
, "int tmpInt%u;\n", i
);
1992 /* Temporary variables for matrix operations */
1993 shader_addline(buffer
, "vec4 tmp0;\n");
1994 shader_addline(buffer
, "vec4 tmp1;\n");
1996 if (!shader
->load_local_constsF
)
1998 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
2000 shader_addline(buffer
, "const vec4 %s_lc%u = ", prefix
, lconst
->idx
);
2001 shader_glsl_append_imm_vec4(buffer
, (const float *)lconst
->value
);
2002 shader_addline(buffer
, ";\n");
2006 /* Start the main program. */
2007 shader_addline(buffer
, "void main()\n{\n");
2009 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2010 * add approximately 0.5. This causes off-by-one problems as spotted by
2011 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2012 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2013 * causes precision troubles when we just subtract 0.5.
2015 * To deal with that, just floor() the position. This will eliminate the
2016 * fraction on all cards.
2018 * TODO: Test how this behaves with multisampling.
2020 * An advantage of floor is that it works even if the driver doesn't add
2021 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2022 * to return in gl_FragCoord, even though coordinates specify the pixel
2023 * centers instead of the pixel corners. This code will behave correctly
2024 * on drivers that returns integer values. */
2025 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& reg_maps
->vpos
)
2027 if (shader
->device
->wined3d
->flags
& WINED3D_PIXEL_CENTER_INTEGER
)
2028 shader_addline(buffer
,
2029 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2031 shader_addline(buffer
,
2032 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2036 /*****************************************************************************
2037 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2039 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2040 ****************************************************************************/
2043 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2044 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
);
2046 /** Used for opcode modifiers - They multiply the result by the specified amount */
2047 static const char * const shift_glsl_tab
[] = {
2049 "2.0 * ", /* 1 (x2) */
2050 "4.0 * ", /* 2 (x4) */
2051 "8.0 * ", /* 3 (x8) */
2052 "16.0 * ", /* 4 (x16) */
2053 "32.0 * ", /* 5 (x32) */
2060 "0.0625 * ", /* 12 (d16) */
2061 "0.125 * ", /* 13 (d8) */
2062 "0.25 * ", /* 14 (d4) */
2063 "0.5 * " /* 15 (d2) */
2066 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2067 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier
,
2068 const char *in_reg
, const char *in_regswizzle
, char *out_str
)
2072 switch (src_modifier
)
2074 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
2075 case WINED3DSPSM_DW
:
2076 case WINED3DSPSM_NONE
:
2077 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2079 case WINED3DSPSM_NEG
:
2080 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
2082 case WINED3DSPSM_NOT
:
2083 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
2085 case WINED3DSPSM_BIAS
:
2086 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2088 case WINED3DSPSM_BIASNEG
:
2089 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2091 case WINED3DSPSM_SIGN
:
2092 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2094 case WINED3DSPSM_SIGNNEG
:
2095 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2097 case WINED3DSPSM_COMP
:
2098 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
2100 case WINED3DSPSM_X2
:
2101 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
2103 case WINED3DSPSM_X2NEG
:
2104 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
2106 case WINED3DSPSM_ABS
:
2107 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
2109 case WINED3DSPSM_ABSNEG
:
2110 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
2113 FIXME("Unhandled modifier %u\n", src_modifier
);
2114 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2118 /** Writes the GLSL variable name that corresponds to the register that the
2119 * DX opcode parameter is trying to access */
2120 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
2121 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
2123 /* oPos, oFog and oPts in D3D */
2124 static const char * const hwrastout_reg_names
[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2126 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
2127 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
2128 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
2129 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
2130 const char *prefix
= shader_glsl_get_prefix(version
->type
);
2131 struct glsl_src_param rel_param0
, rel_param1
;
2132 char imm_str
[4][17];
2134 if (reg
->idx
[0].offset
!= ~0U && reg
->idx
[0].rel_addr
)
2135 shader_glsl_add_src_param(ins
, reg
->idx
[0].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param0
);
2136 if (reg
->idx
[1].offset
!= ~0U && reg
->idx
[1].rel_addr
)
2137 shader_glsl_add_src_param(ins
, reg
->idx
[1].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param1
);
2142 case WINED3DSPR_TEMP
:
2143 sprintf(register_name
, "R%u", reg
->idx
[0].offset
);
2146 case WINED3DSPR_INPUT
:
2147 /* vertex shaders */
2148 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
2150 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2151 if (priv
->cur_vs_args
->swizzle_map
& (1u << reg
->idx
[0].offset
))
2153 sprintf(register_name
, "%s_in%u", prefix
, reg
->idx
[0].offset
);
2157 if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
2159 if (reg
->idx
[0].rel_addr
)
2161 if (reg
->idx
[1].rel_addr
)
2162 sprintf(register_name
, "gs_in[%s + %u][%s + %u]",
2163 rel_param0
.param_str
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2165 sprintf(register_name
, "gs_in[%s + %u][%u]",
2166 rel_param0
.param_str
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2168 else if (reg
->idx
[1].rel_addr
)
2169 sprintf(register_name
, "gs_in[%u][%s + %u]",
2170 reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2172 sprintf(register_name
, "gs_in[%u][%u]", reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2176 /* pixel shaders >= 3.0 */
2177 if (version
->major
>= 3)
2179 DWORD idx
= shader
->u
.ps
.input_reg_map
[reg
->idx
[0].offset
];
2180 unsigned int in_count
= vec4_varyings(version
->major
, gl_info
);
2182 if (reg
->idx
[0].rel_addr
)
2184 /* Removing a + 0 would be an obvious optimization, but
2185 * OS X doesn't see the NOP operation there. */
2188 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2189 && shader
->u
.ps
.declared_in_count
> in_count
)
2191 sprintf(register_name
,
2192 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2193 rel_param0
.param_str
, idx
, in_count
- 1, rel_param0
.param_str
, idx
, in_count
,
2194 prefix
, rel_param0
.param_str
, idx
);
2198 sprintf(register_name
, "%s_in[%s + %u]", prefix
, rel_param0
.param_str
, idx
);
2203 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2204 && shader
->u
.ps
.declared_in_count
> in_count
)
2206 sprintf(register_name
, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2207 rel_param0
.param_str
, in_count
- 1, rel_param0
.param_str
, in_count
,
2208 prefix
, rel_param0
.param_str
);
2212 sprintf(register_name
, "%s_in[%s]", prefix
, rel_param0
.param_str
);
2218 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
2219 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
2220 else sprintf(register_name
, "%s_in[%u]", prefix
, idx
);
2225 if (!reg
->idx
[0].offset
)
2226 strcpy(register_name
, "ffp_varying_diffuse");
2228 strcpy(register_name
, "ffp_varying_specular");
2233 case WINED3DSPR_CONST
:
2235 /* Relative addressing */
2236 if (reg
->idx
[0].rel_addr
)
2238 if (reg
->idx
[0].offset
)
2239 sprintf(register_name
, "%s_c[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2241 sprintf(register_name
, "%s_c[%s]", prefix
, rel_param0
.param_str
);
2245 if (shader_constant_is_local(shader
, reg
->idx
[0].offset
))
2246 sprintf(register_name
, "%s_lc%u", prefix
, reg
->idx
[0].offset
);
2248 sprintf(register_name
, "%s_c[%u]", prefix
, reg
->idx
[0].offset
);
2253 case WINED3DSPR_CONSTINT
:
2254 sprintf(register_name
, "%s_i[%u]", prefix
, reg
->idx
[0].offset
);
2257 case WINED3DSPR_CONSTBOOL
:
2258 sprintf(register_name
, "%s_b[%u]", prefix
, reg
->idx
[0].offset
);
2261 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
2262 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2263 sprintf(register_name
, "T%u", reg
->idx
[0].offset
);
2265 sprintf(register_name
, "A%u", reg
->idx
[0].offset
);
2268 case WINED3DSPR_LOOP
:
2269 sprintf(register_name
, "aL%u", ins
->ctx
->loop_state
->current_reg
- 1);
2272 case WINED3DSPR_SAMPLER
:
2273 sprintf(register_name
, "%s_sampler%u", prefix
, reg
->idx
[0].offset
);
2276 case WINED3DSPR_COLOROUT
:
2277 if (reg
->idx
[0].offset
>= gl_info
->limits
.buffers
)
2278 WARN("Write to render target %u, only %d supported.\n",
2279 reg
->idx
[0].offset
, gl_info
->limits
.buffers
);
2281 sprintf(register_name
, "gl_FragData[%u]", reg
->idx
[0].offset
);
2284 case WINED3DSPR_RASTOUT
:
2285 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
[0].offset
]);
2288 case WINED3DSPR_DEPTHOUT
:
2289 sprintf(register_name
, "gl_FragDepth");
2292 case WINED3DSPR_ATTROUT
:
2293 if (!reg
->idx
[0].offset
)
2294 sprintf(register_name
, "%s_out[8]", prefix
);
2296 sprintf(register_name
, "%s_out[9]", prefix
);
2299 case WINED3DSPR_TEXCRDOUT
:
2300 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2301 sprintf(register_name
, "%s_out[%u]", prefix
, reg
->idx
[0].offset
);
2304 case WINED3DSPR_MISCTYPE
:
2305 if (!reg
->idx
[0].offset
)
2308 sprintf(register_name
, "vpos");
2310 else if (reg
->idx
[0].offset
== 1)
2312 /* Note that gl_FrontFacing is a bool, while vFace is
2313 * a float for which the sign determines front/back */
2314 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
2318 FIXME("Unhandled misctype register %u.\n", reg
->idx
[0].offset
);
2319 sprintf(register_name
, "unrecognized_register");
2323 case WINED3DSPR_IMMCONST
:
2324 switch (reg
->immconst_type
)
2326 case WINED3D_IMMCONST_SCALAR
:
2327 switch (reg
->data_type
)
2329 case WINED3D_DATA_FLOAT
:
2330 wined3d_ftoa(*(const float *)reg
->immconst_data
, register_name
);
2332 case WINED3D_DATA_INT
:
2333 sprintf(register_name
, "%#x", reg
->immconst_data
[0]);
2335 case WINED3D_DATA_RESOURCE
:
2336 case WINED3D_DATA_SAMPLER
:
2337 case WINED3D_DATA_UINT
:
2338 sprintf(register_name
, "%#xu", reg
->immconst_data
[0]);
2341 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2346 case WINED3D_IMMCONST_VEC4
:
2347 switch (reg
->data_type
)
2349 case WINED3D_DATA_FLOAT
:
2350 wined3d_ftoa(*(const float *)®
->immconst_data
[0], imm_str
[0]);
2351 wined3d_ftoa(*(const float *)®
->immconst_data
[1], imm_str
[1]);
2352 wined3d_ftoa(*(const float *)®
->immconst_data
[2], imm_str
[2]);
2353 wined3d_ftoa(*(const float *)®
->immconst_data
[3], imm_str
[3]);
2354 sprintf(register_name
, "vec4(%s, %s, %s, %s)",
2355 imm_str
[0], imm_str
[1], imm_str
[2], imm_str
[3]);
2357 case WINED3D_DATA_INT
:
2358 sprintf(register_name
, "ivec4(%#x, %#x, %#x, %#x)",
2359 reg
->immconst_data
[0], reg
->immconst_data
[1],
2360 reg
->immconst_data
[2], reg
->immconst_data
[3]);
2362 case WINED3D_DATA_RESOURCE
:
2363 case WINED3D_DATA_SAMPLER
:
2364 case WINED3D_DATA_UINT
:
2365 sprintf(register_name
, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2366 reg
->immconst_data
[0], reg
->immconst_data
[1],
2367 reg
->immconst_data
[2], reg
->immconst_data
[3]);
2370 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2376 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
2377 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
2381 case WINED3DSPR_CONSTBUFFER
:
2382 if (reg
->idx
[1].rel_addr
)
2383 sprintf(register_name
, "%s_cb%u[%s + %u]",
2384 prefix
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2386 sprintf(register_name
, "%s_cb%u[%u]", prefix
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2389 case WINED3DSPR_PRIMID
:
2390 sprintf(register_name
, "uint(gl_PrimitiveIDIn)");
2394 FIXME("Unhandled register type %#x.\n", reg
->type
);
2395 sprintf(register_name
, "unrecognized_register");
2400 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
2403 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
2404 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
2405 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
2406 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
2410 /* Get the GLSL write mask for the destination register */
2411 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
2413 DWORD mask
= param
->write_mask
;
2415 if (shader_is_scalar(¶m
->reg
))
2417 mask
= WINED3DSP_WRITEMASK_0
;
2422 shader_glsl_write_mask_to_str(mask
, write_mask
);
2428 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
2429 unsigned int size
= 0;
2431 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
2432 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
2433 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
2434 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
2439 static void shader_glsl_swizzle_to_str(const DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
2441 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2442 * but addressed as "rgba". To fix this we need to swap the register's x
2443 * and z components. */
2444 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
2447 /* swizzle bits fields: wwzzyyxx */
2448 if (mask
& WINED3DSP_WRITEMASK_0
) *str
++ = swizzle_chars
[swizzle
& 0x03];
2449 if (mask
& WINED3DSP_WRITEMASK_1
) *str
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
2450 if (mask
& WINED3DSP_WRITEMASK_2
) *str
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
2451 if (mask
& WINED3DSP_WRITEMASK_3
) *str
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
2455 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
2456 BOOL fixup
, DWORD mask
, char *swizzle_str
)
2458 if (shader_is_scalar(¶m
->reg
))
2459 *swizzle_str
= '\0';
2461 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
2464 /* From a given parameter token, generate the corresponding GLSL string.
2465 * Also, return the actual register name and swizzle in case the
2466 * caller needs this information as well. */
2467 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2468 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
)
2470 BOOL is_color
= FALSE
;
2471 char swizzle_str
[6];
2473 glsl_src
->reg_name
[0] = '\0';
2474 glsl_src
->param_str
[0] = '\0';
2475 swizzle_str
[0] = '\0';
2477 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
2478 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
2480 if (wined3d_src
->reg
.type
== WINED3DSPR_IMMCONST
|| wined3d_src
->reg
.type
== WINED3DSPR_PRIMID
)
2482 shader_glsl_gen_modifier(wined3d_src
->modifiers
, glsl_src
->reg_name
, swizzle_str
, glsl_src
->param_str
);
2488 switch (wined3d_src
->reg
.data_type
)
2490 case WINED3D_DATA_FLOAT
:
2491 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2493 case WINED3D_DATA_INT
:
2494 sprintf(reg_name
, "floatBitsToInt(%s)", glsl_src
->reg_name
);
2496 case WINED3D_DATA_RESOURCE
:
2497 case WINED3D_DATA_SAMPLER
:
2498 case WINED3D_DATA_UINT
:
2499 sprintf(reg_name
, "floatBitsToUint(%s)", glsl_src
->reg_name
);
2502 FIXME("Unhandled data type %#x.\n", wined3d_src
->reg
.data_type
);
2503 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2507 shader_glsl_gen_modifier(wined3d_src
->modifiers
, reg_name
, swizzle_str
, glsl_src
->param_str
);
2511 /* From a given parameter token, generate the corresponding GLSL string.
2512 * Also, return the actual register name and swizzle in case the
2513 * caller needs this information as well. */
2514 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
2515 const struct wined3d_shader_dst_param
*wined3d_dst
, struct glsl_dst_param
*glsl_dst
)
2517 BOOL is_color
= FALSE
;
2519 glsl_dst
->mask_str
[0] = '\0';
2520 glsl_dst
->reg_name
[0] = '\0';
2522 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
2523 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
2526 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2527 static DWORD
shader_glsl_append_dst_ext(struct wined3d_string_buffer
*buffer
,
2528 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
,
2529 enum wined3d_data_type data_type
)
2531 struct glsl_dst_param glsl_dst
;
2534 if ((mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
)))
2538 case WINED3D_DATA_FLOAT
:
2539 shader_addline(buffer
, "%s%s = %s(",
2540 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2542 case WINED3D_DATA_INT
:
2543 shader_addline(buffer
, "%s%s = %sintBitsToFloat(",
2544 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2546 case WINED3D_DATA_RESOURCE
:
2547 case WINED3D_DATA_SAMPLER
:
2548 case WINED3D_DATA_UINT
:
2549 shader_addline(buffer
, "%s%s = %suintBitsToFloat(",
2550 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2553 FIXME("Unhandled data type %#x.\n", data_type
);
2554 shader_addline(buffer
, "%s%s = %s(",
2555 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2563 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2564 static DWORD
shader_glsl_append_dst(struct wined3d_string_buffer
*buffer
, const struct wined3d_shader_instruction
*ins
)
2566 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
2569 /** Process GLSL instruction modifiers */
2570 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
2572 struct glsl_dst_param dst_param
;
2575 if (!ins
->dst_count
) return;
2577 modifiers
= ins
->dst
[0].modifiers
;
2578 if (!modifiers
) return;
2580 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
2582 if (modifiers
& WINED3DSPDM_SATURATE
)
2584 /* _SAT means to clamp the value of the register to between 0 and 1 */
2585 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
2586 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
2589 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
2591 FIXME("_centroid modifier not handled\n");
2594 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
2596 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2600 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op
)
2604 case WINED3D_SHADER_REL_OP_GT
: return ">";
2605 case WINED3D_SHADER_REL_OP_EQ
: return "==";
2606 case WINED3D_SHADER_REL_OP_GE
: return ">=";
2607 case WINED3D_SHADER_REL_OP_LT
: return "<";
2608 case WINED3D_SHADER_REL_OP_NE
: return "!=";
2609 case WINED3D_SHADER_REL_OP_LE
: return "<=";
2611 FIXME("Unrecognized operator %#x.\n", op
);
2616 static void shader_glsl_get_sample_function(const struct wined3d_shader_context
*ctx
,
2617 DWORD resource_idx
, DWORD flags
, struct glsl_sample_function
*sample_function
)
2621 unsigned int coord_size
;
2622 const char *type_part
;
2626 {0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2627 {1, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2628 {1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2629 {2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2630 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2631 {3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2632 {3, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2633 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2634 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2635 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2637 struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
2638 enum wined3d_shader_resource_type resource_type
= ctx
->reg_maps
->resource_info
[resource_idx
].type
;
2639 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
2640 BOOL shadow
= ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
2641 && (priv
->cur_ps_args
->shadow
& (1u << resource_idx
));
2642 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
2643 BOOL texrect
= flags
& WINED3D_GLSL_SAMPLE_NPOT
&& gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
2644 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
2645 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
2646 const char *base
= "texture", *type_part
= "", *suffix
= "";
2647 unsigned int coord_size
;
2649 sample_function
->data_type
= ctx
->reg_maps
->resource_info
[resource_idx
].data_type
;
2651 if (resource_type
>= ARRAY_SIZE(resource_types
))
2653 ERR("Unexpected resource type %#x.\n", resource_type
);
2654 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
2657 /* Note that there's no such thing as a projected cube texture. */
2658 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
2661 if (needs_legacy_glsl_syntax(gl_info
))
2666 type_part
= resource_types
[resource_type
].type_part
;
2667 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
&& texrect
)
2668 type_part
= "2DRect";
2670 FIXME("Unhandled resource type %#x.\n", resource_type
);
2672 if (!lod
&& grad
&& !gl_info
->supported
[EXT_GPU_SHADER4
])
2674 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
2677 FIXME("Unsupported grad function.\n");
2681 sample_function
->name
= string_buffer_get(priv
->string_buffers
);
2682 string_buffer_sprintf(sample_function
->name
, "%s%s%s%s%s", base
, type_part
, projected
? "Proj" : "",
2683 lod
? "Lod" : grad
? "Grad" : "", suffix
);
2685 coord_size
= resource_types
[resource_type
].coord_size
;
2688 sample_function
->coord_mask
= (1u << coord_size
) - 1;
2689 sample_function
->output_single_component
= shadow
&& !needs_legacy_glsl_syntax(gl_info
);
2692 static void shader_glsl_release_sample_function(const struct wined3d_shader_context
*ctx
,
2693 struct glsl_sample_function
*sample_function
)
2695 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
2697 string_buffer_release(priv
->string_buffers
, sample_function
->name
);
2700 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
2701 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
2703 switch(channel_source
)
2705 case CHANNEL_SOURCE_ZERO
:
2706 strcat(arguments
, "0.0");
2709 case CHANNEL_SOURCE_ONE
:
2710 strcat(arguments
, "1.0");
2713 case CHANNEL_SOURCE_X
:
2714 strcat(arguments
, reg_name
);
2715 strcat(arguments
, ".x");
2718 case CHANNEL_SOURCE_Y
:
2719 strcat(arguments
, reg_name
);
2720 strcat(arguments
, ".y");
2723 case CHANNEL_SOURCE_Z
:
2724 strcat(arguments
, reg_name
);
2725 strcat(arguments
, ".z");
2728 case CHANNEL_SOURCE_W
:
2729 strcat(arguments
, reg_name
);
2730 strcat(arguments
, ".w");
2734 FIXME("Unhandled channel source %#x\n", channel_source
);
2735 strcat(arguments
, "undefined");
2739 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
2742 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer
*buffer
,
2743 const char *reg_name
, DWORD mask
, struct color_fixup_desc fixup
)
2745 unsigned int mask_size
, remaining
;
2746 DWORD fixup_mask
= 0;
2747 char arguments
[256];
2750 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) fixup_mask
|= WINED3DSP_WRITEMASK_0
;
2751 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) fixup_mask
|= WINED3DSP_WRITEMASK_1
;
2752 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) fixup_mask
|= WINED3DSP_WRITEMASK_2
;
2753 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) fixup_mask
|= WINED3DSP_WRITEMASK_3
;
2754 if (!(mask
&= fixup_mask
))
2757 if (is_complex_fixup(fixup
))
2759 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
2760 FIXME("Complex fixup (%#x) not supported\n",complex_fixup
);
2764 shader_glsl_write_mask_to_str(mask
, mask_str
);
2765 mask_size
= shader_glsl_get_write_mask_size(mask
);
2767 arguments
[0] = '\0';
2768 remaining
= mask_size
;
2769 if (mask
& WINED3DSP_WRITEMASK_0
)
2771 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
2772 if (--remaining
) strcat(arguments
, ", ");
2774 if (mask
& WINED3DSP_WRITEMASK_1
)
2776 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
2777 if (--remaining
) strcat(arguments
, ", ");
2779 if (mask
& WINED3DSP_WRITEMASK_2
)
2781 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
2782 if (--remaining
) strcat(arguments
, ", ");
2784 if (mask
& WINED3DSP_WRITEMASK_3
)
2786 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
2787 if (--remaining
) strcat(arguments
, ", ");
2791 shader_addline(buffer
, "%s%s = vec%u(%s);\n", reg_name
, mask_str
, mask_size
, arguments
);
2793 shader_addline(buffer
, "%s%s = %s;\n", reg_name
, mask_str
, arguments
);
2796 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
2801 shader_glsl_get_register_name(&ins
->dst
[0].reg
, reg_name
, &is_color
, ins
);
2802 shader_glsl_color_correction_ext(ins
->ctx
->buffer
, reg_name
, ins
->dst
[0].write_mask
, fixup
);
2805 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
2806 DWORD sampler
, const struct glsl_sample_function
*sample_function
, DWORD swizzle
,
2807 const char *dx
, const char *dy
, const char *bias
, const char *coord_reg_fmt
, ...)
2809 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
2810 char dst_swizzle
[6];
2811 struct color_fixup_desc fixup
;
2812 BOOL np2_fixup
= FALSE
;
2816 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
2818 /* FIXME: We currently don't support fixups for vertex shaders or anything
2819 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2820 * the resource index. */
2821 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& version
->major
< 4)
2823 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2824 fixup
= priv
->cur_ps_args
->color_fixup
[sampler
];
2826 if (priv
->cur_ps_args
->np2_fixup
& (1u << sampler
))
2831 fixup
= COLOR_FIXUP_IDENTITY
;
2834 shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], sample_function
->data_type
);
2836 if (sample_function
->output_single_component
)
2837 shader_addline(ins
->ctx
->buffer
, "vec4(");
2839 shader_addline(ins
->ctx
->buffer
, "%s(%s_sampler%u, ",
2840 sample_function
->name
->buffer
, shader_glsl_get_prefix(version
->type
), sampler
);
2844 va_start(args
, coord_reg_fmt
);
2845 ret
= shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
2849 if (!string_buffer_resize(ins
->ctx
->buffer
, ret
))
2855 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2856 const unsigned char idx
= priv
->cur_np2fixup_info
->idx
[sampler
];
2858 switch (shader_glsl_get_write_mask_size(sample_function
->coord_mask
))
2861 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
2862 idx
>> 1, (idx
% 2) ? "z" : "x");
2865 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
2866 idx
>> 1, (idx
% 2) ? "zw" : "xy");
2869 shader_addline(ins
->ctx
->buffer
, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2870 idx
>> 1, (idx
% 2) ? "zw" : "xy");
2873 shader_addline(ins
->ctx
->buffer
, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2874 idx
>> 1, (idx
% 2) ? "zw" : "xy");
2879 shader_addline(ins
->ctx
->buffer
, ", %s, %s)", dx
, dy
);
2881 shader_addline(ins
->ctx
->buffer
, ", %s)", bias
);
2883 shader_addline(ins
->ctx
->buffer
, ")");
2885 if (sample_function
->output_single_component
)
2886 shader_addline(ins
->ctx
->buffer
, ")");
2888 shader_addline(ins
->ctx
->buffer
, "%s);\n", dst_swizzle
);
2890 if (!is_identity_fixup(fixup
))
2891 shader_glsl_color_correction(ins
, fixup
);
2894 /*****************************************************************************
2895 * Begin processing individual instruction opcodes
2896 ****************************************************************************/
2898 static void shader_glsl_binop(const struct wined3d_shader_instruction
*ins
)
2900 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2901 struct glsl_src_param src0_param
;
2902 struct glsl_src_param src1_param
;
2906 /* Determine the GLSL operator to use based on the opcode */
2907 switch (ins
->handler_idx
)
2909 case WINED3DSIH_ADD
: op
= "+"; break;
2910 case WINED3DSIH_AND
: op
= "&"; break;
2911 case WINED3DSIH_DIV
: op
= "/"; break;
2912 case WINED3DSIH_IADD
: op
= "+"; break;
2913 case WINED3DSIH_ISHL
: op
= "<<"; break;
2914 case WINED3DSIH_MUL
: op
= "*"; break;
2915 case WINED3DSIH_OR
: op
= "|"; break;
2916 case WINED3DSIH_SUB
: op
= "-"; break;
2917 case WINED3DSIH_USHR
: op
= ">>"; break;
2918 case WINED3DSIH_XOR
: op
= "^"; break;
2920 op
= "<unhandled operator>";
2921 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
2925 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2926 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2927 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2928 shader_addline(buffer
, "%s %s %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
2931 static void shader_glsl_relop(const struct wined3d_shader_instruction
*ins
)
2933 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2934 struct glsl_src_param src0_param
;
2935 struct glsl_src_param src1_param
;
2936 unsigned int mask_size
;
2940 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2941 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2942 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2943 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2947 switch (ins
->handler_idx
)
2949 case WINED3DSIH_EQ
: op
= "equal"; break;
2950 case WINED3DSIH_GE
: op
= "greaterThanEqual"; break;
2951 case WINED3DSIH_IGE
: op
= "greaterThanEqual"; break;
2952 case WINED3DSIH_UGE
: op
= "greaterThanEqual"; break;
2953 case WINED3DSIH_LT
: op
= "lessThan"; break;
2954 case WINED3DSIH_NE
: op
= "notEqual"; break;
2956 op
= "<unhandled operator>";
2957 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
2961 shader_addline(buffer
, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2962 mask_size
, op
, src0_param
.param_str
, src1_param
.param_str
);
2966 switch (ins
->handler_idx
)
2968 case WINED3DSIH_EQ
: op
= "=="; break;
2969 case WINED3DSIH_GE
: op
= ">="; break;
2970 case WINED3DSIH_IGE
: op
= ">="; break;
2971 case WINED3DSIH_UGE
: op
= ">="; break;
2972 case WINED3DSIH_LT
: op
= "<"; break;
2973 case WINED3DSIH_NE
: op
= "!="; break;
2975 op
= "<unhandled operator>";
2976 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
2980 shader_addline(buffer
, "%s %s %s ? 0xffffffffu : 0u);\n",
2981 src0_param
.param_str
, op
, src1_param
.param_str
);
2985 static void shader_glsl_imul(const struct wined3d_shader_instruction
*ins
)
2987 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
2988 struct glsl_src_param src0_param
;
2989 struct glsl_src_param src1_param
;
2992 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2993 * not, we can emulate it. */
2994 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
2995 FIXME("64-bit integer multiplies not implemented.\n");
2997 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
2999 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3000 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3001 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3003 shader_addline(ins
->ctx
->buffer
, "%s * %s);\n",
3004 src0_param
.param_str
, src1_param
.param_str
);
3008 static void shader_glsl_udiv(const struct wined3d_shader_instruction
*ins
)
3010 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3011 struct glsl_src_param src0_param
, src1_param
;
3014 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3017 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3021 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3022 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3023 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3024 shader_addline(buffer
, "tmp0%s = %s / %s;\n",
3025 dst_mask
, src0_param
.param_str
, src1_param
.param_str
);
3027 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3028 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3029 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3030 shader_addline(buffer
, "%s %% %s));\n", src0_param
.param_str
, src1_param
.param_str
);
3032 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3033 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
3037 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3038 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3039 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3040 shader_addline(buffer
, "%s / %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3043 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3045 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3046 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3047 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3048 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3052 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3053 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
3055 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3056 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3057 struct glsl_src_param src0_param
;
3060 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3061 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3063 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3064 * shader versions WINED3DSIO_MOVA is used for this. */
3065 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
3066 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
3067 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
3069 /* This is a simple floor() */
3070 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3071 if (mask_size
> 1) {
3072 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
3074 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
3077 else if(ins
->handler_idx
== WINED3DSIH_MOVA
)
3079 /* We need to *round* to the nearest int here. */
3080 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3082 if (gl_info
->supported
[EXT_GPU_SHADER4
])
3085 shader_addline(buffer
, "ivec%d(round(%s)));\n", mask_size
, src0_param
.param_str
);
3087 shader_addline(buffer
, "int(round(%s)));\n", src0_param
.param_str
);
3092 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3093 mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
3095 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3096 src0_param
.param_str
, src0_param
.param_str
);
3101 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
3105 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3106 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
3108 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3109 struct glsl_src_param src0_param
;
3110 struct glsl_src_param src1_param
;
3111 DWORD dst_write_mask
, src_write_mask
;
3112 unsigned int dst_size
= 0;
3114 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3115 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3117 /* dp4 works on vec4, dp3 on vec3, etc. */
3118 if (ins
->handler_idx
== WINED3DSIH_DP4
)
3119 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
3120 else if (ins
->handler_idx
== WINED3DSIH_DP3
)
3121 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3123 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
3125 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
3126 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
3129 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
3131 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
3135 /* Note that this instruction has some restrictions. The destination write mask
3136 * can't contain the w component, and the source swizzles have to be .xyzw */
3137 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
3139 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3140 struct glsl_src_param src0_param
;
3141 struct glsl_src_param src1_param
;
3144 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3145 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3146 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3147 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
3148 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
3151 static void shader_glsl_cut(const struct wined3d_shader_instruction
*ins
)
3153 shader_addline(ins
->ctx
->buffer
, "EndPrimitive();\n");
3156 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3157 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3158 * GLSL uses the value as-is. */
3159 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
3161 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3162 struct glsl_src_param src0_param
;
3163 struct glsl_src_param src1_param
;
3164 DWORD dst_write_mask
;
3165 unsigned int dst_size
;
3167 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3168 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3170 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3171 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3175 shader_addline(buffer
, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3176 dst_size
, src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3180 shader_addline(buffer
, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3181 src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3185 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3186 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
3188 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3189 struct glsl_src_param src_param
;
3190 const char *instruction
;
3194 /* Determine the GLSL function to use based on the opcode */
3195 /* TODO: Possibly make this a table for faster lookups */
3196 switch (ins
->handler_idx
)
3198 case WINED3DSIH_MIN
: instruction
= "min"; break;
3199 case WINED3DSIH_MAX
: instruction
= "max"; break;
3200 case WINED3DSIH_ABS
: instruction
= "abs"; break;
3201 case WINED3DSIH_FRC
: instruction
= "fract"; break;
3202 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
3203 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
3204 case WINED3DSIH_ROUND_NI
: instruction
= "floor"; break;
3205 case WINED3DSIH_SQRT
: instruction
= "sqrt"; break;
3206 default: instruction
= "";
3207 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
3211 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3213 shader_addline(buffer
, "%s(", instruction
);
3217 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3218 shader_addline(buffer
, "%s", src_param
.param_str
);
3219 for (i
= 1; i
< ins
->src_count
; ++i
)
3221 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
3222 shader_addline(buffer
, ", %s", src_param
.param_str
);
3226 shader_addline(buffer
, "));\n");
3229 static void shader_glsl_nop(const struct wined3d_shader_instruction
*ins
) {}
3231 static void shader_glsl_nrm(const struct wined3d_shader_instruction
*ins
)
3233 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3234 struct glsl_src_param src_param
;
3235 unsigned int mask_size
;
3239 write_mask
= shader_glsl_get_write_mask(ins
->dst
, dst_mask
);
3240 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3241 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3243 shader_addline(buffer
, "tmp0.x = dot(%s, %s);\n",
3244 src_param
.param_str
, src_param
.param_str
);
3245 shader_glsl_append_dst(buffer
, ins
);
3249 shader_addline(buffer
, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3250 mask_size
, src_param
.param_str
);
3254 shader_addline(buffer
, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3255 src_param
.param_str
);
3259 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction
*ins
)
3261 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3262 struct glsl_src_param src0_param
;
3263 const char *prefix
, *suffix
;
3264 unsigned int dst_size
;
3265 DWORD dst_write_mask
;
3267 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3268 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3270 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src0_param
);
3272 switch (ins
->handler_idx
)
3274 case WINED3DSIH_EXP
:
3275 case WINED3DSIH_EXPP
:
3280 case WINED3DSIH_LOG
:
3281 case WINED3DSIH_LOGP
:
3282 prefix
= "log2(abs(";
3286 case WINED3DSIH_RCP
:
3291 case WINED3DSIH_RSQ
:
3292 prefix
= "inversesqrt(abs(";
3299 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
3304 shader_addline(buffer
, "vec%u(%s%s%s));\n", dst_size
, prefix
, src0_param
.param_str
, suffix
);
3306 shader_addline(buffer
, "%s%s%s);\n", prefix
, src0_param
.param_str
, suffix
);
3309 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3310 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3311 * dst.x = 2^(floor(src))
3312 * dst.y = src - floor(src)
3313 * dst.z = 2^src (partial precision is allowed, but optional)
3315 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3316 * dst = 2^src; (partial precision is allowed, but optional)
3318 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
3320 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
3322 struct glsl_src_param src_param
;
3325 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
3327 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
3328 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
3329 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
3330 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
3332 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3333 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3334 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
3338 shader_glsl_scalar_op(ins
);
3341 static void shader_glsl_to_int(const struct wined3d_shader_instruction
*ins
)
3343 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3344 struct glsl_src_param src_param
;
3345 unsigned int mask_size
;
3348 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3349 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3350 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3353 shader_addline(buffer
, "ivec%u(%s));\n", mask_size
, src_param
.param_str
);
3355 shader_addline(buffer
, "int(%s));\n", src_param
.param_str
);
3358 static void shader_glsl_to_float(const struct wined3d_shader_instruction
*ins
)
3360 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3361 struct glsl_src_param src_param
;
3362 unsigned int mask_size
;
3365 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3366 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3367 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3370 shader_addline(buffer
, "vec%u(%s));\n", mask_size
, src_param
.param_str
);
3372 shader_addline(buffer
, "float(%s));\n", src_param
.param_str
);
3375 /** Process signed comparison opcodes in GLSL. */
3376 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
3378 struct glsl_src_param src0_param
;
3379 struct glsl_src_param src1_param
;
3381 unsigned int mask_size
;
3383 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3384 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3385 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3386 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3388 if (mask_size
> 1) {
3389 const char *compare
;
3391 switch(ins
->handler_idx
)
3393 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
3394 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
3395 default: compare
= "";
3396 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
3399 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
3400 src0_param
.param_str
, src1_param
.param_str
);
3402 switch(ins
->handler_idx
)
3404 case WINED3DSIH_SLT
:
3405 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3406 * to return 0.0 but step returns 1.0 because step is not < x
3407 * An alternative is a bvec compare padded with an unused second component.
3408 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3409 * issue. Playing with not() is not possible either because not() does not accept
3412 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
3413 src0_param
.param_str
, src1_param
.param_str
);
3415 case WINED3DSIH_SGE
:
3416 /* Here we can use the step() function and safe a conditional */
3417 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
3420 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
3426 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction
*ins
)
3428 const char *condition_prefix
, *condition_suffix
;
3429 struct wined3d_shader_dst_param dst
;
3430 struct glsl_src_param src0_param
;
3431 struct glsl_src_param src1_param
;
3432 struct glsl_src_param src2_param
;
3433 BOOL temp_destination
= FALSE
;
3434 DWORD cmp_channel
= 0;
3439 switch (ins
->handler_idx
)
3441 case WINED3DSIH_CMP
:
3442 condition_prefix
= "";
3443 condition_suffix
= " >= 0.0";
3446 case WINED3DSIH_CND
:
3447 condition_prefix
= "";
3448 condition_suffix
= " > 0.5";
3451 case WINED3DSIH_MOVC
:
3452 condition_prefix
= "bool(";
3453 condition_suffix
= ")";
3457 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
3458 condition_prefix
= "<unhandled prefix>";
3459 condition_suffix
= "<unhandled suffix>";
3463 if (shader_is_scalar(&ins
->dst
[0].reg
) || shader_is_scalar(&ins
->src
[0].reg
))
3465 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3466 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3467 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3468 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3470 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
3471 condition_prefix
, src0_param
.param_str
, condition_suffix
,
3472 src1_param
.param_str
, src2_param
.param_str
);
3478 /* Splitting the instruction up in multiple lines imposes a problem:
3479 * The first lines may overwrite source parameters of the following lines.
3480 * Deal with that by using a temporary destination register if needed. */
3481 if ((ins
->src
[0].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3482 && ins
->src
[0].reg
.type
== dst
.reg
.type
)
3483 || (ins
->src
[1].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3484 && ins
->src
[1].reg
.type
== dst
.reg
.type
)
3485 || (ins
->src
[2].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3486 && ins
->src
[2].reg
.type
== dst
.reg
.type
))
3487 temp_destination
= TRUE
;
3489 /* Cycle through all source0 channels. */
3490 for (i
= 0; i
< 4; ++i
)
3493 /* Find the destination channels which use the current source0 channel. */
3494 for (j
= 0; j
< 4; ++j
)
3496 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
3498 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
3499 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
3502 dst
.write_mask
= ins
->dst
[0].write_mask
& write_mask
;
3504 if (temp_destination
)
3506 if (!(write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
)))
3508 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
3510 else if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
, dst
.reg
.data_type
)))
3513 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
3514 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3515 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3517 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
3518 condition_prefix
, src0_param
.param_str
, condition_suffix
,
3519 src1_param
.param_str
, src2_param
.param_str
);
3522 if (temp_destination
)
3524 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
3525 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3526 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
3530 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3531 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3532 * the compare is done per component of src0. */
3533 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
3535 struct glsl_src_param src0_param
;
3536 struct glsl_src_param src1_param
;
3537 struct glsl_src_param src2_param
;
3539 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
3540 ins
->ctx
->reg_maps
->shader_version
.minor
);
3542 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
3544 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3545 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3546 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3547 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3549 if (ins
->coissue
&& ins
->dst
->write_mask
!= WINED3DSP_WRITEMASK_3
)
3550 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
3552 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
3553 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3557 shader_glsl_conditional_move(ins
);
3560 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3561 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
3563 struct glsl_src_param src0_param
;
3564 struct glsl_src_param src1_param
;
3565 struct glsl_src_param src2_param
;
3568 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3569 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3570 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3571 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3572 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
3573 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3576 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3577 Vertex shaders to GLSL codes */
3578 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
3581 int nComponents
= 0;
3582 struct wined3d_shader_dst_param tmp_dst
= {{0}};
3583 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
3584 struct wined3d_shader_instruction tmp_ins
;
3586 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
3588 /* Set constants for the temporary argument */
3589 tmp_ins
.ctx
= ins
->ctx
;
3590 tmp_ins
.dst_count
= 1;
3591 tmp_ins
.dst
= &tmp_dst
;
3592 tmp_ins
.src_count
= 2;
3593 tmp_ins
.src
= tmp_src
;
3595 switch(ins
->handler_idx
)
3597 case WINED3DSIH_M4x4
:
3599 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
3601 case WINED3DSIH_M4x3
:
3603 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
3605 case WINED3DSIH_M3x4
:
3607 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
3609 case WINED3DSIH_M3x3
:
3611 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
3613 case WINED3DSIH_M3x2
:
3615 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
3621 tmp_dst
= ins
->dst
[0];
3622 tmp_src
[0] = ins
->src
[0];
3623 tmp_src
[1] = ins
->src
[1];
3624 for (i
= 0; i
< nComponents
; ++i
)
3626 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
3627 shader_glsl_dot(&tmp_ins
);
3628 ++tmp_src
[1].reg
.idx
[0].offset
;
3633 The LRP instruction performs a component-wise linear interpolation
3634 between the second and third operands using the first operand as the
3635 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3636 This is equivalent to mix(src2, src1, src0);
3638 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
3640 struct glsl_src_param src0_param
;
3641 struct glsl_src_param src1_param
;
3642 struct glsl_src_param src2_param
;
3645 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3647 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3648 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3649 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3651 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
3652 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
3655 /** Process the WINED3DSIO_LIT instruction in GLSL:
3656 * dst.x = dst.w = 1.0
3657 * dst.y = (src0.x > 0) ? src0.x
3658 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3659 * where src.w is clamped at +- 128
3661 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
3663 struct glsl_src_param src0_param
;
3664 struct glsl_src_param src1_param
;
3665 struct glsl_src_param src3_param
;
3668 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3669 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3671 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3672 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
3673 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
3675 /* The sdk specifies the instruction like this
3677 * if(src.x > 0.0) dst.y = src.x
3679 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3682 * (where power = src.w clamped between -128 and 128)
3684 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3685 * dst.x = 1.0 ... No further explanation needed
3686 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3687 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3688 * dst.w = 1.0. ... Nothing fancy.
3690 * So we still have one conditional in there. So do this:
3691 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3693 * 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),
3694 * 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.
3695 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3697 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3698 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3699 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3701 shader_addline(ins
->ctx
->buffer
,
3702 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3703 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3704 src0_param
.param_str
, src3_param
.param_str
, src1_param
.param_str
,
3705 src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
3708 /** Process the WINED3DSIO_DST instruction in GLSL:
3710 * dst.y = src0.x * src0.y
3714 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
3716 struct glsl_src_param src0y_param
;
3717 struct glsl_src_param src0z_param
;
3718 struct glsl_src_param src1y_param
;
3719 struct glsl_src_param src1w_param
;
3722 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3723 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3725 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
3726 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
3727 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
3728 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
3730 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3731 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
3734 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3735 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3736 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3738 * dst.x = cos(src0.?)
3739 * dst.y = sin(src0.?)
3743 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
3745 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3746 struct glsl_src_param src0_param
;
3749 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
3751 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3753 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3756 case WINED3DSP_WRITEMASK_0
:
3757 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
3760 case WINED3DSP_WRITEMASK_1
:
3761 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
3764 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
3765 shader_addline(buffer
, "vec2(cos(%s), sin(%s)));\n",
3766 src0_param
.param_str
, src0_param
.param_str
);
3770 ERR("Write mask should be .x, .y or .xy\n");
3777 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3780 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3784 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3785 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3786 shader_addline(buffer
, "tmp0%s = sin(%s);\n", dst_mask
, src0_param
.param_str
);
3788 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3789 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3790 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
3792 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3793 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
3797 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3798 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3799 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
3802 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3804 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3805 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3806 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
3810 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3811 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3812 * generate invalid code
3814 static void shader_glsl_sgn(const struct wined3d_shader_instruction
*ins
)
3816 struct glsl_src_param src0_param
;
3819 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3820 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3822 shader_addline(ins
->ctx
->buffer
, "sign(%s));\n", src0_param
.param_str
);
3825 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3826 * Start a for() loop where src1.y is the initial value of aL,
3827 * increment aL by src1.z for a total of src1.x iterations.
3828 * Need to use a temporary variable for this operation.
3830 /* FIXME: I don't think nested loops will work correctly this way. */
3831 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
3833 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
3834 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3835 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
3836 const struct wined3d_shader_lconst
*constant
;
3837 struct glsl_src_param src1_param
;
3838 const DWORD
*control_values
= NULL
;
3840 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
3842 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
3844 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3845 * class hardware doesn't support real varying indexing, but Microsoft
3846 * designed this feature for Shader model 2.x+. If the loop control is
3847 * known at compile time, the GLSL compiler can unroll the loop, and
3848 * replace indirect addressing with direct addressing. */
3849 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
3851 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
3853 if (constant
->idx
== ins
->src
[1].reg
.idx
[0].offset
)
3855 control_values
= constant
->value
;
3863 struct wined3d_shader_loop_control loop_control
;
3864 loop_control
.count
= control_values
[0];
3865 loop_control
.start
= control_values
[1];
3866 loop_control
.step
= (int)control_values
[2];
3868 if (loop_control
.step
> 0)
3870 shader_addline(buffer
, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3871 loop_state
->current_depth
, loop_control
.start
,
3872 loop_state
->current_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
3873 loop_state
->current_depth
, loop_control
.step
);
3875 else if (loop_control
.step
< 0)
3877 shader_addline(buffer
, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3878 loop_state
->current_depth
, loop_control
.start
,
3879 loop_state
->current_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
3880 loop_state
->current_depth
, loop_control
.step
);
3884 shader_addline(buffer
, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3885 loop_state
->current_depth
, loop_control
.start
, loop_state
->current_depth
,
3886 loop_state
->current_depth
, loop_control
.count
,
3887 loop_state
->current_depth
);
3892 shader_addline(buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3893 loop_state
->current_depth
, loop_state
->current_reg
,
3894 src1_param
.reg_name
, loop_state
->current_depth
, src1_param
.reg_name
,
3895 loop_state
->current_depth
, loop_state
->current_reg
, src1_param
.reg_name
);
3898 ++loop_state
->current_reg
;
3902 shader_addline(buffer
, "for (;;)\n{\n");
3905 ++loop_state
->current_depth
;
3908 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
3910 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
3912 shader_addline(ins
->ctx
->buffer
, "}\n");
3914 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
3916 --loop_state
->current_depth
;
3917 --loop_state
->current_reg
;
3920 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
3922 --loop_state
->current_depth
;
3926 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
3928 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
3929 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
3930 const struct wined3d_shader_lconst
*constant
;
3931 struct glsl_src_param src0_param
;
3932 const DWORD
*control_values
= NULL
;
3934 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3935 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
3937 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
3939 if (constant
->idx
== ins
->src
[0].reg
.idx
[0].offset
)
3941 control_values
= constant
->value
;
3949 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3950 loop_state
->current_depth
, loop_state
->current_depth
,
3951 control_values
[0], loop_state
->current_depth
);
3955 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3956 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3957 loop_state
->current_depth
, loop_state
->current_depth
,
3958 src0_param
.param_str
, loop_state
->current_depth
);
3961 ++loop_state
->current_depth
;
3964 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
3966 struct glsl_src_param src0_param
;
3968 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3969 shader_addline(ins
->ctx
->buffer
, "if (bool(%s)) {\n", src0_param
.param_str
);
3972 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
3974 struct glsl_src_param src0_param
;
3975 struct glsl_src_param src1_param
;
3977 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3978 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3980 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
3981 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
3984 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
3986 shader_addline(ins
->ctx
->buffer
, "} else {\n");
3989 static void shader_glsl_emit(const struct wined3d_shader_instruction
*ins
)
3991 shader_addline(ins
->ctx
->buffer
, "EmitVertex();\n");
3994 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
3996 shader_addline(ins
->ctx
->buffer
, "break;\n");
3999 /* FIXME: According to MSDN the compare is done per component. */
4000 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
4002 struct glsl_src_param src0_param
;
4003 struct glsl_src_param src1_param
;
4005 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4006 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4008 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
4009 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
4012 static void shader_glsl_breakp(const struct wined3d_shader_instruction
*ins
)
4014 struct glsl_src_param src_param
;
4016 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
4017 shader_addline(ins
->ctx
->buffer
, "if (bool(%s)) break;\n", src_param
.param_str
);
4020 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
4022 shader_addline(ins
->ctx
->buffer
, "}\n");
4023 shader_addline(ins
->ctx
->buffer
, "void subroutine%u()\n{\n", ins
->src
[0].reg
.idx
[0].offset
);
4026 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
4028 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
[0].offset
);
4031 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
4033 struct glsl_src_param src1_param
;
4035 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4036 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n",
4037 src1_param
.param_str
, ins
->src
[0].reg
.idx
[0].offset
);
4040 static void shader_glsl_ret(const struct wined3d_shader_instruction
*ins
)
4042 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4043 * function only suppresses the unhandled instruction warning
4047 /*********************************************
4048 * Pixel Shader Specific Code begins here
4049 ********************************************/
4050 static void shader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
4052 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4053 ins
->ctx
->reg_maps
->shader_version
.minor
);
4054 struct glsl_sample_function sample_function
;
4055 DWORD sample_flags
= 0;
4057 DWORD mask
= 0, swizzle
;
4058 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4060 /* 1.0-1.4: Use destination register as sampler source.
4061 * 2.0+: Use provided sampler source. */
4062 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4063 resource_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4065 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4067 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4069 DWORD flags
= (priv
->cur_ps_args
->tex_transform
>> resource_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
4070 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
4071 enum wined3d_shader_resource_type resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
4073 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4074 if (flags
& WINED3D_PSARGS_PROJECTED
&& resource_type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4076 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4077 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
4079 case WINED3D_TTFF_COUNT1
:
4080 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4082 case WINED3D_TTFF_COUNT2
:
4083 mask
= WINED3DSP_WRITEMASK_1
;
4085 case WINED3D_TTFF_COUNT3
:
4086 mask
= WINED3DSP_WRITEMASK_2
;
4088 case WINED3D_TTFF_COUNT4
:
4089 case WINED3D_TTFF_DISABLE
:
4090 mask
= WINED3DSP_WRITEMASK_3
;
4095 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4097 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
4099 if (src_mod
== WINED3DSPSM_DZ
) {
4100 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4101 mask
= WINED3DSP_WRITEMASK_2
;
4102 } else if (src_mod
== WINED3DSPSM_DW
) {
4103 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4104 mask
= WINED3DSP_WRITEMASK_3
;
4109 if ((ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
4110 && ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4112 /* ps 2.0 texldp instruction always divides by the fourth component. */
4113 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4114 mask
= WINED3DSP_WRITEMASK_3
;
4118 if (priv
->cur_ps_args
->np2_fixup
& (1u << resource_idx
))
4119 sample_flags
|= WINED3D_GLSL_SAMPLE_NPOT
;
4121 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sample_flags
, &sample_function
);
4122 mask
|= sample_function
.coord_mask
;
4123 sample_function
.coord_mask
= mask
;
4125 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
4126 else swizzle
= ins
->src
[1].swizzle
;
4128 /* 1.0-1.3: Use destination register as coordinate source.
4129 1.4+: Use provided coordinate source register. */
4130 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4133 shader_glsl_write_mask_to_str(mask
, coord_mask
);
4134 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
4135 "T%u%s", resource_idx
, coord_mask
);
4139 struct glsl_src_param coord_param
;
4140 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
4141 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
4143 struct glsl_src_param bias
;
4144 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
4145 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
4146 "%s", coord_param
.param_str
);
4148 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
4149 "%s", coord_param
.param_str
);
4152 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4155 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
4157 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4158 struct glsl_src_param coord_param
, dx_param
, dy_param
;
4159 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_GRAD
;
4160 struct glsl_sample_function sample_function
;
4162 DWORD swizzle
= ins
->src
[1].swizzle
;
4163 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4165 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
])
4167 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4168 shader_glsl_tex(ins
);
4172 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4173 if (priv
->cur_ps_args
->np2_fixup
& (1u << sampler_idx
))
4174 sample_flags
|= WINED3D_GLSL_SAMPLE_NPOT
;
4176 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
4177 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4178 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.coord_mask
, &dx_param
);
4179 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.coord_mask
, &dy_param
);
4181 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
, NULL
,
4182 "%s", coord_param
.param_str
);
4183 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4186 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
4188 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4189 struct glsl_src_param coord_param
, lod_param
;
4190 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_LOD
;
4191 struct glsl_sample_function sample_function
;
4193 DWORD swizzle
= ins
->src
[1].swizzle
;
4194 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4196 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4197 if (ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
4198 && priv
->cur_ps_args
->np2_fixup
& (1u << sampler_idx
))
4199 sample_flags
|= WINED3D_GLSL_SAMPLE_NPOT
;
4201 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
4202 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4204 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
4206 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
]
4207 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
4209 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4210 * However, the NVIDIA drivers allow them in fragment shaders as well,
4211 * even without the appropriate extension. */
4212 WARN("Using %s in fragment shader.\n", sample_function
.name
->buffer
);
4214 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
,
4215 "%s", coord_param
.param_str
);
4216 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4219 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map
*sampler_map
,
4220 unsigned int resource_idx
, unsigned int sampler_idx
)
4222 struct wined3d_shader_sampler_map_entry
*entries
= sampler_map
->entries
;
4225 for (i
= 0; i
< sampler_map
->count
; ++i
)
4227 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
4228 return entries
[i
].bind_idx
;
4231 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx
, sampler_idx
);
4236 static void shader_glsl_sample(const struct wined3d_shader_instruction
*ins
)
4238 struct glsl_sample_function sample_function
;
4239 struct glsl_src_param coord_param
;
4240 unsigned int sampler_idx
;
4242 shader_glsl_get_sample_function(ins
->ctx
, ins
->src
[1].reg
.idx
[0].offset
, 0, &sample_function
);
4243 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4244 sampler_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
4245 ins
->src
[1].reg
.idx
[0].offset
, ins
->src
[2].reg
.idx
[0].offset
);
4246 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4247 NULL
, NULL
, NULL
, "%s", coord_param
.param_str
);
4248 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4251 static void shader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
4253 /* FIXME: Make this work for more than just 2D textures */
4254 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4255 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4257 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
4261 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4262 shader_addline(buffer
, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4263 ins
->dst
[0].reg
.idx
[0].offset
, dst_mask
);
4267 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
4268 DWORD reg
= ins
->src
[0].reg
.idx
[0].offset
;
4269 char dst_swizzle
[6];
4271 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
4273 if (src_mod
== WINED3DSPSM_DZ
|| src_mod
== WINED3DSPSM_DW
)
4275 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4276 struct glsl_src_param div_param
;
4277 DWORD src_writemask
= src_mod
== WINED3DSPSM_DZ
? WINED3DSP_WRITEMASK_2
: WINED3DSP_WRITEMASK_3
;
4279 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_writemask
, &div_param
);
4282 shader_addline(buffer
, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
4284 shader_addline(buffer
, "ffp_texcoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
4288 shader_addline(buffer
, "ffp_texcoord[%u]%s);\n", reg
, dst_swizzle
);
4293 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4294 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4295 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4296 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
4298 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4299 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4300 struct glsl_sample_function sample_function
;
4301 struct glsl_src_param src0_param
;
4304 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4306 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4307 * scalar, and projected sampling would require 4.
4309 * It is a dependent read - not valid with conditional NP2 textures
4311 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4312 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
4317 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4318 "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
4322 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4323 "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
4327 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4328 "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
4332 FIXME("Unexpected mask size %u\n", mask_size
);
4335 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4338 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4339 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4340 static void shader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
4342 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4343 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
4344 struct glsl_src_param src0_param
;
4346 unsigned int mask_size
;
4348 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4349 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
4350 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4352 if (mask_size
> 1) {
4353 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
4355 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
4359 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4360 * Calculate the depth as dst.x / dst.y */
4361 static void shader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
4363 struct glsl_dst_param dst_param
;
4365 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
4367 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4368 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4369 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4370 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4373 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4374 dst_param
.reg_name
, dst_param
.reg_name
);
4377 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4378 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4379 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4380 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4382 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
4384 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4385 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
4386 struct glsl_src_param src0_param
;
4388 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4390 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
4391 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4394 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4395 * Calculate the 1st of a 2-row matrix multiplication. */
4396 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
4398 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4399 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4400 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4401 struct glsl_src_param src0_param
;
4403 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4404 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4407 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4408 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4409 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
4411 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4412 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4413 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4414 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4415 struct glsl_src_param src0_param
;
4417 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4418 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx
->current_row
, reg
, src0_param
.param_str
);
4419 tex_mx
->texcoord_w
[tex_mx
->current_row
++] = reg
;
4422 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
4424 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4425 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4426 struct glsl_sample_function sample_function
;
4427 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4428 struct glsl_src_param src0_param
;
4430 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4431 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4433 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4435 /* Sample the texture using the calculated coordinates */
4436 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xy");
4437 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4440 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4441 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4442 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
4444 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4445 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4446 struct glsl_sample_function sample_function
;
4447 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4448 struct glsl_src_param src0_param
;
4450 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4451 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4453 /* Dependent read, not valid with conditional NP2 */
4454 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4456 /* Sample the texture using the calculated coordinates */
4457 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
4458 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4460 tex_mx
->current_row
= 0;
4463 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4464 * Perform the 3rd row of a 3x3 matrix multiply */
4465 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
4467 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4468 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4469 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4470 struct glsl_src_param src0_param
;
4473 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4475 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4476 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4477 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
4479 tex_mx
->current_row
= 0;
4482 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4483 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4484 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
4486 struct glsl_src_param src0_param
;
4487 struct glsl_src_param src1_param
;
4488 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4489 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4490 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4491 struct glsl_sample_function sample_function
;
4492 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4495 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4496 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
4498 /* Perform the last matrix multiply operation */
4499 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
4500 /* Reflection calculation */
4501 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
4503 /* Dependent read, not valid with conditional NP2 */
4504 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4505 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
4507 /* Sample the texture */
4508 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4509 NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
4510 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4512 tex_mx
->current_row
= 0;
4515 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4516 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4517 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
4519 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4520 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
4521 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
4522 struct glsl_sample_function sample_function
;
4523 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
4524 struct glsl_src_param src0_param
;
4527 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
4529 /* Perform the last matrix multiply operation */
4530 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
4532 /* Construct the eye-ray vector from w coordinates */
4533 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4534 tex_mx
->texcoord_w
[0], tex_mx
->texcoord_w
[1], reg
);
4535 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4537 /* Dependent read, not valid with conditional NP2 */
4538 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
4539 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
4541 /* Sample the texture using the calculated coordinates */
4542 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
4543 NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
4544 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4546 tex_mx
->current_row
= 0;
4549 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4550 * Apply a fake bump map transform.
4551 * texbem is pshader <= 1.3 only, this saves a few version checks
4553 static void shader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
4555 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4556 struct glsl_sample_function sample_function
;
4557 struct glsl_src_param coord_param
;
4563 sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4564 flags
= (priv
->cur_ps_args
->tex_transform
>> sampler_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
4565 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
4567 /* Dependent read, not valid with conditional NP2 */
4568 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4569 mask
= sample_function
.coord_mask
;
4571 shader_glsl_write_mask_to_str(mask
, coord_mask
);
4573 /* With projected textures, texbem only divides the static texture coord,
4574 * not the displacement, so we can't let GL handle this. */
4575 if (flags
& WINED3D_PSARGS_PROJECTED
)
4578 char coord_div_mask
[3];
4579 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
4581 case WINED3D_TTFF_COUNT1
:
4582 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4584 case WINED3D_TTFF_COUNT2
:
4585 div_mask
= WINED3DSP_WRITEMASK_1
;
4587 case WINED3D_TTFF_COUNT3
:
4588 div_mask
= WINED3DSP_WRITEMASK_2
;
4590 case WINED3D_TTFF_COUNT4
:
4591 case WINED3D_TTFF_DISABLE
:
4592 div_mask
= WINED3DSP_WRITEMASK_3
;
4595 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
4596 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
4599 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
4601 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4602 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
4603 coord_param
.param_str
, coord_mask
);
4605 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
4607 struct glsl_src_param luminance_param
;
4608 struct glsl_dst_param dst_param
;
4610 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
4611 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
4613 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4614 dst_param
.reg_name
, dst_param
.mask_str
,
4615 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
4617 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4620 static void shader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
4622 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4623 struct glsl_src_param src0_param
, src1_param
;
4625 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
4626 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
4628 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4629 shader_addline(ins
->ctx
->buffer
, "%s + bumpenv_mat%u * %s);\n",
4630 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
4633 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4634 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4635 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
4637 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4638 struct glsl_sample_function sample_function
;
4639 struct glsl_src_param src0_param
;
4641 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
4643 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4644 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4645 "%s.wx", src0_param
.reg_name
);
4646 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4649 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4650 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4651 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
4653 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4654 struct glsl_sample_function sample_function
;
4655 struct glsl_src_param src0_param
;
4657 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
4659 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4660 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4661 "%s.yz", src0_param
.reg_name
);
4662 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4665 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4666 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4667 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
4669 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4670 struct glsl_sample_function sample_function
;
4671 struct glsl_src_param src0_param
;
4673 /* Dependent read, not valid with conditional NP2 */
4674 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
4675 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
4677 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
4678 "%s", src0_param
.param_str
);
4679 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4682 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4683 * If any of the first 3 components are < 0, discard this pixel */
4684 static void shader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
4686 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 4)
4688 struct glsl_src_param src_param
;
4690 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
4691 shader_addline(ins
->ctx
->buffer
, "if (bool(floatBitsToUint(%s))) discard;\n", src_param
.param_str
);
4695 struct glsl_dst_param dst_param
;
4697 /* The argument is a destination parameter, and no writemasks are allowed */
4698 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
4700 /* 2.0 shaders compare all 4 components in texkill. */
4701 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
4702 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
4703 /* 1.x shaders only compare the first 3 components, probably due to
4704 * the nature of the texkill instruction as a tex* instruction, and
4705 * phase, which kills all .w components. Even if all 4 components are
4706 * defined, only the first 3 are used. */
4708 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
4712 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4713 * dst = dot2(src0, src1) + src2 */
4714 static void shader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
4716 struct glsl_src_param src0_param
;
4717 struct glsl_src_param src1_param
;
4718 struct glsl_src_param src2_param
;
4720 unsigned int mask_size
;
4722 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4723 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
4725 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
4726 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
4727 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
4729 if (mask_size
> 1) {
4730 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
4731 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4733 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
4734 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4738 static void shader_glsl_input_pack(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
4739 const struct wined3d_shader_signature
*input_signature
,
4740 const struct wined3d_shader_reg_maps
*reg_maps
,
4741 const struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
4745 for (i
= 0; i
< input_signature
->element_count
; ++i
)
4747 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
4748 const char *semantic_name
;
4753 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
4756 semantic_name
= input
->semantic_name
;
4757 semantic_idx
= input
->semantic_idx
;
4758 shader_glsl_write_mask_to_str(input
->mask
, reg_mask
);
4760 if (args
->vp_mode
== vertexshader
)
4762 if (input
->sysval_semantic
== WINED3D_SV_POSITION
)
4763 shader_addline(buffer
, "ps_in[%u]%s = vpos%s;\n",
4764 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4765 else if (args
->pointsprite
&& shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
4766 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input
->register_idx
);
4768 shader_addline(buffer
, "ps_in[%u]%s = ps_link[%u]%s;\n",
4769 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
4770 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
);
4772 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
4774 if (args
->pointsprite
)
4775 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
4776 shader
->u
.ps
.input_reg_map
[input
->register_idx
]);
4777 else if (args
->vp_mode
== pretransformed
&& args
->texcoords_initialized
& (1u << semantic_idx
))
4778 shader_addline(buffer
, "ps_in[%u]%s = %s[%u]%s;\n",
4779 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
4780 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
4781 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx
, reg_mask
);
4783 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4784 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4786 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
))
4789 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
4790 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4791 else if (semantic_idx
== 1)
4792 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
4793 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4795 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4796 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4800 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4801 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
4806 /*********************************************
4807 * Vertex Shader Specific Code begins here
4808 ********************************************/
4810 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
)
4812 struct glsl_program_key key
;
4814 key
.vs_id
= entry
->vs
.id
;
4815 key
.gs_id
= entry
->gs
.id
;
4816 key
.ps_id
= entry
->ps
.id
;
4818 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
4820 ERR("Failed to insert program entry.\n");
4824 static struct glsl_shader_prog_link
*get_glsl_program_entry(const struct shader_glsl_priv
*priv
,
4825 GLuint vs_id
, GLuint gs_id
, GLuint ps_id
)
4827 struct wine_rb_entry
*entry
;
4828 struct glsl_program_key key
;
4834 entry
= wine_rb_get(&priv
->program_lookup
, &key
);
4835 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
4838 /* Context activation is done by the caller. */
4839 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
4840 struct glsl_shader_prog_link
*entry
)
4842 struct glsl_program_key key
;
4844 key
.vs_id
= entry
->vs
.id
;
4845 key
.gs_id
= entry
->gs
.id
;
4846 key
.ps_id
= entry
->ps
.id
;
4847 wine_rb_remove(&priv
->program_lookup
, &key
);
4849 GL_EXTCALL(glDeleteProgram(entry
->id
));
4851 list_remove(&entry
->vs
.shader_entry
);
4853 list_remove(&entry
->gs
.shader_entry
);
4855 list_remove(&entry
->ps
.shader_entry
);
4856 HeapFree(GetProcessHeap(), 0, entry
->vs
.uniform_f_locations
);
4857 HeapFree(GetProcessHeap(), 0, entry
->ps
.uniform_f_locations
);
4858 HeapFree(GetProcessHeap(), 0, entry
);
4861 static void handle_ps3_input(struct shader_glsl_priv
*priv
,
4862 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
4863 const struct wined3d_shader_signature
*input_signature
,
4864 const struct wined3d_shader_reg_maps
*reg_maps_in
,
4865 const struct wined3d_shader_signature
*output_signature
,
4866 const struct wined3d_shader_reg_maps
*reg_maps_out
)
4868 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
4869 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
4873 unsigned int in_count
= vec4_varyings(3, gl_info
);
4874 unsigned int max_varyings
= legacy_context
? in_count
+ 2 : in_count
;
4876 struct wined3d_string_buffer
*destination
= string_buffer_get(&priv
->string_buffers
);
4878 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * max_varyings
);
4880 for (i
= 0; i
< input_signature
->element_count
; ++i
)
4882 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
4884 if (!(reg_maps_in
->input_registers
& (1u << input
->register_idx
)))
4887 in_idx
= map
[input
->register_idx
];
4888 /* Declared, but not read register */
4891 if (in_idx
>= max_varyings
)
4893 FIXME("More input varyings declared than supported, expect issues.\n");
4897 if (in_idx
== in_count
)
4898 string_buffer_sprintf(destination
, "gl_FrontColor");
4899 else if (in_idx
== in_count
+ 1)
4900 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
4902 string_buffer_sprintf(destination
, "ps_link[%u]", in_idx
);
4907 for (j
= 0; j
< output_signature
->element_count
; ++j
)
4909 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[j
];
4912 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
))
4913 || input
->semantic_idx
!= output
->semantic_idx
4914 || strcmp(input
->semantic_name
, output
->semantic_name
)
4915 || !(mask
= input
->mask
& output
->mask
))
4918 if (set
[in_idx
] == ~0u)
4920 set
[in_idx
] |= mask
& reg_maps_out
->u
.output_registers_mask
[output
->register_idx
];
4921 shader_glsl_write_mask_to_str(mask
, reg_mask
);
4923 shader_addline(buffer
, "%s%s = vs_out[%u]%s;\n",
4924 destination
->buffer
, reg_mask
, output
->register_idx
, reg_mask
);
4928 for (i
= 0; i
< max_varyings
; ++i
)
4932 if (!set
[i
] || set
[i
] == WINED3DSP_WRITEMASK_ALL
)
4935 if (set
[i
] == ~0U) set
[i
] = 0;
4938 if (!(set
[i
] & WINED3DSP_WRITEMASK_0
)) reg_mask
[size
++] = 'x';
4939 if (!(set
[i
] & WINED3DSP_WRITEMASK_1
)) reg_mask
[size
++] = 'y';
4940 if (!(set
[i
] & WINED3DSP_WRITEMASK_2
)) reg_mask
[size
++] = 'z';
4941 if (!(set
[i
] & WINED3DSP_WRITEMASK_3
)) reg_mask
[size
++] = 'w';
4942 reg_mask
[size
] = '\0';
4945 string_buffer_sprintf(destination
, "gl_FrontColor");
4946 else if (i
== in_count
+ 1)
4947 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
4949 string_buffer_sprintf(destination
, "ps_link[%u]", i
);
4951 if (size
== 1) shader_addline(buffer
, "%s.%s = 0.0;\n", destination
->buffer
, reg_mask
);
4952 else shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
->buffer
, reg_mask
, size
);
4955 HeapFree(GetProcessHeap(), 0, set
);
4956 string_buffer_release(&priv
->string_buffers
, destination
);
4959 /* Context activation is done by the caller. */
4960 static GLuint
generate_param_reorder_function(struct shader_glsl_priv
*priv
,
4961 const struct wined3d_shader
*vs
, const struct wined3d_shader
*ps
,
4962 BOOL per_vertex_point_size
, const struct wined3d_gl_info
*gl_info
)
4964 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
4966 DWORD ps_major
= ps
? ps
->reg_maps
.shader_version
.major
: 0;
4968 const char *semantic_name
;
4971 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
4973 string_buffer_clear(buffer
);
4975 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, &vs
->reg_maps
.shader_version
));
4977 if (per_vertex_point_size
)
4979 shader_addline(buffer
, "uniform struct\n{\n");
4980 shader_addline(buffer
, " float size_min;\n");
4981 shader_addline(buffer
, " float size_max;\n");
4982 shader_addline(buffer
, "} ffp_point;\n");
4987 DWORD colors_written_mask
[2] = {0};
4988 DWORD texcoords_written_mask
[MAX_TEXTURES
] = {0};
4990 if (!legacy_context
)
4992 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_diffuse;\n");
4993 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_specular;\n");
4994 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
4995 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
4998 shader_addline(buffer
, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs
->limits
->packed_output
);
5000 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
5002 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
5005 if (!(vs
->reg_maps
.output_registers
& (1u << output
->register_idx
)))
5008 semantic_name
= output
->semantic_name
;
5009 semantic_idx
= output
->semantic_idx
;
5010 write_mask
= output
->mask
;
5011 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
5013 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
) && semantic_idx
< 2)
5016 shader_addline(buffer
, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5017 semantic_idx
? "Secondary" : "", reg_mask
, output
->register_idx
, reg_mask
);
5019 shader_addline(buffer
, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5020 semantic_idx
? "specular" : "diffuse", reg_mask
, output
->register_idx
, reg_mask
);
5022 colors_written_mask
[semantic_idx
] = write_mask
;
5024 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
5026 shader_addline(buffer
, "gl_Position%s = vs_out[%u]%s;\n",
5027 reg_mask
, output
->register_idx
, reg_mask
);
5029 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
5031 if (semantic_idx
< MAX_TEXTURES
)
5033 shader_addline(buffer
, "%s[%u]%s = vs_out[%u]%s;\n",
5034 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord",
5035 semantic_idx
, reg_mask
, output
->register_idx
, reg_mask
);
5036 texcoords_written_mask
[semantic_idx
] = write_mask
;
5039 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
5041 shader_addline(buffer
, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5042 output
->register_idx
, reg_mask
[1]);
5044 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_FOG
))
5046 shader_addline(buffer
, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5047 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5048 output
->register_idx
, reg_mask
[1]);
5052 for (i
= 0; i
< 2; ++i
)
5054 if (colors_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
5056 shader_glsl_write_mask_to_str(~colors_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
5058 shader_addline(buffer
, "%s%s = vec4(1.0)%s;\n",
5059 legacy_context
? "gl_FrontColor" : "ffp_varying_diffuse",
5060 reg_mask
, reg_mask
);
5062 shader_addline(buffer
, "%s%s = vec4(0.0)%s;\n",
5063 legacy_context
? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5064 reg_mask
, reg_mask
);
5067 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
5069 if (ps
&& !(ps
->reg_maps
.texcoord
& (1u << i
)))
5072 if (texcoords_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
5074 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
5075 && !texcoords_written_mask
[i
])
5078 shader_glsl_write_mask_to_str(~texcoords_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
5079 shader_addline(buffer
, "%s[%u]%s = vec4(0.0)%s;\n",
5080 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
, reg_mask
, reg_mask
);
5086 UINT in_count
= min(vec4_varyings(ps_major
, gl_info
), ps
->limits
->packed_input
);
5088 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", in_count
);
5089 shader_addline(buffer
, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs
->limits
->packed_output
);
5091 /* First, sort out position and point size. Those are not passed to the pixel shader */
5092 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
5094 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
5096 if (!(vs
->reg_maps
.output_registers
& (1u << output
->register_idx
)))
5099 semantic_name
= output
->semantic_name
;
5100 semantic_idx
= output
->semantic_idx
;
5101 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
5103 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
5105 shader_addline(buffer
, "gl_Position%s = vs_out[%u]%s;\n",
5106 reg_mask
, output
->register_idx
, reg_mask
);
5108 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
5110 shader_addline(buffer
, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5111 output
->register_idx
, reg_mask
[1]);
5115 /* Then, fix the pixel shader input */
5116 handle_ps3_input(priv
, gl_info
, ps
->u
.ps
.input_reg_map
, &ps
->input_signature
,
5117 &ps
->reg_maps
, &vs
->output_signature
, &vs
->reg_maps
);
5120 shader_addline(buffer
, "}\n");
5122 ret
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
5123 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5124 shader_glsl_compile(gl_info
, ret
, buffer
->buffer
);
5129 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer
*buffer
)
5131 shader_addline(buffer
, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
5132 shader_addline(buffer
, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5133 shader_addline(buffer
, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
5134 shader_addline(buffer
, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
5135 shader_addline(buffer
, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
5136 shader_addline(buffer
, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
5139 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer
*buffer
, enum wined3d_ffp_ps_fog_mode mode
)
5143 case WINED3D_FFP_PS_FOG_OFF
:
5146 case WINED3D_FFP_PS_FOG_LINEAR
:
5147 shader_addline(buffer
, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5150 case WINED3D_FFP_PS_FOG_EXP
:
5151 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5154 case WINED3D_FFP_PS_FOG_EXP2
:
5155 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5156 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5160 ERR("Invalid fog mode %#x.\n", mode
);
5164 shader_addline(buffer
, "gl_FragData[0].xyz = mix(ffp_fog.color.xyz, gl_FragData[0].xyz,"
5165 " clamp(fog, 0.0, 1.0));\n");
5168 /* Context activation is done by the caller. */
5169 static GLuint
shader_glsl_generate_pshader(const struct wined3d_context
*context
,
5170 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5171 const struct wined3d_shader
*shader
,
5172 const struct ps_compile_args
*args
, struct ps_np2fixup_info
*np2fixup_info
)
5174 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
5175 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5176 const DWORD
*function
= shader
->function
;
5177 struct shader_glsl_ctx_priv priv_ctx
;
5178 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5180 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5181 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
5183 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
5184 priv_ctx
.cur_ps_args
= args
;
5185 priv_ctx
.cur_np2fixup_info
= np2fixup_info
;
5186 priv_ctx
.string_buffers
= string_buffers
;
5188 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, ®_maps
->shader_version
));
5190 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
5191 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
5192 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
5193 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
5194 /* The spec says that it doesn't have to be explicitly enabled, but the
5195 * nvidia drivers write a warning if we don't do so. */
5196 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
5197 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
5198 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
5199 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
5200 if (gl_info
->supported
[EXT_GPU_SHADER4
])
5201 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
5203 /* Base Declarations */
5204 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
5206 if (reg_maps
->shader_version
.major
< 3 || args
->vp_mode
!= vertexshader
)
5209 WORD map
= reg_maps
->texcoord
;
5213 if (glsl_is_color_reg_read(shader
, 0))
5214 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
5215 if (glsl_is_color_reg_read(shader
, 1))
5216 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
5219 for (i
= 0; map
; map
>>= 1, ++i
)
5223 if (args
->pointsprite
)
5224 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i
);
5225 else if (args
->texcoords_initialized
& (1u << i
))
5226 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n", i
,
5227 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
);
5229 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", i
);
5230 shader_addline(buffer
, "vec4 T%u = ffp_texcoord[%u];\n", i
, i
);
5235 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5238 /* Pack 3.0 inputs */
5239 if (reg_maps
->shader_version
.major
>= 3)
5240 shader_glsl_input_pack(shader
, buffer
, &shader
->input_signature
, reg_maps
, args
, gl_info
);
5242 /* Base Shader Body */
5243 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
5245 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5246 if (reg_maps
->shader_version
.major
< 2)
5248 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5249 shader_addline(buffer
, "gl_FragData[0] = R0;\n");
5252 if (args
->srgb_correction
)
5253 shader_glsl_generate_srgb_write_correction(buffer
);
5255 /* SM < 3 does not replace the fog stage. */
5256 if (reg_maps
->shader_version
.major
< 3)
5257 shader_glsl_generate_fog_code(buffer
, args
->fog
);
5259 shader_addline(buffer
, "}\n");
5261 TRACE("Compiling shader object %u.\n", shader_id
);
5262 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
5267 /* Context activation is done by the caller. */
5268 static GLuint
shader_glsl_generate_vshader(const struct wined3d_context
*context
,
5269 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5270 const struct wined3d_shader
*shader
,
5271 const struct vs_compile_args
*args
)
5273 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
5274 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5275 const DWORD
*function
= shader
->function
;
5276 struct shader_glsl_ctx_priv priv_ctx
;
5277 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5279 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5280 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
5282 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, ®_maps
->shader_version
));
5284 if (gl_info
->supported
[ARB_DRAW_INSTANCED
])
5285 shader_addline(buffer
, "#extension GL_ARB_draw_instanced : enable\n");
5286 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
5287 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
5288 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
5289 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
5290 if (gl_info
->supported
[EXT_GPU_SHADER4
])
5291 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
5293 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
5294 priv_ctx
.cur_vs_args
= args
;
5295 priv_ctx
.string_buffers
= string_buffers
;
5297 /* Base Declarations */
5298 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
5300 /* Base Shader Body */
5301 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
5303 /* Unpack outputs */
5304 shader_addline(buffer
, "order_ps_input(vs_out);\n");
5306 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5307 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5308 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5309 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5311 if (reg_maps
->shader_version
.major
< 3)
5313 if (args
->fog_src
== VS_FOG_Z
)
5314 shader_addline(buffer
, "%s = gl_Position.z;\n",
5315 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5316 else if (!reg_maps
->fog
)
5317 shader_addline(buffer
, "%s = 0.0;\n",
5318 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5321 /* We always store the clipplanes without y inversion */
5322 if (args
->clip_enabled
)
5323 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
5325 if (args
->point_size
&& !args
->per_vertex_point_size
)
5326 shader_addline(buffer
, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5328 /* Write the final position.
5330 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5331 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5332 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5333 * contains 1.0 to allow a mad.
5335 shader_addline(buffer
, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5336 shader_addline(buffer
, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5338 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5340 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5341 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5342 * which is the same as z = z * 2 - w.
5344 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5346 shader_addline(buffer
, "}\n");
5348 TRACE("Compiling shader object %u.\n", shader_id
);
5349 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
5354 /* Context activation is done by the caller. */
5355 static GLuint
shader_glsl_generate_geometry_shader(const struct wined3d_context
*context
,
5356 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5357 const struct wined3d_shader
*shader
)
5359 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
5360 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
5361 const DWORD
*function
= shader
->function
;
5362 struct shader_glsl_ctx_priv priv_ctx
;
5365 shader_id
= GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER
));
5367 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, ®_maps
->shader_version
));
5369 if (gl_info
->supported
[ARB_GEOMETRY_SHADER4
])
5370 shader_addline(buffer
, "#extension GL_ARB_geometry_shader4 : enable\n");
5371 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
5372 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
5373 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
5374 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
5375 if (gl_info
->supported
[EXT_GPU_SHADER4
])
5376 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
5378 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
5379 priv_ctx
.string_buffers
= string_buffers
;
5380 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
5381 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
5382 shader_addline(buffer
, "}\n");
5384 TRACE("Compiling shader object %u.\n", shader_id
);
5385 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
5390 static GLuint
find_glsl_pshader(const struct wined3d_context
*context
,
5391 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5392 struct wined3d_shader
*shader
,
5393 const struct ps_compile_args
*args
, const struct ps_np2fixup_info
**np2fixup_info
)
5395 struct glsl_ps_compiled_shader
*gl_shaders
, *new_array
;
5396 struct glsl_shader_private
*shader_data
;
5397 struct ps_np2fixup_info
*np2fixup
;
5402 if (!shader
->backend_data
)
5404 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
5405 if (!shader
->backend_data
)
5407 ERR("Failed to allocate backend data.\n");
5411 shader_data
= shader
->backend_data
;
5412 gl_shaders
= shader_data
->gl_shaders
.ps
;
5414 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5415 * so a linear search is more performant than a hashmap or a binary search
5416 * (cache coherency etc)
5418 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
5420 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
5422 if (args
->np2_fixup
)
5423 *np2fixup_info
= &gl_shaders
[i
].np2fixup
;
5424 return gl_shaders
[i
].id
;
5428 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
5429 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
5430 if (shader_data
->num_gl_shaders
)
5432 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
5433 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
,
5434 new_size
* sizeof(*gl_shaders
));
5438 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
5443 ERR("Out of memory\n");
5446 shader_data
->gl_shaders
.ps
= new_array
;
5447 shader_data
->shader_array_size
= new_size
;
5448 gl_shaders
= new_array
;
5451 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
5453 np2fixup
= &gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
;
5454 memset(np2fixup
, 0, sizeof(*np2fixup
));
5455 *np2fixup_info
= args
->np2_fixup
? np2fixup
: NULL
;
5457 pixelshader_update_resource_types(shader
, args
->tex_types
);
5459 string_buffer_clear(buffer
);
5460 ret
= shader_glsl_generate_pshader(context
, buffer
, string_buffers
, shader
, args
, np2fixup
);
5461 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
5466 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
5467 const DWORD use_map
)
5469 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
5470 if((stored
->clip_enabled
) != new->clip_enabled
) return FALSE
;
5471 if (stored
->point_size
!= new->point_size
)
5473 if (stored
->per_vertex_point_size
!= new->per_vertex_point_size
)
5475 return stored
->fog_src
== new->fog_src
;
5478 static GLuint
find_glsl_vshader(const struct wined3d_context
*context
,
5479 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5480 struct wined3d_shader
*shader
,
5481 const struct vs_compile_args
*args
)
5485 DWORD use_map
= context
->stream_info
.use_map
;
5486 struct glsl_vs_compiled_shader
*gl_shaders
, *new_array
;
5487 struct glsl_shader_private
*shader_data
;
5490 if (!shader
->backend_data
)
5492 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
5493 if (!shader
->backend_data
)
5495 ERR("Failed to allocate backend data.\n");
5499 shader_data
= shader
->backend_data
;
5500 gl_shaders
= shader_data
->gl_shaders
.vs
;
5502 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5503 * so a linear search is more performant than a hashmap or a binary search
5504 * (cache coherency etc)
5506 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
5508 if (vs_args_equal(&gl_shaders
[i
].args
, args
, use_map
))
5509 return gl_shaders
[i
].id
;
5512 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
5514 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
5515 if (shader_data
->num_gl_shaders
)
5517 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
5518 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
,
5519 new_size
* sizeof(*gl_shaders
));
5523 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
5528 ERR("Out of memory\n");
5531 shader_data
->gl_shaders
.vs
= new_array
;
5532 shader_data
->shader_array_size
= new_size
;
5533 gl_shaders
= new_array
;
5536 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
5538 string_buffer_clear(buffer
);
5539 ret
= shader_glsl_generate_vshader(context
, buffer
, string_buffers
, shader
, args
);
5540 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
5545 static GLuint
find_glsl_geometry_shader(const struct wined3d_context
*context
,
5546 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
5547 struct wined3d_shader
*shader
)
5549 struct glsl_gs_compiled_shader
*gl_shaders
;
5550 struct glsl_shader_private
*shader_data
;
5553 if (!shader
->backend_data
)
5555 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
5557 ERR("Failed to allocate backend data.\n");
5561 shader_data
= shader
->backend_data
;
5562 gl_shaders
= shader_data
->gl_shaders
.gs
;
5564 if (shader_data
->num_gl_shaders
)
5565 return gl_shaders
[0].id
;
5567 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
5569 if (!(shader_data
->gl_shaders
.gs
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
))))
5571 ERR("Failed to allocate GL shader array.\n");
5574 shader_data
->shader_array_size
= 1;
5575 gl_shaders
= shader_data
->gl_shaders
.gs
;
5577 string_buffer_clear(buffer
);
5578 ret
= shader_glsl_generate_geometry_shader(context
, buffer
, string_buffers
, shader
);
5579 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
5584 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs
, const char *material
)
5588 case WINED3D_MCS_MATERIAL
:
5590 case WINED3D_MCS_COLOR1
:
5591 return "ffp_attrib_diffuse";
5592 case WINED3D_MCS_COLOR2
:
5593 return "ffp_attrib_specular";
5595 ERR("Invalid material color source %#x.\n", mcs
);
5600 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer
*buffer
,
5601 const struct wined3d_ffp_vs_settings
*settings
, BOOL legacy_lighting
)
5603 const char *diffuse
, *specular
, *emissive
, *ambient
;
5604 enum wined3d_light_type light_type
;
5607 if (!settings
->lighting
)
5609 shader_addline(buffer
, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5610 shader_addline(buffer
, "ffp_varying_specular = ffp_attrib_specular;\n");
5614 shader_addline(buffer
, "vec3 ambient = ffp_light_ambient;\n");
5615 shader_addline(buffer
, "vec3 diffuse = vec3(0.0);\n");
5616 shader_addline(buffer
, "vec4 specular = vec4(0.0);\n");
5617 shader_addline(buffer
, "vec3 dir, dst;\n");
5618 shader_addline(buffer
, "float att, t;\n");
5620 ambient
= shader_glsl_ffp_mcs(settings
->ambient_source
, "ffp_material.ambient");
5621 diffuse
= shader_glsl_ffp_mcs(settings
->diffuse_source
, "ffp_material.diffuse");
5622 specular
= shader_glsl_ffp_mcs(settings
->specular_source
, "ffp_material.specular");
5623 emissive
= shader_glsl_ffp_mcs(settings
->emissive_source
, "ffp_material.emissive");
5625 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
5627 light_type
= (settings
->light_type
>> WINED3D_FFP_LIGHT_TYPE_SHIFT(i
)) & WINED3D_FFP_LIGHT_TYPE_MASK
;
5630 case WINED3D_LIGHT_POINT
:
5631 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i
);
5632 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
5633 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
5634 shader_addline(buffer
, "dst.x = 1.0;\n");
5635 if (legacy_lighting
)
5637 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i
, i
);
5638 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
5642 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", i
);
5644 shader_addline(buffer
, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5645 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i
, i
, i
);
5646 if (!legacy_lighting
)
5647 shader_addline(buffer
, "att = 1.0 / att;\n");
5648 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", i
);
5649 if (!settings
->normal
)
5651 if (!legacy_lighting
)
5652 shader_addline(buffer
, "}\n");
5655 shader_addline(buffer
, "dir = normalize(dir);\n");
5656 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5657 " * ffp_light[%u].diffuse.xyz) * att;\n", i
);
5658 if (settings
->localviewer
)
5659 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5661 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5662 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5663 " * ffp_light[%u].specular) * att;\n", i
);
5664 if (!legacy_lighting
)
5665 shader_addline(buffer
, "}\n");
5668 case WINED3D_LIGHT_SPOT
:
5669 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i
);
5670 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
5671 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
5672 shader_addline(buffer
, "dst.x = 1.0;\n");
5673 if (legacy_lighting
)
5675 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i
, i
);
5676 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
5680 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", i
);
5682 shader_addline(buffer
, "dir = normalize(dir);\n");
5683 shader_addline(buffer
, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i
);
5684 shader_addline(buffer
, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i
);
5685 shader_addline(buffer
, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i
);
5686 shader_addline(buffer
, "else att = pow((t - ffp_light[%u].cos_hphi)"
5687 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5689 if (legacy_lighting
)
5690 shader_addline(buffer
, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5691 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5694 shader_addline(buffer
, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5695 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5697 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", i
);
5698 if (!settings
->normal
)
5700 if (!legacy_lighting
)
5701 shader_addline(buffer
, "}\n");
5704 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5705 " * ffp_light[%u].diffuse.xyz) * att;\n", i
);
5706 if (settings
->localviewer
)
5707 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5709 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5710 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5711 " * ffp_light[%u].specular) * att;\n", i
);
5712 if (!legacy_lighting
)
5713 shader_addline(buffer
, "}\n");
5716 case WINED3D_LIGHT_DIRECTIONAL
:
5717 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", i
);
5718 if (!settings
->normal
)
5720 shader_addline(buffer
, "dir = normalize(ffp_light[%u].direction.xyz);\n", i
);
5721 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5722 " * ffp_light[%u].diffuse.xyz;\n", i
);
5723 /* TODO: In the non-local viewer case the halfvector is constant
5724 * and could be precomputed and stored in a uniform. */
5725 if (settings
->localviewer
)
5726 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5728 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5729 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5730 " * ffp_light[%u].specular;\n", i
);
5733 case WINED3D_LIGHT_PARALLELPOINT
:
5734 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", i
);
5735 if (!settings
->normal
)
5737 shader_addline(buffer
, "dir = normalize(ffp_light[%u].position.xyz);\n", i
);
5738 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5739 " * ffp_light[%u].diffuse.xyz;\n", i
);
5740 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5741 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5742 " * ffp_light[%u].specular;\n", i
);
5747 FIXME("Unhandled light type %#x.\n", light_type
);
5752 shader_addline(buffer
, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5753 ambient
, diffuse
, emissive
);
5754 shader_addline(buffer
, "ffp_varying_diffuse.w = %s.w;\n", diffuse
);
5755 shader_addline(buffer
, "ffp_varying_specular = %s * specular;\n", specular
);
5758 /* Context activation is done by the caller. */
5759 static GLuint
shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
5760 const struct wined3d_ffp_vs_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
5762 static const struct attrib_info
5765 const char name
[24];
5769 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
5770 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
5771 /* TODO: Indexed vertex blending */
5772 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
5773 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
5774 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
5775 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
5776 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
5778 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5779 BOOL legacy_lighting
= priv
->legacy_lighting
;
5782 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5783 BOOL output_legacy_fogcoord
= legacy_context
;
5785 string_buffer_clear(buffer
);
5787 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, NULL
));
5789 for (i
= 0; i
< WINED3D_FFP_ATTRIBS_COUNT
; ++i
)
5791 const char *type
= i
< ARRAY_SIZE(attrib_info
) ? attrib_info
[i
].type
: "vec4";
5793 shader_addline(buffer
, "attribute %s vs_in%u;\n", type
, i
);
5795 shader_addline(buffer
, "\n");
5797 shader_addline(buffer
, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS
);
5798 shader_addline(buffer
, "uniform mat4 ffp_projection_matrix;\n");
5799 shader_addline(buffer
, "uniform mat3 ffp_normal_matrix;\n");
5800 shader_addline(buffer
, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES
);
5802 shader_addline(buffer
, "uniform struct\n{\n");
5803 shader_addline(buffer
, " vec4 emissive;\n");
5804 shader_addline(buffer
, " vec4 ambient;\n");
5805 shader_addline(buffer
, " vec4 diffuse;\n");
5806 shader_addline(buffer
, " vec4 specular;\n");
5807 shader_addline(buffer
, " float shininess;\n");
5808 shader_addline(buffer
, "} ffp_material;\n");
5810 shader_addline(buffer
, "uniform vec3 ffp_light_ambient;\n");
5811 shader_addline(buffer
, "uniform struct\n{\n");
5812 shader_addline(buffer
, " vec4 diffuse;\n");
5813 shader_addline(buffer
, " vec4 specular;\n");
5814 shader_addline(buffer
, " vec4 ambient;\n");
5815 shader_addline(buffer
, " vec4 position;\n");
5816 shader_addline(buffer
, " vec3 direction;\n");
5817 shader_addline(buffer
, " float range;\n");
5818 shader_addline(buffer
, " float falloff;\n");
5819 shader_addline(buffer
, " float c_att;\n");
5820 shader_addline(buffer
, " float l_att;\n");
5821 shader_addline(buffer
, " float q_att;\n");
5822 shader_addline(buffer
, " float cos_htheta;\n");
5823 shader_addline(buffer
, " float cos_hphi;\n");
5824 shader_addline(buffer
, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS
);
5826 if (settings
->point_size
)
5828 shader_addline(buffer
, "uniform struct\n{\n");
5829 shader_addline(buffer
, " float size;\n");
5830 shader_addline(buffer
, " float size_min;\n");
5831 shader_addline(buffer
, " float size_max;\n");
5832 shader_addline(buffer
, " float c_att;\n");
5833 shader_addline(buffer
, " float l_att;\n");
5834 shader_addline(buffer
, " float q_att;\n");
5835 shader_addline(buffer
, "} ffp_point;\n");
5840 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
5841 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
5842 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
5843 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
5847 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_diffuse;\n");
5848 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_specular;\n");
5849 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
5850 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
5853 shader_addline(buffer
, "\nvoid main()\n{\n");
5854 shader_addline(buffer
, "float m;\n");
5855 shader_addline(buffer
, "vec3 r;\n");
5857 for (i
= 0; i
< ARRAY_SIZE(attrib_info
); ++i
)
5859 if (attrib_info
[i
].name
[0])
5860 shader_addline(buffer
, "%s %s = vs_in%u;\n",
5861 attrib_info
[i
].type
, attrib_info
[i
].name
, i
);
5863 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
5865 unsigned int coord_idx
= settings
->texgen
[i
] & 0x0000ffff;
5866 if ((settings
->texgen
[i
] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
)
5868 shader_addline(buffer
, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i
, coord_idx
+ WINED3D_FFP_TEXCOORD0
);
5872 shader_addline(buffer
, "ffp_attrib_blendweight[%u] = 1.0;\n", settings
->vertexblends
);
5874 if (settings
->transformed
)
5876 shader_addline(buffer
, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
5877 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5878 shader_addline(buffer
, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
5882 for (i
= 0; i
< settings
->vertexblends
; ++i
)
5883 shader_addline(buffer
, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings
->vertexblends
, i
);
5885 shader_addline(buffer
, "vec4 ec_pos = vec4(0.0);\n");
5886 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
5887 shader_addline(buffer
, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i
, i
);
5889 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5890 if (settings
->clipping
)
5891 shader_addline(buffer
, "gl_ClipVertex = ec_pos;\n");
5892 shader_addline(buffer
, "ec_pos /= ec_pos.w;\n");
5895 shader_addline(buffer
, "vec3 normal = vec3(0.0);\n");
5896 if (settings
->normal
)
5898 if (!settings
->vertexblends
)
5900 shader_addline(buffer
, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
5904 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
5905 shader_addline(buffer
, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i
, i
);
5908 if (settings
->normalize
)
5909 shader_addline(buffer
, "normal = normalize(normal);\n");
5912 shader_glsl_ffp_vertex_lighting(buffer
, settings
, legacy_lighting
);
5915 shader_addline(buffer
, "gl_FrontColor = ffp_varying_diffuse;\n");
5916 shader_addline(buffer
, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
5919 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
5921 BOOL output_legacy_texcoord
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5923 switch (settings
->texgen
[i
] & 0xffff0000)
5925 case WINED3DTSS_TCI_PASSTHRU
:
5926 if (settings
->texcoords
& (1u << i
))
5927 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
5929 else if (gl_info
->limits
.glsl_varyings
>= wined3d_max_compat_varyings(gl_info
))
5930 shader_addline(buffer
, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i
);
5932 output_legacy_texcoord
= FALSE
;
5935 case WINED3DTSS_TCI_CAMERASPACENORMAL
:
5936 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i
, i
);
5939 case WINED3DTSS_TCI_CAMERASPACEPOSITION
:
5940 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i
, i
);
5943 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
:
5944 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
5945 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i
, i
);
5948 case WINED3DTSS_TCI_SPHEREMAP
:
5949 shader_addline(buffer
, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5950 shader_addline(buffer
, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5951 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
5952 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i
, i
);
5956 ERR("Unhandled texgen %#x.\n", settings
->texgen
[i
]);
5959 if (output_legacy_texcoord
)
5960 shader_addline(buffer
, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i
, i
);
5963 switch (settings
->fog_mode
)
5965 case WINED3D_FFP_VS_FOG_OFF
:
5966 output_legacy_fogcoord
= FALSE
;
5969 case WINED3D_FFP_VS_FOG_FOGCOORD
:
5970 shader_addline(buffer
, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
5973 case WINED3D_FFP_VS_FOG_RANGE
:
5974 shader_addline(buffer
, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
5977 case WINED3D_FFP_VS_FOG_DEPTH
:
5978 if (settings
->ortho_fog
)
5979 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5980 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
5981 else if (settings
->transformed
)
5982 shader_addline(buffer
, "ffp_varying_fogcoord = ec_pos.z;\n");
5984 shader_addline(buffer
, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
5988 ERR("Unhandled fog mode %#x.\n", settings
->fog_mode
);
5991 if (output_legacy_fogcoord
)
5992 shader_addline(buffer
, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
5994 if (settings
->point_size
)
5996 shader_addline(buffer
, "gl_PointSize = %s / sqrt(ffp_point.c_att"
5997 " + ffp_point.l_att * length(ec_pos.xyz)"
5998 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
5999 settings
->per_vertex_point_size
? "ffp_attrib_psize" : "ffp_point.size");
6000 shader_addline(buffer
, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6003 shader_addline(buffer
, "}\n");
6005 shader_obj
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
6006 shader_glsl_compile(gl_info
, shader_obj
, buffer
->buffer
);
6011 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer
*buffer
,
6012 DWORD argnum
, unsigned int stage
, DWORD arg
)
6016 if (arg
== ARG_UNUSED
)
6017 return "<unused arg>";
6019 switch (arg
& WINED3DTA_SELECTMASK
)
6021 case WINED3DTA_DIFFUSE
:
6022 ret
= "ffp_varying_diffuse";
6025 case WINED3DTA_CURRENT
:
6027 ret
= "ffp_varying_diffuse";
6032 case WINED3DTA_TEXTURE
:
6035 case 0: ret
= "tex0"; break;
6036 case 1: ret
= "tex1"; break;
6037 case 2: ret
= "tex2"; break;
6038 case 3: ret
= "tex3"; break;
6039 case 4: ret
= "tex4"; break;
6040 case 5: ret
= "tex5"; break;
6041 case 6: ret
= "tex6"; break;
6042 case 7: ret
= "tex7"; break;
6044 ret
= "<invalid texture>";
6049 case WINED3DTA_TFACTOR
:
6053 case WINED3DTA_SPECULAR
:
6054 ret
= "ffp_varying_specular";
6057 case WINED3DTA_TEMP
:
6061 case WINED3DTA_CONSTANT
:
6064 case 0: ret
= "tss_const0"; break;
6065 case 1: ret
= "tss_const1"; break;
6066 case 2: ret
= "tss_const2"; break;
6067 case 3: ret
= "tss_const3"; break;
6068 case 4: ret
= "tss_const4"; break;
6069 case 5: ret
= "tss_const5"; break;
6070 case 6: ret
= "tss_const6"; break;
6071 case 7: ret
= "tss_const7"; break;
6073 ret
= "<invalid constant>";
6079 return "<unhandled arg>";
6082 if (arg
& WINED3DTA_COMPLEMENT
)
6084 shader_addline(buffer
, "arg%u = vec4(1.0) - %s;\n", argnum
, ret
);
6087 else if (argnum
== 1)
6089 else if (argnum
== 2)
6093 if (arg
& WINED3DTA_ALPHAREPLICATE
)
6095 shader_addline(buffer
, "arg%u = vec4(%s.w);\n", argnum
, ret
);
6098 else if (argnum
== 1)
6100 else if (argnum
== 2)
6107 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer
*buffer
, unsigned int stage
, BOOL color
,
6108 BOOL alpha
, DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
)
6110 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
6120 dstreg
= "temp_reg";
6124 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, dw_arg0
);
6125 arg1
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 1, stage
, dw_arg1
);
6126 arg2
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 2, stage
, dw_arg2
);
6130 case WINED3D_TOP_DISABLE
:
6132 shader_addline(buffer
, "%s%s = ffp_varying_diffuse%s;\n", dstreg
, dstmask
, dstmask
);
6135 case WINED3D_TOP_SELECT_ARG1
:
6136 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
);
6139 case WINED3D_TOP_SELECT_ARG2
:
6140 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg2
, dstmask
);
6143 case WINED3D_TOP_MODULATE
:
6144 shader_addline(buffer
, "%s%s = %s%s * %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6147 case WINED3D_TOP_MODULATE_4X
:
6148 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6149 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6152 case WINED3D_TOP_MODULATE_2X
:
6153 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6154 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6157 case WINED3D_TOP_ADD
:
6158 shader_addline(buffer
, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6159 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6162 case WINED3D_TOP_ADD_SIGNED
:
6163 shader_addline(buffer
, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6164 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6167 case WINED3D_TOP_ADD_SIGNED_2X
:
6168 shader_addline(buffer
, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6169 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6172 case WINED3D_TOP_SUBTRACT
:
6173 shader_addline(buffer
, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6174 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
6177 case WINED3D_TOP_ADD_SMOOTH
:
6178 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6179 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
, dstmask
);
6182 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA
:
6183 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
6184 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6185 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
6188 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
6189 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
6190 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6191 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
6194 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
6195 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
6196 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6197 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
6200 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
6201 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
6202 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6203 dstreg
, dstmask
, arg2
, dstmask
, arg0
, arg1
, dstmask
);
6206 case WINED3D_TOP_BLEND_CURRENT_ALPHA
:
6207 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
6208 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6209 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
6212 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR
:
6213 shader_addline(buffer
, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6214 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
6217 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA
:
6218 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6219 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
6222 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR
:
6223 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6224 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
6226 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA
:
6227 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6228 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
6231 case WINED3D_TOP_BUMPENVMAP
:
6232 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
6233 /* These are handled in the first pass, nothing to do. */
6236 case WINED3D_TOP_DOTPRODUCT3
:
6237 shader_addline(buffer
, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6238 dstreg
, dstmask
, arg1
, arg2
, dstmask
);
6241 case WINED3D_TOP_MULTIPLY_ADD
:
6242 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6243 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg0
, dstmask
);
6246 case WINED3D_TOP_LERP
:
6247 /* MSDN isn't quite right here. */
6248 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6249 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
, dstmask
);
6253 FIXME("Unhandled operation %#x.\n", op
);
6258 /* Context activation is done by the caller. */
6259 static GLuint
shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
6260 const struct ffp_frag_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
6262 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6263 BYTE lum_map
= 0, bump_map
= 0, tex_map
= 0, tss_const_map
= 0;
6264 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
6265 const char *final_combiner_src
= "ret";
6266 UINT lowest_disabled_stage
;
6268 DWORD arg0
, arg1
, arg2
;
6270 struct wined3d_string_buffer
*tex_reg_name
= string_buffer_get(&priv
->string_buffers
);
6271 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6273 string_buffer_clear(buffer
);
6275 /* Find out which textures are read */
6276 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6278 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
6281 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
6282 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
6283 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
6285 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
6286 || (stage
== 0 && settings
->color_key_enabled
))
6287 tex_map
|= 1u << stage
;
6288 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
6289 tfactor_used
= TRUE
;
6290 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
6291 tempreg_used
= TRUE
;
6292 if (settings
->op
[stage
].dst
== tempreg
)
6293 tempreg_used
= TRUE
;
6294 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
6295 tss_const_map
|= 1u << stage
;
6297 switch (settings
->op
[stage
].cop
)
6299 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
6300 lum_map
|= 1u << stage
;
6302 case WINED3D_TOP_BUMPENVMAP
:
6303 bump_map
|= 1u << stage
;
6305 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
6306 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
6307 tex_map
|= 1u << stage
;
6310 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
6311 tfactor_used
= TRUE
;
6318 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
6321 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
6322 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
6323 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
6325 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
)
6326 tex_map
|= 1u << stage
;
6327 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
6328 tfactor_used
= TRUE
;
6329 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
6330 tempreg_used
= TRUE
;
6331 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
6332 tss_const_map
|= 1u << stage
;
6334 lowest_disabled_stage
= stage
;
6336 shader_addline(buffer
, "%s\n", shader_glsl_get_version(gl_info
, NULL
));
6338 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
6339 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
6341 shader_addline(buffer
, "vec4 tmp0, tmp1;\n");
6342 shader_addline(buffer
, "vec4 ret;\n");
6343 if (tempreg_used
|| settings
->sRGB_write
)
6344 shader_addline(buffer
, "vec4 temp_reg = vec4(0.0);\n");
6345 shader_addline(buffer
, "vec4 arg0, arg1, arg2;\n");
6347 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6349 if (tss_const_map
& (1u << stage
))
6350 shader_addline(buffer
, "uniform vec4 tss_const%u;\n", stage
);
6352 if (!(tex_map
& (1u << stage
)))
6355 switch (settings
->op
[stage
].tex_type
)
6357 case WINED3D_GL_RES_TYPE_TEX_1D
:
6358 shader_addline(buffer
, "uniform sampler1D ps_sampler%u;\n", stage
);
6360 case WINED3D_GL_RES_TYPE_TEX_2D
:
6361 shader_addline(buffer
, "uniform sampler2D ps_sampler%u;\n", stage
);
6363 case WINED3D_GL_RES_TYPE_TEX_3D
:
6364 shader_addline(buffer
, "uniform sampler3D ps_sampler%u;\n", stage
);
6366 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
6367 shader_addline(buffer
, "uniform samplerCube ps_sampler%u;\n", stage
);
6369 case WINED3D_GL_RES_TYPE_TEX_RECT
:
6370 shader_addline(buffer
, "uniform sampler2DRect ps_sampler%u;\n", stage
);
6373 FIXME("Unhandled sampler type %#x.\n", settings
->op
[stage
].tex_type
);
6377 shader_addline(buffer
, "vec4 tex%u;\n", stage
);
6379 if (!(bump_map
& (1u << stage
)))
6381 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", stage
);
6383 if (!(lum_map
& (1u << stage
)))
6385 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", stage
);
6386 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", stage
);
6389 shader_addline(buffer
, "uniform vec4 tex_factor;\n");
6390 if (settings
->color_key_enabled
)
6391 shader_addline(buffer
, "uniform vec4 color_key[2];\n");
6392 shader_addline(buffer
, "uniform vec4 specular_enable;\n");
6394 if (settings
->sRGB_write
)
6396 shader_addline(buffer
, "const vec4 srgb_const0 = ");
6397 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
6398 shader_addline(buffer
, ";\n");
6399 shader_addline(buffer
, "const vec4 srgb_const1 = ");
6400 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
6401 shader_addline(buffer
, ";\n");
6404 shader_addline(buffer
, "uniform struct\n{\n");
6405 shader_addline(buffer
, " vec4 color;\n");
6406 shader_addline(buffer
, " float density;\n");
6407 shader_addline(buffer
, " float end;\n");
6408 shader_addline(buffer
, " float scale;\n");
6409 shader_addline(buffer
, "} ffp_fog;\n");
6413 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
6414 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
6415 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
6416 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
6417 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
6421 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_diffuse;\n");
6422 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_specular;\n");
6423 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
6424 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
6425 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
6428 shader_addline(buffer
, "void main()\n{\n");
6432 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
6433 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
6436 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6438 if (tex_map
& (1u << stage
))
6440 if (settings
->pointsprite
)
6441 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage
);
6442 else if (settings
->texcoords_initialized
& (1u << stage
))
6443 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n",
6444 stage
, legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", stage
);
6446 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", stage
);
6450 if (legacy_context
&& settings
->fog
!= WINED3D_FFP_PS_FOG_OFF
)
6451 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6453 if (lowest_disabled_stage
< 7 && settings
->emul_clipplanes
)
6454 shader_addline(buffer
, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6456 /* Generate texture sampling instructions) */
6457 for (stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3D_TOP_DISABLE
; ++stage
)
6459 const char *texture_function
, *coord_mask
;
6462 if (!(tex_map
& (1u << stage
)))
6465 if (settings
->op
[stage
].projected
== proj_none
)
6469 else if (settings
->op
[stage
].projected
== proj_count4
6470 || settings
->op
[stage
].projected
== proj_count3
)
6476 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
6480 if (settings
->op
[stage
].tex_type
== WINED3D_GL_RES_TYPE_TEX_CUBE
)
6483 switch (settings
->op
[stage
].tex_type
)
6485 case WINED3D_GL_RES_TYPE_TEX_1D
:
6488 texture_function
= "texture1DProj";
6493 texture_function
= "texture1D";
6497 case WINED3D_GL_RES_TYPE_TEX_2D
:
6500 texture_function
= "texture2DProj";
6505 texture_function
= "texture2D";
6509 case WINED3D_GL_RES_TYPE_TEX_3D
:
6512 texture_function
= "texture3DProj";
6513 coord_mask
= "xyzw";
6517 texture_function
= "texture3D";
6521 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
6522 texture_function
= "textureCube";
6525 case WINED3D_GL_RES_TYPE_TEX_RECT
:
6528 texture_function
= "texture2DRectProj";
6533 texture_function
= "texture2DRect";
6538 FIXME("Unhandled texture type %#x.\n", settings
->op
[stage
].tex_type
);
6539 texture_function
= "";
6540 coord_mask
= "xyzw";
6543 if (!needs_legacy_glsl_syntax(gl_info
))
6544 texture_function
= proj
? "textureProj" : "texture";
6547 && (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP
6548 || settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
))
6550 shader_addline(buffer
, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage
- 1, stage
- 1);
6552 /* With projective textures, texbem only divides the static
6553 * texture coord, not the displacement, so multiply the
6554 * displacement with the dividing parameter before passing it to
6556 if (settings
->op
[stage
].projected
!= proj_none
)
6558 if (settings
->op
[stage
].projected
== proj_count4
)
6560 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6562 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].ww;\n", stage
);
6566 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6568 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].zz;\n", stage
);
6573 shader_addline(buffer
, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage
);
6576 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6577 stage
, texture_function
, stage
, coord_mask
);
6579 if (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
6580 shader_addline(buffer
, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6581 stage
, stage
- 1, stage
- 1, stage
- 1);
6583 else if (settings
->op
[stage
].projected
== proj_count3
)
6585 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6586 stage
, texture_function
, stage
, stage
);
6590 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6591 stage
, texture_function
, stage
, stage
, coord_mask
);
6594 string_buffer_sprintf(tex_reg_name
, "tex%u", stage
);
6595 shader_glsl_color_correction_ext(buffer
, tex_reg_name
->buffer
, WINED3DSP_WRITEMASK_ALL
,
6596 settings
->op
[stage
].color_fixup
);
6599 if (settings
->color_key_enabled
)
6601 shader_addline(buffer
, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6602 shader_addline(buffer
, " discard;\n");
6605 /* Generate the main shader */
6606 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
6610 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
6613 final_combiner_src
= "ffp_varying_diffuse";
6617 if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
6618 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
6619 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
6620 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
6621 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
6622 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
6623 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
6624 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
6625 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
6626 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
6627 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
6628 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6630 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
6631 && settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
6632 && settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
6633 && settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
6635 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
6637 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6638 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6639 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6641 shader_addline(buffer
, "ret.w = ffp_varying_diffuse.w;\n");
6645 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
6646 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6647 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6651 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
6652 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
6653 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
6654 shader_glsl_ffp_fragment_op(buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
6655 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
6656 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
6660 shader_addline(buffer
, "gl_FragData[0] = ffp_varying_specular * specular_enable + %s;\n", final_combiner_src
);
6662 if (settings
->sRGB_write
)
6663 shader_glsl_generate_srgb_write_correction(buffer
);
6665 shader_glsl_generate_fog_code(buffer
, settings
->fog
);
6667 shader_addline(buffer
, "}\n");
6669 shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
6670 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6672 string_buffer_release(&priv
->string_buffers
, tex_reg_name
);
6676 static struct glsl_ffp_vertex_shader
*shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
6677 const struct wined3d_gl_info
*gl_info
, const struct wined3d_ffp_vs_settings
*settings
)
6679 struct glsl_ffp_vertex_shader
*shader
;
6680 const struct wine_rb_entry
*entry
;
6682 if ((entry
= wine_rb_get(&priv
->ffp_vertex_shaders
, settings
)))
6683 return WINE_RB_ENTRY_VALUE(entry
, struct glsl_ffp_vertex_shader
, desc
.entry
);
6685 if (!(shader
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader
))))
6688 shader
->desc
.settings
= *settings
;
6689 shader
->id
= shader_glsl_generate_ffp_vertex_shader(priv
, settings
, gl_info
);
6690 list_init(&shader
->linked_programs
);
6691 if (wine_rb_put(&priv
->ffp_vertex_shaders
, &shader
->desc
.settings
, &shader
->desc
.entry
) == -1)
6692 ERR("Failed to insert ffp vertex shader.\n");
6697 static struct glsl_ffp_fragment_shader
*shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
6698 const struct wined3d_gl_info
*gl_info
, const struct ffp_frag_settings
*args
)
6700 struct glsl_ffp_fragment_shader
*glsl_desc
;
6701 const struct ffp_frag_desc
*desc
;
6703 if ((desc
= find_ffp_frag_shader(&priv
->ffp_fragment_shaders
, args
)))
6704 return CONTAINING_RECORD(desc
, struct glsl_ffp_fragment_shader
, entry
);
6706 if (!(glsl_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc
))))
6709 glsl_desc
->entry
.settings
= *args
;
6710 glsl_desc
->id
= shader_glsl_generate_ffp_fragment_shader(priv
, args
, gl_info
);
6711 list_init(&glsl_desc
->linked_programs
);
6712 add_ffp_frag_shader(&priv
->ffp_fragment_shaders
, &glsl_desc
->entry
);
6718 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
6719 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_vs_program
*vs
, unsigned int vs_c_count
)
6722 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
6724 vs
->uniform_f_locations
= HeapAlloc(GetProcessHeap(), 0,
6725 sizeof(GLuint
) * gl_info
->limits
.glsl_vs_float_constants
);
6726 for (i
= 0; i
< vs_c_count
; ++i
)
6728 string_buffer_sprintf(name
, "vs_c[%u]", i
);
6729 vs
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6731 memset(&vs
->uniform_f_locations
[vs_c_count
], 0xff,
6732 (gl_info
->limits
.glsl_vs_float_constants
- vs_c_count
) * sizeof(GLuint
));
6734 for (i
= 0; i
< MAX_CONST_I
; ++i
)
6736 string_buffer_sprintf(name
, "vs_i[%u]", i
);
6737 vs
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6740 for (i
= 0; i
< MAX_CONST_B
; ++i
)
6742 string_buffer_sprintf(name
, "vs_b[%u]", i
);
6743 vs
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6746 vs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "posFixup"));
6748 for (i
= 0; i
< MAX_VERTEX_BLENDS
; ++i
)
6750 string_buffer_sprintf(name
, "ffp_modelview_matrix[%u]", i
);
6751 vs
->modelview_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6753 vs
->projection_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_projection_matrix"));
6754 vs
->normal_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_normal_matrix"));
6755 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6757 string_buffer_sprintf(name
, "ffp_texture_matrix[%u]", i
);
6758 vs
->texture_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6760 vs
->material_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.ambient"));
6761 vs
->material_diffuse_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.diffuse"));
6762 vs
->material_specular_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.specular"));
6763 vs
->material_emissive_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.emissive"));
6764 vs
->material_shininess_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.shininess"));
6765 vs
->light_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_light_ambient"));
6766 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
6768 string_buffer_sprintf(name
, "ffp_light[%u].diffuse", i
);
6769 vs
->light_location
[i
].diffuse
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6770 string_buffer_sprintf(name
, "ffp_light[%u].specular", i
);
6771 vs
->light_location
[i
].specular
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6772 string_buffer_sprintf(name
, "ffp_light[%u].ambient", i
);
6773 vs
->light_location
[i
].ambient
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6774 string_buffer_sprintf(name
, "ffp_light[%u].position", i
);
6775 vs
->light_location
[i
].position
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6776 string_buffer_sprintf(name
, "ffp_light[%u].direction", i
);
6777 vs
->light_location
[i
].direction
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6778 string_buffer_sprintf(name
, "ffp_light[%u].range", i
);
6779 vs
->light_location
[i
].range
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6780 string_buffer_sprintf(name
, "ffp_light[%u].falloff", i
);
6781 vs
->light_location
[i
].falloff
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6782 string_buffer_sprintf(name
, "ffp_light[%u].c_att", i
);
6783 vs
->light_location
[i
].c_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6784 string_buffer_sprintf(name
, "ffp_light[%u].l_att", i
);
6785 vs
->light_location
[i
].l_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6786 string_buffer_sprintf(name
, "ffp_light[%u].q_att", i
);
6787 vs
->light_location
[i
].q_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6788 string_buffer_sprintf(name
, "ffp_light[%u].cos_htheta", i
);
6789 vs
->light_location
[i
].cos_htheta
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6790 string_buffer_sprintf(name
, "ffp_light[%u].cos_hphi", i
);
6791 vs
->light_location
[i
].cos_hphi
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6793 vs
->pointsize_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size"));
6794 vs
->pointsize_min_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_min"));
6795 vs
->pointsize_max_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_max"));
6796 vs
->pointsize_c_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.c_att"));
6797 vs
->pointsize_l_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.l_att"));
6798 vs
->pointsize_q_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.q_att"));
6800 string_buffer_release(&priv
->string_buffers
, name
);
6803 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info
*gl_info
,
6804 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_ps_program
*ps
, unsigned int ps_c_count
)
6807 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
6809 ps
->uniform_f_locations
= HeapAlloc(GetProcessHeap(), 0,
6810 sizeof(GLuint
) * gl_info
->limits
.glsl_ps_float_constants
);
6811 for (i
= 0; i
< ps_c_count
; ++i
)
6813 string_buffer_sprintf(name
, "ps_c[%u]", i
);
6814 ps
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6816 memset(&ps
->uniform_f_locations
[ps_c_count
], 0xff,
6817 (gl_info
->limits
.glsl_ps_float_constants
- ps_c_count
) * sizeof(GLuint
));
6819 for (i
= 0; i
< MAX_CONST_I
; ++i
)
6821 string_buffer_sprintf(name
, "ps_i[%u]", i
);
6822 ps
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6825 for (i
= 0; i
< MAX_CONST_B
; ++i
)
6827 string_buffer_sprintf(name
, "ps_b[%u]", i
);
6828 ps
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6831 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6833 string_buffer_sprintf(name
, "bumpenv_mat%u", i
);
6834 ps
->bumpenv_mat_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6835 string_buffer_sprintf(name
, "bumpenv_lum_scale%u", i
);
6836 ps
->bumpenv_lum_scale_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6837 string_buffer_sprintf(name
, "bumpenv_lum_offset%u", i
);
6838 ps
->bumpenv_lum_offset_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6839 string_buffer_sprintf(name
, "tss_const%u", i
);
6840 ps
->tss_constant_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
6843 ps
->tex_factor_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "tex_factor"));
6844 ps
->specular_enable_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "specular_enable"));
6846 ps
->fog_color_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.color"));
6847 ps
->fog_density_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.density"));
6848 ps
->fog_end_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.end"));
6849 ps
->fog_scale_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.scale"));
6851 ps
->np2_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ps_samplerNP2Fixup"));
6852 ps
->ycorrection_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ycorrection"));
6853 ps
->color_key_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "color_key"));
6855 string_buffer_release(&priv
->string_buffers
, name
);
6858 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info
*gl_info
,
6859 struct shader_glsl_priv
*priv
, GLuint program_id
,
6860 const struct wined3d_shader_reg_maps
*reg_maps
, unsigned int base
, unsigned int count
)
6862 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
6865 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
6867 for (i
= 0; i
< count
; ++i
)
6869 if (!reg_maps
->cb_sizes
[i
])
6872 string_buffer_sprintf(name
, "block_%s_cb%u", prefix
, i
);
6873 block_idx
= GL_EXTCALL(glGetUniformBlockIndex(program_id
, name
->buffer
));
6874 GL_EXTCALL(glUniformBlockBinding(program_id
, block_idx
, base
+ i
));
6876 checkGLcall("glUniformBlockBinding");
6877 string_buffer_release(&priv
->string_buffers
, name
);
6880 /* Context activation is done by the caller. */
6881 static void set_glsl_shader_program(const struct wined3d_context
*context
, const struct wined3d_state
*state
,
6882 struct shader_glsl_priv
*priv
, struct glsl_context_data
*ctx_data
)
6884 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6885 const struct ps_np2fixup_info
*np2fixup_info
= NULL
;
6886 struct glsl_shader_prog_link
*entry
= NULL
;
6887 struct wined3d_shader
*vshader
= NULL
;
6888 struct wined3d_shader
*gshader
= NULL
;
6889 struct wined3d_shader
*pshader
= NULL
;
6890 GLuint program_id
= 0;
6891 GLuint reorder_shader_id
= 0;
6896 struct list
*ps_list
, *vs_list
;
6898 struct wined3d_string_buffer
*tmp_name
;
6900 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_VERTEX
)) && ctx_data
->glsl_program
)
6902 vs_id
= ctx_data
->glsl_program
->vs
.id
;
6903 vs_list
= &ctx_data
->glsl_program
->vs
.shader_entry
;
6907 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
6908 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
6910 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_GEOMETRY
))
6911 && ctx_data
->glsl_program
->gs
.id
)
6912 gs_id
= ctx_data
->glsl_program
->gs
.id
;
6914 gs_id
= find_glsl_geometry_shader(context
, &priv
->shader_buffer
, &priv
->string_buffers
, gshader
);
6917 else if (use_vs(state
))
6919 struct vs_compile_args vs_compile_args
;
6920 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
6922 find_vs_compile_args(state
, vshader
, context
->stream_info
.swizzle_map
, &vs_compile_args
);
6923 vs_id
= find_glsl_vshader(context
, &priv
->shader_buffer
, &priv
->string_buffers
, vshader
, &vs_compile_args
);
6924 vs_list
= &vshader
->linked_programs
;
6926 if ((gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
]))
6927 gs_id
= find_glsl_geometry_shader(context
, &priv
->shader_buffer
, &priv
->string_buffers
, gshader
);
6929 else if (priv
->vertex_pipe
== &glsl_vertex_pipe
)
6931 struct glsl_ffp_vertex_shader
*ffp_shader
;
6932 struct wined3d_ffp_vs_settings settings
;
6934 wined3d_ffp_get_vs_settings(context
, state
, &settings
);
6935 ffp_shader
= shader_glsl_find_ffp_vertex_shader(priv
, gl_info
, &settings
);
6936 vs_id
= ffp_shader
->id
;
6937 vs_list
= &ffp_shader
->linked_programs
;
6940 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_PIXEL
)) && ctx_data
->glsl_program
)
6942 ps_id
= ctx_data
->glsl_program
->ps
.id
;
6943 ps_list
= &ctx_data
->glsl_program
->ps
.shader_entry
;
6946 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
6948 else if (use_ps(state
))
6950 struct ps_compile_args ps_compile_args
;
6951 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
6952 find_ps_compile_args(state
, pshader
, context
->stream_info
.position_transformed
, &ps_compile_args
, context
);
6953 ps_id
= find_glsl_pshader(context
, &priv
->shader_buffer
, &priv
->string_buffers
,
6954 pshader
, &ps_compile_args
, &np2fixup_info
);
6955 ps_list
= &pshader
->linked_programs
;
6957 else if (priv
->fragment_pipe
== &glsl_fragment_pipe
)
6959 struct glsl_ffp_fragment_shader
*ffp_shader
;
6960 struct ffp_frag_settings settings
;
6962 gen_ffp_frag_op(context
, state
, &settings
, FALSE
);
6963 ffp_shader
= shader_glsl_find_ffp_fragment_shader(priv
, gl_info
, &settings
);
6964 ps_id
= ffp_shader
->id
;
6965 ps_list
= &ffp_shader
->linked_programs
;
6968 if ((!vs_id
&& !gs_id
&& !ps_id
) || (entry
= get_glsl_program_entry(priv
, vs_id
, gs_id
, ps_id
)))
6970 ctx_data
->glsl_program
= entry
;
6974 /* If we get to this point, then no matching program exists, so we create one */
6975 program_id
= GL_EXTCALL(glCreateProgram());
6976 TRACE("Created new GLSL shader program %u.\n", program_id
);
6978 /* Create the entry */
6979 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
6980 entry
->id
= program_id
;
6981 entry
->vs
.id
= vs_id
;
6982 entry
->gs
.id
= gs_id
;
6983 entry
->ps
.id
= ps_id
;
6984 entry
->constant_version
= 0;
6985 entry
->ps
.np2_fixup_info
= np2fixup_info
;
6986 /* Add the hash table entry */
6987 add_glsl_program_entry(priv
, entry
);
6989 /* Set the current program */
6990 ctx_data
->glsl_program
= entry
;
6992 /* Attach GLSL vshader */
6995 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id
, program_id
);
6996 GL_EXTCALL(glAttachShader(program_id
, vs_id
));
6997 checkGLcall("glAttachShader");
6999 list_add_head(vs_list
, &entry
->vs
.shader_entry
);
7004 attribs_map
= vshader
->reg_maps
.input_registers
;
7005 reorder_shader_id
= generate_param_reorder_function(priv
, vshader
, pshader
,
7006 state
->gl_primitive_type
== GL_POINTS
&& vshader
->reg_maps
.point_size
, gl_info
);
7007 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id
, program_id
);
7008 GL_EXTCALL(glAttachShader(program_id
, reorder_shader_id
));
7009 checkGLcall("glAttachShader");
7010 /* Flag the reorder function for deletion, then it will be freed automatically when the program
7013 GL_EXTCALL(glDeleteShader(reorder_shader_id
));
7017 attribs_map
= (1u << WINED3D_FFP_ATTRIBS_COUNT
) - 1;
7020 /* Bind vertex attributes to a corresponding index number to match
7021 * the same index numbers as ARB_vertex_programs (makes loading
7022 * vertex attributes simpler). With this method, we can use the
7023 * exact same code to load the attributes later for both ARB and
7026 * We have to do this here because we need to know the Program ID
7027 * in order to make the bindings work, and it has to be done prior
7028 * to linking the GLSL program. */
7029 tmp_name
= string_buffer_get(&priv
->string_buffers
);
7030 for (i
= 0; attribs_map
; attribs_map
>>= 1, ++i
)
7032 if (!(attribs_map
& 1))
7035 string_buffer_sprintf(tmp_name
, "vs_in%u", i
);
7036 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
7038 checkGLcall("glBindAttribLocation");
7039 string_buffer_release(&priv
->string_buffers
, tmp_name
);
7043 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id
, program_id
);
7044 GL_EXTCALL(glAttachShader(program_id
, gs_id
));
7045 checkGLcall("glAttachShader");
7047 TRACE("input type %s, output type %s, vertices out %u.\n",
7048 debug_d3dprimitivetype(gshader
->u
.gs
.input_type
),
7049 debug_d3dprimitivetype(gshader
->u
.gs
.output_type
),
7050 gshader
->u
.gs
.vertices_out
);
7051 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_INPUT_TYPE_ARB
,
7052 gl_primitive_type_from_d3d(gshader
->u
.gs
.input_type
)));
7053 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_OUTPUT_TYPE_ARB
,
7054 gl_primitive_type_from_d3d(gshader
->u
.gs
.output_type
)));
7055 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_VERTICES_OUT_ARB
,
7056 gshader
->u
.gs
.vertices_out
));
7057 checkGLcall("glProgramParameteriARB");
7059 list_add_head(&gshader
->linked_programs
, &entry
->gs
.shader_entry
);
7062 /* Attach GLSL pshader */
7065 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id
, program_id
);
7066 GL_EXTCALL(glAttachShader(program_id
, ps_id
));
7067 checkGLcall("glAttachShader");
7069 list_add_head(ps_list
, &entry
->ps
.shader_entry
);
7072 /* Link the program */
7073 TRACE("Linking GLSL shader program %u.\n", program_id
);
7074 GL_EXTCALL(glLinkProgram(program_id
));
7075 shader_glsl_validate_link(gl_info
, program_id
);
7077 shader_glsl_init_vs_uniform_locations(gl_info
, priv
, program_id
, &entry
->vs
,
7078 vshader
? min(vshader
->limits
->constant_float
, gl_info
->limits
.glsl_vs_float_constants
) : 0);
7079 shader_glsl_init_ps_uniform_locations(gl_info
, priv
, program_id
, &entry
->ps
,
7080 pshader
? min(pshader
->limits
->constant_float
, gl_info
->limits
.glsl_ps_float_constants
) : 0);
7081 checkGLcall("Find glsl program uniform locations");
7083 if (pshader
&& pshader
->reg_maps
.shader_version
.major
>= 3
7084 && pshader
->u
.ps
.declared_in_count
> vec4_varyings(3, gl_info
))
7086 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id
);
7087 entry
->vs
.vertex_color_clamp
= GL_FALSE
;
7091 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
7094 /* Set the shader to allow uniform loading on it */
7095 GL_EXTCALL(glUseProgram(program_id
));
7096 checkGLcall("glUseProgram");
7098 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
7099 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
7100 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
7101 * vertex shader with fixed function pixel processing is used we make sure that the card
7102 * supports enough samplers to allow the max number of vertex samplers with all possible
7103 * fixed function fragment processing setups. So once the program is linked these samplers
7105 shader_glsl_load_samplers(gl_info
, priv
, context
->tex_unit_map
, program_id
);
7107 entry
->constant_update_mask
= 0;
7110 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
7111 if (vshader
->reg_maps
.integer_constants
)
7112 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_I
;
7113 if (vshader
->reg_maps
.boolean_constants
)
7114 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_B
;
7115 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POS_FIXUP
;
7117 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &vshader
->reg_maps
,
7118 0, gl_info
->limits
.vertex_uniform_blocks
);
7122 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
7123 | WINED3D_SHADER_CONST_FFP_PROJ
;
7125 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
7127 if (entry
->vs
.modelview_matrix_location
[i
] != -1)
7129 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
7134 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7136 if (entry
->vs
.texture_matrix_location
[i
] != -1)
7138 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
7142 if (entry
->vs
.material_ambient_location
!= -1 || entry
->vs
.material_diffuse_location
!= -1
7143 || entry
->vs
.material_specular_location
!= -1
7144 || entry
->vs
.material_emissive_location
!= -1
7145 || entry
->vs
.material_shininess_location
!= -1)
7146 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
7147 if (entry
->vs
.light_ambient_location
!= -1)
7148 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
7150 if (entry
->vs
.pointsize_min_location
!= -1)
7151 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
7154 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &gshader
->reg_maps
,
7155 gl_info
->limits
.vertex_uniform_blocks
, gl_info
->limits
.geometry_uniform_blocks
);
7161 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
7162 if (pshader
->reg_maps
.integer_constants
)
7163 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_I
;
7164 if (pshader
->reg_maps
.boolean_constants
)
7165 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_B
;
7166 if (entry
->ps
.ycorrection_location
!= -1)
7167 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
7169 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &pshader
->reg_maps
,
7170 gl_info
->limits
.vertex_uniform_blocks
+ gl_info
->limits
.geometry_uniform_blocks
,
7171 gl_info
->limits
.fragment_uniform_blocks
);
7175 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
7178 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7180 if (entry
->ps
.bumpenv_mat_location
[i
] != -1)
7182 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_BUMP_ENV
;
7187 if (entry
->ps
.fog_color_location
!= -1)
7188 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
7189 if (entry
->ps
.np2_fixup_location
!= -1)
7190 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_NP2_FIXUP
;
7191 if (entry
->ps
.color_key_location
!= -1)
7192 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
7196 /* Context activation is done by the caller. */
7197 static GLuint
create_glsl_blt_shader(const struct wined3d_gl_info
*gl_info
, enum wined3d_gl_resource_type tex_type
,
7201 GLuint vshader_id
, pshader_id
;
7202 const char *blt_pshader
;
7204 static const char blt_vshader
[] =
7208 " gl_Position = gl_Vertex;\n"
7209 " gl_FrontColor = vec4(1.0);\n"
7210 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7213 static const char * const blt_pshaders_full
[WINED3D_GL_RES_TYPE_COUNT
] =
7215 /* WINED3D_GL_RES_TYPE_TEX_1D */
7217 /* WINED3D_GL_RES_TYPE_TEX_2D */
7219 "uniform sampler2D sampler;\n"
7222 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7224 /* WINED3D_GL_RES_TYPE_TEX_3D */
7226 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7228 "uniform samplerCube sampler;\n"
7231 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7233 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7235 "#extension GL_ARB_texture_rectangle : enable\n"
7236 "uniform sampler2DRect sampler;\n"
7239 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7241 /* WINED3D_GL_RES_TYPE_BUFFER */
7243 /* WINED3D_GL_RES_TYPE_RB */
7247 static const char * const blt_pshaders_masked
[WINED3D_GL_RES_TYPE_COUNT
] =
7249 /* WINED3D_GL_RES_TYPE_TEX_1D */
7251 /* WINED3D_GL_RES_TYPE_TEX_2D */
7253 "uniform sampler2D sampler;\n"
7254 "uniform vec4 mask;\n"
7257 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7258 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7260 /* WINED3D_GL_RES_TYPE_TEX_3D */
7262 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7264 "uniform samplerCube sampler;\n"
7265 "uniform vec4 mask;\n"
7268 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7269 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7271 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7273 "#extension GL_ARB_texture_rectangle : enable\n"
7274 "uniform sampler2DRect sampler;\n"
7275 "uniform vec4 mask;\n"
7278 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7279 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7281 /* WINED3D_GL_RES_TYPE_BUFFER */
7283 /* WINED3D_GL_RES_TYPE_RB */
7287 blt_pshader
= masked
? blt_pshaders_masked
[tex_type
] : blt_pshaders_full
[tex_type
];
7290 FIXME("tex_type %#x not supported\n", tex_type
);
7294 vshader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
7295 shader_glsl_compile(gl_info
, vshader_id
, blt_vshader
);
7297 pshader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
7298 shader_glsl_compile(gl_info
, pshader_id
, blt_pshader
);
7300 program_id
= GL_EXTCALL(glCreateProgram());
7301 GL_EXTCALL(glAttachShader(program_id
, vshader_id
));
7302 GL_EXTCALL(glAttachShader(program_id
, pshader_id
));
7303 GL_EXTCALL(glLinkProgram(program_id
));
7305 shader_glsl_validate_link(gl_info
, program_id
);
7307 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7310 GL_EXTCALL(glDeleteShader(vshader_id
));
7311 GL_EXTCALL(glDeleteShader(pshader_id
));
7315 /* Context activation is done by the caller. */
7316 static void shader_glsl_select(void *shader_priv
, struct wined3d_context
*context
,
7317 const struct wined3d_state
*state
)
7319 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
7320 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7321 struct shader_glsl_priv
*priv
= shader_priv
;
7322 GLuint program_id
= 0, prev_id
= 0;
7323 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
7325 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
7326 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
7328 if (ctx_data
->glsl_program
)
7330 prev_id
= ctx_data
->glsl_program
->id
;
7331 old_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
7336 old_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
7339 set_glsl_shader_program(context
, state
, priv
, ctx_data
);
7341 if (ctx_data
->glsl_program
)
7343 program_id
= ctx_data
->glsl_program
->id
;
7344 current_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
7349 current_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
7352 if (old_vertex_color_clamp
!= current_vertex_color_clamp
)
7354 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
7356 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
7357 checkGLcall("glClampColorARB");
7361 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7365 TRACE("Using GLSL program %u.\n", program_id
);
7367 if (prev_id
!= program_id
)
7369 GL_EXTCALL(glUseProgram(program_id
));
7370 checkGLcall("glUseProgram");
7373 context
->constant_update_mask
|= ctx_data
->glsl_program
->constant_update_mask
;
7377 /* "context" is not necessarily the currently active context. */
7378 static void shader_glsl_invalidate_current_program(struct wined3d_context
*context
)
7380 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
7382 ctx_data
->glsl_program
= NULL
;
7383 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
7384 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
7385 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
);
7388 /* Context activation is done by the caller. */
7389 static void shader_glsl_disable(void *shader_priv
, struct wined3d_context
*context
)
7391 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7392 struct shader_glsl_priv
*priv
= shader_priv
;
7394 shader_glsl_invalidate_current_program(context
);
7395 GL_EXTCALL(glUseProgram(0));
7396 checkGLcall("glUseProgram");
7398 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
7399 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
7401 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
7403 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, GL_FIXED_ONLY_ARB
));
7404 checkGLcall("glClampColorARB");
7408 /* Context activation is done by the caller. */
7409 static void shader_glsl_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
7410 enum wined3d_gl_resource_type tex_type
, const SIZE
*ds_mask_size
)
7412 BOOL masked
= ds_mask_size
->cx
&& ds_mask_size
->cy
;
7413 struct shader_glsl_priv
*priv
= shader_priv
;
7414 GLuint
*blt_program
;
7417 blt_program
= masked
? &priv
->depth_blt_program_masked
[tex_type
] : &priv
->depth_blt_program_full
[tex_type
];
7420 *blt_program
= create_glsl_blt_shader(gl_info
, tex_type
, masked
);
7421 loc
= GL_EXTCALL(glGetUniformLocation(*blt_program
, "sampler"));
7422 GL_EXTCALL(glUseProgram(*blt_program
));
7423 GL_EXTCALL(glUniform1i(loc
, 0));
7427 GL_EXTCALL(glUseProgram(*blt_program
));
7432 loc
= GL_EXTCALL(glGetUniformLocation(*blt_program
, "mask"));
7433 GL_EXTCALL(glUniform4f(loc
, 0.0f
, 0.0f
, (float)ds_mask_size
->cx
, (float)ds_mask_size
->cy
));
7437 /* Context activation is done by the caller. */
7438 static void shader_glsl_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
)
7440 const struct glsl_context_data
*ctx_data
= context_get_current()->shader_backend_data
;
7443 program_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
7444 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
7446 GL_EXTCALL(glUseProgram(program_id
));
7447 checkGLcall("glUseProgram");
7450 static void shader_glsl_invalidate_contexts_program(struct wined3d_device
*device
,
7451 const struct glsl_shader_prog_link
*program
)
7453 const struct glsl_context_data
*ctx_data
;
7454 struct wined3d_context
*context
;
7457 for (i
= 0; i
< device
->context_count
; ++i
)
7459 context
= device
->contexts
[i
];
7460 ctx_data
= context
->shader_backend_data
;
7462 if (ctx_data
->glsl_program
== program
)
7463 shader_glsl_invalidate_current_program(context
);
7467 static void shader_glsl_destroy(struct wined3d_shader
*shader
)
7469 struct glsl_shader_private
*shader_data
= shader
->backend_data
;
7470 struct wined3d_device
*device
= shader
->device
;
7471 struct shader_glsl_priv
*priv
= device
->shader_priv
;
7472 const struct wined3d_gl_info
*gl_info
;
7473 const struct list
*linked_programs
;
7474 struct wined3d_context
*context
;
7476 if (!shader_data
|| !shader_data
->num_gl_shaders
)
7478 HeapFree(GetProcessHeap(), 0, shader_data
);
7479 shader
->backend_data
= NULL
;
7483 context
= context_acquire(device
, NULL
);
7484 gl_info
= context
->gl_info
;
7486 TRACE("Deleting linked programs.\n");
7487 linked_programs
= &shader
->linked_programs
;
7488 if (linked_programs
->next
)
7490 struct glsl_shader_prog_link
*entry
, *entry2
;
7493 switch (shader
->reg_maps
.shader_version
.type
)
7495 case WINED3D_SHADER_TYPE_PIXEL
:
7497 struct glsl_ps_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.ps
;
7499 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
7501 TRACE("Deleting pixel shader %u.\n", gl_shaders
[i
].id
);
7502 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
7503 checkGLcall("glDeleteShader");
7505 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
);
7507 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
7508 struct glsl_shader_prog_link
, ps
.shader_entry
)
7510 shader_glsl_invalidate_contexts_program(device
, entry
);
7511 delete_glsl_program_entry(priv
, gl_info
, entry
);
7517 case WINED3D_SHADER_TYPE_VERTEX
:
7519 struct glsl_vs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.vs
;
7521 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
7523 TRACE("Deleting vertex shader %u.\n", gl_shaders
[i
].id
);
7524 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
7525 checkGLcall("glDeleteShader");
7527 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
);
7529 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
7530 struct glsl_shader_prog_link
, vs
.shader_entry
)
7532 shader_glsl_invalidate_contexts_program(device
, entry
);
7533 delete_glsl_program_entry(priv
, gl_info
, entry
);
7539 case WINED3D_SHADER_TYPE_GEOMETRY
:
7541 struct glsl_gs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.gs
;
7543 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
7545 TRACE("Deleting geometry shader %u.\n", gl_shaders
[i
].id
);
7546 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
7547 checkGLcall("glDeleteShader");
7549 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
);
7551 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
7552 struct glsl_shader_prog_link
, gs
.shader_entry
)
7554 shader_glsl_invalidate_contexts_program(device
, entry
);
7555 delete_glsl_program_entry(priv
, gl_info
, entry
);
7562 ERR("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
7567 HeapFree(GetProcessHeap(), 0, shader
->backend_data
);
7568 shader
->backend_data
= NULL
;
7570 context_release(context
);
7573 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
7575 const struct glsl_program_key
*k
= key
;
7576 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
7577 const struct glsl_shader_prog_link
, program_lookup_entry
);
7579 if (k
->vs_id
> prog
->vs
.id
) return 1;
7580 else if (k
->vs_id
< prog
->vs
.id
) return -1;
7582 if (k
->gs_id
> prog
->gs
.id
) return 1;
7583 else if (k
->gs_id
< prog
->gs
.id
) return -1;
7585 if (k
->ps_id
> prog
->ps
.id
) return 1;
7586 else if (k
->ps_id
< prog
->ps
.id
) return -1;
7591 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
7593 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
)
7594 + constant_count
* sizeof(*heap
->contained
)
7595 + constant_count
* sizeof(*heap
->positions
);
7596 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
7600 ERR("Failed to allocate memory\n");
7604 heap
->entries
= mem
;
7605 heap
->entries
[1].version
= 0;
7606 heap
->contained
= (BOOL
*)(heap
->entries
+ constant_count
+ 1);
7607 memset(heap
->contained
, 0, constant_count
* sizeof(*heap
->contained
));
7608 heap
->positions
= (unsigned int *)(heap
->contained
+ constant_count
);
7614 static void constant_heap_free(struct constant_heap
*heap
)
7616 HeapFree(GetProcessHeap(), 0, heap
->entries
);
7619 static const struct wine_rb_functions wined3d_glsl_program_rb_functions
=
7624 glsl_program_key_compare
,
7627 static HRESULT
shader_glsl_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
7628 const struct fragment_pipeline
*fragment_pipe
)
7630 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
7631 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
7632 SIZE_T stack_size
= wined3d_log2i(max(gl_info
->limits
.glsl_vs_float_constants
,
7633 gl_info
->limits
.glsl_ps_float_constants
)) + 1;
7634 struct fragment_caps fragment_caps
;
7635 void *vertex_priv
, *fragment_priv
;
7637 string_buffer_list_init(&priv
->string_buffers
);
7639 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&glsl_shader_backend
, priv
)))
7641 ERR("Failed to initialize vertex pipe.\n");
7642 HeapFree(GetProcessHeap(), 0, priv
);
7646 if (!(fragment_priv
= fragment_pipe
->alloc_private(&glsl_shader_backend
, priv
)))
7648 ERR("Failed to initialize fragment pipe.\n");
7649 vertex_pipe
->vp_free(device
);
7650 HeapFree(GetProcessHeap(), 0, priv
);
7654 if (!string_buffer_init(&priv
->shader_buffer
))
7656 ERR("Failed to initialize shader buffer.\n");
7660 priv
->stack
= HeapAlloc(GetProcessHeap(), 0, stack_size
* sizeof(*priv
->stack
));
7663 ERR("Failed to allocate memory.\n");
7667 if (!constant_heap_init(&priv
->vconst_heap
, gl_info
->limits
.glsl_vs_float_constants
))
7669 ERR("Failed to initialize vertex shader constant heap\n");
7673 if (!constant_heap_init(&priv
->pconst_heap
, gl_info
->limits
.glsl_ps_float_constants
))
7675 ERR("Failed to initialize pixel shader constant heap\n");
7679 if (wine_rb_init(&priv
->program_lookup
, &wined3d_glsl_program_rb_functions
) == -1)
7681 ERR("Failed to initialize rbtree.\n");
7685 priv
->next_constant_version
= 1;
7686 priv
->vertex_pipe
= vertex_pipe
;
7687 priv
->fragment_pipe
= fragment_pipe
;
7688 fragment_pipe
->get_caps(gl_info
, &fragment_caps
);
7689 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
7690 priv
->legacy_lighting
= device
->wined3d
->flags
& WINED3D_LEGACY_FFP_LIGHTING
;
7692 device
->vertex_priv
= vertex_priv
;
7693 device
->fragment_priv
= fragment_priv
;
7694 device
->shader_priv
= priv
;
7699 constant_heap_free(&priv
->pconst_heap
);
7700 constant_heap_free(&priv
->vconst_heap
);
7701 HeapFree(GetProcessHeap(), 0, priv
->stack
);
7702 string_buffer_free(&priv
->shader_buffer
);
7703 fragment_pipe
->free_private(device
);
7704 vertex_pipe
->vp_free(device
);
7705 HeapFree(GetProcessHeap(), 0, priv
);
7706 return E_OUTOFMEMORY
;
7709 /* Context activation is done by the caller. */
7710 static void shader_glsl_free(struct wined3d_device
*device
)
7712 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
7713 struct shader_glsl_priv
*priv
= device
->shader_priv
;
7716 for (i
= 0; i
< WINED3D_GL_RES_TYPE_COUNT
; ++i
)
7718 if (priv
->depth_blt_program_full
[i
])
7720 GL_EXTCALL(glDeleteProgram(priv
->depth_blt_program_full
[i
]));
7722 if (priv
->depth_blt_program_masked
[i
])
7724 GL_EXTCALL(glDeleteProgram(priv
->depth_blt_program_masked
[i
]));
7728 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
7729 constant_heap_free(&priv
->pconst_heap
);
7730 constant_heap_free(&priv
->vconst_heap
);
7731 HeapFree(GetProcessHeap(), 0, priv
->stack
);
7732 string_buffer_list_cleanup(&priv
->string_buffers
);
7733 string_buffer_free(&priv
->shader_buffer
);
7734 priv
->fragment_pipe
->free_private(device
);
7735 priv
->vertex_pipe
->vp_free(device
);
7737 HeapFree(GetProcessHeap(), 0, device
->shader_priv
);
7738 device
->shader_priv
= NULL
;
7741 static BOOL
shader_glsl_allocate_context_data(struct wined3d_context
*context
)
7743 return !!(context
->shader_backend_data
= HeapAlloc(GetProcessHeap(),
7744 HEAP_ZERO_MEMORY
, sizeof(struct glsl_context_data
)));
7747 static void shader_glsl_free_context_data(struct wined3d_context
*context
)
7749 HeapFree(GetProcessHeap(), 0, context
->shader_backend_data
);
7752 static void shader_glsl_init_context_state(struct wined3d_context
*context
)
7754 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7756 gl_info
->gl_ops
.gl
.p_glEnable(GL_PROGRAM_POINT_SIZE
);
7757 checkGLcall("GL_PROGRAM_POINT_SIZE");
7760 static void shader_glsl_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
7764 if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 50) && gl_info
->supported
[WINED3D_GL_VERSION_3_2
]
7765 && gl_info
->supported
[ARB_SHADER_BIT_ENCODING
] && gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
7767 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7768 * texldd and texldl instructions. */
7769 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] || gl_info
->supported
[EXT_GPU_SHADER4
])
7773 TRACE("Shader model %u.\n", shader_model
);
7775 caps
->vs_version
= min(wined3d_settings
.max_sm_vs
, shader_model
);
7776 caps
->gs_version
= min(wined3d_settings
.max_sm_gs
, shader_model
);
7777 caps
->ps_version
= min(wined3d_settings
.max_sm_ps
, shader_model
);
7779 caps
->vs_uniform_count
= gl_info
->limits
.glsl_vs_float_constants
;
7780 caps
->ps_uniform_count
= gl_info
->limits
.glsl_ps_float_constants
;
7781 caps
->varying_count
= gl_info
->limits
.glsl_varyings
;
7783 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7784 * Direct3D minimum requirement.
7786 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7787 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7789 * The problem is that the refrast clamps temporary results in the shader to
7790 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7791 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7792 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7793 * offer a way to query this.
7795 if (shader_model
>= 4)
7796 caps
->ps_1x_max_value
= FLT_MAX
;
7798 caps
->ps_1x_max_value
= 1024.0f
;
7800 /* Ideally we'd only set caps like sRGB writes here if supported by both
7801 * the shader backend and the fragment pipe, but we can get called before
7802 * shader_glsl_alloc(). */
7803 caps
->wined3d_caps
= WINED3D_SHADER_CAP_VS_CLIPPING
7804 | WINED3D_SHADER_CAP_SRGB_WRITE
;
7807 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
7809 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
7811 TRACE("Checking support for fixup:\n");
7812 dump_color_fixup_desc(fixup
);
7815 /* We support everything except YUV conversions. */
7816 if (!is_complex_fixup(fixup
))
7822 TRACE("[FAILED]\n");
7826 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
7828 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
7829 /* WINED3DSIH_ADD */ shader_glsl_binop
,
7830 /* WINED3DSIH_AND */ shader_glsl_binop
,
7831 /* WINED3DSIH_BEM */ shader_glsl_bem
,
7832 /* WINED3DSIH_BREAK */ shader_glsl_break
,
7833 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
7834 /* WINED3DSIH_BREAKP */ shader_glsl_breakp
,
7835 /* WINED3DSIH_CALL */ shader_glsl_call
,
7836 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
7837 /* WINED3DSIH_CMP */ shader_glsl_conditional_move
,
7838 /* WINED3DSIH_CND */ shader_glsl_cnd
,
7839 /* WINED3DSIH_CRS */ shader_glsl_cross
,
7840 /* WINED3DSIH_CUT */ shader_glsl_cut
,
7841 /* WINED3DSIH_DCL */ shader_glsl_nop
,
7842 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop
,
7843 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop
,
7844 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop
,
7845 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop
,
7846 /* WINED3DSIH_DEF */ shader_glsl_nop
,
7847 /* WINED3DSIH_DEFB */ shader_glsl_nop
,
7848 /* WINED3DSIH_DEFI */ shader_glsl_nop
,
7849 /* WINED3DSIH_DIV */ shader_glsl_binop
,
7850 /* WINED3DSIH_DP2 */ shader_glsl_dot
,
7851 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add
,
7852 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
7853 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
7854 /* WINED3DSIH_DST */ shader_glsl_dst
,
7855 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
7856 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
7857 /* WINED3DSIH_ELSE */ shader_glsl_else
,
7858 /* WINED3DSIH_EMIT */ shader_glsl_emit
,
7859 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
7860 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
7861 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
7862 /* WINED3DSIH_EQ */ shader_glsl_relop
,
7863 /* WINED3DSIH_EXP */ shader_glsl_scalar_op
,
7864 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
7865 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
7866 /* WINED3DSIH_FTOI */ shader_glsl_to_int
,
7867 /* WINED3DSIH_GE */ shader_glsl_relop
,
7868 /* WINED3DSIH_IADD */ shader_glsl_binop
,
7869 /* WINED3DSIH_IEQ */ NULL
,
7870 /* WINED3DSIH_IF */ shader_glsl_if
,
7871 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
7872 /* WINED3DSIH_IGE */ shader_glsl_relop
,
7873 /* WINED3DSIH_IMUL */ shader_glsl_imul
,
7874 /* WINED3DSIH_ISHL */ shader_glsl_binop
,
7875 /* WINED3DSIH_ITOF */ shader_glsl_to_float
,
7876 /* WINED3DSIH_LABEL */ shader_glsl_label
,
7877 /* WINED3DSIH_LD */ NULL
,
7878 /* WINED3DSIH_LIT */ shader_glsl_lit
,
7879 /* WINED3DSIH_LOG */ shader_glsl_scalar_op
,
7880 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op
,
7881 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
7882 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
7883 /* WINED3DSIH_LT */ shader_glsl_relop
,
7884 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
7885 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
7886 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
7887 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
7888 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
7889 /* WINED3DSIH_MAD */ shader_glsl_mad
,
7890 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
7891 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
7892 /* WINED3DSIH_MOV */ shader_glsl_mov
,
7893 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
7894 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move
,
7895 /* WINED3DSIH_MUL */ shader_glsl_binop
,
7896 /* WINED3DSIH_NE */ shader_glsl_relop
,
7897 /* WINED3DSIH_NOP */ shader_glsl_nop
,
7898 /* WINED3DSIH_NRM */ shader_glsl_nrm
,
7899 /* WINED3DSIH_OR */ shader_glsl_binop
,
7900 /* WINED3DSIH_PHASE */ shader_glsl_nop
,
7901 /* WINED3DSIH_POW */ shader_glsl_pow
,
7902 /* WINED3DSIH_RCP */ shader_glsl_scalar_op
,
7903 /* WINED3DSIH_REP */ shader_glsl_rep
,
7904 /* WINED3DSIH_RET */ shader_glsl_ret
,
7905 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl
,
7906 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op
,
7907 /* WINED3DSIH_SAMPLE */ shader_glsl_sample
,
7908 /* WINED3DSIH_SAMPLE_GRAD */ NULL
,
7909 /* WINED3DSIH_SAMPLE_LOD */ NULL
,
7910 /* WINED3DSIH_SETP */ NULL
,
7911 /* WINED3DSIH_SGE */ shader_glsl_compare
,
7912 /* WINED3DSIH_SGN */ shader_glsl_sgn
,
7913 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
7914 /* WINED3DSIH_SLT */ shader_glsl_compare
,
7915 /* WINED3DSIH_SQRT */ shader_glsl_map2gl
,
7916 /* WINED3DSIH_SUB */ shader_glsl_binop
,
7917 /* WINED3DSIH_TEX */ shader_glsl_tex
,
7918 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem
,
7919 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem
,
7920 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord
,
7921 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth
,
7922 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3
,
7923 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex
,
7924 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill
,
7925 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
7926 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
7927 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth
,
7928 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad
,
7929 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex
,
7930 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3
,
7931 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
7932 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad
,
7933 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec
,
7934 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex
,
7935 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec
,
7936 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar
,
7937 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb
,
7938 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb
,
7939 /* WINED3DSIH_UDIV */ shader_glsl_udiv
,
7940 /* WINED3DSIH_UGE */ shader_glsl_relop
,
7941 /* WINED3DSIH_USHR */ shader_glsl_binop
,
7942 /* WINED3DSIH_UTOF */ shader_glsl_to_float
,
7943 /* WINED3DSIH_XOR */ shader_glsl_binop
,
7946 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
7947 SHADER_HANDLER hw_fct
;
7949 /* Select handler */
7950 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
7952 /* Unhandled opcode */
7955 FIXME("Backend can't handle opcode %#x\n", ins
->handler_idx
);
7960 shader_glsl_add_instruction_modifiers(ins
);
7963 static BOOL
shader_glsl_has_ffp_proj_control(void *shader_priv
)
7965 struct shader_glsl_priv
*priv
= shader_priv
;
7967 return priv
->ffp_proj_control
;
7970 const struct wined3d_shader_backend_ops glsl_shader_backend
=
7972 shader_glsl_handle_instruction
,
7974 shader_glsl_disable
,
7975 shader_glsl_select_depth_blt
,
7976 shader_glsl_deselect_depth_blt
,
7977 shader_glsl_update_float_vertex_constants
,
7978 shader_glsl_update_float_pixel_constants
,
7979 shader_glsl_load_constants
,
7980 shader_glsl_destroy
,
7983 shader_glsl_allocate_context_data
,
7984 shader_glsl_free_context_data
,
7985 shader_glsl_init_context_state
,
7986 shader_glsl_get_caps
,
7987 shader_glsl_color_fixup_supported
,
7988 shader_glsl_has_ffp_proj_control
,
7991 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
) {}
7993 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info
*gl_info
, struct wined3d_vertex_caps
*caps
)
7995 caps
->xyzrhw
= TRUE
;
7996 caps
->ffp_generic_attributes
= TRUE
;
7997 caps
->max_active_lights
= MAX_ACTIVE_LIGHTS
;
7998 caps
->max_vertex_blend_matrices
= MAX_VERTEX_BLENDS
;
7999 caps
->max_vertex_blend_matrix_index
= 0;
8000 caps
->vertex_processing_caps
= WINED3DVTXPCAPS_TEXGEN
8001 | WINED3DVTXPCAPS_MATERIALSOURCE7
8002 | WINED3DVTXPCAPS_VERTEXFOG
8003 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8004 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8005 | WINED3DVTXPCAPS_LOCALVIEWER
8006 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP
;
8007 caps
->fvf_caps
= WINED3DFVFCAPS_PSIZE
| 8; /* 8 texture coordinates. */
8008 caps
->max_user_clip_planes
= gl_info
->limits
.clipplanes
;
8009 caps
->raster_caps
= WINED3DPRASTERCAPS_FOGRANGE
;
8012 static DWORD
glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
8014 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
8015 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
8019 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
8021 struct shader_glsl_priv
*priv
;
8023 if (shader_backend
== &glsl_shader_backend
)
8027 if (wine_rb_init(&priv
->ffp_vertex_shaders
, &wined3d_ffp_vertex_program_rb_functions
) == -1)
8029 ERR("Failed to initialize rbtree.\n");
8036 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8041 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry
*entry
, void *context
)
8043 struct glsl_ffp_vertex_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
8044 struct glsl_ffp_vertex_shader
, desc
.entry
);
8045 struct glsl_shader_prog_link
*program
, *program2
;
8046 struct glsl_ffp_destroy_ctx
*ctx
= context
;
8048 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
8049 struct glsl_shader_prog_link
, vs
.shader_entry
)
8051 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
8053 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
8054 HeapFree(GetProcessHeap(), 0, shader
);
8057 /* Context activation is done by the caller. */
8058 static void glsl_vertex_pipe_vp_free(struct wined3d_device
*device
)
8060 struct shader_glsl_priv
*priv
= device
->vertex_priv
;
8061 struct glsl_ffp_destroy_ctx ctx
;
8064 ctx
.gl_info
= &device
->adapter
->gl_info
;
8065 wine_rb_destroy(&priv
->ffp_vertex_shaders
, shader_glsl_free_ffp_vertex_shader
, &ctx
);
8068 static void glsl_vertex_pipe_shader(struct wined3d_context
*context
,
8069 const struct wined3d_state
*state
, DWORD state_id
)
8071 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8074 static void glsl_vertex_pipe_vdecl(struct wined3d_context
*context
,
8075 const struct wined3d_state
*state
, DWORD state_id
)
8077 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8078 BOOL transformed
= context
->stream_info
.position_transformed
;
8079 BOOL wasrhw
= context
->last_was_rhw
;
8082 context
->last_was_rhw
= transformed
;
8084 /* If the vertex declaration contains a transformed position attribute,
8085 * the draw uses the fixed function vertex pipeline regardless of any
8086 * vertex shader set by the application. */
8087 if (transformed
!= wasrhw
)
8088 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8092 if (context
->last_was_vshader
)
8094 for (i
= 0; i
< gl_info
->limits
.clipplanes
; ++i
)
8095 clipplane(context
, state
, STATE_CLIPPLANE(i
));
8098 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
8100 /* Because of settings->texcoords, we have to regenerate the vertex
8101 * shader on a vdecl change if there aren't enough varyings to just
8102 * always output all the texture coordinates. */
8103 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
))
8104 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8107 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.major
== 1
8108 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.minor
<= 3)
8109 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8113 if (!context
->last_was_vshader
)
8115 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8116 for (i
= 0; i
< gl_info
->limits
.clipplanes
; ++i
)
8117 clipplane(context
, state
, STATE_CLIPPLANE(i
));
8121 context
->last_was_vshader
= use_vs(state
);
8124 static void glsl_vertex_pipe_vs(struct wined3d_context
*context
,
8125 const struct wined3d_state
*state
, DWORD state_id
)
8127 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8128 /* Different vertex shaders potentially require a different vertex attributes setup. */
8129 if (!isStateDirty(context
, STATE_VDECL
))
8130 context_apply_state(context
, state
, STATE_VDECL
);
8133 static void glsl_vertex_pipe_world(struct wined3d_context
*context
,
8134 const struct wined3d_state
*state
, DWORD state_id
)
8136 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
;
8139 static void glsl_vertex_pipe_vertexblend(struct wined3d_context
*context
,
8140 const struct wined3d_state
*state
, DWORD state_id
)
8142 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
8145 static void glsl_vertex_pipe_view(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
8147 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8150 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
8151 | WINED3D_SHADER_CONST_FFP_LIGHTS
8152 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
8154 for (k
= 0; k
< gl_info
->limits
.clipplanes
; ++k
)
8156 if (!isStateDirty(context
, STATE_CLIPPLANE(k
)))
8157 clipplane(context
, state
, STATE_CLIPPLANE(k
));
8161 static void glsl_vertex_pipe_projection(struct wined3d_context
*context
,
8162 const struct wined3d_state
*state
, DWORD state_id
)
8164 /* Table fog behavior depends on the projection matrix. */
8165 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
8166 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] != WINED3D_FOG_NONE
)
8167 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
8168 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PROJ
;
8171 static void glsl_vertex_pipe_viewport(struct wined3d_context
*context
,
8172 const struct wined3d_state
*state
, DWORD state_id
)
8174 if (!isStateDirty(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
)))
8175 glsl_vertex_pipe_projection(context
, state
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
8176 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
))
8177 && state
->render_states
[WINED3D_RS_POINTSCALEENABLE
])
8178 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
8179 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POS_FIXUP
;
8182 static void glsl_vertex_pipe_texmatrix(struct wined3d_context
*context
,
8183 const struct wined3d_state
*state
, DWORD state_id
)
8185 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
8188 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context
*context
,
8189 const struct wined3d_state
*state
, DWORD state_id
)
8191 DWORD sampler
= state_id
- STATE_SAMPLER(0);
8192 const struct wined3d_texture
*texture
= state
->textures
[sampler
];
8198 if (sampler
>= MAX_TEXTURES
)
8201 if ((np2
= !(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
8202 || context
->lastWasPow2Texture
& (1u << sampler
))
8205 context
->lastWasPow2Texture
|= 1u << sampler
;
8207 context
->lastWasPow2Texture
&= ~(1u << sampler
);
8209 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
8213 static void glsl_vertex_pipe_material(struct wined3d_context
*context
,
8214 const struct wined3d_state
*state
, DWORD state_id
)
8216 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
8219 static void glsl_vertex_pipe_light(struct wined3d_context
*context
,
8220 const struct wined3d_state
*state
, DWORD state_id
)
8222 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
8225 static void glsl_vertex_pipe_pointsize(struct wined3d_context
*context
,
8226 const struct wined3d_state
*state
, DWORD state_id
)
8228 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
8231 static void glsl_vertex_pipe_pointscale(struct wined3d_context
*context
,
8232 const struct wined3d_state
*state
, DWORD state_id
)
8235 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
8238 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states
[] =
8240 {STATE_VDECL
, {STATE_VDECL
, glsl_vertex_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
8241 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_vertex_pipe_vs
}, WINED3D_GL_EXT_NONE
},
8242 {STATE_MATERIAL
, {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8243 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_vertex_pipe_material
}, WINED3D_GL_EXT_NONE
},
8245 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane
}, WINED3D_GL_EXT_NONE
},
8246 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane
}, WINED3D_GL_EXT_NONE
},
8247 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane
}, WINED3D_GL_EXT_NONE
},
8248 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane
}, WINED3D_GL_EXT_NONE
},
8249 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane
}, WINED3D_GL_EXT_NONE
},
8250 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane
}, WINED3D_GL_EXT_NONE
},
8251 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane
}, WINED3D_GL_EXT_NONE
},
8252 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane
}, WINED3D_GL_EXT_NONE
},
8253 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane
}, WINED3D_GL_EXT_NONE
},
8254 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane
}, WINED3D_GL_EXT_NONE
},
8255 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane
}, WINED3D_GL_EXT_NONE
},
8256 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane
}, WINED3D_GL_EXT_NONE
},
8257 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane
}, WINED3D_GL_EXT_NONE
},
8258 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane
}, WINED3D_GL_EXT_NONE
},
8259 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane
}, WINED3D_GL_EXT_NONE
},
8260 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane
}, WINED3D_GL_EXT_NONE
},
8261 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane
}, WINED3D_GL_EXT_NONE
},
8262 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane
}, WINED3D_GL_EXT_NONE
},
8263 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane
}, WINED3D_GL_EXT_NONE
},
8264 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane
}, WINED3D_GL_EXT_NONE
},
8265 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane
}, WINED3D_GL_EXT_NONE
},
8266 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane
}, WINED3D_GL_EXT_NONE
},
8267 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane
}, WINED3D_GL_EXT_NONE
},
8268 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane
}, WINED3D_GL_EXT_NONE
},
8269 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane
}, WINED3D_GL_EXT_NONE
},
8270 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane
}, WINED3D_GL_EXT_NONE
},
8271 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane
}, WINED3D_GL_EXT_NONE
},
8272 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane
}, WINED3D_GL_EXT_NONE
},
8273 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane
}, WINED3D_GL_EXT_NONE
},
8274 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane
}, WINED3D_GL_EXT_NONE
},
8275 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane
}, WINED3D_GL_EXT_NONE
},
8276 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane
}, WINED3D_GL_EXT_NONE
},
8278 {STATE_LIGHT_TYPE
, {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8279 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8280 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8281 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8282 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8283 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8284 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8285 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8286 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8288 {STATE_VIEWPORT
, {STATE_VIEWPORT
, glsl_vertex_pipe_viewport
}, WINED3D_GL_EXT_NONE
},
8289 /* Transform states */
8290 {STATE_TRANSFORM(WINED3D_TS_VIEW
), {STATE_TRANSFORM(WINED3D_TS_VIEW
), glsl_vertex_pipe_view
}, WINED3D_GL_EXT_NONE
},
8291 {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), glsl_vertex_pipe_projection
}, WINED3D_GL_EXT_NONE
},
8292 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8293 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8294 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8295 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8296 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8297 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8298 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8299 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
8300 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world
}, WINED3D_GL_EXT_NONE
},
8301 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
8302 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
8303 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
8304 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8305 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8306 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8307 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8308 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8309 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8310 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8311 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
8312 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8313 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8314 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8315 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8316 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8317 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8318 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8319 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8321 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8322 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8323 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8324 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8325 {STATE_RENDER(WINED3D_RS_CLIPPING
), {STATE_RENDER(WINED3D_RS_CLIPPING
), state_clipping
}, WINED3D_GL_EXT_NONE
},
8326 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE
), {STATE_RENDER(WINED3D_RS_CLIPPING
), NULL
}, WINED3D_GL_EXT_NONE
},
8327 {STATE_RENDER(WINED3D_RS_LIGHTING
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8328 {STATE_RENDER(WINED3D_RS_AMBIENT
), {STATE_RENDER(WINED3D_RS_AMBIENT
), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
8329 {STATE_RENDER(WINED3D_RS_COLORVERTEX
), {STATE_RENDER(WINED3D_RS_COLORVERTEX
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8330 {STATE_RENDER(WINED3D_RS_LOCALVIEWER
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8331 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8332 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8333 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8334 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8335 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8336 {STATE_RENDER(WINED3D_RS_VERTEXBLEND
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8337 {STATE_RENDER(WINED3D_RS_POINTSIZE
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
8338 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), glsl_vertex_pipe_pointsize
}, WINED3D_GL_EXT_NONE
},
8339 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite
}, ARB_POINT_SPRITE
},
8340 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite_w
}, WINED3D_GL_EXT_NONE
},
8341 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), glsl_vertex_pipe_pointscale
}, WINED3D_GL_EXT_NONE
},
8342 {STATE_RENDER(WINED3D_RS_POINTSCALE_A
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8343 {STATE_RENDER(WINED3D_RS_POINTSCALE_B
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8344 {STATE_RENDER(WINED3D_RS_POINTSCALE_C
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8345 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
8346 {STATE_RENDER(WINED3D_RS_TWEENFACTOR
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8347 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
8348 /* NP2 texture matrix fixups. They are not needed if
8349 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8350 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8352 {STATE_SAMPLER(0), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8353 {STATE_SAMPLER(0), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8354 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8355 {STATE_SAMPLER(1), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8356 {STATE_SAMPLER(1), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8357 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8358 {STATE_SAMPLER(2), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8359 {STATE_SAMPLER(2), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8360 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8361 {STATE_SAMPLER(3), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8362 {STATE_SAMPLER(3), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8363 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8364 {STATE_SAMPLER(4), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8365 {STATE_SAMPLER(4), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8366 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8367 {STATE_SAMPLER(5), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8368 {STATE_SAMPLER(5), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8369 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8370 {STATE_SAMPLER(6), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8371 {STATE_SAMPLER(6), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8372 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8373 {STATE_SAMPLER(7), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
8374 {STATE_SAMPLER(7), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
8375 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
8376 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8377 {0 /* Terminate */, {0, NULL
}, WINED3D_GL_EXT_NONE
},
8381 * - Implement vertex tweening. */
8382 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe
=
8384 glsl_vertex_pipe_vp_enable
,
8385 glsl_vertex_pipe_vp_get_caps
,
8386 glsl_vertex_pipe_vp_get_emul_mask
,
8387 glsl_vertex_pipe_vp_alloc
,
8388 glsl_vertex_pipe_vp_free
,
8389 glsl_vertex_pipe_vp_states
,
8392 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
)
8394 /* Nothing to do. */
8397 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info
*gl_info
, struct fragment_caps
*caps
)
8399 caps
->wined3d_caps
= WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8400 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8401 | WINED3D_FRAGMENT_CAP_COLOR_KEY
;
8402 caps
->PrimitiveMiscCaps
= WINED3DPMISCCAPS_TSSARGTEMP
8403 | WINED3DPMISCCAPS_PERSTAGECONSTANT
;
8404 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
8405 | WINED3DTEXOPCAPS_SELECTARG1
8406 | WINED3DTEXOPCAPS_SELECTARG2
8407 | WINED3DTEXOPCAPS_MODULATE4X
8408 | WINED3DTEXOPCAPS_MODULATE2X
8409 | WINED3DTEXOPCAPS_MODULATE
8410 | WINED3DTEXOPCAPS_ADDSIGNED2X
8411 | WINED3DTEXOPCAPS_ADDSIGNED
8412 | WINED3DTEXOPCAPS_ADD
8413 | WINED3DTEXOPCAPS_SUBTRACT
8414 | WINED3DTEXOPCAPS_ADDSMOOTH
8415 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8416 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8417 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8418 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8419 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8420 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8421 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8422 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8423 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8424 | WINED3DTEXOPCAPS_DOTPRODUCT3
8425 | WINED3DTEXOPCAPS_MULTIPLYADD
8426 | WINED3DTEXOPCAPS_LERP
8427 | WINED3DTEXOPCAPS_BUMPENVMAP
8428 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
8429 caps
->MaxTextureBlendStages
= 8;
8430 caps
->MaxSimultaneousTextures
= min(gl_info
->limits
.fragment_samplers
, 8);
8433 static DWORD
glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
8435 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
8436 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
8440 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
8442 struct shader_glsl_priv
*priv
;
8444 if (shader_backend
== &glsl_shader_backend
)
8448 if (wine_rb_init(&priv
->ffp_fragment_shaders
, &wined3d_ffp_frag_program_rb_functions
) == -1)
8450 ERR("Failed to initialize rbtree.\n");
8457 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8462 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry
*entry
, void *context
)
8464 struct glsl_ffp_fragment_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
8465 struct glsl_ffp_fragment_shader
, entry
.entry
);
8466 struct glsl_shader_prog_link
*program
, *program2
;
8467 struct glsl_ffp_destroy_ctx
*ctx
= context
;
8469 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
8470 struct glsl_shader_prog_link
, ps
.shader_entry
)
8472 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
8474 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
8475 HeapFree(GetProcessHeap(), 0, shader
);
8478 /* Context activation is done by the caller. */
8479 static void glsl_fragment_pipe_free(struct wined3d_device
*device
)
8481 struct shader_glsl_priv
*priv
= device
->fragment_priv
;
8482 struct glsl_ffp_destroy_ctx ctx
;
8485 ctx
.gl_info
= &device
->adapter
->gl_info
;
8486 wine_rb_destroy(&priv
->ffp_fragment_shaders
, shader_glsl_free_ffp_fragment_shader
, &ctx
);
8489 static void glsl_fragment_pipe_shader(struct wined3d_context
*context
,
8490 const struct wined3d_state
*state
, DWORD state_id
)
8492 context
->last_was_pshader
= use_ps(state
);
8494 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8497 static void glsl_fragment_pipe_fogparams(struct wined3d_context
*context
,
8498 const struct wined3d_state
*state
, DWORD state_id
)
8500 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
8503 static void glsl_fragment_pipe_fog(struct wined3d_context
*context
,
8504 const struct wined3d_state
*state
, DWORD state_id
)
8506 BOOL use_vshader
= use_vs(state
);
8507 enum fogsource new_source
;
8508 DWORD fogstart
= state
->render_states
[WINED3D_RS_FOGSTART
];
8509 DWORD fogend
= state
->render_states
[WINED3D_RS_FOGEND
];
8511 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8513 if (!state
->render_states
[WINED3D_RS_FOGENABLE
])
8516 if (state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
8519 new_source
= FOGSOURCE_VS
;
8520 else if (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
|| context
->stream_info
.position_transformed
)
8521 new_source
= FOGSOURCE_COORD
;
8523 new_source
= FOGSOURCE_FFP
;
8527 new_source
= FOGSOURCE_FFP
;
8530 if (new_source
!= context
->fog_source
|| fogstart
== fogend
)
8532 context
->fog_source
= new_source
;
8533 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
8537 static void glsl_fragment_pipe_vdecl(struct wined3d_context
*context
,
8538 const struct wined3d_state
*state
, DWORD state_id
)
8540 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8541 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
8542 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8544 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
)))
8545 glsl_fragment_pipe_fog(context
, state
, state_id
);
8548 static void glsl_fragment_pipe_vs(struct wined3d_context
*context
,
8549 const struct wined3d_state
*state
, DWORD state_id
)
8551 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8552 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
8553 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8556 static void glsl_fragment_pipe_tex_transform(struct wined3d_context
*context
,
8557 const struct wined3d_state
*state
, DWORD state_id
)
8559 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
8562 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context
*context
,
8563 const struct wined3d_state
*state
, DWORD state_id
)
8565 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
8568 static void glsl_fragment_pipe_alpha_test(struct wined3d_context
*context
,
8569 const struct wined3d_state
*state
, DWORD state_id
)
8571 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8575 TRACE("context %p, state %p, state_id %#x.\n", context
, state
, state_id
);
8577 if (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
])
8579 gl_info
->gl_ops
.gl
.p_glEnable(GL_ALPHA_TEST
);
8580 checkGLcall("glEnable GL_ALPHA_TEST");
8584 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
8585 checkGLcall("glDisable GL_ALPHA_TEST");
8589 ref
= ((float)state
->render_states
[WINED3D_RS_ALPHAREF
]) / 255.0f
;
8590 glParm
= wined3d_gl_compare_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
]);
8594 gl_info
->gl_ops
.gl
.p_glAlphaFunc(glParm
, ref
);
8595 checkGLcall("glAlphaFunc");
8599 static void glsl_fragment_pipe_color_key(struct wined3d_context
*context
,
8600 const struct wined3d_state
*state
, DWORD state_id
)
8602 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
8605 static const struct StateEntryTemplate glsl_fragment_pipe_state_template
[] =
8607 {STATE_VDECL
, {STATE_VDECL
, glsl_fragment_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
8608 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_fragment_pipe_vs
}, WINED3D_GL_EXT_NONE
},
8609 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8610 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8611 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8612 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8613 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8614 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8615 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8616 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8617 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8618 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8619 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8620 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8621 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8622 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8623 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8624 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8625 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8626 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8627 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8628 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8629 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8630 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8631 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8632 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8633 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8634 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8635 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8636 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8637 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8638 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8639 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8640 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8641 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8642 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8643 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8644 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8645 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8646 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8647 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8648 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8649 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8650 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8651 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8652 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8653 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8654 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8655 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8656 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8657 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8658 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8659 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8660 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8661 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8662 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8663 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8664 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8665 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8666 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8667 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8668 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8669 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8670 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8671 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8672 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8673 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8674 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8675 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8676 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8677 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8678 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8679 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8680 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8681 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8682 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8683 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8684 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8685 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_alpha_test
}, WINED3D_GL_EXT_NONE
},
8686 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8687 {STATE_COLOR_KEY
, { STATE_COLOR_KEY
, glsl_fragment_pipe_color_key
}, WINED3D_GL_EXT_NONE
},
8688 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_fragment_pipe_fog
}, WINED3D_GL_EXT_NONE
},
8689 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8690 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
8691 {STATE_RENDER(WINED3D_RS_FOGSTART
), {STATE_RENDER(WINED3D_RS_FOGSTART
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
8692 {STATE_RENDER(WINED3D_RS_FOGEND
), {STATE_RENDER(WINED3D_RS_FOGSTART
), NULL
}, WINED3D_GL_EXT_NONE
},
8693 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), state_srgbwrite
}, ARB_FRAMEBUFFER_SRGB
},
8694 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
8695 {STATE_RENDER(WINED3D_RS_FOGCOLOR
), {STATE_RENDER(WINED3D_RS_FOGCOLOR
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
8696 {STATE_RENDER(WINED3D_RS_FOGDENSITY
), {STATE_RENDER(WINED3D_RS_FOGDENSITY
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
8697 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, ARB_POINT_SPRITE
},
8698 {STATE_TEXTURESTAGE(0,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
8699 {STATE_TEXTURESTAGE(1,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
8700 {STATE_TEXTURESTAGE(2,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
8701 {STATE_TEXTURESTAGE(3,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
8702 {STATE_TEXTURESTAGE(4,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
8703 {STATE_TEXTURESTAGE(5,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
8704 {STATE_TEXTURESTAGE(6,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
8705 {STATE_TEXTURESTAGE(7,WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_fragment_pipe_tex_transform
}, WINED3D_GL_EXT_NONE
},
8706 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8707 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8708 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8709 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8710 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8711 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8712 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8713 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8714 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
8715 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
8716 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE
},
8719 static BOOL
glsl_fragment_pipe_alloc_context_data(struct wined3d_context
*context
)
8724 static void glsl_fragment_pipe_free_context_data(struct wined3d_context
*context
)
8728 const struct fragment_pipeline glsl_fragment_pipe
=
8730 glsl_fragment_pipe_enable
,
8731 glsl_fragment_pipe_get_caps
,
8732 glsl_fragment_pipe_get_emul_mask
,
8733 glsl_fragment_pipe_alloc
,
8734 glsl_fragment_pipe_free
,
8735 glsl_fragment_pipe_alloc_context_data
,
8736 glsl_fragment_pipe_free_context_data
,
8737 shader_glsl_color_fixup_supported
,
8738 glsl_fragment_pipe_state_template
,