push(c) 7dcddf6204ed5518706a76fe66fd836d81e70dd4
[wine/hacks.git] / dlls / wined3d / pixelshader.c
blob8b2977bec4d0d6336b52e75fb4a80ff9925dc44b
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)
45 /* *******************************************
46 IWineD3DPixelShader IUnknown parts follow
47 ******************************************* */
48 static HRESULT WINAPI IWineD3DPixelShaderImpl_QueryInterface(IWineD3DPixelShader *iface, REFIID riid, LPVOID *ppobj)
50 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
51 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
52 if (IsEqualGUID(riid, &IID_IUnknown)
53 || IsEqualGUID(riid, &IID_IWineD3DBase)
54 || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
55 || IsEqualGUID(riid, &IID_IWineD3DPixelShader)) {
56 IUnknown_AddRef(iface);
57 *ppobj = This;
58 return S_OK;
60 *ppobj = NULL;
61 return E_NOINTERFACE;
64 static ULONG WINAPI IWineD3DPixelShaderImpl_AddRef(IWineD3DPixelShader *iface) {
65 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
66 TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
67 return InterlockedIncrement(&This->ref);
70 static void destroy_glsl_pshader(IWineD3DPixelShaderImpl *This) {
71 struct list *linked_programs = &This->baseShader.linked_programs;
73 TRACE("Deleting linked programs\n");
74 if (linked_programs->next) {
75 struct glsl_shader_prog_link *entry, *entry2;
76 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs, struct glsl_shader_prog_link, pshader_entry) {
77 delete_glsl_program_entry(This->baseShader.device, entry);
81 TRACE("Deleting shader object %u\n", This->baseShader.prgId);
82 GL_EXTCALL(glDeleteObjectARB(This->baseShader.prgId));
83 checkGLcall("glDeleteObjectARB");
86 static ULONG WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
87 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
88 ULONG ref;
89 TRACE("(%p) : Releasing from %d\n", This, This->ref);
90 ref = InterlockedDecrement(&This->ref);
91 if (ref == 0) {
92 if (This->baseShader.shader_mode == SHADER_GLSL && This->baseShader.prgId != 0) {
93 destroy_glsl_pshader(This);
95 shader_delete_constant_list(&This->baseShader.constantsF);
96 shader_delete_constant_list(&This->baseShader.constantsB);
97 shader_delete_constant_list(&This->baseShader.constantsI);
98 HeapFree(GetProcessHeap(), 0, This);
100 return ref;
103 /* *******************************************
104 IWineD3DPixelShader IWineD3DPixelShader parts follow
105 ******************************************* */
107 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
108 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
110 *parent = This->parent;
111 IUnknown_AddRef(*parent);
112 TRACE("(%p) : returning %p\n", This, *parent);
113 return WINED3D_OK;
116 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
117 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
118 IWineD3DDevice_AddRef(This->baseShader.device);
119 *pDevice = This->baseShader.device;
120 TRACE("(%p) returning %p\n", This, *pDevice);
121 return WINED3D_OK;
125 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
126 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
127 TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
129 if (NULL == pData) {
130 *pSizeOfData = This->baseShader.functionLength;
131 return WINED3D_OK;
133 if (*pSizeOfData < This->baseShader.functionLength) {
134 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
135 * than the required size we should write the required size and
136 * return D3DERR_MOREDATA. That's not actually true. */
137 return WINED3DERR_INVALIDCALL;
139 if (NULL == This->baseShader.function) { /* no function defined */
140 TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
141 (*(DWORD **) pData) = NULL;
142 } else {
143 if (This->baseShader.functionLength == 0) {
146 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
147 memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
149 return WINED3D_OK;
152 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[] = {
153 /* Arithmethic */
154 {WINED3DSIO_NOP, "nop", "NOP", 0, 0, pshader_hw_map2gl, NULL, 0, 0},
155 {WINED3DSIO_MOV, "mov", "MOV", 1, 2, pshader_hw_map2gl, shader_glsl_mov, 0, 0},
156 {WINED3DSIO_ADD, "add", "ADD", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
157 {WINED3DSIO_SUB, "sub", "SUB", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
158 {WINED3DSIO_MAD, "mad", "MAD", 1, 4, pshader_hw_map2gl, shader_glsl_mad, 0, 0},
159 {WINED3DSIO_MUL, "mul", "MUL", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
160 {WINED3DSIO_RCP, "rcp", "RCP", 1, 2, pshader_hw_map2gl, shader_glsl_rcp, 0, 0},
161 {WINED3DSIO_RSQ, "rsq", "RSQ", 1, 2, pshader_hw_map2gl, shader_glsl_rsq, 0, 0},
162 {WINED3DSIO_DP3, "dp3", "DP3", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
163 {WINED3DSIO_DP4, "dp4", "DP4", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
164 {WINED3DSIO_MIN, "min", "MIN", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
165 {WINED3DSIO_MAX, "max", "MAX", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
166 {WINED3DSIO_SLT, "slt", "SLT", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
167 {WINED3DSIO_SGE, "sge", "SGE", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
168 {WINED3DSIO_ABS, "abs", "ABS", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
169 {WINED3DSIO_EXP, "exp", "EX2", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
170 {WINED3DSIO_LOG, "log", "LG2", 1, 2, pshader_hw_map2gl, shader_glsl_log, 0, 0},
171 {WINED3DSIO_EXPP, "expp", "EXP", 1, 2, pshader_hw_map2gl, shader_glsl_expp, 0, 0},
172 {WINED3DSIO_LOGP, "logp", "LOG", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
173 {WINED3DSIO_DST, "dst", "DST", 1, 3, pshader_hw_map2gl, shader_glsl_dst, 0, 0},
174 {WINED3DSIO_LRP, "lrp", "LRP", 1, 4, pshader_hw_map2gl, shader_glsl_lrp, 0, 0},
175 {WINED3DSIO_FRC, "frc", "FRC", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
176 {WINED3DSIO_CND, "cnd", NULL, 1, 4, pshader_hw_cnd, shader_glsl_cnd, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,4)},
177 {WINED3DSIO_CMP, "cmp", NULL, 1, 4, pshader_hw_cmp, shader_glsl_cmp, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(3,0)},
178 {WINED3DSIO_POW, "pow", "POW", 1, 3, pshader_hw_map2gl, shader_glsl_pow, 0, 0},
179 {WINED3DSIO_CRS, "crs", "XPD", 1, 3, pshader_hw_map2gl, shader_glsl_cross, 0, 0},
180 {WINED3DSIO_NRM, "nrm", NULL, 1, 2, shader_hw_nrm, shader_glsl_map2gl, 0, 0},
181 {WINED3DSIO_SINCOS, "sincos", NULL, 1, 4, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(2,0), WINED3DPS_VERSION(2,1)},
182 {WINED3DSIO_SINCOS, "sincos", "SCS", 1, 2, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(3,0), -1},
183 {WINED3DSIO_DP2ADD, "dp2add", NULL, 1, 4, pshader_hw_dp2add, pshader_glsl_dp2add, WINED3DPS_VERSION(2,0), -1},
184 /* Matrix */
185 {WINED3DSIO_M4x4, "m4x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
186 {WINED3DSIO_M4x3, "m4x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
187 {WINED3DSIO_M3x4, "m3x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
188 {WINED3DSIO_M3x3, "m3x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
189 {WINED3DSIO_M3x2, "m3x2", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
190 /* Register declarations */
191 {WINED3DSIO_DCL, "dcl", NULL, 0, 2, NULL, NULL, 0, 0},
192 /* Flow control - requires GLSL or software shaders */
193 {WINED3DSIO_REP , "rep", NULL, 0, 1, NULL, shader_glsl_rep, WINED3DPS_VERSION(2,1), -1},
194 {WINED3DSIO_ENDREP, "endrep", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
195 {WINED3DSIO_IF, "if", NULL, 0, 1, NULL, shader_glsl_if, WINED3DPS_VERSION(2,1), -1},
196 {WINED3DSIO_IFC, "ifc", NULL, 0, 2, NULL, shader_glsl_ifc, WINED3DPS_VERSION(2,1), -1},
197 {WINED3DSIO_ELSE, "else", NULL, 0, 0, NULL, shader_glsl_else, WINED3DPS_VERSION(2,1), -1},
198 {WINED3DSIO_ENDIF, "endif", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
199 {WINED3DSIO_BREAK, "break", NULL, 0, 0, NULL, shader_glsl_break, WINED3DPS_VERSION(2,1), -1},
200 {WINED3DSIO_BREAKC, "breakc", NULL, 0, 2, NULL, shader_glsl_breakc, WINED3DPS_VERSION(2,1), -1},
201 {WINED3DSIO_BREAKP, "breakp", GLNAME_REQUIRE_GLSL, 0, 1, NULL, NULL, 0, 0},
202 {WINED3DSIO_CALL, "call", NULL, 0, 1, NULL, shader_glsl_call, WINED3DPS_VERSION(2,1), -1},
203 {WINED3DSIO_CALLNZ, "callnz", NULL, 0, 2, NULL, shader_glsl_callnz, WINED3DPS_VERSION(2,1), -1},
204 {WINED3DSIO_LOOP, "loop", NULL, 0, 2, NULL, shader_glsl_loop, WINED3DPS_VERSION(3,0), -1},
205 {WINED3DSIO_RET, "ret", NULL, 0, 0, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
206 {WINED3DSIO_ENDLOOP, "endloop", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(3,0), -1},
207 {WINED3DSIO_LABEL, "label", NULL, 0, 1, NULL, shader_glsl_label, WINED3DPS_VERSION(2,1), -1},
208 /* Constant definitions */
209 {WINED3DSIO_DEF, "def", "undefined", 1, 5, NULL, NULL, 0, 0},
210 {WINED3DSIO_DEFB, "defb", GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, 0, 0},
211 {WINED3DSIO_DEFI, "defi", GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, 0, 0},
212 /* Texture */
213 {WINED3DSIO_TEXCOORD, "texcoord", "undefined", 1, 1, pshader_hw_texcoord, pshader_glsl_texcoord, 0, WINED3DPS_VERSION(1,3)},
214 {WINED3DSIO_TEXCOORD, "texcrd", "undefined", 1, 2, pshader_hw_texcoord, pshader_glsl_texcoord, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
215 {WINED3DSIO_TEXKILL, "texkill", "KIL", 1, 1, pshader_hw_texkill, pshader_glsl_texkill, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(3,0)},
216 {WINED3DSIO_TEX, "tex", "undefined", 1, 1, pshader_hw_tex, pshader_glsl_tex, 0, WINED3DPS_VERSION(1,3)},
217 {WINED3DSIO_TEX, "texld", "undefined", 1, 2, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
218 {WINED3DSIO_TEX, "texld", "undefined", 1, 3, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(2,0), -1},
219 {WINED3DSIO_TEXBEM, "texbem", "undefined", 1, 2, pshader_hw_texbem, pshader_glsl_texbem, 0, WINED3DPS_VERSION(1,3)},
220 {WINED3DSIO_TEXBEML, "texbeml", GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texbem, pshader_glsl_texbem, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
221 {WINED3DSIO_TEXREG2AR,"texreg2ar","undefined", 1, 2, pshader_hw_texreg2ar, pshader_glsl_texreg2ar, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
222 {WINED3DSIO_TEXREG2GB,"texreg2gb","undefined", 1, 2, pshader_hw_texreg2gb, pshader_glsl_texreg2gb, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
223 {WINED3DSIO_TEXREG2RGB, "texreg2rgb", "undefined", 1, 2, pshader_hw_texreg2rgb, pshader_glsl_texreg2rgb, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
224 {WINED3DSIO_TEXM3x2PAD, "texm3x2pad", "undefined", 1, 2, pshader_hw_texm3x2pad, pshader_glsl_texm3x2pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
225 {WINED3DSIO_TEXM3x2TEX, "texm3x2tex", "undefined", 1, 2, pshader_hw_texm3x2tex, pshader_glsl_texm3x2tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
226 {WINED3DSIO_TEXM3x3PAD, "texm3x3pad", "undefined", 1, 2, pshader_hw_texm3x3pad, pshader_glsl_texm3x3pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
227 {WINED3DSIO_TEXM3x3DIFF, "texm3x3diff", GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, WINED3DPS_VERSION(0,0), WINED3DPS_VERSION(0,0)},
228 {WINED3DSIO_TEXM3x3SPEC, "texm3x3spec", "undefined", 1, 3, pshader_hw_texm3x3spec, pshader_glsl_texm3x3spec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
229 {WINED3DSIO_TEXM3x3VSPEC, "texm3x3vspec", "undefined", 1, 2, pshader_hw_texm3x3vspec, pshader_glsl_texm3x3vspec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
230 {WINED3DSIO_TEXM3x3TEX, "texm3x3tex", "undefined", 1, 2, pshader_hw_texm3x3tex, pshader_glsl_texm3x3tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
231 {WINED3DSIO_TEXDP3TEX, "texdp3tex", NULL, 1, 2, pshader_hw_texdp3tex, pshader_glsl_texdp3tex, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
232 {WINED3DSIO_TEXM3x2DEPTH, "texm3x2depth", GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texm3x2depth, pshader_glsl_texm3x2depth, WINED3DPS_VERSION(1,3), WINED3DPS_VERSION(1,3)},
233 {WINED3DSIO_TEXDP3, "texdp3", NULL, 1, 2, pshader_hw_texdp3, pshader_glsl_texdp3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
234 {WINED3DSIO_TEXM3x3, "texm3x3", NULL, 1, 2, pshader_hw_texm3x3, pshader_glsl_texm3x3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
235 {WINED3DSIO_TEXDEPTH, "texdepth", NULL, 1, 1, pshader_hw_texdepth, pshader_glsl_texdepth, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
236 {WINED3DSIO_BEM, "bem", "undefined", 1, 3, pshader_hw_bem, pshader_glsl_bem, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
237 {WINED3DSIO_DSX, "dsx", NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
238 {WINED3DSIO_DSY, "dsy", NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
239 {WINED3DSIO_TEXLDD, "texldd", GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
240 {WINED3DSIO_SETP, "setp", GLNAME_REQUIRE_GLSL, 1, 3, NULL, NULL, 0, 0},
241 {WINED3DSIO_TEXLDL, "texldl", NULL, 1, 3, NULL, shader_glsl_texldl, WINED3DPS_VERSION(3,0), -1},
242 {WINED3DSIO_PHASE, "phase", GLNAME_REQUIRE_GLSL, 0, 0, NULL, NULL, 0, 0},
243 {0, NULL, NULL, 0, 0, NULL, NULL, 0, 0}
246 static void pshader_set_limits(
247 IWineD3DPixelShaderImpl *This) {
249 This->baseShader.limits.attributes = 0;
250 This->baseShader.limits.address = 0;
251 This->baseShader.limits.packed_output = 0;
253 switch (This->baseShader.hex_version) {
254 case WINED3DPS_VERSION(1,0):
255 case WINED3DPS_VERSION(1,1):
256 case WINED3DPS_VERSION(1,2):
257 case WINED3DPS_VERSION(1,3):
258 This->baseShader.limits.temporary = 2;
259 This->baseShader.limits.constant_float = 8;
260 This->baseShader.limits.constant_int = 0;
261 This->baseShader.limits.constant_bool = 0;
262 This->baseShader.limits.texcoord = 4;
263 This->baseShader.limits.sampler = 4;
264 This->baseShader.limits.packed_input = 0;
265 This->baseShader.limits.label = 0;
266 break;
268 case WINED3DPS_VERSION(1,4):
269 This->baseShader.limits.temporary = 6;
270 This->baseShader.limits.constant_float = 8;
271 This->baseShader.limits.constant_int = 0;
272 This->baseShader.limits.constant_bool = 0;
273 This->baseShader.limits.texcoord = 6;
274 This->baseShader.limits.sampler = 6;
275 This->baseShader.limits.packed_input = 0;
276 This->baseShader.limits.label = 0;
277 break;
279 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
280 case WINED3DPS_VERSION(2,0):
281 This->baseShader.limits.temporary = 32;
282 This->baseShader.limits.constant_float = 32;
283 This->baseShader.limits.constant_int = 16;
284 This->baseShader.limits.constant_bool = 16;
285 This->baseShader.limits.texcoord = 8;
286 This->baseShader.limits.sampler = 16;
287 This->baseShader.limits.packed_input = 0;
288 break;
290 case WINED3DPS_VERSION(2,1):
291 This->baseShader.limits.temporary = 32;
292 This->baseShader.limits.constant_float = 32;
293 This->baseShader.limits.constant_int = 16;
294 This->baseShader.limits.constant_bool = 16;
295 This->baseShader.limits.texcoord = 8;
296 This->baseShader.limits.sampler = 16;
297 This->baseShader.limits.packed_input = 0;
298 This->baseShader.limits.label = 16;
299 break;
301 case WINED3DPS_VERSION(3,0):
302 This->baseShader.limits.temporary = 32;
303 This->baseShader.limits.constant_float = 224;
304 This->baseShader.limits.constant_int = 16;
305 This->baseShader.limits.constant_bool = 16;
306 This->baseShader.limits.texcoord = 0;
307 This->baseShader.limits.sampler = 16;
308 This->baseShader.limits.packed_input = 12;
309 This->baseShader.limits.label = 16; /* FIXME: 2048 */
310 break;
312 default: This->baseShader.limits.temporary = 32;
313 This->baseShader.limits.constant_float = 32;
314 This->baseShader.limits.constant_int = 16;
315 This->baseShader.limits.constant_bool = 16;
316 This->baseShader.limits.texcoord = 8;
317 This->baseShader.limits.sampler = 16;
318 This->baseShader.limits.packed_input = 0;
319 This->baseShader.limits.label = 0;
320 FIXME("Unrecognized pixel shader version %#x\n",
321 This->baseShader.hex_version);
325 /** Generate a pixel shader string using either GL_FRAGMENT_PROGRAM_ARB
326 or GLSL and send it to the card */
327 static inline VOID IWineD3DPixelShaderImpl_GenerateShader(
328 IWineD3DPixelShader *iface,
329 shader_reg_maps* reg_maps,
330 CONST DWORD *pFunction) {
332 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
333 SHADER_BUFFER buffer;
335 #if 0 /* FIXME: Use the buffer that is held by the device, this is ok since fixups will be skipped for software shaders
336 it also requires entering a critical section but cuts down the runtime footprint of wined3d and any memory fragmentation that may occur... */
337 if (This->device->fixupVertexBufferSize < SHADER_PGMSIZE) {
338 HeapFree(GetProcessHeap(), 0, This->fixupVertexBuffer);
339 This->fixupVertexBuffer = HeapAlloc(GetProcessHeap() , 0, SHADER_PGMSIZE);
340 This->fixupVertexBufferSize = PGMSIZE;
341 This->fixupVertexBuffer[0] = 0;
343 buffer.buffer = This->device->fixupVertexBuffer;
344 #else
345 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
346 #endif
347 buffer.bsize = 0;
348 buffer.lineNo = 0;
349 buffer.newline = TRUE;
351 if (This->baseShader.shader_mode == SHADER_GLSL) {
353 /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
354 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
356 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
357 shader_addline(&buffer, "#extension GL_ARB_draw_buffers : enable\n");
360 /* Base Declarations */
361 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
363 /* Pack 3.0 inputs */
364 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0))
365 pshader_glsl_input_pack(&buffer, This->semantics_in);
367 /* Base Shader Body */
368 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
370 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
371 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
372 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
373 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
374 shader_addline(&buffer, "gl_FragData[0] = R0;\n");
375 else
376 shader_addline(&buffer, "gl_FragColor = R0;\n");
379 /* Pixel shader < 3.0 do not replace the fog stage.
380 * This implements linear fog computation and blending.
381 * TODO: non linear fog
382 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
383 * -1/(e-s) and e/(e-s) respectively.
385 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
386 shader_addline(&buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
387 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
388 shader_addline(&buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
389 else
390 shader_addline(&buffer, "gl_FragColor.xyz = mix(gl_Fog.color.xyz, gl_FragColor.xyz, Fog);\n");
392 if(This->srgb_enabled) {
393 const char *fragcolor;
395 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
396 fragcolor = "gl_FragData[0]";
397 } else {
398 fragcolor = "gl_FragColor";
400 shader_addline(&buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
401 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
402 srgb_sub_high, srgb_sub_high, srgb_sub_high);
403 shader_addline(&buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
404 shader_addline(&buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
405 shader_addline(&buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
406 shader_addline(&buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
407 shader_addline(&buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
410 shader_addline(&buffer, "}\n");
412 TRACE("Compiling shader object %u\n", shader_obj);
413 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer.buffer, NULL));
414 GL_EXTCALL(glCompileShaderARB(shader_obj));
415 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
417 /* Store the shader object */
418 This->baseShader.prgId = shader_obj;
420 } else if (This->baseShader.shader_mode == SHADER_ARB) {
421 /* Create the hw ARB shader */
422 shader_addline(&buffer, "!!ARBfp1.0\n");
424 shader_addline(&buffer, "TEMP TMP;\n"); /* Used in matrix ops */
425 shader_addline(&buffer, "TEMP TMP2;\n"); /* Used in matrix ops */
426 shader_addline(&buffer, "TEMP TA;\n"); /* Used for modifiers */
427 shader_addline(&buffer, "TEMP TB;\n"); /* Used for modifiers */
428 shader_addline(&buffer, "TEMP TC;\n"); /* Used for modifiers */
429 shader_addline(&buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
430 shader_addline(&buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
431 shader_addline(&buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
433 /* Base Declarations */
434 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
436 /* We need two variables for fog blending */
437 shader_addline(&buffer, "TEMP TMP_FOG;\n");
438 if (This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
439 shader_addline(&buffer, "TEMP TMP_COLOR;\n");
442 /* Base Shader Body */
443 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
445 /* calculate fog and blend it
446 * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
447 * -1/(e-s) and e/(e-s) respectively.
449 shader_addline(&buffer, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
451 if(This->srgb_enabled) {
452 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
453 shader_addline(&buffer, "LRP TMP_COLOR.rgb, TMP_FOG.x, R0, state.fog.color;\n");
454 shader_addline(&buffer, "MOV result.color.a, R0.a;\n");
455 } else {
456 shader_addline(&buffer, "LRP TMP_COLOR.rgb, TMP_FOG.x, TMP_COLOR, state.fog.color;\n");
457 shader_addline(&buffer, "MOV result.color.a, TMP_COLOR.a;\n");
459 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
461 /* Calculate the > 0.0031308 case */
462 shader_addline(&buffer, "POW TMP.x, TMP_COLOR.x, srgb_pow.x;\n");
463 shader_addline(&buffer, "POW TMP.y, TMP_COLOR.y, srgb_pow.y;\n");
464 shader_addline(&buffer, "POW TMP.z, TMP_COLOR.z, srgb_pow.z;\n");
465 shader_addline(&buffer, "MUL TMP, TMP, srgb_mul_hi;\n");
466 shader_addline(&buffer, "SUB TMP, TMP, srgb_sub_hi;\n");
467 /* Calculate the < case */
468 shader_addline(&buffer, "MUL TMP2, srgb_mul_low, TMP_COLOR;\n");
469 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
470 shader_addline(&buffer, "SLT TA, srgb_comparison, TMP_COLOR;\n");
471 shader_addline(&buffer, "SGE TB, srgb_comparison, TMP_COLOR;\n");
472 /* Store the components > 0.0031308 in the destination */
473 shader_addline(&buffer, "MUL TMP_COLOR, TMP, TA;\n");
474 /* Add the components that are < 0.0031308 */
475 shader_addline(&buffer, "MAD result.color.xyz, TMP2, TB, TMP_COLOR;\n");
476 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
477 } else {
478 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
479 shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, R0, state.fog.color;\n");
480 shader_addline(&buffer, "MOV result.color.a, R0.a;\n");
481 } else {
482 shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, TMP_COLOR, state.fog.color;\n");
483 shader_addline(&buffer, "MOV result.color.a, TMP_COLOR.a;\n");
487 shader_addline(&buffer, "END\n");
489 /* TODO: change to resource.glObjectHandle or something like that */
490 GL_EXTCALL(glGenProgramsARB(1, &This->baseShader.prgId));
492 TRACE("Creating a hw pixel shader, prg=%d\n", This->baseShader.prgId);
493 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, This->baseShader.prgId));
495 TRACE("Created hw pixel shader, prg=%d\n", This->baseShader.prgId);
496 /* Create the program and check for errors */
497 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
498 buffer.bsize, buffer.buffer));
500 if (glGetError() == GL_INVALID_OPERATION) {
501 GLint errPos;
502 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
503 FIXME("HW PixelShader Error at position %d: %s\n",
504 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
505 This->baseShader.prgId = -1;
509 This->needsbumpmat = reg_maps->bumpmat;
511 #if 1 /* if were using the data buffer of device then we don't need to free it */
512 HeapFree(GetProcessHeap(), 0, buffer.buffer);
513 #endif
516 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
518 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
519 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
521 TRACE("(%p) : pFunction %p\n", iface, pFunction);
523 /* First pass: trace shader */
524 shader_trace_init((IWineD3DBaseShader*) This, pFunction);
525 pshader_set_limits(This);
527 /* Initialize immediate constant lists */
528 list_init(&This->baseShader.constantsF);
529 list_init(&This->baseShader.constantsB);
530 list_init(&This->baseShader.constantsI);
532 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) > 1) {
533 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
534 HRESULT hr;
536 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
537 memset(reg_maps, 0, sizeof(shader_reg_maps));
538 hr = shader_get_registers_used((IWineD3DBaseShader*) This, reg_maps,
539 This->semantics_in, NULL, pFunction, NULL);
540 if (FAILED(hr)) return hr;
541 /* FIXME: validate reg_maps against OpenGL */
544 This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
546 TRACE("(%p) : Copying the function\n", This);
547 if (NULL != pFunction) {
548 void *function;
550 function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->baseShader.functionLength);
551 if (!function) return E_OUTOFMEMORY;
552 memcpy(function, pFunction, This->baseShader.functionLength);
553 This->baseShader.function = function;
554 } else {
555 This->baseShader.function = NULL;
558 return WINED3D_OK;
561 static HRESULT WINAPI IWineD3DPixelShaderImpl_CompileShader(IWineD3DPixelShader *iface) {
563 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
564 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
565 CONST DWORD *function = This->baseShader.function;
566 UINT i, sampler;
567 IWineD3DBaseTextureImpl *texture;
569 TRACE("(%p) : function %p\n", iface, function);
571 /* We're already compiled, but check if any of the hardcoded stateblock assumptions
572 * changed.
574 if (This->baseShader.is_compiled) {
575 char srgbenabled = deviceImpl->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
576 for(i = 0; i < This->baseShader.num_sampled_samplers; i++) {
577 sampler = This->baseShader.sampled_samplers[i];
578 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler];
579 if(texture && texture->baseTexture.shader_conversion_group != This->baseShader.sampled_format[sampler]) {
580 WARN("Recompiling shader %p due to format change on sampler %d\n", This, sampler);
581 WARN("Old format group %s, new is %s\n",
582 debug_d3dformat(This->baseShader.sampled_format[sampler]),
583 debug_d3dformat(texture->baseTexture.shader_conversion_group));
584 goto recompile;
588 /* TODO: Check projected textures */
589 /* TODO: Check texture types(2D, Cube, 3D) */
591 if(srgbenabled != This->srgb_enabled && This->srgb_mode_hardcoded) {
592 WARN("Recompiling shader because srgb correction is different and hardcoded\n");
593 goto recompile;
595 if(This->baseShader.reg_maps.vpos && !This->vpos_uniform) {
596 if(This->render_offscreen != deviceImpl->render_offscreen ||
597 This->height != ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height) {
598 WARN("Recompiling shader because vpos is used, hard compiled and changed\n");
599 goto recompile;
603 return WINED3D_OK;
605 recompile:
606 if(This->baseShader.recompile_count > 50) {
607 FIXME("Shader %p recompiled more than 50 times\n", This);
608 } else {
609 This->baseShader.recompile_count++;
612 if (This->baseShader.shader_mode == SHADER_GLSL && This->baseShader.prgId != 0) {
613 destroy_glsl_pshader(This);
617 /* We don't need to compile */
618 if (!function) {
619 This->baseShader.is_compiled = TRUE;
620 return WINED3D_OK;
623 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1) {
624 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
625 HRESULT hr;
627 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
628 memset(reg_maps, 0, sizeof(shader_reg_maps));
629 hr = shader_get_registers_used((IWineD3DBaseShader*)This, reg_maps,
630 This->semantics_in, NULL, This->baseShader.function, deviceImpl->stateBlock);
631 if (FAILED(hr)) return hr;
632 /* FIXME: validate reg_maps against OpenGL */
635 /* Reset fields tracking stateblock values beeing hardcoded in the shader */
636 This->baseShader.num_sampled_samplers = 0;
638 /* Generate the HW shader */
639 TRACE("(%p) : Generating hardware program\n", This);
640 IWineD3DPixelShaderImpl_GenerateShader(iface, &This->baseShader.reg_maps, function);
642 This->baseShader.is_compiled = TRUE;
644 return WINED3D_OK;
647 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
649 /*** IUnknown methods ***/
650 IWineD3DPixelShaderImpl_QueryInterface,
651 IWineD3DPixelShaderImpl_AddRef,
652 IWineD3DPixelShaderImpl_Release,
653 /*** IWineD3DBase methods ***/
654 IWineD3DPixelShaderImpl_GetParent,
655 /*** IWineD3DBaseShader methods ***/
656 IWineD3DPixelShaderImpl_SetFunction,
657 IWineD3DPixelShaderImpl_CompileShader,
658 /*** IWineD3DPixelShader methods ***/
659 IWineD3DPixelShaderImpl_GetDevice,
660 IWineD3DPixelShaderImpl_GetFunction