Fix CreateProcess to handle lpCommandline better.
[wine/multimedia.git] / graphics / d3dtexture.c
blobc5ca90149768a7c1b3b31f49b80677715c5fb67a
1 /* Direct3D Texture
2 (c) 1998 Lionel ULMER
4 This files contains the implementation of interface Direct3DTexture2. */
7 #include <string.h>
8 #include "config.h"
9 #include "wintypes.h"
10 #include "winerror.h"
11 #include "wine/obj_base.h"
12 #include "heap.h"
13 #include "ddraw.h"
14 #include "d3d.h"
15 #include "debug.h"
17 #include "d3d_private.h"
19 #ifdef HAVE_MESAGL
21 /* Define this if you want to save to a file all the textures used by a game
22 (can be funny to see how they managed to cram all the pictures in
23 texture memory) */
24 #undef TEXTURE_SNOOP
26 static IDirect3DTexture2_VTable texture2_vtable;
27 static IDirect3DTexture_VTable texture_vtable;
29 /*******************************************************************************
30 * Texture2 Creation functions
32 LPDIRECT3DTEXTURE2 d3dtexture2_create(LPDIRECTDRAWSURFACE4 surf)
34 LPDIRECT3DTEXTURE2 mat;
36 mat = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2));
37 mat->ref = 1;
38 mat->lpvtbl = &texture2_vtable;
39 mat->surface = surf;
41 return mat;
44 /*******************************************************************************
45 * Texture Creation functions
47 LPDIRECT3DTEXTURE d3dtexture_create(LPDIRECTDRAWSURFACE4 surf)
49 LPDIRECT3DTEXTURE mat;
51 mat = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture));
52 mat->ref = 1;
53 mat->lpvtbl = (IDirect3DTexture2_VTable*) &texture_vtable;
54 mat->surface = surf;
56 return mat;
60 /*******************************************************************************
61 * IDirect3DTexture2 methods
64 static HRESULT WINAPI IDirect3DTexture2_QueryInterface(LPDIRECT3DTEXTURE2 this,
65 REFIID riid,
66 LPVOID* ppvObj)
68 char xrefiid[50];
70 WINE_StringFromCLSID((LPCLSID)riid,xrefiid);
71 FIXME(ddraw, "(%p)->(%s,%p): stub\n", this, xrefiid,ppvObj);
73 return S_OK;
78 static ULONG WINAPI IDirect3DTexture2_AddRef(LPDIRECT3DTEXTURE2 this)
80 TRACE(ddraw, "(%p)->()incrementing from %lu.\n", this, this->ref );
82 return ++(this->ref);
87 static ULONG WINAPI IDirect3DTexture2_Release(LPDIRECT3DTEXTURE2 this)
89 FIXME( ddraw, "(%p)->() decrementing from %lu.\n", this, this->ref );
91 if (!--(this->ref)) {
92 /* Delete texture from OpenGL */
93 glDeleteTextures(1, &(this->tex_name));
95 /* Release surface */
96 this->surface->lpvtbl->fnRelease(this->surface);
98 HeapFree(GetProcessHeap(),0,this);
99 return 0;
102 return this->ref;
105 /*** IDirect3DTexture methods ***/
106 static HRESULT WINAPI IDirect3DTexture_GetHandle(LPDIRECT3DTEXTURE this,
107 LPDIRECT3DDEVICE lpD3DDevice,
108 LPD3DTEXTUREHANDLE lpHandle)
110 FIXME(ddraw, "(%p)->(%p,%p): stub\n", this, lpD3DDevice, lpHandle);
112 *lpHandle = (DWORD) this;
114 /* Now, bind a new texture */
115 lpD3DDevice->set_context(lpD3DDevice);
116 this->D3Ddevice = (void *) lpD3DDevice;
117 if (this->tex_name == 0)
118 glGenTextures(1, &(this->tex_name));
120 TRACE(ddraw, "OpenGL texture handle is : %d\n", this->tex_name);
122 return D3D_OK;
125 static HRESULT WINAPI IDirect3DTexture_Initialize(LPDIRECT3DTEXTURE this,
126 LPDIRECT3DDEVICE lpD3DDevice,
127 LPDIRECTDRAWSURFACE lpSurface)
129 TRACE(ddraw, "(%p)->(%p,%p)\n", this, lpD3DDevice, lpSurface);
131 return DDERR_ALREADYINITIALIZED;
134 static HRESULT WINAPI IDirect3DTexture_Unload(LPDIRECT3DTEXTURE this)
136 FIXME(ddraw, "(%p)->(): stub\n", this);
138 return D3D_OK;
141 /*** IDirect3DTexture2 methods ***/
142 static HRESULT WINAPI IDirect3DTexture2_GetHandle(LPDIRECT3DTEXTURE2 this,
143 LPDIRECT3DDEVICE2 lpD3DDevice2,
144 LPD3DTEXTUREHANDLE lpHandle)
146 TRACE(ddraw, "(%p)->(%p,%p)\n", this, lpD3DDevice2, lpHandle);
148 /* For 32 bits OSes, handles = pointers */
149 *lpHandle = (DWORD) this;
151 /* Now, bind a new texture */
152 lpD3DDevice2->set_context(lpD3DDevice2);
153 this->D3Ddevice = (void *) lpD3DDevice2;
154 if (this->tex_name == 0)
155 glGenTextures(1, &(this->tex_name));
157 TRACE(ddraw, "OpenGL texture handle is : %d\n", this->tex_name);
159 return D3D_OK;
162 /* Common methods */
163 static HRESULT WINAPI IDirect3DTexture2_PaletteChanged(LPDIRECT3DTEXTURE2 this,
164 DWORD dwStart,
165 DWORD dwCount)
167 FIXME(ddraw, "(%p)->(%8ld,%8ld): stub\n", this, dwStart, dwCount);
169 return D3D_OK;
172 /* NOTE : if you experience crashes in this function, you must have a buggy
173 version of Mesa. See the file d3dtexture.c for a cure */
174 static HRESULT WINAPI IDirect3DTexture2_Load(LPDIRECT3DTEXTURE2 this,
175 LPDIRECT3DTEXTURE2 lpD3DTexture2)
177 DDSURFACEDESC *src_d, *dst_d;
178 TRACE(ddraw, "(%p)->(%p)\n", this, lpD3DTexture2);
180 TRACE(ddraw, "Copied to surface %p, surface %p\n", this->surface, lpD3DTexture2->surface);
182 /* Suppress the ALLOCONLOAD flag */
183 this->surface->s.surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
185 /* Copy one surface on the other */
186 dst_d = &(this->surface->s.surface_desc);
187 src_d = &(lpD3DTexture2->surface->s.surface_desc);
189 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
190 /* Should also check for same pixel format, lPitch, ... */
191 ERR(ddraw, "Error in surface sizes\n");
192 return D3DERR_TEXTURE_LOAD_FAILED;
193 } else {
194 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) this->D3Ddevice; */
195 /* I should put a macro for the calculus of bpp */
196 int bpp = (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
197 1 /* 8 bit of palette index */:
198 src_d->ddpfPixelFormat.x.dwRGBBitCount / 8 /* RGB bits for each colors */ );
199 GLuint current_texture;
201 /* Not sure if this is usefull ! */
202 memcpy(dst_d->y.lpSurface, src_d->y.lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
204 /* Now, load the texture */
205 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
206 glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
208 /* If the GetHandle was not done, get the texture name here */
209 if (this->tex_name == 0)
210 glGenTextures(1, &(this->tex_name));
211 glBindTexture(GL_TEXTURE_2D, this->tex_name);
213 if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
214 /* ****************
215 Paletted Texture
216 **************** */
217 LPDIRECTDRAWPALETTE pal = this->surface->s.palette;
218 BYTE table[256][4];
219 int i;
221 if (pal == NULL) {
222 ERR(ddraw, "Palettized texture Loading with a NULL palette !\n");
223 return D3DERR_TEXTURE_LOAD_FAILED;
226 /* Get the surface's palette */
227 for (i = 0; i < 256; i++) {
228 table[i][0] = pal->palents[i].peRed;
229 table[i][1] = pal->palents[i].peGreen;
230 table[i][2] = pal->palents[i].peBlue;
231 if ((this->surface->s.surface_desc.dwFlags & DDSD_CKSRCBLT) &&
232 (i >= this->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue) &&
233 (i <= this->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue))
234 table[i][3] = 0x00;
235 else
236 table[i][3] = 0xFF;
239 #ifdef TEXTURE_SNOOP
241 FILE *f;
242 char buf[32];
243 int x, y;
245 sprintf(buf, "%d.pnm", this->tex_name);
246 f = fopen(buf, "wb");
247 fprintf(f, "P6\n%d %d\n255\n", src_d->dwWidth, src_d->dwHeight);
248 for (y = 0; y < src_d->dwHeight; y++) {
249 for (x = 0; x < src_d->dwWidth; x++) {
250 unsigned char c = ((unsigned char *) src_d->y.lpSurface)[y * src_d->dwWidth + x];
251 fputc(table[c][0], f);
252 fputc(table[c][1], f);
253 fputc(table[c][2], f);
256 fclose(f);
258 #endif
259 /* Use Paletted Texture Extension */
260 glColorTableEXT(GL_TEXTURE_2D, /* target */
261 GL_RGBA, /* internal format */
262 256, /* table size */
263 GL_RGBA, /* table format */
264 GL_UNSIGNED_BYTE, /* table type */
265 table); /* the color table */
267 glTexImage2D(GL_TEXTURE_2D, /* target */
268 0, /* level */
269 GL_COLOR_INDEX8_EXT, /* internal format */
270 src_d->dwWidth, src_d->dwHeight, /* width, height */
271 0, /* border */
272 GL_COLOR_INDEX, /* texture format */
273 GL_UNSIGNED_BYTE, /* texture type */
274 src_d->y.lpSurface); /* the texture */
275 } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
276 /* ************
277 RGB Textures
278 ************ */
279 if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 8) {
280 /* **********************
281 GL_UNSIGNED_BYTE_3_3_2
282 ********************** */
283 glTexImage2D(GL_TEXTURE_2D,
285 GL_RGB,
286 src_d->dwWidth, src_d->dwHeight,
288 GL_RGB,
289 GL_UNSIGNED_BYTE_3_3_2,
290 src_d->y.lpSurface);
291 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 16) {
292 if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000000) {
293 #ifdef TEXTURE_SNOOP
295 FILE *f;
296 char buf[32];
297 int x, y;
299 sprintf(buf, "%d.pnm", this->tex_name);
300 f = fopen(buf, "wb");
301 fprintf(f, "P6\n%d %d\n255\n", src_d->dwWidth, src_d->dwHeight);
302 for (y = 0; y < src_d->dwHeight; y++) {
303 for (x = 0; x < src_d->dwWidth; x++) {
304 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x];
305 fputc((c & 0xF800) >> 8, f);
306 fputc((c & 0x07E0) >> 3, f);
307 fputc((c & 0x001F) << 3, f);
310 fclose(f);
312 #endif
313 glTexImage2D(GL_TEXTURE_2D,
315 GL_RGB,
316 src_d->dwWidth, src_d->dwHeight,
318 GL_RGB,
319 GL_UNSIGNED_SHORT_5_6_5,
320 src_d->y.lpSurface);
321 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x00000001) {
322 #ifdef TEXTURE_SNOOP
324 FILE *f;
325 char buf[32];
326 int x, y;
328 sprintf(buf, "%d.pnm", this->tex_name);
329 f = fopen(buf, "wb");
330 fprintf(f, "P6\n%d %d\n255\n", src_d->dwWidth, src_d->dwHeight);
331 for (y = 0; y < src_d->dwHeight; y++) {
332 for (x = 0; x < src_d->dwWidth; x++) {
333 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x];
334 fputc((c & 0xF800) >> 8, f);
335 fputc((c & 0x07C0) >> 3, f);
336 fputc((c & 0x003E) << 2, f);
339 fclose(f);
341 #endif
343 glTexImage2D(GL_TEXTURE_2D,
345 GL_RGBA,
346 src_d->dwWidth, src_d->dwHeight,
348 GL_RGBA,
349 GL_UNSIGNED_SHORT_5_5_5_1,
350 src_d->y.lpSurface);
351 } else if (src_d->ddpfPixelFormat.xy.dwRGBAlphaBitMask == 0x0000000F) {
352 glTexImage2D(GL_TEXTURE_2D,
354 GL_RGBA,
355 src_d->dwWidth, src_d->dwHeight,
357 GL_RGBA,
358 GL_UNSIGNED_SHORT_4_4_4_4,
359 src_d->y.lpSurface);
360 } else {
361 ERR(ddraw, "Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
363 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 24) {
364 glTexImage2D(GL_TEXTURE_2D,
366 GL_RGB,
367 src_d->dwWidth, src_d->dwHeight,
369 GL_RGB,
370 GL_UNSIGNED_BYTE,
371 src_d->y.lpSurface);
372 } else if (src_d->ddpfPixelFormat.x.dwRGBBitCount == 32) {
373 glTexImage2D(GL_TEXTURE_2D,
375 GL_RGBA,
376 src_d->dwWidth, src_d->dwHeight,
378 GL_RGBA,
379 GL_UNSIGNED_BYTE,
380 src_d->y.lpSurface);
381 } else {
382 ERR(ddraw, "Unhandled texture format (bad RGB count)\n");
384 } else {
385 ERR(ddraw, "Unhandled texture format (neither RGB nor INDEX)\n");
388 glBindTexture(GL_TEXTURE_2D, current_texture);
391 return D3D_OK;
395 /*******************************************************************************
396 * IDirect3DTexture2 VTable
398 static IDirect3DTexture2_VTable texture2_vtable = {
399 /*** IUnknown methods ***/
400 IDirect3DTexture2_QueryInterface,
401 IDirect3DTexture2_AddRef,
402 IDirect3DTexture2_Release,
403 /*** IDirect3DTexture methods ***/
404 IDirect3DTexture2_GetHandle,
405 IDirect3DTexture2_PaletteChanged,
406 IDirect3DTexture2_Load
409 /*******************************************************************************
410 * IDirect3DTexture VTable
412 static IDirect3DTexture_VTable texture_vtable = {
413 /*** IUnknown methods ***/
414 IDirect3DTexture2_QueryInterface,
415 IDirect3DTexture2_AddRef,
416 IDirect3DTexture2_Release,
417 /*** IDirect3DTexture methods ***/
418 IDirect3DTexture_Initialize,
419 IDirect3DTexture_GetHandle,
420 IDirect3DTexture2_PaletteChanged,
421 IDirect3DTexture2_Load,
422 IDirect3DTexture_Unload
425 #else /* HAVE_MESAGL */
427 /* These function should never be called if MesaGL is not present */
428 LPDIRECT3DTEXTURE2 d3dtexture2_create(LPDIRECTDRAWSURFACE4 surf) {
429 ERR(ddraw, "Should not be called...\n");
430 return NULL;
433 LPDIRECT3DTEXTURE d3dtexture_create(LPDIRECTDRAWSURFACE4 surf) {
434 ERR(ddraw, "Should not be called...\n");
435 return NULL;
438 #endif /* HAVE_MESAGL */