Fix warnings generated with -Wsign-compare.
[wine] / dlls / d3d9 / pixelshader.c
1 /*
2  * IDirect3DPixelShader9 implementation
3  *
4  * Copyright 2002-2003 Jason Edmeades
5  *                     Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <stdarg.h>
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "wingdi.h"
32 #include "wine/debug.h"
33
34 #include "d3d9_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
37
38 /* IDirect3DPixelShader9 IUnknown parts follow: */
39 HRESULT WINAPI IDirect3DPixelShader9Impl_QueryInterface(LPDIRECT3DPIXELSHADER9 iface, REFIID riid, LPVOID* ppobj) {
40     ICOM_THIS(IDirect3DPixelShader9Impl,iface);
41
42     if (IsEqualGUID(riid, &IID_IUnknown)
43         || IsEqualGUID(riid, &IID_IDirect3DPixelShader9)) {
44         IDirect3DPixelShader9Impl_AddRef(iface);
45         *ppobj = This;
46         return D3D_OK;
47     }
48
49     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
50     return E_NOINTERFACE;
51 }
52
53 ULONG WINAPI IDirect3DPixelShader9Impl_AddRef(LPDIRECT3DPIXELSHADER9 iface) {
54     ICOM_THIS(IDirect3DPixelShader9Impl,iface);
55     TRACE("(%p) : AddRef from %ld\n", This, This->ref);
56     return ++(This->ref);
57 }
58
59 ULONG WINAPI IDirect3DPixelShader9Impl_Release(LPDIRECT3DPIXELSHADER9 iface) {
60     ICOM_THIS(IDirect3DPixelShader9Impl,iface);
61     ULONG ref = --This->ref;
62
63     TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
64     if (ref == 0) {
65         HeapFree(GetProcessHeap(), 0, This);
66     }
67     return ref;
68 }
69
70 /* IDirect3DPixelShader9 Interface follow: */
71 HRESULT WINAPI IDirect3DPixelShader9Impl_GetDevice(LPDIRECT3DPIXELSHADER9 iface, IDirect3DDevice9** ppDevice) {
72     ICOM_THIS(IDirect3DPixelShader9Impl,iface);
73     TRACE("(%p) : returning %p\n", This, This->Device);
74     *ppDevice = (LPDIRECT3DDEVICE9) This->Device;
75     IDirect3DDevice9Impl_AddRef(*ppDevice);
76     return D3D_OK;
77 }
78
79 HRESULT WINAPI IDirect3DPixelShader9Impl_GetFunction(LPDIRECT3DPIXELSHADER9 iface, VOID* pData, UINT* pSizeOfData) {
80     ICOM_THIS(IDirect3DPixelShader9Impl,iface);
81     FIXME("(%p): stub\n", This);
82     return D3D_OK;
83 }
84
85
86 ICOM_VTABLE(IDirect3DPixelShader9) Direct3DPixelShader9_Vtbl =
87 {
88     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
89     IDirect3DPixelShader9Impl_QueryInterface,
90     IDirect3DPixelShader9Impl_AddRef,
91     IDirect3DPixelShader9Impl_Release,
92     IDirect3DPixelShader9Impl_GetDevice,
93     IDirect3DPixelShader9Impl_GetFunction
94 };
95
96
97 /* IDirect3DDevice9 IDirect3DPixelShader9 Methods follow:  */
98 HRESULT WINAPI IDirect3DDevice9Impl_CreatePixelShader(LPDIRECT3DDEVICE9 iface, CONST DWORD* pFunction, IDirect3DPixelShader9** ppShader) {
99     ICOM_THIS(IDirect3DDevice9Impl,iface);
100     FIXME("(%p) : stub\n", This);
101     return D3D_OK;
102 }
103
104 HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShader(LPDIRECT3DDEVICE9 iface, IDirect3DPixelShader9* pShader) {
105     ICOM_THIS(IDirect3DDevice9Impl,iface);
106
107     This->UpdateStateBlock->PixelShader = pShader;
108     This->UpdateStateBlock->Changed.pixelShader = TRUE;
109     This->UpdateStateBlock->Set.pixelShader = TRUE;
110     
111     /* Handle recording of state blocks */
112     if (This->isRecordingState) {
113       TRACE("Recording... not performing anything\n");
114       return D3D_OK;
115     }
116     /**
117      * TODO: merge HAL shaders context switching from prototype
118      */
119     return D3D_OK;
120 }
121
122 HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShader(LPDIRECT3DDEVICE9 iface, IDirect3DPixelShader9** ppShader) {
123     ICOM_THIS(IDirect3DDevice9Impl,iface);
124     TRACE("(%p) : GetPixelShader returning %p\n", This, This->StateBlock->PixelShader);
125     *ppShader = This->StateBlock->PixelShader;
126     IDirect3DPixelShader9Impl_AddRef(*ppShader);
127     return D3D_OK;
128 }
129
130 HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantF(LPDIRECT3DDEVICE9 iface, UINT Register, CONST float* pConstantData, UINT Vector4fCount) {
131   ICOM_THIS(IDirect3DDevice9Impl,iface);
132
133   if (Register + Vector4fCount > D3D_VSHADER_MAX_CONSTANTS) {
134     ERR("(%p) : SetPixelShaderConstant C[%u] invalid\n", This, Register);
135     return D3DERR_INVALIDCALL;
136   }
137   if (NULL == pConstantData) {
138     return D3DERR_INVALIDCALL;
139   }
140   if (Vector4fCount > 1) {
141     CONST FLOAT* f = pConstantData;
142     UINT i;
143     TRACE("(%p) : SetPixelShaderConstant C[%u..%u]=\n", This, Register, Register + Vector4fCount - 1);
144     for (i = 0; i < Vector4fCount; ++i) {
145       TRACE("{%f, %f, %f, %f}\n", f[0], f[1], f[2], f[3]);
146       f += 4;
147     }
148   } else { 
149     FLOAT* f = (FLOAT*) pConstantData;
150     TRACE("(%p) : SetPixelShaderConstant, C[%u]={%f, %f, %f, %f}\n", This, Register, f[0], f[1], f[2], f[3]);
151   }
152   This->UpdateStateBlock->Changed.pixelShaderConstant = TRUE;
153   memcpy(&This->UpdateStateBlock->pixelShaderConstantF[Register], pConstantData, Vector4fCount * 4 * sizeof(FLOAT));
154   return D3D_OK;
155 }
156
157 HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantF(LPDIRECT3DDEVICE9 iface, UINT Register, float* pConstantData, UINT Vector4fCount) {
158   ICOM_THIS(IDirect3DDevice9Impl,iface);
159
160   TRACE("(%p) : C[%u] count=%u\n", This, Register, Vector4fCount);
161   if (Register + Vector4fCount > D3D_VSHADER_MAX_CONSTANTS) {
162     return D3DERR_INVALIDCALL;
163   }
164   if (NULL == pConstantData) {
165     return D3DERR_INVALIDCALL;
166   }
167   memcpy(pConstantData, &This->UpdateStateBlock->pixelShaderConstantF[Register], Vector4fCount * 4 * sizeof(FLOAT));
168   return D3D_OK;
169 }
170
171 HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantI(LPDIRECT3DDEVICE9 iface, UINT Register, CONST int* pConstantData, UINT Vector4iCount) {
172   ICOM_THIS(IDirect3DDevice9Impl,iface);
173
174   if (Register + Vector4iCount > D3D_VSHADER_MAX_CONSTANTS) {
175     ERR("(%p) : SetPixelShaderConstantI C[%u] invalid\n", This, Register);
176     return D3DERR_INVALIDCALL;
177   }
178   if (NULL == pConstantData) {
179     return D3DERR_INVALIDCALL;
180   }
181   if (Vector4iCount > 1) {
182     CONST int* f = pConstantData;
183     UINT i;
184     TRACE("(%p) : SetPixelShaderConstantI C[%u..%u]=\n", This, Register, Register + Vector4iCount - 1);
185     for (i = 0; i < Vector4iCount; ++i) {
186       TRACE("{%d, %d, %d, %d}\n", f[0], f[1], f[2], f[3]);
187       f += 4;
188     }
189   } else { 
190     CONST int* f = pConstantData;
191     TRACE("(%p) : SetPixelShaderConstantI, C[%u]={%i, %i, %i, %i}\n", This, Register, f[0], f[1], f[2], f[3]);
192   }
193   This->UpdateStateBlock->Changed.pixelShaderConstant = TRUE;
194   memcpy(&This->UpdateStateBlock->pixelShaderConstantI[Register], pConstantData, Vector4iCount * 4 * sizeof(int));
195   return D3D_OK;
196 }
197
198 HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantI(LPDIRECT3DDEVICE9 iface, UINT Register, int* pConstantData, UINT Vector4iCount) {
199   ICOM_THIS(IDirect3DDevice9Impl,iface);
200
201   TRACE("(%p) : C[%u] count=%u\n", This, Register, Vector4iCount);
202   if (Register + Vector4iCount > D3D_VSHADER_MAX_CONSTANTS) {
203     return D3DERR_INVALIDCALL;
204   }
205   if (NULL == pConstantData) {
206     return D3DERR_INVALIDCALL;
207   }
208   memcpy(pConstantData, &This->UpdateStateBlock->pixelShaderConstantI[Register], Vector4iCount * 4 * sizeof(FLOAT));
209   return D3D_OK;
210 }
211
212 HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantB(LPDIRECT3DDEVICE9 iface, UINT Register, CONST BOOL* pConstantData, UINT BoolCount) {
213   ICOM_THIS(IDirect3DDevice9Impl,iface);
214   UINT i;
215
216   if (Register + BoolCount > D3D_VSHADER_MAX_CONSTANTS) {
217     ERR("(%p) : SetPixelShaderConstantB C[%u] invalid\n", This, Register);
218     return D3DERR_INVALIDCALL;
219   }
220   if (NULL == pConstantData) {
221     return D3DERR_INVALIDCALL;
222   }
223   if (BoolCount > 1) {
224     CONST BOOL* f = pConstantData;
225     TRACE("(%p) : SetPixelShaderConstantB C[%u..%u]=\n", This, Register, Register + BoolCount - 1);
226     for (i = 0; i < BoolCount; ++i) {
227       TRACE("{%u}\n", f[i]);
228     }
229   } else { 
230     CONST BOOL* f = pConstantData;
231     TRACE("(%p) : SetPixelShaderConstantB, C[%u]={%u}\n", This, Register, f[0]);
232   }
233   This->UpdateStateBlock->Changed.pixelShaderConstant = TRUE;
234   for (i = 0; i < BoolCount; ++i) {
235     This->UpdateStateBlock->pixelShaderConstantB[Register] = pConstantData[i];
236   }
237   return D3D_OK;
238 }
239
240 HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantB(LPDIRECT3DDEVICE9 iface, UINT Register, BOOL* pConstantData, UINT BoolCount) {
241     ICOM_THIS(IDirect3DDevice9Impl,iface);
242     FIXME("(%p) : stub\n", This);
243     return D3D_OK;
244 }