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
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)
41 # define TRACE_VSVECTOR(name)
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 ULONG WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
55 return IWineD3DBaseShaderImpl_Release((IWineD3DBaseShader *) iface);
58 /* *******************************************
59 IWineD3DPixelShader IWineD3DPixelShader parts follow
60 ******************************************* */
62 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
63 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
65 *parent = This->parent;
66 IUnknown_AddRef(*parent);
67 TRACE("(%p) : returning %p\n", This, *parent);
71 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
72 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
73 IWineD3DDevice_AddRef(This->baseShader.device);
74 *pDevice = This->baseShader.device;
75 TRACE("(%p) returning %p\n", This, *pDevice);
80 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
81 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
82 TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
85 *pSizeOfData = This->baseShader.functionLength;
88 if (*pSizeOfData < This->baseShader.functionLength) {
89 /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
90 * than the required size we should write the required size and
91 * return D3DERR_MOREDATA. That's not actually true. */
92 return WINED3DERR_INVALIDCALL;
94 if (NULL == This->baseShader.function) { /* no function defined */
95 TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
96 (*(DWORD **) pData) = NULL;
98 if (This->baseShader.functionLength == 0) {
101 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
102 memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
107 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[] = {
109 {WINED3DSIO_NOP, "nop", "NOP", 0, 0, pshader_hw_map2gl, NULL, 0, 0},
110 {WINED3DSIO_MOV, "mov", "MOV", 1, 2, pshader_hw_map2gl, shader_glsl_mov, 0, 0},
111 {WINED3DSIO_ADD, "add", "ADD", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
112 {WINED3DSIO_SUB, "sub", "SUB", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
113 {WINED3DSIO_MAD, "mad", "MAD", 1, 4, pshader_hw_map2gl, shader_glsl_mad, 0, 0},
114 {WINED3DSIO_MUL, "mul", "MUL", 1, 3, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
115 {WINED3DSIO_RCP, "rcp", "RCP", 1, 2, pshader_hw_map2gl, shader_glsl_rcp, 0, 0},
116 {WINED3DSIO_RSQ, "rsq", "RSQ", 1, 2, pshader_hw_map2gl, shader_glsl_rsq, 0, 0},
117 {WINED3DSIO_DP3, "dp3", "DP3", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
118 {WINED3DSIO_DP4, "dp4", "DP4", 1, 3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
119 {WINED3DSIO_MIN, "min", "MIN", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
120 {WINED3DSIO_MAX, "max", "MAX", 1, 3, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
121 {WINED3DSIO_SLT, "slt", "SLT", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
122 {WINED3DSIO_SGE, "sge", "SGE", 1, 3, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
123 {WINED3DSIO_ABS, "abs", "ABS", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
124 {WINED3DSIO_EXP, "exp", "EX2", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
125 {WINED3DSIO_LOG, "log", "LG2", 1, 2, pshader_hw_map2gl, shader_glsl_log, 0, 0},
126 {WINED3DSIO_EXPP, "expp", "EXP", 1, 2, pshader_hw_map2gl, shader_glsl_expp, 0, 0},
127 {WINED3DSIO_LOGP, "logp", "LOG", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
128 {WINED3DSIO_DST, "dst", "DST", 1, 3, pshader_hw_map2gl, shader_glsl_dst, 0, 0},
129 {WINED3DSIO_LRP, "lrp", "LRP", 1, 4, pshader_hw_map2gl, shader_glsl_lrp, 0, 0},
130 {WINED3DSIO_FRC, "frc", "FRC", 1, 2, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
131 {WINED3DSIO_CND, "cnd", NULL, 1, 4, pshader_hw_cnd, shader_glsl_cnd, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,4)},
132 {WINED3DSIO_CMP, "cmp", NULL, 1, 4, pshader_hw_cmp, shader_glsl_cmp, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(3,0)},
133 {WINED3DSIO_POW, "pow", "POW", 1, 3, pshader_hw_map2gl, shader_glsl_pow, 0, 0},
134 {WINED3DSIO_CRS, "crs", "XPD", 1, 3, pshader_hw_map2gl, shader_glsl_cross, 0, 0},
135 {WINED3DSIO_NRM, "nrm", NULL, 1, 2, shader_hw_nrm, shader_glsl_map2gl, 0, 0},
136 {WINED3DSIO_SINCOS, "sincos", NULL, 1, 4, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(2,0), WINED3DPS_VERSION(2,1)},
137 {WINED3DSIO_SINCOS, "sincos", "SCS", 1, 2, shader_hw_sincos, shader_glsl_sincos, WINED3DPS_VERSION(3,0), -1},
138 {WINED3DSIO_DP2ADD, "dp2add", NULL, 1, 4, pshader_hw_dp2add, pshader_glsl_dp2add, WINED3DPS_VERSION(2,0), -1},
140 {WINED3DSIO_M4x4, "m4x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
141 {WINED3DSIO_M4x3, "m4x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
142 {WINED3DSIO_M3x4, "m3x4", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
143 {WINED3DSIO_M3x3, "m3x3", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
144 {WINED3DSIO_M3x2, "m3x2", "undefined", 1, 3, shader_hw_mnxn, shader_glsl_mnxn, 0, 0},
145 /* Register declarations */
146 {WINED3DSIO_DCL, "dcl", NULL, 0, 2, NULL, NULL, 0, 0},
147 /* Flow control - requires GLSL or software shaders */
148 {WINED3DSIO_REP , "rep", NULL, 0, 1, NULL, shader_glsl_rep, WINED3DPS_VERSION(2,1), -1},
149 {WINED3DSIO_ENDREP, "endrep", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
150 {WINED3DSIO_IF, "if", NULL, 0, 1, NULL, shader_glsl_if, WINED3DPS_VERSION(2,1), -1},
151 {WINED3DSIO_IFC, "ifc", NULL, 0, 2, NULL, shader_glsl_ifc, WINED3DPS_VERSION(2,1), -1},
152 {WINED3DSIO_ELSE, "else", NULL, 0, 0, NULL, shader_glsl_else, WINED3DPS_VERSION(2,1), -1},
153 {WINED3DSIO_ENDIF, "endif", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
154 {WINED3DSIO_BREAK, "break", NULL, 0, 0, NULL, shader_glsl_break, WINED3DPS_VERSION(2,1), -1},
155 {WINED3DSIO_BREAKC, "breakc", NULL, 0, 2, NULL, shader_glsl_breakc, WINED3DPS_VERSION(2,1), -1},
156 {WINED3DSIO_BREAKP, "breakp", GLNAME_REQUIRE_GLSL, 0, 1, NULL, NULL, 0, 0},
157 {WINED3DSIO_CALL, "call", NULL, 0, 1, NULL, shader_glsl_call, WINED3DPS_VERSION(2,1), -1},
158 {WINED3DSIO_CALLNZ, "callnz", NULL, 0, 2, NULL, shader_glsl_callnz, WINED3DPS_VERSION(2,1), -1},
159 {WINED3DSIO_LOOP, "loop", NULL, 0, 2, NULL, shader_glsl_loop, WINED3DPS_VERSION(3,0), -1},
160 {WINED3DSIO_RET, "ret", NULL, 0, 0, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
161 {WINED3DSIO_ENDLOOP, "endloop", NULL, 0, 0, NULL, shader_glsl_end, WINED3DPS_VERSION(3,0), -1},
162 {WINED3DSIO_LABEL, "label", NULL, 0, 1, NULL, shader_glsl_label, WINED3DPS_VERSION(2,1), -1},
163 /* Constant definitions */
164 {WINED3DSIO_DEF, "def", "undefined", 1, 5, NULL, NULL, 0, 0},
165 {WINED3DSIO_DEFB, "defb", GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, 0, 0},
166 {WINED3DSIO_DEFI, "defi", GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, 0, 0},
168 {WINED3DSIO_TEXCOORD, "texcoord", "undefined", 1, 1, pshader_hw_texcoord, pshader_glsl_texcoord, 0, WINED3DPS_VERSION(1,3)},
169 {WINED3DSIO_TEXCOORD, "texcrd", "undefined", 1, 2, pshader_hw_texcoord, pshader_glsl_texcoord, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
170 {WINED3DSIO_TEXKILL, "texkill", "KIL", 1, 1, pshader_hw_texkill, pshader_glsl_texkill, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(3,0)},
171 {WINED3DSIO_TEX, "tex", "undefined", 1, 1, pshader_hw_tex, pshader_glsl_tex, 0, WINED3DPS_VERSION(1,3)},
172 {WINED3DSIO_TEX, "texld", "undefined", 1, 2, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
173 {WINED3DSIO_TEX, "texld", "undefined", 1, 3, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(2,0), -1},
174 {WINED3DSIO_TEXBEM, "texbem", "undefined", 1, 2, pshader_hw_texbem, pshader_glsl_texbem, 0, WINED3DPS_VERSION(1,3)},
175 {WINED3DSIO_TEXBEML, "texbeml", GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texbem, pshader_glsl_texbem, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
176 {WINED3DSIO_TEXREG2AR,"texreg2ar","undefined", 1, 2, pshader_hw_texreg2ar, pshader_glsl_texreg2ar, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
177 {WINED3DSIO_TEXREG2GB,"texreg2gb","undefined", 1, 2, pshader_hw_texreg2gb, pshader_glsl_texreg2gb, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
178 {WINED3DSIO_TEXREG2RGB, "texreg2rgb", "undefined", 1, 2, pshader_hw_texreg2rgb, pshader_glsl_texreg2rgb, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
179 {WINED3DSIO_TEXM3x2PAD, "texm3x2pad", "undefined", 1, 2, pshader_hw_texm3x2pad, pshader_glsl_texm3x2pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
180 {WINED3DSIO_TEXM3x2TEX, "texm3x2tex", "undefined", 1, 2, pshader_hw_texm3x2tex, pshader_glsl_texm3x2tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
181 {WINED3DSIO_TEXM3x3PAD, "texm3x3pad", "undefined", 1, 2, pshader_hw_texm3x3pad, pshader_glsl_texm3x3pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
182 {WINED3DSIO_TEXM3x3DIFF, "texm3x3diff", GLNAME_REQUIRE_GLSL, 1, 2, NULL, NULL, WINED3DPS_VERSION(0,0), WINED3DPS_VERSION(0,0)},
183 {WINED3DSIO_TEXM3x3SPEC, "texm3x3spec", "undefined", 1, 3, pshader_hw_texm3x3spec, pshader_glsl_texm3x3spec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
184 {WINED3DSIO_TEXM3x3VSPEC, "texm3x3vspec", "undefined", 1, 2, pshader_hw_texm3x3vspec, pshader_glsl_texm3x3vspec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
185 {WINED3DSIO_TEXM3x3TEX, "texm3x3tex", "undefined", 1, 2, pshader_hw_texm3x3tex, pshader_glsl_texm3x3tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
186 {WINED3DSIO_TEXDP3TEX, "texdp3tex", NULL, 1, 2, pshader_hw_texdp3tex, pshader_glsl_texdp3tex, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
187 {WINED3DSIO_TEXM3x2DEPTH, "texm3x2depth", GLNAME_REQUIRE_GLSL, 1, 2, pshader_hw_texm3x2depth, pshader_glsl_texm3x2depth, WINED3DPS_VERSION(1,3), WINED3DPS_VERSION(1,3)},
188 {WINED3DSIO_TEXDP3, "texdp3", NULL, 1, 2, pshader_hw_texdp3, pshader_glsl_texdp3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
189 {WINED3DSIO_TEXM3x3, "texm3x3", NULL, 1, 2, pshader_hw_texm3x3, pshader_glsl_texm3x3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
190 {WINED3DSIO_TEXDEPTH, "texdepth", NULL, 1, 1, pshader_hw_texdepth, pshader_glsl_texdepth, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
191 {WINED3DSIO_BEM, "bem", "undefined", 1, 3, pshader_hw_bem, pshader_glsl_bem, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
192 {WINED3DSIO_DSX, "dsx", NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
193 {WINED3DSIO_DSY, "dsy", NULL, 1, 2, NULL, shader_glsl_map2gl, WINED3DPS_VERSION(2,1), -1},
194 {WINED3DSIO_TEXLDD, "texldd", GLNAME_REQUIRE_GLSL, 1, 5, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
195 {WINED3DSIO_SETP, "setp", GLNAME_REQUIRE_GLSL, 1, 3, NULL, NULL, 0, 0},
196 {WINED3DSIO_TEXLDL, "texldl", NULL, 1, 3, NULL, shader_glsl_texldl, WINED3DPS_VERSION(3,0), -1},
197 {WINED3DSIO_PHASE, "phase", GLNAME_REQUIRE_GLSL, 0, 0, NULL, NULL, 0, 0},
198 {0, NULL, NULL, 0, 0, NULL, NULL, 0, 0}
201 static void pshader_set_limits(
202 IWineD3DPixelShaderImpl *This) {
204 This->baseShader.limits.attributes = 0;
205 This->baseShader.limits.address = 0;
206 This->baseShader.limits.packed_output = 0;
208 switch (This->baseShader.hex_version) {
209 case WINED3DPS_VERSION(1,0):
210 case WINED3DPS_VERSION(1,1):
211 case WINED3DPS_VERSION(1,2):
212 case WINED3DPS_VERSION(1,3):
213 This->baseShader.limits.temporary = 2;
214 This->baseShader.limits.constant_float = 8;
215 This->baseShader.limits.constant_int = 0;
216 This->baseShader.limits.constant_bool = 0;
217 This->baseShader.limits.texcoord = 4;
218 This->baseShader.limits.sampler = 4;
219 This->baseShader.limits.packed_input = 0;
220 This->baseShader.limits.label = 0;
223 case WINED3DPS_VERSION(1,4):
224 This->baseShader.limits.temporary = 6;
225 This->baseShader.limits.constant_float = 8;
226 This->baseShader.limits.constant_int = 0;
227 This->baseShader.limits.constant_bool = 0;
228 This->baseShader.limits.texcoord = 6;
229 This->baseShader.limits.sampler = 6;
230 This->baseShader.limits.packed_input = 0;
231 This->baseShader.limits.label = 0;
234 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
235 case WINED3DPS_VERSION(2,0):
236 This->baseShader.limits.temporary = 32;
237 This->baseShader.limits.constant_float = 32;
238 This->baseShader.limits.constant_int = 16;
239 This->baseShader.limits.constant_bool = 16;
240 This->baseShader.limits.texcoord = 8;
241 This->baseShader.limits.sampler = 16;
242 This->baseShader.limits.packed_input = 0;
245 case WINED3DPS_VERSION(2,1):
246 This->baseShader.limits.temporary = 32;
247 This->baseShader.limits.constant_float = 32;
248 This->baseShader.limits.constant_int = 16;
249 This->baseShader.limits.constant_bool = 16;
250 This->baseShader.limits.texcoord = 8;
251 This->baseShader.limits.sampler = 16;
252 This->baseShader.limits.packed_input = 0;
253 This->baseShader.limits.label = 16;
256 case WINED3DPS_VERSION(3,0):
257 This->baseShader.limits.temporary = 32;
258 This->baseShader.limits.constant_float = 224;
259 This->baseShader.limits.constant_int = 16;
260 This->baseShader.limits.constant_bool = 16;
261 This->baseShader.limits.texcoord = 0;
262 This->baseShader.limits.sampler = 16;
263 This->baseShader.limits.packed_input = 12;
264 This->baseShader.limits.label = 16; /* FIXME: 2048 */
267 default: This->baseShader.limits.temporary = 32;
268 This->baseShader.limits.constant_float = 32;
269 This->baseShader.limits.constant_int = 16;
270 This->baseShader.limits.constant_bool = 16;
271 This->baseShader.limits.texcoord = 8;
272 This->baseShader.limits.sampler = 16;
273 This->baseShader.limits.packed_input = 0;
274 This->baseShader.limits.label = 0;
275 FIXME("Unrecognized pixel shader version %#x\n",
276 This->baseShader.hex_version);
280 /** Generate a pixel shader string using either GL_FRAGMENT_PROGRAM_ARB
281 or GLSL and send it to the card */
282 static inline VOID IWineD3DPixelShaderImpl_GenerateShader(
283 IWineD3DPixelShader *iface,
284 shader_reg_maps* reg_maps,
285 CONST DWORD *pFunction) {
287 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
288 SHADER_BUFFER buffer;
289 const char *fragcolor;
291 #if 0 /* FIXME: Use the buffer that is held by the device, this is ok since fixups will be skipped for software shaders
292 it also requires entering a critical section but cuts down the runtime footprint of wined3d and any memory fragmentation that may occur... */
293 if (This->device->fixupVertexBufferSize < SHADER_PGMSIZE) {
294 HeapFree(GetProcessHeap(), 0, This->fixupVertexBuffer);
295 This->fixupVertexBuffer = HeapAlloc(GetProcessHeap() , 0, SHADER_PGMSIZE);
296 This->fixupVertexBufferSize = PGMSIZE;
297 This->fixupVertexBuffer[0] = 0;
299 buffer.buffer = This->device->fixupVertexBuffer;
301 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
305 buffer.newline = TRUE;
307 if (This->baseShader.shader_mode == SHADER_GLSL) {
309 /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
310 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
312 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
313 shader_addline(&buffer, "#extension GL_ARB_draw_buffers : enable\n");
315 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
316 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
317 * drivers write a warning if we don't do so
319 shader_addline(&buffer, "#extension GL_ARB_texture_rectangle : enable\n");
322 /* Base Declarations */
323 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
325 /* Pack 3.0 inputs */
326 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
328 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed) {
329 This->vertexprocessing = pretransformed;
330 pshader_glsl_input_pack(&buffer, This->semantics_in, iface);
331 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device)) {
332 This->vertexprocessing = fixedfunction;
333 pshader_glsl_input_pack(&buffer, This->semantics_in, iface);
335 This->vertexprocessing = vertexshader;
339 /* Base Shader Body */
340 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
342 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
343 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
344 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
345 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
346 shader_addline(&buffer, "gl_FragData[0] = R0;\n");
348 shader_addline(&buffer, "gl_FragColor = R0;\n");
351 if(GL_SUPPORT(ARB_DRAW_BUFFERS)) {
352 fragcolor = "gl_FragData[0]";
354 fragcolor = "gl_FragColor";
356 if(This->srgb_enabled) {
357 shader_addline(&buffer, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
358 fragcolor, srgb_pow, srgb_pow, srgb_pow, srgb_mul_high, srgb_mul_high, srgb_mul_high,
359 srgb_sub_high, srgb_sub_high, srgb_sub_high);
360 shader_addline(&buffer, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor);
361 shader_addline(&buffer, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor, fragcolor);
362 shader_addline(&buffer, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor, fragcolor);
363 shader_addline(&buffer, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor, fragcolor);
364 shader_addline(&buffer, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor, fragcolor);
366 /* Pixel shader < 3.0 do not replace the fog stage.
367 * This implements linear fog computation and blending.
368 * TODO: non linear fog
369 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
370 * -1/(e-s) and e/(e-s) respectively.
372 if(This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
373 shader_addline(&buffer, "float Fog = clamp(gl_FogFragCoord * gl_Fog.start + gl_Fog.end, 0.0, 1.0);\n");
374 shader_addline(&buffer, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor, fragcolor);
377 shader_addline(&buffer, "}\n");
379 TRACE("Compiling shader object %u\n", shader_obj);
380 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer.buffer, NULL));
381 GL_EXTCALL(glCompileShaderARB(shader_obj));
382 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
384 /* Store the shader object */
385 This->baseShader.prgId = shader_obj;
387 } else if (This->baseShader.shader_mode == SHADER_ARB) {
388 /* Create the hw ARB shader */
389 shader_addline(&buffer, "!!ARBfp1.0\n");
391 shader_addline(&buffer, "TEMP TMP;\n"); /* Used in matrix ops */
392 shader_addline(&buffer, "TEMP TMP2;\n"); /* Used in matrix ops */
393 shader_addline(&buffer, "TEMP TA;\n"); /* Used for modifiers */
394 shader_addline(&buffer, "TEMP TB;\n"); /* Used for modifiers */
395 shader_addline(&buffer, "TEMP TC;\n"); /* Used for modifiers */
396 shader_addline(&buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
397 shader_addline(&buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
398 shader_addline(&buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
400 /* Base Declarations */
401 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
403 /* We need two variables for fog blending */
404 shader_addline(&buffer, "TEMP TMP_FOG;\n");
405 if (This->baseShader.hex_version >= WINED3DPS_VERSION(2,0)) {
406 shader_addline(&buffer, "TEMP TMP_COLOR;\n");
409 /* Base Shader Body */
410 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
412 /* calculate fog and blend it
413 * NOTE: state.fog.params.y and state.fog.params.z don't hold fog start s and end e but
414 * -1/(e-s) and e/(e-s) respectively.
416 shader_addline(&buffer, "MAD_SAT TMP_FOG, fragment.fogcoord, state.fog.params.y, state.fog.params.z;\n");
418 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
421 fragcolor = "TMP_COLOR";
423 if(This->srgb_enabled) {
424 /* Perform sRGB write correction. See GLX_EXT_framebuffer_sRGB */
426 /* Calculate the > 0.0031308 case */
427 shader_addline(&buffer, "POW TMP.x, %s.x, srgb_pow.x;\n", fragcolor);
428 shader_addline(&buffer, "POW TMP.y, %s.y, srgb_pow.y;\n", fragcolor);
429 shader_addline(&buffer, "POW TMP.z, %s.z, srgb_pow.z;\n", fragcolor);
430 shader_addline(&buffer, "MUL TMP, TMP, srgb_mul_hi;\n");
431 shader_addline(&buffer, "SUB TMP, TMP, srgb_sub_hi;\n");
432 /* Calculate the < case */
433 shader_addline(&buffer, "MUL TMP2, srgb_mul_low, %s;\n", fragcolor);
434 /* Get 1.0 / 0.0 masks for > 0.0031308 and < 0.0031308 */
435 shader_addline(&buffer, "SLT TA, srgb_comparison, %s;\n", fragcolor);
436 shader_addline(&buffer, "SGE TB, srgb_comparison, %s;\n", fragcolor);
437 /* Store the components > 0.0031308 in the destination */
438 shader_addline(&buffer, "MUL %s, TMP, TA;\n", fragcolor);
439 /* Add the components that are < 0.0031308 */
440 shader_addline(&buffer, "MAD result.color.xyz, TMP2, TB, %s;\n", fragcolor);
441 /* [0.0;1.0] clamping. Not needed, this is done implicitly */
443 if (This->baseShader.hex_version < WINED3DPS_VERSION(3,0)) {
444 shader_addline(&buffer, "LRP result.color.rgb, TMP_FOG.x, %s, state.fog.color;\n", fragcolor);
445 shader_addline(&buffer, "MOV result.color.a, %s.a;\n", fragcolor);
448 shader_addline(&buffer, "END\n");
450 /* TODO: change to resource.glObjectHandle or something like that */
451 GL_EXTCALL(glGenProgramsARB(1, &This->baseShader.prgId));
453 TRACE("Creating a hw pixel shader, prg=%d\n", This->baseShader.prgId);
454 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, This->baseShader.prgId));
456 TRACE("Created hw pixel shader, prg=%d\n", This->baseShader.prgId);
457 /* Create the program and check for errors */
458 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
459 buffer.bsize, buffer.buffer));
461 if (glGetError() == GL_INVALID_OPERATION) {
463 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
464 FIXME("HW PixelShader Error at position %d: %s\n",
465 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
466 This->baseShader.prgId = -1;
470 This->needsbumpmat = reg_maps->bumpmat;
472 #if 1 /* if were using the data buffer of device then we don't need to free it */
473 HeapFree(GetProcessHeap(), 0, buffer.buffer);
477 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
479 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
480 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
482 TRACE("(%p) : pFunction %p\n", iface, pFunction);
484 /* First pass: trace shader */
485 shader_trace_init((IWineD3DBaseShader*) This, pFunction);
486 pshader_set_limits(This);
488 /* Initialize immediate constant lists */
489 list_init(&This->baseShader.constantsF);
490 list_init(&This->baseShader.constantsB);
491 list_init(&This->baseShader.constantsI);
493 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) > 1) {
494 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
496 unsigned int i, j, highest_reg_used = 0, num_regs_used = 0;
498 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
499 memset(reg_maps, 0, sizeof(shader_reg_maps));
500 hr = shader_get_registers_used((IWineD3DBaseShader*) This, reg_maps,
501 This->semantics_in, NULL, pFunction, NULL);
502 if (FAILED(hr)) return hr;
503 /* FIXME: validate reg_maps against OpenGL */
505 for(i = 0; i < MAX_REG_INPUT; i++) {
506 if(This->input_reg_used[i]) {
508 highest_reg_used = i;
512 /* Don't do any register mapping magic if it is not needed, or if we can't
513 * achive anything anyway
515 if(highest_reg_used < (GL_LIMITS(glsl_varyings) / 4) ||
516 num_regs_used > (GL_LIMITS(glsl_varyings) / 4) ) {
517 if(num_regs_used > (GL_LIMITS(glsl_varyings) / 4)) {
518 /* This happens with relative addressing. The input mapper function
519 * warns about this if the higher registers are declared too, so
520 * don't write a FIXME here
522 WARN("More varying registers used than supported\n");
525 for(i = 0; i < MAX_REG_INPUT; i++) {
526 This->input_reg_map[i] = i;
530 for(i = 0; i < MAX_REG_INPUT; i++) {
531 if(This->input_reg_used[i]) {
532 This->input_reg_map[i] = j;
535 This->input_reg_map[i] = -1;
540 This->baseShader.load_local_constsF = FALSE;
542 This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
544 TRACE("(%p) : Copying the function\n", This);
545 if (NULL != pFunction) {
548 function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->baseShader.functionLength);
549 if (!function) return E_OUTOFMEMORY;
550 memcpy(function, pFunction, This->baseShader.functionLength);
551 This->baseShader.function = function;
553 This->baseShader.function = NULL;
559 static HRESULT WINAPI IWineD3DPixelShaderImpl_CompileShader(IWineD3DPixelShader *iface) {
561 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
562 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
563 CONST DWORD *function = This->baseShader.function;
565 IWineD3DBaseTextureImpl *texture;
567 TRACE("(%p) : function %p\n", iface, function);
569 /* We're already compiled, but check if any of the hardcoded stateblock assumptions
572 if (This->baseShader.is_compiled) {
573 char srgbenabled = deviceImpl->stateBlock->renderState[WINED3DRS_SRGBWRITEENABLE] ? 1 : 0;
574 for(i = 0; i < This->baseShader.num_sampled_samplers; i++) {
575 sampler = This->baseShader.sampled_samplers[i];
576 texture = (IWineD3DBaseTextureImpl *) deviceImpl->stateBlock->textures[sampler];
577 if(texture && texture->baseTexture.shader_conversion_group != This->baseShader.sampled_format[sampler]) {
578 WARN("Recompiling shader %p due to format change on sampler %d\n", This, sampler);
579 WARN("Old format group %s, new is %s\n",
580 debug_d3dformat(This->baseShader.sampled_format[sampler]),
581 debug_d3dformat(texture->baseTexture.shader_conversion_group));
586 /* TODO: Check projected textures */
587 /* TODO: Check texture types(2D, Cube, 3D) */
589 if(srgbenabled != This->srgb_enabled && This->srgb_mode_hardcoded) {
590 WARN("Recompiling shader because srgb correction is different and hardcoded\n");
593 if(This->baseShader.reg_maps.vpos && !This->vpos_uniform) {
594 if(This->render_offscreen != deviceImpl->render_offscreen ||
595 This->height != ((IWineD3DSurfaceImpl *) deviceImpl->render_targets[0])->currentDesc.Height) {
596 WARN("Recompiling shader because vpos is used, hard compiled and changed\n");
600 if(This->baseShader.reg_maps.usesdsy && !This->vpos_uniform) {
601 if(This->render_offscreen ? 0 : 1 != deviceImpl->render_offscreen ? 0 : 1) {
602 WARN("Recompiling shader because dsy is used, hard compiled and render_offscreen changed\n");
606 if(This->baseShader.hex_version >= WINED3DPS_VERSION(3,0)) {
607 if(((IWineD3DDeviceImpl *) This->baseShader.device)->strided_streams.u.s.position_transformed &&
608 This->vertexprocessing != pretransformed) {
609 WARN("Recompiling shader because pretransformed vertices are provided, which wasn't the case before\n");
611 } else if(!use_vs((IWineD3DDeviceImpl *) This->baseShader.device) &&
612 This->vertexprocessing != fixedfunction) {
613 WARN("Recompiling shader because fixed function vp is in use, which wasn't the case before\n");
615 } else if(This->vertexprocessing != vertexshader) {
616 WARN("Recompiling shader because vertex shaders are in use, which wasn't the case before\n");
624 if(This->baseShader.recompile_count > 50) {
625 FIXME("Shader %p recompiled more than 50 times\n", This);
627 This->baseShader.recompile_count++;
630 deviceImpl->shader_backend->shader_destroy((IWineD3DBaseShader *) iface);
633 /* We don't need to compile */
635 This->baseShader.is_compiled = TRUE;
639 if (WINED3DSHADER_VERSION_MAJOR(This->baseShader.hex_version) == 1) {
640 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
643 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
644 memset(reg_maps, 0, sizeof(shader_reg_maps));
645 hr = shader_get_registers_used((IWineD3DBaseShader*)This, reg_maps,
646 This->semantics_in, NULL, This->baseShader.function, deviceImpl->stateBlock);
647 if (FAILED(hr)) return hr;
648 /* FIXME: validate reg_maps against OpenGL */
651 /* Reset fields tracking stateblock values being hardcoded in the shader */
652 This->baseShader.num_sampled_samplers = 0;
654 /* Generate the HW shader */
655 TRACE("(%p) : Generating hardware program\n", This);
656 IWineD3DPixelShaderImpl_GenerateShader(iface, &This->baseShader.reg_maps, function);
658 This->baseShader.is_compiled = TRUE;
663 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
665 /*** IUnknown methods ***/
666 IWineD3DPixelShaderImpl_QueryInterface,
667 IWineD3DPixelShaderImpl_AddRef,
668 IWineD3DPixelShaderImpl_Release,
669 /*** IWineD3DBase methods ***/
670 IWineD3DPixelShaderImpl_GetParent,
671 /*** IWineD3DBaseShader methods ***/
672 IWineD3DPixelShaderImpl_SetFunction,
673 IWineD3DPixelShaderImpl_CompileShader,
674 /*** IWineD3DPixelShader methods ***/
675 IWineD3DPixelShaderImpl_GetDevice,
676 IWineD3DPixelShaderImpl_GetFunction