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 0x01
48 #define WINED3D_GLSL_SAMPLE_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
55 unsigned int coord_size
;
56 unsigned int resinfo_size
;
57 const char *type_part
;
59 resource_type_info
[] =
61 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
62 {1, 1, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
63 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
64 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
66 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
67 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
68 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
69 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
70 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
85 struct glsl_sample_function
87 struct wined3d_string_buffer
*name
;
88 unsigned int coord_mask
;
89 unsigned int deriv_mask
;
90 enum wined3d_data_type data_type
;
91 BOOL output_single_component
;
92 unsigned int offset_size
;
97 HEAP_NODE_TRAVERSE_LEFT
,
98 HEAP_NODE_TRAVERSE_RIGHT
,
102 struct constant_entry
105 unsigned int version
;
110 struct constant_entry
*entries
;
112 unsigned int *positions
;
116 /* GLSL shader private data */
117 struct shader_glsl_priv
{
118 struct wined3d_string_buffer shader_buffer
;
119 struct wined3d_string_buffer_list string_buffers
;
120 struct wine_rb_tree program_lookup
;
121 struct constant_heap vconst_heap
;
122 struct constant_heap pconst_heap
;
123 unsigned char *stack
;
124 GLuint depth_blt_program_full
[WINED3D_GL_RES_TYPE_COUNT
];
125 GLuint depth_blt_program_masked
[WINED3D_GL_RES_TYPE_COUNT
];
126 UINT next_constant_version
;
128 const struct wined3d_vertex_pipe_ops
*vertex_pipe
;
129 const struct fragment_pipeline
*fragment_pipe
;
130 struct wine_rb_tree ffp_vertex_shaders
;
131 struct wine_rb_tree ffp_fragment_shaders
;
132 BOOL ffp_proj_control
;
133 BOOL legacy_lighting
;
136 struct glsl_vs_program
138 struct list shader_entry
;
140 GLenum vertex_color_clamp
;
141 GLint uniform_f_locations
[WINED3D_MAX_VS_CONSTS_F
];
142 GLint uniform_i_locations
[WINED3D_MAX_CONSTS_I
];
143 GLint uniform_b_locations
[WINED3D_MAX_CONSTS_B
];
144 GLint pos_fixup_location
;
146 GLint modelview_matrix_location
[MAX_VERTEX_BLENDS
];
147 GLint projection_matrix_location
;
148 GLint normal_matrix_location
;
149 GLint texture_matrix_location
[MAX_TEXTURES
];
150 GLint material_ambient_location
;
151 GLint material_diffuse_location
;
152 GLint material_specular_location
;
153 GLint material_emissive_location
;
154 GLint material_shininess_location
;
155 GLint light_ambient_location
;
170 } light_location
[MAX_ACTIVE_LIGHTS
];
171 GLint pointsize_location
;
172 GLint pointsize_min_location
;
173 GLint pointsize_max_location
;
174 GLint pointsize_c_att_location
;
175 GLint pointsize_l_att_location
;
176 GLint pointsize_q_att_location
;
177 GLint clip_planes_location
;
180 struct glsl_gs_program
182 struct list shader_entry
;
185 GLint pos_fixup_location
;
188 struct glsl_ps_program
190 struct list shader_entry
;
192 GLint uniform_f_locations
[WINED3D_MAX_PS_CONSTS_F
];
193 GLint uniform_i_locations
[WINED3D_MAX_CONSTS_I
];
194 GLint uniform_b_locations
[WINED3D_MAX_CONSTS_B
];
195 GLint bumpenv_mat_location
[MAX_TEXTURES
];
196 GLint bumpenv_lum_scale_location
[MAX_TEXTURES
];
197 GLint bumpenv_lum_offset_location
[MAX_TEXTURES
];
198 GLint tss_constant_location
[MAX_TEXTURES
];
199 GLint tex_factor_location
;
200 GLint specular_enable_location
;
201 GLint fog_color_location
;
202 GLint fog_density_location
;
203 GLint fog_end_location
;
204 GLint fog_scale_location
;
205 GLint alpha_test_ref_location
;
206 GLint ycorrection_location
;
207 GLint np2_fixup_location
;
208 GLint color_key_location
;
209 const struct ps_np2fixup_info
*np2_fixup_info
;
212 /* Struct to maintain data about a linked GLSL program */
213 struct glsl_shader_prog_link
215 struct wine_rb_entry program_lookup_entry
;
216 struct glsl_vs_program vs
;
217 struct glsl_gs_program gs
;
218 struct glsl_ps_program ps
;
220 DWORD constant_update_mask
;
221 UINT constant_version
;
224 struct glsl_program_key
231 struct shader_glsl_ctx_priv
{
232 const struct vs_compile_args
*cur_vs_args
;
233 const struct ps_compile_args
*cur_ps_args
;
234 struct ps_np2fixup_info
*cur_np2fixup_info
;
235 struct wined3d_string_buffer_list
*string_buffers
;
238 struct glsl_context_data
240 struct glsl_shader_prog_link
*glsl_program
;
243 struct glsl_ps_compiled_shader
245 struct ps_compile_args args
;
246 struct ps_np2fixup_info np2fixup
;
250 struct glsl_vs_compiled_shader
252 struct vs_compile_args args
;
256 struct glsl_gs_compiled_shader
258 struct gs_compile_args args
;
262 struct glsl_shader_private
266 struct glsl_vs_compiled_shader
*vs
;
267 struct glsl_gs_compiled_shader
*gs
;
268 struct glsl_ps_compiled_shader
*ps
;
270 UINT num_gl_shaders
, shader_array_size
;
273 struct glsl_ffp_vertex_shader
275 struct wined3d_ffp_vs_desc desc
;
277 struct list linked_programs
;
280 struct glsl_ffp_fragment_shader
282 struct ffp_frag_desc entry
;
284 struct list linked_programs
;
287 struct glsl_ffp_destroy_ctx
289 struct shader_glsl_priv
*priv
;
290 const struct wined3d_gl_info
*gl_info
;
293 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context
*ctx
);
295 static const char *debug_gl_shader_type(GLenum type
)
299 #define WINED3D_TO_STR(u) case u: return #u
300 WINED3D_TO_STR(GL_VERTEX_SHADER
);
301 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER
);
302 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER
);
303 WINED3D_TO_STR(GL_GEOMETRY_SHADER
);
304 WINED3D_TO_STR(GL_FRAGMENT_SHADER
);
305 WINED3D_TO_STR(GL_COMPUTE_SHADER
);
306 #undef WINED3D_TO_STR
308 return wine_dbg_sprintf("UNKNOWN(%#x)", type
);
312 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type
)
316 case WINED3D_SHADER_TYPE_VERTEX
:
319 case WINED3D_SHADER_TYPE_HULL
:
322 case WINED3D_SHADER_TYPE_DOMAIN
:
325 case WINED3D_SHADER_TYPE_GEOMETRY
:
328 case WINED3D_SHADER_TYPE_PIXEL
:
331 case WINED3D_SHADER_TYPE_COMPUTE
:
335 FIXME("Unhandled shader type %#x.\n", type
);
340 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info
*gl_info
,
341 const struct wined3d_shader_version
*version
)
343 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
345 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 30) && version
&& version
->major
>= 4)
351 static const char *shader_glsl_get_version_declaration(const struct wined3d_gl_info
*gl_info
,
352 const struct wined3d_shader_version
*version
)
354 unsigned int glsl_version
;
356 switch (glsl_version
= shader_glsl_get_version(gl_info
, version
))
359 return "#version 150";
361 return "#version 130";
363 return "#version 120";
365 FIXME("Unexpected GLSL version %u requested.\n", glsl_version
);
370 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer
*buffer
, const float *values
)
374 wined3d_ftoa(values
[0], str
[0]);
375 wined3d_ftoa(values
[1], str
[1]);
376 wined3d_ftoa(values
[2], str
[2]);
377 wined3d_ftoa(values
[3], str
[3]);
378 shader_addline(buffer
, "vec4(%s, %s, %s, %s)", str
[0], str
[1], str
[2], str
[3]);
381 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer
*buffer
,
382 const int *values
, unsigned int size
)
386 if (!size
|| size
> 4)
388 ERR("Invalid vector size %u.\n", size
);
393 shader_addline(buffer
, "ivec%u(", size
);
395 for (i
= 0; i
< size
; ++i
)
396 shader_addline(buffer
, i
? ", %#x" : "%#x", values
[i
]);
399 shader_addline(buffer
, ")");
402 static const char *get_info_log_line(const char **ptr
)
407 if (!(q
= strstr(p
, "\n")))
409 if (!*p
) return NULL
;
418 /* Context activation is done by the caller. */
419 void print_glsl_info_log(const struct wined3d_gl_info
*gl_info
, GLuint id
, BOOL program
)
424 if (!WARN_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
428 GL_EXTCALL(glGetProgramiv(id
, GL_INFO_LOG_LENGTH
, &length
));
430 GL_EXTCALL(glGetShaderiv(id
, GL_INFO_LOG_LENGTH
, &length
));
432 /* A size of 1 is just a null-terminated string, so the log should be bigger than
433 * that if there are errors. */
436 const char *ptr
, *line
;
438 log
= HeapAlloc(GetProcessHeap(), 0, length
);
439 /* The info log is supposed to be zero-terminated, but at least some
440 * versions of fglrx don't terminate the string properly. The reported
441 * length does include the terminator, so explicitly set it to zero
445 GL_EXTCALL(glGetProgramInfoLog(id
, length
, NULL
, log
));
447 GL_EXTCALL(glGetShaderInfoLog(id
, length
, NULL
, log
));
450 if (gl_info
->quirks
& WINED3D_QUIRK_INFO_LOG_SPAM
)
452 WARN("Info log received from GLSL shader #%u:\n", id
);
453 while ((line
= get_info_log_line(&ptr
))) WARN(" %.*s", (int)(ptr
- line
), line
);
457 FIXME("Info log received from GLSL shader #%u:\n", id
);
458 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
460 HeapFree(GetProcessHeap(), 0, log
);
464 /* Context activation is done by the caller. */
465 static void shader_glsl_compile(const struct wined3d_gl_info
*gl_info
, GLuint shader
, const char *src
)
467 const char *ptr
, *line
;
469 TRACE("Compiling shader object %u.\n", shader
);
471 if (TRACE_ON(d3d_shader
))
474 while ((line
= get_info_log_line(&ptr
))) TRACE_(d3d_shader
)(" %.*s", (int)(ptr
- line
), line
);
477 GL_EXTCALL(glShaderSource(shader
, 1, &src
, NULL
));
478 checkGLcall("glShaderSource");
479 GL_EXTCALL(glCompileShader(shader
));
480 checkGLcall("glCompileShader");
481 print_glsl_info_log(gl_info
, shader
, FALSE
);
484 /* Context activation is done by the caller. */
485 static void shader_glsl_dump_program_source(const struct wined3d_gl_info
*gl_info
, GLuint program
)
487 GLint i
, shader_count
, source_size
= -1;
491 GL_EXTCALL(glGetProgramiv(program
, GL_ATTACHED_SHADERS
, &shader_count
));
492 if (!(shaders
= wined3d_calloc(shader_count
, sizeof(*shaders
))))
494 ERR("Failed to allocate shader array memory.\n");
498 GL_EXTCALL(glGetAttachedShaders(program
, shader_count
, NULL
, shaders
));
499 for (i
= 0; i
< shader_count
; ++i
)
501 const char *ptr
, *line
;
504 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_SOURCE_LENGTH
, &tmp
));
506 if (source_size
< tmp
)
508 HeapFree(GetProcessHeap(), 0, source
);
510 source
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, tmp
);
513 ERR("Failed to allocate %d bytes for shader source.\n", tmp
);
514 HeapFree(GetProcessHeap(), 0, shaders
);
520 FIXME("Shader %u:\n", shaders
[i
]);
521 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_SHADER_TYPE
, &tmp
));
522 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp
));
523 GL_EXTCALL(glGetShaderiv(shaders
[i
], GL_COMPILE_STATUS
, &tmp
));
524 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp
);
528 GL_EXTCALL(glGetShaderSource(shaders
[i
], source_size
, NULL
, source
));
529 while ((line
= get_info_log_line(&ptr
))) FIXME(" %.*s", (int)(ptr
- line
), line
);
533 HeapFree(GetProcessHeap(), 0, source
);
534 HeapFree(GetProcessHeap(), 0, shaders
);
537 /* Context activation is done by the caller. */
538 void shader_glsl_validate_link(const struct wined3d_gl_info
*gl_info
, GLuint program
)
542 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
545 GL_EXTCALL(glGetProgramiv(program
, GL_LINK_STATUS
, &tmp
));
548 FIXME("Program %u link status invalid.\n", program
);
549 shader_glsl_dump_program_source(gl_info
, program
);
552 print_glsl_info_log(gl_info
, program
, TRUE
);
555 /* Context activation is done by the caller. */
556 static void shader_glsl_load_samplers(const struct wined3d_gl_info
*gl_info
,
557 struct shader_glsl_priv
*priv
, const DWORD
*tex_unit_map
, GLuint program_id
)
559 unsigned int mapped_unit
;
560 struct wined3d_string_buffer
*sampler_name
= string_buffer_get(&priv
->string_buffers
);
567 enum wined3d_shader_type type
;
568 unsigned int base_idx
;
573 {WINED3D_SHADER_TYPE_PIXEL
, 0, MAX_FRAGMENT_SAMPLERS
},
574 {WINED3D_SHADER_TYPE_VERTEX
, MAX_FRAGMENT_SAMPLERS
, MAX_VERTEX_SAMPLERS
},
577 for (i
= 0; i
< ARRAY_SIZE(sampler_info
); ++i
)
579 prefix
= shader_glsl_get_prefix(sampler_info
[i
].type
);
581 for (j
= 0; j
< sampler_info
[i
].count
; ++j
)
583 string_buffer_sprintf(sampler_name
, "%s_sampler%u", prefix
, j
);
584 name_loc
= GL_EXTCALL(glGetUniformLocation(program_id
, sampler_name
->buffer
));
588 mapped_unit
= tex_unit_map
[sampler_info
[i
].base_idx
+ j
];
589 if (mapped_unit
== WINED3D_UNMAPPED_STAGE
|| mapped_unit
>= gl_info
->limits
.combined_samplers
)
591 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name
->buffer
, mapped_unit
);
595 TRACE("Loading sampler %s on unit %u.\n", sampler_name
->buffer
, mapped_unit
);
596 GL_EXTCALL(glUniform1i(name_loc
, mapped_unit
));
599 checkGLcall("Load sampler bindings");
600 string_buffer_release(&priv
->string_buffers
, sampler_name
);
603 static void shader_glsl_load_icb(const struct wined3d_gl_info
*gl_info
, struct shader_glsl_priv
*priv
,
604 GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
606 const struct wined3d_shader_immediate_constant_buffer
*icb
= reg_maps
->icb
;
610 struct wined3d_string_buffer
*icb_name
= string_buffer_get(&priv
->string_buffers
);
611 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
614 string_buffer_sprintf(icb_name
, "%s_icb", prefix
);
615 icb_location
= GL_EXTCALL(glGetUniformLocation(program_id
, icb_name
->buffer
));
616 GL_EXTCALL(glUniform4fv(icb_location
, icb
->vec4_count
, (const GLfloat
*)icb
->data
));
617 checkGLcall("Load immediate constant buffer");
619 string_buffer_release(&priv
->string_buffers
, icb_name
);
623 /* Context activation is done by the caller. */
624 static void shader_glsl_load_images(const struct wined3d_gl_info
*gl_info
, struct shader_glsl_priv
*priv
,
625 GLuint program_id
, const struct wined3d_shader_reg_maps
*reg_maps
)
627 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
631 for (i
= 0; i
< MAX_UNORDERED_ACCESS_VIEWS
; ++i
)
633 if (!reg_maps
->uav_resource_info
[i
].type
)
636 string_buffer_sprintf(name
, "ps_image%u", i
);
637 location
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
641 TRACE("Loading image %s on unit %u.\n", name
->buffer
, i
);
642 GL_EXTCALL(glUniform1i(location
, i
));
644 checkGLcall("Load image bindings");
645 string_buffer_release(&priv
->string_buffers
, name
);
648 /* Context activation is done by the caller. */
649 static inline void walk_constant_heap(const struct wined3d_gl_info
*gl_info
, const struct wined3d_vec4
*constants
,
650 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
652 unsigned int start
= ~0U, end
= 0;
654 unsigned int heap_idx
= 1;
657 if (heap
->entries
[heap_idx
].version
<= version
) return;
659 idx
= heap
->entries
[heap_idx
].idx
;
660 if (constant_locations
[idx
] != -1)
662 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
664 while (stack_idx
>= 0)
666 /* Note that we fall through to the next case statement. */
667 switch(stack
[stack_idx
])
669 case HEAP_NODE_TRAVERSE_LEFT
:
671 unsigned int left_idx
= heap_idx
<< 1;
672 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
675 idx
= heap
->entries
[heap_idx
].idx
;
676 if (constant_locations
[idx
] != -1)
684 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
685 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
690 case HEAP_NODE_TRAVERSE_RIGHT
:
692 unsigned int right_idx
= (heap_idx
<< 1) + 1;
693 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
695 heap_idx
= right_idx
;
696 idx
= heap
->entries
[heap_idx
].idx
;
697 if (constant_locations
[idx
] != -1)
705 stack
[stack_idx
++] = HEAP_NODE_POP
;
706 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
718 GL_EXTCALL(glUniform4fv(constant_locations
[start
], end
- start
+ 1, &constants
[start
].x
));
719 checkGLcall("walk_constant_heap()");
722 /* Context activation is done by the caller. */
723 static inline void apply_clamped_constant(const struct wined3d_gl_info
*gl_info
,
724 GLint location
, const struct wined3d_vec4
*data
)
726 GLfloat clamped_constant
[4];
728 if (location
== -1) return;
730 clamped_constant
[0] = data
->x
< -1.0f
? -1.0f
: data
->x
> 1.0f
? 1.0f
: data
->x
;
731 clamped_constant
[1] = data
->y
< -1.0f
? -1.0f
: data
->y
> 1.0f
? 1.0f
: data
->y
;
732 clamped_constant
[2] = data
->z
< -1.0f
? -1.0f
: data
->z
> 1.0f
? 1.0f
: data
->z
;
733 clamped_constant
[3] = data
->w
< -1.0f
? -1.0f
: data
->w
> 1.0f
? 1.0f
: data
->w
;
735 GL_EXTCALL(glUniform4fv(location
, 1, clamped_constant
));
738 /* Context activation is done by the caller. */
739 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info
*gl_info
,
740 const struct wined3d_vec4
*constants
, const GLint
*constant_locations
,
741 const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
744 unsigned int heap_idx
= 1;
747 if (heap
->entries
[heap_idx
].version
<= version
) return;
749 idx
= heap
->entries
[heap_idx
].idx
;
750 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
751 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
753 while (stack_idx
>= 0)
755 /* Note that we fall through to the next case statement. */
756 switch(stack
[stack_idx
])
758 case HEAP_NODE_TRAVERSE_LEFT
:
760 unsigned int left_idx
= heap_idx
<< 1;
761 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
764 idx
= heap
->entries
[heap_idx
].idx
;
765 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
767 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
768 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
773 case HEAP_NODE_TRAVERSE_RIGHT
:
775 unsigned int right_idx
= (heap_idx
<< 1) + 1;
776 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
778 heap_idx
= right_idx
;
779 idx
= heap
->entries
[heap_idx
].idx
;
780 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
]);
782 stack
[stack_idx
++] = HEAP_NODE_POP
;
783 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
794 checkGLcall("walk_constant_heap_clamped()");
797 /* Context activation is done by the caller. */
798 static void shader_glsl_load_constants_f(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
799 const struct wined3d_vec4
*constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
800 unsigned char *stack
, unsigned int version
)
802 const struct wined3d_shader_lconst
*lconst
;
804 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
805 if (shader
->reg_maps
.shader_version
.major
== 1
806 && shader
->reg_maps
.shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
)
807 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
809 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
811 if (!shader
->load_local_constsF
)
813 TRACE("No need to load local float constants for this shader.\n");
817 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
818 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
820 GL_EXTCALL(glUniform4fv(constant_locations
[lconst
->idx
], 1, (const GLfloat
*)lconst
->value
));
822 checkGLcall("glUniform4fv()");
825 /* Context activation is done by the caller. */
826 static void shader_glsl_load_constants_i(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
827 const struct wined3d_ivec4
*constants
, const GLint locations
[WINED3D_MAX_CONSTS_I
], WORD constants_set
)
832 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
834 if (!(constants_set
& 1)) continue;
836 /* We found this uniform name in the program - go ahead and send the data */
837 GL_EXTCALL(glUniform4iv(locations
[i
], 1, &constants
[i
].x
));
840 /* Load immediate constants */
841 ptr
= list_head(&shader
->constantsI
);
844 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
845 unsigned int idx
= lconst
->idx
;
846 const GLint
*values
= (const GLint
*)lconst
->value
;
848 /* We found this uniform name in the program - go ahead and send the data */
849 GL_EXTCALL(glUniform4iv(locations
[idx
], 1, values
));
850 ptr
= list_next(&shader
->constantsI
, ptr
);
852 checkGLcall("glUniform4iv()");
855 /* Context activation is done by the caller. */
856 static void shader_glsl_load_constantsB(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
857 const GLint locations
[WINED3D_MAX_CONSTS_B
], const BOOL
*constants
, WORD constants_set
)
862 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
864 if (!(constants_set
& 1)) continue;
866 GL_EXTCALL(glUniform1iv(locations
[i
], 1, &constants
[i
]));
869 /* Load immediate constants */
870 ptr
= list_head(&shader
->constantsB
);
873 const struct wined3d_shader_lconst
*lconst
= LIST_ENTRY(ptr
, const struct wined3d_shader_lconst
, entry
);
874 unsigned int idx
= lconst
->idx
;
875 const GLint
*values
= (const GLint
*)lconst
->value
;
877 GL_EXTCALL(glUniform1iv(locations
[idx
], 1, values
));
878 ptr
= list_next(&shader
->constantsB
, ptr
);
880 checkGLcall("glUniform1iv()");
883 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
885 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
888 /* Context activation is done by the caller (state handler). */
889 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program
*ps
,
890 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
892 GLfloat np2fixup_constants
[4 * MAX_FRAGMENT_SAMPLERS
];
893 UINT fixup
= ps
->np2_fixup_info
->active
;
896 for (i
= 0; fixup
; fixup
>>= 1, ++i
)
898 const struct wined3d_texture
*tex
= state
->textures
[i
];
899 unsigned char idx
= ps
->np2_fixup_info
->idx
[i
];
900 GLfloat
*tex_dim
= &np2fixup_constants
[(idx
>> 1) * 4];
904 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
910 tex_dim
[2] = tex
->pow2_matrix
[0];
911 tex_dim
[3] = tex
->pow2_matrix
[5];
915 tex_dim
[0] = tex
->pow2_matrix
[0];
916 tex_dim
[1] = tex
->pow2_matrix
[5];
920 GL_EXTCALL(glUniform4fv(ps
->np2_fixup_location
, ps
->np2_fixup_info
->num_consts
, np2fixup_constants
));
923 /* Taken and adapted from Mesa. */
924 static BOOL
invert_matrix_3d(struct wined3d_matrix
*out
, const struct wined3d_matrix
*in
)
926 float pos
, neg
, t
, det
;
927 struct wined3d_matrix temp
;
929 /* Calculate the determinant of upper left 3x3 submatrix and
930 * determine if the matrix is singular. */
932 t
= in
->_11
* in
->_22
* in
->_33
;
938 t
= in
->_21
* in
->_32
* in
->_13
;
943 t
= in
->_31
* in
->_12
* in
->_23
;
949 t
= -in
->_31
* in
->_22
* in
->_13
;
954 t
= -in
->_21
* in
->_12
* in
->_33
;
960 t
= -in
->_11
* in
->_32
* in
->_23
;
968 if (fabsf(det
) < 1e-25f
)
972 temp
._11
= (in
->_22
* in
->_33
- in
->_32
* in
->_23
) * det
;
973 temp
._12
= -(in
->_12
* in
->_33
- in
->_32
* in
->_13
) * det
;
974 temp
._13
= (in
->_12
* in
->_23
- in
->_22
* in
->_13
) * det
;
975 temp
._21
= -(in
->_21
* in
->_33
- in
->_31
* in
->_23
) * det
;
976 temp
._22
= (in
->_11
* in
->_33
- in
->_31
* in
->_13
) * det
;
977 temp
._23
= -(in
->_11
* in
->_23
- in
->_21
* in
->_13
) * det
;
978 temp
._31
= (in
->_21
* in
->_32
- in
->_31
* in
->_22
) * det
;
979 temp
._32
= -(in
->_11
* in
->_32
- in
->_31
* in
->_12
) * det
;
980 temp
._33
= (in
->_11
* in
->_22
- in
->_21
* in
->_12
) * det
;
986 static void swap_rows(float **a
, float **b
)
994 static BOOL
invert_matrix(struct wined3d_matrix
*out
, struct wined3d_matrix
*m
)
997 float m0
, m1
, m2
, m3
, s
;
998 float *r0
, *r1
, *r2
, *r3
;
1010 r0
[5] = r0
[6] = r0
[7] = 0.0f
;
1017 r1
[4] = r1
[6] = r1
[7] = 0.0f
;
1024 r2
[4] = r2
[5] = r2
[7] = 0.0f
;
1031 r3
[4] = r3
[5] = r3
[6] = 0.0f
;
1033 /* Choose pivot - or die. */
1034 if (fabsf(r3
[0]) > fabsf(r2
[0]))
1035 swap_rows(&r3
, &r2
);
1036 if (fabsf(r2
[0]) > fabsf(r1
[0]))
1037 swap_rows(&r2
, &r1
);
1038 if (fabsf(r1
[0]) > fabsf(r0
[0]))
1039 swap_rows(&r1
, &r0
);
1043 /* Eliminate first variable. */
1044 m1
= r1
[0] / r0
[0]; m2
= r2
[0] / r0
[0]; m3
= r3
[0] / r0
[0];
1045 s
= r0
[1]; r1
[1] -= m1
* s
; r2
[1] -= m2
* s
; r3
[1] -= m3
* s
;
1046 s
= r0
[2]; r1
[2] -= m1
* s
; r2
[2] -= m2
* s
; r3
[2] -= m3
* s
;
1047 s
= r0
[3]; r1
[3] -= m1
* s
; r2
[3] -= m2
* s
; r3
[3] -= m3
* s
;
1077 /* Choose pivot - or die. */
1078 if (fabsf(r3
[1]) > fabsf(r2
[1]))
1079 swap_rows(&r3
, &r2
);
1080 if (fabsf(r2
[1]) > fabsf(r1
[1]))
1081 swap_rows(&r2
, &r1
);
1085 /* Eliminate second variable. */
1086 m2
= r2
[1] / r1
[1]; m3
= r3
[1] / r1
[1];
1087 r2
[2] -= m2
* r1
[2]; r3
[2] -= m3
* r1
[2];
1088 r2
[3] -= m2
* r1
[3]; r3
[3] -= m3
* r1
[3];
1114 /* Choose pivot - or die. */
1115 if (fabsf(r3
[2]) > fabsf(r2
[2]))
1116 swap_rows(&r3
, &r2
);
1120 /* Eliminate third variable. */
1122 r3
[3] -= m3
* r2
[3];
1123 r3
[4] -= m3
* r2
[4];
1124 r3
[5] -= m3
* r2
[5];
1125 r3
[6] -= m3
* r2
[6];
1126 r3
[7] -= m3
* r2
[7];
1132 /* Back substitute row 3. */
1139 /* Back substitute row 2. */
1142 r2
[4] = s
* (r2
[4] - r3
[4] * m2
);
1143 r2
[5] = s
* (r2
[5] - r3
[5] * m2
);
1144 r2
[6] = s
* (r2
[6] - r3
[6] * m2
);
1145 r2
[7] = s
* (r2
[7] - r3
[7] * m2
);
1147 r1
[4] -= r3
[4] * m1
;
1148 r1
[5] -= r3
[5] * m1
;
1149 r1
[6] -= r3
[6] * m1
;
1150 r1
[7] -= r3
[7] * m1
;
1152 r0
[4] -= r3
[4] * m0
;
1153 r0
[5] -= r3
[5] * m0
;
1154 r0
[6] -= r3
[6] * m0
;
1155 r0
[7] -= r3
[7] * m0
;
1157 /* Back substitute row 1. */
1160 r1
[4] = s
* (r1
[4] - r2
[4] * m1
);
1161 r1
[5] = s
* (r1
[5] - r2
[5] * m1
);
1162 r1
[6] = s
* (r1
[6] - r2
[6] * m1
);
1163 r1
[7] = s
* (r1
[7] - r2
[7] * m1
);
1165 r0
[4] -= r2
[4] * m0
;
1166 r0
[5] -= r2
[5] * m0
;
1167 r0
[6] -= r2
[6] * m0
;
1168 r0
[7] -= r2
[7] * m0
;
1170 /* Back substitute row 0. */
1173 r0
[4] = s
* (r0
[4] - r1
[4] * m0
);
1174 r0
[5] = s
* (r0
[5] - r1
[5] * m0
);
1175 r0
[6] = s
* (r0
[6] - r1
[6] * m0
);
1176 r0
[7] = s
* (r0
[7] - r1
[7] * m0
);
1198 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context
*context
,
1199 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1201 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1203 struct wined3d_matrix mv
;
1206 if (prog
->vs
.normal_matrix_location
== -1)
1209 get_modelview_matrix(context
, state
, 0, &mv
);
1210 if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_LEGACY_FFP_LIGHTING
)
1211 invert_matrix_3d(&mv
, &mv
);
1213 invert_matrix(&mv
, &mv
);
1214 /* Tests show that singular modelview matrices are used unchanged as normal
1215 * matrices on D3D3 and older. There seems to be no clearly consistent
1216 * behavior on newer D3D versions so always follow older ddraw behavior. */
1217 for (i
= 0; i
< 3; ++i
)
1218 for (j
= 0; j
< 3; ++j
)
1219 mat
[i
* 3 + j
] = (&mv
._11
)[j
* 4 + i
];
1221 GL_EXTCALL(glUniformMatrix3fv(prog
->vs
.normal_matrix_location
, 1, FALSE
, mat
));
1222 checkGLcall("glUniformMatrix3fv");
1225 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context
*context
,
1226 const struct wined3d_state
*state
, unsigned int tex
, struct glsl_shader_prog_link
*prog
)
1228 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1229 struct wined3d_matrix mat
;
1231 if (tex
>= MAX_TEXTURES
)
1233 if (prog
->vs
.texture_matrix_location
[tex
] == -1)
1236 get_texture_matrix(context
, state
, tex
, &mat
);
1237 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.texture_matrix_location
[tex
], 1, FALSE
, &mat
._11
));
1238 checkGLcall("glUniformMatrix4fv");
1241 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context
*context
,
1242 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1244 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1246 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1248 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, &state
->material
.specular
.r
));
1249 GL_EXTCALL(glUniform1f(prog
->vs
.material_shininess_location
, state
->material
.power
));
1253 static const float black
[] = {0.0f
, 0.0f
, 0.0f
, 0.0f
};
1255 GL_EXTCALL(glUniform4fv(prog
->vs
.material_specular_location
, 1, black
));
1257 GL_EXTCALL(glUniform4fv(prog
->vs
.material_ambient_location
, 1, &state
->material
.ambient
.r
));
1258 GL_EXTCALL(glUniform4fv(prog
->vs
.material_diffuse_location
, 1, &state
->material
.diffuse
.r
));
1259 GL_EXTCALL(glUniform4fv(prog
->vs
.material_emissive_location
, 1, &state
->material
.emissive
.r
));
1260 checkGLcall("setting FFP material uniforms");
1263 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context
*context
,
1264 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1266 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1267 struct wined3d_color color
;
1269 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_AMBIENT
]);
1270 GL_EXTCALL(glUniform3fv(prog
->vs
.light_ambient_location
, 1, &color
.r
));
1271 checkGLcall("glUniform3fv");
1274 static void multiply_vector_matrix(struct wined3d_vec4
*dest
, const struct wined3d_vec4
*src1
,
1275 const struct wined3d_matrix
*src2
)
1277 struct wined3d_vec4 temp
;
1279 temp
.x
= (src1
->x
* src2
->_11
) + (src1
->y
* src2
->_21
) + (src1
->z
* src2
->_31
) + (src1
->w
* src2
->_41
);
1280 temp
.y
= (src1
->x
* src2
->_12
) + (src1
->y
* src2
->_22
) + (src1
->z
* src2
->_32
) + (src1
->w
* src2
->_42
);
1281 temp
.z
= (src1
->x
* src2
->_13
) + (src1
->y
* src2
->_23
) + (src1
->z
* src2
->_33
) + (src1
->w
* src2
->_43
);
1282 temp
.w
= (src1
->x
* src2
->_14
) + (src1
->y
* src2
->_24
) + (src1
->z
* src2
->_34
) + (src1
->w
* src2
->_44
);
1287 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context
*context
,
1288 const struct wined3d_state
*state
, unsigned int light
, struct glsl_shader_prog_link
*prog
)
1290 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1291 const struct wined3d_light_info
*light_info
= state
->lights
[light
];
1292 struct wined3d_vec4 vec4
;
1293 const struct wined3d_matrix
*view
= &state
->transforms
[WINED3D_TS_VIEW
];
1298 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].diffuse
, 1, &light_info
->OriginalParms
.diffuse
.r
));
1299 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].specular
, 1, &light_info
->OriginalParms
.specular
.r
));
1300 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].ambient
, 1, &light_info
->OriginalParms
.ambient
.r
));
1302 switch (light_info
->OriginalParms
.type
)
1304 case WINED3D_LIGHT_POINT
:
1305 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1306 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1307 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1308 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1309 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1310 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1313 case WINED3D_LIGHT_SPOT
:
1314 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1315 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1317 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1318 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1320 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].range
, light_info
->OriginalParms
.range
));
1321 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].falloff
, light_info
->OriginalParms
.falloff
));
1322 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].c_att
, light_info
->OriginalParms
.attenuation0
));
1323 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].l_att
, light_info
->OriginalParms
.attenuation1
));
1324 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].q_att
, light_info
->OriginalParms
.attenuation2
));
1325 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_htheta
, cosf(light_info
->OriginalParms
.theta
/ 2.0f
)));
1326 GL_EXTCALL(glUniform1f(prog
->vs
.light_location
[light
].cos_hphi
, cosf(light_info
->OriginalParms
.phi
/ 2.0f
)));
1329 case WINED3D_LIGHT_DIRECTIONAL
:
1330 multiply_vector_matrix(&vec4
, &light_info
->direction
, view
);
1331 GL_EXTCALL(glUniform3fv(prog
->vs
.light_location
[light
].direction
, 1, &vec4
.x
));
1334 case WINED3D_LIGHT_PARALLELPOINT
:
1335 multiply_vector_matrix(&vec4
, &light_info
->position
, view
);
1336 GL_EXTCALL(glUniform4fv(prog
->vs
.light_location
[light
].position
, 1, &vec4
.x
));
1340 FIXME("Unrecognized light type %#x.\n", light_info
->OriginalParms
.type
);
1342 checkGLcall("setting FFP lights uniforms");
1345 static void shader_glsl_pointsize_uniform(const struct wined3d_context
*context
,
1346 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1348 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1352 get_pointsize_minmax(context
, state
, &min
, &max
);
1354 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_min_location
, min
));
1355 checkGLcall("glUniform1f");
1356 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_max_location
, max
));
1357 checkGLcall("glUniform1f");
1359 get_pointsize(context
, state
, &size
, att
);
1361 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_location
, size
));
1362 checkGLcall("glUniform1f");
1363 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_c_att_location
, att
[0]));
1364 checkGLcall("glUniform1f");
1365 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_l_att_location
, att
[1]));
1366 checkGLcall("glUniform1f");
1367 GL_EXTCALL(glUniform1f(prog
->vs
.pointsize_q_att_location
, att
[2]));
1368 checkGLcall("glUniform1f");
1371 static void shader_glsl_load_fog_uniform(const struct wined3d_context
*context
,
1372 const struct wined3d_state
*state
, struct glsl_shader_prog_link
*prog
)
1374 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1375 struct wined3d_color color
;
1376 float start
, end
, scale
;
1383 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_FOGCOLOR
]);
1384 GL_EXTCALL(glUniform4fv(prog
->ps
.fog_color_location
, 1, &color
.r
));
1385 tmpvalue
.d
= state
->render_states
[WINED3D_RS_FOGDENSITY
];
1386 GL_EXTCALL(glUniform1f(prog
->ps
.fog_density_location
, tmpvalue
.f
));
1387 get_fog_start_end(context
, state
, &start
, &end
);
1388 scale
= 1.0f
/ (end
- start
);
1389 GL_EXTCALL(glUniform1f(prog
->ps
.fog_end_location
, end
));
1390 GL_EXTCALL(glUniform1f(prog
->ps
.fog_scale_location
, scale
));
1391 checkGLcall("fog emulation uniforms");
1394 static void shader_glsl_clip_plane_uniform(const struct wined3d_context
*context
,
1395 const struct wined3d_state
*state
, unsigned int index
, struct glsl_shader_prog_link
*prog
)
1397 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1398 struct wined3d_vec4 plane
;
1400 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1402 multiply_vector_matrix(&plane
, &state
->clip_planes
[index
], &state
->transforms
[WINED3D_TS_VIEW
]);
1404 plane
= state
->clip_planes
[index
];
1406 GL_EXTCALL(glUniform4fv(prog
->vs
.clip_planes_location
+ index
, 1, &plane
.x
));
1409 /* Context activation is done by the caller (state handler). */
1410 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program
*ps
,
1411 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
1413 struct wined3d_color float_key
[2];
1414 const struct wined3d_texture
*texture
= state
->textures
[0];
1416 wined3d_format_get_float_color_key(texture
->resource
.format
, &texture
->async
.src_blt_color_key
, float_key
);
1417 GL_EXTCALL(glUniform4fv(ps
->color_key_location
, 2, &float_key
[0].r
));
1420 /* Context activation is done by the caller (state handler). */
1421 static void shader_glsl_load_constants(void *shader_priv
, struct wined3d_context
*context
,
1422 const struct wined3d_state
*state
)
1424 const struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
1425 const struct wined3d_shader
*vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
1426 const struct wined3d_shader
*pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
1427 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1428 struct shader_glsl_priv
*priv
= shader_priv
;
1429 float position_fixup
[4];
1432 struct glsl_shader_prog_link
*prog
= ctx_data
->glsl_program
;
1433 UINT constant_version
;
1437 /* No GLSL program set - nothing to do. */
1440 constant_version
= prog
->constant_version
;
1441 update_mask
= context
->constant_update_mask
& prog
->constant_update_mask
;
1443 if (update_mask
& WINED3D_SHADER_CONST_VS_F
)
1444 shader_glsl_load_constants_f(vshader
, gl_info
, state
->vs_consts_f
,
1445 prog
->vs
.uniform_f_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
1447 if (update_mask
& WINED3D_SHADER_CONST_VS_I
)
1448 shader_glsl_load_constants_i(vshader
, gl_info
, state
->vs_consts_i
,
1449 prog
->vs
.uniform_i_locations
, vshader
->reg_maps
.integer_constants
);
1451 if (update_mask
& WINED3D_SHADER_CONST_VS_B
)
1452 shader_glsl_load_constantsB(vshader
, gl_info
, prog
->vs
.uniform_b_locations
, state
->vs_consts_b
,
1453 vshader
->reg_maps
.boolean_constants
);
1455 if (update_mask
& WINED3D_SHADER_CONST_VS_CLIP_PLANES
)
1457 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
1458 shader_glsl_clip_plane_uniform(context
, state
, i
, prog
);
1461 if (update_mask
& WINED3D_SHADER_CONST_VS_POINTSIZE
)
1462 shader_glsl_pointsize_uniform(context
, state
, prog
);
1464 if (update_mask
& WINED3D_SHADER_CONST_POS_FIXUP
)
1466 shader_get_position_fixup(context
, state
, position_fixup
);
1467 if (state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
])
1468 GL_EXTCALL(glUniform4fv(prog
->gs
.pos_fixup_location
, 1, position_fixup
));
1470 GL_EXTCALL(glUniform4fv(prog
->vs
.pos_fixup_location
, 1, position_fixup
));
1471 checkGLcall("glUniform4fv");
1474 if (update_mask
& WINED3D_SHADER_CONST_FFP_MODELVIEW
)
1476 struct wined3d_matrix mat
;
1478 get_modelview_matrix(context
, state
, 0, &mat
);
1479 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[0], 1, FALSE
, &mat
._11
));
1480 checkGLcall("glUniformMatrix4fv");
1482 shader_glsl_ffp_vertex_normalmatrix_uniform(context
, state
, prog
);
1485 if (update_mask
& WINED3D_SHADER_CONST_FFP_VERTEXBLEND
)
1487 struct wined3d_matrix mat
;
1489 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
1491 if (prog
->vs
.modelview_matrix_location
[i
] == -1)
1494 get_modelview_matrix(context
, state
, i
, &mat
);
1495 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.modelview_matrix_location
[i
], 1, FALSE
, &mat
._11
));
1496 checkGLcall("glUniformMatrix4fv");
1500 if (update_mask
& WINED3D_SHADER_CONST_FFP_PROJ
)
1502 struct wined3d_matrix projection
;
1504 get_projection_matrix(context
, state
, &projection
);
1505 GL_EXTCALL(glUniformMatrix4fv(prog
->vs
.projection_matrix_location
, 1, FALSE
, &projection
._11
));
1506 checkGLcall("glUniformMatrix4fv");
1509 if (update_mask
& WINED3D_SHADER_CONST_FFP_TEXMATRIX
)
1511 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1512 shader_glsl_ffp_vertex_texmatrix_uniform(context
, state
, i
, prog
);
1515 if (update_mask
& WINED3D_SHADER_CONST_FFP_MATERIAL
)
1516 shader_glsl_ffp_vertex_material_uniform(context
, state
, prog
);
1518 if (update_mask
& WINED3D_SHADER_CONST_FFP_LIGHTS
)
1520 shader_glsl_ffp_vertex_lightambient_uniform(context
, state
, prog
);
1521 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
1522 shader_glsl_ffp_vertex_light_uniform(context
, state
, i
, prog
);
1525 if (update_mask
& WINED3D_SHADER_CONST_PS_F
)
1526 shader_glsl_load_constants_f(pshader
, gl_info
, state
->ps_consts_f
,
1527 prog
->ps
.uniform_f_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
1529 if (update_mask
& WINED3D_SHADER_CONST_PS_I
)
1530 shader_glsl_load_constants_i(pshader
, gl_info
, state
->ps_consts_i
,
1531 prog
->ps
.uniform_i_locations
, pshader
->reg_maps
.integer_constants
);
1533 if (update_mask
& WINED3D_SHADER_CONST_PS_B
)
1534 shader_glsl_load_constantsB(pshader
, gl_info
, prog
->ps
.uniform_b_locations
, state
->ps_consts_b
,
1535 pshader
->reg_maps
.boolean_constants
);
1537 if (update_mask
& WINED3D_SHADER_CONST_PS_BUMP_ENV
)
1539 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1541 if (prog
->ps
.bumpenv_mat_location
[i
] == -1)
1544 GL_EXTCALL(glUniformMatrix2fv(prog
->ps
.bumpenv_mat_location
[i
], 1, 0,
1545 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_MAT00
]));
1547 if (prog
->ps
.bumpenv_lum_scale_location
[i
] != -1)
1549 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_scale_location
[i
], 1,
1550 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LSCALE
]));
1551 GL_EXTCALL(glUniform1fv(prog
->ps
.bumpenv_lum_offset_location
[i
], 1,
1552 (const GLfloat
*)&state
->texture_states
[i
][WINED3D_TSS_BUMPENV_LOFFSET
]));
1556 checkGLcall("bump env uniforms");
1559 if (update_mask
& WINED3D_SHADER_CONST_PS_Y_CORR
)
1561 const struct wined3d_vec4 correction_params
=
1563 /* Position is relative to the framebuffer, not the viewport. */
1564 context
->render_offscreen
? 0.0f
: (float)state
->fb
->render_targets
[0]->height
,
1565 context
->render_offscreen
? 1.0f
: -1.0f
,
1570 GL_EXTCALL(glUniform4fv(prog
->ps
.ycorrection_location
, 1, &correction_params
.x
));
1573 if (update_mask
& WINED3D_SHADER_CONST_PS_NP2_FIXUP
)
1574 shader_glsl_load_np2fixup_constants(&prog
->ps
, gl_info
, state
);
1575 if (update_mask
& WINED3D_SHADER_CONST_FFP_COLOR_KEY
)
1576 shader_glsl_load_color_key_constant(&prog
->ps
, gl_info
, state
);
1578 if (update_mask
& WINED3D_SHADER_CONST_FFP_PS
)
1580 struct wined3d_color color
;
1582 if (prog
->ps
.tex_factor_location
!= -1)
1584 wined3d_color_from_d3dcolor(&color
, state
->render_states
[WINED3D_RS_TEXTUREFACTOR
]);
1585 GL_EXTCALL(glUniform4fv(prog
->ps
.tex_factor_location
, 1, &color
.r
));
1588 if (state
->render_states
[WINED3D_RS_SPECULARENABLE
])
1589 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 1.0f
, 1.0f
, 1.0f
, 0.0f
));
1591 GL_EXTCALL(glUniform4f(prog
->ps
.specular_enable_location
, 0.0f
, 0.0f
, 0.0f
, 0.0f
));
1593 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
1595 if (prog
->ps
.tss_constant_location
[i
] == -1)
1598 wined3d_color_from_d3dcolor(&color
, state
->texture_states
[i
][WINED3D_TSS_CONSTANT
]);
1599 GL_EXTCALL(glUniform4fv(prog
->ps
.tss_constant_location
[i
], 1, &color
.r
));
1602 checkGLcall("fixed function uniforms");
1605 if (update_mask
& WINED3D_SHADER_CONST_PS_FOG
)
1606 shader_glsl_load_fog_uniform(context
, state
, prog
);
1608 if (update_mask
& WINED3D_SHADER_CONST_PS_ALPHA_TEST
)
1610 float ref
= state
->render_states
[WINED3D_RS_ALPHAREF
] / 255.0f
;
1612 GL_EXTCALL(glUniform1f(prog
->ps
.alpha_test_ref_location
, ref
));
1613 checkGLcall("alpha test emulation uniform");
1616 if (priv
->next_constant_version
== UINT_MAX
)
1618 TRACE("Max constant version reached, resetting to 0.\n");
1619 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
1620 priv
->next_constant_version
= 1;
1624 prog
->constant_version
= priv
->next_constant_version
++;
1628 static void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
, DWORD new_version
)
1630 struct constant_entry
*entries
= heap
->entries
;
1631 unsigned int *positions
= heap
->positions
;
1632 unsigned int heap_idx
, parent_idx
;
1634 if (!heap
->contained
[idx
])
1636 heap_idx
= heap
->size
++;
1637 heap
->contained
[idx
] = TRUE
;
1641 heap_idx
= positions
[idx
];
1644 while (heap_idx
> 1)
1646 parent_idx
= heap_idx
>> 1;
1648 if (new_version
<= entries
[parent_idx
].version
) break;
1650 entries
[heap_idx
] = entries
[parent_idx
];
1651 positions
[entries
[parent_idx
].idx
] = heap_idx
;
1652 heap_idx
= parent_idx
;
1655 entries
[heap_idx
].version
= new_version
;
1656 entries
[heap_idx
].idx
= idx
;
1657 positions
[idx
] = heap_idx
;
1660 static void shader_glsl_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1662 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1663 struct constant_heap
*heap
= &priv
->vconst_heap
;
1666 for (i
= start
; i
< count
+ start
; ++i
)
1668 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1672 static void shader_glsl_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
1674 struct shader_glsl_priv
*priv
= device
->shader_priv
;
1675 struct constant_heap
*heap
= &priv
->pconst_heap
;
1678 for (i
= start
; i
< count
+ start
; ++i
)
1680 update_heap_entry(heap
, i
, priv
->next_constant_version
);
1684 static unsigned int vec4_varyings(DWORD shader_major
, const struct wined3d_gl_info
*gl_info
)
1686 unsigned int ret
= gl_info
->limits
.glsl_varyings
/ 4;
1687 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1688 if(shader_major
> 3) return ret
;
1690 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1691 if (gl_info
->quirks
& WINED3D_QUIRK_GLSL_CLIP_VARYING
) ret
-= 1;
1695 static BOOL
needs_legacy_glsl_syntax(const struct wined3d_gl_info
*gl_info
)
1697 return gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
1700 static BOOL
shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info
*gl_info
)
1702 return !needs_legacy_glsl_syntax(gl_info
) && gl_info
->supported
[ARB_EXPLICIT_ATTRIB_LOCATION
];
1705 static const char *get_attribute_keyword(const struct wined3d_gl_info
*gl_info
)
1707 return needs_legacy_glsl_syntax(gl_info
) ? "attribute" : "in";
1710 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info
*gl_info
,
1711 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1716 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1717 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "in");
1720 va_start(args
, format
);
1721 ret
= shader_vaddline(buffer
, format
, args
);
1725 if (!string_buffer_resize(buffer
, ret
))
1730 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info
*gl_info
,
1731 struct wined3d_string_buffer
*buffer
, BOOL flat
, const char *format
, ...)
1736 shader_addline(buffer
, "%s%s ", flat
? "flat " : "",
1737 needs_legacy_glsl_syntax(gl_info
) ? "varying" : "out");
1740 va_start(args
, format
);
1741 ret
= shader_vaddline(buffer
, format
, args
);
1745 if (!string_buffer_resize(buffer
, ret
))
1750 static const char *get_fragment_output(const struct wined3d_gl_info
*gl_info
)
1752 return needs_legacy_glsl_syntax(gl_info
) ? "gl_FragData" : "ps_out";
1755 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type
)
1757 switch (primitive_type
)
1759 case WINED3D_PT_POINTLIST
:
1762 case WINED3D_PT_LINELIST
:
1765 case WINED3D_PT_LINESTRIP
:
1766 return "line_strip";
1768 case WINED3D_PT_TRIANGLELIST
:
1771 case WINED3D_PT_TRIANGLESTRIP
:
1772 return "triangle_strip";
1774 case WINED3D_PT_LINELIST_ADJ
:
1775 return "lines_adjacency";
1777 case WINED3D_PT_TRIANGLELIST_ADJ
:
1778 return "triangles_adjacency";
1781 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type
));
1786 static BOOL
glsl_is_color_reg_read(const struct wined3d_shader
*shader
, unsigned int idx
)
1788 const struct wined3d_shader_signature
*input_signature
= &shader
->input_signature
;
1789 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
1790 const BOOL
*input_reg_used
= shader
->u
.ps
.input_reg_used
;
1793 if (reg_maps
->shader_version
.major
< 3)
1794 return input_reg_used
[idx
];
1796 for (i
= 0; i
< input_signature
->element_count
; ++i
)
1798 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
1800 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
1803 if (shader_match_semantic(input
->semantic_name
, WINED3D_DECL_USAGE_COLOR
)
1804 && input
->semantic_idx
== idx
)
1806 if (input_reg_used
[input
->register_idx
])
1815 static BOOL
glsl_is_shadow_sampler(const struct wined3d_shader
*shader
,
1816 const struct ps_compile_args
*ps_args
, unsigned int resource_idx
, unsigned int sampler_idx
)
1818 const struct wined3d_shader_version
*version
= &shader
->reg_maps
.shader_version
;
1820 if (version
->major
>= 4)
1821 return shader
->reg_maps
.sampler_comparison_mode
& (1u << sampler_idx
);
1823 return version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& (ps_args
->shadow
& (1u << resource_idx
));
1826 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer
*buffer
,
1827 const struct wined3d_gl_info
*gl_info
, const char *vector_type
, const char *scalar_type
,
1830 shader_addline(buffer
, "%s %s4 vs_in_%s%u;\n",
1831 get_attribute_keyword(gl_info
), vector_type
, scalar_type
, index
);
1832 shader_addline(buffer
, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1833 index
, scalar_type
, scalar_type
, index
);
1836 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer
*buffer
,
1837 const struct wined3d_gl_info
*gl_info
, const struct wined3d_shader_signature_element
*e
)
1839 unsigned int index
= e
->register_idx
;
1841 if (e
->sysval_semantic
== WINED3D_SV_VERTEX_ID
)
1843 shader_addline(buffer
, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1847 if (e
->sysval_semantic
== WINED3D_SV_INSTANCE_ID
)
1849 shader_addline(buffer
, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1853 if (e
->sysval_semantic
&& e
->sysval_semantic
!= WINED3D_SV_POSITION
)
1854 FIXME("Unhandled sysval semantic %#x.\n", e
->sysval_semantic
);
1856 if (shader_glsl_use_explicit_attrib_location(gl_info
))
1857 shader_addline(buffer
, "layout(location = %u) ", index
);
1859 switch (e
->component_type
)
1861 case WINED3D_TYPE_UINT
:
1862 shader_glsl_declare_typed_vertex_attribute(buffer
, gl_info
, "uvec", "uint", index
);
1864 case WINED3D_TYPE_INT
:
1865 shader_glsl_declare_typed_vertex_attribute(buffer
, gl_info
, "ivec", "int", index
);
1869 FIXME("Unhandled type %#x.\n", e
->component_type
);
1871 case WINED3D_TYPE_UNKNOWN
:
1872 case WINED3D_TYPE_FLOAT
:
1873 shader_addline(buffer
, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info
), index
);
1878 /** Generate the variable & register declarations for the GLSL output target */
1879 static void shader_generate_glsl_declarations(const struct wined3d_context
*context
,
1880 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
1881 const struct wined3d_shader_reg_maps
*reg_maps
, const struct shader_glsl_ctx_priv
*ctx_priv
)
1883 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
1884 const struct vs_compile_args
*vs_args
= ctx_priv
->cur_vs_args
;
1885 const struct ps_compile_args
*ps_args
= ctx_priv
->cur_ps_args
;
1886 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1887 const struct wined3d_shader_indexable_temp
*idx_temp_reg
;
1888 unsigned int i
, extra_constants_needed
= 0;
1889 const struct wined3d_shader_lconst
*lconst
;
1893 prefix
= shader_glsl_get_prefix(version
->type
);
1895 /* Prototype the subroutines */
1896 for (i
= 0, map
= reg_maps
->labels
; map
; map
>>= 1, ++i
)
1898 if (map
& 1) shader_addline(buffer
, "void subroutine%u();\n", i
);
1901 /* Declare the constants (aka uniforms) */
1902 if (shader
->limits
->constant_float
> 0)
1904 unsigned max_constantsF
;
1906 /* Unless the shader uses indirect addressing, always declare the
1907 * maximum array size and ignore that we need some uniforms privately.
1908 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1909 * and immediate values, still declare VC[256]. If the shader needs
1910 * more uniforms than we have it won't work in any case. If it uses
1911 * less, the compiler will figure out which uniforms are really used
1912 * and strip them out. This allows a shader to use c255 on a dx9 card,
1913 * as long as it doesn't also use all the other constants.
1915 * If the shader uses indirect addressing the compiler must assume
1916 * that all declared uniforms are used. In this case, declare only the
1917 * amount that we're assured to have.
1919 * Thus we run into problems in these two cases:
1920 * 1) The shader really uses more uniforms than supported.
1921 * 2) The shader uses indirect addressing, less constants than
1922 * supported, but uses a constant index > #supported consts. */
1923 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
1925 /* No indirect addressing here. */
1926 max_constantsF
= gl_info
->limits
.glsl_ps_float_constants
;
1930 if (reg_maps
->usesrelconstF
)
1932 /* Subtract the other potential uniforms from the max
1933 * available (bools, ints, and 1 row of projection matrix).
1934 * Subtract another uniform for immediate values, which have
1935 * to be loaded via uniform by the driver as well. The shader
1936 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1937 * shader code, so one vec4 should be enough. (Unfortunately
1938 * the Nvidia driver doesn't store 128 and -128 in one float).
1940 * Writing gl_ClipVertex requires one uniform for each
1941 * clipplane as well. */
1942 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
- 3;
1943 if (vs_args
->clip_enabled
)
1944 max_constantsF
-= gl_info
->limits
.user_clip_distances
;
1945 max_constantsF
-= wined3d_popcount(reg_maps
->integer_constants
);
1946 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1947 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1948 * for now take this into account when calculating the number of available constants
1950 max_constantsF
-= wined3d_popcount(reg_maps
->boolean_constants
);
1951 /* Set by driver quirks in directx.c */
1952 max_constantsF
-= gl_info
->reserved_glsl_constants
;
1954 if (max_constantsF
< shader
->limits
->constant_float
)
1956 static unsigned int once
;
1959 ERR_(winediag
)("The hardware does not support enough uniform components to run this shader,"
1960 " it may not render correctly.\n");
1962 WARN("The hardware does not support enough uniform components to run this shader.\n");
1967 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
;
1970 max_constantsF
= min(shader
->limits
->constant_float
, max_constantsF
);
1971 shader_addline(buffer
, "uniform vec4 %s_c[%u];\n", prefix
, max_constantsF
);
1974 /* Always declare the full set of constants, the compiler can remove the
1975 * unused ones because d3d doesn't (yet) support indirect int and bool
1976 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1977 if (shader
->limits
->constant_int
> 0 && reg_maps
->integer_constants
)
1978 shader_addline(buffer
, "uniform ivec4 %s_i[%u];\n", prefix
, shader
->limits
->constant_int
);
1980 if (shader
->limits
->constant_bool
> 0 && reg_maps
->boolean_constants
)
1981 shader_addline(buffer
, "uniform bool %s_b[%u];\n", prefix
, shader
->limits
->constant_bool
);
1983 /* Declare immediate constant buffer */
1985 shader_addline(buffer
, "uniform vec4 %s_icb[%u];\n", prefix
, reg_maps
->icb
->vec4_count
);
1987 /* Declare constant buffers */
1988 for (i
= 0; i
< WINED3D_MAX_CBS
; ++i
)
1990 if (reg_maps
->cb_sizes
[i
])
1991 shader_addline(buffer
, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1992 prefix
, i
, prefix
, i
, reg_maps
->cb_sizes
[i
]);
1995 /* Declare texture samplers */
1996 for (i
= 0; i
< reg_maps
->sampler_map
.count
; ++i
)
1998 struct wined3d_shader_sampler_map_entry
*entry
;
1999 const char *sampler_type_prefix
, *sampler_type
;
2000 BOOL shadow_sampler
, tex_rect
;
2002 entry
= ®_maps
->sampler_map
.entries
[i
];
2004 if (entry
->resource_idx
>= ARRAY_SIZE(reg_maps
->resource_info
))
2006 ERR("Invalid resource index %u.\n", entry
->resource_idx
);
2010 switch (reg_maps
->resource_info
[entry
->resource_idx
].data_type
)
2012 case WINED3D_DATA_FLOAT
:
2013 case WINED3D_DATA_UNORM
:
2014 case WINED3D_DATA_SNORM
:
2015 sampler_type_prefix
= "";
2018 case WINED3D_DATA_INT
:
2019 sampler_type_prefix
= "i";
2022 case WINED3D_DATA_UINT
:
2023 sampler_type_prefix
= "u";
2027 sampler_type_prefix
= "";
2028 ERR("Unhandled resource data type %#x.\n", reg_maps
->resource_info
[i
].data_type
);
2032 shadow_sampler
= glsl_is_shadow_sampler(shader
, ps_args
, entry
->resource_idx
, entry
->sampler_idx
);
2033 switch (reg_maps
->resource_info
[entry
->resource_idx
].type
)
2035 case WINED3D_SHADER_RESOURCE_TEXTURE_1D
:
2037 sampler_type
= "sampler1DShadow";
2039 sampler_type
= "sampler1D";
2042 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2043 tex_rect
= version
->type
== WINED3D_SHADER_TYPE_PIXEL
2044 && (ps_args
->np2_fixup
& (1u << entry
->resource_idx
))
2045 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
2049 sampler_type
= "sampler2DRectShadow";
2051 sampler_type
= "sampler2DShadow";
2056 sampler_type
= "sampler2DRect";
2058 sampler_type
= "sampler2D";
2062 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2064 FIXME("Unsupported 3D shadow sampler.\n");
2065 sampler_type
= "sampler3D";
2068 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
:
2070 FIXME("Unsupported Cube shadow sampler.\n");
2071 sampler_type
= "samplerCube";
2074 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
2076 sampler_type
= "sampler2DArrayShadow";
2078 sampler_type
= "sampler2DArray";
2082 sampler_type
= "unsupported_sampler";
2083 FIXME("Unhandled resource type %#x.\n", reg_maps
->resource_info
[entry
->resource_idx
].type
);
2086 shader_addline(buffer
, "uniform %s%s %s_sampler%u;\n",
2087 sampler_type_prefix
, sampler_type
, prefix
, entry
->bind_idx
);
2090 /* Declare images */
2091 for (i
= 0; i
< ARRAY_SIZE(reg_maps
->uav_resource_info
); ++i
)
2093 const char *image_type_prefix
, *image_type
, *read_format
;
2095 if (!reg_maps
->uav_resource_info
[i
].type
)
2098 switch (reg_maps
->uav_resource_info
[i
].data_type
)
2100 case WINED3D_DATA_FLOAT
:
2101 case WINED3D_DATA_UNORM
:
2102 case WINED3D_DATA_SNORM
:
2103 image_type_prefix
= "";
2104 read_format
= "r32f";
2107 case WINED3D_DATA_INT
:
2108 image_type_prefix
= "i";
2109 read_format
= "r32i";
2112 case WINED3D_DATA_UINT
:
2113 image_type_prefix
= "u";
2114 read_format
= "r32ui";
2118 image_type_prefix
= "";
2120 ERR("Unhandled resource data type %#x.\n", reg_maps
->uav_resource_info
[i
].data_type
);
2124 switch (reg_maps
->uav_resource_info
[i
].type
)
2126 case WINED3D_SHADER_RESOURCE_BUFFER
:
2127 image_type
= "imageBuffer";
2130 case WINED3D_SHADER_RESOURCE_TEXTURE_2D
:
2131 image_type
= "image2D";
2134 case WINED3D_SHADER_RESOURCE_TEXTURE_3D
:
2135 image_type
= "image3D";
2138 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
:
2139 image_type
= "image2DArray";
2143 image_type
= "unsupported_image";
2144 FIXME("Unhandled resource type %#x.\n", reg_maps
->uav_resource_info
[i
].type
);
2148 if (reg_maps
->uav_read_mask
& (1u << i
))
2149 shader_addline(buffer
, "layout(%s) uniform %s%s %s_image%u;\n",
2150 read_format
, image_type_prefix
, image_type
, prefix
, i
);
2152 shader_addline(buffer
, "writeonly uniform %s%s %s_image%u;\n",
2153 image_type_prefix
, image_type
, prefix
, i
);
2156 /* Declare uniforms for NP2 texcoord fixup:
2157 * This is NOT done inside the loop that declares the texture samplers
2158 * since the NP2 fixup code is currently only used for the GeforceFX
2159 * series and when forcing the ARB_npot extension off. Modern cards just
2160 * skip the code anyway, so put it inside a separate loop. */
2161 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& ps_args
->np2_fixup
)
2163 struct ps_np2fixup_info
*fixup
= ctx_priv
->cur_np2fixup_info
;
2166 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2167 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2168 * samplerNP2Fixup stores texture dimensions and is updated through
2169 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2171 for (i
= 0; i
< shader
->limits
->sampler
; ++i
)
2173 if (!reg_maps
->resource_info
[i
].type
|| !(ps_args
->np2_fixup
& (1u << i
)))
2176 if (reg_maps
->resource_info
[i
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_2D
)
2178 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2182 fixup
->idx
[i
] = cur
++;
2185 fixup
->num_consts
= (cur
+ 1) >> 1;
2186 fixup
->active
= ps_args
->np2_fixup
;
2187 shader_addline(buffer
, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix
, fixup
->num_consts
);
2190 /* Declare address variables */
2191 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
2193 if (map
& 1) shader_addline(buffer
, "ivec4 A%u;\n", i
);
2196 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
2198 for (i
= 0; i
< shader
->input_signature
.element_count
; ++i
)
2199 shader_glsl_declare_generic_vertex_attribute(buffer
, gl_info
, &shader
->input_signature
.elements
[i
]);
2201 if (vs_args
->point_size
&& !vs_args
->per_vertex_point_size
)
2203 shader_addline(buffer
, "uniform struct\n{\n");
2204 shader_addline(buffer
, " float size;\n");
2205 shader_addline(buffer
, " float size_min;\n");
2206 shader_addline(buffer
, " float size_max;\n");
2207 shader_addline(buffer
, "} ffp_point;\n");
2210 if (!gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2212 if (vs_args
->clip_enabled
)
2213 shader_addline(buffer
, "uniform vec4 clip_planes[%u];\n", gl_info
->limits
.user_clip_distances
);
2215 if (version
->major
< 3)
2217 declare_out_varying(gl_info
, buffer
, vs_args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
2218 declare_out_varying(gl_info
, buffer
, vs_args
->flatshading
, "vec4 ffp_varying_specular;\n");
2219 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
2220 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
2224 if (version
->major
< 4)
2225 shader_addline(buffer
, "void setup_vs_output(in vec4[%u]);\n", shader
->limits
->packed_output
);
2227 else if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
2229 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2231 shader_addline(buffer
, "varying in vec4 gs_in[][%u];\n", shader
->limits
->packed_input
);
2235 shader_addline(buffer
, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader
->u
.gs
.input_type
));
2236 shader_addline(buffer
, "layout(%s, max_vertices = %u) out;\n",
2237 glsl_primitive_type_from_d3d(shader
->u
.gs
.output_type
), shader
->u
.gs
.vertices_out
);
2238 shader_addline(buffer
, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader
->limits
->packed_input
);
2241 else if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2243 if (version
->major
< 3 || ps_args
->vp_mode
!= vertexshader
)
2245 shader_addline(buffer
, "uniform struct\n{\n");
2246 shader_addline(buffer
, " vec4 color;\n");
2247 shader_addline(buffer
, " float density;\n");
2248 shader_addline(buffer
, " float end;\n");
2249 shader_addline(buffer
, " float scale;\n");
2250 shader_addline(buffer
, "} ffp_fog;\n");
2252 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
2254 if (glsl_is_color_reg_read(shader
, 0))
2255 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
2256 if (glsl_is_color_reg_read(shader
, 1))
2257 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
2258 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
2259 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
2263 if (glsl_is_color_reg_read(shader
, 0))
2264 declare_in_varying(gl_info
, buffer
, ps_args
->flatshading
, "vec4 ffp_varying_diffuse;\n");
2265 if (glsl_is_color_reg_read(shader
, 1))
2266 declare_in_varying(gl_info
, buffer
, ps_args
->flatshading
, "vec4 ffp_varying_specular;\n");
2267 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
2268 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
2269 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
2273 if (version
->major
>= 3)
2275 UINT in_count
= min(vec4_varyings(version
->major
, gl_info
), shader
->limits
->packed_input
);
2277 if (ps_args
->vp_mode
== vertexshader
)
2278 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 %s_link[%u];\n", prefix
, in_count
);
2279 shader_addline(buffer
, "vec4 %s_in[%u];\n", prefix
, in_count
);
2282 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
2287 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", i
);
2289 if (reg_maps
->luminanceparams
& (1u << i
))
2291 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", i
);
2292 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", i
);
2293 extra_constants_needed
++;
2296 extra_constants_needed
++;
2299 if (ps_args
->srgb_correction
)
2301 shader_addline(buffer
, "const vec4 srgb_const0 = ");
2302 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
2303 shader_addline(buffer
, ";\n");
2304 shader_addline(buffer
, "const vec4 srgb_const1 = ");
2305 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
2306 shader_addline(buffer
, ";\n");
2308 if (reg_maps
->vpos
|| reg_maps
->usesdsy
)
2310 if (reg_maps
->usesdsy
|| !gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
2312 ++extra_constants_needed
;
2313 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
2317 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
2319 if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_PIXEL_CENTER_INTEGER
)
2320 shader_addline(buffer
, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2321 ps_args
->render_offscreen
? "" : "origin_upper_left, ");
2322 else if (!ps_args
->render_offscreen
)
2323 shader_addline(buffer
, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2325 shader_addline(buffer
, "vec4 vpos;\n");
2329 if (ps_args
->alpha_test_func
+ 1 != WINED3D_CMP_ALWAYS
)
2330 shader_addline(buffer
, "uniform float alpha_test_ref;\n");
2332 if (!needs_legacy_glsl_syntax(gl_info
))
2333 shader_addline(buffer
, "out vec4 ps_out[%u];\n", gl_info
->limits
.buffers
);
2335 if (shader
->limits
->constant_float
+ extra_constants_needed
>= gl_info
->limits
.glsl_ps_float_constants
)
2336 FIXME("Insufficient uniforms to run this shader.\n");
2339 /* Declare output register temporaries */
2340 if (shader
->limits
->packed_output
)
2341 shader_addline(buffer
, "vec4 %s_out[%u];\n", prefix
, shader
->limits
->packed_output
);
2343 /* Declare temporary variables */
2344 if (reg_maps
->temporary_count
)
2346 for (i
= 0; i
< reg_maps
->temporary_count
; ++i
)
2347 shader_addline(buffer
, "vec4 R%u;\n", i
);
2351 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
2354 shader_addline(buffer
, "vec4 R%u;\n", i
);
2358 /* Declare indexable temporary variables */
2359 LIST_FOR_EACH_ENTRY(idx_temp_reg
, ®_maps
->indexable_temps
, struct wined3d_shader_indexable_temp
, entry
)
2361 if (idx_temp_reg
->component_count
!= 4)
2362 FIXME("Ignoring component count %u.\n", idx_temp_reg
->component_count
);
2363 shader_addline(buffer
, "vec4 X%u[%u];\n", idx_temp_reg
->register_idx
, idx_temp_reg
->register_size
);
2366 /* Declare loop registers aLx */
2367 if (version
->major
< 4)
2369 for (i
= 0; i
< reg_maps
->loop_depth
; ++i
)
2371 shader_addline(buffer
, "int aL%u;\n", i
);
2372 shader_addline(buffer
, "int tmpInt%u;\n", i
);
2376 /* Temporary variables for matrix operations */
2377 shader_addline(buffer
, "vec4 tmp0;\n");
2378 shader_addline(buffer
, "vec4 tmp1;\n");
2380 if (!shader
->load_local_constsF
)
2382 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, struct wined3d_shader_lconst
, entry
)
2384 shader_addline(buffer
, "const vec4 %s_lc%u = ", prefix
, lconst
->idx
);
2385 shader_glsl_append_imm_vec4(buffer
, (const float *)lconst
->value
);
2386 shader_addline(buffer
, ";\n");
2391 /*****************************************************************************
2392 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2394 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2395 ****************************************************************************/
2398 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2399 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
);
2401 /** Used for opcode modifiers - They multiply the result by the specified amount */
2402 static const char * const shift_glsl_tab
[] = {
2404 "2.0 * ", /* 1 (x2) */
2405 "4.0 * ", /* 2 (x4) */
2406 "8.0 * ", /* 3 (x8) */
2407 "16.0 * ", /* 4 (x16) */
2408 "32.0 * ", /* 5 (x32) */
2415 "0.0625 * ", /* 12 (d16) */
2416 "0.125 * ", /* 13 (d8) */
2417 "0.25 * ", /* 14 (d4) */
2418 "0.5 * " /* 15 (d2) */
2421 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2422 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier
,
2423 const char *in_reg
, const char *in_regswizzle
, char *out_str
)
2425 switch (src_modifier
)
2427 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
2428 case WINED3DSPSM_DW
:
2429 case WINED3DSPSM_NONE
:
2430 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2432 case WINED3DSPSM_NEG
:
2433 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
2435 case WINED3DSPSM_NOT
:
2436 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
2438 case WINED3DSPSM_BIAS
:
2439 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2441 case WINED3DSPSM_BIASNEG
:
2442 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
2444 case WINED3DSPSM_SIGN
:
2445 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2447 case WINED3DSPSM_SIGNNEG
:
2448 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
2450 case WINED3DSPSM_COMP
:
2451 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
2453 case WINED3DSPSM_X2
:
2454 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
2456 case WINED3DSPSM_X2NEG
:
2457 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
2459 case WINED3DSPSM_ABS
:
2460 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
2462 case WINED3DSPSM_ABSNEG
:
2463 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
2466 FIXME("Unhandled modifier %u\n", src_modifier
);
2467 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
2471 /** Writes the GLSL variable name that corresponds to the register that the
2472 * DX opcode parameter is trying to access */
2473 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
2474 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
2476 /* oPos, oFog and oPts in D3D */
2477 static const char * const hwrastout_reg_names
[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2479 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
2480 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
2481 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
2482 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
2483 const char *prefix
= shader_glsl_get_prefix(version
->type
);
2484 struct glsl_src_param rel_param0
, rel_param1
;
2485 char imm_str
[4][17];
2487 if (reg
->idx
[0].offset
!= ~0U && reg
->idx
[0].rel_addr
)
2488 shader_glsl_add_src_param(ins
, reg
->idx
[0].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param0
);
2489 if (reg
->idx
[1].offset
!= ~0U && reg
->idx
[1].rel_addr
)
2490 shader_glsl_add_src_param(ins
, reg
->idx
[1].rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param1
);
2495 case WINED3DSPR_TEMP
:
2496 sprintf(register_name
, "R%u", reg
->idx
[0].offset
);
2499 case WINED3DSPR_INPUT
:
2500 if (version
->type
== WINED3D_SHADER_TYPE_VERTEX
)
2502 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2504 if (reg
->idx
[0].rel_addr
)
2505 FIXME("VS3+ input registers relative addressing.\n");
2506 if (priv
->cur_vs_args
->swizzle_map
& (1u << reg
->idx
[0].offset
))
2508 sprintf(register_name
, "%s_in%u", prefix
, reg
->idx
[0].offset
);
2512 if (version
->type
== WINED3D_SHADER_TYPE_GEOMETRY
)
2514 if (reg
->idx
[0].rel_addr
)
2516 if (reg
->idx
[1].rel_addr
)
2517 sprintf(register_name
, "gs_in[%s + %u]%s[%s + %u]",
2518 rel_param0
.param_str
, reg
->idx
[0].offset
,
2519 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2520 rel_param1
.param_str
, reg
->idx
[1].offset
);
2522 sprintf(register_name
, "gs_in[%s + %u]%s[%u]",
2523 rel_param0
.param_str
, reg
->idx
[0].offset
,
2524 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2525 reg
->idx
[1].offset
);
2527 else if (reg
->idx
[1].rel_addr
)
2528 sprintf(register_name
, "gs_in[%u]%s[%s + %u]", reg
->idx
[0].offset
,
2529 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2530 rel_param1
.param_str
, reg
->idx
[1].offset
);
2532 sprintf(register_name
, "gs_in[%u]%s[%u]", reg
->idx
[0].offset
,
2533 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] ? "" : ".gs_in",
2534 reg
->idx
[1].offset
);
2538 /* pixel shaders >= 3.0 */
2539 if (version
->major
>= 3)
2541 DWORD idx
= shader
->u
.ps
.input_reg_map
[reg
->idx
[0].offset
];
2542 unsigned int in_count
= vec4_varyings(version
->major
, gl_info
);
2544 if (reg
->idx
[0].rel_addr
)
2546 /* Removing a + 0 would be an obvious optimization, but
2547 * OS X doesn't see the NOP operation there. */
2550 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2551 && shader
->u
.ps
.declared_in_count
> in_count
)
2553 sprintf(register_name
,
2554 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2555 rel_param0
.param_str
, idx
, in_count
- 1, rel_param0
.param_str
, idx
, in_count
,
2556 prefix
, rel_param0
.param_str
, idx
);
2560 sprintf(register_name
, "%s_in[%s + %u]", prefix
, rel_param0
.param_str
, idx
);
2565 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
2566 && shader
->u
.ps
.declared_in_count
> in_count
)
2568 sprintf(register_name
, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2569 rel_param0
.param_str
, in_count
- 1, rel_param0
.param_str
, in_count
,
2570 prefix
, rel_param0
.param_str
);
2574 sprintf(register_name
, "%s_in[%s]", prefix
, rel_param0
.param_str
);
2580 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
2581 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
2582 else sprintf(register_name
, "%s_in[%u]", prefix
, idx
);
2587 if (!reg
->idx
[0].offset
)
2588 strcpy(register_name
, "ffp_varying_diffuse");
2590 strcpy(register_name
, "ffp_varying_specular");
2595 case WINED3DSPR_CONST
:
2597 /* Relative addressing */
2598 if (reg
->idx
[0].rel_addr
)
2600 if (wined3d_settings
.check_float_constants
)
2601 sprintf(register_name
, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2602 rel_param0
.param_str
, reg
->idx
[0].offset
,
2603 rel_param0
.param_str
, reg
->idx
[0].offset
, shader
->limits
->constant_float
,
2604 prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2605 else if (reg
->idx
[0].offset
)
2606 sprintf(register_name
, "%s_c[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2608 sprintf(register_name
, "%s_c[%s]", prefix
, rel_param0
.param_str
);
2612 if (shader_constant_is_local(shader
, reg
->idx
[0].offset
))
2613 sprintf(register_name
, "%s_lc%u", prefix
, reg
->idx
[0].offset
);
2615 sprintf(register_name
, "%s_c[%u]", prefix
, reg
->idx
[0].offset
);
2620 case WINED3DSPR_CONSTINT
:
2621 sprintf(register_name
, "%s_i[%u]", prefix
, reg
->idx
[0].offset
);
2624 case WINED3DSPR_CONSTBOOL
:
2625 sprintf(register_name
, "%s_b[%u]", prefix
, reg
->idx
[0].offset
);
2628 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
2629 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
)
2630 sprintf(register_name
, "T%u", reg
->idx
[0].offset
);
2632 sprintf(register_name
, "A%u", reg
->idx
[0].offset
);
2635 case WINED3DSPR_LOOP
:
2636 sprintf(register_name
, "aL%u", ins
->ctx
->state
->current_loop_reg
- 1);
2639 case WINED3DSPR_SAMPLER
:
2640 sprintf(register_name
, "%s_sampler%u", prefix
, reg
->idx
[0].offset
);
2643 case WINED3DSPR_COLOROUT
:
2644 if (reg
->idx
[0].offset
>= gl_info
->limits
.buffers
)
2645 WARN("Write to render target %u, only %d supported.\n",
2646 reg
->idx
[0].offset
, gl_info
->limits
.buffers
);
2648 sprintf(register_name
, "%s[%u]", get_fragment_output(gl_info
), reg
->idx
[0].offset
);
2651 case WINED3DSPR_RASTOUT
:
2652 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
[0].offset
]);
2655 case WINED3DSPR_DEPTHOUT
:
2656 sprintf(register_name
, "gl_FragDepth");
2659 case WINED3DSPR_ATTROUT
:
2660 if (!reg
->idx
[0].offset
)
2661 sprintf(register_name
, "%s_out[8]", prefix
);
2663 sprintf(register_name
, "%s_out[9]", prefix
);
2666 case WINED3DSPR_TEXCRDOUT
:
2667 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2668 if (reg
->idx
[0].rel_addr
)
2669 FIXME("VS3 output registers relative addressing.\n");
2670 sprintf(register_name
, "%s_out[%u]", prefix
, reg
->idx
[0].offset
);
2673 case WINED3DSPR_MISCTYPE
:
2674 if (!reg
->idx
[0].offset
)
2677 sprintf(register_name
, "vpos");
2679 else if (reg
->idx
[0].offset
== 1)
2681 /* Note that gl_FrontFacing is a bool, while vFace is
2682 * a float for which the sign determines front/back */
2683 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
2687 FIXME("Unhandled misctype register %u.\n", reg
->idx
[0].offset
);
2688 sprintf(register_name
, "unrecognized_register");
2692 case WINED3DSPR_IMMCONST
:
2693 switch (reg
->immconst_type
)
2695 case WINED3D_IMMCONST_SCALAR
:
2696 switch (reg
->data_type
)
2698 case WINED3D_DATA_FLOAT
:
2699 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
2700 sprintf(register_name
, "uintBitsToFloat(%#xu)", reg
->u
.immconst_data
[0]);
2702 wined3d_ftoa(*(const float *)reg
->u
.immconst_data
, register_name
);
2704 case WINED3D_DATA_INT
:
2705 sprintf(register_name
, "%#x", reg
->u
.immconst_data
[0]);
2707 case WINED3D_DATA_RESOURCE
:
2708 case WINED3D_DATA_SAMPLER
:
2709 case WINED3D_DATA_UINT
:
2710 sprintf(register_name
, "%#xu", reg
->u
.immconst_data
[0]);
2713 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2718 case WINED3D_IMMCONST_VEC4
:
2719 switch (reg
->data_type
)
2721 case WINED3D_DATA_FLOAT
:
2722 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
2724 sprintf(register_name
, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2725 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
2726 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
2730 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[0], imm_str
[0]);
2731 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[1], imm_str
[1]);
2732 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[2], imm_str
[2]);
2733 wined3d_ftoa(*(const float *)®
->u
.immconst_data
[3], imm_str
[3]);
2734 sprintf(register_name
, "vec4(%s, %s, %s, %s)",
2735 imm_str
[0], imm_str
[1], imm_str
[2], imm_str
[3]);
2738 case WINED3D_DATA_INT
:
2739 sprintf(register_name
, "ivec4(%#x, %#x, %#x, %#x)",
2740 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
2741 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
2743 case WINED3D_DATA_RESOURCE
:
2744 case WINED3D_DATA_SAMPLER
:
2745 case WINED3D_DATA_UINT
:
2746 sprintf(register_name
, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2747 reg
->u
.immconst_data
[0], reg
->u
.immconst_data
[1],
2748 reg
->u
.immconst_data
[2], reg
->u
.immconst_data
[3]);
2751 sprintf(register_name
, "<unhandled data type %#x>", reg
->data_type
);
2757 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
2758 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
2762 case WINED3DSPR_CONSTBUFFER
:
2763 if (reg
->idx
[1].rel_addr
)
2764 sprintf(register_name
, "%s_cb%u[%s + %u]",
2765 prefix
, reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2767 sprintf(register_name
, "%s_cb%u[%u]", prefix
, reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2770 case WINED3DSPR_IMMCONSTBUFFER
:
2771 if (reg
->idx
[0].rel_addr
)
2772 sprintf(register_name
, "%s_icb[%s + %u]", prefix
, rel_param0
.param_str
, reg
->idx
[0].offset
);
2774 sprintf(register_name
, "%s_icb[%u]", prefix
, reg
->idx
[0].offset
);
2777 case WINED3DSPR_PRIMID
:
2778 sprintf(register_name
, "uint(gl_PrimitiveIDIn)");
2781 case WINED3DSPR_IDXTEMP
:
2782 if (reg
->idx
[1].rel_addr
)
2783 sprintf(register_name
, "X%u[%s + %u]", reg
->idx
[0].offset
, rel_param1
.param_str
, reg
->idx
[1].offset
);
2785 sprintf(register_name
, "X%u[%u]", reg
->idx
[0].offset
, reg
->idx
[1].offset
);
2789 FIXME("Unhandled register type %#x.\n", reg
->type
);
2790 sprintf(register_name
, "unrecognized_register");
2795 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
2798 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
2799 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
2800 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
2801 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
2805 /* Get the GLSL write mask for the destination register */
2806 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
2808 DWORD mask
= param
->write_mask
;
2810 if (shader_is_scalar(¶m
->reg
))
2812 mask
= WINED3DSP_WRITEMASK_0
;
2817 shader_glsl_write_mask_to_str(mask
, write_mask
);
2823 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
2824 unsigned int size
= 0;
2826 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
2827 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
2828 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
2829 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
2834 static void shader_glsl_swizzle_to_str(const DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
2836 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2837 * but addressed as "rgba". To fix this we need to swap the register's x
2838 * and z components. */
2839 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
2842 /* swizzle bits fields: wwzzyyxx */
2843 if (mask
& WINED3DSP_WRITEMASK_0
) *str
++ = swizzle_chars
[swizzle
& 0x03];
2844 if (mask
& WINED3DSP_WRITEMASK_1
) *str
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
2845 if (mask
& WINED3DSP_WRITEMASK_2
) *str
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
2846 if (mask
& WINED3DSP_WRITEMASK_3
) *str
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
2850 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
2851 BOOL fixup
, DWORD mask
, char *swizzle_str
)
2853 if (shader_is_scalar(¶m
->reg
))
2854 *swizzle_str
= '\0';
2856 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
2859 /* From a given parameter token, generate the corresponding GLSL string.
2860 * Also, return the actual register name and swizzle in case the
2861 * caller needs this information as well. */
2862 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction
*ins
,
2863 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
,
2864 enum wined3d_data_type data_type
)
2866 BOOL is_color
= FALSE
;
2867 char swizzle_str
[6];
2869 glsl_src
->reg_name
[0] = '\0';
2870 glsl_src
->param_str
[0] = '\0';
2871 swizzle_str
[0] = '\0';
2873 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
2874 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
2876 if (wined3d_src
->reg
.type
== WINED3DSPR_IMMCONST
|| wined3d_src
->reg
.type
== WINED3DSPR_PRIMID
)
2878 shader_glsl_gen_modifier(wined3d_src
->modifiers
, glsl_src
->reg_name
, swizzle_str
, glsl_src
->param_str
);
2886 case WINED3D_DATA_FLOAT
:
2887 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2889 case WINED3D_DATA_INT
:
2890 sprintf(reg_name
, "floatBitsToInt(%s)", glsl_src
->reg_name
);
2892 case WINED3D_DATA_RESOURCE
:
2893 case WINED3D_DATA_SAMPLER
:
2894 case WINED3D_DATA_UINT
:
2895 sprintf(reg_name
, "floatBitsToUint(%s)", glsl_src
->reg_name
);
2898 FIXME("Unhandled data type %#x.\n", data_type
);
2899 sprintf(reg_name
, "%s", glsl_src
->reg_name
);
2903 shader_glsl_gen_modifier(wined3d_src
->modifiers
, reg_name
, swizzle_str
, glsl_src
->param_str
);
2907 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
2908 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
)
2910 shader_glsl_add_src_param_ext(ins
, wined3d_src
, mask
, glsl_src
, wined3d_src
->reg
.data_type
);
2913 /* From a given parameter token, generate the corresponding GLSL string.
2914 * Also, return the actual register name and swizzle in case the
2915 * caller needs this information as well. */
2916 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
2917 const struct wined3d_shader_dst_param
*wined3d_dst
, struct glsl_dst_param
*glsl_dst
)
2919 BOOL is_color
= FALSE
;
2921 glsl_dst
->mask_str
[0] = '\0';
2922 glsl_dst
->reg_name
[0] = '\0';
2924 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
2925 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
2928 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2929 static DWORD
shader_glsl_append_dst_ext(struct wined3d_string_buffer
*buffer
,
2930 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
,
2931 enum wined3d_data_type data_type
)
2933 struct glsl_dst_param glsl_dst
;
2936 if ((mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
)))
2940 case WINED3D_DATA_FLOAT
:
2941 shader_addline(buffer
, "%s%s = %s(",
2942 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2944 case WINED3D_DATA_INT
:
2945 shader_addline(buffer
, "%s%s = %sintBitsToFloat(",
2946 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2948 case WINED3D_DATA_RESOURCE
:
2949 case WINED3D_DATA_SAMPLER
:
2950 case WINED3D_DATA_UINT
:
2951 shader_addline(buffer
, "%s%s = %suintBitsToFloat(",
2952 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2955 FIXME("Unhandled data type %#x.\n", data_type
);
2956 shader_addline(buffer
, "%s%s = %s(",
2957 glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
2965 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2966 static DWORD
shader_glsl_append_dst(struct wined3d_string_buffer
*buffer
, const struct wined3d_shader_instruction
*ins
)
2968 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
2971 /** Process GLSL instruction modifiers */
2972 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
2974 struct glsl_dst_param dst_param
;
2977 if (!ins
->dst_count
) return;
2979 modifiers
= ins
->dst
[0].modifiers
;
2980 if (!modifiers
) return;
2982 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
2984 if (modifiers
& WINED3DSPDM_SATURATE
)
2986 /* _SAT means to clamp the value of the register to between 0 and 1 */
2987 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
2988 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
2991 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
2993 FIXME("_centroid modifier not handled\n");
2996 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
2998 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3002 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op
)
3006 case WINED3D_SHADER_REL_OP_GT
: return ">";
3007 case WINED3D_SHADER_REL_OP_EQ
: return "==";
3008 case WINED3D_SHADER_REL_OP_GE
: return ">=";
3009 case WINED3D_SHADER_REL_OP_LT
: return "<";
3010 case WINED3D_SHADER_REL_OP_NE
: return "!=";
3011 case WINED3D_SHADER_REL_OP_LE
: return "<=";
3013 FIXME("Unrecognized operator %#x.\n", op
);
3018 static BOOL
shader_glsl_has_core_grad(const struct wined3d_gl_info
*gl_info
,
3019 const struct wined3d_shader_version
*version
)
3021 return shader_glsl_get_version(gl_info
, version
) >= 130 || gl_info
->supported
[EXT_GPU_SHADER4
];
3024 static void shader_glsl_get_sample_function(const struct wined3d_shader_context
*ctx
,
3025 DWORD resource_idx
, DWORD sampler_idx
, DWORD flags
, struct glsl_sample_function
*sample_function
)
3027 enum wined3d_shader_resource_type resource_type
= ctx
->reg_maps
->resource_info
[resource_idx
].type
;
3028 struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
3029 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
3030 BOOL shadow
= glsl_is_shadow_sampler(ctx
->shader
, priv
->cur_ps_args
, resource_idx
, sampler_idx
);
3031 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
3032 BOOL texrect
= ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_PIXEL
3033 && priv
->cur_ps_args
->np2_fixup
& (1u << resource_idx
)
3034 && gl_info
->supported
[ARB_TEXTURE_RECTANGLE
];
3035 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
3036 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
3037 BOOL offset
= flags
& WINED3D_GLSL_SAMPLE_OFFSET
;
3038 const char *base
= "texture", *type_part
= "", *suffix
= "";
3039 unsigned int coord_size
, deriv_size
;
3042 sample_function
->data_type
= ctx
->reg_maps
->resource_info
[resource_idx
].data_type
;
3044 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
3046 ERR("Unexpected resource type %#x.\n", resource_type
);
3047 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
3049 array
= resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3050 || resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
;
3052 /* Note that there's no such thing as a projected cube texture. */
3053 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
3056 if (needs_legacy_glsl_syntax(gl_info
))
3061 type_part
= resource_type_info
[resource_type
].type_part
;
3062 if (resource_type
== WINED3D_SHADER_RESOURCE_TEXTURE_2D
&& texrect
)
3063 type_part
= "2DRect";
3065 FIXME("Unhandled resource type %#x.\n", resource_type
);
3067 if (!lod
&& grad
&& !shader_glsl_has_core_grad(gl_info
, &ctx
->shader
->reg_maps
.shader_version
))
3069 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
3072 FIXME("Unsupported grad function.\n");
3076 if (flags
& WINED3D_GLSL_SAMPLE_LOAD
)
3078 static const DWORD texel_fetch_flags
= WINED3D_GLSL_SAMPLE_LOAD
| WINED3D_GLSL_SAMPLE_OFFSET
;
3079 if (flags
& ~texel_fetch_flags
)
3080 ERR("Unexpected flags %#x for texelFetch.\n", flags
& ~texel_fetch_flags
);
3082 base
= "texelFetch";
3086 sample_function
->name
= string_buffer_get(priv
->string_buffers
);
3087 string_buffer_sprintf(sample_function
->name
, "%s%s%s%s%s%s", base
, type_part
, projected
? "Proj" : "",
3088 lod
? "Lod" : grad
? "Grad" : "", offset
? "Offset" : "", suffix
);
3090 coord_size
= resource_type_info
[resource_type
].coord_size
;
3091 deriv_size
= coord_size
;
3096 sample_function
->offset_size
= offset
? deriv_size
: 0;
3097 sample_function
->coord_mask
= (1u << coord_size
) - 1;
3098 sample_function
->deriv_mask
= (1u << deriv_size
) - 1;
3099 sample_function
->output_single_component
= shadow
&& !needs_legacy_glsl_syntax(gl_info
);
3102 static void shader_glsl_release_sample_function(const struct wined3d_shader_context
*ctx
,
3103 struct glsl_sample_function
*sample_function
)
3105 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
3107 string_buffer_release(priv
->string_buffers
, sample_function
->name
);
3110 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
3111 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
3113 switch(channel_source
)
3115 case CHANNEL_SOURCE_ZERO
:
3116 strcat(arguments
, "0.0");
3119 case CHANNEL_SOURCE_ONE
:
3120 strcat(arguments
, "1.0");
3123 case CHANNEL_SOURCE_X
:
3124 strcat(arguments
, reg_name
);
3125 strcat(arguments
, ".x");
3128 case CHANNEL_SOURCE_Y
:
3129 strcat(arguments
, reg_name
);
3130 strcat(arguments
, ".y");
3133 case CHANNEL_SOURCE_Z
:
3134 strcat(arguments
, reg_name
);
3135 strcat(arguments
, ".z");
3138 case CHANNEL_SOURCE_W
:
3139 strcat(arguments
, reg_name
);
3140 strcat(arguments
, ".w");
3144 FIXME("Unhandled channel source %#x\n", channel_source
);
3145 strcat(arguments
, "undefined");
3149 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
3152 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer
*buffer
,
3153 const char *reg_name
, DWORD mask
, struct color_fixup_desc fixup
)
3155 unsigned int mask_size
, remaining
;
3156 DWORD fixup_mask
= 0;
3157 char arguments
[256];
3160 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) fixup_mask
|= WINED3DSP_WRITEMASK_0
;
3161 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) fixup_mask
|= WINED3DSP_WRITEMASK_1
;
3162 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) fixup_mask
|= WINED3DSP_WRITEMASK_2
;
3163 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) fixup_mask
|= WINED3DSP_WRITEMASK_3
;
3164 if (!(mask
&= fixup_mask
))
3167 if (is_complex_fixup(fixup
))
3169 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
3170 FIXME("Complex fixup (%#x) not supported\n",complex_fixup
);
3174 shader_glsl_write_mask_to_str(mask
, mask_str
);
3175 mask_size
= shader_glsl_get_write_mask_size(mask
);
3177 arguments
[0] = '\0';
3178 remaining
= mask_size
;
3179 if (mask
& WINED3DSP_WRITEMASK_0
)
3181 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
3182 if (--remaining
) strcat(arguments
, ", ");
3184 if (mask
& WINED3DSP_WRITEMASK_1
)
3186 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
3187 if (--remaining
) strcat(arguments
, ", ");
3189 if (mask
& WINED3DSP_WRITEMASK_2
)
3191 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
3192 if (--remaining
) strcat(arguments
, ", ");
3194 if (mask
& WINED3DSP_WRITEMASK_3
)
3196 shader_glsl_append_fixup_arg(arguments
, reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
3197 if (--remaining
) strcat(arguments
, ", ");
3201 shader_addline(buffer
, "%s%s = vec%u(%s);\n", reg_name
, mask_str
, mask_size
, arguments
);
3203 shader_addline(buffer
, "%s%s = %s;\n", reg_name
, mask_str
, arguments
);
3206 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
3211 shader_glsl_get_register_name(&ins
->dst
[0].reg
, reg_name
, &is_color
, ins
);
3212 shader_glsl_color_correction_ext(ins
->ctx
->buffer
, reg_name
, ins
->dst
[0].write_mask
, fixup
);
3215 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
3216 unsigned int sampler_bind_idx
, const struct glsl_sample_function
*sample_function
, DWORD swizzle
,
3217 const char *dx
, const char *dy
, const char *bias
, const struct wined3d_shader_texel_offset
*offset
,
3218 const char *coord_reg_fmt
, ...)
3220 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
3221 char dst_swizzle
[6];
3222 struct color_fixup_desc fixup
;
3223 BOOL np2_fixup
= FALSE
;
3227 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
3229 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3230 * We actually rely on it for vertex shaders and SM4+. */
3231 if (version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& version
->major
< 4)
3233 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3234 fixup
= priv
->cur_ps_args
->color_fixup
[sampler_bind_idx
];
3236 if (priv
->cur_ps_args
->np2_fixup
& (1u << sampler_bind_idx
))
3241 fixup
= COLOR_FIXUP_IDENTITY
;
3244 shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], sample_function
->data_type
);
3246 if (sample_function
->output_single_component
)
3247 shader_addline(ins
->ctx
->buffer
, "vec4(");
3249 shader_addline(ins
->ctx
->buffer
, "%s(%s_sampler%u, ",
3250 sample_function
->name
->buffer
, shader_glsl_get_prefix(version
->type
), sampler_bind_idx
);
3254 va_start(args
, coord_reg_fmt
);
3255 ret
= shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
3259 if (!string_buffer_resize(ins
->ctx
->buffer
, ret
))
3265 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3266 const unsigned char idx
= priv
->cur_np2fixup_info
->idx
[sampler_bind_idx
];
3268 switch (shader_glsl_get_write_mask_size(sample_function
->coord_mask
))
3271 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
3272 idx
>> 1, (idx
% 2) ? "z" : "x");
3275 shader_addline(ins
->ctx
->buffer
, " * ps_samplerNP2Fixup[%u].%s",
3276 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3279 shader_addline(ins
->ctx
->buffer
, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3280 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3283 shader_addline(ins
->ctx
->buffer
, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3284 idx
>> 1, (idx
% 2) ? "zw" : "xy");
3289 shader_addline(ins
->ctx
->buffer
, ", %s, %s", dx
, dy
);
3291 shader_addline(ins
->ctx
->buffer
, ", %s", bias
);
3292 if (sample_function
->offset_size
)
3294 int offset_immdata
[4] = {offset
->u
, offset
->v
, offset
->w
};
3295 shader_addline(ins
->ctx
->buffer
, ", ");
3296 shader_glsl_append_imm_ivec(ins
->ctx
->buffer
, offset_immdata
, sample_function
->offset_size
);
3298 shader_addline(ins
->ctx
->buffer
, ")");
3300 if (sample_function
->output_single_component
)
3301 shader_addline(ins
->ctx
->buffer
, ")");
3303 shader_addline(ins
->ctx
->buffer
, "%s);\n", dst_swizzle
);
3305 if (!is_identity_fixup(fixup
))
3306 shader_glsl_color_correction(ins
, fixup
);
3309 static void shader_glsl_fixup_position(struct wined3d_string_buffer
*buffer
)
3311 /* Write the final position.
3313 * OpenGL coordinates specify the center of the pixel while D3D coords
3314 * specify the corner. The offsets are stored in z and w in
3315 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3316 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3318 shader_addline(buffer
, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3319 shader_addline(buffer
, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3321 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3324 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3325 * shaders are run before the homogeneous divide, so we have to take the w
3326 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3328 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3331 /*****************************************************************************
3332 * Begin processing individual instruction opcodes
3333 ****************************************************************************/
3335 static void shader_glsl_binop(const struct wined3d_shader_instruction
*ins
)
3337 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3338 struct glsl_src_param src0_param
;
3339 struct glsl_src_param src1_param
;
3343 /* Determine the GLSL operator to use based on the opcode */
3344 switch (ins
->handler_idx
)
3346 case WINED3DSIH_ADD
: op
= "+"; break;
3347 case WINED3DSIH_AND
: op
= "&"; break;
3348 case WINED3DSIH_DIV
: op
= "/"; break;
3349 case WINED3DSIH_IADD
: op
= "+"; break;
3350 case WINED3DSIH_ISHL
: op
= "<<"; break;
3351 case WINED3DSIH_ISHR
: op
= ">>"; break;
3352 case WINED3DSIH_MUL
: op
= "*"; break;
3353 case WINED3DSIH_OR
: op
= "|"; break;
3354 case WINED3DSIH_SUB
: op
= "-"; break;
3355 case WINED3DSIH_USHR
: op
= ">>"; break;
3356 case WINED3DSIH_XOR
: op
= "^"; break;
3358 op
= "<unhandled operator>";
3359 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3363 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3364 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3365 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3366 shader_addline(buffer
, "%s %s %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
3369 static void shader_glsl_relop(const struct wined3d_shader_instruction
*ins
)
3371 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3372 struct glsl_src_param src0_param
;
3373 struct glsl_src_param src1_param
;
3374 unsigned int mask_size
;
3378 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3379 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3380 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3381 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3385 switch (ins
->handler_idx
)
3387 case WINED3DSIH_EQ
: op
= "equal"; break;
3388 case WINED3DSIH_IEQ
: op
= "equal"; break;
3389 case WINED3DSIH_GE
: op
= "greaterThanEqual"; break;
3390 case WINED3DSIH_IGE
: op
= "greaterThanEqual"; break;
3391 case WINED3DSIH_UGE
: op
= "greaterThanEqual"; break;
3392 case WINED3DSIH_LT
: op
= "lessThan"; break;
3393 case WINED3DSIH_ILT
: op
= "lessThan"; break;
3394 case WINED3DSIH_ULT
: op
= "lessThan"; break;
3395 case WINED3DSIH_NE
: op
= "notEqual"; break;
3396 case WINED3DSIH_INE
: op
= "notEqual"; break;
3398 op
= "<unhandled operator>";
3399 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3403 shader_addline(buffer
, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3404 mask_size
, op
, src0_param
.param_str
, src1_param
.param_str
);
3408 switch (ins
->handler_idx
)
3410 case WINED3DSIH_EQ
: op
= "=="; break;
3411 case WINED3DSIH_IEQ
: op
= "=="; break;
3412 case WINED3DSIH_GE
: op
= ">="; break;
3413 case WINED3DSIH_IGE
: op
= ">="; break;
3414 case WINED3DSIH_UGE
: op
= ">="; break;
3415 case WINED3DSIH_LT
: op
= "<"; break;
3416 case WINED3DSIH_ILT
: op
= "<"; break;
3417 case WINED3DSIH_ULT
: op
= "<"; break;
3418 case WINED3DSIH_NE
: op
= "!="; break;
3419 case WINED3DSIH_INE
: op
= "!="; break;
3421 op
= "<unhandled operator>";
3422 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
3426 shader_addline(buffer
, "%s %s %s ? 0xffffffffu : 0u);\n",
3427 src0_param
.param_str
, op
, src1_param
.param_str
);
3431 static void shader_glsl_unary_op(const struct wined3d_shader_instruction
*ins
)
3433 struct glsl_src_param src_param
;
3437 switch (ins
->handler_idx
)
3439 case WINED3DSIH_INEG
: op
= "-"; break;
3440 case WINED3DSIH_NOT
: op
= "~"; break;
3442 op
= "<unhandled operator>";
3443 ERR("Unhandled opcode %s.\n",
3444 debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3448 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3449 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3450 shader_addline(ins
->ctx
->buffer
, "%s%s);\n", op
, src_param
.param_str
);
3453 static void shader_glsl_imul(const struct wined3d_shader_instruction
*ins
)
3455 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3456 struct glsl_src_param src0_param
;
3457 struct glsl_src_param src1_param
;
3460 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3461 * not, we can emulate it. */
3462 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3463 FIXME("64-bit integer multiplies not implemented.\n");
3465 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3467 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3468 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3469 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3471 shader_addline(ins
->ctx
->buffer
, "%s * %s);\n",
3472 src0_param
.param_str
, src1_param
.param_str
);
3476 static void shader_glsl_udiv(const struct wined3d_shader_instruction
*ins
)
3478 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3479 struct glsl_src_param src0_param
, src1_param
;
3482 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
3484 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3488 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3489 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3490 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3491 shader_addline(buffer
, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3492 dst_mask
, src0_param
.param_str
, src1_param
.param_str
);
3494 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3495 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3496 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3497 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3499 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], WINED3D_DATA_FLOAT
);
3500 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
3504 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
3505 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3506 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3507 shader_addline(buffer
, "%s / %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3510 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
3512 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
3513 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3514 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3515 shader_addline(buffer
, "%s %% %s);\n", src0_param
.param_str
, src1_param
.param_str
);
3519 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3520 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
3522 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3523 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3524 struct glsl_src_param src0_param
;
3527 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3528 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3530 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3531 * shader versions WINED3DSIO_MOVA is used for this. */
3532 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
3533 && ins
->ctx
->reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
3534 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
3536 /* This is a simple floor() */
3537 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3538 if (mask_size
> 1) {
3539 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
3541 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
3544 else if (ins
->handler_idx
== WINED3DSIH_MOVA
)
3546 const struct wined3d_shader_version
*version
= &ins
->ctx
->shader
->reg_maps
.shader_version
;
3547 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3549 if (shader_glsl_get_version(gl_info
, version
) >= 130 || gl_info
->supported
[EXT_GPU_SHADER4
])
3552 shader_addline(buffer
, "ivec%d(round(%s)));\n", mask_size
, src0_param
.param_str
);
3554 shader_addline(buffer
, "int(round(%s)));\n", src0_param
.param_str
);
3559 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3560 mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
3562 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3563 src0_param
.param_str
, src0_param
.param_str
);
3568 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
3572 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3573 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
3575 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3576 struct glsl_src_param src0_param
;
3577 struct glsl_src_param src1_param
;
3578 DWORD dst_write_mask
, src_write_mask
;
3579 unsigned int dst_size
;
3581 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3582 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3584 /* dp4 works on vec4, dp3 on vec3, etc. */
3585 if (ins
->handler_idx
== WINED3DSIH_DP4
)
3586 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
3587 else if (ins
->handler_idx
== WINED3DSIH_DP3
)
3588 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3590 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
3592 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
3593 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
3596 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
3598 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
3602 /* Note that this instruction has some restrictions. The destination write mask
3603 * can't contain the w component, and the source swizzles have to be .xyzw */
3604 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
3606 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3607 struct glsl_src_param src0_param
;
3608 struct glsl_src_param src1_param
;
3611 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3612 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3613 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3614 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
3615 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
3618 static void shader_glsl_cut(const struct wined3d_shader_instruction
*ins
)
3620 unsigned int stream
= ins
->handler_idx
== WINED3DSIH_CUT
? 0 : ins
->src
[0].reg
.idx
[0].offset
;
3623 shader_addline(ins
->ctx
->buffer
, "EndPrimitive();\n");
3625 FIXME("Unhandled primitive stream %u.\n", stream
);
3628 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3629 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3630 * GLSL uses the value as-is. */
3631 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
3633 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3634 struct glsl_src_param src0_param
;
3635 struct glsl_src_param src1_param
;
3636 DWORD dst_write_mask
;
3637 unsigned int dst_size
;
3639 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3640 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3642 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
3643 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3647 shader_addline(buffer
, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3648 dst_size
, src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3652 shader_addline(buffer
, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3653 src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
3657 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3658 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
3660 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3661 struct glsl_src_param src_param
;
3662 const char *instruction
;
3666 /* Determine the GLSL function to use based on the opcode */
3667 /* TODO: Possibly make this a table for faster lookups */
3668 switch (ins
->handler_idx
)
3670 case WINED3DSIH_ABS
: instruction
= "abs"; break;
3671 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
3672 case WINED3DSIH_DSX_COARSE
: instruction
= "dFdxCoarse"; break;
3673 case WINED3DSIH_DSX_FINE
: instruction
= "dFdxFine"; break;
3674 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
3675 case WINED3DSIH_DSY_COARSE
: instruction
= "ycorrection.y * dFdyCoarse"; break;
3676 case WINED3DSIH_DSY_FINE
: instruction
= "ycorrection.y * dFdyFine"; break;
3677 case WINED3DSIH_FRC
: instruction
= "fract"; break;
3678 case WINED3DSIH_IMAX
: instruction
= "max"; break;
3679 case WINED3DSIH_IMIN
: instruction
= "min"; break;
3680 case WINED3DSIH_MAX
: instruction
= "max"; break;
3681 case WINED3DSIH_MIN
: instruction
= "min"; break;
3682 case WINED3DSIH_ROUND_NE
: instruction
= "roundEven"; break;
3683 case WINED3DSIH_ROUND_NI
: instruction
= "floor"; break;
3684 case WINED3DSIH_ROUND_PI
: instruction
= "ceil"; break;
3685 case WINED3DSIH_ROUND_Z
: instruction
= "trunc"; break;
3686 case WINED3DSIH_SQRT
: instruction
= "sqrt"; break;
3687 case WINED3DSIH_UMAX
: instruction
= "max"; break;
3688 case WINED3DSIH_UMIN
: instruction
= "min"; break;
3689 default: instruction
= "";
3690 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3694 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3696 shader_addline(buffer
, "%s(", instruction
);
3700 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3701 shader_addline(buffer
, "%s", src_param
.param_str
);
3702 for (i
= 1; i
< ins
->src_count
; ++i
)
3704 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
3705 shader_addline(buffer
, ", %s", src_param
.param_str
);
3709 shader_addline(buffer
, "));\n");
3712 static void shader_glsl_nop(const struct wined3d_shader_instruction
*ins
) {}
3714 static void shader_glsl_nrm(const struct wined3d_shader_instruction
*ins
)
3716 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3717 struct glsl_src_param src_param
;
3718 unsigned int mask_size
;
3722 write_mask
= shader_glsl_get_write_mask(ins
->dst
, dst_mask
);
3723 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3724 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3726 shader_addline(buffer
, "tmp0.x = dot(%s, %s);\n",
3727 src_param
.param_str
, src_param
.param_str
);
3728 shader_glsl_append_dst(buffer
, ins
);
3732 shader_addline(buffer
, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3733 mask_size
, src_param
.param_str
);
3737 shader_addline(buffer
, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3738 src_param
.param_str
);
3742 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction
*ins
)
3744 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
3745 ins
->ctx
->reg_maps
->shader_version
.minor
);
3746 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3747 struct glsl_src_param src0_param
;
3748 const char *prefix
, *suffix
;
3749 unsigned int dst_size
;
3750 DWORD dst_write_mask
;
3752 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
3753 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
3755 if (shader_version
< WINED3D_SHADER_VERSION(4, 0))
3756 dst_write_mask
= WINED3DSP_WRITEMASK_3
;
3758 shader_glsl_add_src_param(ins
, &ins
->src
[0], dst_write_mask
, &src0_param
);
3760 switch (ins
->handler_idx
)
3762 case WINED3DSIH_EXP
:
3763 case WINED3DSIH_EXPP
:
3768 case WINED3DSIH_LOG
:
3769 case WINED3DSIH_LOGP
:
3770 prefix
= "log2(abs(";
3774 case WINED3DSIH_RCP
:
3779 case WINED3DSIH_RSQ
:
3780 prefix
= "inversesqrt(abs(";
3787 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
3791 if (dst_size
> 1 && shader_version
< WINED3D_SHADER_VERSION(4, 0))
3792 shader_addline(buffer
, "vec%u(%s%s%s));\n", dst_size
, prefix
, src0_param
.param_str
, suffix
);
3794 shader_addline(buffer
, "%s%s%s);\n", prefix
, src0_param
.param_str
, suffix
);
3797 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3798 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3799 * dst.x = 2^(floor(src))
3800 * dst.y = src - floor(src)
3801 * dst.z = 2^src (partial precision is allowed, but optional)
3803 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3804 * dst = 2^src; (partial precision is allowed, but optional)
3806 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
3808 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
3810 struct glsl_src_param src_param
;
3813 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
3815 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
3816 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
3817 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
3818 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
3820 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3821 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3822 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
3826 shader_glsl_scalar_op(ins
);
3829 static void shader_glsl_cast(const struct wined3d_shader_instruction
*ins
,
3830 const char *vector_constructor
, const char *scalar_constructor
)
3832 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
3833 struct glsl_src_param src_param
;
3834 unsigned int mask_size
;
3837 write_mask
= shader_glsl_append_dst(buffer
, ins
);
3838 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3839 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
3842 shader_addline(buffer
, "%s%u(%s));\n", vector_constructor
, mask_size
, src_param
.param_str
);
3844 shader_addline(buffer
, "%s(%s));\n", scalar_constructor
, src_param
.param_str
);
3847 static void shader_glsl_to_int(const struct wined3d_shader_instruction
*ins
)
3849 shader_glsl_cast(ins
, "ivec", "int");
3852 static void shader_glsl_to_uint(const struct wined3d_shader_instruction
*ins
)
3854 shader_glsl_cast(ins
, "uvec", "uint");
3857 static void shader_glsl_to_float(const struct wined3d_shader_instruction
*ins
)
3859 shader_glsl_cast(ins
, "vec", "float");
3862 /** Process signed comparison opcodes in GLSL. */
3863 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
3865 struct glsl_src_param src0_param
;
3866 struct glsl_src_param src1_param
;
3868 unsigned int mask_size
;
3870 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3871 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3872 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3873 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3875 if (mask_size
> 1) {
3876 const char *compare
;
3878 switch(ins
->handler_idx
)
3880 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
3881 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
3882 default: compare
= "";
3883 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3886 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
3887 src0_param
.param_str
, src1_param
.param_str
);
3889 switch(ins
->handler_idx
)
3891 case WINED3DSIH_SLT
:
3892 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3893 * to return 0.0 but step returns 1.0 because step is not < x
3894 * An alternative is a bvec compare padded with an unused second component.
3895 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3896 * issue. Playing with not() is not possible either because not() does not accept
3899 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
3900 src0_param
.param_str
, src1_param
.param_str
);
3902 case WINED3DSIH_SGE
:
3903 /* Here we can use the step() function and safe a conditional */
3904 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
3907 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
3913 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction
*ins
)
3915 const char *condition_prefix
, *condition_suffix
;
3916 struct wined3d_shader_dst_param dst
;
3917 struct glsl_src_param src0_param
;
3918 struct glsl_src_param src1_param
;
3919 struct glsl_src_param src2_param
;
3920 BOOL temp_destination
= FALSE
;
3921 DWORD cmp_channel
= 0;
3926 switch (ins
->handler_idx
)
3928 case WINED3DSIH_CMP
:
3929 condition_prefix
= "";
3930 condition_suffix
= " >= 0.0";
3933 case WINED3DSIH_CND
:
3934 condition_prefix
= "";
3935 condition_suffix
= " > 0.5";
3938 case WINED3DSIH_MOVC
:
3939 condition_prefix
= "bool(";
3940 condition_suffix
= ")";
3944 FIXME("Unhandled instruction %#x.\n", ins
->handler_idx
);
3945 condition_prefix
= "<unhandled prefix>";
3946 condition_suffix
= "<unhandled suffix>";
3950 if (shader_is_scalar(&ins
->dst
[0].reg
) || shader_is_scalar(&ins
->src
[0].reg
))
3952 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3953 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
3954 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
3955 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
3957 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
3958 condition_prefix
, src0_param
.param_str
, condition_suffix
,
3959 src1_param
.param_str
, src2_param
.param_str
);
3965 /* Splitting the instruction up in multiple lines imposes a problem:
3966 * The first lines may overwrite source parameters of the following lines.
3967 * Deal with that by using a temporary destination register if needed. */
3968 if ((ins
->src
[0].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3969 && ins
->src
[0].reg
.type
== dst
.reg
.type
)
3970 || (ins
->src
[1].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3971 && ins
->src
[1].reg
.type
== dst
.reg
.type
)
3972 || (ins
->src
[2].reg
.idx
[0].offset
== dst
.reg
.idx
[0].offset
3973 && ins
->src
[2].reg
.type
== dst
.reg
.type
))
3974 temp_destination
= TRUE
;
3976 /* Cycle through all source0 channels. */
3977 for (i
= 0; i
< 4; ++i
)
3980 /* Find the destination channels which use the current source0 channel. */
3981 for (j
= 0; j
< 4; ++j
)
3983 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
3985 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
3986 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
3989 dst
.write_mask
= ins
->dst
[0].write_mask
& write_mask
;
3991 if (temp_destination
)
3993 if (!(write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
)))
3995 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
3997 else if (!(write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
, dst
.reg
.data_type
)))
4000 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
4001 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4002 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4004 shader_addline(ins
->ctx
->buffer
, "%s%s%s ? %s : %s);\n",
4005 condition_prefix
, src0_param
.param_str
, condition_suffix
,
4006 src1_param
.param_str
, src2_param
.param_str
);
4009 if (temp_destination
)
4011 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
4012 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4013 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
4017 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4018 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4019 * the compare is done per component of src0. */
4020 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
4022 struct glsl_src_param src0_param
;
4023 struct glsl_src_param src1_param
;
4024 struct glsl_src_param src2_param
;
4026 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4027 ins
->ctx
->reg_maps
->shader_version
.minor
);
4029 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
4031 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4032 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4033 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4034 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4036 if (ins
->coissue
&& ins
->dst
->write_mask
!= WINED3DSP_WRITEMASK_3
)
4037 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
4039 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
4040 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4044 shader_glsl_conditional_move(ins
);
4047 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4048 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
4050 struct glsl_src_param src0_param
;
4051 struct glsl_src_param src1_param
;
4052 struct glsl_src_param src2_param
;
4055 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4056 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4057 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4058 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4059 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
4060 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
4063 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4064 Vertex shaders to GLSL codes */
4065 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
4068 int nComponents
= 0;
4069 struct wined3d_shader_dst_param tmp_dst
= {{0}};
4070 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
4071 struct wined3d_shader_instruction tmp_ins
;
4073 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
4075 /* Set constants for the temporary argument */
4076 tmp_ins
.ctx
= ins
->ctx
;
4077 tmp_ins
.dst_count
= 1;
4078 tmp_ins
.dst
= &tmp_dst
;
4079 tmp_ins
.src_count
= 2;
4080 tmp_ins
.src
= tmp_src
;
4082 switch(ins
->handler_idx
)
4084 case WINED3DSIH_M4x4
:
4086 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
4088 case WINED3DSIH_M4x3
:
4090 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
4092 case WINED3DSIH_M3x4
:
4094 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4096 case WINED3DSIH_M3x3
:
4098 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4100 case WINED3DSIH_M3x2
:
4102 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
4108 tmp_dst
= ins
->dst
[0];
4109 tmp_src
[0] = ins
->src
[0];
4110 tmp_src
[1] = ins
->src
[1];
4111 for (i
= 0; i
< nComponents
; ++i
)
4113 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
4114 shader_glsl_dot(&tmp_ins
);
4115 ++tmp_src
[1].reg
.idx
[0].offset
;
4120 The LRP instruction performs a component-wise linear interpolation
4121 between the second and third operands using the first operand as the
4122 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4123 This is equivalent to mix(src2, src1, src0);
4125 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
4127 struct glsl_src_param src0_param
;
4128 struct glsl_src_param src1_param
;
4129 struct glsl_src_param src2_param
;
4132 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4134 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4135 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
4136 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
4138 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
4139 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
4142 /** Process the WINED3DSIO_LIT instruction in GLSL:
4143 * dst.x = dst.w = 1.0
4144 * dst.y = (src0.x > 0) ? src0.x
4145 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4146 * where src.w is clamped at +- 128
4148 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
4150 struct glsl_src_param src0_param
;
4151 struct glsl_src_param src1_param
;
4152 struct glsl_src_param src3_param
;
4155 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4156 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4158 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4159 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
4160 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
4162 /* The sdk specifies the instruction like this
4164 * if(src.x > 0.0) dst.y = src.x
4166 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4169 * (where power = src.w clamped between -128 and 128)
4171 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4172 * dst.x = 1.0 ... No further explanation needed
4173 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4174 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4175 * dst.w = 1.0. ... Nothing fancy.
4177 * So we still have one conditional in there. So do this:
4178 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4180 * 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),
4181 * 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.
4182 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4184 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4185 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4186 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4188 shader_addline(ins
->ctx
->buffer
,
4189 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4190 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4191 src0_param
.param_str
, src3_param
.param_str
, src1_param
.param_str
,
4192 src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
4195 /** Process the WINED3DSIO_DST instruction in GLSL:
4197 * dst.y = src0.x * src0.y
4201 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
4203 struct glsl_src_param src0y_param
;
4204 struct glsl_src_param src0z_param
;
4205 struct glsl_src_param src1y_param
;
4206 struct glsl_src_param src1w_param
;
4209 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4210 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4212 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
4213 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
4214 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
4215 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
4217 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4218 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
4221 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4222 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4223 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4225 * dst.x = cos(src0.?)
4226 * dst.y = sin(src0.?)
4230 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
4232 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4233 struct glsl_src_param src0_param
;
4236 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
4238 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4240 write_mask
= shader_glsl_append_dst(buffer
, ins
);
4243 case WINED3DSP_WRITEMASK_0
:
4244 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4247 case WINED3DSP_WRITEMASK_1
:
4248 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
4251 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
4252 shader_addline(buffer
, "vec2(cos(%s), sin(%s)));\n",
4253 src0_param
.param_str
, src0_param
.param_str
);
4257 ERR("Write mask should be .x, .y or .xy\n");
4264 if (ins
->dst
[0].reg
.type
!= WINED3DSPR_NULL
)
4267 if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
4271 write_mask
= shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
4272 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4273 shader_addline(buffer
, "tmp0%s = sin(%s);\n", dst_mask
, src0_param
.param_str
);
4275 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
4276 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4277 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4279 shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
4280 shader_addline(buffer
, "tmp0%s);\n", dst_mask
);
4284 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0], ins
->dst
[0].reg
.data_type
);
4285 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4286 shader_addline(buffer
, "sin(%s));\n", src0_param
.param_str
);
4289 else if (ins
->dst
[1].reg
.type
!= WINED3DSPR_NULL
)
4291 write_mask
= shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[1], ins
->dst
[1].reg
.data_type
);
4292 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4293 shader_addline(buffer
, "cos(%s));\n", src0_param
.param_str
);
4297 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4298 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4299 * generate invalid code
4301 static void shader_glsl_sgn(const struct wined3d_shader_instruction
*ins
)
4303 struct glsl_src_param src0_param
;
4306 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
4307 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
4309 shader_addline(ins
->ctx
->buffer
, "sign(%s));\n", src0_param
.param_str
);
4312 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4313 * Start a for() loop where src1.y is the initial value of aL,
4314 * increment aL by src1.z for a total of src1.x iterations.
4315 * Need to use a temporary variable for this operation.
4317 /* FIXME: I don't think nested loops will work correctly this way. */
4318 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
4320 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4321 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
4322 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
4323 const struct wined3d_shader_lconst
*constant
;
4324 struct glsl_src_param src1_param
;
4325 const DWORD
*control_values
= NULL
;
4327 if (ins
->ctx
->reg_maps
->shader_version
.major
< 4)
4329 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
4331 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4332 * class hardware doesn't support real varying indexing, but Microsoft
4333 * designed this feature for Shader model 2.x+. If the loop control is
4334 * known at compile time, the GLSL compiler can unroll the loop, and
4335 * replace indirect addressing with direct addressing. */
4336 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
4338 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
4340 if (constant
->idx
== ins
->src
[1].reg
.idx
[0].offset
)
4342 control_values
= constant
->value
;
4350 struct wined3d_shader_loop_control loop_control
;
4351 loop_control
.count
= control_values
[0];
4352 loop_control
.start
= control_values
[1];
4353 loop_control
.step
= (int)control_values
[2];
4355 if (loop_control
.step
> 0)
4357 shader_addline(buffer
, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4358 state
->current_loop_depth
, loop_control
.start
,
4359 state
->current_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
4360 state
->current_loop_depth
, loop_control
.step
);
4362 else if (loop_control
.step
< 0)
4364 shader_addline(buffer
, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4365 state
->current_loop_depth
, loop_control
.start
,
4366 state
->current_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
4367 state
->current_loop_depth
, loop_control
.step
);
4371 shader_addline(buffer
, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4372 state
->current_loop_depth
, loop_control
.start
, state
->current_loop_depth
,
4373 state
->current_loop_depth
, loop_control
.count
,
4374 state
->current_loop_depth
);
4379 shader_addline(buffer
, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4380 state
->current_loop_depth
, state
->current_loop_reg
,
4381 src1_param
.reg_name
, state
->current_loop_depth
, src1_param
.reg_name
,
4382 state
->current_loop_depth
, state
->current_loop_reg
, src1_param
.reg_name
);
4385 ++state
->current_loop_reg
;
4389 shader_addline(buffer
, "for (;;)\n{\n");
4392 ++state
->current_loop_depth
;
4395 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
4397 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4399 shader_addline(ins
->ctx
->buffer
, "}\n");
4401 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
4403 --state
->current_loop_depth
;
4404 --state
->current_loop_reg
;
4407 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
4409 --state
->current_loop_depth
;
4413 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
4415 struct wined3d_shader_parser_state
*state
= ins
->ctx
->state
;
4416 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
4417 const struct wined3d_shader_lconst
*constant
;
4418 struct glsl_src_param src0_param
;
4419 const DWORD
*control_values
= NULL
;
4421 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4422 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
4424 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, struct wined3d_shader_lconst
, entry
)
4426 if (constant
->idx
== ins
->src
[0].reg
.idx
[0].offset
)
4428 control_values
= constant
->value
;
4436 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4437 state
->current_loop_depth
, state
->current_loop_depth
,
4438 control_values
[0], state
->current_loop_depth
);
4442 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4443 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4444 state
->current_loop_depth
, state
->current_loop_depth
,
4445 src0_param
.param_str
, state
->current_loop_depth
);
4448 ++state
->current_loop_depth
;
4451 static void shader_glsl_switch(const struct wined3d_shader_instruction
*ins
)
4453 struct glsl_src_param src0_param
;
4455 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4456 shader_addline(ins
->ctx
->buffer
, "switch (%s)\n{\n", src0_param
.param_str
);
4459 static void shader_glsl_case(const struct wined3d_shader_instruction
*ins
)
4461 struct glsl_src_param src0_param
;
4463 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4464 shader_addline(ins
->ctx
->buffer
, "case %s:\n", src0_param
.param_str
);
4467 static void shader_glsl_default(const struct wined3d_shader_instruction
*ins
)
4469 shader_addline(ins
->ctx
->buffer
, "default:\n");
4472 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
4474 const char *condition
= (ins
->flags
== WINED3D_SHADER_CONDITIONAL_OP_NZ
) ? "bool" : "!bool";
4475 struct glsl_src_param src0_param
;
4477 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4478 shader_addline(ins
->ctx
->buffer
, "if (%s(%s)) {\n", condition
, src0_param
.param_str
);
4481 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
4483 struct glsl_src_param src0_param
;
4484 struct glsl_src_param src1_param
;
4486 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4487 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4489 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
4490 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
4493 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
4495 shader_addline(ins
->ctx
->buffer
, "} else {\n");
4498 static void shader_glsl_emit(const struct wined3d_shader_instruction
*ins
)
4500 unsigned int stream
= ins
->handler_idx
== WINED3DSIH_EMIT
? 0 : ins
->src
[0].reg
.idx
[0].offset
;
4502 shader_addline(ins
->ctx
->buffer
, "setup_gs_output(gs_out);\n");
4503 if (!ins
->ctx
->gl_info
->supported
[ARB_CLIP_CONTROL
])
4504 shader_glsl_fixup_position(ins
->ctx
->buffer
);
4507 shader_addline(ins
->ctx
->buffer
, "EmitVertex();\n");
4509 FIXME("Unhandled primitive stream %u.\n", stream
);
4512 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
4514 shader_addline(ins
->ctx
->buffer
, "break;\n");
4517 /* FIXME: According to MSDN the compare is done per component. */
4518 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
4520 struct glsl_src_param src0_param
;
4521 struct glsl_src_param src1_param
;
4523 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
4524 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4526 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
4527 src0_param
.param_str
, shader_glsl_get_rel_op(ins
->flags
), src1_param
.param_str
);
4530 static void shader_glsl_breakp(const struct wined3d_shader_instruction
*ins
)
4532 const char *condition
= (ins
->flags
== WINED3D_SHADER_CONDITIONAL_OP_NZ
) ? "bool" : "!bool";
4533 struct glsl_src_param src_param
;
4535 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
4536 shader_addline(ins
->ctx
->buffer
, "if (%s(%s)) break;\n", condition
, src_param
.param_str
);
4539 static void shader_glsl_continue(const struct wined3d_shader_instruction
*ins
)
4541 shader_addline(ins
->ctx
->buffer
, "continue;\n");
4544 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
4546 shader_addline(ins
->ctx
->buffer
, "}\n");
4547 shader_addline(ins
->ctx
->buffer
, "void subroutine%u()\n{\n", ins
->src
[0].reg
.idx
[0].offset
);
4549 /* Subroutines appear at the end of the shader. */
4550 ins
->ctx
->state
->in_subroutine
= TRUE
;
4553 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
4555 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
[0].offset
);
4558 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
4560 struct glsl_src_param src1_param
;
4562 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
4563 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n",
4564 src1_param
.param_str
, ins
->src
[0].reg
.idx
[0].offset
);
4567 static void shader_glsl_ret(const struct wined3d_shader_instruction
*ins
)
4569 const struct wined3d_shader_version
*version
= &ins
->ctx
->shader
->reg_maps
.shader_version
;
4571 if (version
->major
>= 4 && !ins
->ctx
->state
->in_subroutine
)
4573 shader_glsl_generate_shader_epilogue(ins
->ctx
);
4574 shader_addline(ins
->ctx
->buffer
, "return;\n");
4578 /*********************************************
4579 * Pixel Shader Specific Code begins here
4580 ********************************************/
4581 static void shader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
4583 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
4584 ins
->ctx
->reg_maps
->shader_version
.minor
);
4585 struct glsl_sample_function sample_function
;
4586 DWORD sample_flags
= 0;
4588 DWORD mask
= 0, swizzle
;
4589 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
4591 /* 1.0-1.4: Use destination register as sampler source.
4592 * 2.0+: Use provided sampler source. */
4593 if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4594 resource_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4596 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4598 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4600 DWORD flags
= (priv
->cur_ps_args
->tex_transform
>> resource_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
4601 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
4602 enum wined3d_shader_resource_type resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
4604 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4605 if (flags
& WINED3D_PSARGS_PROJECTED
&& resource_type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4607 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4608 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
4610 case WINED3D_TTFF_COUNT1
:
4611 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4613 case WINED3D_TTFF_COUNT2
:
4614 mask
= WINED3DSP_WRITEMASK_1
;
4616 case WINED3D_TTFF_COUNT3
:
4617 mask
= WINED3DSP_WRITEMASK_2
;
4619 case WINED3D_TTFF_COUNT4
:
4620 case WINED3D_TTFF_DISABLE
:
4621 mask
= WINED3DSP_WRITEMASK_3
;
4626 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
4628 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
4630 if (src_mod
== WINED3DSPSM_DZ
) {
4631 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4632 mask
= WINED3DSP_WRITEMASK_2
;
4633 } else if (src_mod
== WINED3DSPSM_DW
) {
4634 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4635 mask
= WINED3DSP_WRITEMASK_3
;
4640 if ((ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
4641 && ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
!= WINED3D_SHADER_RESOURCE_TEXTURE_CUBE
)
4643 /* ps 2.0 texldp instruction always divides by the fourth component. */
4644 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
4645 mask
= WINED3DSP_WRITEMASK_3
;
4649 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, resource_idx
, sample_flags
, &sample_function
);
4650 mask
|= sample_function
.coord_mask
;
4651 sample_function
.coord_mask
= mask
;
4653 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
4654 else swizzle
= ins
->src
[1].swizzle
;
4656 /* 1.0-1.3: Use destination register as coordinate source.
4657 1.4+: Use provided coordinate source register. */
4658 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
4661 shader_glsl_write_mask_to_str(mask
, coord_mask
);
4662 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
, NULL
,
4663 "T%u%s", resource_idx
, coord_mask
);
4667 struct glsl_src_param coord_param
;
4668 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
4669 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
4671 struct glsl_src_param bias
;
4672 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
4673 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
4674 NULL
, "%s", coord_param
.param_str
);
4676 shader_glsl_gen_sample_code(ins
, resource_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
, NULL
,
4677 "%s", coord_param
.param_str
);
4680 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4683 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
4685 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4686 struct glsl_src_param coord_param
, dx_param
, dy_param
;
4687 struct glsl_sample_function sample_function
;
4689 DWORD swizzle
= ins
->src
[1].swizzle
;
4691 if (!shader_glsl_has_core_grad(gl_info
, &ins
->ctx
->shader
->reg_maps
.shader_version
)
4692 && !gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
4694 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4695 shader_glsl_tex(ins
);
4699 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4701 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, WINED3D_GLSL_SAMPLE_GRAD
, &sample_function
);
4702 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4703 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.deriv_mask
, &dx_param
);
4704 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.deriv_mask
, &dy_param
);
4706 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
,
4707 NULL
, NULL
, "%s", coord_param
.param_str
);
4708 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4711 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
4713 const struct wined3d_shader_version
*shader_version
= &ins
->ctx
->reg_maps
->shader_version
;
4714 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4715 struct glsl_src_param coord_param
, lod_param
;
4716 struct glsl_sample_function sample_function
;
4717 DWORD swizzle
= ins
->src
[1].swizzle
;
4720 sampler_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4722 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, WINED3D_GLSL_SAMPLE_LOD
, &sample_function
);
4723 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4725 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
4727 if (shader_version
->type
== WINED3D_SHADER_TYPE_PIXEL
&& !shader_glsl_has_core_grad(gl_info
, shader_version
)
4728 && !gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
4730 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4731 * However, the NVIDIA drivers allow them in fragment shaders as well,
4732 * even without the appropriate extension. */
4733 WARN("Using %s in fragment shader.\n", sample_function
.name
->buffer
);
4735 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
, NULL
,
4736 "%s", coord_param
.param_str
);
4737 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4740 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map
*sampler_map
,
4741 unsigned int resource_idx
, unsigned int sampler_idx
)
4743 struct wined3d_shader_sampler_map_entry
*entries
= sampler_map
->entries
;
4746 for (i
= 0; i
< sampler_map
->count
; ++i
)
4748 if (entries
[i
].resource_idx
== resource_idx
&& entries
[i
].sampler_idx
== sampler_idx
)
4749 return entries
[i
].bind_idx
;
4752 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx
, sampler_idx
);
4757 static void shader_glsl_atomic(const struct wined3d_shader_instruction
*ins
)
4759 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
4760 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
4761 struct glsl_src_param image_coord_param
, image_data_param
;
4762 enum wined3d_shader_resource_type resource_type
;
4763 enum wined3d_data_type data_type
;
4764 unsigned int uav_idx
;
4768 switch (ins
->handler_idx
)
4770 case WINED3DSIH_ATOMIC_IADD
: op
= "imageAtomicAdd"; break;
4772 ERR("Unhandled opcode %#x.\n", ins
->handler_idx
);
4776 uav_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
4777 resource_type
= reg_maps
->uav_resource_info
[uav_idx
].type
;
4778 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
4780 ERR("Unexpected resource type %#x.\n", resource_type
);
4781 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
4783 data_type
= reg_maps
->uav_resource_info
[uav_idx
].data_type
;
4784 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
4786 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &image_coord_param
);
4787 shader_glsl_add_src_param_ext(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &image_data_param
, data_type
);
4788 shader_addline(ins
->ctx
->buffer
, "%s(%s_image%u, %s, %s);\n",
4789 op
, shader_glsl_get_prefix(version
->type
), uav_idx
,
4790 image_coord_param
.param_str
, image_data_param
.param_str
);
4793 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction
*ins
)
4795 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
4796 const struct wined3d_shader_version
*version
= ®_maps
->shader_version
;
4797 enum wined3d_shader_resource_type resource_type
;
4798 struct glsl_src_param image_coord_param
;
4799 enum wined3d_data_type data_type
;
4800 DWORD coord_mask
, write_mask
;
4801 unsigned int uav_idx
;
4802 char dst_swizzle
[6];
4804 uav_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4805 if (uav_idx
>= ARRAY_SIZE(reg_maps
->uav_resource_info
))
4807 ERR("Invalid UAV index %u.\n", uav_idx
);
4810 resource_type
= reg_maps
->uav_resource_info
[uav_idx
].type
;
4811 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
4813 ERR("Unexpected resource type %#x.\n", resource_type
);
4814 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
4816 data_type
= reg_maps
->uav_resource_info
[uav_idx
].data_type
;
4817 coord_mask
= (1u << resource_type_info
[resource_type
].coord_size
) - 1;
4819 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], data_type
);
4820 shader_glsl_get_swizzle(&ins
->src
[1], FALSE
, write_mask
, dst_swizzle
);
4822 shader_glsl_add_src_param(ins
, &ins
->src
[0], coord_mask
, &image_coord_param
);
4823 shader_addline(ins
->ctx
->buffer
, "imageLoad(%s_image%u, %s)%s);\n",
4824 shader_glsl_get_prefix(version
->type
), uav_idx
, image_coord_param
.param_str
, dst_swizzle
);
4827 static void shader_glsl_resinfo(const struct wined3d_shader_instruction
*ins
)
4829 const struct wined3d_shader_version
*version
= &ins
->ctx
->reg_maps
->shader_version
;
4830 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
4831 enum wined3d_shader_resource_type resource_type
;
4832 enum wined3d_shader_register_type reg_type
;
4833 unsigned int resource_idx
, bind_idx
, i
;
4834 enum wined3d_data_type dst_data_type
;
4835 struct glsl_src_param lod_param
;
4836 char dst_swizzle
[6];
4839 dst_data_type
= ins
->dst
[0].reg
.data_type
;
4840 if (ins
->flags
== WINED3DSI_RESINFO_UINT
)
4841 dst_data_type
= WINED3D_DATA_UINT
;
4842 else if (ins
->flags
)
4843 FIXME("Unhandled flags %#x.\n", ins
->flags
);
4845 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &ins
->dst
[0], dst_data_type
);
4846 shader_glsl_get_swizzle(&ins
->src
[1], FALSE
, write_mask
, dst_swizzle
);
4848 reg_type
= ins
->src
[1].reg
.type
;
4849 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4850 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &lod_param
);
4851 if (reg_type
== WINED3DSPR_RESOURCE
)
4853 resource_type
= ins
->ctx
->reg_maps
->resource_info
[resource_idx
].type
;
4854 bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
,
4855 resource_idx
, WINED3D_SAMPLER_DEFAULT
);
4859 resource_type
= ins
->ctx
->reg_maps
->uav_resource_info
[resource_idx
].type
;
4860 bind_idx
= resource_idx
;
4863 if (resource_type
>= ARRAY_SIZE(resource_type_info
))
4865 ERR("Unexpected resource type %#x.\n", resource_type
);
4866 resource_type
= WINED3D_SHADER_RESOURCE_TEXTURE_2D
;
4869 if (dst_data_type
== WINED3D_DATA_UINT
)
4870 shader_addline(ins
->ctx
->buffer
, "uvec4(");
4872 shader_addline(ins
->ctx
->buffer
, "vec4(");
4874 if (reg_type
== WINED3DSPR_RESOURCE
)
4876 shader_addline(ins
->ctx
->buffer
, "textureSize(%s_sampler%u, %s), ",
4877 shader_glsl_get_prefix(version
->type
), bind_idx
, lod_param
.param_str
);
4879 for (i
= 0; i
< 3 - resource_type_info
[resource_type
].resinfo_size
; ++i
)
4880 shader_addline(ins
->ctx
->buffer
, "0, ");
4882 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
4884 shader_addline(ins
->ctx
->buffer
, "textureQueryLevels(%s_sampler%u)",
4885 shader_glsl_get_prefix(version
->type
), bind_idx
);
4889 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4890 shader_addline(ins
->ctx
->buffer
, "1");
4895 shader_addline(ins
->ctx
->buffer
, "imageSize(%s_image%u), ",
4896 shader_glsl_get_prefix(version
->type
), bind_idx
);
4898 for (i
= 0; i
< 3 - resource_type_info
[resource_type
].resinfo_size
; ++i
)
4899 shader_addline(ins
->ctx
->buffer
, "0, ");
4901 /* For UAVs the returned miplevel count is always 1. */
4902 shader_addline(ins
->ctx
->buffer
, "1");
4905 shader_addline(ins
->ctx
->buffer
, ")%s);\n", dst_swizzle
);
4908 /* FIXME: The current implementation does not handle multisample textures correctly. */
4909 static void shader_glsl_ld(const struct wined3d_shader_instruction
*ins
)
4911 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
4912 struct glsl_src_param coord_param
, lod_param
;
4913 struct glsl_sample_function sample_function
;
4914 DWORD flags
= WINED3D_GLSL_SAMPLE_LOAD
;
4916 if (wined3d_shader_instruction_has_texel_offset(ins
))
4917 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
4919 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4920 sampler_idx
= WINED3D_SAMPLER_DEFAULT
;
4922 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
4923 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4924 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
4925 sampler_bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
, resource_idx
, sampler_idx
);
4926 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, ins
->src
[1].swizzle
,
4927 NULL
, NULL
, lod_param
.param_str
, &ins
->texel_offset
, "%s", coord_param
.param_str
);
4928 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4931 static void shader_glsl_sample(const struct wined3d_shader_instruction
*ins
)
4933 const char *lod_param_str
= NULL
, *dx_param_str
= NULL
, *dy_param_str
= NULL
;
4934 struct glsl_src_param coord_param
, lod_param
, dx_param
, dy_param
;
4935 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
4936 struct glsl_sample_function sample_function
;
4939 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_GRAD
)
4940 flags
|= WINED3D_GLSL_SAMPLE_GRAD
;
4941 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_LOD
)
4942 flags
|= WINED3D_GLSL_SAMPLE_LOD
;
4943 if (wined3d_shader_instruction_has_texel_offset(ins
))
4944 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
4946 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
4947 sampler_idx
= ins
->src
[2].reg
.idx
[0].offset
;
4949 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
4950 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
4952 switch (ins
->handler_idx
)
4954 case WINED3DSIH_SAMPLE
:
4956 case WINED3DSIH_SAMPLE_B
:
4957 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &lod_param
);
4958 lod_param_str
= lod_param
.param_str
;
4960 case WINED3DSIH_SAMPLE_GRAD
:
4961 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.deriv_mask
, &dx_param
);
4962 shader_glsl_add_src_param(ins
, &ins
->src
[4], sample_function
.deriv_mask
, &dy_param
);
4963 dx_param_str
= dx_param
.param_str
;
4964 dy_param_str
= dy_param
.param_str
;
4966 case WINED3DSIH_SAMPLE_LOD
:
4967 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &lod_param
);
4968 lod_param_str
= lod_param
.param_str
;
4971 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
4975 sampler_bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
, resource_idx
, sampler_idx
);
4976 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, ins
->src
[1].swizzle
,
4977 dx_param_str
, dy_param_str
, lod_param_str
, &ins
->texel_offset
, "%s", coord_param
.param_str
);
4978 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
4981 static void shader_glsl_sample_c(const struct wined3d_shader_instruction
*ins
)
4983 unsigned int resource_idx
, sampler_idx
, sampler_bind_idx
;
4984 struct glsl_src_param coord_param
, compare_param
;
4985 struct glsl_sample_function sample_function
;
4986 const char *lod_param
= NULL
;
4990 if (ins
->handler_idx
== WINED3DSIH_SAMPLE_C_LZ
)
4993 flags
|= WINED3D_GLSL_SAMPLE_LOD
;
4996 if (wined3d_shader_instruction_has_texel_offset(ins
))
4997 flags
|= WINED3D_GLSL_SAMPLE_OFFSET
;
4999 resource_idx
= ins
->src
[1].reg
.idx
[0].offset
;
5000 sampler_idx
= ins
->src
[2].reg
.idx
[0].offset
;
5002 shader_glsl_get_sample_function(ins
->ctx
, resource_idx
, sampler_idx
, flags
, &sample_function
);
5003 coord_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
5004 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
>> 1, &coord_param
);
5005 shader_glsl_add_src_param(ins
, &ins
->src
[3], WINED3DSP_WRITEMASK_0
, &compare_param
);
5006 sampler_bind_idx
= shader_glsl_find_sampler(&ins
->ctx
->reg_maps
->sampler_map
, resource_idx
, sampler_idx
);
5007 shader_glsl_gen_sample_code(ins
, sampler_bind_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
,
5008 NULL
, NULL
, lod_param
, &ins
->texel_offset
, "vec%u(%s, %s)",
5009 coord_size
, coord_param
.param_str
, compare_param
.param_str
);
5010 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5013 static void shader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
5015 /* FIXME: Make this work for more than just 2D textures */
5016 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5017 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5019 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
5023 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
5024 shader_addline(buffer
, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5025 ins
->dst
[0].reg
.idx
[0].offset
, dst_mask
);
5029 enum wined3d_shader_src_modifier src_mod
= ins
->src
[0].modifiers
;
5030 DWORD reg
= ins
->src
[0].reg
.idx
[0].offset
;
5031 char dst_swizzle
[6];
5033 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
5035 if (src_mod
== WINED3DSPSM_DZ
|| src_mod
== WINED3DSPSM_DW
)
5037 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
5038 struct glsl_src_param div_param
;
5039 DWORD src_writemask
= src_mod
== WINED3DSPSM_DZ
? WINED3DSP_WRITEMASK_2
: WINED3DSP_WRITEMASK_3
;
5041 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_writemask
, &div_param
);
5044 shader_addline(buffer
, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
5046 shader_addline(buffer
, "ffp_texcoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
5050 shader_addline(buffer
, "ffp_texcoord[%u]%s);\n", reg
, dst_swizzle
);
5055 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5056 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5057 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5058 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
5060 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5061 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5062 struct glsl_sample_function sample_function
;
5063 struct glsl_src_param src0_param
;
5066 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5068 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5069 * scalar, and projected sampling would require 4.
5071 * It is a dependent read - not valid with conditional NP2 textures
5073 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5074 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
5079 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
5080 NULL
, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
5084 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
5085 NULL
, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
5089 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
5090 NULL
, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
5094 FIXME("Unexpected mask size %u\n", mask_size
);
5097 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5100 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5101 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5102 static void shader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
5104 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5105 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
5106 struct glsl_src_param src0_param
;
5108 unsigned int mask_size
;
5110 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5111 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
5112 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5114 if (mask_size
> 1) {
5115 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
5117 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
5121 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5122 * Calculate the depth as dst.x / dst.y */
5123 static void shader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
5125 struct glsl_dst_param dst_param
;
5127 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
5129 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5130 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5131 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5132 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5135 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5136 dst_param
.reg_name
, dst_param
.reg_name
);
5139 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5140 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5141 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5142 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5144 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
5146 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5147 DWORD dstreg
= ins
->dst
[0].reg
.idx
[0].offset
;
5148 struct glsl_src_param src0_param
;
5150 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5152 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
5153 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5156 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5157 * Calculate the 1st of a 2-row matrix multiplication. */
5158 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
5160 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5161 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5162 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5163 struct glsl_src_param src0_param
;
5165 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5166 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5169 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5170 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5171 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
5173 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5174 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5175 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5176 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5177 struct glsl_src_param src0_param
;
5179 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5180 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx
->current_row
, reg
, src0_param
.param_str
);
5181 tex_mx
->texcoord_w
[tex_mx
->current_row
++] = reg
;
5184 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
5186 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5187 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5188 struct glsl_sample_function sample_function
;
5189 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5190 struct glsl_src_param src0_param
;
5192 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5193 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5195 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5197 /* Sample the texture using the calculated coordinates */
5198 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
, "tmp0.xy");
5199 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5202 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5203 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5204 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
5206 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5207 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5208 struct glsl_sample_function sample_function
;
5209 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5210 struct glsl_src_param src0_param
;
5212 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5213 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5215 /* Dependent read, not valid with conditional NP2 */
5216 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5218 /* Sample the texture using the calculated coordinates */
5219 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
, "tmp0.xyz");
5220 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5222 tex_mx
->current_row
= 0;
5225 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5226 * Perform the 3rd row of a 3x3 matrix multiply */
5227 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
5229 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5230 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5231 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5232 struct glsl_src_param src0_param
;
5235 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5237 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5238 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
5239 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
5241 tex_mx
->current_row
= 0;
5244 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5245 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5246 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
5248 struct glsl_src_param src0_param
;
5249 struct glsl_src_param src1_param
;
5250 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5251 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5252 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5253 struct glsl_sample_function sample_function
;
5254 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5257 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5258 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
5260 /* Perform the last matrix multiply operation */
5261 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
5262 /* Reflection calculation */
5263 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
5265 /* Dependent read, not valid with conditional NP2 */
5266 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5267 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
5269 /* Sample the texture */
5270 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
5271 NULL
, NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
5272 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5274 tex_mx
->current_row
= 0;
5277 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5278 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5279 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
5281 struct wined3d_string_buffer
*buffer
= ins
->ctx
->buffer
;
5282 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
5283 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
5284 struct glsl_sample_function sample_function
;
5285 DWORD reg
= ins
->dst
[0].reg
.idx
[0].offset
;
5286 struct glsl_src_param src0_param
;
5289 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
5291 /* Perform the last matrix multiply operation */
5292 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
5294 /* Construct the eye-ray vector from w coordinates */
5295 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5296 tex_mx
->texcoord_w
[0], tex_mx
->texcoord_w
[1], reg
);
5297 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5299 /* Dependent read, not valid with conditional NP2 */
5300 shader_glsl_get_sample_function(ins
->ctx
, reg
, reg
, 0, &sample_function
);
5301 shader_glsl_write_mask_to_str(sample_function
.coord_mask
, coord_mask
);
5303 /* Sample the texture using the calculated coordinates */
5304 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
,
5305 NULL
, NULL
, NULL
, NULL
, "tmp0%s", coord_mask
);
5306 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5308 tex_mx
->current_row
= 0;
5311 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5312 * Apply a fake bump map transform.
5313 * texbem is pshader <= 1.3 only, this saves a few version checks
5315 static void shader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
5317 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
5318 struct glsl_sample_function sample_function
;
5319 struct glsl_src_param coord_param
;
5325 sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5326 flags
= (priv
->cur_ps_args
->tex_transform
>> sampler_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
5327 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
5329 /* Dependent read, not valid with conditional NP2 */
5330 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5331 mask
= sample_function
.coord_mask
;
5333 shader_glsl_write_mask_to_str(mask
, coord_mask
);
5335 /* With projected textures, texbem only divides the static texture coord,
5336 * not the displacement, so we can't let GL handle this. */
5337 if (flags
& WINED3D_PSARGS_PROJECTED
)
5340 char coord_div_mask
[3];
5341 switch (flags
& ~WINED3D_PSARGS_PROJECTED
)
5343 case WINED3D_TTFF_COUNT1
:
5344 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5346 case WINED3D_TTFF_COUNT2
:
5347 div_mask
= WINED3DSP_WRITEMASK_1
;
5349 case WINED3D_TTFF_COUNT3
:
5350 div_mask
= WINED3DSP_WRITEMASK_2
;
5352 case WINED3D_TTFF_COUNT4
:
5353 case WINED3D_TTFF_DISABLE
:
5354 div_mask
= WINED3DSP_WRITEMASK_3
;
5357 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
5358 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
5361 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
5363 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
5364 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
5365 coord_param
.param_str
, coord_mask
);
5367 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
5369 struct glsl_src_param luminance_param
;
5370 struct glsl_dst_param dst_param
;
5372 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
5373 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
5375 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
5376 dst_param
.reg_name
, dst_param
.mask_str
,
5377 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
5379 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5382 static void shader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
5384 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5385 struct glsl_src_param src0_param
, src1_param
;
5387 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
5388 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
5390 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5391 shader_addline(ins
->ctx
->buffer
, "%s + bumpenv_mat%u * %s);\n",
5392 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
5395 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5396 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5397 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
5399 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5400 struct glsl_sample_function sample_function
;
5401 struct glsl_src_param src0_param
;
5403 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
5405 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5406 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
5407 "%s.wx", src0_param
.reg_name
);
5408 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5411 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5412 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5413 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
5415 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5416 struct glsl_sample_function sample_function
;
5417 struct glsl_src_param src0_param
;
5419 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
5421 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5422 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
5423 "%s.yz", src0_param
.reg_name
);
5424 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5427 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5428 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5429 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
5431 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
[0].offset
;
5432 struct glsl_sample_function sample_function
;
5433 struct glsl_src_param src0_param
;
5435 /* Dependent read, not valid with conditional NP2 */
5436 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sampler_idx
, 0, &sample_function
);
5437 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
5439 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, NULL
,
5440 "%s", src0_param
.param_str
);
5441 shader_glsl_release_sample_function(ins
->ctx
, &sample_function
);
5444 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5445 * If any of the first 3 components are < 0, discard this pixel */
5446 static void shader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
5448 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 4)
5450 struct glsl_src_param src_param
;
5452 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
5453 shader_addline(ins
->ctx
->buffer
, "if (bool(floatBitsToUint(%s))) discard;\n", src_param
.param_str
);
5457 struct glsl_dst_param dst_param
;
5459 /* The argument is a destination parameter, and no writemasks are allowed */
5460 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
5462 /* 2.0 shaders compare all 4 components in texkill. */
5463 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
5464 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
5465 /* 1.x shaders only compare the first 3 components, probably due to
5466 * the nature of the texkill instruction as a tex* instruction, and
5467 * phase, which kills all .w components. Even if all 4 components are
5468 * defined, only the first 3 are used. */
5470 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
5474 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5475 * dst = dot2(src0, src1) + src2 */
5476 static void shader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
5478 struct glsl_src_param src0_param
;
5479 struct glsl_src_param src1_param
;
5480 struct glsl_src_param src2_param
;
5482 unsigned int mask_size
;
5484 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
5485 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
5487 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
5488 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
5489 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
5491 if (mask_size
> 1) {
5492 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
5493 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
5495 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
5496 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
5500 static void shader_glsl_input_pack(const struct wined3d_shader
*shader
, struct wined3d_string_buffer
*buffer
,
5501 const struct wined3d_shader_signature
*input_signature
,
5502 const struct wined3d_shader_reg_maps
*reg_maps
,
5503 const struct ps_compile_args
*args
, const struct wined3d_gl_info
*gl_info
)
5507 for (i
= 0; i
< input_signature
->element_count
; ++i
)
5509 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
5510 const char *semantic_name
;
5515 if (!(reg_maps
->input_registers
& (1u << input
->register_idx
)))
5518 semantic_name
= input
->semantic_name
;
5519 semantic_idx
= input
->semantic_idx
;
5520 shader_glsl_write_mask_to_str(input
->mask
, reg_mask
);
5522 if (args
->vp_mode
== vertexshader
)
5524 if (input
->sysval_semantic
== WINED3D_SV_POSITION
&& !semantic_idx
)
5526 shader_addline(buffer
, "ps_in[%u]%s = vpos%s;\n",
5527 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5529 else if (args
->pointsprite
&& shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
5531 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input
->register_idx
);
5533 else if (input
->sysval_semantic
== WINED3D_SV_IS_FRONT_FACE
)
5535 shader_addline(buffer
, "ps_in[%u] = vec4("
5536 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
5537 input
->register_idx
);
5541 if (input
->sysval_semantic
)
5542 FIXME("Unhandled sysval semantic %#x.\n", input
->sysval_semantic
);
5543 shader_addline(buffer
, "ps_in[%u]%s = ps_link[%u]%s;\n",
5544 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
5545 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
);
5548 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
5550 if (args
->pointsprite
)
5551 shader_addline(buffer
, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5552 shader
->u
.ps
.input_reg_map
[input
->register_idx
]);
5553 else if (args
->vp_mode
== pretransformed
&& args
->texcoords_initialized
& (1u << semantic_idx
))
5554 shader_addline(buffer
, "ps_in[%u]%s = %s[%u]%s;\n",
5555 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
,
5556 gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
]
5557 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx
, reg_mask
);
5559 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5560 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5562 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
))
5565 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5566 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5567 else if (semantic_idx
== 1)
5568 shader_addline(buffer
, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5569 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5571 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5572 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5576 shader_addline(buffer
, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5577 shader
->u
.ps
.input_reg_map
[input
->register_idx
], reg_mask
, reg_mask
);
5582 /*********************************************
5583 * Vertex Shader Specific Code begins here
5584 ********************************************/
5586 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
)
5588 struct glsl_program_key key
;
5590 key
.vs_id
= entry
->vs
.id
;
5591 key
.gs_id
= entry
->gs
.id
;
5592 key
.ps_id
= entry
->ps
.id
;
5594 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
5596 ERR("Failed to insert program entry.\n");
5600 static struct glsl_shader_prog_link
*get_glsl_program_entry(const struct shader_glsl_priv
*priv
,
5601 GLuint vs_id
, GLuint gs_id
, GLuint ps_id
)
5603 struct wine_rb_entry
*entry
;
5604 struct glsl_program_key key
;
5610 entry
= wine_rb_get(&priv
->program_lookup
, &key
);
5611 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
5614 /* Context activation is done by the caller. */
5615 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
5616 struct glsl_shader_prog_link
*entry
)
5618 wine_rb_remove(&priv
->program_lookup
, &entry
->program_lookup_entry
);
5620 GL_EXTCALL(glDeleteProgram(entry
->id
));
5622 list_remove(&entry
->vs
.shader_entry
);
5624 list_remove(&entry
->gs
.shader_entry
);
5626 list_remove(&entry
->ps
.shader_entry
);
5627 HeapFree(GetProcessHeap(), 0, entry
);
5630 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv
*priv
,
5631 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
5632 const struct wined3d_shader_signature
*input_signature
,
5633 const struct wined3d_shader_reg_maps
*reg_maps_in
,
5634 const struct wined3d_shader_signature
*output_signature
,
5635 const struct wined3d_shader_reg_maps
*reg_maps_out
, const char *out_array_name
)
5637 struct wined3d_string_buffer
*destination
= string_buffer_get(&priv
->string_buffers
);
5638 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5639 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5640 unsigned int in_count
= vec4_varyings(3, gl_info
);
5641 unsigned int max_varyings
= legacy_context
? in_count
+ 2 : in_count
;
5642 DWORD in_idx
, *set
= NULL
;
5646 set
= wined3d_calloc(max_varyings
, sizeof(*set
));
5648 for (i
= 0; i
< input_signature
->element_count
; ++i
)
5650 const struct wined3d_shader_signature_element
*input
= &input_signature
->elements
[i
];
5652 if (!(reg_maps_in
->input_registers
& (1u << input
->register_idx
)))
5655 in_idx
= map
[input
->register_idx
];
5656 /* Declared, but not read register */
5659 if (in_idx
>= max_varyings
)
5661 FIXME("More input varyings declared than supported, expect issues.\n");
5665 if (in_idx
== in_count
)
5666 string_buffer_sprintf(destination
, "gl_FrontColor");
5667 else if (in_idx
== in_count
+ 1)
5668 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
5670 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, in_idx
);
5675 for (j
= 0; j
< output_signature
->element_count
; ++j
)
5677 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[j
];
5680 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
))
5681 || input
->semantic_idx
!= output
->semantic_idx
5682 || strcmp(input
->semantic_name
, output
->semantic_name
)
5683 || !(mask
= input
->mask
& output
->mask
))
5686 if (set
[in_idx
] == ~0u)
5688 set
[in_idx
] |= mask
& reg_maps_out
->u
.output_registers_mask
[output
->register_idx
];
5689 shader_glsl_write_mask_to_str(mask
, reg_mask
);
5691 shader_addline(buffer
, "%s%s = shader_out[%u]%s;\n",
5692 destination
->buffer
, reg_mask
, output
->register_idx
, reg_mask
);
5696 for (i
= 0; i
< max_varyings
; ++i
)
5700 if (!set
[i
] || set
[i
] == WINED3DSP_WRITEMASK_ALL
)
5707 if (!(set
[i
] & WINED3DSP_WRITEMASK_0
))
5708 reg_mask
[size
++] = 'x';
5709 if (!(set
[i
] & WINED3DSP_WRITEMASK_1
))
5710 reg_mask
[size
++] = 'y';
5711 if (!(set
[i
] & WINED3DSP_WRITEMASK_2
))
5712 reg_mask
[size
++] = 'z';
5713 if (!(set
[i
] & WINED3DSP_WRITEMASK_3
))
5714 reg_mask
[size
++] = 'w';
5715 reg_mask
[size
] = '\0';
5718 string_buffer_sprintf(destination
, "gl_FrontColor");
5719 else if (i
== in_count
+ 1)
5720 string_buffer_sprintf(destination
, "gl_FrontSecondaryColor");
5722 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, i
);
5725 shader_addline(buffer
, "%s.%s = 0.0;\n", destination
->buffer
, reg_mask
);
5727 shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
->buffer
, reg_mask
, size
);
5730 HeapFree(GetProcessHeap(), 0, set
);
5731 string_buffer_release(&priv
->string_buffers
, destination
);
5734 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv
*priv
,
5735 unsigned int input_count
, const struct wined3d_shader_signature
*output_signature
,
5736 const struct wined3d_shader_reg_maps
*reg_maps_out
, const char *out_array_name
)
5738 struct wined3d_string_buffer
*destination
= string_buffer_get(&priv
->string_buffers
);
5739 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5743 for (i
= 0; i
< output_signature
->element_count
; ++i
)
5745 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[i
];
5747 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
)))
5750 if (output
->register_idx
>= input_count
)
5753 string_buffer_sprintf(destination
, "%s[%u]", out_array_name
, output
->register_idx
);
5755 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
5757 shader_addline(buffer
, "%s%s = shader_out[%u]%s;\n",
5758 destination
->buffer
, reg_mask
, output
->register_idx
, reg_mask
);
5761 string_buffer_release(&priv
->string_buffers
, destination
);
5764 /* Context activation is done by the caller. */
5765 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv
*priv
,
5766 const struct wined3d_shader
*vs
, unsigned int input_count
,
5767 const struct wined3d_gl_info
*gl_info
)
5769 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5770 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5773 shader_addline(buffer
, "varying out vec4 gs_in[%u];\n", input_count
);
5775 shader_addline(buffer
, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count
);
5776 shader_addline(buffer
, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs
->limits
->packed_output
);
5778 shader_glsl_setup_sm4_shader_output(priv
, input_count
, &vs
->output_signature
, &vs
->reg_maps
,
5779 legacy_context
? "gs_in" : "gs_in.gs_in");
5781 shader_addline(buffer
, "}\n");
5784 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv
*priv
,
5785 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
5786 const struct wined3d_shader_signature
*input_signature
,
5787 const struct wined3d_shader_reg_maps
*reg_maps_in
, unsigned int input_count
,
5788 const struct wined3d_shader_signature
*output_signature
,
5789 const struct wined3d_shader_reg_maps
*reg_maps_out
, BOOL per_vertex_point_size
)
5791 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5792 const char *semantic_name
;
5797 /* First, sort out position and point size system values. */
5798 for (i
= 0; i
< output_signature
->element_count
; ++i
)
5800 const struct wined3d_shader_signature_element
*output
= &output_signature
->elements
[i
];
5802 if (!(reg_maps_out
->output_registers
& (1u << output
->register_idx
)))
5805 semantic_name
= output
->semantic_name
;
5806 semantic_idx
= output
->semantic_idx
;
5807 shader_glsl_write_mask_to_str(output
->mask
, reg_mask
);
5809 if (output
->sysval_semantic
== WINED3D_SV_POSITION
&& !semantic_idx
)
5811 shader_addline(buffer
, "gl_Position%s = shader_out[%u]%s;\n",
5812 reg_mask
, output
->register_idx
, reg_mask
);
5814 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
5816 shader_addline(buffer
, "gl_PointSize = clamp(shader_out[%u].%c, "
5817 "ffp_point.size_min, ffp_point.size_max);\n", output
->register_idx
, reg_mask
[1]);
5819 else if (output
->sysval_semantic
)
5821 FIXME("Unhandled sysval semantic %#x.\n", output
->sysval_semantic
);
5825 /* Then, setup the pixel shader input. */
5826 if (reg_maps_out
->shader_version
.major
< 4)
5827 shader_glsl_setup_vs3_output(priv
, gl_info
, map
, input_signature
, reg_maps_in
,
5828 output_signature
, reg_maps_out
, "ps_link");
5830 shader_glsl_setup_sm4_shader_output(priv
, input_count
, output_signature
, reg_maps_out
, "ps_link");
5833 /* Context activation is done by the caller. */
5834 static GLuint
shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv
*priv
,
5835 const struct wined3d_shader
*vs
, const struct wined3d_shader
*ps
,
5836 BOOL per_vertex_point_size
, BOOL flatshading
, const struct wined3d_gl_info
*gl_info
)
5838 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5840 DWORD ps_major
= ps
? ps
->reg_maps
.shader_version
.major
: 0;
5842 const char *semantic_name
;
5845 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
5847 string_buffer_clear(buffer
);
5849 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, &vs
->reg_maps
.shader_version
));
5851 if (per_vertex_point_size
)
5853 shader_addline(buffer
, "uniform struct\n{\n");
5854 shader_addline(buffer
, " float size_min;\n");
5855 shader_addline(buffer
, " float size_max;\n");
5856 shader_addline(buffer
, "} ffp_point;\n");
5861 DWORD colors_written_mask
[2] = {0};
5862 DWORD texcoords_written_mask
[MAX_TEXTURES
] = {0};
5864 if (!legacy_context
)
5866 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_diffuse;\n");
5867 declare_out_varying(gl_info
, buffer
, flatshading
, "vec4 ffp_varying_specular;\n");
5868 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
5869 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
5872 shader_addline(buffer
, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs
->limits
->packed_output
);
5874 for (i
= 0; i
< vs
->output_signature
.element_count
; ++i
)
5876 const struct wined3d_shader_signature_element
*output
= &vs
->output_signature
.elements
[i
];
5879 if (!(vs
->reg_maps
.output_registers
& (1u << output
->register_idx
)))
5882 semantic_name
= output
->semantic_name
;
5883 semantic_idx
= output
->semantic_idx
;
5884 write_mask
= output
->mask
;
5885 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
5887 if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_COLOR
) && semantic_idx
< 2)
5890 shader_addline(buffer
, "gl_Front%sColor%s = shader_out[%u]%s;\n",
5891 semantic_idx
? "Secondary" : "", reg_mask
, output
->register_idx
, reg_mask
);
5893 shader_addline(buffer
, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
5894 semantic_idx
? "specular" : "diffuse", reg_mask
, output
->register_idx
, reg_mask
);
5896 colors_written_mask
[semantic_idx
] = write_mask
;
5898 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_POSITION
) && !semantic_idx
)
5900 shader_addline(buffer
, "gl_Position%s = shader_out[%u]%s;\n",
5901 reg_mask
, output
->register_idx
, reg_mask
);
5903 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_TEXCOORD
))
5905 if (semantic_idx
< MAX_TEXTURES
)
5907 shader_addline(buffer
, "%s[%u]%s = shader_out[%u]%s;\n",
5908 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord",
5909 semantic_idx
, reg_mask
, output
->register_idx
, reg_mask
);
5910 texcoords_written_mask
[semantic_idx
] = write_mask
;
5913 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_PSIZE
) && per_vertex_point_size
)
5915 shader_addline(buffer
, "gl_PointSize = clamp(shader_out[%u].%c, "
5916 "ffp_point.size_min, ffp_point.size_max);\n", output
->register_idx
, reg_mask
[1]);
5918 else if (shader_match_semantic(semantic_name
, WINED3D_DECL_USAGE_FOG
))
5920 shader_addline(buffer
, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
5921 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5922 output
->register_idx
, reg_mask
[1]);
5926 for (i
= 0; i
< 2; ++i
)
5928 if (colors_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
5930 shader_glsl_write_mask_to_str(~colors_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
5932 shader_addline(buffer
, "%s%s = vec4(1.0)%s;\n",
5933 legacy_context
? "gl_FrontColor" : "ffp_varying_diffuse",
5934 reg_mask
, reg_mask
);
5936 shader_addline(buffer
, "%s%s = vec4(0.0)%s;\n",
5937 legacy_context
? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5938 reg_mask
, reg_mask
);
5941 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
5943 if (ps
&& !(ps
->reg_maps
.texcoord
& (1u << i
)))
5946 if (texcoords_written_mask
[i
] != WINED3DSP_WRITEMASK_ALL
)
5948 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
5949 && !texcoords_written_mask
[i
])
5952 shader_glsl_write_mask_to_str(~texcoords_written_mask
[i
] & WINED3DSP_WRITEMASK_ALL
, reg_mask
);
5953 shader_addline(buffer
, "%s[%u]%s = vec4(0.0)%s;\n",
5954 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
, reg_mask
, reg_mask
);
5960 UINT in_count
= min(vec4_varyings(ps_major
, gl_info
), ps
->limits
->packed_input
);
5962 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", in_count
);
5963 shader_addline(buffer
, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs
->limits
->packed_output
);
5964 shader_glsl_setup_sm3_rasterizer_input(priv
, gl_info
, ps
->u
.ps
.input_reg_map
, &ps
->input_signature
,
5965 &ps
->reg_maps
, 0, &vs
->output_signature
, &vs
->reg_maps
, per_vertex_point_size
);
5968 shader_addline(buffer
, "}\n");
5970 ret
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
5971 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5972 shader_glsl_compile(gl_info
, ret
, buffer
->buffer
);
5977 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv
*priv
,
5978 const struct wined3d_shader
*shader
, unsigned int input_count
,
5979 const struct wined3d_gl_info
*gl_info
)
5981 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
5984 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info
), input_count
));
5986 shader_addline(buffer
, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
5987 shader_glsl_get_prefix(shader
->reg_maps
.shader_version
.type
), shader
->limits
->packed_output
);
5989 shader_glsl_setup_sm3_rasterizer_input(priv
, gl_info
, NULL
, NULL
,
5990 NULL
, input_count
, &shader
->output_signature
, &shader
->reg_maps
, FALSE
);
5992 shader_addline(buffer
, "}\n");
5995 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer
*buffer
,
5996 const struct wined3d_gl_info
*gl_info
)
5998 const char *output
= get_fragment_output(gl_info
);
6000 shader_addline(buffer
, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output
);
6001 shader_addline(buffer
, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6002 shader_addline(buffer
, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output
);
6003 shader_addline(buffer
, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output
);
6004 shader_addline(buffer
, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output
);
6005 shader_addline(buffer
, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output
, output
);
6008 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer
*buffer
,
6009 const struct wined3d_gl_info
*gl_info
, enum wined3d_ffp_ps_fog_mode mode
)
6011 const char *output
= get_fragment_output(gl_info
);
6015 case WINED3D_FFP_PS_FOG_OFF
:
6018 case WINED3D_FFP_PS_FOG_LINEAR
:
6019 shader_addline(buffer
, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6022 case WINED3D_FFP_PS_FOG_EXP
:
6023 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6026 case WINED3D_FFP_PS_FOG_EXP2
:
6027 shader_addline(buffer
, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6028 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6032 ERR("Invalid fog mode %#x.\n", mode
);
6036 shader_addline(buffer
, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6040 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer
*buffer
,
6041 const struct wined3d_gl_info
*gl_info
, enum wined3d_cmp_func alpha_func
)
6043 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6044 * flipping all the operators here, just negate the comparison below. */
6045 static const char * const comparison_operator
[] =
6047 "", /* WINED3D_CMP_NEVER */
6048 "<", /* WINED3D_CMP_LESS */
6049 "==", /* WINED3D_CMP_EQUAL */
6050 "<=", /* WINED3D_CMP_LESSEQUAL */
6051 ">", /* WINED3D_CMP_GREATER */
6052 "!=", /* WINED3D_CMP_NOTEQUAL */
6053 ">=", /* WINED3D_CMP_GREATEREQUAL */
6054 "" /* WINED3D_CMP_ALWAYS */
6057 if (alpha_func
== WINED3D_CMP_ALWAYS
)
6060 if (alpha_func
!= WINED3D_CMP_NEVER
)
6061 shader_addline(buffer
, "if (!(%s[0].a %s alpha_test_ref))\n",
6062 get_fragment_output(gl_info
), comparison_operator
[alpha_func
- WINED3D_CMP_NEVER
]);
6063 shader_addline(buffer
, " discard;\n");
6066 static void shader_glsl_enable_extensions(struct wined3d_string_buffer
*buffer
,
6067 const struct wined3d_gl_info
*gl_info
)
6069 if (gl_info
->supported
[ARB_SHADER_BIT_ENCODING
])
6070 shader_addline(buffer
, "#extension GL_ARB_shader_bit_encoding : enable\n");
6071 if (gl_info
->supported
[ARB_SHADER_IMAGE_LOAD_STORE
])
6072 shader_addline(buffer
, "#extension GL_ARB_shader_image_load_store : enable\n");
6073 if (gl_info
->supported
[ARB_SHADER_IMAGE_SIZE
])
6074 shader_addline(buffer
, "#extension GL_ARB_shader_image_size : enable\n");
6075 if (gl_info
->supported
[ARB_TEXTURE_QUERY_LEVELS
])
6076 shader_addline(buffer
, "#extension GL_ARB_texture_query_levels : enable\n");
6077 if (gl_info
->supported
[ARB_UNIFORM_BUFFER_OBJECT
])
6078 shader_addline(buffer
, "#extension GL_ARB_uniform_buffer_object : enable\n");
6079 if (gl_info
->supported
[EXT_GPU_SHADER4
])
6080 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
6081 if (gl_info
->supported
[EXT_TEXTURE_ARRAY
])
6082 shader_addline(buffer
, "#extension GL_EXT_texture_array : enable\n");
6085 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info
*gl_info
,
6086 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
6087 const struct ps_compile_args
*args
)
6089 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6091 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6092 if (reg_maps
->shader_version
.major
< 2)
6093 shader_addline(buffer
, "%s[0] = R0;\n", get_fragment_output(gl_info
));
6095 if (args
->srgb_correction
)
6096 shader_glsl_generate_srgb_write_correction(buffer
, gl_info
);
6098 /* SM < 3 does not replace the fog stage. */
6099 if (reg_maps
->shader_version
.major
< 3)
6100 shader_glsl_generate_fog_code(buffer
, gl_info
, args
->fog
);
6102 shader_glsl_generate_alpha_test(buffer
, gl_info
, args
->alpha_test_func
+ 1);
6105 /* Context activation is done by the caller. */
6106 static GLuint
shader_glsl_generate_pshader(const struct wined3d_context
*context
,
6107 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
6108 const struct wined3d_shader
*shader
,
6109 const struct ps_compile_args
*args
, struct ps_np2fixup_info
*np2fixup_info
)
6111 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6112 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6113 const DWORD
*function
= shader
->function
;
6114 struct shader_glsl_ctx_priv priv_ctx
;
6115 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6117 /* Create the hw GLSL shader object and assign it as the shader->prgId */
6118 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
6120 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
6121 priv_ctx
.cur_ps_args
= args
;
6122 priv_ctx
.cur_np2fixup_info
= np2fixup_info
;
6123 priv_ctx
.string_buffers
= string_buffers
;
6125 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, ®_maps
->shader_version
));
6127 shader_glsl_enable_extensions(buffer
, gl_info
);
6128 if (gl_info
->supported
[ARB_DERIVATIVE_CONTROL
])
6129 shader_addline(buffer
, "#extension GL_ARB_derivative_control : enable\n");
6130 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
6131 shader_addline(buffer
, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6132 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
6133 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
6134 /* The spec says that it doesn't have to be explicitly enabled, but the
6135 * nvidia drivers write a warning if we don't do so. */
6136 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
6137 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
6139 /* Base Declarations */
6140 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
6142 shader_addline(buffer
, "void main()\n{\n");
6144 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6145 * add approximately 0.5. This causes off-by-one problems as spotted by
6146 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6147 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6148 * causes precision troubles when we just subtract 0.5.
6150 * To deal with that, just floor() the position. This will eliminate the
6151 * fraction on all cards.
6153 * TODO: Test how this behaves with multisampling.
6155 * An advantage of floor is that it works even if the driver doesn't add
6156 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6157 * to return in gl_FragCoord, even though coordinates specify the pixel
6158 * centers instead of the pixel corners. This code will behave correctly
6159 * on drivers that returns integer values. */
6162 if (gl_info
->supported
[ARB_FRAGMENT_COORD_CONVENTIONS
])
6163 shader_addline(buffer
, "vpos = gl_FragCoord;\n");
6164 else if (context
->d3d_info
->wined3d_creation_flags
& WINED3D_PIXEL_CENTER_INTEGER
)
6165 shader_addline(buffer
,
6166 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6168 shader_addline(buffer
,
6169 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6172 if (reg_maps
->shader_version
.major
< 3 || args
->vp_mode
!= vertexshader
)
6175 WORD map
= reg_maps
->texcoord
;
6179 if (glsl_is_color_reg_read(shader
, 0))
6180 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
6181 if (glsl_is_color_reg_read(shader
, 1))
6182 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
6185 for (i
= 0; map
; map
>>= 1, ++i
)
6189 if (args
->pointsprite
)
6190 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i
);
6191 else if (args
->texcoords_initialized
& (1u << i
))
6192 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n", i
,
6193 legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", i
);
6195 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", i
);
6196 shader_addline(buffer
, "vec4 T%u = ffp_texcoord[%u];\n", i
, i
);
6201 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6204 /* Pack 3.0 inputs */
6205 if (reg_maps
->shader_version
.major
>= 3)
6206 shader_glsl_input_pack(shader
, buffer
, &shader
->input_signature
, reg_maps
, args
, gl_info
);
6208 /* Base Shader Body */
6209 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
6211 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6212 if (reg_maps
->shader_version
.major
< 4)
6213 shader_glsl_generate_ps_epilogue(gl_info
, buffer
, shader
, args
);
6215 shader_addline(buffer
, "}\n");
6217 TRACE("Compiling shader object %u.\n", shader_id
);
6218 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6223 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info
*gl_info
,
6224 struct wined3d_string_buffer
*buffer
, const struct wined3d_shader
*shader
,
6225 const struct vs_compile_args
*args
)
6227 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6228 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6231 /* Unpack outputs. */
6232 shader_addline(buffer
, "setup_vs_output(vs_out);\n");
6234 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6235 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6236 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6237 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6239 if (reg_maps
->shader_version
.major
< 3)
6241 if (args
->fog_src
== VS_FOG_Z
)
6242 shader_addline(buffer
, "%s = gl_Position.z;\n",
6243 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6244 else if (!reg_maps
->fog
)
6245 shader_addline(buffer
, "%s = 0.0;\n",
6246 legacy_context
? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6249 /* We always store the clipplanes without y inversion. */
6250 if (args
->clip_enabled
)
6253 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
6255 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
6256 shader_addline(buffer
, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i
, i
);
6259 if (args
->point_size
&& !args
->per_vertex_point_size
)
6260 shader_addline(buffer
, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6262 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
6263 shader_glsl_fixup_position(buffer
);
6266 /* Context activation is done by the caller. */
6267 static GLuint
shader_glsl_generate_vshader(const struct wined3d_context
*context
,
6268 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
, const struct vs_compile_args
*args
)
6270 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
6271 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6272 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6273 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6274 const DWORD
*function
= shader
->function
;
6275 struct shader_glsl_ctx_priv priv_ctx
;
6277 /* Create the hw GLSL shader program and assign it as the shader->prgId */
6278 GLuint shader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
6280 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, ®_maps
->shader_version
));
6282 shader_glsl_enable_extensions(buffer
, gl_info
);
6283 if (gl_info
->supported
[ARB_DRAW_INSTANCED
])
6284 shader_addline(buffer
, "#extension GL_ARB_draw_instanced : enable\n");
6285 if (gl_info
->supported
[ARB_EXPLICIT_ATTRIB_LOCATION
])
6286 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
6288 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
6289 priv_ctx
.cur_vs_args
= args
;
6290 priv_ctx
.string_buffers
= string_buffers
;
6292 /* Base Declarations */
6293 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
6295 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
&& !gl_info
->supported
[ARB_CLIP_CONTROL
])
6296 shader_addline(buffer
, "uniform vec4 pos_fixup;\n");
6298 if (reg_maps
->shader_version
.major
>= 4)
6300 if (args
->next_shader_type
== WINED3D_SHADER_TYPE_PIXEL
)
6301 shader_glsl_generate_sm4_rasterizer_input_setup(priv
, shader
, args
->next_shader_input_count
, gl_info
);
6302 else if (args
->next_shader_type
== WINED3D_SHADER_TYPE_GEOMETRY
)
6303 shader_glsl_generate_vs_gs_setup(priv
, shader
, args
->next_shader_input_count
, gl_info
);
6306 shader_addline(buffer
, "void main()\n{\n");
6308 /* Base Shader Body */
6309 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
6311 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6312 if (reg_maps
->shader_version
.major
< 4)
6313 shader_glsl_generate_vs_epilogue(gl_info
, buffer
, shader
, args
);
6315 shader_addline(buffer
, "}\n");
6317 TRACE("Compiling shader object %u.\n", shader_id
);
6318 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6323 /* Context activation is done by the caller. */
6324 static GLuint
shader_glsl_generate_geometry_shader(const struct wined3d_context
*context
,
6325 struct shader_glsl_priv
*priv
, const struct wined3d_shader
*shader
, const struct gs_compile_args
*args
)
6327 struct wined3d_string_buffer_list
*string_buffers
= &priv
->string_buffers
;
6328 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
6329 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6330 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
6331 const DWORD
*function
= shader
->function
;
6332 struct shader_glsl_ctx_priv priv_ctx
;
6335 shader_id
= GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER
));
6337 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, ®_maps
->shader_version
));
6339 shader_glsl_enable_extensions(buffer
, gl_info
);
6340 if (gl_info
->supported
[ARB_GEOMETRY_SHADER4
])
6341 shader_addline(buffer
, "#extension GL_ARB_geometry_shader4 : enable\n");
6343 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
6344 priv_ctx
.string_buffers
= string_buffers
;
6345 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
6346 if (!gl_info
->supported
[ARB_CLIP_CONTROL
])
6347 shader_addline(buffer
, "uniform vec4 pos_fixup;\n");
6348 shader_glsl_generate_sm4_rasterizer_input_setup(priv
, shader
, args
->ps_input_count
, gl_info
);
6349 shader_addline(buffer
, "void main()\n{\n");
6350 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
6351 shader_addline(buffer
, "}\n");
6353 TRACE("Compiling shader object %u.\n", shader_id
);
6354 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
6359 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context
*ctx
)
6361 const struct shader_glsl_ctx_priv
*priv
= ctx
->backend_data
;
6362 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
6363 const struct wined3d_shader
*shader
= ctx
->shader
;
6365 switch (shader
->reg_maps
.shader_version
.type
)
6367 case WINED3D_SHADER_TYPE_PIXEL
:
6368 shader_glsl_generate_ps_epilogue(gl_info
, ctx
->buffer
, shader
, priv
->cur_ps_args
);
6370 case WINED3D_SHADER_TYPE_VERTEX
:
6371 shader_glsl_generate_vs_epilogue(gl_info
, ctx
->buffer
, shader
, priv
->cur_vs_args
);
6373 case WINED3D_SHADER_TYPE_GEOMETRY
:
6376 FIXME("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
6381 static GLuint
find_glsl_pshader(const struct wined3d_context
*context
,
6382 struct wined3d_string_buffer
*buffer
, struct wined3d_string_buffer_list
*string_buffers
,
6383 struct wined3d_shader
*shader
,
6384 const struct ps_compile_args
*args
, const struct ps_np2fixup_info
**np2fixup_info
)
6386 struct glsl_ps_compiled_shader
*gl_shaders
, *new_array
;
6387 struct glsl_shader_private
*shader_data
;
6388 struct ps_np2fixup_info
*np2fixup
;
6393 if (!shader
->backend_data
)
6395 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
6396 if (!shader
->backend_data
)
6398 ERR("Failed to allocate backend data.\n");
6402 shader_data
= shader
->backend_data
;
6403 gl_shaders
= shader_data
->gl_shaders
.ps
;
6405 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6406 * so a linear search is more performant than a hashmap or a binary search
6407 * (cache coherency etc)
6409 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
6411 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
6413 if (args
->np2_fixup
)
6414 *np2fixup_info
= &gl_shaders
[i
].np2fixup
;
6415 return gl_shaders
[i
].id
;
6419 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
6420 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
6421 if (shader_data
->num_gl_shaders
)
6423 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
6424 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
,
6425 new_size
* sizeof(*gl_shaders
));
6429 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
6434 ERR("Out of memory\n");
6437 shader_data
->gl_shaders
.ps
= new_array
;
6438 shader_data
->shader_array_size
= new_size
;
6439 gl_shaders
= new_array
;
6442 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
6444 np2fixup
= &gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
;
6445 memset(np2fixup
, 0, sizeof(*np2fixup
));
6446 *np2fixup_info
= args
->np2_fixup
? np2fixup
: NULL
;
6448 pixelshader_update_resource_types(shader
, args
->tex_types
);
6450 string_buffer_clear(buffer
);
6451 ret
= shader_glsl_generate_pshader(context
, buffer
, string_buffers
, shader
, args
, np2fixup
);
6452 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
6457 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
6458 const DWORD use_map
)
6460 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
6461 if((stored
->clip_enabled
) != new->clip_enabled
) return FALSE
;
6462 if (stored
->point_size
!= new->point_size
)
6464 if (stored
->per_vertex_point_size
!= new->per_vertex_point_size
)
6466 if (stored
->flatshading
!= new->flatshading
)
6468 if (stored
->next_shader_type
!= new->next_shader_type
)
6470 if (stored
->next_shader_input_count
!= new->next_shader_input_count
)
6472 return stored
->fog_src
== new->fog_src
;
6475 static GLuint
find_glsl_vshader(const struct wined3d_context
*context
, struct shader_glsl_priv
*priv
,
6476 struct wined3d_shader
*shader
, const struct vs_compile_args
*args
)
6480 DWORD use_map
= context
->stream_info
.use_map
;
6481 struct glsl_vs_compiled_shader
*gl_shaders
, *new_array
;
6482 struct glsl_shader_private
*shader_data
;
6485 if (!shader
->backend_data
)
6487 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
6488 if (!shader
->backend_data
)
6490 ERR("Failed to allocate backend data.\n");
6494 shader_data
= shader
->backend_data
;
6495 gl_shaders
= shader_data
->gl_shaders
.vs
;
6497 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6498 * so a linear search is more performant than a hashmap or a binary search
6499 * (cache coherency etc)
6501 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
6503 if (vs_args_equal(&gl_shaders
[i
].args
, args
, use_map
))
6504 return gl_shaders
[i
].id
;
6507 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
6509 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
6510 if (shader_data
->num_gl_shaders
)
6512 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
6513 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
,
6514 new_size
* sizeof(*gl_shaders
));
6518 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders
));
6523 ERR("Out of memory\n");
6526 shader_data
->gl_shaders
.vs
= new_array
;
6527 shader_data
->shader_array_size
= new_size
;
6528 gl_shaders
= new_array
;
6531 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
6533 string_buffer_clear(&priv
->shader_buffer
);
6534 ret
= shader_glsl_generate_vshader(context
, priv
, shader
, args
);
6535 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
6540 static GLuint
find_glsl_geometry_shader(const struct wined3d_context
*context
,
6541 struct shader_glsl_priv
*priv
, struct wined3d_shader
*shader
, const struct gs_compile_args
*args
)
6543 struct glsl_gs_compiled_shader
*gl_shaders
, *new_array
;
6544 struct glsl_shader_private
*shader_data
;
6545 unsigned int i
, new_size
;
6548 if (!shader
->backend_data
)
6550 if (!(shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
))))
6552 ERR("Failed to allocate backend data.\n");
6556 shader_data
= shader
->backend_data
;
6557 gl_shaders
= shader_data
->gl_shaders
.gs
;
6559 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
6561 if (!memcmp(&gl_shaders
[i
].args
, args
, sizeof(*args
)))
6562 return gl_shaders
[i
].id
;
6565 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
6567 if (shader_data
->num_gl_shaders
)
6569 new_size
= shader_data
->shader_array_size
+ 1;
6570 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
,
6571 new_size
* sizeof(*new_array
));
6575 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array
));
6581 ERR("Failed to allocate GL shaders array.\n");
6584 shader_data
->gl_shaders
.gs
= new_array
;
6585 shader_data
->shader_array_size
= new_size
;
6586 gl_shaders
= new_array
;
6588 string_buffer_clear(&priv
->shader_buffer
);
6589 ret
= shader_glsl_generate_geometry_shader(context
, priv
, shader
, args
);
6590 gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
6591 gl_shaders
[shader_data
->num_gl_shaders
++].id
= ret
;
6596 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs
, const char *material
)
6600 case WINED3D_MCS_MATERIAL
:
6602 case WINED3D_MCS_COLOR1
:
6603 return "ffp_attrib_diffuse";
6604 case WINED3D_MCS_COLOR2
:
6605 return "ffp_attrib_specular";
6607 ERR("Invalid material color source %#x.\n", mcs
);
6612 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer
*buffer
,
6613 const struct wined3d_ffp_vs_settings
*settings
, BOOL legacy_lighting
)
6615 const char *diffuse
, *specular
, *emissive
, *ambient
;
6616 enum wined3d_light_type light_type
;
6619 if (!settings
->lighting
)
6621 shader_addline(buffer
, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6622 shader_addline(buffer
, "ffp_varying_specular = ffp_attrib_specular;\n");
6626 shader_addline(buffer
, "vec3 ambient = ffp_light_ambient;\n");
6627 shader_addline(buffer
, "vec3 diffuse = vec3(0.0);\n");
6628 shader_addline(buffer
, "vec4 specular = vec4(0.0);\n");
6629 shader_addline(buffer
, "vec3 dir, dst;\n");
6630 shader_addline(buffer
, "float att, t;\n");
6632 ambient
= shader_glsl_ffp_mcs(settings
->ambient_source
, "ffp_material.ambient");
6633 diffuse
= shader_glsl_ffp_mcs(settings
->diffuse_source
, "ffp_material.diffuse");
6634 specular
= shader_glsl_ffp_mcs(settings
->specular_source
, "ffp_material.specular");
6635 emissive
= shader_glsl_ffp_mcs(settings
->emissive_source
, "ffp_material.emissive");
6637 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
6639 light_type
= (settings
->light_type
>> WINED3D_FFP_LIGHT_TYPE_SHIFT(i
)) & WINED3D_FFP_LIGHT_TYPE_MASK
;
6642 case WINED3D_LIGHT_POINT
:
6643 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i
);
6644 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
6645 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
6646 shader_addline(buffer
, "dst.x = 1.0;\n");
6647 if (legacy_lighting
)
6649 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i
, i
);
6650 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
6654 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", i
);
6656 shader_addline(buffer
, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6657 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i
, i
, i
);
6658 if (!legacy_lighting
)
6659 shader_addline(buffer
, "att = 1.0 / att;\n");
6660 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", i
);
6661 if (!settings
->normal
)
6663 if (!legacy_lighting
)
6664 shader_addline(buffer
, "}\n");
6667 shader_addline(buffer
, "dir = normalize(dir);\n");
6668 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6669 " * ffp_light[%u].diffuse.xyz) * att;\n", i
);
6670 if (settings
->localviewer
)
6671 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6673 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6674 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6675 " * ffp_light[%u].specular) * att;\n", i
);
6676 if (!legacy_lighting
)
6677 shader_addline(buffer
, "}\n");
6680 case WINED3D_LIGHT_SPOT
:
6681 shader_addline(buffer
, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i
);
6682 shader_addline(buffer
, "dst.z = dot(dir, dir);\n");
6683 shader_addline(buffer
, "dst.y = sqrt(dst.z);\n");
6684 shader_addline(buffer
, "dst.x = 1.0;\n");
6685 if (legacy_lighting
)
6687 shader_addline(buffer
, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i
, i
);
6688 shader_addline(buffer
, "dst.z = dst.y * dst.y;\n");
6692 shader_addline(buffer
, "if (dst.y <= ffp_light[%u].range)\n{\n", i
);
6694 shader_addline(buffer
, "dir = normalize(dir);\n");
6695 shader_addline(buffer
, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i
);
6696 shader_addline(buffer
, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i
);
6697 shader_addline(buffer
, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i
);
6698 shader_addline(buffer
, "else att = pow((t - ffp_light[%u].cos_hphi)"
6699 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
6701 if (legacy_lighting
)
6702 shader_addline(buffer
, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6703 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6706 shader_addline(buffer
, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6707 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6709 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz * att;\n", i
);
6710 if (!settings
->normal
)
6712 if (!legacy_lighting
)
6713 shader_addline(buffer
, "}\n");
6716 shader_addline(buffer
, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6717 " * ffp_light[%u].diffuse.xyz) * att;\n", i
);
6718 if (settings
->localviewer
)
6719 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6721 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6722 shader_addline(buffer
, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6723 " * ffp_light[%u].specular) * att;\n", i
);
6724 if (!legacy_lighting
)
6725 shader_addline(buffer
, "}\n");
6728 case WINED3D_LIGHT_DIRECTIONAL
:
6729 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", i
);
6730 if (!settings
->normal
)
6732 shader_addline(buffer
, "dir = normalize(ffp_light[%u].direction.xyz);\n", i
);
6733 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6734 " * ffp_light[%u].diffuse.xyz;\n", i
);
6735 /* TODO: In the non-local viewer case the halfvector is constant
6736 * and could be precomputed and stored in a uniform. */
6737 if (settings
->localviewer
)
6738 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6740 shader_addline(buffer
, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6741 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6742 " * ffp_light[%u].specular;\n", i
);
6745 case WINED3D_LIGHT_PARALLELPOINT
:
6746 shader_addline(buffer
, "ambient += ffp_light[%u].ambient.xyz;\n", i
);
6747 if (!settings
->normal
)
6749 shader_addline(buffer
, "dir = normalize(ffp_light[%u].position.xyz);\n", i
);
6750 shader_addline(buffer
, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6751 " * ffp_light[%u].diffuse.xyz;\n", i
);
6752 shader_addline(buffer
, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6753 shader_addline(buffer
, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6754 " * ffp_light[%u].specular;\n", i
);
6759 FIXME("Unhandled light type %#x.\n", light_type
);
6764 shader_addline(buffer
, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6765 ambient
, diffuse
, emissive
);
6766 shader_addline(buffer
, "ffp_varying_diffuse.w = %s.w;\n", diffuse
);
6767 shader_addline(buffer
, "ffp_varying_specular = %s * specular;\n", specular
);
6770 /* Context activation is done by the caller. */
6771 static GLuint
shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
6772 const struct wined3d_ffp_vs_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
6774 static const struct attrib_info
6777 const char name
[24];
6781 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6782 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6783 /* TODO: Indexed vertex blending */
6784 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6785 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6786 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6787 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6788 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6790 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
6791 BOOL legacy_lighting
= priv
->legacy_lighting
;
6794 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6795 BOOL output_legacy_fogcoord
= legacy_context
;
6797 string_buffer_clear(buffer
);
6799 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, NULL
));
6801 if (shader_glsl_use_explicit_attrib_location(gl_info
))
6802 shader_addline(buffer
, "#extension GL_ARB_explicit_attrib_location : enable\n");
6804 for (i
= 0; i
< WINED3D_FFP_ATTRIBS_COUNT
; ++i
)
6806 const char *type
= i
< ARRAY_SIZE(attrib_info
) ? attrib_info
[i
].type
: "vec4";
6808 if (shader_glsl_use_explicit_attrib_location(gl_info
))
6809 shader_addline(buffer
, "layout(location = %u) ", i
);
6810 shader_addline(buffer
, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info
), type
, i
);
6812 shader_addline(buffer
, "\n");
6814 shader_addline(buffer
, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS
);
6815 shader_addline(buffer
, "uniform mat4 ffp_projection_matrix;\n");
6816 shader_addline(buffer
, "uniform mat3 ffp_normal_matrix;\n");
6817 shader_addline(buffer
, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES
);
6819 shader_addline(buffer
, "uniform struct\n{\n");
6820 shader_addline(buffer
, " vec4 emissive;\n");
6821 shader_addline(buffer
, " vec4 ambient;\n");
6822 shader_addline(buffer
, " vec4 diffuse;\n");
6823 shader_addline(buffer
, " vec4 specular;\n");
6824 shader_addline(buffer
, " float shininess;\n");
6825 shader_addline(buffer
, "} ffp_material;\n");
6827 shader_addline(buffer
, "uniform vec3 ffp_light_ambient;\n");
6828 shader_addline(buffer
, "uniform struct\n{\n");
6829 shader_addline(buffer
, " vec4 diffuse;\n");
6830 shader_addline(buffer
, " vec4 specular;\n");
6831 shader_addline(buffer
, " vec4 ambient;\n");
6832 shader_addline(buffer
, " vec4 position;\n");
6833 shader_addline(buffer
, " vec3 direction;\n");
6834 shader_addline(buffer
, " float range;\n");
6835 shader_addline(buffer
, " float falloff;\n");
6836 shader_addline(buffer
, " float c_att;\n");
6837 shader_addline(buffer
, " float l_att;\n");
6838 shader_addline(buffer
, " float q_att;\n");
6839 shader_addline(buffer
, " float cos_htheta;\n");
6840 shader_addline(buffer
, " float cos_hphi;\n");
6841 shader_addline(buffer
, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS
);
6843 if (settings
->point_size
)
6845 shader_addline(buffer
, "uniform struct\n{\n");
6846 shader_addline(buffer
, " float size;\n");
6847 shader_addline(buffer
, " float size_min;\n");
6848 shader_addline(buffer
, " float size_max;\n");
6849 shader_addline(buffer
, " float c_att;\n");
6850 shader_addline(buffer
, " float l_att;\n");
6851 shader_addline(buffer
, " float q_att;\n");
6852 shader_addline(buffer
, "} ffp_point;\n");
6857 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
6858 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
6859 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
6860 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
6864 if (settings
->clipping
)
6865 shader_addline(buffer
, "uniform vec4 clip_planes[%u];\n", gl_info
->limits
.user_clip_distances
);
6867 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
6868 declare_out_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
6869 declare_out_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
6870 declare_out_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
6873 shader_addline(buffer
, "\nvoid main()\n{\n");
6874 shader_addline(buffer
, "float m;\n");
6875 shader_addline(buffer
, "vec3 r;\n");
6877 for (i
= 0; i
< ARRAY_SIZE(attrib_info
); ++i
)
6879 if (attrib_info
[i
].name
[0])
6880 shader_addline(buffer
, "%s %s = vs_in%u%s;\n", attrib_info
[i
].type
, attrib_info
[i
].name
,
6881 i
, settings
->swizzle_map
& (1u << i
) ? ".zyxw" : "");
6883 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6885 unsigned int coord_idx
= settings
->texgen
[i
] & 0x0000ffff;
6886 if ((settings
->texgen
[i
] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6887 && settings
->texcoords
& (1u << i
))
6888 shader_addline(buffer
, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i
, coord_idx
+ WINED3D_FFP_TEXCOORD0
);
6891 shader_addline(buffer
, "ffp_attrib_blendweight[%u] = 1.0;\n", settings
->vertexblends
);
6893 if (settings
->transformed
)
6895 shader_addline(buffer
, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6896 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6897 shader_addline(buffer
, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6901 for (i
= 0; i
< settings
->vertexblends
; ++i
)
6902 shader_addline(buffer
, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings
->vertexblends
, i
);
6904 shader_addline(buffer
, "vec4 ec_pos = vec4(0.0);\n");
6905 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
6906 shader_addline(buffer
, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i
, i
);
6908 shader_addline(buffer
, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6909 if (settings
->clipping
)
6911 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
6912 shader_addline(buffer
, "gl_ClipVertex = ec_pos;\n");
6914 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
6915 shader_addline(buffer
, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i
, i
);
6917 shader_addline(buffer
, "ec_pos /= ec_pos.w;\n");
6920 shader_addline(buffer
, "vec3 normal = vec3(0.0);\n");
6921 if (settings
->normal
)
6923 if (!settings
->vertexblends
)
6925 shader_addline(buffer
, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6929 for (i
= 0; i
< settings
->vertexblends
+ 1; ++i
)
6930 shader_addline(buffer
, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i
, i
);
6933 if (settings
->normalize
)
6934 shader_addline(buffer
, "normal = normalize(normal);\n");
6937 shader_glsl_ffp_vertex_lighting(buffer
, settings
, legacy_lighting
);
6940 shader_addline(buffer
, "gl_FrontColor = ffp_varying_diffuse;\n");
6941 shader_addline(buffer
, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6945 shader_addline(buffer
, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
6946 shader_addline(buffer
, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
6949 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
6951 BOOL output_legacy_texcoord
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
6953 switch (settings
->texgen
[i
] & 0xffff0000)
6955 case WINED3DTSS_TCI_PASSTHRU
:
6956 if (settings
->texcoords
& (1u << i
))
6957 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6959 else if (gl_info
->limits
.glsl_varyings
>= wined3d_max_compat_varyings(gl_info
))
6960 shader_addline(buffer
, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i
);
6962 output_legacy_texcoord
= FALSE
;
6965 case WINED3DTSS_TCI_CAMERASPACENORMAL
:
6966 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i
, i
);
6969 case WINED3DTSS_TCI_CAMERASPACEPOSITION
:
6970 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i
, i
);
6973 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR
:
6974 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6975 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i
, i
);
6978 case WINED3DTSS_TCI_SPHEREMAP
:
6979 shader_addline(buffer
, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6980 shader_addline(buffer
, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6981 shader_addline(buffer
, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6982 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i
, i
);
6986 ERR("Unhandled texgen %#x.\n", settings
->texgen
[i
]);
6989 if (output_legacy_texcoord
)
6990 shader_addline(buffer
, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i
, i
);
6993 switch (settings
->fog_mode
)
6995 case WINED3D_FFP_VS_FOG_OFF
:
6996 output_legacy_fogcoord
= FALSE
;
6999 case WINED3D_FFP_VS_FOG_FOGCOORD
:
7000 shader_addline(buffer
, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7003 case WINED3D_FFP_VS_FOG_RANGE
:
7004 shader_addline(buffer
, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7007 case WINED3D_FFP_VS_FOG_DEPTH
:
7008 if (settings
->ortho_fog
)
7010 if (gl_info
->supported
[ARB_CLIP_CONTROL
])
7011 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z;\n");
7013 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7014 shader_addline(buffer
, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7016 else if (settings
->transformed
)
7018 shader_addline(buffer
, "ffp_varying_fogcoord = ec_pos.z;\n");
7022 shader_addline(buffer
, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7027 ERR("Unhandled fog mode %#x.\n", settings
->fog_mode
);
7030 if (output_legacy_fogcoord
)
7031 shader_addline(buffer
, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7033 if (settings
->point_size
)
7035 shader_addline(buffer
, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7036 " + ffp_point.l_att * length(ec_pos.xyz)"
7037 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7038 settings
->per_vertex_point_size
? "ffp_attrib_psize" : "ffp_point.size");
7039 shader_addline(buffer
, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7042 shader_addline(buffer
, "}\n");
7044 shader_obj
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
7045 shader_glsl_compile(gl_info
, shader_obj
, buffer
->buffer
);
7050 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer
*buffer
,
7051 DWORD argnum
, unsigned int stage
, DWORD arg
)
7055 if (arg
== ARG_UNUSED
)
7056 return "<unused arg>";
7058 switch (arg
& WINED3DTA_SELECTMASK
)
7060 case WINED3DTA_DIFFUSE
:
7061 ret
= "ffp_varying_diffuse";
7064 case WINED3DTA_CURRENT
:
7068 case WINED3DTA_TEXTURE
:
7071 case 0: ret
= "tex0"; break;
7072 case 1: ret
= "tex1"; break;
7073 case 2: ret
= "tex2"; break;
7074 case 3: ret
= "tex3"; break;
7075 case 4: ret
= "tex4"; break;
7076 case 5: ret
= "tex5"; break;
7077 case 6: ret
= "tex6"; break;
7078 case 7: ret
= "tex7"; break;
7080 ret
= "<invalid texture>";
7085 case WINED3DTA_TFACTOR
:
7089 case WINED3DTA_SPECULAR
:
7090 ret
= "ffp_varying_specular";
7093 case WINED3DTA_TEMP
:
7097 case WINED3DTA_CONSTANT
:
7100 case 0: ret
= "tss_const0"; break;
7101 case 1: ret
= "tss_const1"; break;
7102 case 2: ret
= "tss_const2"; break;
7103 case 3: ret
= "tss_const3"; break;
7104 case 4: ret
= "tss_const4"; break;
7105 case 5: ret
= "tss_const5"; break;
7106 case 6: ret
= "tss_const6"; break;
7107 case 7: ret
= "tss_const7"; break;
7109 ret
= "<invalid constant>";
7115 return "<unhandled arg>";
7118 if (arg
& WINED3DTA_COMPLEMENT
)
7120 shader_addline(buffer
, "arg%u = vec4(1.0) - %s;\n", argnum
, ret
);
7123 else if (argnum
== 1)
7125 else if (argnum
== 2)
7129 if (arg
& WINED3DTA_ALPHAREPLICATE
)
7131 shader_addline(buffer
, "arg%u = vec4(%s.w);\n", argnum
, ret
);
7134 else if (argnum
== 1)
7136 else if (argnum
== 2)
7143 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer
*buffer
, unsigned int stage
, BOOL color
,
7144 BOOL alpha
, DWORD dst
, DWORD op
, DWORD dw_arg0
, DWORD dw_arg1
, DWORD dw_arg2
)
7146 const char *dstmask
, *dstreg
, *arg0
, *arg1
, *arg2
;
7156 dstreg
= "temp_reg";
7160 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, dw_arg0
);
7161 arg1
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 1, stage
, dw_arg1
);
7162 arg2
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 2, stage
, dw_arg2
);
7166 case WINED3D_TOP_DISABLE
:
7169 case WINED3D_TOP_SELECT_ARG1
:
7170 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
);
7173 case WINED3D_TOP_SELECT_ARG2
:
7174 shader_addline(buffer
, "%s%s = %s%s;\n", dstreg
, dstmask
, arg2
, dstmask
);
7177 case WINED3D_TOP_MODULATE
:
7178 shader_addline(buffer
, "%s%s = %s%s * %s%s;\n", dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7181 case WINED3D_TOP_MODULATE_4X
:
7182 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7183 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7186 case WINED3D_TOP_MODULATE_2X
:
7187 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7188 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7191 case WINED3D_TOP_ADD
:
7192 shader_addline(buffer
, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7193 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7196 case WINED3D_TOP_ADD_SIGNED
:
7197 shader_addline(buffer
, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7198 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7201 case WINED3D_TOP_ADD_SIGNED_2X
:
7202 shader_addline(buffer
, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7203 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7206 case WINED3D_TOP_SUBTRACT
:
7207 shader_addline(buffer
, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7208 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
);
7211 case WINED3D_TOP_ADD_SMOOTH
:
7212 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7213 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
, dstmask
);
7216 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA
:
7217 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_DIFFUSE
);
7218 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7219 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
7222 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
7223 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
7224 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7225 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
7228 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
7229 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TFACTOR
);
7230 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7231 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
7234 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
7235 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_TEXTURE
);
7236 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7237 dstreg
, dstmask
, arg2
, dstmask
, arg0
, arg1
, dstmask
);
7240 case WINED3D_TOP_BLEND_CURRENT_ALPHA
:
7241 arg0
= shader_glsl_get_ffp_fragment_op_arg(buffer
, 0, stage
, WINED3DTA_CURRENT
);
7242 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7243 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
);
7246 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR
:
7247 shader_addline(buffer
, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
7248 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
7251 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA
:
7252 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
7253 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
7256 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR
:
7257 shader_addline(buffer
, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7258 dstreg
, dstmask
, arg2
, dstmask
, arg1
, arg1
, dstmask
);
7260 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA
:
7261 shader_addline(buffer
, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
7262 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg1
);
7265 case WINED3D_TOP_BUMPENVMAP
:
7266 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
7267 /* These are handled in the first pass, nothing to do. */
7270 case WINED3D_TOP_DOTPRODUCT3
:
7271 shader_addline(buffer
, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
7272 dstreg
, dstmask
, arg1
, arg2
, dstmask
);
7275 case WINED3D_TOP_MULTIPLY_ADD
:
7276 shader_addline(buffer
, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
7277 dstreg
, dstmask
, arg1
, dstmask
, arg2
, dstmask
, arg0
, dstmask
);
7280 case WINED3D_TOP_LERP
:
7281 /* MSDN isn't quite right here. */
7282 shader_addline(buffer
, "%s%s = mix(%s%s, %s%s, %s%s);\n",
7283 dstreg
, dstmask
, arg2
, dstmask
, arg1
, dstmask
, arg0
, dstmask
);
7287 FIXME("Unhandled operation %#x.\n", op
);
7292 /* Context activation is done by the caller. */
7293 static GLuint
shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
7294 const struct ffp_frag_settings
*settings
, const struct wined3d_gl_info
*gl_info
)
7296 struct wined3d_string_buffer
*tex_reg_name
= string_buffer_get(&priv
->string_buffers
);
7297 enum wined3d_cmp_func alpha_test_func
= settings
->alpha_test_func
+ 1;
7298 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
7299 struct wined3d_string_buffer
*buffer
= &priv
->shader_buffer
;
7300 BYTE lum_map
= 0, bump_map
= 0, tex_map
= 0, tss_const_map
= 0;
7301 BOOL tempreg_used
= FALSE
, tfactor_used
= FALSE
;
7302 UINT lowest_disabled_stage
;
7304 DWORD arg0
, arg1
, arg2
;
7307 string_buffer_clear(buffer
);
7309 /* Find out which textures are read */
7310 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
7312 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
7315 arg0
= settings
->op
[stage
].carg0
& WINED3DTA_SELECTMASK
;
7316 arg1
= settings
->op
[stage
].carg1
& WINED3DTA_SELECTMASK
;
7317 arg2
= settings
->op
[stage
].carg2
& WINED3DTA_SELECTMASK
;
7319 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
7320 || (stage
== 0 && settings
->color_key_enabled
))
7321 tex_map
|= 1u << stage
;
7322 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
7323 tfactor_used
= TRUE
;
7324 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
7325 tempreg_used
= TRUE
;
7326 if (settings
->op
[stage
].dst
== tempreg
)
7327 tempreg_used
= TRUE
;
7328 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
7329 tss_const_map
|= 1u << stage
;
7331 switch (settings
->op
[stage
].cop
)
7333 case WINED3D_TOP_BUMPENVMAP_LUMINANCE
:
7334 lum_map
|= 1u << stage
;
7336 case WINED3D_TOP_BUMPENVMAP
:
7337 bump_map
|= 1u << stage
;
7339 case WINED3D_TOP_BLEND_TEXTURE_ALPHA
:
7340 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM
:
7341 tex_map
|= 1u << stage
;
7344 case WINED3D_TOP_BLEND_FACTOR_ALPHA
:
7345 tfactor_used
= TRUE
;
7352 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
7355 arg0
= settings
->op
[stage
].aarg0
& WINED3DTA_SELECTMASK
;
7356 arg1
= settings
->op
[stage
].aarg1
& WINED3DTA_SELECTMASK
;
7357 arg2
= settings
->op
[stage
].aarg2
& WINED3DTA_SELECTMASK
;
7359 if (arg0
== WINED3DTA_TEXTURE
|| arg1
== WINED3DTA_TEXTURE
|| arg2
== WINED3DTA_TEXTURE
)
7360 tex_map
|= 1u << stage
;
7361 if (arg0
== WINED3DTA_TFACTOR
|| arg1
== WINED3DTA_TFACTOR
|| arg2
== WINED3DTA_TFACTOR
)
7362 tfactor_used
= TRUE
;
7363 if (arg0
== WINED3DTA_TEMP
|| arg1
== WINED3DTA_TEMP
|| arg2
== WINED3DTA_TEMP
)
7364 tempreg_used
= TRUE
;
7365 if (arg0
== WINED3DTA_CONSTANT
|| arg1
== WINED3DTA_CONSTANT
|| arg2
== WINED3DTA_CONSTANT
)
7366 tss_const_map
|= 1u << stage
;
7368 lowest_disabled_stage
= stage
;
7370 shader_addline(buffer
, "%s\n", shader_glsl_get_version_declaration(gl_info
, NULL
));
7372 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
7373 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
7375 if (!needs_legacy_glsl_syntax(gl_info
))
7376 shader_addline(buffer
, "out vec4 ps_out[1];\n");
7378 shader_addline(buffer
, "vec4 tmp0, tmp1;\n");
7379 shader_addline(buffer
, "vec4 ret;\n");
7380 if (tempreg_used
|| settings
->sRGB_write
)
7381 shader_addline(buffer
, "vec4 temp_reg = vec4(0.0);\n");
7382 shader_addline(buffer
, "vec4 arg0, arg1, arg2;\n");
7384 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
7386 if (tss_const_map
& (1u << stage
))
7387 shader_addline(buffer
, "uniform vec4 tss_const%u;\n", stage
);
7389 if (!(tex_map
& (1u << stage
)))
7392 switch (settings
->op
[stage
].tex_type
)
7394 case WINED3D_GL_RES_TYPE_TEX_1D
:
7395 shader_addline(buffer
, "uniform sampler1D ps_sampler%u;\n", stage
);
7397 case WINED3D_GL_RES_TYPE_TEX_2D
:
7398 shader_addline(buffer
, "uniform sampler2D ps_sampler%u;\n", stage
);
7400 case WINED3D_GL_RES_TYPE_TEX_3D
:
7401 shader_addline(buffer
, "uniform sampler3D ps_sampler%u;\n", stage
);
7403 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
7404 shader_addline(buffer
, "uniform samplerCube ps_sampler%u;\n", stage
);
7406 case WINED3D_GL_RES_TYPE_TEX_RECT
:
7407 shader_addline(buffer
, "uniform sampler2DRect ps_sampler%u;\n", stage
);
7410 FIXME("Unhandled sampler type %#x.\n", settings
->op
[stage
].tex_type
);
7414 shader_addline(buffer
, "vec4 tex%u;\n", stage
);
7416 if (!(bump_map
& (1u << stage
)))
7418 shader_addline(buffer
, "uniform mat2 bumpenv_mat%u;\n", stage
);
7420 if (!(lum_map
& (1u << stage
)))
7422 shader_addline(buffer
, "uniform float bumpenv_lum_scale%u;\n", stage
);
7423 shader_addline(buffer
, "uniform float bumpenv_lum_offset%u;\n", stage
);
7426 shader_addline(buffer
, "uniform vec4 tex_factor;\n");
7427 if (settings
->color_key_enabled
)
7428 shader_addline(buffer
, "uniform vec4 color_key[2];\n");
7429 shader_addline(buffer
, "uniform vec4 specular_enable;\n");
7431 if (settings
->sRGB_write
)
7433 shader_addline(buffer
, "const vec4 srgb_const0 = ");
7434 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const0
);
7435 shader_addline(buffer
, ";\n");
7436 shader_addline(buffer
, "const vec4 srgb_const1 = ");
7437 shader_glsl_append_imm_vec4(buffer
, wined3d_srgb_const1
);
7438 shader_addline(buffer
, ";\n");
7441 shader_addline(buffer
, "uniform struct\n{\n");
7442 shader_addline(buffer
, " vec4 color;\n");
7443 shader_addline(buffer
, " float density;\n");
7444 shader_addline(buffer
, " float end;\n");
7445 shader_addline(buffer
, " float scale;\n");
7446 shader_addline(buffer
, "} ffp_fog;\n");
7448 if (alpha_test_func
!= WINED3D_CMP_ALWAYS
)
7449 shader_addline(buffer
, "uniform float alpha_test_ref;\n");
7453 shader_addline(buffer
, "vec4 ffp_varying_diffuse;\n");
7454 shader_addline(buffer
, "vec4 ffp_varying_specular;\n");
7455 shader_addline(buffer
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7456 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
7457 shader_addline(buffer
, "float ffp_varying_fogcoord;\n");
7461 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_diffuse;\n");
7462 declare_in_varying(gl_info
, buffer
, settings
->flatshading
, "vec4 ffp_varying_specular;\n");
7463 declare_in_varying(gl_info
, buffer
, FALSE
, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES
);
7464 shader_addline(buffer
, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES
);
7465 declare_in_varying(gl_info
, buffer
, FALSE
, "float ffp_varying_fogcoord;\n");
7468 shader_addline(buffer
, "void main()\n{\n");
7472 shader_addline(buffer
, "ffp_varying_diffuse = gl_Color;\n");
7473 shader_addline(buffer
, "ffp_varying_specular = gl_SecondaryColor;\n");
7476 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
7478 if (tex_map
& (1u << stage
))
7480 if (settings
->pointsprite
)
7481 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage
);
7482 else if (settings
->texcoords_initialized
& (1u << stage
))
7483 shader_addline(buffer
, "ffp_texcoord[%u] = %s[%u];\n",
7484 stage
, legacy_context
? "gl_TexCoord" : "ffp_varying_texcoord", stage
);
7486 shader_addline(buffer
, "ffp_texcoord[%u] = vec4(0.0);\n", stage
);
7490 if (legacy_context
&& settings
->fog
!= WINED3D_FFP_PS_FOG_OFF
)
7491 shader_addline(buffer
, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7493 if (lowest_disabled_stage
< 7 && settings
->emul_clipplanes
)
7494 shader_addline(buffer
, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
7496 /* Generate texture sampling instructions */
7497 for (stage
= 0; stage
< MAX_TEXTURES
&& settings
->op
[stage
].cop
!= WINED3D_TOP_DISABLE
; ++stage
)
7499 const char *texture_function
, *coord_mask
;
7502 if (!(tex_map
& (1u << stage
)))
7505 if (settings
->op
[stage
].projected
== proj_none
)
7509 else if (settings
->op
[stage
].projected
== proj_count4
7510 || settings
->op
[stage
].projected
== proj_count3
)
7516 FIXME("Unexpected projection mode %d\n", settings
->op
[stage
].projected
);
7520 if (settings
->op
[stage
].tex_type
== WINED3D_GL_RES_TYPE_TEX_CUBE
)
7523 switch (settings
->op
[stage
].tex_type
)
7525 case WINED3D_GL_RES_TYPE_TEX_1D
:
7528 texture_function
= "texture1DProj";
7533 texture_function
= "texture1D";
7537 case WINED3D_GL_RES_TYPE_TEX_2D
:
7540 texture_function
= "texture2DProj";
7545 texture_function
= "texture2D";
7549 case WINED3D_GL_RES_TYPE_TEX_3D
:
7552 texture_function
= "texture3DProj";
7553 coord_mask
= "xyzw";
7557 texture_function
= "texture3D";
7561 case WINED3D_GL_RES_TYPE_TEX_CUBE
:
7562 texture_function
= "textureCube";
7565 case WINED3D_GL_RES_TYPE_TEX_RECT
:
7568 texture_function
= "texture2DRectProj";
7573 texture_function
= "texture2DRect";
7578 FIXME("Unhandled texture type %#x.\n", settings
->op
[stage
].tex_type
);
7579 texture_function
= "";
7580 coord_mask
= "xyzw";
7583 if (!needs_legacy_glsl_syntax(gl_info
))
7584 texture_function
= proj
? "textureProj" : "texture";
7587 && (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP
7588 || settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
))
7590 shader_addline(buffer
, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage
- 1, stage
- 1);
7592 /* With projective textures, texbem only divides the static
7593 * texture coord, not the displacement, so multiply the
7594 * displacement with the dividing parameter before passing it to
7596 if (settings
->op
[stage
].projected
!= proj_none
)
7598 if (settings
->op
[stage
].projected
== proj_count4
)
7600 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7602 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].ww;\n", stage
);
7606 shader_addline(buffer
, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7608 shader_addline(buffer
, "ret.zw = ffp_texcoord[%u].zz;\n", stage
);
7613 shader_addline(buffer
, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage
);
7616 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7617 stage
, texture_function
, stage
, coord_mask
);
7619 if (settings
->op
[stage
- 1].cop
== WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
7620 shader_addline(buffer
, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7621 stage
, stage
- 1, stage
- 1, stage
- 1);
7623 else if (settings
->op
[stage
].projected
== proj_count3
)
7625 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7626 stage
, texture_function
, stage
, stage
);
7630 shader_addline(buffer
, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7631 stage
, texture_function
, stage
, stage
, coord_mask
);
7634 string_buffer_sprintf(tex_reg_name
, "tex%u", stage
);
7635 shader_glsl_color_correction_ext(buffer
, tex_reg_name
->buffer
, WINED3DSP_WRITEMASK_ALL
,
7636 settings
->op
[stage
].color_fixup
);
7639 if (settings
->color_key_enabled
)
7641 shader_addline(buffer
, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7642 shader_addline(buffer
, " discard;\n");
7645 shader_addline(buffer
, "ret = ffp_varying_diffuse;\n");
7647 /* Generate the main shader */
7648 for (stage
= 0; stage
< MAX_TEXTURES
; ++stage
)
7652 if (settings
->op
[stage
].cop
== WINED3D_TOP_DISABLE
)
7655 if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
7656 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
7657 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
;
7658 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG1
7659 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
7660 op_equal
= settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg2
;
7661 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
7662 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG1
)
7663 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg1
;
7664 else if (settings
->op
[stage
].cop
== WINED3D_TOP_SELECT_ARG2
7665 && settings
->op
[stage
].aop
== WINED3D_TOP_SELECT_ARG2
)
7666 op_equal
= settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
7668 op_equal
= settings
->op
[stage
].aop
== settings
->op
[stage
].cop
7669 && settings
->op
[stage
].carg0
== settings
->op
[stage
].aarg0
7670 && settings
->op
[stage
].carg1
== settings
->op
[stage
].aarg1
7671 && settings
->op
[stage
].carg2
== settings
->op
[stage
].aarg2
;
7673 if (settings
->op
[stage
].aop
== WINED3D_TOP_DISABLE
)
7675 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
7676 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
7677 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
7681 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, TRUE
, settings
->op
[stage
].dst
,
7682 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
7683 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
7685 else if (settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP
7686 && settings
->op
[stage
].cop
!= WINED3D_TOP_BUMPENVMAP_LUMINANCE
)
7688 shader_glsl_ffp_fragment_op(buffer
, stage
, TRUE
, FALSE
, settings
->op
[stage
].dst
,
7689 settings
->op
[stage
].cop
, settings
->op
[stage
].carg0
,
7690 settings
->op
[stage
].carg1
, settings
->op
[stage
].carg2
);
7691 shader_glsl_ffp_fragment_op(buffer
, stage
, FALSE
, TRUE
, settings
->op
[stage
].dst
,
7692 settings
->op
[stage
].aop
, settings
->op
[stage
].aarg0
,
7693 settings
->op
[stage
].aarg1
, settings
->op
[stage
].aarg2
);
7697 shader_addline(buffer
, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
7698 get_fragment_output(gl_info
));
7700 if (settings
->sRGB_write
)
7701 shader_glsl_generate_srgb_write_correction(buffer
, gl_info
);
7703 shader_glsl_generate_fog_code(buffer
, gl_info
, settings
->fog
);
7705 shader_glsl_generate_alpha_test(buffer
, gl_info
, alpha_test_func
);
7707 shader_addline(buffer
, "}\n");
7709 shader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
7710 shader_glsl_compile(gl_info
, shader_id
, buffer
->buffer
);
7712 string_buffer_release(&priv
->string_buffers
, tex_reg_name
);
7716 static struct glsl_ffp_vertex_shader
*shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv
*priv
,
7717 const struct wined3d_gl_info
*gl_info
, const struct wined3d_ffp_vs_settings
*settings
)
7719 struct glsl_ffp_vertex_shader
*shader
;
7720 const struct wine_rb_entry
*entry
;
7722 if ((entry
= wine_rb_get(&priv
->ffp_vertex_shaders
, settings
)))
7723 return WINE_RB_ENTRY_VALUE(entry
, struct glsl_ffp_vertex_shader
, desc
.entry
);
7725 if (!(shader
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader
))))
7728 shader
->desc
.settings
= *settings
;
7729 shader
->id
= shader_glsl_generate_ffp_vertex_shader(priv
, settings
, gl_info
);
7730 list_init(&shader
->linked_programs
);
7731 if (wine_rb_put(&priv
->ffp_vertex_shaders
, &shader
->desc
.settings
, &shader
->desc
.entry
) == -1)
7732 ERR("Failed to insert ffp vertex shader.\n");
7737 static struct glsl_ffp_fragment_shader
*shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv
*priv
,
7738 const struct wined3d_gl_info
*gl_info
, const struct ffp_frag_settings
*args
)
7740 struct glsl_ffp_fragment_shader
*glsl_desc
;
7741 const struct ffp_frag_desc
*desc
;
7743 if ((desc
= find_ffp_frag_shader(&priv
->ffp_fragment_shaders
, args
)))
7744 return CONTAINING_RECORD(desc
, struct glsl_ffp_fragment_shader
, entry
);
7746 if (!(glsl_desc
= HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc
))))
7749 glsl_desc
->entry
.settings
= *args
;
7750 glsl_desc
->id
= shader_glsl_generate_ffp_fragment_shader(priv
, args
, gl_info
);
7751 list_init(&glsl_desc
->linked_programs
);
7752 add_ffp_frag_shader(&priv
->ffp_fragment_shaders
, &glsl_desc
->entry
);
7758 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
7759 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_vs_program
*vs
, unsigned int vs_c_count
)
7762 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
7764 for (i
= 0; i
< vs_c_count
; ++i
)
7766 string_buffer_sprintf(name
, "vs_c[%u]", i
);
7767 vs
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7769 memset(&vs
->uniform_f_locations
[vs_c_count
], 0xff, (WINED3D_MAX_VS_CONSTS_F
- vs_c_count
) * sizeof(GLuint
));
7771 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
7773 string_buffer_sprintf(name
, "vs_i[%u]", i
);
7774 vs
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7777 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
7779 string_buffer_sprintf(name
, "vs_b[%u]", i
);
7780 vs
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7783 vs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "pos_fixup"));
7785 for (i
= 0; i
< MAX_VERTEX_BLENDS
; ++i
)
7787 string_buffer_sprintf(name
, "ffp_modelview_matrix[%u]", i
);
7788 vs
->modelview_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7790 vs
->projection_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_projection_matrix"));
7791 vs
->normal_matrix_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_normal_matrix"));
7792 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7794 string_buffer_sprintf(name
, "ffp_texture_matrix[%u]", i
);
7795 vs
->texture_matrix_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7797 vs
->material_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.ambient"));
7798 vs
->material_diffuse_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.diffuse"));
7799 vs
->material_specular_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.specular"));
7800 vs
->material_emissive_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.emissive"));
7801 vs
->material_shininess_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_material.shininess"));
7802 vs
->light_ambient_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_light_ambient"));
7803 for (i
= 0; i
< MAX_ACTIVE_LIGHTS
; ++i
)
7805 string_buffer_sprintf(name
, "ffp_light[%u].diffuse", i
);
7806 vs
->light_location
[i
].diffuse
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7807 string_buffer_sprintf(name
, "ffp_light[%u].specular", i
);
7808 vs
->light_location
[i
].specular
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7809 string_buffer_sprintf(name
, "ffp_light[%u].ambient", i
);
7810 vs
->light_location
[i
].ambient
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7811 string_buffer_sprintf(name
, "ffp_light[%u].position", i
);
7812 vs
->light_location
[i
].position
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7813 string_buffer_sprintf(name
, "ffp_light[%u].direction", i
);
7814 vs
->light_location
[i
].direction
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7815 string_buffer_sprintf(name
, "ffp_light[%u].range", i
);
7816 vs
->light_location
[i
].range
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7817 string_buffer_sprintf(name
, "ffp_light[%u].falloff", i
);
7818 vs
->light_location
[i
].falloff
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7819 string_buffer_sprintf(name
, "ffp_light[%u].c_att", i
);
7820 vs
->light_location
[i
].c_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7821 string_buffer_sprintf(name
, "ffp_light[%u].l_att", i
);
7822 vs
->light_location
[i
].l_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7823 string_buffer_sprintf(name
, "ffp_light[%u].q_att", i
);
7824 vs
->light_location
[i
].q_att
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7825 string_buffer_sprintf(name
, "ffp_light[%u].cos_htheta", i
);
7826 vs
->light_location
[i
].cos_htheta
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7827 string_buffer_sprintf(name
, "ffp_light[%u].cos_hphi", i
);
7828 vs
->light_location
[i
].cos_hphi
= GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7830 vs
->pointsize_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size"));
7831 vs
->pointsize_min_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_min"));
7832 vs
->pointsize_max_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.size_max"));
7833 vs
->pointsize_c_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.c_att"));
7834 vs
->pointsize_l_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.l_att"));
7835 vs
->pointsize_q_att_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_point.q_att"));
7836 vs
->clip_planes_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "clip_planes"));
7838 string_buffer_release(&priv
->string_buffers
, name
);
7841 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info
*gl_info
,
7842 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_gs_program
*gs
)
7844 gs
->pos_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "pos_fixup"));
7847 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info
*gl_info
,
7848 struct shader_glsl_priv
*priv
, GLuint program_id
, struct glsl_ps_program
*ps
, unsigned int ps_c_count
)
7851 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
7853 for (i
= 0; i
< ps_c_count
; ++i
)
7855 string_buffer_sprintf(name
, "ps_c[%u]", i
);
7856 ps
->uniform_f_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7858 memset(&ps
->uniform_f_locations
[ps_c_count
], 0xff, (WINED3D_MAX_PS_CONSTS_F
- ps_c_count
) * sizeof(GLuint
));
7860 for (i
= 0; i
< WINED3D_MAX_CONSTS_I
; ++i
)
7862 string_buffer_sprintf(name
, "ps_i[%u]", i
);
7863 ps
->uniform_i_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7866 for (i
= 0; i
< WINED3D_MAX_CONSTS_B
; ++i
)
7868 string_buffer_sprintf(name
, "ps_b[%u]", i
);
7869 ps
->uniform_b_locations
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7872 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
7874 string_buffer_sprintf(name
, "bumpenv_mat%u", i
);
7875 ps
->bumpenv_mat_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7876 string_buffer_sprintf(name
, "bumpenv_lum_scale%u", i
);
7877 ps
->bumpenv_lum_scale_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7878 string_buffer_sprintf(name
, "bumpenv_lum_offset%u", i
);
7879 ps
->bumpenv_lum_offset_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7880 string_buffer_sprintf(name
, "tss_const%u", i
);
7881 ps
->tss_constant_location
[i
] = GL_EXTCALL(glGetUniformLocation(program_id
, name
->buffer
));
7884 ps
->tex_factor_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "tex_factor"));
7885 ps
->specular_enable_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "specular_enable"));
7887 ps
->fog_color_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.color"));
7888 ps
->fog_density_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.density"));
7889 ps
->fog_end_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.end"));
7890 ps
->fog_scale_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ffp_fog.scale"));
7892 ps
->alpha_test_ref_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "alpha_test_ref"));
7894 ps
->np2_fixup_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ps_samplerNP2Fixup"));
7895 ps
->ycorrection_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "ycorrection"));
7896 ps
->color_key_location
= GL_EXTCALL(glGetUniformLocation(program_id
, "color_key"));
7898 string_buffer_release(&priv
->string_buffers
, name
);
7901 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info
*gl_info
,
7902 struct shader_glsl_priv
*priv
, GLuint program_id
,
7903 const struct wined3d_shader_reg_maps
*reg_maps
)
7905 struct wined3d_string_buffer
*name
= string_buffer_get(&priv
->string_buffers
);
7906 const char *prefix
= shader_glsl_get_prefix(reg_maps
->shader_version
.type
);
7907 unsigned int i
, base
, count
;
7910 wined3d_gl_limits_get_uniform_block_range(&gl_info
->limits
, reg_maps
->shader_version
.type
, &base
, &count
);
7911 for (i
= 0; i
< count
; ++i
)
7913 if (!reg_maps
->cb_sizes
[i
])
7916 string_buffer_sprintf(name
, "block_%s_cb%u", prefix
, i
);
7917 block_idx
= GL_EXTCALL(glGetUniformBlockIndex(program_id
, name
->buffer
));
7918 GL_EXTCALL(glUniformBlockBinding(program_id
, block_idx
, base
+ i
));
7920 checkGLcall("glUniformBlockBinding");
7921 string_buffer_release(&priv
->string_buffers
, name
);
7924 /* Context activation is done by the caller. */
7925 static void set_glsl_shader_program(const struct wined3d_context
*context
, const struct wined3d_state
*state
,
7926 struct shader_glsl_priv
*priv
, struct glsl_context_data
*ctx_data
)
7928 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
7929 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
7930 const struct ps_np2fixup_info
*np2fixup_info
= NULL
;
7931 struct glsl_shader_prog_link
*entry
= NULL
;
7932 struct wined3d_shader
*vshader
= NULL
;
7933 struct wined3d_shader
*gshader
= NULL
;
7934 struct wined3d_shader
*pshader
= NULL
;
7936 GLuint reorder_shader_id
= 0;
7941 struct list
*ps_list
, *vs_list
;
7943 struct wined3d_string_buffer
*tmp_name
;
7945 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_VERTEX
)) && ctx_data
->glsl_program
)
7947 vs_id
= ctx_data
->glsl_program
->vs
.id
;
7948 vs_list
= &ctx_data
->glsl_program
->vs
.shader_entry
;
7952 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
7953 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
7955 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_GEOMETRY
))
7956 && ctx_data
->glsl_program
->gs
.id
)
7958 gs_id
= ctx_data
->glsl_program
->gs
.id
;
7962 struct gs_compile_args args
;
7964 find_gs_compile_args(state
, gshader
, &args
);
7965 gs_id
= find_glsl_geometry_shader(context
, priv
, gshader
, &args
);
7969 else if (use_vs(state
))
7971 struct vs_compile_args vs_compile_args
;
7973 vshader
= state
->shader
[WINED3D_SHADER_TYPE_VERTEX
];
7974 gshader
= state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
];
7976 find_vs_compile_args(state
, vshader
, context
->stream_info
.swizzle_map
, &vs_compile_args
, d3d_info
);
7977 vs_id
= find_glsl_vshader(context
, priv
, vshader
, &vs_compile_args
);
7978 vs_list
= &vshader
->linked_programs
;
7980 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_GEOMETRY
))
7981 && ctx_data
->glsl_program
->gs
.id
)
7983 gs_id
= ctx_data
->glsl_program
->gs
.id
;
7987 struct gs_compile_args gs_compile_args
;
7989 find_gs_compile_args(state
, gshader
, &gs_compile_args
);
7990 gs_id
= find_glsl_geometry_shader(context
, priv
, gshader
, &gs_compile_args
);
7993 else if (priv
->vertex_pipe
== &glsl_vertex_pipe
)
7995 struct glsl_ffp_vertex_shader
*ffp_shader
;
7996 struct wined3d_ffp_vs_settings settings
;
7998 wined3d_ffp_get_vs_settings(context
, state
, &settings
);
7999 ffp_shader
= shader_glsl_find_ffp_vertex_shader(priv
, gl_info
, &settings
);
8000 vs_id
= ffp_shader
->id
;
8001 vs_list
= &ffp_shader
->linked_programs
;
8004 if (!(context
->shader_update_mask
& (1u << WINED3D_SHADER_TYPE_PIXEL
)) && ctx_data
->glsl_program
)
8006 ps_id
= ctx_data
->glsl_program
->ps
.id
;
8007 ps_list
= &ctx_data
->glsl_program
->ps
.shader_entry
;
8010 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
8012 else if (use_ps(state
))
8014 struct ps_compile_args ps_compile_args
;
8015 pshader
= state
->shader
[WINED3D_SHADER_TYPE_PIXEL
];
8016 find_ps_compile_args(state
, pshader
, context
->stream_info
.position_transformed
, &ps_compile_args
, context
);
8017 ps_id
= find_glsl_pshader(context
, &priv
->shader_buffer
, &priv
->string_buffers
,
8018 pshader
, &ps_compile_args
, &np2fixup_info
);
8019 ps_list
= &pshader
->linked_programs
;
8021 else if (priv
->fragment_pipe
== &glsl_fragment_pipe
)
8023 struct glsl_ffp_fragment_shader
*ffp_shader
;
8024 struct ffp_frag_settings settings
;
8026 gen_ffp_frag_op(context
, state
, &settings
, FALSE
);
8027 ffp_shader
= shader_glsl_find_ffp_fragment_shader(priv
, gl_info
, &settings
);
8028 ps_id
= ffp_shader
->id
;
8029 ps_list
= &ffp_shader
->linked_programs
;
8032 if ((!vs_id
&& !gs_id
&& !ps_id
) || (entry
= get_glsl_program_entry(priv
, vs_id
, gs_id
, ps_id
)))
8034 ctx_data
->glsl_program
= entry
;
8038 /* If we get to this point, then no matching program exists, so we create one */
8039 program_id
= GL_EXTCALL(glCreateProgram());
8040 TRACE("Created new GLSL shader program %u.\n", program_id
);
8042 /* Create the entry */
8043 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
8044 entry
->id
= program_id
;
8045 entry
->vs
.id
= vs_id
;
8046 entry
->gs
.id
= gs_id
;
8047 entry
->ps
.id
= ps_id
;
8048 entry
->constant_version
= 0;
8049 entry
->ps
.np2_fixup_info
= np2fixup_info
;
8050 /* Add the hash table entry */
8051 add_glsl_program_entry(priv
, entry
);
8053 /* Set the current program */
8054 ctx_data
->glsl_program
= entry
;
8056 /* Attach GLSL vshader */
8059 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id
, program_id
);
8060 GL_EXTCALL(glAttachShader(program_id
, vs_id
));
8061 checkGLcall("glAttachShader");
8063 list_add_head(vs_list
, &entry
->vs
.shader_entry
);
8068 attribs_map
= vshader
->reg_maps
.input_registers
;
8069 if (vshader
->reg_maps
.shader_version
.major
< 4)
8071 reorder_shader_id
= shader_glsl_generate_vs3_rasterizer_input_setup(priv
, vshader
, pshader
,
8072 state
->gl_primitive_type
== GL_POINTS
&& vshader
->reg_maps
.point_size
,
8073 d3d_info
->emulated_flatshading
8074 && state
->render_states
[WINED3D_RS_SHADEMODE
] == WINED3D_SHADE_FLAT
, gl_info
);
8075 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id
, program_id
);
8076 GL_EXTCALL(glAttachShader(program_id
, reorder_shader_id
));
8077 checkGLcall("glAttachShader");
8078 /* Flag the reorder function for deletion, it will be freed
8079 * automatically when the program is destroyed. */
8080 GL_EXTCALL(glDeleteShader(reorder_shader_id
));
8085 attribs_map
= (1u << WINED3D_FFP_ATTRIBS_COUNT
) - 1;
8088 if (!shader_glsl_use_explicit_attrib_location(gl_info
))
8090 /* Bind vertex attributes to a corresponding index number to match
8091 * the same index numbers as ARB_vertex_programs (makes loading
8092 * vertex attributes simpler). With this method, we can use the
8093 * exact same code to load the attributes later for both ARB and
8096 * We have to do this here because we need to know the Program ID
8097 * in order to make the bindings work, and it has to be done prior
8098 * to linking the GLSL program. */
8099 tmp_name
= string_buffer_get(&priv
->string_buffers
);
8100 for (i
= 0; attribs_map
; attribs_map
>>= 1, ++i
)
8102 if (!(attribs_map
& 1))
8105 string_buffer_sprintf(tmp_name
, "vs_in%u", i
);
8106 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
8107 if (vshader
&& vshader
->reg_maps
.shader_version
.major
>= 4)
8109 string_buffer_sprintf(tmp_name
, "vs_in_uint%u", i
);
8110 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
8111 string_buffer_sprintf(tmp_name
, "vs_in_int%u", i
);
8112 GL_EXTCALL(glBindAttribLocation(program_id
, i
, tmp_name
->buffer
));
8115 checkGLcall("glBindAttribLocation");
8116 string_buffer_release(&priv
->string_buffers
, tmp_name
);
8121 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id
, program_id
);
8122 GL_EXTCALL(glAttachShader(program_id
, gs_id
));
8123 checkGLcall("glAttachShader");
8125 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
8127 TRACE("input type %s, output type %s, vertices out %u.\n",
8128 debug_d3dprimitivetype(gshader
->u
.gs
.input_type
),
8129 debug_d3dprimitivetype(gshader
->u
.gs
.output_type
),
8130 gshader
->u
.gs
.vertices_out
);
8131 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_INPUT_TYPE_ARB
,
8132 gl_primitive_type_from_d3d(gshader
->u
.gs
.input_type
)));
8133 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_OUTPUT_TYPE_ARB
,
8134 gl_primitive_type_from_d3d(gshader
->u
.gs
.output_type
)));
8135 GL_EXTCALL(glProgramParameteriARB(program_id
, GL_GEOMETRY_VERTICES_OUT_ARB
,
8136 gshader
->u
.gs
.vertices_out
));
8137 checkGLcall("glProgramParameteriARB");
8140 list_add_head(&gshader
->linked_programs
, &entry
->gs
.shader_entry
);
8143 /* Attach GLSL pshader */
8146 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id
, program_id
);
8147 GL_EXTCALL(glAttachShader(program_id
, ps_id
));
8148 checkGLcall("glAttachShader");
8150 list_add_head(ps_list
, &entry
->ps
.shader_entry
);
8153 /* Link the program */
8154 TRACE("Linking GLSL shader program %u.\n", program_id
);
8155 GL_EXTCALL(glLinkProgram(program_id
));
8156 shader_glsl_validate_link(gl_info
, program_id
);
8158 shader_glsl_init_vs_uniform_locations(gl_info
, priv
, program_id
, &entry
->vs
,
8159 vshader
? vshader
->limits
->constant_float
: 0);
8160 shader_glsl_init_gs_uniform_locations(gl_info
, priv
, program_id
, &entry
->gs
);
8161 shader_glsl_init_ps_uniform_locations(gl_info
, priv
, program_id
, &entry
->ps
,
8162 pshader
? pshader
->limits
->constant_float
: 0);
8163 checkGLcall("Find glsl program uniform locations");
8165 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
8167 if (pshader
&& pshader
->reg_maps
.shader_version
.major
>= 3
8168 && pshader
->u
.ps
.declared_in_count
> vec4_varyings(3, gl_info
))
8170 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id
);
8171 entry
->vs
.vertex_color_clamp
= GL_FALSE
;
8175 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
8180 /* With core profile we never change vertex_color_clamp from
8181 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
8182 * glClampColorARB(). */
8183 entry
->vs
.vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
8186 /* Set the shader to allow uniform loading on it */
8187 GL_EXTCALL(glUseProgram(program_id
));
8188 checkGLcall("glUseProgram");
8190 /* Texture unit mapping is set up to be the same each time the shader
8191 * program is used so we can hardcode the sampler uniform values. */
8192 shader_glsl_load_samplers(gl_info
, priv
, context
->tex_unit_map
, program_id
);
8194 entry
->constant_update_mask
= 0;
8197 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_F
;
8198 if (vshader
->reg_maps
.integer_constants
)
8199 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_I
;
8200 if (vshader
->reg_maps
.boolean_constants
)
8201 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_B
;
8202 if (entry
->vs
.pos_fixup_location
!= -1)
8203 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
8205 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &vshader
->reg_maps
);
8206 shader_glsl_load_icb(gl_info
, priv
, program_id
, &vshader
->reg_maps
);
8210 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
8211 | WINED3D_SHADER_CONST_FFP_PROJ
;
8213 for (i
= 1; i
< MAX_VERTEX_BLENDS
; ++i
)
8215 if (entry
->vs
.modelview_matrix_location
[i
] != -1)
8217 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
8222 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
8224 if (entry
->vs
.texture_matrix_location
[i
] != -1)
8226 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
8230 if (entry
->vs
.material_ambient_location
!= -1 || entry
->vs
.material_diffuse_location
!= -1
8231 || entry
->vs
.material_specular_location
!= -1
8232 || entry
->vs
.material_emissive_location
!= -1
8233 || entry
->vs
.material_shininess_location
!= -1)
8234 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
8235 if (entry
->vs
.light_ambient_location
!= -1)
8236 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
8238 if (entry
->vs
.clip_planes_location
!= -1)
8239 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
8240 if (entry
->vs
.pointsize_min_location
!= -1)
8241 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
8245 if (entry
->gs
.pos_fixup_location
!= -1)
8246 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
8247 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &gshader
->reg_maps
);
8248 shader_glsl_load_icb(gl_info
, priv
, program_id
, &gshader
->reg_maps
);
8255 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_F
;
8256 if (pshader
->reg_maps
.integer_constants
)
8257 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_I
;
8258 if (pshader
->reg_maps
.boolean_constants
)
8259 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_B
;
8260 if (entry
->ps
.ycorrection_location
!= -1)
8261 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_Y_CORR
;
8263 shader_glsl_init_uniform_block_bindings(gl_info
, priv
, program_id
, &pshader
->reg_maps
);
8264 shader_glsl_load_icb(gl_info
, priv
, program_id
, &pshader
->reg_maps
);
8265 shader_glsl_load_images(gl_info
, priv
, program_id
, &pshader
->reg_maps
);
8269 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
8272 for (i
= 0; i
< MAX_TEXTURES
; ++i
)
8274 if (entry
->ps
.bumpenv_mat_location
[i
] != -1)
8276 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_BUMP_ENV
;
8281 if (entry
->ps
.fog_color_location
!= -1)
8282 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
8283 if (entry
->ps
.alpha_test_ref_location
!= -1)
8284 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_ALPHA_TEST
;
8285 if (entry
->ps
.np2_fixup_location
!= -1)
8286 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_NP2_FIXUP
;
8287 if (entry
->ps
.color_key_location
!= -1)
8288 entry
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
8292 /* Context activation is done by the caller. */
8293 static GLuint
create_glsl_blt_shader(const struct wined3d_gl_info
*gl_info
, enum wined3d_gl_resource_type tex_type
,
8297 GLuint vshader_id
, pshader_id
;
8298 const char *blt_pshader
;
8300 static const char blt_vshader
[] =
8304 " gl_Position = gl_Vertex;\n"
8305 " gl_FrontColor = vec4(1.0);\n"
8306 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
8309 static const char * const blt_pshaders_full
[WINED3D_GL_RES_TYPE_COUNT
] =
8311 /* WINED3D_GL_RES_TYPE_TEX_1D */
8313 /* WINED3D_GL_RES_TYPE_TEX_2D */
8315 "uniform sampler2D sampler;\n"
8318 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
8320 /* WINED3D_GL_RES_TYPE_TEX_3D */
8322 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
8324 "uniform samplerCube sampler;\n"
8327 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
8329 /* WINED3D_GL_RES_TYPE_TEX_RECT */
8331 "#extension GL_ARB_texture_rectangle : enable\n"
8332 "uniform sampler2DRect sampler;\n"
8335 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
8337 /* WINED3D_GL_RES_TYPE_BUFFER */
8339 /* WINED3D_GL_RES_TYPE_RB */
8343 static const char * const blt_pshaders_masked
[WINED3D_GL_RES_TYPE_COUNT
] =
8345 /* WINED3D_GL_RES_TYPE_TEX_1D */
8347 /* WINED3D_GL_RES_TYPE_TEX_2D */
8349 "uniform sampler2D sampler;\n"
8350 "uniform vec4 mask;\n"
8353 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
8354 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
8356 /* WINED3D_GL_RES_TYPE_TEX_3D */
8358 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
8360 "uniform samplerCube sampler;\n"
8361 "uniform vec4 mask;\n"
8364 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
8365 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
8367 /* WINED3D_GL_RES_TYPE_TEX_RECT */
8369 "#extension GL_ARB_texture_rectangle : enable\n"
8370 "uniform sampler2DRect sampler;\n"
8371 "uniform vec4 mask;\n"
8374 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
8375 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
8377 /* WINED3D_GL_RES_TYPE_BUFFER */
8379 /* WINED3D_GL_RES_TYPE_RB */
8383 blt_pshader
= masked
? blt_pshaders_masked
[tex_type
] : blt_pshaders_full
[tex_type
];
8386 FIXME("tex_type %#x not supported\n", tex_type
);
8390 vshader_id
= GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER
));
8391 shader_glsl_compile(gl_info
, vshader_id
, blt_vshader
);
8393 pshader_id
= GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER
));
8394 shader_glsl_compile(gl_info
, pshader_id
, blt_pshader
);
8396 program_id
= GL_EXTCALL(glCreateProgram());
8397 GL_EXTCALL(glAttachShader(program_id
, vshader_id
));
8398 GL_EXTCALL(glAttachShader(program_id
, pshader_id
));
8399 GL_EXTCALL(glLinkProgram(program_id
));
8401 shader_glsl_validate_link(gl_info
, program_id
);
8403 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
8406 GL_EXTCALL(glDeleteShader(vshader_id
));
8407 GL_EXTCALL(glDeleteShader(pshader_id
));
8411 /* Context activation is done by the caller. */
8412 static void shader_glsl_select(void *shader_priv
, struct wined3d_context
*context
,
8413 const struct wined3d_state
*state
)
8415 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
8416 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8417 struct shader_glsl_priv
*priv
= shader_priv
;
8418 GLuint program_id
= 0, prev_id
= 0;
8419 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
8421 priv
->vertex_pipe
->vp_enable(gl_info
, !use_vs(state
));
8422 priv
->fragment_pipe
->enable_extension(gl_info
, !use_ps(state
));
8424 if (ctx_data
->glsl_program
)
8426 prev_id
= ctx_data
->glsl_program
->id
;
8427 old_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
8432 old_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
8435 set_glsl_shader_program(context
, state
, priv
, ctx_data
);
8437 if (ctx_data
->glsl_program
)
8439 program_id
= ctx_data
->glsl_program
->id
;
8440 current_vertex_color_clamp
= ctx_data
->glsl_program
->vs
.vertex_color_clamp
;
8445 current_vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
8448 if (old_vertex_color_clamp
!= current_vertex_color_clamp
)
8450 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
8452 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
8453 checkGLcall("glClampColorARB");
8457 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
8461 TRACE("Using GLSL program %u.\n", program_id
);
8463 if (prev_id
!= program_id
)
8465 GL_EXTCALL(glUseProgram(program_id
));
8466 checkGLcall("glUseProgram");
8469 context
->constant_update_mask
|= ctx_data
->glsl_program
->constant_update_mask
;
8473 /* "context" is not necessarily the currently active context. */
8474 static void shader_glsl_invalidate_current_program(struct wined3d_context
*context
)
8476 struct glsl_context_data
*ctx_data
= context
->shader_backend_data
;
8478 ctx_data
->glsl_program
= NULL
;
8479 context
->shader_update_mask
= (1u << WINED3D_SHADER_TYPE_PIXEL
)
8480 | (1u << WINED3D_SHADER_TYPE_VERTEX
)
8481 | (1u << WINED3D_SHADER_TYPE_GEOMETRY
)
8482 | (1u << WINED3D_SHADER_TYPE_HULL
)
8483 | (1u << WINED3D_SHADER_TYPE_DOMAIN
)
8484 | (1u << WINED3D_SHADER_TYPE_COMPUTE
);
8487 /* Context activation is done by the caller. */
8488 static void shader_glsl_disable(void *shader_priv
, struct wined3d_context
*context
)
8490 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8491 struct shader_glsl_priv
*priv
= shader_priv
;
8493 shader_glsl_invalidate_current_program(context
);
8494 GL_EXTCALL(glUseProgram(0));
8495 checkGLcall("glUseProgram");
8497 priv
->vertex_pipe
->vp_enable(gl_info
, FALSE
);
8498 priv
->fragment_pipe
->enable_extension(gl_info
, FALSE
);
8500 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
8502 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, GL_FIXED_ONLY_ARB
));
8503 checkGLcall("glClampColorARB");
8507 /* Context activation is done by the caller. */
8508 static void shader_glsl_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
8509 enum wined3d_gl_resource_type tex_type
, const SIZE
*ds_mask_size
)
8511 BOOL masked
= ds_mask_size
->cx
&& ds_mask_size
->cy
;
8512 struct shader_glsl_priv
*priv
= shader_priv
;
8513 GLuint
*blt_program
;
8516 blt_program
= masked
? &priv
->depth_blt_program_masked
[tex_type
] : &priv
->depth_blt_program_full
[tex_type
];
8519 *blt_program
= create_glsl_blt_shader(gl_info
, tex_type
, masked
);
8520 loc
= GL_EXTCALL(glGetUniformLocation(*blt_program
, "sampler"));
8521 GL_EXTCALL(glUseProgram(*blt_program
));
8522 GL_EXTCALL(glUniform1i(loc
, 0));
8526 GL_EXTCALL(glUseProgram(*blt_program
));
8531 loc
= GL_EXTCALL(glGetUniformLocation(*blt_program
, "mask"));
8532 GL_EXTCALL(glUniform4f(loc
, 0.0f
, 0.0f
, (float)ds_mask_size
->cx
, (float)ds_mask_size
->cy
));
8536 /* Context activation is done by the caller. */
8537 static void shader_glsl_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
)
8539 const struct glsl_context_data
*ctx_data
= context_get_current()->shader_backend_data
;
8542 program_id
= ctx_data
->glsl_program
? ctx_data
->glsl_program
->id
: 0;
8543 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
8545 GL_EXTCALL(glUseProgram(program_id
));
8546 checkGLcall("glUseProgram");
8549 static void shader_glsl_invalidate_contexts_program(struct wined3d_device
*device
,
8550 const struct glsl_shader_prog_link
*program
)
8552 const struct glsl_context_data
*ctx_data
;
8553 struct wined3d_context
*context
;
8556 for (i
= 0; i
< device
->context_count
; ++i
)
8558 context
= device
->contexts
[i
];
8559 ctx_data
= context
->shader_backend_data
;
8561 if (ctx_data
->glsl_program
== program
)
8562 shader_glsl_invalidate_current_program(context
);
8566 static void shader_glsl_destroy(struct wined3d_shader
*shader
)
8568 struct glsl_shader_private
*shader_data
= shader
->backend_data
;
8569 struct wined3d_device
*device
= shader
->device
;
8570 struct shader_glsl_priv
*priv
= device
->shader_priv
;
8571 const struct wined3d_gl_info
*gl_info
;
8572 const struct list
*linked_programs
;
8573 struct wined3d_context
*context
;
8575 if (!shader_data
|| !shader_data
->num_gl_shaders
)
8577 HeapFree(GetProcessHeap(), 0, shader_data
);
8578 shader
->backend_data
= NULL
;
8582 context
= context_acquire(device
, NULL
);
8583 gl_info
= context
->gl_info
;
8585 TRACE("Deleting linked programs.\n");
8586 linked_programs
= &shader
->linked_programs
;
8587 if (linked_programs
->next
)
8589 struct glsl_shader_prog_link
*entry
, *entry2
;
8592 switch (shader
->reg_maps
.shader_version
.type
)
8594 case WINED3D_SHADER_TYPE_PIXEL
:
8596 struct glsl_ps_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.ps
;
8598 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8600 TRACE("Deleting pixel shader %u.\n", gl_shaders
[i
].id
);
8601 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
8602 checkGLcall("glDeleteShader");
8604 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.ps
);
8606 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
8607 struct glsl_shader_prog_link
, ps
.shader_entry
)
8609 shader_glsl_invalidate_contexts_program(device
, entry
);
8610 delete_glsl_program_entry(priv
, gl_info
, entry
);
8616 case WINED3D_SHADER_TYPE_VERTEX
:
8618 struct glsl_vs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.vs
;
8620 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8622 TRACE("Deleting vertex shader %u.\n", gl_shaders
[i
].id
);
8623 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
8624 checkGLcall("glDeleteShader");
8626 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.vs
);
8628 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
8629 struct glsl_shader_prog_link
, vs
.shader_entry
)
8631 shader_glsl_invalidate_contexts_program(device
, entry
);
8632 delete_glsl_program_entry(priv
, gl_info
, entry
);
8638 case WINED3D_SHADER_TYPE_GEOMETRY
:
8640 struct glsl_gs_compiled_shader
*gl_shaders
= shader_data
->gl_shaders
.gs
;
8642 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
8644 TRACE("Deleting geometry shader %u.\n", gl_shaders
[i
].id
);
8645 GL_EXTCALL(glDeleteShader(gl_shaders
[i
].id
));
8646 checkGLcall("glDeleteShader");
8648 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
.gs
);
8650 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
,
8651 struct glsl_shader_prog_link
, gs
.shader_entry
)
8653 shader_glsl_invalidate_contexts_program(device
, entry
);
8654 delete_glsl_program_entry(priv
, gl_info
, entry
);
8661 ERR("Unhandled shader type %#x.\n", shader
->reg_maps
.shader_version
.type
);
8666 HeapFree(GetProcessHeap(), 0, shader
->backend_data
);
8667 shader
->backend_data
= NULL
;
8669 context_release(context
);
8672 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
8674 const struct glsl_program_key
*k
= key
;
8675 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
8676 const struct glsl_shader_prog_link
, program_lookup_entry
);
8678 if (k
->vs_id
> prog
->vs
.id
) return 1;
8679 else if (k
->vs_id
< prog
->vs
.id
) return -1;
8681 if (k
->gs_id
> prog
->gs
.id
) return 1;
8682 else if (k
->gs_id
< prog
->gs
.id
) return -1;
8684 if (k
->ps_id
> prog
->ps
.id
) return 1;
8685 else if (k
->ps_id
< prog
->ps
.id
) return -1;
8690 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
8692 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
)
8693 + constant_count
* sizeof(*heap
->contained
)
8694 + constant_count
* sizeof(*heap
->positions
);
8695 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
8699 ERR("Failed to allocate memory\n");
8703 heap
->entries
= mem
;
8704 heap
->entries
[1].version
= 0;
8705 heap
->contained
= (BOOL
*)(heap
->entries
+ constant_count
+ 1);
8706 memset(heap
->contained
, 0, constant_count
* sizeof(*heap
->contained
));
8707 heap
->positions
= (unsigned int *)(heap
->contained
+ constant_count
);
8713 static void constant_heap_free(struct constant_heap
*heap
)
8715 HeapFree(GetProcessHeap(), 0, heap
->entries
);
8718 static HRESULT
shader_glsl_alloc(struct wined3d_device
*device
, const struct wined3d_vertex_pipe_ops
*vertex_pipe
,
8719 const struct fragment_pipeline
*fragment_pipe
)
8721 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
8722 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
8723 SIZE_T stack_size
= wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F
, WINED3D_MAX_PS_CONSTS_F
)) + 1;
8724 struct fragment_caps fragment_caps
;
8725 void *vertex_priv
, *fragment_priv
;
8727 string_buffer_list_init(&priv
->string_buffers
);
8729 if (!(vertex_priv
= vertex_pipe
->vp_alloc(&glsl_shader_backend
, priv
)))
8731 ERR("Failed to initialize vertex pipe.\n");
8732 HeapFree(GetProcessHeap(), 0, priv
);
8736 if (!(fragment_priv
= fragment_pipe
->alloc_private(&glsl_shader_backend
, priv
)))
8738 ERR("Failed to initialize fragment pipe.\n");
8739 vertex_pipe
->vp_free(device
);
8740 HeapFree(GetProcessHeap(), 0, priv
);
8744 if (!string_buffer_init(&priv
->shader_buffer
))
8746 ERR("Failed to initialize shader buffer.\n");
8750 if (!(priv
->stack
= wined3d_calloc(stack_size
, sizeof(*priv
->stack
))))
8752 ERR("Failed to allocate memory.\n");
8756 if (!constant_heap_init(&priv
->vconst_heap
, WINED3D_MAX_VS_CONSTS_F
))
8758 ERR("Failed to initialize vertex shader constant heap\n");
8762 if (!constant_heap_init(&priv
->pconst_heap
, WINED3D_MAX_PS_CONSTS_F
))
8764 ERR("Failed to initialize pixel shader constant heap\n");
8768 wine_rb_init(&priv
->program_lookup
, glsl_program_key_compare
);
8770 priv
->next_constant_version
= 1;
8771 priv
->vertex_pipe
= vertex_pipe
;
8772 priv
->fragment_pipe
= fragment_pipe
;
8773 fragment_pipe
->get_caps(gl_info
, &fragment_caps
);
8774 priv
->ffp_proj_control
= fragment_caps
.wined3d_caps
& WINED3D_FRAGMENT_CAP_PROJ_CONTROL
;
8775 priv
->legacy_lighting
= device
->wined3d
->flags
& WINED3D_LEGACY_FFP_LIGHTING
;
8777 device
->vertex_priv
= vertex_priv
;
8778 device
->fragment_priv
= fragment_priv
;
8779 device
->shader_priv
= priv
;
8784 constant_heap_free(&priv
->pconst_heap
);
8785 constant_heap_free(&priv
->vconst_heap
);
8786 HeapFree(GetProcessHeap(), 0, priv
->stack
);
8787 string_buffer_free(&priv
->shader_buffer
);
8788 fragment_pipe
->free_private(device
);
8789 vertex_pipe
->vp_free(device
);
8790 HeapFree(GetProcessHeap(), 0, priv
);
8791 return E_OUTOFMEMORY
;
8794 /* Context activation is done by the caller. */
8795 static void shader_glsl_free(struct wined3d_device
*device
)
8797 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
8798 struct shader_glsl_priv
*priv
= device
->shader_priv
;
8801 for (i
= 0; i
< WINED3D_GL_RES_TYPE_COUNT
; ++i
)
8803 if (priv
->depth_blt_program_full
[i
])
8805 GL_EXTCALL(glDeleteProgram(priv
->depth_blt_program_full
[i
]));
8807 if (priv
->depth_blt_program_masked
[i
])
8809 GL_EXTCALL(glDeleteProgram(priv
->depth_blt_program_masked
[i
]));
8813 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
8814 constant_heap_free(&priv
->pconst_heap
);
8815 constant_heap_free(&priv
->vconst_heap
);
8816 HeapFree(GetProcessHeap(), 0, priv
->stack
);
8817 string_buffer_list_cleanup(&priv
->string_buffers
);
8818 string_buffer_free(&priv
->shader_buffer
);
8819 priv
->fragment_pipe
->free_private(device
);
8820 priv
->vertex_pipe
->vp_free(device
);
8822 HeapFree(GetProcessHeap(), 0, device
->shader_priv
);
8823 device
->shader_priv
= NULL
;
8826 static BOOL
shader_glsl_allocate_context_data(struct wined3d_context
*context
)
8828 return !!(context
->shader_backend_data
= HeapAlloc(GetProcessHeap(),
8829 HEAP_ZERO_MEMORY
, sizeof(struct glsl_context_data
)));
8832 static void shader_glsl_free_context_data(struct wined3d_context
*context
)
8834 HeapFree(GetProcessHeap(), 0, context
->shader_backend_data
);
8837 static void shader_glsl_init_context_state(struct wined3d_context
*context
)
8839 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
8841 gl_info
->gl_ops
.gl
.p_glEnable(GL_PROGRAM_POINT_SIZE
);
8842 checkGLcall("GL_PROGRAM_POINT_SIZE");
8845 static void shader_glsl_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
8849 /* FIXME: Check for the specific extensions required for SM5 support
8850 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8851 * soon as we introduce them, adjusting the GL / GLSL version checks
8853 if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(4, 30) && gl_info
->supported
[WINED3D_GL_VERSION_4_3
]
8854 && gl_info
->supported
[ARB_DERIVATIVE_CONTROL
]
8855 && gl_info
->supported
[ARB_SHADER_IMAGE_LOAD_STORE
]
8856 && gl_info
->supported
[ARB_SHADER_IMAGE_SIZE
])
8858 else if (gl_info
->glsl_version
>= MAKEDWORD_VERSION(1, 50) && gl_info
->supported
[WINED3D_GL_VERSION_3_2
]
8859 && gl_info
->supported
[ARB_SHADER_BIT_ENCODING
] && gl_info
->supported
[ARB_SAMPLER_OBJECTS
]
8860 && gl_info
->supported
[ARB_TEXTURE_SWIZZLE
])
8862 /* Support for texldd and texldl instructions in pixel shaders is required
8864 else if (shader_glsl_has_core_grad(gl_info
, NULL
) || gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
8868 TRACE("Shader model %u.\n", shader_model
);
8870 caps
->vs_version
= min(wined3d_settings
.max_sm_vs
, shader_model
);
8871 caps
->hs_version
= min(wined3d_settings
.max_sm_hs
, shader_model
);
8872 caps
->ds_version
= min(wined3d_settings
.max_sm_ds
, shader_model
);
8873 caps
->gs_version
= min(wined3d_settings
.max_sm_gs
, shader_model
);
8874 caps
->ps_version
= min(wined3d_settings
.max_sm_ps
, shader_model
);
8875 caps
->cs_version
= min(wined3d_settings
.max_sm_cs
, shader_model
);
8877 caps
->vs_uniform_count
= min(WINED3D_MAX_VS_CONSTS_F
, gl_info
->limits
.glsl_vs_float_constants
);
8878 caps
->ps_uniform_count
= min(WINED3D_MAX_PS_CONSTS_F
, gl_info
->limits
.glsl_ps_float_constants
);
8879 caps
->varying_count
= gl_info
->limits
.glsl_varyings
;
8881 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8882 * Direct3D minimum requirement.
8884 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8885 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8887 * The problem is that the refrast clamps temporary results in the shader to
8888 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8889 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8890 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8891 * offer a way to query this.
8893 if (shader_model
>= 4)
8894 caps
->ps_1x_max_value
= FLT_MAX
;
8896 caps
->ps_1x_max_value
= 1024.0f
;
8898 /* Ideally we'd only set caps like sRGB writes here if supported by both
8899 * the shader backend and the fragment pipe, but we can get called before
8900 * shader_glsl_alloc(). */
8901 caps
->wined3d_caps
= WINED3D_SHADER_CAP_VS_CLIPPING
8902 | WINED3D_SHADER_CAP_SRGB_WRITE
;
8905 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
8907 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
8909 TRACE("Checking support for fixup:\n");
8910 dump_color_fixup_desc(fixup
);
8913 /* We support everything except YUV conversions. */
8914 if (!is_complex_fixup(fixup
))
8920 TRACE("[FAILED]\n");
8924 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
8926 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
8927 /* WINED3DSIH_ADD */ shader_glsl_binop
,
8928 /* WINED3DSIH_AND */ shader_glsl_binop
,
8929 /* WINED3DSIH_ATOMIC_AND */ NULL
,
8930 /* WINED3DSIH_ATOMIC_CMP_STORE */ NULL
,
8931 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic
,
8932 /* WINED3DSIH_ATOMIC_IMAX */ NULL
,
8933 /* WINED3DSIH_ATOMIC_IMIN */ NULL
,
8934 /* WINED3DSIH_ATOMIC_OR */ NULL
,
8935 /* WINED3DSIH_ATOMIC_UMAX */ NULL
,
8936 /* WINED3DSIH_ATOMIC_UMIN */ NULL
,
8937 /* WINED3DSIH_ATOMIC_XOR */ NULL
,
8938 /* WINED3DSIH_BEM */ shader_glsl_bem
,
8939 /* WINED3DSIH_BFI */ NULL
,
8940 /* WINED3DSIH_BFREV */ NULL
,
8941 /* WINED3DSIH_BREAK */ shader_glsl_break
,
8942 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
8943 /* WINED3DSIH_BREAKP */ shader_glsl_breakp
,
8944 /* WINED3DSIH_BUFINFO */ NULL
,
8945 /* WINED3DSIH_CALL */ shader_glsl_call
,
8946 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
8947 /* WINED3DSIH_CASE */ shader_glsl_case
,
8948 /* WINED3DSIH_CMP */ shader_glsl_conditional_move
,
8949 /* WINED3DSIH_CND */ shader_glsl_cnd
,
8950 /* WINED3DSIH_CONTINUE */ shader_glsl_continue
,
8951 /* WINED3DSIH_CRS */ shader_glsl_cross
,
8952 /* WINED3DSIH_CUT */ shader_glsl_cut
,
8953 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut
,
8954 /* WINED3DSIH_DCL */ shader_glsl_nop
,
8955 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop
,
8956 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL
,
8957 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL
,
8958 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop
,
8959 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL
,
8960 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL
,
8961 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop
,
8962 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop
,
8963 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop
,
8964 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL
,
8965 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop
,
8966 /* WINED3DSIH_DCL_INPUT_PS */ NULL
,
8967 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL
,
8968 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL
,
8969 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop
,
8970 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop
,
8971 /* WINED3DSIH_DCL_INTERFACE */ NULL
,
8972 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop
,
8973 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL
,
8974 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop
,
8975 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop
,
8976 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL
,
8977 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop
,
8978 /* WINED3DSIH_DCL_STREAM */ NULL
,
8979 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop
,
8980 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL
,
8981 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL
,
8982 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL
,
8983 /* WINED3DSIH_DCL_TGSM_RAW */ NULL
,
8984 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL
,
8985 /* WINED3DSIH_DCL_THREAD_GROUP */ NULL
,
8986 /* WINED3DSIH_DCL_UAV_RAW */ NULL
,
8987 /* WINED3DSIH_DCL_UAV_STRUCTURED */ NULL
,
8988 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop
,
8989 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop
,
8990 /* WINED3DSIH_DEF */ shader_glsl_nop
,
8991 /* WINED3DSIH_DEFAULT */ shader_glsl_default
,
8992 /* WINED3DSIH_DEFB */ shader_glsl_nop
,
8993 /* WINED3DSIH_DEFI */ shader_glsl_nop
,
8994 /* WINED3DSIH_DIV */ shader_glsl_binop
,
8995 /* WINED3DSIH_DP2 */ shader_glsl_dot
,
8996 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add
,
8997 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
8998 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
8999 /* WINED3DSIH_DST */ shader_glsl_dst
,
9000 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
9001 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl
,
9002 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl
,
9003 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
9004 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl
,
9005 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl
,
9006 /* WINED3DSIH_ELSE */ shader_glsl_else
,
9007 /* WINED3DSIH_EMIT */ shader_glsl_emit
,
9008 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit
,
9009 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
9010 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
9011 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
9012 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end
,
9013 /* WINED3DSIH_EQ */ shader_glsl_relop
,
9014 /* WINED3DSIH_EXP */ shader_glsl_scalar_op
,
9015 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
9016 /* WINED3DSIH_FCALL */ NULL
,
9017 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
9018 /* WINED3DSIH_FTOI */ shader_glsl_to_int
,
9019 /* WINED3DSIH_FTOU */ shader_glsl_to_uint
,
9020 /* WINED3DSIH_GATHER4 */ NULL
,
9021 /* WINED3DSIH_GATHER4_C */ NULL
,
9022 /* WINED3DSIH_GE */ shader_glsl_relop
,
9023 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL
,
9024 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop
,
9025 /* WINED3DSIH_HS_FORK_PHASE */ NULL
,
9026 /* WINED3DSIH_HS_JOIN_PHASE */ NULL
,
9027 /* WINED3DSIH_IADD */ shader_glsl_binop
,
9028 /* WINED3DSIH_IEQ */ shader_glsl_relop
,
9029 /* WINED3DSIH_IF */ shader_glsl_if
,
9030 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
9031 /* WINED3DSIH_IGE */ shader_glsl_relop
,
9032 /* WINED3DSIH_ILT */ shader_glsl_relop
,
9033 /* WINED3DSIH_IMAD */ shader_glsl_mad
,
9034 /* WINED3DSIH_IMAX */ shader_glsl_map2gl
,
9035 /* WINED3DSIH_IMIN */ shader_glsl_map2gl
,
9036 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL
,
9037 /* WINED3DSIH_IMM_ATOMIC_AND */ NULL
,
9038 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ NULL
,
9039 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL
,
9040 /* WINED3DSIH_IMM_ATOMIC_EXCH */ NULL
,
9041 /* WINED3DSIH_IMM_ATOMIC_OR */ NULL
,
9042 /* WINED3DSIH_IMM_ATOMIC_UMAX */ NULL
,
9043 /* WINED3DSIH_IMM_ATOMIC_UMIN */ NULL
,
9044 /* WINED3DSIH_IMM_ATOMIC_XOR */ NULL
,
9045 /* WINED3DSIH_IMUL */ shader_glsl_imul
,
9046 /* WINED3DSIH_INE */ shader_glsl_relop
,
9047 /* WINED3DSIH_INEG */ shader_glsl_unary_op
,
9048 /* WINED3DSIH_ISHL */ shader_glsl_binop
,
9049 /* WINED3DSIH_ISHR */ shader_glsl_binop
,
9050 /* WINED3DSIH_ITOF */ shader_glsl_to_float
,
9051 /* WINED3DSIH_LABEL */ shader_glsl_label
,
9052 /* WINED3DSIH_LD */ shader_glsl_ld
,
9053 /* WINED3DSIH_LD2DMS */ NULL
,
9054 /* WINED3DSIH_LD_RAW */ NULL
,
9055 /* WINED3DSIH_LD_STRUCTURED */ NULL
,
9056 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav
,
9057 /* WINED3DSIH_LIT */ shader_glsl_lit
,
9058 /* WINED3DSIH_LOD */ NULL
,
9059 /* WINED3DSIH_LOG */ shader_glsl_scalar_op
,
9060 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op
,
9061 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
9062 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
9063 /* WINED3DSIH_LT */ shader_glsl_relop
,
9064 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
9065 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
9066 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
9067 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
9068 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
9069 /* WINED3DSIH_MAD */ shader_glsl_mad
,
9070 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
9071 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
9072 /* WINED3DSIH_MOV */ shader_glsl_mov
,
9073 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
9074 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move
,
9075 /* WINED3DSIH_MUL */ shader_glsl_binop
,
9076 /* WINED3DSIH_NE */ shader_glsl_relop
,
9077 /* WINED3DSIH_NOP */ shader_glsl_nop
,
9078 /* WINED3DSIH_NOT */ shader_glsl_unary_op
,
9079 /* WINED3DSIH_NRM */ shader_glsl_nrm
,
9080 /* WINED3DSIH_OR */ shader_glsl_binop
,
9081 /* WINED3DSIH_PHASE */ shader_glsl_nop
,
9082 /* WINED3DSIH_POW */ shader_glsl_pow
,
9083 /* WINED3DSIH_RCP */ shader_glsl_scalar_op
,
9084 /* WINED3DSIH_REP */ shader_glsl_rep
,
9085 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo
,
9086 /* WINED3DSIH_RET */ shader_glsl_ret
,
9087 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl
,
9088 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl
,
9089 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl
,
9090 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl
,
9091 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op
,
9092 /* WINED3DSIH_SAMPLE */ shader_glsl_sample
,
9093 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample
,
9094 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c
,
9095 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c
,
9096 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample
,
9097 /* WINED3DSIH_SAMPLE_INFO */ NULL
,
9098 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample
,
9099 /* WINED3DSIH_SAMPLE_POS */ NULL
,
9100 /* WINED3DSIH_SETP */ NULL
,
9101 /* WINED3DSIH_SGE */ shader_glsl_compare
,
9102 /* WINED3DSIH_SGN */ shader_glsl_sgn
,
9103 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
9104 /* WINED3DSIH_SLT */ shader_glsl_compare
,
9105 /* WINED3DSIH_SQRT */ shader_glsl_map2gl
,
9106 /* WINED3DSIH_STORE_RAW */ NULL
,
9107 /* WINED3DSIH_STORE_STRUCTURED */ NULL
,
9108 /* WINED3DSIH_STORE_UAV_TYPED */ NULL
,
9109 /* WINED3DSIH_SUB */ shader_glsl_binop
,
9110 /* WINED3DSIH_SWAPC */ NULL
,
9111 /* WINED3DSIH_SWITCH */ shader_glsl_switch
,
9112 /* WINED3DSIH_SYNC */ NULL
,
9113 /* WINED3DSIH_TEX */ shader_glsl_tex
,
9114 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem
,
9115 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem
,
9116 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord
,
9117 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth
,
9118 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3
,
9119 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex
,
9120 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill
,
9121 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
9122 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
9123 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth
,
9124 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad
,
9125 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex
,
9126 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3
,
9127 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
9128 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad
,
9129 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec
,
9130 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex
,
9131 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec
,
9132 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar
,
9133 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb
,
9134 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb
,
9135 /* WINED3DSIH_UBFE */ NULL
,
9136 /* WINED3DSIH_UDIV */ shader_glsl_udiv
,
9137 /* WINED3DSIH_UGE */ shader_glsl_relop
,
9138 /* WINED3DSIH_ULT */ shader_glsl_relop
,
9139 /* WINED3DSIH_UMAX */ shader_glsl_map2gl
,
9140 /* WINED3DSIH_UMIN */ shader_glsl_map2gl
,
9141 /* WINED3DSIH_USHR */ shader_glsl_binop
,
9142 /* WINED3DSIH_UTOF */ shader_glsl_to_float
,
9143 /* WINED3DSIH_XOR */ shader_glsl_binop
,
9146 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
9147 SHADER_HANDLER hw_fct
;
9149 /* Select handler */
9150 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
9152 /* Unhandled opcode */
9155 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins
->handler_idx
));
9160 shader_glsl_add_instruction_modifiers(ins
);
9163 static BOOL
shader_glsl_has_ffp_proj_control(void *shader_priv
)
9165 struct shader_glsl_priv
*priv
= shader_priv
;
9167 return priv
->ffp_proj_control
;
9170 const struct wined3d_shader_backend_ops glsl_shader_backend
=
9172 shader_glsl_handle_instruction
,
9174 shader_glsl_disable
,
9175 shader_glsl_select_depth_blt
,
9176 shader_glsl_deselect_depth_blt
,
9177 shader_glsl_update_float_vertex_constants
,
9178 shader_glsl_update_float_pixel_constants
,
9179 shader_glsl_load_constants
,
9180 shader_glsl_destroy
,
9183 shader_glsl_allocate_context_data
,
9184 shader_glsl_free_context_data
,
9185 shader_glsl_init_context_state
,
9186 shader_glsl_get_caps
,
9187 shader_glsl_color_fixup_supported
,
9188 shader_glsl_has_ffp_proj_control
,
9191 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
) {}
9193 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info
*gl_info
, struct wined3d_vertex_caps
*caps
)
9195 caps
->xyzrhw
= TRUE
;
9196 caps
->emulated_flatshading
= !gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
9197 caps
->ffp_generic_attributes
= TRUE
;
9198 caps
->max_active_lights
= MAX_ACTIVE_LIGHTS
;
9199 caps
->max_vertex_blend_matrices
= MAX_VERTEX_BLENDS
;
9200 caps
->max_vertex_blend_matrix_index
= 0;
9201 caps
->vertex_processing_caps
= WINED3DVTXPCAPS_TEXGEN
9202 | WINED3DVTXPCAPS_MATERIALSOURCE7
9203 | WINED3DVTXPCAPS_VERTEXFOG
9204 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9205 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9206 | WINED3DVTXPCAPS_LOCALVIEWER
9207 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP
;
9208 caps
->fvf_caps
= WINED3DFVFCAPS_PSIZE
| 8; /* 8 texture coordinates. */
9209 caps
->max_user_clip_planes
= gl_info
->limits
.user_clip_distances
;
9210 caps
->raster_caps
= WINED3DPRASTERCAPS_FOGRANGE
;
9213 static DWORD
glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
9215 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
9216 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
9220 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
9222 struct shader_glsl_priv
*priv
;
9224 if (shader_backend
== &glsl_shader_backend
)
9227 wine_rb_init(&priv
->ffp_vertex_shaders
, wined3d_ffp_vertex_program_key_compare
);
9231 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9236 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry
*entry
, void *context
)
9238 struct glsl_ffp_vertex_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
9239 struct glsl_ffp_vertex_shader
, desc
.entry
);
9240 struct glsl_shader_prog_link
*program
, *program2
;
9241 struct glsl_ffp_destroy_ctx
*ctx
= context
;
9243 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
9244 struct glsl_shader_prog_link
, vs
.shader_entry
)
9246 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
9248 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
9249 HeapFree(GetProcessHeap(), 0, shader
);
9252 /* Context activation is done by the caller. */
9253 static void glsl_vertex_pipe_vp_free(struct wined3d_device
*device
)
9255 struct shader_glsl_priv
*priv
= device
->vertex_priv
;
9256 struct glsl_ffp_destroy_ctx ctx
;
9259 ctx
.gl_info
= &device
->adapter
->gl_info
;
9260 wine_rb_destroy(&priv
->ffp_vertex_shaders
, shader_glsl_free_ffp_vertex_shader
, &ctx
);
9263 static void glsl_vertex_pipe_nop(struct wined3d_context
*context
,
9264 const struct wined3d_state
*state
, DWORD state_id
) {}
9266 static void glsl_vertex_pipe_shader(struct wined3d_context
*context
,
9267 const struct wined3d_state
*state
, DWORD state_id
)
9269 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9272 static void glsl_vertex_pipe_vdecl(struct wined3d_context
*context
,
9273 const struct wined3d_state
*state
, DWORD state_id
)
9275 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9276 BOOL normal
= !!(context
->stream_info
.use_map
& (1u << WINED3D_FFP_NORMAL
));
9277 BOOL legacy_context
= gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
];
9278 BOOL transformed
= context
->stream_info
.position_transformed
;
9279 BOOL wasrhw
= context
->last_was_rhw
;
9282 context
->last_was_rhw
= transformed
;
9284 /* If the vertex declaration contains a transformed position attribute,
9285 * the draw uses the fixed function vertex pipeline regardless of any
9286 * vertex shader set by the application. */
9287 if (transformed
!= wasrhw
9288 || context
->stream_info
.swizzle_map
!= context
->last_swizzle_map
)
9289 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9291 context
->last_swizzle_map
= context
->stream_info
.swizzle_map
;
9295 if (context
->last_was_vshader
)
9298 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
9299 clipplane(context
, state
, STATE_CLIPPLANE(i
));
9301 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
9304 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
9306 /* Because of settings->texcoords, we have to regenerate the vertex
9307 * shader on a vdecl change if there aren't enough varyings to just
9308 * always output all the texture coordinates. */
9309 if (gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(gl_info
)
9310 || normal
!= context
->last_was_normal
)
9311 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9314 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.major
== 1
9315 && state
->shader
[WINED3D_SHADER_TYPE_PIXEL
]->reg_maps
.shader_version
.minor
<= 3)
9316 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9320 if (!context
->last_was_vshader
)
9322 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
9324 for (i
= 0; i
< gl_info
->limits
.user_clip_distances
; ++i
)
9325 clipplane(context
, state
, STATE_CLIPPLANE(i
));
9327 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
9331 context
->last_was_vshader
= use_vs(state
);
9332 context
->last_was_normal
= normal
;
9335 static void glsl_vertex_pipe_vs(struct wined3d_context
*context
,
9336 const struct wined3d_state
*state
, DWORD state_id
)
9338 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9339 /* Different vertex shaders potentially require a different vertex attributes setup. */
9340 if (!isStateDirty(context
, STATE_VDECL
))
9341 context_apply_state(context
, state
, STATE_VDECL
);
9344 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context
*context
,
9345 const struct wined3d_state
*state
, DWORD state_id
)
9347 if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]
9348 && state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.shader_version
.major
>= 4)
9349 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9352 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context
*context
,
9353 const struct wined3d_state
*state
, DWORD state_id
)
9355 if (state
->shader
[WINED3D_SHADER_TYPE_GEOMETRY
])
9356 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_GEOMETRY
;
9357 else if (state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]
9358 && state
->shader
[WINED3D_SHADER_TYPE_VERTEX
]->reg_maps
.shader_version
.major
>= 4)
9359 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9362 static void glsl_vertex_pipe_world(struct wined3d_context
*context
,
9363 const struct wined3d_state
*state
, DWORD state_id
)
9365 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
;
9368 static void glsl_vertex_pipe_vertexblend(struct wined3d_context
*context
,
9369 const struct wined3d_state
*state
, DWORD state_id
)
9371 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
9374 static void glsl_vertex_pipe_view(struct wined3d_context
*context
, const struct wined3d_state
*state
, DWORD state_id
)
9376 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9379 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MODELVIEW
9380 | WINED3D_SHADER_CONST_FFP_LIGHTS
9381 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND
;
9383 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
9385 for (k
= 0; k
< gl_info
->limits
.user_clip_distances
; ++k
)
9387 if (!isStateDirty(context
, STATE_CLIPPLANE(k
)))
9388 clipplane(context
, state
, STATE_CLIPPLANE(k
));
9393 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
9397 static void glsl_vertex_pipe_projection(struct wined3d_context
*context
,
9398 const struct wined3d_state
*state
, DWORD state_id
)
9400 /* Table fog behavior depends on the projection matrix. */
9401 if (state
->render_states
[WINED3D_RS_FOGENABLE
]
9402 && state
->render_states
[WINED3D_RS_FOGTABLEMODE
] != WINED3D_FOG_NONE
)
9403 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9404 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PROJ
;
9407 static void glsl_vertex_pipe_viewport(struct wined3d_context
*context
,
9408 const struct wined3d_state
*state
, DWORD state_id
)
9410 if (!isStateDirty(context
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
)))
9411 glsl_vertex_pipe_projection(context
, state
, STATE_TRANSFORM(WINED3D_TS_PROJECTION
));
9412 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
))
9413 && state
->render_states
[WINED3D_RS_POINTSCALEENABLE
])
9414 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
9415 context
->constant_update_mask
|= WINED3D_SHADER_CONST_POS_FIXUP
;
9418 static void glsl_vertex_pipe_texmatrix(struct wined3d_context
*context
,
9419 const struct wined3d_state
*state
, DWORD state_id
)
9421 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
9424 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context
*context
,
9425 const struct wined3d_state
*state
, DWORD state_id
)
9427 DWORD sampler
= state_id
- STATE_SAMPLER(0);
9428 const struct wined3d_texture
*texture
= state
->textures
[sampler
];
9434 if (sampler
>= MAX_TEXTURES
)
9437 if ((np2
= !(texture
->flags
& WINED3D_TEXTURE_POW2_MAT_IDENT
))
9438 || context
->lastWasPow2Texture
& (1u << sampler
))
9441 context
->lastWasPow2Texture
|= 1u << sampler
;
9443 context
->lastWasPow2Texture
&= ~(1u << sampler
);
9445 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_TEXMATRIX
;
9449 static void glsl_vertex_pipe_material(struct wined3d_context
*context
,
9450 const struct wined3d_state
*state
, DWORD state_id
)
9452 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_MATERIAL
;
9455 static void glsl_vertex_pipe_light(struct wined3d_context
*context
,
9456 const struct wined3d_state
*state
, DWORD state_id
)
9458 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_LIGHTS
;
9461 static void glsl_vertex_pipe_pointsize(struct wined3d_context
*context
,
9462 const struct wined3d_state
*state
, DWORD state_id
)
9464 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
9467 static void glsl_vertex_pipe_pointscale(struct wined3d_context
*context
,
9468 const struct wined3d_state
*state
, DWORD state_id
)
9471 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_POINTSIZE
;
9474 static void glsl_vertex_pointsprite_core(struct wined3d_context
*context
,
9475 const struct wined3d_state
*state
, DWORD state_id
)
9477 static unsigned int once
;
9479 if (state
->gl_primitive_type
== GL_POINTS
&& !state
->render_states
[WINED3D_RS_POINTSPRITEENABLE
] && !once
++)
9480 FIXME("Non-point sprite points not supported in core profile.\n");
9483 static void glsl_vertex_pipe_shademode(struct wined3d_context
*context
,
9484 const struct wined3d_state
*state
, DWORD state_id
)
9486 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_VERTEX
;
9489 static void glsl_vertex_pipe_clip_plane(struct wined3d_context
*context
,
9490 const struct wined3d_state
*state
, DWORD state_id
)
9492 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9493 UINT index
= state_id
- STATE_CLIPPLANE(0);
9495 if (index
>= gl_info
->limits
.user_clip_distances
)
9498 context
->constant_update_mask
|= WINED3D_SHADER_CONST_VS_CLIP_PLANES
;
9501 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states
[] =
9503 {STATE_VDECL
, {STATE_VDECL
, glsl_vertex_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
9504 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_vertex_pipe_vs
}, WINED3D_GL_EXT_NONE
},
9505 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY
), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY
), glsl_vertex_pipe_geometry_shader
}, WINED3D_GL_EXT_NONE
},
9506 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_vertex_pipe_pixel_shader
}, WINED3D_GL_EXT_NONE
},
9507 {STATE_MATERIAL
, {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9508 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_vertex_pipe_material
}, WINED3D_GL_EXT_NONE
},
9510 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9511 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9512 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9513 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9514 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9515 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9516 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9517 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9518 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9519 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9520 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9521 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9522 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9523 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9524 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9525 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9526 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9527 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9528 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9529 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9530 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9531 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9532 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9533 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9534 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9535 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9536 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9537 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9538 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9539 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9540 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9541 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9542 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9543 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9544 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9545 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9546 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9547 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9548 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9549 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9550 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9551 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9552 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9553 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9554 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9555 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9556 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9557 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9558 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9559 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9560 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9561 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9562 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9563 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9564 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9565 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9566 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9567 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9568 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9569 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9570 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9571 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9572 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane
}, WINED3D_GL_LEGACY_CONTEXT
},
9573 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane
}, WINED3D_GL_EXT_NONE
},
9575 {STATE_LIGHT_TYPE
, {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9576 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9577 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9578 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9579 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9580 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9581 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9582 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9583 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9585 {STATE_VIEWPORT
, {STATE_VIEWPORT
, glsl_vertex_pipe_viewport
}, WINED3D_GL_EXT_NONE
},
9586 /* Transform states */
9587 {STATE_TRANSFORM(WINED3D_TS_VIEW
), {STATE_TRANSFORM(WINED3D_TS_VIEW
), glsl_vertex_pipe_view
}, WINED3D_GL_EXT_NONE
},
9588 {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), {STATE_TRANSFORM(WINED3D_TS_PROJECTION
), glsl_vertex_pipe_projection
}, WINED3D_GL_EXT_NONE
},
9589 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9590 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9591 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9592 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9593 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9594 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9595 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9596 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), NULL
}, WINED3D_GL_EXT_NONE
},
9597 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world
}, WINED3D_GL_EXT_NONE
},
9598 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
9599 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
9600 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend
}, WINED3D_GL_EXT_NONE
},
9601 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9602 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9603 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9604 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9605 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9606 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9607 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9608 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS
), glsl_vertex_pipe_texmatrix
}, WINED3D_GL_EXT_NONE
},
9609 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9610 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9611 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9612 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9613 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9614 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9615 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9616 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9618 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
9619 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9620 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9621 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9622 {STATE_RENDER(WINED3D_RS_CLIPPING
), {STATE_RENDER(WINED3D_RS_CLIPPING
), state_clipping
}, WINED3D_GL_EXT_NONE
},
9623 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE
), {STATE_RENDER(WINED3D_RS_CLIPPING
), NULL
}, WINED3D_GL_EXT_NONE
},
9624 {STATE_RENDER(WINED3D_RS_LIGHTING
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9625 {STATE_RENDER(WINED3D_RS_AMBIENT
), {STATE_RENDER(WINED3D_RS_AMBIENT
), glsl_vertex_pipe_light
}, WINED3D_GL_EXT_NONE
},
9626 {STATE_RENDER(WINED3D_RS_COLORVERTEX
), {STATE_RENDER(WINED3D_RS_COLORVERTEX
), glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
9627 {STATE_RENDER(WINED3D_RS_LOCALVIEWER
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9628 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9629 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9630 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9631 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9632 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9633 {STATE_RENDER(WINED3D_RS_VERTEXBLEND
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9634 {STATE_RENDER(WINED3D_RS_POINTSIZE
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
9635 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), glsl_vertex_pipe_pointsize
}, WINED3D_GL_EXT_NONE
},
9636 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite
}, ARB_POINT_SPRITE
},
9637 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), state_pointsprite_w
}, WINED3D_GL_LEGACY_CONTEXT
},
9638 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_vertex_pointsprite_core
}, WINED3D_GL_EXT_NONE
},
9639 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), glsl_vertex_pipe_pointscale
}, WINED3D_GL_EXT_NONE
},
9640 {STATE_RENDER(WINED3D_RS_POINTSCALE_A
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9641 {STATE_RENDER(WINED3D_RS_POINTSCALE_B
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9642 {STATE_RENDER(WINED3D_RS_POINTSCALE_C
), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9643 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX
), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN
), NULL
}, WINED3D_GL_EXT_NONE
},
9644 {STATE_RENDER(WINED3D_RS_TWEENFACTOR
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9645 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), NULL
}, WINED3D_GL_EXT_NONE
},
9646 /* NP2 texture matrix fixups. They are not needed if
9647 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
9648 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
9650 {STATE_SAMPLER(0), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9651 {STATE_SAMPLER(0), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9652 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9653 {STATE_SAMPLER(1), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9654 {STATE_SAMPLER(1), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9655 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9656 {STATE_SAMPLER(2), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9657 {STATE_SAMPLER(2), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9658 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9659 {STATE_SAMPLER(3), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9660 {STATE_SAMPLER(3), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9661 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9662 {STATE_SAMPLER(4), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9663 {STATE_SAMPLER(4), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9664 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9665 {STATE_SAMPLER(5), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9666 {STATE_SAMPLER(5), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9667 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9668 {STATE_SAMPLER(6), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9669 {STATE_SAMPLER(6), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9670 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9671 {STATE_SAMPLER(7), {0, NULL
}, ARB_TEXTURE_NON_POWER_OF_TWO
},
9672 {STATE_SAMPLER(7), {0, NULL
}, WINED3D_GL_NORMALIZED_TEXRECT
},
9673 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2
}, WINED3D_GL_EXT_NONE
},
9674 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_vertex_pipe_shader
}, WINED3D_GL_EXT_NONE
},
9675 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_nop
}, WINED3D_GL_LEGACY_CONTEXT
},
9676 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_vertex_pipe_shademode
}, WINED3D_GL_EXT_NONE
},
9677 {0 /* Terminate */, {0, NULL
}, WINED3D_GL_EXT_NONE
},
9681 * - Implement vertex tweening. */
9682 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe
=
9684 glsl_vertex_pipe_vp_enable
,
9685 glsl_vertex_pipe_vp_get_caps
,
9686 glsl_vertex_pipe_vp_get_emul_mask
,
9687 glsl_vertex_pipe_vp_alloc
,
9688 glsl_vertex_pipe_vp_free
,
9689 glsl_vertex_pipe_vp_states
,
9692 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info
*gl_info
, BOOL enable
)
9694 /* Nothing to do. */
9697 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info
*gl_info
, struct fragment_caps
*caps
)
9699 caps
->wined3d_caps
= WINED3D_FRAGMENT_CAP_PROJ_CONTROL
9700 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
9701 | WINED3D_FRAGMENT_CAP_COLOR_KEY
;
9702 caps
->PrimitiveMiscCaps
= WINED3DPMISCCAPS_TSSARGTEMP
9703 | WINED3DPMISCCAPS_PERSTAGECONSTANT
;
9704 caps
->TextureOpCaps
= WINED3DTEXOPCAPS_DISABLE
9705 | WINED3DTEXOPCAPS_SELECTARG1
9706 | WINED3DTEXOPCAPS_SELECTARG2
9707 | WINED3DTEXOPCAPS_MODULATE4X
9708 | WINED3DTEXOPCAPS_MODULATE2X
9709 | WINED3DTEXOPCAPS_MODULATE
9710 | WINED3DTEXOPCAPS_ADDSIGNED2X
9711 | WINED3DTEXOPCAPS_ADDSIGNED
9712 | WINED3DTEXOPCAPS_ADD
9713 | WINED3DTEXOPCAPS_SUBTRACT
9714 | WINED3DTEXOPCAPS_ADDSMOOTH
9715 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
9716 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
9717 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
9718 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
9719 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
9720 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
9721 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
9722 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
9723 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
9724 | WINED3DTEXOPCAPS_DOTPRODUCT3
9725 | WINED3DTEXOPCAPS_MULTIPLYADD
9726 | WINED3DTEXOPCAPS_LERP
9727 | WINED3DTEXOPCAPS_BUMPENVMAP
9728 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE
;
9729 caps
->MaxTextureBlendStages
= MAX_TEXTURES
;
9730 caps
->MaxSimultaneousTextures
= min(gl_info
->limits
.fragment_samplers
, MAX_TEXTURES
);
9733 static DWORD
glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info
*gl_info
)
9735 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
])
9736 return GL_EXT_EMUL_ARB_MULTITEXTURE
;
9740 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops
*shader_backend
, void *shader_priv
)
9742 struct shader_glsl_priv
*priv
;
9744 if (shader_backend
== &glsl_shader_backend
)
9747 wine_rb_init(&priv
->ffp_fragment_shaders
, wined3d_ffp_frag_program_key_compare
);
9751 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
9756 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry
*entry
, void *context
)
9758 struct glsl_ffp_fragment_shader
*shader
= WINE_RB_ENTRY_VALUE(entry
,
9759 struct glsl_ffp_fragment_shader
, entry
.entry
);
9760 struct glsl_shader_prog_link
*program
, *program2
;
9761 struct glsl_ffp_destroy_ctx
*ctx
= context
;
9763 LIST_FOR_EACH_ENTRY_SAFE(program
, program2
, &shader
->linked_programs
,
9764 struct glsl_shader_prog_link
, ps
.shader_entry
)
9766 delete_glsl_program_entry(ctx
->priv
, ctx
->gl_info
, program
);
9768 ctx
->gl_info
->gl_ops
.ext
.p_glDeleteShader(shader
->id
);
9769 HeapFree(GetProcessHeap(), 0, shader
);
9772 /* Context activation is done by the caller. */
9773 static void glsl_fragment_pipe_free(struct wined3d_device
*device
)
9775 struct shader_glsl_priv
*priv
= device
->fragment_priv
;
9776 struct glsl_ffp_destroy_ctx ctx
;
9779 ctx
.gl_info
= &device
->adapter
->gl_info
;
9780 wine_rb_destroy(&priv
->ffp_fragment_shaders
, shader_glsl_free_ffp_fragment_shader
, &ctx
);
9783 static void glsl_fragment_pipe_shader(struct wined3d_context
*context
,
9784 const struct wined3d_state
*state
, DWORD state_id
)
9786 context
->last_was_pshader
= use_ps(state
);
9788 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9791 static void glsl_fragment_pipe_fogparams(struct wined3d_context
*context
,
9792 const struct wined3d_state
*state
, DWORD state_id
)
9794 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
9797 static void glsl_fragment_pipe_fog(struct wined3d_context
*context
,
9798 const struct wined3d_state
*state
, DWORD state_id
)
9800 BOOL use_vshader
= use_vs(state
);
9801 enum fogsource new_source
;
9802 DWORD fogstart
= state
->render_states
[WINED3D_RS_FOGSTART
];
9803 DWORD fogend
= state
->render_states
[WINED3D_RS_FOGEND
];
9805 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9807 if (!state
->render_states
[WINED3D_RS_FOGENABLE
])
9810 if (state
->render_states
[WINED3D_RS_FOGTABLEMODE
] == WINED3D_FOG_NONE
)
9813 new_source
= FOGSOURCE_VS
;
9814 else if (state
->render_states
[WINED3D_RS_FOGVERTEXMODE
] == WINED3D_FOG_NONE
|| context
->stream_info
.position_transformed
)
9815 new_source
= FOGSOURCE_COORD
;
9817 new_source
= FOGSOURCE_FFP
;
9821 new_source
= FOGSOURCE_FFP
;
9824 if (new_source
!= context
->fog_source
|| fogstart
== fogend
)
9826 context
->fog_source
= new_source
;
9827 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_FOG
;
9831 static void glsl_fragment_pipe_vdecl(struct wined3d_context
*context
,
9832 const struct wined3d_state
*state
, DWORD state_id
)
9834 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9835 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
9836 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9838 if (!isStateDirty(context
, STATE_RENDER(WINED3D_RS_FOGENABLE
)))
9839 glsl_fragment_pipe_fog(context
, state
, state_id
);
9842 static void glsl_fragment_pipe_vs(struct wined3d_context
*context
,
9843 const struct wined3d_state
*state
, DWORD state_id
)
9845 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9846 if (context
->gl_info
->limits
.glsl_varyings
< wined3d_max_compat_varyings(context
->gl_info
))
9847 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9850 static void glsl_fragment_pipe_tex_transform(struct wined3d_context
*context
,
9851 const struct wined3d_state
*state
, DWORD state_id
)
9853 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9856 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context
*context
,
9857 const struct wined3d_state
*state
, DWORD state_id
)
9859 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_PS
;
9862 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context
*context
,
9863 const struct wined3d_state
*state
, DWORD state_id
)
9865 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9866 GLint func
= wined3d_gl_compare_func(state
->render_states
[WINED3D_RS_ALPHAFUNC
]);
9867 float ref
= state
->render_states
[WINED3D_RS_ALPHAREF
] / 255.0f
;
9871 gl_info
->gl_ops
.gl
.p_glAlphaFunc(func
, ref
);
9872 checkGLcall("glAlphaFunc");
9876 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context
*context
,
9877 const struct wined3d_state
*state
, DWORD state_id
)
9879 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9882 static void glsl_fragment_pipe_alpha_test(struct wined3d_context
*context
,
9883 const struct wined3d_state
*state
, DWORD state_id
)
9885 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
9887 if (state
->render_states
[WINED3D_RS_ALPHATESTENABLE
])
9889 gl_info
->gl_ops
.gl
.p_glEnable(GL_ALPHA_TEST
);
9890 checkGLcall("glEnable(GL_ALPHA_TEST)");
9894 gl_info
->gl_ops
.gl
.p_glDisable(GL_ALPHA_TEST
);
9895 checkGLcall("glDisable(GL_ALPHA_TEST)");
9899 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context
*context
,
9900 const struct wined3d_state
*state
, DWORD state_id
)
9902 context
->constant_update_mask
|= WINED3D_SHADER_CONST_PS_ALPHA_TEST
;
9905 static void glsl_fragment_pipe_color_key(struct wined3d_context
*context
,
9906 const struct wined3d_state
*state
, DWORD state_id
)
9908 context
->constant_update_mask
|= WINED3D_SHADER_CONST_FFP_COLOR_KEY
;
9911 static void glsl_fragment_pipe_shademode(struct wined3d_context
*context
,
9912 const struct wined3d_state
*state
, DWORD state_id
)
9914 context
->shader_update_mask
|= 1u << WINED3D_SHADER_TYPE_PIXEL
;
9917 static const struct StateEntryTemplate glsl_fragment_pipe_state_template
[] =
9919 {STATE_VDECL
, {STATE_VDECL
, glsl_fragment_pipe_vdecl
}, WINED3D_GL_EXT_NONE
},
9920 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX
), glsl_fragment_pipe_vs
}, WINED3D_GL_EXT_NONE
},
9921 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
9922 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9923 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9924 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9925 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9926 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9927 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9928 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9929 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9930 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9931 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9932 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9933 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9934 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9935 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9936 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9937 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9938 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9939 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9940 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9941 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9942 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9943 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9944 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9945 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9946 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9947 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9948 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9949 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9950 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9951 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9952 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9953 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9954 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9955 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9956 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9957 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9958 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9959 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9960 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9961 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9962 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9963 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9964 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9965 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9966 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9967 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9968 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9969 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9970 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9971 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9972 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9973 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9974 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9975 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9976 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9977 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9978 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9979 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9980 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9981 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9982 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9983 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9984 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9985 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9986 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9987 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9988 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9989 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9990 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9991 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9992 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9993 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
9994 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
9995 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), glsl_fragment_pipe_alpha_test_func
}, WINED3D_GL_LEGACY_CONTEXT
},
9996 {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
9997 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHAFUNC
), NULL
}, WINED3D_GL_LEGACY_CONTEXT
},
9998 {STATE_RENDER(WINED3D_RS_ALPHAREF
), {STATE_RENDER(WINED3D_RS_ALPHAREF
), glsl_fragment_pipe_core_alpha_test_ref
}, WINED3D_GL_EXT_NONE
},
9999 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_alpha_test
}, WINED3D_GL_LEGACY_CONTEXT
},
10000 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE
), glsl_fragment_pipe_core_alpha_test
}, WINED3D_GL_EXT_NONE
},
10001 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10002 {STATE_COLOR_KEY
, { STATE_COLOR_KEY
, glsl_fragment_pipe_color_key
}, WINED3D_GL_EXT_NONE
},
10003 {STATE_RENDER(WINED3D_RS_FOGENABLE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), glsl_fragment_pipe_fog
}, WINED3D_GL_EXT_NONE
},
10004 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10005 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE
), {STATE_RENDER(WINED3D_RS_FOGENABLE
), NULL
}, WINED3D_GL_EXT_NONE
},
10006 {STATE_RENDER(WINED3D_RS_FOGSTART
), {STATE_RENDER(WINED3D_RS_FOGSTART
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
10007 {STATE_RENDER(WINED3D_RS_FOGEND
), {STATE_RENDER(WINED3D_RS_FOGSTART
), NULL
}, WINED3D_GL_EXT_NONE
},
10008 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), state_srgbwrite
}, ARB_FRAMEBUFFER_SRGB
},
10009 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE
), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL
), NULL
}, WINED3D_GL_EXT_NONE
},
10010 {STATE_RENDER(WINED3D_RS_FOGCOLOR
), {STATE_RENDER(WINED3D_RS_FOGCOLOR
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
10011 {STATE_RENDER(WINED3D_RS_FOGDENSITY
), {STATE_RENDER(WINED3D_RS_FOGDENSITY
), glsl_fragment_pipe_fogparams
}, WINED3D_GL_EXT_NONE
},
10012 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, ARB_POINT_SPRITE
},
10013 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE
), glsl_fragment_pipe_shader
}, WINED3D_GL_VERSION_2_0
},
10014 {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
},
10015 {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
},
10016 {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
},
10017 {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
},
10018 {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
},
10019 {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
},
10020 {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
},
10021 {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
},
10022 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10023 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10024 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10025 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10026 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10027 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10028 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10029 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10030 {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), {STATE_RENDER(WINED3D_RS_SPECULARENABLE
), glsl_fragment_pipe_invalidate_constants
}, WINED3D_GL_EXT_NONE
},
10031 {STATE_POINT_ENABLE
, {STATE_POINT_ENABLE
, glsl_fragment_pipe_shader
}, WINED3D_GL_EXT_NONE
},
10032 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), state_shademode
}, WINED3D_GL_LEGACY_CONTEXT
},
10033 {STATE_RENDER(WINED3D_RS_SHADEMODE
), {STATE_RENDER(WINED3D_RS_SHADEMODE
), glsl_fragment_pipe_shademode
}, WINED3D_GL_EXT_NONE
},
10034 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE
},
10037 static BOOL
glsl_fragment_pipe_alloc_context_data(struct wined3d_context
*context
)
10042 static void glsl_fragment_pipe_free_context_data(struct wined3d_context
*context
)
10046 const struct fragment_pipeline glsl_fragment_pipe
=
10048 glsl_fragment_pipe_enable
,
10049 glsl_fragment_pipe_get_caps
,
10050 glsl_fragment_pipe_get_emul_mask
,
10051 glsl_fragment_pipe_alloc
,
10052 glsl_fragment_pipe_free
,
10053 glsl_fragment_pipe_alloc_context_data
,
10054 glsl_fragment_pipe_free_context_data
,
10055 shader_glsl_color_fixup_supported
,
10056 glsl_fragment_pipe_state_template
,