- fix (stupid) regressions introduced by last series of patch
[wine/multimedia.git] / dlls / ddraw / d3dtexture.c
blob6e78c7b854616d570fc9f4530fce75ba7b7e9073
1 /* Direct3D Texture
2 * Copyright (c) 1998 Lionel ULMER
4 * This file contains the implementation of interface Direct3DTexture2.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <string.h>
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winerror.h"
29 #include "objbase.h"
30 #include "ddraw.h"
31 #include "d3d.h"
32 #include "wine/debug.h"
34 #include "mesa_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
38 /* Define this if you want to save to a file all the textures used by a game
39 (can be funny to see how they managed to cram all the pictures in
40 texture memory) */
41 #undef TEXTURE_SNOOP
43 #ifdef TEXTURE_SNOOP
44 #include <stdio.h>
46 static void
47 snoop_texture(IDirectDrawSurfaceImpl *This) {
48 IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
49 char buf[128];
50 FILE *f;
52 sprintf(buf, "tex_%05d_%02d.pnm", glThis->tex_name, This->mipmap_level);
53 f = fopen(buf, "wb");
54 DDRAW_dump_surface_to_disk(This, f);
57 #else
59 #define snoop_texture(a)
61 #endif
63 static IDirectDrawSurfaceImpl *
64 get_sub_mimaplevel(IDirectDrawSurfaceImpl *tex_ptr)
66 /* Now go down the mipmap chain to the next surface */
67 static const DDSCAPS2 mipmap_caps = { DDSCAPS_MIPMAP | DDSCAPS_TEXTURE, 0, 0, 0 };
68 LPDIRECTDRAWSURFACE7 next_level;
69 IDirectDrawSurfaceImpl *surf_ptr;
70 HRESULT hr;
72 hr = IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(tex_ptr, IDirectDrawSurface7),
73 (DDSCAPS2 *) &mipmap_caps, &next_level);
74 if (FAILED(hr)) return NULL;
76 surf_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, next_level);
77 IDirectDrawSurface7_Release(next_level);
79 return surf_ptr;
82 /*******************************************************************************
83 * IDirectSurface callback methods
86 HRESULT
87 gltex_download_texture(IDirectDrawSurfaceImpl *surf_ptr) {
88 IDirect3DTextureGLImpl *gl_surf_ptr = (IDirect3DTextureGLImpl *) surf_ptr->tex_private;
90 FIXME("This is not supported yet... Expect some graphical glitches !!!\n");
92 /* GL and memory are in sync again ...
93 No need to change the 'global' flag as it only handles the 'MEMORY_DIRTY' case.
95 gl_surf_ptr->dirty_flag = SURFACE_MEMORY;
97 return DD_OK;
100 static GLenum
101 convert_min_filter_to_GL(D3DTEXTUREMINFILTER dwMinState, D3DTEXTUREMIPFILTER dwMipState)
103 GLenum gl_state;
105 if (dwMipState == D3DTFP_NONE) {
106 switch (dwMinState) {
107 case D3DTFN_POINT: gl_state = GL_NEAREST; break;
108 case D3DTFN_LINEAR: gl_state = GL_LINEAR; break;
109 default: gl_state = GL_LINEAR; break;
111 } else if (dwMipState == D3DTFP_POINT) {
112 switch (dwMinState) {
113 case D3DTFN_POINT: gl_state = GL_NEAREST_MIPMAP_NEAREST; break;
114 case D3DTFN_LINEAR: gl_state = GL_LINEAR_MIPMAP_NEAREST; break;
115 default: gl_state = GL_LINEAR_MIPMAP_NEAREST; break;
117 } else {
118 switch (dwMinState) {
119 case D3DTFN_POINT: gl_state = GL_NEAREST_MIPMAP_LINEAR; break;
120 case D3DTFN_LINEAR: gl_state = GL_LINEAR_MIPMAP_LINEAR; break;
121 default: gl_state = GL_LINEAR_MIPMAP_LINEAR; break;
124 return gl_state;
127 static GLenum
128 convert_mag_filter_to_GL(D3DTEXTUREMAGFILTER dwState)
130 GLenum gl_state;
132 switch (dwState) {
133 case D3DTFG_POINT:
134 gl_state = GL_NEAREST;
135 break;
136 case D3DTFG_LINEAR:
137 gl_state = GL_LINEAR;
138 break;
139 default:
140 gl_state = GL_LINEAR;
141 break;
143 return gl_state;
146 static GLenum
147 convert_tex_address_to_GL(D3DTEXTUREADDRESS dwState)
149 GLenum gl_state;
150 switch (dwState) {
151 case D3DTADDRESS_WRAP: gl_state = GL_REPEAT; break;
152 case D3DTADDRESS_CLAMP: gl_state = GL_CLAMP; break;
153 case D3DTADDRESS_BORDER: gl_state = GL_CLAMP_TO_EDGE; break;
154 #if defined(GL_VERSION_1_4)
155 case D3DTADDRESS_MIRROR: gl_state = GL_MIRRORED_REPEAT; break;
156 #elif defined(GL_ARB_texture_mirrored_repeat)
157 case D3DTADDRESS_MIRROR: gl_state = GL_MIRRORED_REPEAT_ARB; break;
158 #endif
159 default: gl_state = GL_REPEAT; break;
161 return gl_state;
164 HRESULT
165 gltex_upload_texture(IDirectDrawSurfaceImpl *surf_ptr, IDirect3DDeviceImpl *d3ddev, DWORD stage) {
166 IDirect3DTextureGLImpl *gl_surf_ptr = (IDirect3DTextureGLImpl *) surf_ptr->tex_private;
167 BOOLEAN changed = FALSE;
169 if (surf_ptr->mipmap_level != 0) {
170 WARN(" application activating a sub-level of the mipmapping chain (level %d) !\n", surf_ptr->mipmap_level);
173 /* Now check if the texture parameters for this texture are still in-line with what D3D expect
174 us to do..
176 NOTE: there is no check for the situation where the same texture is bound to multiple stage
177 but with different parameters per stage.
179 if ((gl_surf_ptr->tex_parameters == NULL) ||
180 (gl_surf_ptr->tex_parameters[D3DTSS_MAXMIPLEVEL - D3DTSS_ADDRESSU] !=
181 d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAXMIPLEVEL - 1])) {
182 DWORD max_mip_level;
184 if ((surf_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) == 0) {
185 max_mip_level = 0;
186 } else {
187 max_mip_level = surf_ptr->surface_desc.u2.dwMipMapCount - 1;
188 if (d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAXMIPLEVEL - 1] != 0) {
189 if (max_mip_level >= d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAXMIPLEVEL - 1]) {
190 max_mip_level = d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAXMIPLEVEL - 1] - 1;
194 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, max_mip_level);
195 changed = TRUE;
198 if ((gl_surf_ptr->tex_parameters == NULL) ||
199 (gl_surf_ptr->tex_parameters[D3DTSS_MAGFILTER - D3DTSS_ADDRESSU] !=
200 d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAGFILTER - 1])) {
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
202 convert_mag_filter_to_GL(d3ddev->state_block.texture_stage_state[stage][D3DTSS_MAGFILTER - 1]));
203 changed = TRUE;
205 if ((gl_surf_ptr->tex_parameters == NULL) ||
206 (gl_surf_ptr->tex_parameters[D3DTSS_MINFILTER - D3DTSS_ADDRESSU] !=
207 d3ddev->state_block.texture_stage_state[stage][D3DTSS_MINFILTER - 1]) ||
208 (gl_surf_ptr->tex_parameters[D3DTSS_MIPFILTER - D3DTSS_ADDRESSU] !=
209 d3ddev->state_block.texture_stage_state[stage][D3DTSS_MIPFILTER - 1])) {
210 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
211 convert_min_filter_to_GL(d3ddev->state_block.texture_stage_state[stage][D3DTSS_MINFILTER - 1],
212 d3ddev->state_block.texture_stage_state[stage][D3DTSS_MIPFILTER - 1]));
213 changed = TRUE;
215 if ((gl_surf_ptr->tex_parameters == NULL) ||
216 (gl_surf_ptr->tex_parameters[D3DTSS_ADDRESSU - D3DTSS_ADDRESSU] !=
217 d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSU - 1])) {
218 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
219 convert_tex_address_to_GL(d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSU - 1]));
220 changed = TRUE;
222 if ((gl_surf_ptr->tex_parameters == NULL) ||
223 (gl_surf_ptr->tex_parameters[D3DTSS_ADDRESSV - D3DTSS_ADDRESSU] !=
224 d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSV - 1])) {
225 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
226 convert_tex_address_to_GL(d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSV - 1]));
227 changed = TRUE;
229 if ((gl_surf_ptr->tex_parameters == NULL) ||
230 (gl_surf_ptr->tex_parameters[D3DTSS_BORDERCOLOR - D3DTSS_ADDRESSU] !=
231 d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1])) {
232 GLfloat color[4];
234 color[0] = ((d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1] >> 16) & 0xFF) / 255.0;
235 color[1] = ((d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1] >> 8) & 0xFF) / 255.0;
236 color[2] = ((d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1] >> 0) & 0xFF) / 255.0;
237 color[3] = ((d3ddev->state_block.texture_stage_state[stage][D3DTSS_BORDERCOLOR - 1] >> 24) & 0xFF) / 255.0;
238 glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color);
239 changed = TRUE;
242 if (changed == TRUE) {
243 if (gl_surf_ptr->tex_parameters == NULL) {
244 gl_surf_ptr->tex_parameters = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
245 sizeof(DWORD) * (D3DTSS_MAXMIPLEVEL + 1 - D3DTSS_ADDRESSU));
247 memcpy(gl_surf_ptr->tex_parameters, &(d3ddev->state_block.texture_stage_state[stage][D3DTSS_ADDRESSU - 1]),
248 sizeof(DWORD) * (D3DTSS_MAXMIPLEVEL + 1 - D3DTSS_ADDRESSU));
251 if (*(gl_surf_ptr->global_dirty_flag) != SURFACE_MEMORY_DIRTY) {
252 TRACE(" nothing to do - memory copy and GL state in synch for all texture levels.\n");
253 return DD_OK;
256 while (surf_ptr != NULL) {
257 IDirect3DTextureGLImpl *gl_surf_ptr = (IDirect3DTextureGLImpl *) surf_ptr->tex_private;
259 if (gl_surf_ptr->dirty_flag != SURFACE_MEMORY_DIRTY) {
260 TRACE(" - level %d already uploaded.\n", surf_ptr->mipmap_level);
261 } else {
262 TRACE(" - uploading texture level %d (initial done = %d).\n",
263 surf_ptr->mipmap_level, gl_surf_ptr->initial_upload_done);
265 /* Texture snooping for the curious :-) */
266 snoop_texture(surf_ptr);
268 if (upload_surface_to_tex_memory_init(surf_ptr, surf_ptr->mipmap_level, &(gl_surf_ptr->current_internal_format),
269 gl_surf_ptr->initial_upload_done == FALSE, TRUE, 0, 0) == DD_OK) {
270 upload_surface_to_tex_memory(NULL, 0, 0, &(gl_surf_ptr->surface_ptr));
271 upload_surface_to_tex_memory_release();
272 gl_surf_ptr->dirty_flag = SURFACE_MEMORY;
273 } else {
274 ERR("Problem for upload of texture %d (level = %d / initial done = %d).\n",
275 gl_surf_ptr->tex_name, surf_ptr->mipmap_level, gl_surf_ptr->initial_upload_done);
279 if (surf_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) {
280 surf_ptr = get_sub_mimaplevel(surf_ptr);
281 } else {
282 surf_ptr = NULL;
286 *(gl_surf_ptr->global_dirty_flag) = SURFACE_MEMORY;
288 return DD_OK;
291 HRESULT WINAPI
292 Main_IDirect3DTextureImpl_1_Initialize(LPDIRECT3DTEXTURE iface,
293 LPDIRECT3DDEVICE lpDirect3DDevice,
294 LPDIRECTDRAWSURFACE lpDDSurface)
296 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
297 FIXME("(%p/%p)->(%p,%p) no-op...\n", This, iface, lpDirect3DDevice, lpDDSurface);
298 return DD_OK;
301 static HRESULT
302 gltex_setcolorkey_cb(IDirectDrawSurfaceImpl *This, DWORD dwFlags, LPDDCOLORKEY ckey )
304 IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
306 if (glThis->dirty_flag == SURFACE_GL) {
307 GLuint cur_tex;
309 TRACE(" flushing GL texture back to memory.\n");
311 ENTER_GL();
312 glGetIntegerv(GL_TEXTURE_BINDING_2D, &cur_tex);
313 glBindTexture(GL_TEXTURE_2D, glThis->tex_name);
314 gltex_download_texture(This);
315 glBindTexture(GL_TEXTURE_2D, cur_tex);
316 LEAVE_GL();
319 glThis->dirty_flag = SURFACE_MEMORY_DIRTY;
320 *(glThis->global_dirty_flag) = SURFACE_MEMORY_DIRTY;
321 /* TODO: check color-keying on mipmapped surfaces... */
323 return DD_OK;
326 HRESULT
327 gltex_blt(IDirectDrawSurfaceImpl *This, LPRECT rdst,
328 LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
329 DWORD dwFlags, LPDDBLTFX lpbltfx)
331 if (src != NULL) {
332 IDirectDrawSurfaceImpl *src_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, src);
333 if (src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE) {
334 FIXME("Blt from framebuffer to texture - unsupported now, please report !\n");
337 return DDERR_INVALIDPARAMS;
340 HRESULT
341 gltex_bltfast(IDirectDrawSurfaceImpl *surf_ptr, DWORD dstx,
342 DWORD dsty, LPDIRECTDRAWSURFACE7 src,
343 LPRECT rsrc, DWORD trans)
345 if (src != NULL) {
346 IDirectDrawSurfaceImpl *src_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, src);
348 if ((src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE) &&
349 ((trans & (DDBLTFAST_SRCCOLORKEY | DDBLTFAST_DESTCOLORKEY)) == 0)) {
350 /* This is a blt without color keying... We can use the direct copy. */
351 RECT rsrc2;
352 DWORD width, height;
353 GLuint cur_tex;
354 IDirect3DTextureGLImpl *gl_surf_ptr = surf_ptr->tex_private;
355 int y;
357 if (rsrc == NULL) {
358 WARN("rsrc is NULL\n");
359 rsrc = &rsrc2;
361 rsrc->left = 0;
362 rsrc->top = 0;
363 rsrc->right = src_ptr->surface_desc.dwWidth;
364 rsrc->bottom = src_ptr->surface_desc.dwHeight;
367 width = rsrc->right - rsrc->left;
368 height = rsrc->bottom - rsrc->top;
370 if (((dstx + width) > surf_ptr->surface_desc.dwWidth) ||
371 ((dsty + height) > surf_ptr->surface_desc.dwHeight)) {
372 FIXME("Does not handle clipping yet in FB => Texture blits !\n");
373 return DDERR_INVALIDPARAMS;
376 if ((width == 0) || (height == 0)) {
377 return DD_OK;
380 TRACE(" direct frame buffer => texture BltFast override.\n");
382 ENTER_GL();
384 glGetIntegerv(GL_TEXTURE_BINDING_2D, &cur_tex);
385 glBindTexture(GL_TEXTURE_2D, gl_surf_ptr->tex_name);
387 if ((gl_surf_ptr->dirty_flag == SURFACE_MEMORY_DIRTY) &&
388 !((dstx == 0) && (dsty == 0) &&
389 (width == surf_ptr->surface_desc.dwWidth) && (height == surf_ptr->surface_desc.dwHeight))) {
390 /* If not 'full size' and the surface is dirty, first flush it to GL before doing the copy. */
391 if (upload_surface_to_tex_memory_init(surf_ptr, surf_ptr->mipmap_level, &(gl_surf_ptr->current_internal_format),
392 gl_surf_ptr->initial_upload_done == FALSE, TRUE, 0, 0) == DD_OK) {
393 upload_surface_to_tex_memory(NULL, 0, 0, &(gl_surf_ptr->surface_ptr));
394 upload_surface_to_tex_memory_release();
395 gl_surf_ptr->dirty_flag = SURFACE_MEMORY;
396 } else {
397 glBindTexture(GL_TEXTURE_2D, cur_tex);
398 LEAVE_GL();
399 ERR("Error at texture upload !\n");
400 return DDERR_INVALIDPARAMS;
404 /* This is a hack and would need some clean-up :-) */
405 if (gl_surf_ptr->initial_upload_done == FALSE) {
406 if (upload_surface_to_tex_memory_init(surf_ptr, surf_ptr->mipmap_level, &(gl_surf_ptr->current_internal_format),
407 TRUE, TRUE, 0, 0) == DD_OK) {
408 upload_surface_to_tex_memory(NULL, 0, 0, &(gl_surf_ptr->surface_ptr));
409 upload_surface_to_tex_memory_release();
410 gl_surf_ptr->dirty_flag = SURFACE_MEMORY;
411 } else {
412 glBindTexture(GL_TEXTURE_2D, cur_tex);
413 LEAVE_GL();
414 ERR("Error at texture upload (initial case) !\n");
415 return DDERR_INVALIDPARAMS;
419 if ((src_ptr->surface_desc.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER|DDSCAPS_PRIMARYSURFACE)) != 0)
420 glReadBuffer(GL_FRONT);
421 else if ((src_ptr->surface_desc.ddsCaps.dwCaps & (DDSCAPS_BACKBUFFER)) == (DDSCAPS_BACKBUFFER))
422 glReadBuffer(GL_BACK);
423 else {
424 ERR("Wrong surface type for locking !\n");
425 glBindTexture(GL_TEXTURE_2D, cur_tex);
426 LEAVE_GL();
427 return DDERR_INVALIDPARAMS;
430 for (y = (src_ptr->surface_desc.dwHeight - rsrc->top - 1);
431 y >= (src_ptr->surface_desc.dwHeight - (rsrc->top + height));
432 y--) {
433 glCopyTexSubImage2D(GL_TEXTURE_2D, surf_ptr->mipmap_level,
434 dstx, dsty,
435 rsrc->left, y,
436 width, 1);
437 dsty++;
440 glBindTexture(GL_TEXTURE_2D, cur_tex);
441 LEAVE_GL();
443 /* The SURFACE_GL case is not handled by the 'global' dirty flag */
444 gl_surf_ptr->dirty_flag = SURFACE_GL;
446 return DD_OK;
449 return DDERR_INVALIDPARAMS;
452 HRESULT WINAPI
453 Main_IDirect3DTextureImpl_2_1T_PaletteChanged(LPDIRECT3DTEXTURE2 iface,
454 DWORD dwStart,
455 DWORD dwCount)
457 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
458 FIXME("(%p/%p)->(%08lx,%08lx): stub!\n", This, iface, dwStart, dwCount);
459 return DD_OK;
462 HRESULT WINAPI
463 Main_IDirect3DTextureImpl_1_Unload(LPDIRECT3DTEXTURE iface)
465 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture, iface);
466 FIXME("(%p/%p)->(): stub!\n", This, iface);
467 return DD_OK;
470 HRESULT WINAPI
471 Main_IDirect3DTextureImpl_2_1T_GetHandle(LPDIRECT3DTEXTURE2 iface,
472 LPDIRECT3DDEVICE2 lpDirect3DDevice2,
473 LPD3DTEXTUREHANDLE lpHandle)
475 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
476 IDirect3DDeviceImpl *lpDeviceImpl = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice2, lpDirect3DDevice2);
478 TRACE("(%p/%p)->(%p,%p)\n", This, iface, lpDirect3DDevice2, lpHandle);
480 /* The handle is simply the pointer to the implementation structure */
481 *lpHandle = (D3DTEXTUREHANDLE) This;
483 TRACE(" returning handle %08lx.\n", *lpHandle);
485 /* Now set the device for this texture */
486 This->d3ddevice = lpDeviceImpl;
488 return D3D_OK;
491 HRESULT WINAPI
492 Main_IDirect3DTextureImpl_2_1T_Load(LPDIRECT3DTEXTURE2 iface,
493 LPDIRECT3DTEXTURE2 lpD3DTexture2)
495 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
496 FIXME("(%p/%p)->(%p): stub!\n", This, iface, lpD3DTexture2);
497 return DD_OK;
500 static void gltex_set_palette(IDirectDrawSurfaceImpl* This, IDirectDrawPaletteImpl* pal)
502 IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
504 if (glThis->dirty_flag == SURFACE_GL) {
505 GLuint cur_tex;
507 TRACE(" flushing GL texture back to memory.\n");
509 ENTER_GL();
510 glGetIntegerv(GL_TEXTURE_BINDING_2D, &cur_tex);
511 glBindTexture(GL_TEXTURE_2D, glThis->tex_name);
512 gltex_download_texture(This);
513 glBindTexture(GL_TEXTURE_2D, cur_tex);
514 LEAVE_GL();
517 /* First call the previous set_palette function */
518 glThis->set_palette(This, pal);
520 /* And set the dirty flag */
521 glThis->dirty_flag = SURFACE_MEMORY_DIRTY;
522 *(glThis->global_dirty_flag) = SURFACE_MEMORY_DIRTY;
524 /* TODO: check palette on mipmapped surfaces...
525 TODO: do we need to re-upload in case of usage of the paletted texture extension ? */
528 static void
529 gltex_final_release(IDirectDrawSurfaceImpl *This)
531 IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
532 DWORD mem_used;
533 int i;
535 TRACE(" deleting texture with GL id %d.\n", glThis->tex_name);
537 /* And delete texture handle */
538 ENTER_GL();
539 if (glThis->tex_name != 0)
540 glDeleteTextures(1, &(glThis->tex_name));
541 LEAVE_GL();
543 if (glThis->surface_ptr != NULL)
544 HeapFree(GetProcessHeap(), 0, glThis->surface_ptr);
546 /* And if this texture was the current one, remove it at the device level */
547 if (This->d3ddevice != NULL)
548 for (i = 0; i < MAX_TEXTURES; i++)
549 if (This->d3ddevice->current_texture[i] == This)
550 This->d3ddevice->current_texture[i] = NULL;
552 /* All this should be part of main surface management not just a hack for texture.. */
553 if (glThis->loaded) {
554 mem_used = This->surface_desc.dwHeight *
555 This->surface_desc.u1.lPitch;
556 This->ddraw_owner->free_memory(This->ddraw_owner, mem_used);
559 glThis->final_release(This);
562 static void
563 gltex_lock_update(IDirectDrawSurfaceImpl* This, LPCRECT pRect, DWORD dwFlags)
565 IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
567 glThis->lock_update(This, pRect, dwFlags);
570 static void
571 gltex_unlock_update(IDirectDrawSurfaceImpl* This, LPCRECT pRect)
573 IDirect3DTextureGLImpl *glThis = (IDirect3DTextureGLImpl *) This->tex_private;
575 glThis->unlock_update(This, pRect);
577 /* Set the dirty flag according to the lock type */
578 if ((This->lastlocktype & DDLOCK_READONLY) == 0) {
579 glThis->dirty_flag = SURFACE_MEMORY_DIRTY;
580 *(glThis->global_dirty_flag) = SURFACE_MEMORY_DIRTY;
584 HRESULT WINAPI
585 GL_IDirect3DTextureImpl_2_1T_Load(LPDIRECT3DTEXTURE2 iface,
586 LPDIRECT3DTEXTURE2 lpD3DTexture2)
588 ICOM_THIS_FROM(IDirectDrawSurfaceImpl, IDirect3DTexture2, iface);
589 IDirectDrawSurfaceImpl *src_ptr = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, lpD3DTexture2);
590 IDirectDrawSurfaceImpl *dst_ptr = This;
591 HRESULT ret_value = D3D_OK;
593 TRACE("(%p/%p)->(%p)\n", This, iface, lpD3DTexture2);
595 if (((src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) != (dst_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP)) ||
596 (src_ptr->surface_desc.u2.dwMipMapCount != dst_ptr->surface_desc.u2.dwMipMapCount)) {
597 ERR("Trying to load surfaces with different mip-map counts !\n");
600 /* Now loop through all mipmap levels and load all of them... */
601 while (1) {
602 IDirect3DTextureGLImpl *gl_dst_ptr = (IDirect3DTextureGLImpl *) dst_ptr->tex_private;
603 DDSURFACEDESC *src_d, *dst_d;
605 if (gl_dst_ptr != NULL) {
606 if (gl_dst_ptr->loaded == FALSE) {
607 /* Only check memory for not already loaded texture... */
608 DWORD mem_used = dst_ptr->surface_desc.dwHeight * dst_ptr->surface_desc.u1.lPitch;
609 if (This->ddraw_owner->allocate_memory(This->ddraw_owner, mem_used) < 0) {
610 TRACE(" out of virtual memory... Warning application.\n");
611 return D3DERR_TEXTURE_LOAD_FAILED;
614 gl_dst_ptr->loaded = TRUE;
617 TRACE(" copying surface %p to surface %p (mipmap level %d)\n", src_ptr, dst_ptr, src_ptr->mipmap_level);
619 if ( dst_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_ALLOCONLOAD )
620 /* If the surface is not allocated and its location is not yet specified,
621 force it to video memory */
622 if ( !(dst_ptr->surface_desc.ddsCaps.dwCaps & (DDSCAPS_SYSTEMMEMORY|DDSCAPS_VIDEOMEMORY)) )
623 dst_ptr->surface_desc.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
625 /* Suppress the ALLOCONLOAD flag */
626 dst_ptr->surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
628 /* After seeing some logs, not sure at all about this... */
629 if (dst_ptr->palette == NULL) {
630 dst_ptr->palette = src_ptr->palette;
631 if (src_ptr->palette != NULL) IDirectDrawPalette_AddRef(ICOM_INTERFACE(src_ptr->palette, IDirectDrawPalette));
632 } else {
633 if (src_ptr->palette != NULL) {
634 PALETTEENTRY palent[256];
635 IDirectDrawPalette_GetEntries(ICOM_INTERFACE(src_ptr->palette, IDirectDrawPalette),
636 0, 0, 256, palent);
637 IDirectDrawPalette_SetEntries(ICOM_INTERFACE(dst_ptr->palette, IDirectDrawPalette),
638 0, 0, 256, palent);
642 /* Copy one surface on the other */
643 dst_d = (DDSURFACEDESC *)&(dst_ptr->surface_desc);
644 src_d = (DDSURFACEDESC *)&(src_ptr->surface_desc);
646 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
647 /* Should also check for same pixel format, u1.lPitch, ... */
648 ERR("Error in surface sizes\n");
649 return D3DERR_TEXTURE_LOAD_FAILED;
650 } else {
651 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
652 /* I should put a macro for the calculus of bpp */
654 /* Copy also the ColorKeying stuff */
655 if (src_d->dwFlags & DDSD_CKSRCBLT) {
656 dst_d->dwFlags |= DDSD_CKSRCBLT;
657 dst_d->ddckCKSrcBlt.dwColorSpaceLowValue = src_d->ddckCKSrcBlt.dwColorSpaceLowValue;
658 dst_d->ddckCKSrcBlt.dwColorSpaceHighValue = src_d->ddckCKSrcBlt.dwColorSpaceHighValue;
661 /* Copy the main memory texture into the surface that corresponds to the OpenGL
662 texture object. */
663 memcpy(dst_d->lpSurface, src_d->lpSurface, src_d->u1.lPitch * src_d->dwHeight);
665 if (gl_dst_ptr != NULL) {
666 /* If the GetHandle was not done, it is an error... */
667 if (gl_dst_ptr->tex_name == 0) ERR("Unbound GL texture !!!\n");
669 /* Set this texture as dirty */
670 gl_dst_ptr->dirty_flag = SURFACE_MEMORY_DIRTY;
671 *(gl_dst_ptr->global_dirty_flag) = SURFACE_MEMORY_DIRTY;
675 if (src_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) {
676 src_ptr = get_sub_mimaplevel(src_ptr);
677 } else {
678 src_ptr = NULL;
680 if (dst_ptr->surface_desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) {
681 dst_ptr = get_sub_mimaplevel(dst_ptr);
682 } else {
683 dst_ptr = NULL;
686 if ((src_ptr == NULL) || (dst_ptr == NULL)) {
687 if (src_ptr != dst_ptr) {
688 ERR(" Loading surface with different mipmap structure !!!\n");
690 break;
694 return ret_value;
697 HRESULT WINAPI
698 Thunk_IDirect3DTextureImpl_2_QueryInterface(LPDIRECT3DTEXTURE2 iface,
699 REFIID riid,
700 LPVOID* obp)
702 TRACE("(%p)->(%s,%p) thunking to IDirectDrawSurface7 interface.\n", iface, debugstr_guid(riid), obp);
703 return IDirectDrawSurface7_QueryInterface(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture2, IDirectDrawSurface7, iface),
704 riid,
705 obp);
708 ULONG WINAPI
709 Thunk_IDirect3DTextureImpl_2_AddRef(LPDIRECT3DTEXTURE2 iface)
711 TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", iface);
712 return IDirectDrawSurface7_AddRef(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture2, IDirectDrawSurface7, iface));
715 ULONG WINAPI
716 Thunk_IDirect3DTextureImpl_2_Release(LPDIRECT3DTEXTURE2 iface)
718 TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", iface);
719 return IDirectDrawSurface7_Release(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture2, IDirectDrawSurface7, iface));
722 HRESULT WINAPI
723 Thunk_IDirect3DTextureImpl_1_QueryInterface(LPDIRECT3DTEXTURE iface,
724 REFIID riid,
725 LPVOID* obp)
727 TRACE("(%p)->(%s,%p) thunking to IDirectDrawSurface7 interface.\n", iface, debugstr_guid(riid), obp);
728 return IDirectDrawSurface7_QueryInterface(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirectDrawSurface7, iface),
729 riid,
730 obp);
733 ULONG WINAPI
734 Thunk_IDirect3DTextureImpl_1_AddRef(LPDIRECT3DTEXTURE iface)
736 TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", iface);
737 return IDirectDrawSurface7_AddRef(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirectDrawSurface7, iface));
740 ULONG WINAPI
741 Thunk_IDirect3DTextureImpl_1_Release(LPDIRECT3DTEXTURE iface)
743 TRACE("(%p)->() thunking to IDirectDrawSurface7 interface.\n", iface);
744 return IDirectDrawSurface7_Release(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirectDrawSurface7, iface));
747 HRESULT WINAPI
748 Thunk_IDirect3DTextureImpl_1_PaletteChanged(LPDIRECT3DTEXTURE iface,
749 DWORD dwStart,
750 DWORD dwCount)
752 TRACE("(%p)->(%08lx,%08lx) thunking to IDirect3DTexture2 interface.\n", iface, dwStart, dwCount);
753 return IDirect3DTexture2_PaletteChanged(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, iface),
754 dwStart,
755 dwCount);
758 HRESULT WINAPI
759 Thunk_IDirect3DTextureImpl_1_GetHandle(LPDIRECT3DTEXTURE iface,
760 LPDIRECT3DDEVICE lpDirect3DDevice,
761 LPD3DTEXTUREHANDLE lpHandle)
763 TRACE("(%p)->(%p,%p) thunking to IDirect3DTexture2 interface.\n", iface, lpDirect3DDevice, lpHandle);
764 return IDirect3DTexture2_GetHandle(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, iface),
765 COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice2, lpDirect3DDevice),
766 lpHandle);
769 HRESULT WINAPI
770 Thunk_IDirect3DTextureImpl_1_Load(LPDIRECT3DTEXTURE iface,
771 LPDIRECT3DTEXTURE lpD3DTexture)
773 TRACE("(%p)->(%p) thunking to IDirect3DTexture2 interface.\n", iface, lpD3DTexture);
774 return IDirect3DTexture2_Load(COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, iface),
775 COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirect3DTexture, IDirect3DTexture2, lpD3DTexture));
778 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
779 # define XCAST(fun) (typeof(VTABLE_IDirect3DTexture2.fun))
780 #else
781 # define XCAST(fun) (void*)
782 #endif
784 ICOM_VTABLE(IDirect3DTexture2) VTABLE_IDirect3DTexture2 =
786 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
787 XCAST(QueryInterface) Thunk_IDirect3DTextureImpl_2_QueryInterface,
788 XCAST(AddRef) Thunk_IDirect3DTextureImpl_2_AddRef,
789 XCAST(Release) Thunk_IDirect3DTextureImpl_2_Release,
790 XCAST(GetHandle) Main_IDirect3DTextureImpl_2_1T_GetHandle,
791 XCAST(PaletteChanged) Main_IDirect3DTextureImpl_2_1T_PaletteChanged,
792 XCAST(Load) GL_IDirect3DTextureImpl_2_1T_Load,
795 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
796 #undef XCAST
797 #endif
800 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
801 # define XCAST(fun) (typeof(VTABLE_IDirect3DTexture.fun))
802 #else
803 # define XCAST(fun) (void*)
804 #endif
806 ICOM_VTABLE(IDirect3DTexture) VTABLE_IDirect3DTexture =
808 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
809 XCAST(QueryInterface) Thunk_IDirect3DTextureImpl_1_QueryInterface,
810 XCAST(AddRef) Thunk_IDirect3DTextureImpl_1_AddRef,
811 XCAST(Release) Thunk_IDirect3DTextureImpl_1_Release,
812 XCAST(Initialize) Main_IDirect3DTextureImpl_1_Initialize,
813 XCAST(GetHandle) Thunk_IDirect3DTextureImpl_1_GetHandle,
814 XCAST(PaletteChanged) Thunk_IDirect3DTextureImpl_1_PaletteChanged,
815 XCAST(Load) Thunk_IDirect3DTextureImpl_1_Load,
816 XCAST(Unload) Main_IDirect3DTextureImpl_1_Unload,
819 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
820 #undef XCAST
821 #endif
823 HRESULT d3dtexture_create(IDirectDrawImpl *d3d, IDirectDrawSurfaceImpl *surf, BOOLEAN at_creation,
824 IDirectDrawSurfaceImpl *main)
826 /* First, initialize the texture vtables... */
827 ICOM_INIT_INTERFACE(surf, IDirect3DTexture, VTABLE_IDirect3DTexture);
828 ICOM_INIT_INTERFACE(surf, IDirect3DTexture2, VTABLE_IDirect3DTexture2);
830 /* Only create all the private stuff if we actually have an OpenGL context.. */
831 if (d3d->current_device != NULL) {
832 IDirect3DTextureGLImpl *private;
834 private = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTextureGLImpl));
835 if (private == NULL) return DDERR_OUTOFMEMORY;
837 private->final_release = surf->final_release;
838 private->lock_update = surf->lock_update;
839 private->unlock_update = surf->unlock_update;
840 private->set_palette = surf->set_palette;
842 /* If at creation, we can optimize stuff and wait the first 'unlock' to upload a valid stuff to OpenGL.
843 Otherwise, it will be uploaded here (and may be invalid). */
844 surf->final_release = gltex_final_release;
845 surf->lock_update = gltex_lock_update;
846 surf->unlock_update = gltex_unlock_update;
847 surf->tex_private = private;
848 surf->aux_setcolorkey_cb = gltex_setcolorkey_cb;
849 surf->set_palette = gltex_set_palette;
851 /* We are the only one to use the aux_blt and aux_bltfast overides, so no need
852 to save those... */
853 surf->aux_blt = gltex_blt;
854 surf->aux_bltfast = gltex_bltfast;
856 TRACE(" GL texture created for surface %p (private data at %p)\n", surf, private);
858 ENTER_GL();
859 if (surf->mipmap_level == 0) {
860 glGenTextures(1, &(private->tex_name));
861 if (private->tex_name == 0) ERR("Error at creation of OpenGL texture ID !\n");
862 TRACE(" GL texture id is : %d.\n", private->tex_name);
863 private->__global_dirty_flag = (at_creation == FALSE ? SURFACE_MEMORY_DIRTY : SURFACE_MEMORY);
864 private->global_dirty_flag = &(private->__global_dirty_flag);
865 } else {
866 private->tex_name = ((IDirect3DTextureGLImpl *) (main->tex_private))->tex_name;
867 TRACE(" GL texture id reusing id %d from surface %p (private at %p)).\n", private->tex_name, main, main->tex_private);
868 private->global_dirty_flag = &(((IDirect3DTextureGLImpl *) (main->tex_private))->__global_dirty_flag);
870 LEAVE_GL();
872 /* And set the dirty flag accordingly */
873 private->dirty_flag = (at_creation == FALSE ? SURFACE_MEMORY_DIRTY : SURFACE_MEMORY);
874 private->initial_upload_done = FALSE;
877 return D3D_OK;