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 ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->baseShader.device)->wineD3D))->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)
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);
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 ULONG WINAPI IWineD3DPixelShaderImpl_Release(IWineD3DPixelShader *iface) {
71 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
73 TRACE("(%p) : Releasing from %d\n", This, This->ref);
74 ref = InterlockedDecrement(&This->ref);
76 if (This->baseShader.shader_mode == SHADER_GLSL && This->baseShader.prgId != 0) {
77 /* If this shader is still attached to a program, GL will perform a lazy delete */
78 TRACE("Deleting shader object %u\n", This->baseShader.prgId);
79 GL_EXTCALL(glDeleteObjectARB(This->baseShader.prgId));
80 checkGLcall("glDeleteObjectARB");
82 shader_delete_constant_list(&This->baseShader.constantsF);
83 shader_delete_constant_list(&This->baseShader.constantsB);
84 shader_delete_constant_list(&This->baseShader.constantsI);
85 HeapFree(GetProcessHeap(), 0, This);
90 /* *******************************************
91 IWineD3DPixelShader IWineD3DPixelShader parts follow
92 ******************************************* */
94 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetParent(IWineD3DPixelShader *iface, IUnknown** parent){
95 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
97 *parent = This->parent;
98 IUnknown_AddRef(*parent);
99 TRACE("(%p) : returning %p\n", This, *parent);
103 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetDevice(IWineD3DPixelShader* iface, IWineD3DDevice **pDevice){
104 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
105 IWineD3DDevice_AddRef(This->baseShader.device);
106 *pDevice = This->baseShader.device;
107 TRACE("(%p) returning %p\n", This, *pDevice);
112 static HRESULT WINAPI IWineD3DPixelShaderImpl_GetFunction(IWineD3DPixelShader* impl, VOID* pData, UINT* pSizeOfData) {
113 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)impl;
114 TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
117 *pSizeOfData = This->baseShader.functionLength;
120 if (*pSizeOfData < This->baseShader.functionLength) {
121 *pSizeOfData = This->baseShader.functionLength;
122 return WINED3DERR_MOREDATA;
124 if (NULL == This->baseShader.function) { /* no function defined */
125 TRACE("(%p) : GetFunction no User Function defined using NULL to %p\n", This, pData);
126 (*(DWORD **) pData) = NULL;
128 if (This->baseShader.functionLength == 0) {
131 TRACE("(%p) : GetFunction copying to %p\n", This, pData);
132 memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
137 /*******************************
138 * pshader functions software VM
141 static void pshader_add(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
142 d->x = s0->x + s1->x;
143 d->y = s0->y + s1->y;
144 d->z = s0->z + s1->z;
145 d->w = s0->w + s1->w;
146 PSTRACE(("executing add: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
147 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
150 static void pshader_dp3(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
151 d->x = d->y = d->z = d->w = s0->x * s1->x + s0->y * s1->y + s0->z * s1->z;
152 PSTRACE(("executing dp3: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
153 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
156 static void pshader_dp4(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
157 d->x = d->y = d->z = d->w = s0->x * s1->x + s0->y * s1->y + s0->z * s1->z + s0->w * s1->w;
158 PSTRACE(("executing dp4: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
159 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
162 static void pshader_dst(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
164 d->y = s0->y * s1->y;
167 PSTRACE(("executing dst: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
168 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
171 static void pshader_expp(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
177 tmp.f = floorf(s0->w);
178 d->x = powf(2.0f, tmp.f);
179 d->y = s0->w - tmp.f;
180 tmp.f = powf(2.0f, s0->w);
181 tmp.d &= 0xFFFFFF00U;
184 PSTRACE(("executing exp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
185 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
188 static void pshader_logp(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
189 float tmp_f = fabsf(s0->w);
190 d->x = d->y = d->z = d->w = (0.0f != tmp_f) ? logf(tmp_f) / logf(2.0f) : -HUGE_VAL;
191 PSTRACE(("executing logp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
192 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
195 static void pshader_mad(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1, WINED3DSHADERVECTOR* s2) {
196 d->x = s0->x * s1->x + s2->x;
197 d->y = s0->y * s1->y + s2->y;
198 d->z = s0->z * s1->z + s2->z;
199 d->w = s0->w * s1->w + s2->w;
200 PSTRACE(("executing mad: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) s2=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
201 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, s2->x, s2->y, s2->z, s2->w, d->x, d->y, d->z, d->w));
204 static void pshader_max(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
205 d->x = (s0->x >= s1->x) ? s0->x : s1->x;
206 d->y = (s0->y >= s1->y) ? s0->y : s1->y;
207 d->z = (s0->z >= s1->z) ? s0->z : s1->z;
208 d->w = (s0->w >= s1->w) ? s0->w : s1->w;
209 PSTRACE(("executing max: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
210 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
213 static void pshader_min(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
214 d->x = (s0->x < s1->x) ? s0->x : s1->x;
215 d->y = (s0->y < s1->y) ? s0->y : s1->y;
216 d->z = (s0->z < s1->z) ? s0->z : s1->z;
217 d->w = (s0->w < s1->w) ? s0->w : s1->w;
218 PSTRACE(("executing min: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
219 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
222 static void pshader_mov(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
227 PSTRACE(("executing mov: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
228 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
231 static void pshader_mul(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
232 d->x = s0->x * s1->x;
233 d->y = s0->y * s1->y;
234 d->z = s0->z * s1->z;
235 d->w = s0->w * s1->w;
236 PSTRACE(("executing mul: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
237 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
240 static void pshader_nop(void) {
241 /* NOPPPP ahhh too easy ;) */
242 PSTRACE(("executing nop\n"));
245 static void pshader_rcp(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
246 d->x = d->y = d->z = d->w = (0.0f == s0->w) ? HUGE_VAL : 1.0f / s0->w;
247 PSTRACE(("executing rcp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
248 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
251 static void pshader_rsq(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
252 float tmp_f = fabsf(s0->w);
253 d->x = d->y = d->z = d->w = (0.0f == tmp_f) ? HUGE_VAL : ((1.0f != tmp_f) ? 1.0f / sqrtf(tmp_f) : 1.0f);
254 PSTRACE(("executing rsq: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
255 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
258 static void pshader_sge(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
259 d->x = (s0->x >= s1->x) ? 1.0f : 0.0f;
260 d->y = (s0->y >= s1->y) ? 1.0f : 0.0f;
261 d->z = (s0->z >= s1->z) ? 1.0f : 0.0f;
262 d->w = (s0->w >= s1->w) ? 1.0f : 0.0f;
263 PSTRACE(("executing sge: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
264 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
267 static void pshader_slt(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
268 d->x = (s0->x < s1->x) ? 1.0f : 0.0f;
269 d->y = (s0->y < s1->y) ? 1.0f : 0.0f;
270 d->z = (s0->z < s1->z) ? 1.0f : 0.0f;
271 d->w = (s0->w < s1->w) ? 1.0f : 0.0f;
272 PSTRACE(("executing slt: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
273 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
276 static void pshader_sub(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
277 d->x = s0->x - s1->x;
278 d->y = s0->y - s1->y;
279 d->z = s0->z - s1->z;
280 d->w = s0->w - s1->w;
281 PSTRACE(("executing sub: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
282 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
286 * Version 1.1 specific
289 static void pshader_exp(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
290 d->x = d->y = d->z = d->w = powf(2.0f, s0->w);
291 PSTRACE(("executing exp: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
292 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
295 static void pshader_log(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
296 float tmp_f = fabsf(s0->w);
297 d->x = d->y = d->z = d->w = (0.0f != tmp_f) ? logf(tmp_f) / logf(2.0f) : -HUGE_VAL;
298 PSTRACE(("executing log: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
299 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
302 static void pshader_frc(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
303 d->x = s0->x - floorf(s0->x);
304 d->y = s0->y - floorf(s0->y);
307 PSTRACE(("executing frc: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
308 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
311 typedef FLOAT D3DMATRIX44[4][4];
312 typedef FLOAT D3DMATRIX43[4][3];
313 typedef FLOAT D3DMATRIX34[3][4];
314 typedef FLOAT D3DMATRIX33[3][3];
315 typedef FLOAT D3DMATRIX23[2][3];
317 static void pshader_m4x4(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, /*WINED3DSHADERVECTOR* mat1*/ D3DMATRIX44 mat) {
319 * Buggy CODE: here only if cast not work for copy/paste
320 WINED3DSHADERVECTOR* mat2 = mat1 + 1;
321 WINED3DSHADERVECTOR* mat3 = mat1 + 2;
322 WINED3DSHADERVECTOR* mat4 = mat1 + 3;
323 d->x = mat1->x * s0->x + mat2->x * s0->y + mat3->x * s0->z + mat4->x * s0->w;
324 d->y = mat1->y * s0->x + mat2->y * s0->y + mat3->y * s0->z + mat4->y * s0->w;
325 d->z = mat1->z * s0->x + mat2->z * s0->y + mat3->z * s0->z + mat4->z * s0->w;
326 d->w = mat1->w * s0->x + mat2->w * s0->y + mat3->w * s0->z + mat4->w * s0->w;
328 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
329 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
330 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
331 d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z + mat[3][3] * s0->w;
332 PSTRACE(("executing m4x4(1): mat=(%f, %f, %f, %f) s0=(%f) d=(%f) \n", mat[0][0], mat[0][1], mat[0][2], mat[0][3], s0->x, d->x));
333 PSTRACE(("executing m4x4(2): mat=(%f, %f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], mat[1][3], s0->y, d->y));
334 PSTRACE(("executing m4x4(3): mat=(%f, %f, %f, %f) X (%f) = (%f) \n", mat[2][0], mat[2][1], mat[2][2], mat[2][3], s0->z, d->z));
335 PSTRACE(("executing m4x4(4): mat=(%f, %f, %f, %f) (%f) (%f) \n", mat[3][0], mat[3][1], mat[3][2], mat[3][3], s0->w, d->w));
338 static void pshader_m4x3(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, D3DMATRIX34 mat) {
339 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z + mat[0][3] * s0->w;
340 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z + mat[1][3] * s0->w;
341 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z + mat[2][3] * s0->w;
343 PSTRACE(("executing m4x3(1): mat=(%f, %f, %f, %f) s0=(%f) d=(%f) \n", mat[0][0], mat[0][1], mat[0][2], mat[0][3], s0->x, d->x));
344 PSTRACE(("executing m4x3(2): mat=(%f, %f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], mat[1][3], s0->y, d->y));
345 PSTRACE(("executing m4x3(3): mat=(%f, %f, %f, %f) X (%f) = (%f) \n", mat[2][0], mat[2][1], mat[2][2], mat[2][3], s0->z, d->z));
346 PSTRACE(("executing m4x3(4): (%f) (%f) \n", s0->w, d->w));
349 static void pshader_m3x4(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, D3DMATRIX43 mat) {
350 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
351 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
352 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
353 d->w = mat[3][0] * s0->x + mat[3][1] * s0->y + mat[3][2] * s0->z;
354 PSTRACE(("executing m3x4(1): mat=(%f, %f, %f) s0=(%f) d=(%f) \n", mat[0][0], mat[0][1], mat[0][2], s0->x, d->x));
355 PSTRACE(("executing m3x4(2): mat=(%f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], s0->y, d->y));
356 PSTRACE(("executing m3x4(3): mat=(%f, %f, %f) X (%f) = (%f) \n", mat[2][0], mat[2][1], mat[2][2], s0->z, d->z));
357 PSTRACE(("executing m3x4(4): mat=(%f, %f, %f) (%f) (%f) \n", mat[3][0], mat[3][1], mat[3][2], s0->w, d->w));
360 static void pshader_m3x3(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, D3DMATRIX33 mat) {
361 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
362 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
363 d->z = mat[2][0] * s0->x + mat[2][1] * s0->y + mat[2][2] * s0->z;
365 PSTRACE(("executing m3x3(1): mat=(%f, %f, %f) s0=(%f) d=(%f) \n", mat[0][0], mat[0][1], mat[0][2], s0->x, d->x));
366 PSTRACE(("executing m3x3(2): mat=(%f, %f, %f) (%f) (%f) \n", mat[1][0], mat[1][1], mat[1][2], s0->y, d->y));
367 PSTRACE(("executing m3x3(3): mat=(%f, %f, %f) X (%f) = (%f) \n", mat[2][0], mat[2][1], mat[2][2], s0->z, d->z));
368 PSTRACE(("executing m3x3(4): (%f) \n", d->w));
371 static void pshader_m3x2(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, D3DMATRIX23 mat) {
373 d->x = mat[0][0] * s0->x + mat[0][1] * s0->y + mat[0][2] * s0->z;
374 d->y = mat[1][0] * s0->x + mat[1][1] * s0->y + mat[1][2] * s0->z;
380 * Version 2.0 specific
382 static void pshader_lrp(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1, WINED3DSHADERVECTOR* s2) {
383 d->x = s0->x * (s1->x - s2->x) + s2->x;
384 d->y = s0->y * (s1->y - s2->y) + s2->y;
385 d->z = s0->z * (s1->z - s2->z) + s2->z;
386 d->w = s0->w * (s1->w - s2->w) + s2->w;
389 static void pshader_crs(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
390 d->x = s0->y * s1->z - s0->z * s1->y;
391 d->y = s0->z * s1->x - s0->x * s1->z;
392 d->z = s0->x * s1->y - s0->y * s1->x;
393 d->w = 0.9f; /* w is undefined, so set it to something safeish */
395 PSTRACE(("executing crs: s0=(%f, %f, %f, %f) s1=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
396 s0->x, s0->y, s0->z, s0->w, s1->x, s1->y, s1->z, s1->w, d->x, d->y, d->z, d->w));
399 static void pshader_abs(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
404 PSTRACE(("executing abs: s0=(%f, %f, %f, %f) => d=(%f, %f, %f, %f)\n",
405 s0->x, s0->y, s0->z, s0->w, d->x, d->y, d->z, d->w));
409 static void pshader_texcoord(WINED3DSHADERVECTOR* d) {
413 static void pshader_texkill(WINED3DSHADERVECTOR* d) {
417 static void pshader_tex(WINED3DSHADERVECTOR* d) {
420 static void pshader_texld(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
424 static void pshader_texbem(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
428 static void pshader_texbeml(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
432 static void pshader_texreg2ar(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
436 static void pshader_texreg2gb(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
440 static void pshader_texm3x2pad(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
444 static void pshader_texm3x2tex(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
448 static void pshader_texm3x3tex(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
452 static void pshader_texm3x3pad(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
456 static void pshader_texm3x3diff(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
460 static void pshader_texm3x3spec(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
464 static void pshader_texm3x3vspec(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
468 static void pshader_cnd(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1, WINED3DSHADERVECTOR* s2) {
472 /* Def is C[n] = {n.nf, n.nf, n.nf, n.nf} */
473 static void pshader_def(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1, WINED3DSHADERVECTOR* s2, WINED3DSHADERVECTOR* s3) {
477 static void pshader_texreg2rgb(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
481 static void pshader_texdp3tex(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
485 static void pshader_texm3x2depth(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
489 static void pshader_texdp3(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
493 static void pshader_texm3x3(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
497 static void pshader_texdepth(WINED3DSHADERVECTOR* d) {
501 static void pshader_cmp(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1, WINED3DSHADERVECTOR* s2) {
505 static void pshader_bem(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
509 static void pshader_call(WINED3DSHADERVECTOR* d) {
513 static void pshader_callnz(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
517 static void pshader_loop(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
521 static void pshader_ret(void) {
525 static void pshader_endloop(void) {
529 static void pshader_dcl(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
533 static void pshader_pow(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
537 static void pshader_nrm(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
541 static void pshader_sincos3(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
545 static void pshader_sincos2(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1, WINED3DSHADERVECTOR* s2) {
549 static void pshader_rep(WINED3DSHADERVECTOR* d) {
553 static void pshader_endrep(void) {
557 static void pshader_if(WINED3DSHADERVECTOR* d) {
561 static void pshader_ifc(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
565 static void pshader_else(void) {
569 static void pshader_label(WINED3DSHADERVECTOR* d) {
573 static void pshader_endif(void) {
577 static void pshader_break(void) {
581 static void pshader_breakc(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0) {
585 static void pshader_breakp(WINED3DSHADERVECTOR* d) {
589 static void pshader_defb(WINED3DSHADERVECTOR* d) {
593 static void pshader_defi(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1, WINED3DSHADERVECTOR* s2, WINED3DSHADERVECTOR* s3) {
597 static void pshader_dp2add(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1, WINED3DSHADERVECTOR* s2) {
601 static void pshader_dsx(WINED3DSHADERVECTOR* d) {
605 static void pshader_dsy(WINED3DSHADERVECTOR* d) {
609 static void pshader_texldd(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1, WINED3DSHADERVECTOR* s2, WINED3DSHADERVECTOR* s3) {
613 static void pshader_setp(WINED3DSHADERVECTOR* d, WINED3DSHADERVECTOR* s0, WINED3DSHADERVECTOR* s1) {
617 static void pshader_texldl(WINED3DSHADERVECTOR* d) {
621 CONST SHADER_OPCODE IWineD3DPixelShaderImpl_shader_ins[] = {
624 {WINED3DSIO_NOP, "nop", "NOP", 0, 0, pshader_nop, pshader_hw_map2gl, NULL, 0, 0},
625 {WINED3DSIO_MOV, "mov", "MOV", 1, 2, pshader_mov, pshader_hw_map2gl, shader_glsl_mov, 0, 0},
626 {WINED3DSIO_ADD, "add", "ADD", 1, 3, pshader_add, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
627 {WINED3DSIO_SUB, "sub", "SUB", 1, 3, pshader_sub, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
628 {WINED3DSIO_MAD, "mad", "MAD", 1, 4, pshader_mad, pshader_hw_map2gl, shader_glsl_mad, 0, 0},
629 {WINED3DSIO_MUL, "mul", "MUL", 1, 3, pshader_mul, pshader_hw_map2gl, shader_glsl_arith, 0, 0},
630 {WINED3DSIO_RCP, "rcp", "RCP", 1, 2, pshader_rcp, pshader_hw_map2gl, shader_glsl_rcp, 0, 0},
631 {WINED3DSIO_RSQ, "rsq", "RSQ", 1, 2, pshader_rsq, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
632 {WINED3DSIO_DP3, "dp3", "DP3", 1, 3, pshader_dp3, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
633 {WINED3DSIO_DP4, "dp4", "DP4", 1, 3, pshader_dp4, pshader_hw_map2gl, shader_glsl_dot, 0, 0},
634 {WINED3DSIO_MIN, "min", "MIN", 1, 3, pshader_min, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
635 {WINED3DSIO_MAX, "max", "MAX", 1, 3, pshader_max, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
636 {WINED3DSIO_SLT, "slt", "SLT", 1, 3, pshader_slt, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
637 {WINED3DSIO_SGE, "sge", "SGE", 1, 3, pshader_sge, pshader_hw_map2gl, shader_glsl_compare, 0, 0},
638 {WINED3DSIO_ABS, "abs", "ABS", 1, 2, pshader_abs, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
639 {WINED3DSIO_EXP, "exp", "EX2", 1, 2, pshader_exp, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
640 {WINED3DSIO_LOG, "log", "LG2", 1, 2, pshader_log, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
641 {WINED3DSIO_EXPP, "expp", "EXP", 1, 2, pshader_expp, pshader_hw_map2gl, shader_glsl_expp, 0, 0},
642 {WINED3DSIO_LOGP, "logp", "LOG", 1, 2, pshader_logp, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
643 {WINED3DSIO_DST, "dst", "DST", 1, 3, pshader_dst, pshader_hw_map2gl, shader_glsl_dst, 0, 0},
644 {WINED3DSIO_LRP, "lrp", "LRP", 1, 4, pshader_lrp, pshader_hw_map2gl, shader_glsl_lrp, 0, 0},
645 {WINED3DSIO_FRC, "frc", "FRC", 1, 2, pshader_frc, pshader_hw_map2gl, shader_glsl_map2gl, 0, 0},
646 {WINED3DSIO_CND, "cnd", NULL, 1, 4, pshader_cnd, pshader_hw_cnd, shader_glsl_cnd, WINED3DPS_VERSION(1,1), WINED3DPS_VERSION(1,4)},
647 {WINED3DSIO_CMP, "cmp", NULL, 1, 4, pshader_cmp, pshader_hw_cmp, shader_glsl_cmp, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(3,0)},
648 {WINED3DSIO_POW, "pow", "POW", 1, 3, pshader_pow, NULL, shader_glsl_map2gl, 0, 0},
649 {WINED3DSIO_CRS, "crs", "XPS", 1, 3, pshader_crs, NULL, shader_glsl_cross, 0, 0},
650 /* TODO: xyz normalise can be performed as VS_ARB using one temporary register,
653 MUL vec.xyz, vec, tmp;
654 but I think this is better because it accounts for w properly.
660 {WINED3DSIO_NRM, "nrm", NULL, 1, 2, pshader_nrm, NULL, shader_glsl_map2gl, 0, 0},
661 {WINED3DSIO_SINCOS, "sincos", NULL, 1, 4, pshader_sincos2, NULL, shader_glsl_sincos, WINED3DPS_VERSION(2,0), WINED3DPS_VERSION(2,1)},
662 {WINED3DSIO_SINCOS, "sincos", NULL, 1, 2, pshader_sincos3, NULL, shader_glsl_sincos, WINED3DPS_VERSION(3,0), -1},
663 /* TODO: dp2add can be made out of multiple instuctions */
664 {WINED3DSIO_DP2ADD, "dp2add", GLNAME_REQUIRE_GLSL, 1, 4, pshader_dp2add, NULL, pshader_glsl_dp2add, WINED3DPS_VERSION(2,0), -1},
667 {WINED3DSIO_M4x4, "m4x4", "undefined", 1, 3, pshader_m4x4, NULL, shader_glsl_mnxn, 0, 0},
668 {WINED3DSIO_M4x3, "m4x3", "undefined", 1, 3, pshader_m4x3, NULL, shader_glsl_mnxn, 0, 0},
669 {WINED3DSIO_M3x4, "m3x4", "undefined", 1, 3, pshader_m3x4, NULL, shader_glsl_mnxn, 0, 0},
670 {WINED3DSIO_M3x3, "m3x3", "undefined", 1, 3, pshader_m3x3, NULL, shader_glsl_mnxn, 0, 0},
671 {WINED3DSIO_M3x2, "m3x2", "undefined", 1, 3, pshader_m3x2, NULL, shader_glsl_mnxn, 0, 0},
673 /* Register declarations */
674 {WINED3DSIO_DCL, "dcl", NULL, 0, 2, pshader_dcl, NULL, NULL, 0, 0},
676 /* Flow control - requires GLSL or software shaders */
677 {WINED3DSIO_REP , "rep", NULL, 0, 1, pshader_rep, NULL, shader_glsl_rep, WINED3DPS_VERSION(2,1), -1},
678 {WINED3DSIO_ENDREP, "endrep", NULL, 0, 0, pshader_endrep, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
679 {WINED3DSIO_IF, "if", NULL, 0, 1, pshader_if, NULL, shader_glsl_if, WINED3DPS_VERSION(2,1), -1},
680 {WINED3DSIO_IFC, "ifc", NULL, 0, 2, pshader_ifc, NULL, shader_glsl_ifc, WINED3DPS_VERSION(2,1), -1},
681 {WINED3DSIO_ELSE, "else", NULL, 0, 0, pshader_else, NULL, shader_glsl_else, WINED3DPS_VERSION(2,1), -1},
682 {WINED3DSIO_ENDIF, "endif", NULL, 0, 0, pshader_endif, NULL, shader_glsl_end, WINED3DPS_VERSION(2,1), -1},
683 {WINED3DSIO_BREAK, "break", NULL, 0, 0, pshader_break, NULL, shader_glsl_break, WINED3DPS_VERSION(2,1), -1},
684 {WINED3DSIO_BREAKC, "breakc", NULL, 0, 2, pshader_breakc, NULL, shader_glsl_breakc, WINED3DPS_VERSION(2,1), -1},
685 {WINED3DSIO_BREAKP, "breakp", GLNAME_REQUIRE_GLSL, 0, 1, pshader_breakp, NULL, NULL, 0, 0},
686 {WINED3DSIO_CALL, "call", NULL, 0, 1, pshader_call, NULL, shader_glsl_call, WINED3DPS_VERSION(2,1), -1},
687 {WINED3DSIO_CALLNZ, "callnz", NULL, 0, 2, pshader_callnz, NULL, shader_glsl_callnz, WINED3DPS_VERSION(2,1), -1},
688 {WINED3DSIO_LOOP, "loop", NULL, 0, 2, pshader_loop, NULL, shader_glsl_loop, WINED3DPS_VERSION(3,0), -1},
689 {WINED3DSIO_RET, "ret", NULL, 0, 0, pshader_ret, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
690 {WINED3DSIO_ENDLOOP, "endloop", NULL, 0, 0, pshader_endloop, NULL, shader_glsl_end, WINED3DPS_VERSION(3,0), -1},
691 {WINED3DSIO_LABEL, "label", NULL, 0, 1, pshader_label, NULL, shader_glsl_label, WINED3DPS_VERSION(2,1), -1},
693 /* Constant definitions */
694 {WINED3DSIO_DEF, "def", "undefined", 1, 5, pshader_def, NULL, NULL, 0, 0},
695 {WINED3DSIO_DEFB, "defb", GLNAME_REQUIRE_GLSL, 1, 2, pshader_defb, NULL, NULL, 0, 0},
696 {WINED3DSIO_DEFI, "defi", GLNAME_REQUIRE_GLSL, 1, 5, pshader_defi, NULL, NULL, 0, 0},
699 {WINED3DSIO_TEXCOORD, "texcoord", "undefined", 1, 1, pshader_texcoord, pshader_hw_texcoord, pshader_glsl_texcoord, 0, WINED3DPS_VERSION(1,3)},
700 {WINED3DSIO_TEXCOORD, "texcrd", "undefined", 1, 2, pshader_texcoord, pshader_hw_texcoord, pshader_glsl_texcoord, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
701 {WINED3DSIO_TEXKILL, "texkill", "KIL", 1, 1, pshader_texkill, pshader_hw_map2gl, pshader_glsl_texkill, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(3,0)},
702 {WINED3DSIO_TEX, "tex", "undefined", 1, 1, pshader_tex, pshader_hw_tex, pshader_glsl_tex, 0, WINED3DPS_VERSION(1,3)},
703 {WINED3DSIO_TEX, "texld", "undefined", 1, 2, pshader_texld, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
704 {WINED3DSIO_TEX, "texld", "undefined", 1, 3, pshader_texld, pshader_hw_tex, pshader_glsl_tex, WINED3DPS_VERSION(2,0), -1},
705 {WINED3DSIO_TEXBEM, "texbem", "undefined", 1, 2, pshader_texbem, pshader_hw_texbem, pshader_glsl_texbem, 0, WINED3DPS_VERSION(1,3)},
706 {WINED3DSIO_TEXBEML, "texbeml", GLNAME_REQUIRE_GLSL, 1, 2, pshader_texbeml, NULL, NULL, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
707 {WINED3DSIO_TEXREG2AR,"texreg2ar","undefined", 1, 2, pshader_texreg2ar, pshader_hw_texreg2ar, pshader_glsl_texreg2ar, WINED3DPS_VERSION(1,1), WINED3DPS_VERSION(1,3)},
708 {WINED3DSIO_TEXREG2GB,"texreg2gb","undefined", 1, 2, pshader_texreg2gb, pshader_hw_texreg2gb, pshader_glsl_texreg2gb, WINED3DPS_VERSION(1,1), WINED3DPS_VERSION(1,3)},
709 {WINED3DSIO_TEXREG2RGB, "texreg2rgb", GLNAME_REQUIRE_GLSL, 1, 2, pshader_texreg2rgb, NULL, pshader_glsl_texreg2rgb, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
710 {WINED3DSIO_TEXM3x2PAD, "texm3x2pad", "undefined", 1, 2, pshader_texm3x2pad, pshader_hw_texm3x2pad, pshader_glsl_texm3x2pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
711 {WINED3DSIO_TEXM3x2TEX, "texm3x2tex", "undefined", 1, 2, pshader_texm3x2tex, pshader_hw_texm3x2tex, pshader_glsl_texm3x2tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
712 {WINED3DSIO_TEXM3x3PAD, "texm3x3pad", "undefined", 1, 2, pshader_texm3x3pad, pshader_hw_texm3x3pad, pshader_glsl_texm3x3pad, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
713 {WINED3DSIO_TEXM3x3DIFF, "texm3x3diff", GLNAME_REQUIRE_GLSL, 1, 2, pshader_texm3x3diff, NULL, NULL, WINED3DPS_VERSION(0,0), WINED3DPS_VERSION(0,0)},
714 {WINED3DSIO_TEXM3x3SPEC, "texm3x3spec", "undefined", 1, 3, pshader_texm3x3spec, pshader_hw_texm3x3spec, pshader_glsl_texm3x3spec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
715 {WINED3DSIO_TEXM3x3VSPEC, "texm3x3vspec", "undefined", 1, 2, pshader_texm3x3vspec, pshader_hw_texm3x3vspec, pshader_glsl_texm3x3vspec, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
716 {WINED3DSIO_TEXM3x3TEX, "texm3x3tex", "undefined", 1, 2, pshader_texm3x3tex, pshader_hw_texm3x3tex, pshader_glsl_texm3x3tex, WINED3DPS_VERSION(1,0), WINED3DPS_VERSION(1,3)},
717 {WINED3DSIO_TEXDP3TEX, "texdp3tex", GLNAME_REQUIRE_GLSL, 1, 2, pshader_texdp3tex, NULL, pshader_glsl_texdp3tex, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
718 {WINED3DSIO_TEXM3x2DEPTH, "texm3x2depth", GLNAME_REQUIRE_GLSL, 1, 2, pshader_texm3x2depth, NULL, pshader_glsl_texm3x2depth, WINED3DPS_VERSION(1,3), WINED3DPS_VERSION(1,3)},
719 {WINED3DSIO_TEXDP3, "texdp3", GLNAME_REQUIRE_GLSL, 1, 2, pshader_texdp3, NULL, pshader_glsl_texdp3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
720 {WINED3DSIO_TEXM3x3, "texm3x3", GLNAME_REQUIRE_GLSL, 1, 2, pshader_texm3x3, NULL, pshader_glsl_texm3x3, WINED3DPS_VERSION(1,2), WINED3DPS_VERSION(1,3)},
721 {WINED3DSIO_TEXDEPTH, "texdepth", GLNAME_REQUIRE_GLSL, 1, 1, pshader_texdepth, NULL, pshader_glsl_texdepth, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
722 {WINED3DSIO_BEM, "bem", GLNAME_REQUIRE_GLSL, 1, 3, pshader_bem, NULL, NULL, WINED3DPS_VERSION(1,4), WINED3DPS_VERSION(1,4)},
723 {WINED3DSIO_DSX, "dsx", GLNAME_REQUIRE_GLSL, 1, 2, pshader_dsx, NULL, NULL, 0, 0},
724 {WINED3DSIO_DSY, "dsy", GLNAME_REQUIRE_GLSL, 1, 2, pshader_dsy, NULL, NULL, 0, 0},
725 {WINED3DSIO_TEXLDD, "texldd", GLNAME_REQUIRE_GLSL, 1, 5, pshader_texldd, NULL, NULL, WINED3DPS_VERSION(2,1), -1},
726 {WINED3DSIO_SETP, "setp", GLNAME_REQUIRE_GLSL, 1, 3, pshader_setp, NULL, NULL, 0, 0},
727 {WINED3DSIO_TEXLDL, "texldl", GLNAME_REQUIRE_GLSL, 1, 2, pshader_texldl, NULL, NULL, 0, 0},
728 {WINED3DSIO_PHASE, "phase", GLNAME_REQUIRE_GLSL, 0, 0, pshader_nop, NULL, NULL, 0, 0},
729 {0, NULL, NULL, 0, 0, NULL, NULL, 0, 0}
732 static void pshader_set_limits(
733 IWineD3DPixelShaderImpl *This) {
735 This->baseShader.limits.attributes = 0;
736 This->baseShader.limits.address = 0;
737 This->baseShader.limits.packed_output = 0;
739 switch (This->baseShader.hex_version) {
740 case WINED3DPS_VERSION(1,0):
741 case WINED3DPS_VERSION(1,1):
742 case WINED3DPS_VERSION(1,2):
743 case WINED3DPS_VERSION(1,3):
744 This->baseShader.limits.temporary = 2;
745 This->baseShader.limits.constant_float = 8;
746 This->baseShader.limits.constant_int = 0;
747 This->baseShader.limits.constant_bool = 0;
748 This->baseShader.limits.texcoord = 4;
749 This->baseShader.limits.sampler = 4;
750 This->baseShader.limits.packed_input = 0;
751 This->baseShader.limits.label = 0;
754 case WINED3DPS_VERSION(1,4):
755 This->baseShader.limits.temporary = 6;
756 This->baseShader.limits.constant_float = 8;
757 This->baseShader.limits.constant_int = 0;
758 This->baseShader.limits.constant_bool = 0;
759 This->baseShader.limits.texcoord = 6;
760 This->baseShader.limits.sampler = 6;
761 This->baseShader.limits.packed_input = 0;
762 This->baseShader.limits.label = 0;
765 /* FIXME: temporaries must match D3DPSHADERCAPS2_0.NumTemps */
766 case WINED3DPS_VERSION(2,0):
767 This->baseShader.limits.temporary = 32;
768 This->baseShader.limits.constant_float = 32;
769 This->baseShader.limits.constant_int = 16;
770 This->baseShader.limits.constant_bool = 16;
771 This->baseShader.limits.texcoord = 8;
772 This->baseShader.limits.sampler = 16;
773 This->baseShader.limits.packed_input = 0;
776 case WINED3DPS_VERSION(2,1):
777 This->baseShader.limits.temporary = 32;
778 This->baseShader.limits.constant_float = 32;
779 This->baseShader.limits.constant_int = 16;
780 This->baseShader.limits.constant_bool = 16;
781 This->baseShader.limits.texcoord = 8;
782 This->baseShader.limits.sampler = 16;
783 This->baseShader.limits.packed_input = 0;
784 This->baseShader.limits.label = 16;
787 case WINED3DPS_VERSION(3,0):
788 This->baseShader.limits.temporary = 32;
789 This->baseShader.limits.constant_float = 224;
790 This->baseShader.limits.constant_int = 16;
791 This->baseShader.limits.constant_bool = 16;
792 This->baseShader.limits.texcoord = 0;
793 This->baseShader.limits.sampler = 16;
794 This->baseShader.limits.packed_input = 12;
795 This->baseShader.limits.label = 16; /* FIXME: 2048 */
798 default: This->baseShader.limits.temporary = 32;
799 This->baseShader.limits.constant_float = 32;
800 This->baseShader.limits.constant_int = 16;
801 This->baseShader.limits.constant_bool = 16;
802 This->baseShader.limits.texcoord = 8;
803 This->baseShader.limits.sampler = 16;
804 This->baseShader.limits.packed_input = 0;
805 This->baseShader.limits.label = 0;
806 FIXME("Unrecognized pixel shader version %#x\n",
807 This->baseShader.hex_version);
811 /** Generate a pixel shader string using either GL_FRAGMENT_PROGRAM_ARB
812 or GLSL and send it to the card */
813 inline static VOID IWineD3DPixelShaderImpl_GenerateShader(
814 IWineD3DPixelShader *iface,
815 shader_reg_maps* reg_maps,
816 CONST DWORD *pFunction) {
818 IWineD3DPixelShaderImpl *This = (IWineD3DPixelShaderImpl *)iface;
819 SHADER_BUFFER buffer;
821 #if 0 /* FIXME: Use the buffer that is held by the device, this is ok since fixups will be skipped for software shaders
822 it also requires entering a critical section but cuts down the runtime footprint of wined3d and any memory fragmentation that may occur... */
823 if (This->device->fixupVertexBufferSize < SHADER_PGMSIZE) {
824 HeapFree(GetProcessHeap(), 0, This->fixupVertexBuffer);
825 This->fixupVertexBuffer = HeapAlloc(GetProcessHeap() , 0, SHADER_PGMSIZE);
826 This->fixupVertexBufferSize = PGMSIZE;
827 This->fixupVertexBuffer[0] = 0;
829 buffer.buffer = This->device->fixupVertexBuffer;
831 buffer.buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, SHADER_PGMSIZE);
835 buffer.newline = TRUE;
837 if (This->baseShader.shader_mode == SHADER_GLSL) {
839 /* Create the hw GLSL shader object and assign it as the baseShader.prgId */
840 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
842 if (GL_SUPPORT(ARB_DRAW_BUFFERS)) {
843 shader_addline(&buffer, "#extension GL_ARB_draw_buffers : enable\n");
846 /* Base Declarations */
847 shader_generate_glsl_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
849 /* Pack 3.0 inputs */
850 if (This->baseShader.hex_version >= WINED3DPS_VERSION(3,0))
851 pshader_glsl_input_pack(&buffer, This->semantics_in);
853 /* Base Shader Body */
854 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
856 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
857 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0)) {
858 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
859 if(GL_SUPPORT(ARB_DRAW_BUFFERS))
860 shader_addline(&buffer, "gl_FragData[0] = R0;\n");
862 shader_addline(&buffer, "gl_FragColor = R0;\n");
864 shader_addline(&buffer, "}\n");
866 TRACE("Compiling shader object %u\n", shader_obj);
867 GL_EXTCALL(glShaderSourceARB(shader_obj, 1, (const char**)&buffer.buffer, NULL));
868 GL_EXTCALL(glCompileShaderARB(shader_obj));
869 print_glsl_info_log(&GLINFO_LOCATION, shader_obj);
871 /* Store the shader object */
872 This->baseShader.prgId = shader_obj;
874 } else if (This->baseShader.shader_mode == SHADER_ARB) {
875 /* Create the hw ARB shader */
876 shader_addline(&buffer, "!!ARBfp1.0\n");
878 shader_addline(&buffer, "TEMP TMP;\n"); /* Used in matrix ops */
879 shader_addline(&buffer, "TEMP TMP2;\n"); /* Used in matrix ops */
880 shader_addline(&buffer, "TEMP TA;\n"); /* Used for modifiers */
881 shader_addline(&buffer, "TEMP TB;\n"); /* Used for modifiers */
882 shader_addline(&buffer, "TEMP TC;\n"); /* Used for modifiers */
883 shader_addline(&buffer, "PARAM coefdiv = { 0.5, 0.25, 0.125, 0.0625 };\n");
884 shader_addline(&buffer, "PARAM coefmul = { 2, 4, 8, 16 };\n");
885 shader_addline(&buffer, "PARAM one = { 1.0, 1.0, 1.0, 1.0 };\n");
887 /* Base Declarations */
888 shader_generate_arb_declarations( (IWineD3DBaseShader*) This, reg_maps, &buffer, &GLINFO_LOCATION);
890 /* Base Shader Body */
891 shader_generate_main( (IWineD3DBaseShader*) This, &buffer, reg_maps, pFunction);
893 if (This->baseShader.hex_version < WINED3DPS_VERSION(2,0))
894 shader_addline(&buffer, "MOV result.color, R0;\n");
895 shader_addline(&buffer, "END\n");
897 /* TODO: change to resource.glObjectHandle or something like that */
898 GL_EXTCALL(glGenProgramsARB(1, &This->baseShader.prgId));
900 TRACE("Creating a hw pixel shader, prg=%d\n", This->baseShader.prgId);
901 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, This->baseShader.prgId));
903 TRACE("Created hw pixel shader, prg=%d\n", This->baseShader.prgId);
904 /* Create the program and check for errors */
905 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
906 buffer.bsize, buffer.buffer));
908 if (glGetError() == GL_INVALID_OPERATION) {
910 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
911 FIXME("HW PixelShader Error at position %d: %s\n",
912 errPos, debugstr_a((const char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
913 This->baseShader.prgId = -1;
917 #if 1 /* if were using the data buffer of device then we don't need to free it */
918 HeapFree(GetProcessHeap(), 0, buffer.buffer);
922 static HRESULT WINAPI IWineD3DPixelShaderImpl_SetFunction(IWineD3DPixelShader *iface, CONST DWORD *pFunction) {
924 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
925 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl *) This->baseShader.device;
927 TRACE("(%p) : pFunction %p\n", iface, pFunction);
929 /* First pass: trace shader */
930 shader_trace_init((IWineD3DBaseShader*) This, pFunction);
931 pshader_set_limits(This);
933 /* Initialize immediate constant lists */
934 list_init(&This->baseShader.constantsF);
935 list_init(&This->baseShader.constantsB);
936 list_init(&This->baseShader.constantsI);
938 This->baseShader.shader_mode = deviceImpl->ps_selected_mode;
940 TRACE("(%p) : Copying the function\n", This);
941 if (NULL != pFunction) {
944 function = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->baseShader.functionLength);
945 if (!function) return E_OUTOFMEMORY;
946 memcpy(function, pFunction, This->baseShader.functionLength);
947 This->baseShader.function = function;
949 This->baseShader.function = NULL;
955 static HRESULT WINAPI IWineD3DPixelShaderImpl_CompileShader(IWineD3DPixelShader *iface) {
957 IWineD3DPixelShaderImpl *This =(IWineD3DPixelShaderImpl *)iface;
958 IWineD3DDeviceImpl *deviceImpl = (IWineD3DDeviceImpl*) This->baseShader.device;
959 CONST DWORD *function = This->baseShader.function;
960 shader_reg_maps *reg_maps = &This->baseShader.reg_maps;
963 TRACE("(%p) : function %p\n", iface, function);
965 /* We're already compiled. */
966 if (This->baseShader.is_compiled) return WINED3D_OK;
968 /* We don't need to compile */
969 if (!function || This->baseShader.shader_mode == SHADER_SW) {
970 This->baseShader.is_compiled = TRUE;
974 /* Second pass: figure out which registers are used, what the semantics are, etc.. */
975 memset(reg_maps, 0, sizeof(shader_reg_maps));
976 hr = shader_get_registers_used((IWineD3DBaseShader*) This, reg_maps,
977 This->semantics_in, NULL, This->baseShader.function, deviceImpl->stateBlock);
978 if (hr != WINED3D_OK) return hr;
979 /* FIXME: validate reg_maps against OpenGL */
981 /* Generate the HW shader */
982 TRACE("(%p) : Generating hardware program\n", This);
983 IWineD3DPixelShaderImpl_GenerateShader(iface, &This->baseShader.reg_maps, function);
985 This->baseShader.is_compiled = TRUE;
990 const IWineD3DPixelShaderVtbl IWineD3DPixelShader_Vtbl =
992 /*** IUnknown methods ***/
993 IWineD3DPixelShaderImpl_QueryInterface,
994 IWineD3DPixelShaderImpl_AddRef,
995 IWineD3DPixelShaderImpl_Release,
996 /*** IWineD3DBase methods ***/
997 IWineD3DPixelShaderImpl_GetParent,
998 /*** IWineD3DBaseShader methods ***/
999 IWineD3DPixelShaderImpl_SetFunction,
1000 IWineD3DPixelShaderImpl_CompileShader,
1001 /*** IWineD3DPixelShader methods ***/
1002 IWineD3DPixelShaderImpl_GetDevice,
1003 IWineD3DPixelShaderImpl_GetFunction