blob: 47e95bbe38ac28ecaf7df9f425a9af6f50407391 [file] [log] [blame]
Lionel Ulmer5ff00771999-01-03 17:00:19 +00001/* Direct3D Texture
2 (c) 1998 Lionel ULMER
3
4 This files contains the implementation of interface Direct3DTexture2. */
5
6
David Luyeree517e81999-02-28 12:27:56 +00007#include <string.h>
Lionel Ulmer5ff00771999-01-03 17:00:19 +00008#include "config.h"
Jim Aston2e1cafa1999-03-14 16:35:05 +00009#include "windef.h"
Lionel Ulmer5ff00771999-01-03 17:00:19 +000010#include "winerror.h"
Francois Gougeta94d6491999-02-02 16:14:23 +000011#include "wine/obj_base.h"
Lionel Ulmer5ff00771999-01-03 17:00:19 +000012#include "heap.h"
13#include "ddraw.h"
14#include "d3d.h"
Alexandre Julliard61fece01999-06-26 19:09:08 +000015#include "debugtools.h"
Lionel Ulmer5ff00771999-01-03 17:00:19 +000016
17#include "d3d_private.h"
18
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000019DEFAULT_DEBUG_CHANNEL(ddraw)
20
Lionel Ulmer5ff00771999-01-03 17:00:19 +000021#ifdef HAVE_MESAGL
22
Lionel Ulmer1d3e5011999-01-23 12:32:46 +000023/* Define this if you want to save to a file all the textures used by a game
24 (can be funny to see how they managed to cram all the pictures in
25 texture memory) */
26#undef TEXTURE_SNOOP
27
Lionel Ulmer81c75441999-04-06 07:10:48 +000028#ifdef TEXTURE_SNOOP
29#define SNOOP_PALETTED() \
30 { \
31 FILE *f; \
32 char buf[32]; \
33 int x, y; \
34 \
Lionel Ulmer48c08162000-01-05 01:51:02 +000035 sprintf(buf, "%ld.pnm", This->tex_name); \
Lionel Ulmer81c75441999-04-06 07:10:48 +000036 f = fopen(buf, "wb"); \
37 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
38 for (y = 0; y < src_d->dwHeight; y++) { \
39 for (x = 0; x < src_d->dwWidth; x++) { \
40 unsigned char c = ((unsigned char *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
41 fputc(table[c][0], f); \
42 fputc(table[c][1], f); \
43 fputc(table[c][2], f); \
44 } \
45 } \
46 fclose(f); \
47 }
48
49#define SNOOP_5650() \
50 { \
51 FILE *f; \
52 char buf[32]; \
53 int x, y; \
54 \
Lionel Ulmer48c08162000-01-05 01:51:02 +000055 sprintf(buf, "%ld.pnm", This->tex_name); \
Lionel Ulmer81c75441999-04-06 07:10:48 +000056 f = fopen(buf, "wb"); \
57 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
58 for (y = 0; y < src_d->dwHeight; y++) { \
59 for (x = 0; x < src_d->dwWidth; x++) { \
60 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
61 fputc((c & 0xF800) >> 8, f); \
62 fputc((c & 0x07E0) >> 3, f); \
63 fputc((c & 0x001F) << 3, f); \
64 } \
65 } \
66 fclose(f); \
67 }
68
69#define SNOOP_5551() \
70 { \
71 FILE *f; \
72 char buf[32]; \
73 int x, y; \
74 \
Lionel Ulmer48c08162000-01-05 01:51:02 +000075 sprintf(buf, "%ld.pnm", This->tex_name); \
Lionel Ulmer81c75441999-04-06 07:10:48 +000076 f = fopen(buf, "wb"); \
77 fprintf(f, "P6\n%ld %ld\n255\n", src_d->dwWidth, src_d->dwHeight); \
78 for (y = 0; y < src_d->dwHeight; y++) { \
79 for (x = 0; x < src_d->dwWidth; x++) { \
80 unsigned short c = ((unsigned short *) src_d->y.lpSurface)[y * src_d->dwWidth + x]; \
81 fputc((c & 0xF800) >> 8, f); \
82 fputc((c & 0x07C0) >> 3, f); \
83 fputc((c & 0x003E) << 2, f); \
84 } \
85 } \
86 fclose(f); \
87 }
88#else
89#define SNOOP_PALETTED()
90#define SNOOP_5650()
91#define SNOOP_5551()
92#endif
93
Francois Gouget022d3721999-03-27 16:56:13 +000094static ICOM_VTABLE(IDirect3DTexture2) texture2_vtable;
95static ICOM_VTABLE(IDirect3DTexture) texture_vtable;
Lionel Ulmer5ff00771999-01-03 17:00:19 +000096
97/*******************************************************************************
98 * Texture2 Creation functions
99 */
Francois Gouget022d3721999-03-27 16:56:13 +0000100LPDIRECT3DTEXTURE2 d3dtexture2_create(IDirectDrawSurface4Impl* surf)
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000101{
Francois Gouget022d3721999-03-27 16:56:13 +0000102 IDirect3DTexture2Impl* tex;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000103
Francois Gouget022d3721999-03-27 16:56:13 +0000104 tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
105 tex->ref = 1;
Francois Gouget01c9ac41999-10-31 01:59:23 +0000106 ICOM_VTBL(tex) = &texture2_vtable;
Francois Gouget022d3721999-03-27 16:56:13 +0000107 tex->surface = surf;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000108
Francois Gouget022d3721999-03-27 16:56:13 +0000109 return (LPDIRECT3DTEXTURE2)tex;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000110}
111
112/*******************************************************************************
113 * Texture Creation functions
114 */
Francois Gouget022d3721999-03-27 16:56:13 +0000115LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurface4Impl* surf)
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000116{
Francois Gouget022d3721999-03-27 16:56:13 +0000117 IDirect3DTexture2Impl* tex;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000118
Francois Gouget022d3721999-03-27 16:56:13 +0000119 tex = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DTexture2Impl));
120 tex->ref = 1;
Francois Gouget01c9ac41999-10-31 01:59:23 +0000121 ICOM_VTBL(tex) = (ICOM_VTABLE(IDirect3DTexture2)*)&texture_vtable;
Francois Gouget022d3721999-03-27 16:56:13 +0000122 tex->surface = surf;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000123
Francois Gouget022d3721999-03-27 16:56:13 +0000124 return (LPDIRECT3DTEXTURE)tex;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000125}
126
Lionel Ulmer81c75441999-04-06 07:10:48 +0000127/*******************************************************************************
128 * IDirectSurface callback methods
129 */
130HRESULT WINAPI SetColorKey_cb(IDirect3DTexture2Impl *texture, DWORD dwFlags, LPDDCOLORKEY ckey )
131{
132 DDSURFACEDESC *tex_d;
133 int bpp;
134 GLuint current_texture;
135
Alexandre Julliard61fece01999-06-26 19:09:08 +0000136 TRACE("(%p) : colorkey callback\n", texture);
Lionel Ulmer81c75441999-04-06 07:10:48 +0000137
138 /* Get the texture description */
139 tex_d = &(texture->surface->s.surface_desc);
140 bpp = (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
141 1 /* 8 bit of palette index */:
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000142 tex_d->ddpfPixelFormat.u.dwRGBBitCount / 8 /* RGB bits for each colors */ );
Lionel Ulmer81c75441999-04-06 07:10:48 +0000143
144 /* Now, save the current texture */
Lionel Ulmerad725851999-05-13 18:53:05 +0000145 ENTER_GL();
Lionel Ulmer81c75441999-04-06 07:10:48 +0000146 glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
147
148 /* If the GetHandle was not done yet, it's an error */
149 if (texture->tex_name == 0) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000150 ERR("Unloaded texture !\n");
Lionel Ulmerad725851999-05-13 18:53:05 +0000151 LEAVE_GL();
Lionel Ulmer81c75441999-04-06 07:10:48 +0000152 return DD_OK;
153 }
154 glBindTexture(GL_TEXTURE_2D, texture->tex_name);
155
156 if (tex_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000157 FIXME("Todo Paletted\n");
Lionel Ulmer81c75441999-04-06 07:10:48 +0000158 } else if (tex_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000159 if (tex_d->ddpfPixelFormat.u.dwRGBBitCount == 8) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000160 FIXME("Todo 3_3_2_0\n");
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000161 } else if (tex_d->ddpfPixelFormat.u.dwRGBBitCount == 16) {
162 if (tex_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x00000000) {
Lionel Ulmer81c75441999-04-06 07:10:48 +0000163 /* Now transform the 5_6_5 into a 5_5_5_1 surface to support color keying */
164 unsigned short *dest = (unsigned short *) HeapAlloc(GetProcessHeap(),
165 HEAP_ZERO_MEMORY,
166 tex_d->dwWidth * tex_d->dwHeight * bpp);
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000167 unsigned short *src = (unsigned short *) tex_d->u1.lpSurface;
Lionel Ulmer81c75441999-04-06 07:10:48 +0000168 int x, y;
169
170 for (y = 0; y < tex_d->dwHeight; y++) {
171 for (x = 0; x < tex_d->dwWidth; x++) {
172 unsigned short cpixel = src[x + y * tex_d->dwWidth];
173
174 if ((dwFlags & DDCKEY_SRCBLT) &&
175 (cpixel >= ckey->dwColorSpaceLowValue) &&
176 (cpixel <= ckey->dwColorSpaceHighValue)) /* No alpha bit => this pixel is transparent */
177 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0000;
178 else /* Alpha bit is set => this pixel will be seen */
179 dest[x + y * tex_d->dwWidth] = (cpixel & ~0x003F) | ((cpixel & 0x001F) << 1) | 0x0001;
180 }
181 }
182
183 glTexImage2D(GL_TEXTURE_2D,
184 0,
185 GL_RGBA,
186 tex_d->dwWidth, tex_d->dwHeight,
187 0,
188 GL_RGBA,
189 GL_UNSIGNED_SHORT_5_5_5_1,
190 dest);
191
192 /* Frees the temporary surface */
193 HeapFree(GetProcessHeap(),0,dest);
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000194 } else if (tex_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x00000001) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000195 FIXME("Todo 5_5_5_1\n");
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000196 } else if (tex_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x0000000F) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000197 FIXME("Todo 4_4_4_4\n");
Lionel Ulmer81c75441999-04-06 07:10:48 +0000198 } else {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000199 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
Lionel Ulmer81c75441999-04-06 07:10:48 +0000200 }
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000201 } else if (tex_d->ddpfPixelFormat.u.dwRGBBitCount == 24) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000202 FIXME("Todo 8_8_8_0\n");
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000203 } else if (tex_d->ddpfPixelFormat.u.dwRGBBitCount == 32) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000204 FIXME("Todo 8_8_8_8\n");
Lionel Ulmer81c75441999-04-06 07:10:48 +0000205 } else {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000206 ERR("Unhandled texture format (bad RGB count)\n");
Lionel Ulmer81c75441999-04-06 07:10:48 +0000207 }
208 } else {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000209 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
Lionel Ulmer81c75441999-04-06 07:10:48 +0000210 }
Lionel Ulmerad725851999-05-13 18:53:05 +0000211 LEAVE_GL();
Lionel Ulmer81c75441999-04-06 07:10:48 +0000212
213 return DD_OK;
214}
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000215
216/*******************************************************************************
217 * IDirect3DTexture2 methods
218 */
219
Francois Gouget022d3721999-03-27 16:56:13 +0000220static HRESULT WINAPI IDirect3DTexture2Impl_QueryInterface(LPDIRECT3DTEXTURE2 iface,
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000221 REFIID riid,
222 LPVOID* ppvObj)
223{
Francois Gouget022d3721999-03-27 16:56:13 +0000224 ICOM_THIS(IDirect3DTexture2Impl,iface);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000225
Alexandre Julliard681c75b2000-01-18 05:09:49 +0000226 FIXME("(%p)->(%s,%p): stub\n", This, debugstr_guid(riid),ppvObj);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000227
228 return S_OK;
229}
230
231
232
Francois Gouget022d3721999-03-27 16:56:13 +0000233static ULONG WINAPI IDirect3DTexture2Impl_AddRef(LPDIRECT3DTEXTURE2 iface)
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000234{
Francois Gouget022d3721999-03-27 16:56:13 +0000235 ICOM_THIS(IDirect3DTexture2Impl,iface);
Alexandre Julliard61fece01999-06-26 19:09:08 +0000236 TRACE("(%p)->()incrementing from %lu.\n", This, This->ref );
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000237
Francois Gouget022d3721999-03-27 16:56:13 +0000238 return ++(This->ref);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000239}
240
241
242
Francois Gouget022d3721999-03-27 16:56:13 +0000243static ULONG WINAPI IDirect3DTexture2Impl_Release(LPDIRECT3DTEXTURE2 iface)
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000244{
Francois Gouget022d3721999-03-27 16:56:13 +0000245 ICOM_THIS(IDirect3DTexture2Impl,iface);
Alexandre Julliard61fece01999-06-26 19:09:08 +0000246 FIXME("(%p)->() decrementing from %lu.\n", This, This->ref );
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000247
Francois Gouget022d3721999-03-27 16:56:13 +0000248 if (!--(This->ref)) {
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000249 /* Delete texture from OpenGL */
Lionel Ulmerad725851999-05-13 18:53:05 +0000250 ENTER_GL();
Francois Gouget022d3721999-03-27 16:56:13 +0000251 glDeleteTextures(1, &(This->tex_name));
Lionel Ulmerad725851999-05-13 18:53:05 +0000252 LEAVE_GL();
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000253
254 /* Release surface */
Francois Gouget022d3721999-03-27 16:56:13 +0000255 IDirectDrawSurface4_Release((IDirectDrawSurface4*)This->surface);
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000256
Francois Gouget022d3721999-03-27 16:56:13 +0000257 HeapFree(GetProcessHeap(),0,This);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000258 return 0;
259 }
260
Francois Gouget022d3721999-03-27 16:56:13 +0000261 return This->ref;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000262}
263
264/*** IDirect3DTexture methods ***/
Francois Gouget022d3721999-03-27 16:56:13 +0000265static HRESULT WINAPI IDirect3DTextureImpl_GetHandle(LPDIRECT3DTEXTURE iface,
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000266 LPDIRECT3DDEVICE lpD3DDevice,
267 LPD3DTEXTUREHANDLE lpHandle)
268{
Francois Gouget022d3721999-03-27 16:56:13 +0000269 ICOM_THIS(IDirect3DTexture2Impl,iface);
270 IDirect3DDeviceImpl* ilpD3DDevice=(IDirect3DDeviceImpl*)lpD3DDevice;
Alexandre Julliard61fece01999-06-26 19:09:08 +0000271 FIXME("(%p)->(%p,%p): stub\n", This, ilpD3DDevice, lpHandle);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000272
Francois Gouget022d3721999-03-27 16:56:13 +0000273 *lpHandle = (D3DTEXTUREHANDLE) This;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000274
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000275 /* Now, bind a new texture */
Lionel Ulmerad725851999-05-13 18:53:05 +0000276 ENTER_GL();
Francois Gouget022d3721999-03-27 16:56:13 +0000277 ilpD3DDevice->set_context(ilpD3DDevice);
278 This->D3Ddevice = (void *) ilpD3DDevice;
279 if (This->tex_name == 0)
280 glGenTextures(1, &(This->tex_name));
Lionel Ulmerad725851999-05-13 18:53:05 +0000281 LEAVE_GL();
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000282
Andreas Mohr8cd93512000-01-29 21:12:58 +0000283 TRACE("OpenGL texture handle is : %d\n", This->tex_name);
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000284
Alexandre Julliard638f1691999-01-17 16:32:32 +0000285 return D3D_OK;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000286}
287
Francois Gouget022d3721999-03-27 16:56:13 +0000288static HRESULT WINAPI IDirect3DTextureImpl_Initialize(LPDIRECT3DTEXTURE iface,
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000289 LPDIRECT3DDEVICE lpD3DDevice,
290 LPDIRECTDRAWSURFACE lpSurface)
291{
Francois Gouget022d3721999-03-27 16:56:13 +0000292 ICOM_THIS(IDirect3DTexture2Impl,iface);
Alexandre Julliard61fece01999-06-26 19:09:08 +0000293 TRACE("(%p)->(%p,%p)\n", This, lpD3DDevice, lpSurface);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000294
295 return DDERR_ALREADYINITIALIZED;
296}
297
Francois Gouget022d3721999-03-27 16:56:13 +0000298static HRESULT WINAPI IDirect3DTextureImpl_Unload(LPDIRECT3DTEXTURE iface)
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000299{
Francois Gouget022d3721999-03-27 16:56:13 +0000300 ICOM_THIS(IDirect3DTexture2Impl,iface);
Alexandre Julliard61fece01999-06-26 19:09:08 +0000301 FIXME("(%p)->(): stub\n", This);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000302
Alexandre Julliard638f1691999-01-17 16:32:32 +0000303 return D3D_OK;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000304}
305
306/*** IDirect3DTexture2 methods ***/
Francois Gouget022d3721999-03-27 16:56:13 +0000307static HRESULT WINAPI IDirect3DTexture2Impl_GetHandle(LPDIRECT3DTEXTURE2 iface,
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000308 LPDIRECT3DDEVICE2 lpD3DDevice2,
309 LPD3DTEXTUREHANDLE lpHandle)
310{
Francois Gouget022d3721999-03-27 16:56:13 +0000311 ICOM_THIS(IDirect3DTexture2Impl,iface);
312 IDirect3DDevice2Impl* ilpD3DDevice2=(IDirect3DDevice2Impl*)lpD3DDevice2;
Alexandre Julliard61fece01999-06-26 19:09:08 +0000313 TRACE("(%p)->(%p,%p)\n", This, ilpD3DDevice2, lpHandle);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000314
Alexandre Julliard638f1691999-01-17 16:32:32 +0000315 /* For 32 bits OSes, handles = pointers */
Francois Gouget022d3721999-03-27 16:56:13 +0000316 *lpHandle = (D3DTEXTUREHANDLE) This;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000317
Alexandre Julliard638f1691999-01-17 16:32:32 +0000318 /* Now, bind a new texture */
Lionel Ulmerad725851999-05-13 18:53:05 +0000319 ENTER_GL();
Francois Gouget022d3721999-03-27 16:56:13 +0000320 ilpD3DDevice2->set_context(ilpD3DDevice2);
321 This->D3Ddevice = (void *) ilpD3DDevice2;
322 if (This->tex_name == 0)
323 glGenTextures(1, &(This->tex_name));
Lionel Ulmerad725851999-05-13 18:53:05 +0000324 LEAVE_GL();
Alexandre Julliard638f1691999-01-17 16:32:32 +0000325
Andreas Mohr8cd93512000-01-29 21:12:58 +0000326 TRACE("OpenGL texture handle is : %d\n", This->tex_name);
Alexandre Julliard638f1691999-01-17 16:32:32 +0000327
328 return D3D_OK;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000329}
330
331/* Common methods */
Francois Gouget022d3721999-03-27 16:56:13 +0000332static HRESULT WINAPI IDirect3DTexture2Impl_PaletteChanged(LPDIRECT3DTEXTURE2 iface,
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000333 DWORD dwStart,
334 DWORD dwCount)
335{
Francois Gouget022d3721999-03-27 16:56:13 +0000336 ICOM_THIS(IDirect3DTexture2Impl,iface);
Alexandre Julliard61fece01999-06-26 19:09:08 +0000337 FIXME("(%p)->(%8ld,%8ld): stub\n", This, dwStart, dwCount);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000338
Alexandre Julliard638f1691999-01-17 16:32:32 +0000339 return D3D_OK;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000340}
341
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000342/* NOTE : if you experience crashes in this function, you must have a buggy
343 version of Mesa. See the file d3dtexture.c for a cure */
Francois Gouget022d3721999-03-27 16:56:13 +0000344static HRESULT WINAPI IDirect3DTexture2Impl_Load(LPDIRECT3DTEXTURE2 iface,
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000345 LPDIRECT3DTEXTURE2 lpD3DTexture2)
346{
Francois Gouget022d3721999-03-27 16:56:13 +0000347 ICOM_THIS(IDirect3DTexture2Impl,iface);
348 IDirect3DTexture2Impl* ilpD3DTexture2=(IDirect3DTexture2Impl*)lpD3DTexture2;
Alexandre Julliard638f1691999-01-17 16:32:32 +0000349 DDSURFACEDESC *src_d, *dst_d;
Alexandre Julliard61fece01999-06-26 19:09:08 +0000350 TRACE("(%p)->(%p)\n", This, ilpD3DTexture2);
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000351
Alexandre Julliard61fece01999-06-26 19:09:08 +0000352 TRACE("Copied surface %p to surface %p\n", ilpD3DTexture2->surface, This->surface);
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000353
354 /* Suppress the ALLOCONLOAD flag */
Francois Gouget022d3721999-03-27 16:56:13 +0000355 This->surface->s.surface_desc.ddsCaps.dwCaps &= ~DDSCAPS_ALLOCONLOAD;
Alexandre Julliard638f1691999-01-17 16:32:32 +0000356
357 /* Copy one surface on the other */
Francois Gouget022d3721999-03-27 16:56:13 +0000358 dst_d = &(This->surface->s.surface_desc);
359 src_d = &(ilpD3DTexture2->surface->s.surface_desc);
Alexandre Julliard638f1691999-01-17 16:32:32 +0000360
Lionel Ulmer81c75441999-04-06 07:10:48 +0000361 /* Install the callbacks to the destination surface */
362 This->surface->s.texture = This;
363 This->surface->s.SetColorKey_cb = SetColorKey_cb;
364
Alexandre Julliard638f1691999-01-17 16:32:32 +0000365 if ((src_d->dwWidth != dst_d->dwWidth) || (src_d->dwHeight != dst_d->dwHeight)) {
366 /* Should also check for same pixel format, lPitch, ... */
Alexandre Julliard61fece01999-06-26 19:09:08 +0000367 ERR("Error in surface sizes\n");
Alexandre Julliard638f1691999-01-17 16:32:32 +0000368 return D3DERR_TEXTURE_LOAD_FAILED;
369 } else {
Francois Gouget022d3721999-03-27 16:56:13 +0000370 /* LPDIRECT3DDEVICE2 d3dd = (LPDIRECT3DDEVICE2) This->D3Ddevice; */
Alexandre Julliard638f1691999-01-17 16:32:32 +0000371 /* I should put a macro for the calculus of bpp */
372 int bpp = (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8 ?
373 1 /* 8 bit of palette index */:
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000374 src_d->ddpfPixelFormat.u.dwRGBBitCount / 8 /* RGB bits for each colors */ );
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000375 GLuint current_texture;
Alexandre Julliard638f1691999-01-17 16:32:32 +0000376
Lionel Ulmer81c75441999-04-06 07:10:48 +0000377 /* Copy the main memry texture into the surface that corresponds to the OpenGL
378 texture object. */
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000379 memcpy(dst_d->u1.lpSurface, src_d->u1.lpSurface, src_d->dwWidth * src_d->dwHeight * bpp);
Alexandre Julliard638f1691999-01-17 16:32:32 +0000380
Lionel Ulmerad725851999-05-13 18:53:05 +0000381 ENTER_GL();
382
Alexandre Julliard638f1691999-01-17 16:32:32 +0000383 /* Now, load the texture */
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000384 /* d3dd->set_context(d3dd); We need to set the context somehow.... */
385 glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_texture);
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000386
387 /* If the GetHandle was not done, get the texture name here */
Francois Gouget022d3721999-03-27 16:56:13 +0000388 if (This->tex_name == 0)
389 glGenTextures(1, &(This->tex_name));
390 glBindTexture(GL_TEXTURE_2D, This->tex_name);
Alexandre Julliard638f1691999-01-17 16:32:32 +0000391
392 if (src_d->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000393 /* ****************
394 Paletted Texture
395 **************** */
Francois Gouget022d3721999-03-27 16:56:13 +0000396 IDirectDrawPaletteImpl* pal = This->surface->s.palette;
Alexandre Julliard638f1691999-01-17 16:32:32 +0000397 BYTE table[256][4];
398 int i;
399
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000400 if (pal == NULL) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000401 ERR("Palettized texture Loading with a NULL palette !\n");
Lionel Ulmerad725851999-05-13 18:53:05 +0000402 LEAVE_GL();
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000403 return D3DERR_TEXTURE_LOAD_FAILED;
404 }
405
Alexandre Julliard638f1691999-01-17 16:32:32 +0000406 /* Get the surface's palette */
407 for (i = 0; i < 256; i++) {
408 table[i][0] = pal->palents[i].peRed;
409 table[i][1] = pal->palents[i].peGreen;
410 table[i][2] = pal->palents[i].peBlue;
Francois Gouget022d3721999-03-27 16:56:13 +0000411 if ((This->surface->s.surface_desc.dwFlags & DDSD_CKSRCBLT) &&
412 (i >= This->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceLowValue) &&
413 (i <= This->surface->s.surface_desc.ddckCKSrcBlt.dwColorSpaceHighValue))
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000414 table[i][3] = 0x00;
415 else
Alexandre Julliard638f1691999-01-17 16:32:32 +0000416 table[i][3] = 0xFF;
417 }
418
Lionel Ulmer81c75441999-04-06 07:10:48 +0000419 /* Texture snooping */
420 SNOOP_PALETTED();
Lionel Ulmer48c08162000-01-05 01:51:02 +0000421
422#if defined(HAVE_GL_COLOR_TABLE) && defined(HAVE_GL_PALETTED_TEXTURE)
423 /* use Paletted Texture Extension */
Alexandre Julliard638f1691999-01-17 16:32:32 +0000424 glColorTableEXT(GL_TEXTURE_2D, /* target */
425 GL_RGBA, /* internal format */
426 256, /* table size */
427 GL_RGBA, /* table format */
428 GL_UNSIGNED_BYTE, /* table type */
429 table); /* the color table */
430
431 glTexImage2D(GL_TEXTURE_2D, /* target */
432 0, /* level */
433 GL_COLOR_INDEX8_EXT, /* internal format */
434 src_d->dwWidth, src_d->dwHeight, /* width, height */
435 0, /* border */
436 GL_COLOR_INDEX, /* texture format */
437 GL_UNSIGNED_BYTE, /* texture type */
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000438 src_d->u1.lpSurface); /* the texture */
Lionel Ulmer48c08162000-01-05 01:51:02 +0000439#endif
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000440 } else if (src_d->ddpfPixelFormat.dwFlags & DDPF_RGB) {
441 /* ************
442 RGB Textures
443 ************ */
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000444 if (src_d->ddpfPixelFormat.u.dwRGBBitCount == 8) {
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000445 /* **********************
446 GL_UNSIGNED_BYTE_3_3_2
447 ********************** */
448 glTexImage2D(GL_TEXTURE_2D,
449 0,
450 GL_RGB,
451 src_d->dwWidth, src_d->dwHeight,
452 0,
453 GL_RGB,
454 GL_UNSIGNED_BYTE_3_3_2,
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000455 src_d->u1.lpSurface);
456 } else if (src_d->ddpfPixelFormat.u.dwRGBBitCount == 16) {
457 if (src_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x00000000) {
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000458
Lionel Ulmer81c75441999-04-06 07:10:48 +0000459 /* Texture snooping */
460 SNOOP_5650();
461
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000462 glTexImage2D(GL_TEXTURE_2D,
463 0,
464 GL_RGB,
465 src_d->dwWidth, src_d->dwHeight,
466 0,
467 GL_RGB,
468 GL_UNSIGNED_SHORT_5_6_5,
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000469 src_d->u1.lpSurface);
470 } else if (src_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x00000001) {
Lionel Ulmer81c75441999-04-06 07:10:48 +0000471 /* Texture snooping */
472 SNOOP_5551();
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000473
474 glTexImage2D(GL_TEXTURE_2D,
475 0,
476 GL_RGBA,
477 src_d->dwWidth, src_d->dwHeight,
478 0,
479 GL_RGBA,
480 GL_UNSIGNED_SHORT_5_5_5_1,
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000481 src_d->u1.lpSurface);
482 } else if (src_d->ddpfPixelFormat.u4.dwRGBAlphaBitMask == 0x0000000F) {
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000483 glTexImage2D(GL_TEXTURE_2D,
484 0,
485 GL_RGBA,
486 src_d->dwWidth, src_d->dwHeight,
487 0,
488 GL_RGBA,
489 GL_UNSIGNED_SHORT_4_4_4_4,
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000490 src_d->u1.lpSurface);
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000491 } else {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000492 ERR("Unhandled texture format (bad Aplha channel for a 16 bit texture)\n");
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000493 }
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000494 } else if (src_d->ddpfPixelFormat.u.dwRGBBitCount == 24) {
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000495 glTexImage2D(GL_TEXTURE_2D,
496 0,
497 GL_RGB,
498 src_d->dwWidth, src_d->dwHeight,
499 0,
500 GL_RGB,
501 GL_UNSIGNED_BYTE,
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000502 src_d->u1.lpSurface);
503 } else if (src_d->ddpfPixelFormat.u.dwRGBBitCount == 32) {
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000504 glTexImage2D(GL_TEXTURE_2D,
505 0,
506 GL_RGBA,
507 src_d->dwWidth, src_d->dwHeight,
508 0,
509 GL_RGBA,
510 GL_UNSIGNED_BYTE,
Ove Kaaven1d4bccc1999-10-23 18:52:17 +0000511 src_d->u1.lpSurface);
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000512 } else {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000513 ERR("Unhandled texture format (bad RGB count)\n");
Lionel Ulmer1d3e5011999-01-23 12:32:46 +0000514 }
Alexandre Julliard638f1691999-01-17 16:32:32 +0000515 } else {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000516 ERR("Unhandled texture format (neither RGB nor INDEX)\n");
Alexandre Julliard638f1691999-01-17 16:32:32 +0000517 }
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000518
519 glBindTexture(GL_TEXTURE_2D, current_texture);
Lionel Ulmerad725851999-05-13 18:53:05 +0000520
521 LEAVE_GL();
Alexandre Julliard638f1691999-01-17 16:32:32 +0000522 }
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000523
Alexandre Julliard8da12c41999-01-17 16:55:11 +0000524 return D3D_OK;
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000525}
526
527
528/*******************************************************************************
529 * IDirect3DTexture2 VTable
530 */
Paul Quinn2305f3c1999-05-22 11:41:38 +0000531static ICOM_VTABLE(IDirect3DTexture2) texture2_vtable =
532{
533 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000534 /*** IUnknown methods ***/
Francois Gouget022d3721999-03-27 16:56:13 +0000535 IDirect3DTexture2Impl_QueryInterface,
536 IDirect3DTexture2Impl_AddRef,
537 IDirect3DTexture2Impl_Release,
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000538 /*** IDirect3DTexture methods ***/
Francois Gouget022d3721999-03-27 16:56:13 +0000539 IDirect3DTexture2Impl_GetHandle,
540 IDirect3DTexture2Impl_PaletteChanged,
541 IDirect3DTexture2Impl_Load
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000542};
543
544/*******************************************************************************
545 * IDirect3DTexture VTable
546 */
Paul Quinn2305f3c1999-05-22 11:41:38 +0000547static ICOM_VTABLE(IDirect3DTexture) texture_vtable =
548{
549 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000550 /*** IUnknown methods ***/
Francois Gouget022d3721999-03-27 16:56:13 +0000551 IDirect3DTexture2Impl_QueryInterface,
552 IDirect3DTexture2Impl_AddRef,
553 IDirect3DTexture2Impl_Release,
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000554 /*** IDirect3DTexture methods ***/
Francois Gouget022d3721999-03-27 16:56:13 +0000555 IDirect3DTextureImpl_Initialize,
556 IDirect3DTextureImpl_GetHandle,
557 IDirect3DTexture2Impl_PaletteChanged,
558 IDirect3DTexture2Impl_Load,
559 IDirect3DTextureImpl_Unload
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000560};
561
562#else /* HAVE_MESAGL */
563
564/* These function should never be called if MesaGL is not present */
Francois Gouget022d3721999-03-27 16:56:13 +0000565LPDIRECT3DTEXTURE2 d3dtexture2_create(IDirectDrawSurface4Impl* surf) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000566 ERR("Should not be called...\n");
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000567 return NULL;
568}
569
Francois Gouget022d3721999-03-27 16:56:13 +0000570LPDIRECT3DTEXTURE d3dtexture_create(IDirectDrawSurface4Impl* surf) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000571 ERR("Should not be called...\n");
Lionel Ulmer5ff00771999-01-03 17:00:19 +0000572 return NULL;
573}
574
575#endif /* HAVE_MESAGL */