wined3d: Move IUnknown functions to IWineD3DBaseShader.
[wine.git] / dlls / wined3d / pixelshader.c
blob5654c46c07d99e994cdd6703633a30436eaa4bfd
1 /*
2 * shaders implementation
4 * Copyright 2002-2003 Jason Edmeades
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2006 Ivan Gyurdiev
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
27 #include <math.h>
28 #include <stdio.h>
30 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
34 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *) This->baseShader.device)->adapter->gl_info
36 #if 0 /* Must not be 1 in cvs version */
37 # define PSTRACE(A) TRACE A
38 # define TRACE_VSVECTOR(name) TRACE( #name "=(%f, %f, %f, %f)\n", name.x, name.y, name.z, name.w)
39 #else
40 # define PSTRACE(A)
41 # define TRACE_VSVECTOR(name)
42 #endif
44 #define GLNAME_REQUIRE_GLSL ((const char *)1)
46 static HRESULT WINAPI IWineD3DPixelShaderImpl_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, LPVOID *ppobj) {
47 return IWineD3DBaseShaderImpl_QueryInterface((IWineD3DBaseShader *) iface, riid, ppobj);
50 static ULONG WINAPI IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader *iface) {
51 return IWineD3DBaseShaderImpl_AddRef((IWineD3DBaseShader *) iface);
54 static void destroy_glsl_pshader(IWineD3DPixelShaderImpl *This) {
55 struct list *linked_programs = &This->baseShader.linked_programs;
57 TRACE("Deleting linked programs\n");
58 if (linked_programs->next) {
59 struct glsl_shader_prog_link *entry, *entry2;
60 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
61 delete_glsl_program_entry(This->baseShader.device, entry);
65 TRACE("Deleting shader object %u\n", This->baseShader.prgId);
66 GL_EXTCALL(glDeleteObjectARB(This->baseShader.prgId));
67 checkGLcall("glDeleteObjectARB");
70 static ULONG WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
71 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
72 ULONG ref;
73 ref = IWineD3DBaseShaderImpl_Release((IWineD3DBaseShader *) iface);
75 if(ref == 0) {
76 if (This->baseShader.shader_mode == SHADER_GLSL && This->baseShader.prgId != 0) {
77 destroy_glsl_pshader(This);
79 HeapFree(GetProcessHeap(), 0, This);
82 return ref;
85 /* *******************************************
86 IWineD3DPixelShader IWineD3DPixelShader parts follow
87 ******************************************* */
89 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
90 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
92 *parent = This->parent;
93 IUnknown_AddRef(*parent);
94 TRACE("(%p) : returning %p\n", This, *parent);
95 return WINED3D_OK;
98 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
99 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
100 IWineD3DDevice_AddRef(This->baseShader.device);
101 *pDevice = This->baseShader.device;
102 TRACE("(%p) returning %p\n", This, *pDevice);
103 return WINED3D_OK;
107 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
108 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
109 TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
111 if (NULL == pData) {
112 *pSizeOfData = This->baseShader.functionLength;
113 return WINED3D_OK;
115 if (*pSizeOfData < This->baseShader.functionLength) {
116 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
117 * than the required size we should write the required size and
118 * return D3DERR_MOREDATA. That's not actually true. */
119 return WINED3DERR_INVALIDCALL;
121 if (NULL == This->baseShader.function) { /* no function defined */
122 TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
123 (*(DWORD **) pData) = NULL;
124 } else {
125 if (This->baseShader.functionLength == 0) {
128 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
129 memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
131 return WINED3D_OK;
134 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[] = {
135 /* Arithmethic */
136 {WINED3DSIO_NOP, "nop", "NOP", 0, 0, pshader_hw_map2gl, NULL, 0, 0},
137 {WINED3DSIO_MOV, "mov", "MOV", 1, 2, pshader_hw_map2gl, shader_glsl_mov, 0, 0},
138 {WINED3DSIO_ADD, "add", "ADD", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
139 {WINED3DSIO_SUB, "sub", "SUB", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
140 {WINED3DSIO_MAD, "mad", "MAD", 1, 4, pshader_hw_map2gl, shader_glsl_mad, 0, 0},
141 {WINED3DSIO_MUL, "mul", "MUL", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
142 {WINED3DSIO_RCP, "rcp", "RCP", 1, 2, pshader_hw_map2gl, shader_glsl_rcp, 0, 0},
143 {WINED3DSIO_RSQ, "rsq", "RSQ", 1, 2, pshader_hw_map2gl, shader_glsl_rsq, 0, 0},
144 {WINED3DSIO_DP3, "dp3", "DP3", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
145 {WINED3DSIO_DP4, "dp4", "DP4", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
146 {WINED3DSIO_MIN, "min", "MIN", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
147 {WINED3DSIO_MAX, "max", "MAX", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
148 {WINED3DSIO_SLT, "slt", "SLT", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
149 {WINED3DSIO_SGE, "sge", "SGE", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
150 {WINED3DSIO_ABS, "abs", "ABS", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
151 {WINED3DSIO_EXP, "exp", "EX2", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
152 {WINED3DSIO_LOG, "log", "LG2", 1, 2, pshader_hw_map2gl, shader_glsl_log, 0, 0},
153 {WINED3DSIO_EXPP, "expp", "EXP", 1, 2, pshader_hw_map2gl, shader_glsl_expp, 0, 0},
154 {WINED3DSIO_LOGP, "logp", "LOG", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
155 {WINED3DSIO_DST, "dst", "DST", 1, 3, pshader_hw_map2gl, shader_glsl_dst, 0, 0},
156 {WINED3DSIO_LRP, "lrp", "LRP", 1, 4, pshader_hw_map2gl, shader_glsl_lrp, 0, 0},
157 {WINED3DSIO_FRC, "frc", "FRC", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
158 {WINED3DSIO_CND, "cnd", NULL, 1, 4, pshader_hw_cnd, shader_glsl_cnd, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,4)},
159 {WINED3DSIO_CMP, "cmp", NULL, 1, 4, pshader_hw_cmp, shader_glsl_cmp, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(3,0)},
160 {WINED3DSIO_POW, "pow", "POW", 1, 3, pshader_hw_map2gl, shader_glsl_pow, 0, 0},
161 {WINED3DSIO_CRS, "crs", "XPD", 1, 3, pshader_hw_map2gl, shader_glsl_cross, 0, 0},
162 {WINED3DSIO_NRM, "nrm", NULL, 1, 2, shader_hw_nrm, shader_glsl_map2gl, 0, 0},
163 {WINED3DSIO_SINCOS, "sincos", NULL, 1, 4, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(2,0), WINED3DPS_VERSION(2,1)},
164 {WINED3DSIO_SINCOS, "sincos", "SCS", 1, 2, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(3,0), -1},
165 {WINED3DSIO_DP2ADD, "dp2add", NULL, 1, 4, pshader_hw_dp2add, pshader_glsl_dp2add, WINED3DPS_VERSION(2,0), -1},
166 /* Matrix */
167 {WINED3DSIO_M4x4, "m4x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
168 {WINED3DSIO_M4x3, "m4x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
169 {WINED3DSIO_M3x4, "m3x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
170 {WINED3DSIO_M3x3, "m3x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
171 {WINED3DSIO_M3x2, "m3x2", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
172 /* Register declarations */
173 {WINED3DSIO_DCL, "dcl", NULL, 0, 2, NULL, NULL, 0, 0},
174 /* Flow control - requires GLSL or software shaders */
175 {WINED3DSIO_REP , "rep", NULL, 0, 1, NULL, shader_glsl_rep, WINED3DPS_VERSION(2,1), -1},
176 {WINED3DSIO_ENDREP, "endrep", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
177 {WINED3DSIO_IF, "if", NULL, 0, 1, NULL, shader_glsl_if, WINED3DPS_VERSION(2,1), -1},
178 {WINED3DSIO_IFC, "ifc", NULL, 0, 2, NULL, shader_glsl_ifc, WINED3DPS_VERSION(2,1), -1},
179 {WINED3DSIO_ELSE, "else", NULL, 0, 0, NULL, shader_glsl_else, WINED3DPS_VERSION(2,1), -1},
180 {WINED3DSIO_ENDIF, "endif", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
181 {WINED3DSIO_BREAK, "break", NULL, 0, 0, NULL, shader_glsl_break, WINED3DPS_VERSION(2,1), -1},
182 {WINED3DSIO_BREAKC, "breakc", NULL, 0, 2, NULL, shader_glsl_breakc, WINED3DPS_VERSION(2,1), -1},
183 {WINED3DSIO_BREAKP, "breakp", GLNAME_REQUIRE_GLSL, 0, 1, NULL, NULL, 0, 0},
184 {WINED3DSIO_CALL, "call", NULL, 0, 1, NULL, shader_glsl_call, WINED3DPS_VERSION(2,1), -1},
185 {WINED3DSIO_CALLNZ, "callnz", NULL, 0, 2, NULL, shader_glsl_callnz, WINED3DPS_VERSION(2,1), -1},
186 {WINED3DSIO_LOOP, "loop", NULL, 0, 2, NULL, shader_glsl_loop, WINED3DPS_VERSION(3,0), -1},
187 {WINED3DSIO_RET, "ret", NULL, 0, 0, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
188 {WINED3DSIO_ENDLOOP, "endloop", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(3,0), -1},
189 {WINED3DSIO_LABEL, "label", NULL, 0, 1, NULL, shader_glsl_label, WINED3DPS_VERSION(2,1), -1},
190 /* Constant definitions */
191 {WINED3DSIO_DEF, "def", "undefined", 1, 5, NULL, NULL, 0, 0},
192 {WINED3DSIO_DEFB, "defb", GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, 0, 0},
193 {WINED3DSIO_DEFI, "defi", GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, 0, 0},
194 /* Texture */
195 {WINED3DSIO_TEXCOORD, "texcoord", "undefined", 1, 1, pshader_hw_texcoord, pshader_glsl_texcoord, 0, WINED3DPS_VERSION(1,3)},
196 {WINED3DSIO_TEXCOORD, "texcrd", "undefined", 1, 2, pshader_hw_texcoord, pshader_glsl_texcoord, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
197 {WINED3DSIO_TEXKILL, "texkill", "KIL", 1, 1, pshader_hw_texkill, pshader_glsl_texkill, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(3,0)},
198 {WINED3DSIO_TEX, "tex", "undefined", 1, 1, pshader_hw_tex, pshader_glsl_tex, 0, WINED3DPS_VERSION(1,3)},
199 {WINED3DSIO_TEX, "texld", "undefined", 1, 2, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
200 {WINED3DSIO_TEX, "texld", "undefined", 1, 3, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(2,0), -1},
201 {WINED3DSIO_TEXBEM, "texbem", "undefined", 1, 2, pshader_hw_texbem, pshader_glsl_texbem, 0, WINED3DPS_VERSION(1,3)},
202 {WINED3DSIO_TEXBEML, "texbeml", GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texbem, pshader_glsl_texbem, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
203 {WINED3DSIO_TEXREG2AR,"texreg2ar","undefined", 1, 2, pshader_hw_texreg2ar, pshader_glsl_texreg2ar, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
204 {WINED3DSIO_TEXREG2GB,"texreg2gb","undefined", 1, 2, pshader_hw_texreg2gb, pshader_glsl_texreg2gb, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
205 {WINED3DSIO_TEXREG2RGB, "texreg2rgb", "undefined", 1, 2, pshader_hw_texreg2rgb, pshader_glsl_texreg2rgb, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
206 {WINED3DSIO_TEXM3x2PAD, "texm3x2pad", "undefined", 1, 2, pshader_hw_texm3x2pad, pshader_glsl_texm3x2pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
207 {WINED3DSIO_TEXM3x2TEX, "texm3x2tex", "undefined", 1, 2, pshader_hw_texm3x2tex, pshader_glsl_texm3x2tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
208 {WINED3DSIO_TEXM3x3PAD, "texm3x3pad", "undefined", 1, 2, pshader_hw_texm3x3pad, pshader_glsl_texm3x3pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
209 {WINED3DSIO_TEXM3x3DIFF, "texm3x3diff", GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, WINED3DPS_VERSION(0,0), WINED3DPS_VERSION(0,0)},
210 {WINED3DSIO_TEXM3x3SPEC, "texm3x3spec", "undefined", 1, 3, pshader_hw_texm3x3spec, pshader_glsl_texm3x3spec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
211 {WINED3DSIO_TEXM3x3VSPEC, "texm3x3vspec", "undefined", 1, 2, pshader_hw_texm3x3vspec, pshader_glsl_texm3x3vspec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
212 {WINED3DSIO_TEXM3x3TEX, "texm3x3tex", "undefined", 1, 2, pshader_hw_texm3x3tex, pshader_glsl_texm3x3tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
213 {WINED3DSIO_TEXDP3TEX, "texdp3tex", NULL, 1, 2, pshader_hw_texdp3tex, pshader_glsl_texdp3tex, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
214 {WINED3DSIO_TEXM3x2DEPTH, "texm3x2depth", GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texm3x2depth, pshader_glsl_texm3x2depth, WINED3DPS_VERSION(1,3), WINED3DPS_VERSION(1,3)},
215 {WINED3DSIO_TEXDP3, "texdp3", NULL, 1, 2, pshader_hw_texdp3, pshader_glsl_texdp3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
216 {WINED3DSIO_TEXM3x3, "texm3x3", NULL, 1, 2, pshader_hw_texm3x3, pshader_glsl_texm3x3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
217 {WINED3DSIO_TEXDEPTH, "texdepth", NULL, 1, 1, pshader_hw_texdepth, pshader_glsl_texdepth, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
218 {WINED3DSIO_BEM, "bem", "undefined", 1, 3, pshader_hw_bem, pshader_glsl_bem, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
219 {WINED3DSIO_DSX, "dsx", NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
220 {WINED3DSIO_DSY, "dsy", NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
221 {WINED3DSIO_TEXLDD, "texldd", GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
222 {WINED3DSIO_SETP, "setp", GLNAME_REQUIRE_GLSL, 1, 3, NULL, NULL, 0, 0},
223 {WINED3DSIO_TEXLDL, "texldl", NULL, 1, 3, NULL, shader_glsl_texldl, WINED3DPS_VERSION(3,0), -1},
224 {WINED3DSIO_PHASE, "phase", GLNAME_REQUIRE_GLSL, 0, 0, NULL, NULL, 0, 0},
225 {0, NULL, NULL, 0, 0, NULL, NULL, 0, 0}
228 static void pshader_set_limits(
229 IWineD3DPixelShaderImpl *This) {
231 This->baseShader.limits.attributes = 0;
232 This->baseShader.limits.address = 0;
233 This->baseShader.limits.packed_output = 0;
235 switch (This->baseShader.hex_version) {
236 case WINED3DPS_VERSION(1,0):
237 case WINED3DPS_VERSION(1,1):
238 case WINED3DPS_VERSION(1,2):
239 case WINED3DPS_VERSION(1,3):
240 This->baseShader.limits.temporary = 2;
241 This->baseShader.limits.constant_float = 8;
242 This->baseShader.limits.constant_int = 0;
243 This->baseShader.limits.constant_bool = 0;
244 This->baseShader.limits.texcoord = 4;
245 This->baseShader.limits.sampler = 4;
246 This->baseShader.limits.packed_input = 0;
247 This->baseShader.limits.label = 0;
248 break;
250 case WINED3DPS_VERSION(1,4):
251 This->baseShader.limits.temporary = 6;
252 This->baseShader.limits.constant_float = 8;
253 This->baseShader.limits.constant_int = 0;
254 This->baseShader.limits.constant_bool = 0;
255 This->baseShader.limits.texcoord = 6;
256 This->baseShader.limits.sampler = 6;
257 This->baseShader.limits.packed_input = 0;
258 This->baseShader.limits.label = 0;
259 break;
261 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
262 case WINED3DPS_VERSION(2,0):
263 This->baseShader.limits.temporary = 32;
264 This->baseShader.limits.constant_float = 32;
265 This->baseShader.limits.constant_int = 16;
266 This->baseShader.limits.constant_bool = 16;
267 This->baseShader.limits.texcoord = 8;
268 This->baseShader.limits.sampler = 16;
269 This->baseShader.limits.packed_input = 0;
270 break;
272 case WINED3DPS_VERSION(2,1):
273 This->baseShader.limits.temporary = 32;
274 This->baseShader.limits.constant_float = 32;
275 This->baseShader.limits.constant_int = 16;
276 This->baseShader.limits.constant_bool = 16;
277 This->baseShader.limits.texcoord = 8;
278 This->baseShader.limits.sampler = 16;
279 This->baseShader.limits.packed_input = 0;
280 This->baseShader.limits.label = 16;
281 break;
283 case WINED3DPS_VERSION(3,0):
284 This->baseShader.limits.temporary = 32;
285 This->baseShader.limits.constant_float = 224;
286 This->baseShader.limits.constant_int = 16;
287 This->baseShader.limits.constant_bool = 16;
288 This->baseShader.limits.texcoord = 0;
289 This->baseShader.limits.sampler = 16;
290 This->baseShader.limits.packed_input = 12;
291 This->baseShader.limits.label = 16; /* FIXME: 2048 */
292 break;
294 default: This->baseShader.limits.temporary = 32;
295 This->baseShader.limits.constant_float = 32;
296 This->baseShader.limits.constant_int = 16;
297 This->baseShader.limits.constant_bool = 16;
298 This->baseShader.limits.texcoord = 8;
299 This->baseShader.limits.sampler = 16;
300 This->baseShader.limits.packed_input = 0;
301 This->baseShader.limits.label = 0;
302 FIXME("Unrecognized pixel shader version %#x\n",
303 This->baseShader.hex_version);
307 /** Generate a pixel shader string using either GL_FRAGMENT_PROGRAM_ARB
308 or GLSL and send it to the card */
309 static inline VOID IWineD3DPixelShaderImpl_GenerateShader(
310 IWineD3DPixelShader *iface,
311 shader_reg_maps* reg_maps,
312 CONST DWORD *pFunction) {
314 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
315 SHADER_BUFFER buffer;
317 #if 0 /* FIXME: Use the buffer that is held by the device, this is ok since fixups will be skipped for software shaders
318 it also requires entering a critical section but cuts down the runtime footprint of wined3d and any memory fragmentation that may occur... */
319 if (This->device->fixupVertexBufferSize < SHADER_PGMSIZE) {
320 HeapFree(GetProcessHeap(), 0, This->fixupVertexBuffer);
321 This->fixupVertexBuffer = HeapAlloc(GetProcessHeap() , 0, SHADER_PGMSIZE);
322 This->fixupVertexBufferSize = PGMSIZE;
323 This->fixupVertexBuffer[0] = 0;
325 buffer.buffer = This->device->fixupVertexBuffer;
326 #else
327 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
328 #endif
329 buffer.bsize = 0;
330 buffer.lineNo = 0;
331 buffer.newline = TRUE;
333 if (This->baseShader.shader_mode == SHADER_GLSL) {
335 /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
336 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
338 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
339 shader_addline(&buffer, "#extension GL_ARB_draw_buffers : enable\n");
342 /* Base Declarations */
343 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
345 /* Pack 3.0 inputs */
346 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
348 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
349 This->vertexprocessing = pretransformed;
350 pshader_glsl_input_pack(&buffer, This->semantics_in, iface);
351 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
352 This->vertexprocessing = fixedfunction;
353 pshader_glsl_input_pack(&buffer, This->semantics_in, iface);
354 } else {
355 This->vertexprocessing = vertexshader;
359 /* Base Shader Body */
360 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
362 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
363 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
364 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
365 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
366 shader_addline(&buffer, "gl_FragData[0] = R0;\n");
367 else
368 shader_addline(&buffer, "gl_FragColor = R0;\n");
371 /* Pixel shader < 3.0 do not replace the fog stage.
372 * This implements linear fog computation and blending.
373 * TODO: non linear fog
374 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
375 * -1/(e-s) and e/(e-s) respectively.
377 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
378 shader_addline(&buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
379 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
380 shader_addline(&buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
381 else
382 shader_addline(&buffer, "gl_FragColor.xyz = mix(gl_Fog.color.xyz, gl_FragColor.xyz, Fog);\n");
384 if(This->srgb_enabled) {
385 const char *fragcolor;
387 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
388 fragcolor = "gl_FragData[0]";
389 } else {
390 fragcolor = "gl_FragColor";
392 shader_addline(&buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
393 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
394 srgb_sub_high, srgb_sub_high, srgb_sub_high);
395 shader_addline(&buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
396 shader_addline(&buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
397 shader_addline(&buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
398 shader_addline(&buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
399 shader_addline(&buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
402 shader_addline(&buffer, "}\n");
404 TRACE("Compiling shader object %u\n", shader_obj);
405 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer.buffer, NULL));
406 GL_EXTCALL(glCompileShaderARB(shader_obj));
407 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
409 /* Store the shader object */
410 This->baseShader.prgId = shader_obj;
412 } else if (This->baseShader.shader_mode == SHADER_ARB) {
413 /* Create the hw ARB shader */
414 shader_addline(&buffer, "!!ARBfp1.0\n");
416 shader_addline(&buffer, "TEMP TMP;\n"); /* Used in matrix ops */
417 shader_addline(&buffer, "TEMP TMP2;\n"); /* Used in matrix ops */
418 shader_addline(&buffer, "TEMP TA;\n"); /* Used for modifiers */
419 shader_addline(&buffer, "TEMP TB;\n"); /* Used for modifiers */
420 shader_addline(&buffer, "TEMP TC;\n"); /* Used for modifiers */
421 shader_addline(&buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
422 shader_addline(&buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
423 shader_addline(&buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
425 /* Base Declarations */
426 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
428 /* We need two variables for fog blending */
429 shader_addline(&buffer, "TEMP TMP_FOG;\n");
430 if (This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
431 shader_addline(&buffer, "TEMP TMP_COLOR;\n");
434 /* Base Shader Body */
435 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
437 /* calculate fog and blend it
438 * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
439 * -1/(e-s) and e/(e-s) respectively.
441 shader_addline(&buffer, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
443 if(This->srgb_enabled) {
444 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
445 shader_addline(&buffer, "LRP TMP_COLOR.rgb, TMP_FOG.x, R0, state.fog.color;\n");
446 shader_addline(&buffer, "MOV result.color.a, R0.a;\n");
447 } else {
448 shader_addline(&buffer, "LRP TMP_COLOR.rgb, TMP_FOG.x, TMP_COLOR, state.fog.color;\n");
449 shader_addline(&buffer, "MOV result.color.a, TMP_COLOR.a;\n");
451 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
453 /* Calculate the > 0.0031308 case */
454 shader_addline(&buffer, "POW TMP.x, TMP_COLOR.x, srgb_pow.x;\n");
455 shader_addline(&buffer, "POW TMP.y, TMP_COLOR.y, srgb_pow.y;\n");
456 shader_addline(&buffer, "POW TMP.z, TMP_COLOR.z, srgb_pow.z;\n");
457 shader_addline(&buffer, "MUL TMP, TMP, srgb_mul_hi;\n");
458 shader_addline(&buffer, "SUB TMP, TMP, srgb_sub_hi;\n");
459 /* Calculate the < case */
460 shader_addline(&buffer, "MUL TMP2, srgb_mul_low, TMP_COLOR;\n");
461 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
462 shader_addline(&buffer, "SLT TA, srgb_comparison, TMP_COLOR;\n");
463 shader_addline(&buffer, "SGE TB, srgb_comparison, TMP_COLOR;\n");
464 /* Store the components > 0.0031308 in the destination */
465 shader_addline(&buffer, "MUL TMP_COLOR, TMP, TA;\n");
466 /* Add the components that are < 0.0031308 */
467 shader_addline(&buffer, "MAD result.color.xyz, TMP2, TB, TMP_COLOR;\n");
468 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
469 } else {
470 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
471 shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, R0, state.fog.color;\n");
472 shader_addline(&buffer, "MOV result.color.a, R0.a;\n");
473 } else {
474 shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, TMP_COLOR, state.fog.color;\n");
475 shader_addline(&buffer, "MOV result.color.a, TMP_COLOR.a;\n");
479 shader_addline(&buffer, "END\n");
481 /* TODO: change to resource.glObjectHandle or something like that */
482 GL_EXTCALL(glGenProgramsARB(1, &This->baseShader.prgId));
484 TRACE("Creating a hw pixel shader, prg=%d\n", This->baseShader.prgId);
485 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, This->baseShader.prgId));
487 TRACE("Created hw pixel shader, prg=%d\n", This->baseShader.prgId);
488 /* Create the program and check for errors */
489 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
490 buffer.bsize, buffer.buffer));
492 if (glGetError() == GL_INVALID_OPERATION) {
493 GLint errPos;
494 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
495 FIXME("HW PixelShader Error at position %d: %s\n",
496 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
497 This->baseShader.prgId = -1;
501 This->needsbumpmat = reg_maps->bumpmat;
503 #if 1 /* if were using the data buffer of device then we don't need to free it */
504 HeapFree(GetProcessHeap(), 0, buffer.buffer);
505 #endif
508 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
510 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
511 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
513 TRACE("(%p) : pFunction %p\n", iface, pFunction);
515 /* First pass: trace shader */
516 shader_trace_init((IWineD3DBaseShader*) This, pFunction);
517 pshader_set_limits(This);
519 /* Initialize immediate constant lists */
520 list_init(&This->baseShader.constantsF);
521 list_init(&This->baseShader.constantsB);
522 list_init(&This->baseShader.constantsI);
524 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) > 1) {
525 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
526 HRESULT hr;
527 unsigned int i, j, highest_reg_used = 0, num_regs_used = 0;
529 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
530 memset(reg_maps, 0, sizeof(shader_reg_maps));
531 hr = shader_get_registers_used((IWineD3DBaseShader*) This, reg_maps,
532 This->semantics_in, NULL, pFunction, NULL);
533 if (FAILED(hr)) return hr;
534 /* FIXME: validate reg_maps against OpenGL */
536 for(i = 0; i < MAX_REG_INPUT; i++) {
537 if(This->input_reg_used[i]) {
538 num_regs_used++;
539 highest_reg_used = i;
543 /* Don't do any register mapping magic if it is not needed, or if we can't
544 * achive anything anyway
546 if(highest_reg_used < (GL_LIMITS(glsl_varyings) / 4) ||
547 num_regs_used > (GL_LIMITS(glsl_varyings) / 4) ) {
548 if(num_regs_used > (GL_LIMITS(glsl_varyings) / 4)) {
549 /* This happens with relative addressing. The input mapper function
550 * warns about this if the higher registers are declared too, so
551 * don't write a FIXME here
553 WARN("More varying registers used than supported\n");
556 for(i = 0; i < MAX_REG_INPUT; i++) {
557 This->input_reg_map[i] = i;
559 } else {
560 j = 0;
561 for(i = 0; i < MAX_REG_INPUT; i++) {
562 if(This->input_reg_used[i]) {
563 This->input_reg_map[i] = j;
564 j++;
565 } else {
566 This->input_reg_map[i] = -1;
571 This->baseShader.load_local_constsF = FALSE;
573 This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
575 TRACE("(%p) : Copying the function\n", This);
576 if (NULL != pFunction) {
577 void *function;
579 function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->baseShader.functionLength);
580 if (!function) return E_OUTOFMEMORY;
581 memcpy(function, pFunction, This->baseShader.functionLength);
582 This->baseShader.function = function;
583 } else {
584 This->baseShader.function = NULL;
587 return WINED3D_OK;
590 static HRESULT WINAPI IWineD3DPixelShaderImpl_CompileShader(IWineD3DPixelShader *iface) {
592 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
593 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
594 CONST DWORD *function = This->baseShader.function;
595 UINT i, sampler;
596 IWineD3DBaseTextureImpl *texture;
598 TRACE("(%p) : function %p\n", iface, function);
600 /* We're already compiled, but check if any of the hardcoded stateblock assumptions
601 * changed.
603 if (This->baseShader.is_compiled) {
604 char srgbenabled = deviceImpl->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
605 for(i = 0; i < This->baseShader.num_sampled_samplers; i++) {
606 sampler = This->baseShader.sampled_samplers[i];
607 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler];
608 if(texture && texture->baseTexture.shader_conversion_group != This->baseShader.sampled_format[sampler]) {
609 WARN("Recompiling shader %p due to format change on sampler %d\n", This, sampler);
610 WARN("Old format group %s, new is %s\n",
611 debug_d3dformat(This->baseShader.sampled_format[sampler]),
612 debug_d3dformat(texture->baseTexture.shader_conversion_group));
613 goto recompile;
617 /* TODO: Check projected textures */
618 /* TODO: Check texture types(2D, Cube, 3D) */
620 if(srgbenabled != This->srgb_enabled && This->srgb_mode_hardcoded) {
621 WARN("Recompiling shader because srgb correction is different and hardcoded\n");
622 goto recompile;
624 if(This->baseShader.reg_maps.vpos && !This->vpos_uniform) {
625 if(This->render_offscreen != deviceImpl->render_offscreen ||
626 This->height != ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height) {
627 WARN("Recompiling shader because vpos is used, hard compiled and changed\n");
628 goto recompile;
631 if(This->baseShader.reg_maps.usesdsy && !This->vpos_uniform) {
632 if(This->render_offscreen ? 0 : 1 != deviceImpl->render_offscreen ? 0 : 1) {
633 WARN("Recompiling shader because dsy is used, hard compiled and render_offscreen changed\n");
634 goto recompile;
637 if(This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
638 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed &&
639 This->vertexprocessing != pretransformed) {
640 WARN("Recompiling shader because pretransformed vertices are provided, which wasn't the case before\n");
641 goto recompile;
642 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device) &&
643 This->vertexprocessing != fixedfunction) {
644 WARN("Recompiling shader because fixed function vp is in use, which wasn't the case before\n");
645 goto recompile;
646 } else if(This->vertexprocessing != vertexshader) {
647 WARN("Recompiling shader because vertex shaders are in use, which wasn't the case before\n");
648 goto recompile;
652 return WINED3D_OK;
654 recompile:
655 if(This->baseShader.recompile_count > 50) {
656 FIXME("Shader %p recompiled more than 50 times\n", This);
657 } else {
658 This->baseShader.recompile_count++;
661 if (This->baseShader.shader_mode == SHADER_GLSL && This->baseShader.prgId != 0) {
662 destroy_glsl_pshader(This);
666 /* We don't need to compile */
667 if (!function) {
668 This->baseShader.is_compiled = TRUE;
669 return WINED3D_OK;
672 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1) {
673 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
674 HRESULT hr;
676 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
677 memset(reg_maps, 0, sizeof(shader_reg_maps));
678 hr = shader_get_registers_used((IWineD3DBaseShader*)This, reg_maps,
679 This->semantics_in, NULL, This->baseShader.function, deviceImpl->stateBlock);
680 if (FAILED(hr)) return hr;
681 /* FIXME: validate reg_maps against OpenGL */
684 /* Reset fields tracking stateblock values being hardcoded in the shader */
685 This->baseShader.num_sampled_samplers = 0;
687 /* Generate the HW shader */
688 TRACE("(%p) : Generating hardware program\n", This);
689 IWineD3DPixelShaderImpl_GenerateShader(iface, &This->baseShader.reg_maps, function);
691 This->baseShader.is_compiled = TRUE;
693 return WINED3D_OK;
696 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
698 /*** IUnknown methods ***/
699 IWineD3DPixelShaderImpl_QueryInterface,
700 IWineD3DPixelShaderImpl_AddRef,
701 IWineD3DPixelShaderImpl_Release,
702 /*** IWineD3DBase methods ***/
703 IWineD3DPixelShaderImpl_GetParent,
704 /*** IWineD3DBaseShader methods ***/
705 IWineD3DPixelShaderImpl_SetFunction,
706 IWineD3DPixelShaderImpl_CompileShader,
707 /*** IWineD3DPixelShader methods ***/
708 IWineD3DPixelShaderImpl_GetDevice,
709 IWineD3DPixelShaderImpl_GetFunction