DXGL r609 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r608‎ | r609 | r610 >
Date:18:29, 3 May 2015
Author:admin
Status:new
Tags:
Comment:
Move texture state to glUtil object.
TextureManager object will be folded into TEXTURE object soon.
Modified paths:
  • /ddraw/TextureManager.c (deleted) (history)
  • /ddraw/TextureManager.cpp (added) (history)
  • /ddraw/TextureManager.h (modified) (history)
  • /ddraw/ddraw.vcxproj (modified) (history)
  • /ddraw/ddraw.vcxproj.filters (modified) (history)
  • /ddraw/glDirectDrawSurface.cpp (modified) (history)
  • /ddraw/glDirectDrawSurface.h (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/glUtil.cpp (modified) (history)
  • /ddraw/glUtil.h (modified) (history)

Diff [purge]

Index: ddraw/TextureManager.c
@@ -1,596 +0,0 @@
2 -// DXGL
3 -// Copyright (C) 2012-2014 William Feely
4 -
5 -// This library is free software; you can redistribute it and/or
6 -// modify it under the terms of the GNU Lesser General Public
7 -// License as published by the Free Software Foundation; either
8 -// version 2.1 of the License, or (at your option) any later version.
9 -
10 -// This library is distributed in the hope that it will be useful,
11 -// but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 -// Lesser General Public License for more details.
14 -
15 -// You should have received a copy of the GNU Lesser General Public
16 -// License along with this library; if not, write to the Free Software
17 -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 -
19 -#include "common.h"
20 -#include "BufferObject.h"
21 -#include "TextureManager.h"
22 -#include <math.h>
23 -
24 -// Use EXACTLY one line per entry. Don't change layout of the list.
25 -static const int START_TEXFORMATS = __LINE__;
26 -const DDPIXELFORMAT texformats[] =
27 -{ // Size Flags FOURCC bits R/Ymask G/U/Zmask B/V/STmask A/Zmask
28 - {sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8, 0, 8, 0, 0, 0, 0},
29 - {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 8, 0xE0, 0x1C, 0x3, 0},
30 - {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x7C00, 0x3E0, 0x1F, 0},
31 - {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0xF800, 0x7E0, 0x1F, 0},
32 - {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 24, 0xFF0000, 0xFF00, 0xFF, 0},
33 - {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0xFF0000, 0xFF00, 0xFF, 0},
34 - {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0xFF, 0xFF00, 0xFF0000, 0},
35 - {sizeof(DDPIXELFORMAT), DDPF_RGB|DDPF_ALPHAPIXELS, 0, 16, 0xE0, 0x1C, 0x3, 0xFF00},
36 - {sizeof(DDPIXELFORMAT), DDPF_RGB|DDPF_ALPHAPIXELS, 0, 16, 0xF00, 0xF0, 0xF, 0xF000},
37 - {sizeof(DDPIXELFORMAT), DDPF_RGB|DDPF_ALPHAPIXELS, 0, 16, 0x7c00, 0x3E0, 0x1F, 0x8000},
38 - {sizeof(DDPIXELFORMAT), DDPF_RGB|DDPF_ALPHAPIXELS, 0, 32, 0xFF0000, 0xFF00, 0xFF, 0xFF000000},
39 - {sizeof(DDPIXELFORMAT), DDPF_LUMINANCE, 0, 8, 0xFF, 0, 0, 0},
40 - {sizeof(DDPIXELFORMAT), DDPF_ALPHA, 0, 8, 0, 0, 0, 0},
41 - {sizeof(DDPIXELFORMAT), DDPF_LUMINANCE|DDPF_ALPHAPIXELS,0, 16, 0xFF, 0, 0, 0xFF00},
42 - {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 16, 0, 0xFFFF, 0, 0},
43 - {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 24, 0, 0xFFFFFF00, 0, 0},
44 - {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 32, 0, 0xFFFFFF00, 0, 0},
45 - {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 32, 0, 0xFFFFFFFF, 0, 0},
46 - {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 32, 8, 0xFFFFFF00, 0xFF, 0},
47 - {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 32, 8, 0xFF, 0xFFFFFF00, 0}
48 -};
49 -static const int END_TEXFORMATS = __LINE__ - 4;
50 -int numtexformats;
51 -
52 -void ClearError()
53 -{
54 - do
55 - {
56 - if (glGetError() == GL_NO_ERROR) break;
57 - } while (1);
58 -}
59 -
60 -DWORD CalculateMipLevels(DWORD width, DWORD height)
61 -{
62 - DWORD x, y;
63 - DWORD levels = 1;
64 - if ((!width) || (!height)) return 0;
65 - if ((width == 1) || (height == 1)) return 1;
66 - x = width;
67 - y = height;
68 -miploop:
69 - x = max(1,(DWORD)floorf((float)x / 2.0f));
70 - y = max(1,(DWORD)floorf((float)y / 2.0f));
71 - levels++;
72 - if ((x == 1) || (y == 1)) return levels;
73 - else goto miploop;
74 -}
75 -
76 -void ShrinkMip(DWORD *x, DWORD *y)
77 -{
78 - *x = max(1, (DWORD)floorf((float)*x / 2.0f));
79 - *y = max(1, (DWORD)floorf((float)*y / 2.0f));
80 -}
81 -
82 -TextureManager *TextureManager_Create(glExtensions *glext)
83 -{
84 - TextureManager *newtex;
85 - numtexformats = END_TEXFORMATS - START_TEXFORMATS;
86 - newtex = (TextureManager*)malloc(sizeof(TextureManager));
87 - if (!newtex) return 0;
88 - ZeroMemory(newtex, sizeof(TextureManager));
89 - newtex->ext = glext;
90 - newtex->texlevel = 0;
91 - ZeroMemory(newtex->textures, 16 * sizeof(GLuint));
92 - return newtex;
93 -}
94 -
95 -void TextureManager__CreateTexture(TextureManager *This, TEXTURE *texture, int width, int height)
96 -{
97 - TextureManager_CreateTextureClassic(This, texture, width, height);
98 -}
99 -void TextureManager__DeleteTexture(TextureManager *This, TEXTURE *texture)
100 -{
101 - TextureManager_DeleteTexture(This, texture);
102 -}
103 -void TextureManager__UploadTexture(TextureManager *This, TEXTURE *texture, int level, const void *data, int width, int height, BOOL checkerror, BOOL realloc)
104 -{
105 - TextureManager_UploadTextureClassic(This, texture, level, data, width, height, checkerror, realloc);
106 -}
107 -void TextureManager__DownloadTexture(TextureManager *This, TEXTURE *texture, int level, void *data)
108 -{
109 - TextureManager_DownloadTextureClassic(This, texture, level, data);
110 -}
111 -
112 -void TextureManager_CreateTextureClassic(TextureManager *This, TEXTURE *texture, int width, int height)
113 -{
114 - int texformat = -1;
115 - int i;
116 - int x, y;
117 - GLenum error;
118 - if (!texture->miplevel) texture->miplevel = 1;
119 - texture->pixelformat.dwSize = sizeof(DDPIXELFORMAT);
120 - for(i = 0; i < numtexformats; i++)
121 - {
122 - if(!memcmp(&texformats[i],&texture->pixelformat,sizeof(DDPIXELFORMAT)))
123 - {
124 - texformat = i;
125 - break;
126 - }
127 - }
128 - ZeroMemory(texture->internalformats, 8 * sizeof(GLint));
129 - switch(texformat)
130 - {
131 - case -1:
132 - case 0: // 8-bit palette
133 - if(This->ext->glver_major >= 3)
134 - {
135 - texture->internalformats[0] = GL_R8;
136 - texture->format = GL_RED;
137 - }
138 - else
139 - {
140 - texture->internalformats[0] = GL_RGBA8;
141 - texture->format = GL_LUMINANCE;
142 - }
143 - texture->type = GL_UNSIGNED_BYTE;
144 - texture->colororder = 4;
145 - texture->colorsizes[0] = 255;
146 - texture->colorsizes[1] = 255;
147 - texture->colorsizes[2] = 255;
148 - texture->colorsizes[3] = 255;
149 - texture->colorbits[0] = 8;
150 - texture->colorbits[1] = 0;
151 - texture->colorbits[2] = 0;
152 - texture->colorbits[3] = 0;
153 - break;
154 - case 1: // 8-bit RGB332
155 - texture->internalformats[0] = GL_R3_G3_B2;
156 - texture->internalformats[1] = GL_RGB8;
157 - texture->internalformats[2] = GL_RGBA8;
158 - texture->format = GL_RGB;
159 - texture->type = GL_UNSIGNED_BYTE_3_3_2;
160 - texture->colororder = 1;
161 - texture->colorsizes[0] = 7;
162 - texture->colorsizes[1] = 7;
163 - texture->colorsizes[2] = 3;
164 - texture->colorsizes[3] = 1;
165 - texture->colorbits[0] = 3;
166 - texture->colorbits[1] = 3;
167 - texture->colorbits[2] = 2;
168 - texture->colorbits[3] = 0;
169 - break;
170 - case 2: // 16-bit RGB555
171 - texture->internalformats[0] = GL_RGB5_A1;
172 - texture->internalformats[1] = GL_RGBA8;
173 - texture->format = GL_BGRA;
174 - texture->type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
175 - texture->colororder = 1;
176 - texture->colorsizes[0] = 31;
177 - texture->colorsizes[1] = 31;
178 - texture->colorsizes[2] = 31;
179 - texture->colorsizes[3] = 1;
180 - texture->colorbits[0] = 5;
181 - texture->colorbits[1] = 5;
182 - texture->colorbits[2] = 5;
183 - texture->colorbits[3] = 1;
184 - break;
185 - case 3: // 16-bit RGB565
186 - texture->internalformats[0] = GL_RGB565;
187 - texture->internalformats[1] = GL_RGB8;
188 - texture->internalformats[2] = GL_RGBA8;
189 - texture->format = GL_RGB;
190 - texture->type = GL_UNSIGNED_SHORT_5_6_5;
191 - texture->colororder = 1;
192 - texture->colorsizes[0] = 31;
193 - texture->colorsizes[1] = 63;
194 - texture->colorsizes[2] = 31;
195 - texture->colorsizes[3] = 1;
196 - texture->colorbits[0] = 5;
197 - texture->colorbits[1] = 6;
198 - texture->colorbits[2] = 5;
199 - texture->colorbits[3] = 0;
200 - break;
201 - case 4: // 24-bit RGB888
202 - texture->internalformats[0] = GL_RGB8;
203 - texture->internalformats[1] = GL_RGBA8;
204 - texture->format = GL_BGR;
205 - texture->type = GL_UNSIGNED_BYTE;
206 - texture->colororder = 1;
207 - texture->colorsizes[0] = 255;
208 - texture->colorsizes[1] = 255;
209 - texture->colorsizes[2] = 255;
210 - texture->colorsizes[3] = 1;
211 - texture->colorbits[0] = 8;
212 - texture->colorbits[1] = 8;
213 - texture->colorbits[2] = 8;
214 - texture->colorbits[3] = 0;
215 - break;
216 - case 5: // 32-bit RGB888
217 - texture->internalformats[0] = GL_RGBA8;
218 - texture->format = GL_BGRA;
219 - texture->type = GL_UNSIGNED_INT_8_8_8_8_REV;
220 - texture->colororder = 1;
221 - texture->colorsizes[0] = 255;
222 - texture->colorsizes[1] = 255;
223 - texture->colorsizes[2] = 255;
224 - texture->colorsizes[3] = 1;
225 - texture->colorbits[0] = 8;
226 - texture->colorbits[1] = 8;
227 - texture->colorbits[2] = 8;
228 - texture->colorbits[3] = 0;
229 - break;
230 - case 6: // 32-bit BGR888
231 - texture->internalformats[0] = GL_RGBA8;
232 - texture->format = GL_RGBA;
233 - texture->type = GL_UNSIGNED_INT_8_8_8_8_REV;
234 - texture->colororder = 0;
235 - texture->colorsizes[0] = 255;
236 - texture->colorsizes[1] = 255;
237 - texture->colorsizes[2] = 255;
238 - texture->colorsizes[3] = 1;
239 - texture->colorbits[0] = 8;
240 - texture->colorbits[1] = 8;
241 - texture->colorbits[2] = 8;
242 - texture->colorbits[3] = 0;
243 - break;
244 - case 7: // 16-bit RGBA8332
245 - FIXME("Unusual texture format RGBA8332 not supported");
246 - texture->colororder = 1;
247 - texture->colorsizes[0] = 7;
248 - texture->colorsizes[1] = 7;
249 - texture->colorsizes[2] = 3;
250 - texture->colorsizes[3] = 255;
251 - texture->colorbits[0] = 3;
252 - texture->colorbits[1] = 3;
253 - texture->colorbits[2] = 2;
254 - texture->colorbits[3] = 8;
255 - break;
256 - case 8: // 16-bit RGBA4444
257 - texture->internalformats[0] = GL_RGBA4;
258 - texture->internalformats[1] = GL_RGBA8;
259 - texture->format = GL_BGRA;
260 - texture->type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
261 - texture->colororder = 1;
262 - texture->colorsizes[0] = 15;
263 - texture->colorsizes[1] = 15;
264 - texture->colorsizes[2] = 15;
265 - texture->colorsizes[3] = 15;
266 - texture->colorbits[0] = 4;
267 - texture->colorbits[1] = 4;
268 - texture->colorbits[2] = 4;
269 - texture->colorbits[3] = 4;
270 - break;
271 - case 9: // 16-bit RGBA1555
272 - texture->internalformats[0] = GL_RGB5_A1;
273 - texture->internalformats[1] = GL_RGBA8;
274 - texture->format = GL_BGRA;
275 - texture->type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
276 - texture->colorbits[0] = 5;
277 - texture->colorbits[1] = 5;
278 - texture->colorbits[2] = 5;
279 - texture->colorbits[3] = 1;
280 - break;
281 - case 10: // 32-bit RGBA8888
282 - texture->internalformats[0] = GL_RGBA8;
283 - texture->format = GL_BGRA;
284 - texture->type = GL_UNSIGNED_INT_8_8_8_8_REV;
285 - texture->colororder = 1;
286 - texture->colorsizes[0] = 255;
287 - texture->colorsizes[1] = 255;
288 - texture->colorsizes[2] = 255;
289 - texture->colorsizes[3] = 255;
290 - texture->colorbits[0] = 8;
291 - texture->colorbits[1] = 8;
292 - texture->colorbits[2] = 8;
293 - texture->colorbits[3] = 8;
294 - break;
295 - case 11: // 8-bit Luminance
296 - texture->internalformats[0] = GL_LUMINANCE8;
297 - texture->internalformats[1] = GL_RGB8;
298 - texture->internalformats[2] = GL_RGBA8;
299 - texture->format = GL_LUMINANCE;
300 - texture->type = GL_UNSIGNED_BYTE;
301 - texture->colororder = 5;
302 - texture->colorsizes[0] = 255;
303 - texture->colorsizes[1] = 255;
304 - texture->colorsizes[2] = 255;
305 - texture->colorsizes[3] = 255;
306 - texture->colorbits[0] = 8;
307 - texture->colorbits[1] = 0;
308 - texture->colorbits[2] = 0;
309 - texture->colorbits[3] = 0;
310 - break;
311 - case 12: // 8-bit Alpha
312 - texture->internalformats[0] = GL_ALPHA8;
313 - texture->format = GL_ALPHA;
314 - texture->type = GL_UNSIGNED_BYTE;
315 - texture->colororder = 6;
316 - texture->colorsizes[0] = 255;
317 - texture->colorsizes[1] = 255;
318 - texture->colorsizes[2] = 255;
319 - texture->colorsizes[3] = 255;
320 - texture->colorbits[0] = 0;
321 - texture->colorbits[1] = 0;
322 - texture->colorbits[2] = 0;
323 - texture->colorbits[3] = 8;
324 - break;
325 - case 13: // 16-bit Luminance Alpha
326 - texture->internalformats[0] = GL_LUMINANCE8_ALPHA8;
327 - texture->internalformats[1] = GL_RGBA8;
328 - texture->format = GL_LUMINANCE_ALPHA;
329 - texture->type = GL_UNSIGNED_BYTE;
330 - texture->colororder = 7;
331 - texture->colorsizes[0] = 255;
332 - texture->colorsizes[1] = 255;
333 - texture->colorsizes[2] = 255;
334 - texture->colorsizes[3] = 255;
335 - texture->colorbits[0] = 8;
336 - texture->colorbits[1] = 0;
337 - texture->colorbits[2] = 0;
338 - texture->colorbits[3] = 8;
339 - break;
340 - case 14: // 16-bit Z buffer
341 - texture->internalformats[0] = GL_DEPTH_COMPONENT16;
342 - texture->format = GL_DEPTH_COMPONENT;
343 - texture->type = GL_UNSIGNED_SHORT;
344 - texture->colororder = 4;
345 - texture->colorsizes[0] = 65535;
346 - texture->colorsizes[1] = 65535;
347 - texture->colorsizes[2] = 65535;
348 - texture->colorsizes[3] = 65535;
349 - texture->colorbits[0] = 16;
350 - texture->colorbits[1] = 0;
351 - texture->colorbits[2] = 0;
352 - texture->colorbits[3] = 0;
353 - break;
354 - case 15: // 24-bit Z buffer
355 - texture->internalformats[0] = GL_DEPTH_COMPONENT24;
356 - texture->format = GL_DEPTH_COMPONENT;
357 - texture->type = GL_UNSIGNED_INT;
358 - texture->colororder = 4;
359 - texture->colorsizes[0] = 16777215;
360 - texture->colorsizes[1] = 16777215;
361 - texture->colorsizes[2] = 16777215;
362 - texture->colorsizes[3] = 16777215;
363 - texture->colorbits[0] = 24;
364 - texture->colorbits[1] = 0;
365 - texture->colorbits[2] = 0;
366 - texture->colorbits[3] = 0;
367 - break;
368 - case 16: // 32/24 bit Z buffer
369 - texture->internalformats[0] = GL_DEPTH_COMPONENT24;
370 - texture->format = GL_DEPTH_COMPONENT;
371 - texture->type = GL_UNSIGNED_INT;
372 - texture->colororder = 4;
373 - texture->colorsizes[0] = 16777215;
374 - texture->colorsizes[1] = 16777215;
375 - texture->colorsizes[2] = 16777215;
376 - texture->colorsizes[3] = 16777215;
377 - texture->colorbits[0] = 24;
378 - texture->colorbits[1] = 0;
379 - texture->colorbits[2] = 0;
380 - texture->colorbits[3] = 0;
381 - break;
382 - case 17: // 32-bit Z buffer
383 - texture->internalformats[0] = GL_DEPTH_COMPONENT32;
384 - texture->format = GL_DEPTH_COMPONENT;
385 - texture->type = GL_UNSIGNED_INT;
386 - texture->colororder = 4;
387 - texture->colorsizes[0] = 4294967295;
388 - texture->colorsizes[1] = 4294967295;
389 - texture->colorsizes[2] = 4294967295;
390 - texture->colorsizes[3] = 4294967295;
391 - texture->colorbits[0] = 32;
392 - texture->colorbits[1] = 0;
393 - texture->colorbits[2] = 0;
394 - texture->colorbits[3] = 0;
395 - break;
396 - case 18: // 32-bit Z/Stencil buffer, depth LSB
397 - texture->internalformats[0] = GL_DEPTH24_STENCIL8;
398 - texture->format = GL_DEPTH_STENCIL;
399 - texture->type = GL_UNSIGNED_INT_24_8;
400 - texture->colororder = 7;
401 - texture->colorsizes[0] = 16777215;
402 - texture->colorsizes[1] = 16777215;
403 - texture->colorsizes[2] = 16777215;
404 - texture->colorsizes[3] = 255;
405 - texture->colorbits[0] = 24;
406 - texture->colorbits[1] = 0;
407 - texture->colorbits[2] = 0;
408 - texture->colorbits[3] = 8;
409 - break;
410 - case 19: // 32-bit Z/Stencil buffer, depth MSB
411 - texture->internalformats[0] = GL_DEPTH24_STENCIL8;
412 - texture->format = GL_DEPTH_STENCIL;
413 - texture->type = GL_UNSIGNED_INT_24_8;
414 - texture->colororder = 7;
415 - texture->colorsizes[0] = 16777215;
416 - texture->colorsizes[1] = 16777215;
417 - texture->colorsizes[2] = 16777215;
418 - texture->colorsizes[3] = 255;
419 - texture->colorbits[0] = 24;
420 - texture->colorbits[1] = 0;
421 - texture->colorbits[2] = 0;
422 - texture->colorbits[3] = 8;
423 - break;
424 - }
425 - texture->width = width;
426 - texture->height = height;
427 - glGenTextures(1,&texture->id);
428 - TextureManager_SetTexture(This,0,texture);
429 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texture->minfilter);
430 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texture->magfilter);
431 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texture->wraps);
432 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texture->wrapt);
433 - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, texture->miplevel - 1);
434 - x = texture->width;
435 - y = texture->height;
436 - for (i = 0; i < texture->miplevel; i++)
437 - {
438 - do
439 - {
440 - ClearError();
441 - glTexImage2D(GL_TEXTURE_2D, i, texture->internalformats[0], x, y, 0, texture->format, texture->type, NULL);
442 - ShrinkMip(&x, &y);
443 - error = glGetError();
444 - if (error != GL_NO_ERROR)
445 - {
446 - if (texture->internalformats[1] == 0)
447 - {
448 - FIXME("Failed to create texture, cannot find internal format");
449 - break;
450 - }
451 - memmove(&texture->internalformats[0], &texture->internalformats[1], 7 * sizeof(GLint));
452 - texture->internalformats[7] = 0;
453 - }
454 - else break;
455 - } while (1);
456 - }
457 -}
458 -
459 -void TextureManager_DeleteTexture(TextureManager *This, TEXTURE *texture)
460 -{
461 - glDeleteTextures(1,&texture->id);
462 - texture->bordercolor = texture->format = texture->type = texture->width =
463 - texture->height = texture->magfilter = texture->minfilter =
464 - texture->miplevel = texture->wraps = texture->wrapt =
465 - texture->id = 0;
466 - texture->pboPack = texture->pboUnpack = NULL;
467 - ZeroMemory(texture->internalformats, 8 * sizeof(GLint));
468 -}
469 -
470 -void TextureManager_UploadTextureClassic(TextureManager *This, TEXTURE *texture, int level, const void *data, int width, int height, BOOL checkerror, BOOL realloc)
471 -{
472 - GLenum error;
473 - texture->width = width;
474 - texture->height = height;
475 - if (checkerror)
476 - {
477 - do
478 - {
479 - ClearError();
480 - if (This->ext->GLEXT_EXT_direct_state_access)
481 - {
482 - if (realloc)This->ext->glTextureImage2DEXT(texture->id, GL_TEXTURE_2D, level, texture->internalformats[0],
483 - width, height, 0, texture->format, texture->type, data);
484 - else This->ext->glTextureSubImage2DEXT(texture->id, GL_TEXTURE_2D, level, 0, 0, width, height, texture->format, texture->type, data);
485 - }
486 - else
487 - {
488 - TextureManager_SetActiveTexture(This, 0);
489 - TextureManager_SetTexture(This, 0, texture);
490 - if (realloc)glTexImage2D(GL_TEXTURE_2D, level, texture->internalformats[0], width, height, 0, texture->format, texture->type, data);
491 - else glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, texture->format, texture->type, data);
492 - }
493 - error = glGetError();
494 - if (error != GL_NO_ERROR)
495 - {
496 - if (texture->internalformats[1] == 0)
497 - {
498 - FIXME("Failed to update texture, cannot find internal format");
499 - break;
500 - }
501 - memmove(&texture->internalformats[0], &texture->internalformats[1], 7 * sizeof(GLint));
502 - texture->internalformats[7] = 0;
503 - }
504 - else break;
505 - } while (1);
506 - }
507 - else
508 - {
509 - if (This->ext->GLEXT_EXT_direct_state_access)
510 - {
511 - if (realloc)This->ext->glTextureImage2DEXT(texture->id, GL_TEXTURE_2D, level, texture->internalformats[0],
512 - width, height, 0, texture->format, texture->type, data);
513 - else This->ext->glTextureSubImage2DEXT(texture->id, GL_TEXTURE_2D, level, 0, 0, width, height, texture->format, texture->type, data);
514 - }
515 - else
516 - {
517 - TextureManager_SetActiveTexture(This, 0);
518 - TextureManager_SetTexture(This, 0, texture);
519 - if (realloc)glTexImage2D(GL_TEXTURE_2D, level, texture->internalformats[0], width, height, 0, texture->format, texture->type, data);
520 - else glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, texture->format, texture->type, data);
521 - }
522 - }
523 -}
524 -
525 -void TextureManager_DownloadTextureClassic(TextureManager *This, TEXTURE *texture, int level, void *data)
526 -{
527 - if(This->ext->GLEXT_EXT_direct_state_access) This->ext->glGetTextureImageEXT(texture->id,GL_TEXTURE_2D,level,texture->format,texture->type,data);
528 - else
529 - {
530 - TextureManager_SetActiveTexture(This, 0);
531 - TextureManager_SetTexture(This, 0,texture);
532 - glGetTexImage(GL_TEXTURE_2D,level,texture->format,texture->type,data);
533 - }
534 -}
535 -
536 -void TextureManager_SetActiveTexture(TextureManager *This, int level)
537 -{
538 - if(level != This->texlevel)
539 - {
540 - This->texlevel = level;
541 - This->ext->glActiveTexture(GL_TEXTURE0+level);
542 - }
543 -}
544 -
545 -
546 -void TextureManager_SetTexture(TextureManager *This, unsigned int level, TEXTURE *texture)
547 -{
548 - GLuint texname;
549 - if (level >= 16) return;
550 - if(!texture) texname = 0;
551 - else texname=texture->id;
552 - if(texname != This->textures[level])
553 - {
554 - TextureManager_SetActiveTexture(This, level);
555 - glBindTexture(GL_TEXTURE_2D,texname);
556 - }
557 -}
558 -
559 -BOOL TextureManager_FixTexture(TextureManager *This, TEXTURE *texture, void *data, DWORD *dirty, GLint level)
560 -{
561 - // data should be null to create uninitialized texture or be pointer to top-level
562 - // buffer to retain texture data
563 - TEXTURE newtexture;
564 - GLenum error;
565 - memcpy(&newtexture, texture, sizeof(TEXTURE));
566 - if (texture->internalformats[1] == 0) return FALSE;
567 - glGenTextures(1, &newtexture.id);
568 - TextureManager_SetActiveTexture(This, 0);
569 - if (data)
570 - {
571 - TextureManager_SetTexture(This, 0, texture);
572 - glGetTexImage(GL_TEXTURE_2D, level, texture->format, texture->type, data);
573 - if (dirty) *dirty |= 2;
574 - }
575 - TextureManager_SetTexture(This, 0, &newtexture);
576 - do
577 - {
578 - memmove(&newtexture.internalformats[0], &newtexture.internalformats[1], 7 * sizeof(GLint));
579 - newtexture.internalformats[7] = 0;
580 - ClearError();
581 - glTexImage2D(GL_TEXTURE_2D, level, newtexture.internalformats[0], newtexture.width, newtexture.height,
582 - 0, newtexture.format, newtexture.type, data);
583 - error = glGetError();
584 - if (error != GL_NO_ERROR)
585 - {
586 - if (newtexture.internalformats[1] == 0)
587 - {
588 - FIXME("Failed to repair texture, cannot find internal format");
589 - break;
590 - }
591 - }
592 - else break;
593 - } while (1);
594 - TextureManager__DeleteTexture(This, texture);
595 - memcpy(texture, &newtexture, sizeof(TEXTURE));
596 - return TRUE;
597 -}
\ No newline at end of file
Index: ddraw/TextureManager.cpp
@@ -0,0 +1,576 @@
 2+// DXGL
 3+// Copyright (C) 2012-2014 William Feely
 4+
 5+// This library is free software; you can redistribute it and/or
 6+// modify it under the terms of the GNU Lesser General Public
 7+// License as published by the Free Software Foundation; either
 8+// version 2.1 of the License, or (at your option) any later version.
 9+
 10+// This library is distributed in the hope that it will be useful,
 11+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 12+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13+// Lesser General Public License for more details.
 14+
 15+// You should have received a copy of the GNU Lesser General Public
 16+// License along with this library; if not, write to the Free Software
 17+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 18+
 19+#include "common.h"
 20+#include "BufferObject.h"
 21+#include "TextureManager.h"
 22+#include "glUtil.h"
 23+#include <math.h>
 24+
 25+extern "C" {
 26+
 27+// Use EXACTLY one line per entry. Don't change layout of the list.
 28+static const int START_TEXFORMATS = __LINE__;
 29+const DDPIXELFORMAT texformats[] =
 30+{ // Size Flags FOURCC bits R/Ymask G/U/Zmask B/V/STmask A/Zmask
 31+ {sizeof(DDPIXELFORMAT), DDPF_PALETTEINDEXED8, 0, 8, 0, 0, 0, 0},
 32+ {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 8, 0xE0, 0x1C, 0x3, 0},
 33+ {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x7C00, 0x3E0, 0x1F, 0},
 34+ {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0xF800, 0x7E0, 0x1F, 0},
 35+ {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 24, 0xFF0000, 0xFF00, 0xFF, 0},
 36+ {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0xFF0000, 0xFF00, 0xFF, 0},
 37+ {sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0xFF, 0xFF00, 0xFF0000, 0},
 38+ {sizeof(DDPIXELFORMAT), DDPF_RGB|DDPF_ALPHAPIXELS, 0, 16, 0xE0, 0x1C, 0x3, 0xFF00},
 39+ {sizeof(DDPIXELFORMAT), DDPF_RGB|DDPF_ALPHAPIXELS, 0, 16, 0xF00, 0xF0, 0xF, 0xF000},
 40+ {sizeof(DDPIXELFORMAT), DDPF_RGB|DDPF_ALPHAPIXELS, 0, 16, 0x7c00, 0x3E0, 0x1F, 0x8000},
 41+ {sizeof(DDPIXELFORMAT), DDPF_RGB|DDPF_ALPHAPIXELS, 0, 32, 0xFF0000, 0xFF00, 0xFF, 0xFF000000},
 42+ {sizeof(DDPIXELFORMAT), DDPF_LUMINANCE, 0, 8, 0xFF, 0, 0, 0},
 43+ {sizeof(DDPIXELFORMAT), DDPF_ALPHA, 0, 8, 0, 0, 0, 0},
 44+ {sizeof(DDPIXELFORMAT), DDPF_LUMINANCE|DDPF_ALPHAPIXELS,0, 16, 0xFF, 0, 0, 0xFF00},
 45+ {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 16, 0, 0xFFFF, 0, 0},
 46+ {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 24, 0, 0xFFFFFF00, 0, 0},
 47+ {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 32, 0, 0xFFFFFF00, 0, 0},
 48+ {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 32, 0, 0xFFFFFFFF, 0, 0},
 49+ {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 32, 8, 0xFFFFFF00, 0xFF, 0},
 50+ {sizeof(DDPIXELFORMAT), DDPF_ZBUFFER, 0, 32, 8, 0xFF, 0xFFFFFF00, 0}
 51+};
 52+static const int END_TEXFORMATS = __LINE__ - 4;
 53+int numtexformats;
 54+
 55+void ClearError()
 56+{
 57+ do
 58+ {
 59+ if (glGetError() == GL_NO_ERROR) break;
 60+ } while (1);
 61+}
 62+
 63+DWORD CalculateMipLevels(DWORD width, DWORD height)
 64+{
 65+ DWORD x, y;
 66+ DWORD levels = 1;
 67+ if ((!width) || (!height)) return 0;
 68+ if ((width == 1) || (height == 1)) return 1;
 69+ x = width;
 70+ y = height;
 71+miploop:
 72+ x = max(1,(DWORD)floorf((float)x / 2.0f));
 73+ y = max(1,(DWORD)floorf((float)y / 2.0f));
 74+ levels++;
 75+ if ((x == 1) || (y == 1)) return levels;
 76+ else goto miploop;
 77+}
 78+
 79+void ShrinkMip(DWORD *x, DWORD *y)
 80+{
 81+ *x = max(1, (DWORD)floorf((float)*x / 2.0f));
 82+ *y = max(1, (DWORD)floorf((float)*y / 2.0f));
 83+}
 84+
 85+TextureManager *TextureManager_Create(glExtensions *glext)
 86+{
 87+ TextureManager *newtex;
 88+ numtexformats = END_TEXFORMATS - START_TEXFORMATS;
 89+ newtex = (TextureManager*)malloc(sizeof(TextureManager));
 90+ if (!newtex) return 0;
 91+ ZeroMemory(newtex, sizeof(TextureManager));
 92+ newtex->ext = glext;
 93+ return newtex;
 94+}
 95+
 96+void TextureManager__CreateTexture(TextureManager *This, TEXTURE *texture, int width, int height, glUtil *util)
 97+{
 98+ TextureManager_CreateTextureClassic(This, texture, width, height, util);
 99+}
 100+void TextureManager__DeleteTexture(TextureManager *This, TEXTURE *texture)
 101+{
 102+ TextureManager_DeleteTexture(This, texture);
 103+}
 104+void TextureManager__UploadTexture(TextureManager *This, TEXTURE *texture, int level, const void *data, int width, int height, BOOL checkerror, BOOL realloc, glUtil *util)
 105+{
 106+ TextureManager_UploadTextureClassic(This, texture, level, data, width, height, checkerror, realloc, util);
 107+}
 108+void TextureManager__DownloadTexture(TextureManager *This, TEXTURE *texture, int level, void *data, glUtil *util)
 109+{
 110+ TextureManager_DownloadTextureClassic(This, texture, level, data, util);
 111+}
 112+
 113+void TextureManager_CreateTextureClassic(TextureManager *This, TEXTURE *texture, int width, int height, glUtil *util)
 114+{
 115+ int texformat = -1;
 116+ int i;
 117+ DWORD x, y;
 118+ GLenum error;
 119+ if (!texture->miplevel) texture->miplevel = 1;
 120+ texture->pixelformat.dwSize = sizeof(DDPIXELFORMAT);
 121+ for(i = 0; i < numtexformats; i++)
 122+ {
 123+ if(!memcmp(&texformats[i],&texture->pixelformat,sizeof(DDPIXELFORMAT)))
 124+ {
 125+ texformat = i;
 126+ break;
 127+ }
 128+ }
 129+ ZeroMemory(texture->internalformats, 8 * sizeof(GLint));
 130+ switch(texformat)
 131+ {
 132+ case -1:
 133+ case 0: // 8-bit palette
 134+ if(This->ext->glver_major >= 3)
 135+ {
 136+ texture->internalformats[0] = GL_R8;
 137+ texture->format = GL_RED;
 138+ }
 139+ else
 140+ {
 141+ texture->internalformats[0] = GL_RGBA8;
 142+ texture->format = GL_LUMINANCE;
 143+ }
 144+ texture->type = GL_UNSIGNED_BYTE;
 145+ texture->colororder = 4;
 146+ texture->colorsizes[0] = 255;
 147+ texture->colorsizes[1] = 255;
 148+ texture->colorsizes[2] = 255;
 149+ texture->colorsizes[3] = 255;
 150+ texture->colorbits[0] = 8;
 151+ texture->colorbits[1] = 0;
 152+ texture->colorbits[2] = 0;
 153+ texture->colorbits[3] = 0;
 154+ break;
 155+ case 1: // 8-bit RGB332
 156+ texture->internalformats[0] = GL_R3_G3_B2;
 157+ texture->internalformats[1] = GL_RGB8;
 158+ texture->internalformats[2] = GL_RGBA8;
 159+ texture->format = GL_RGB;
 160+ texture->type = GL_UNSIGNED_BYTE_3_3_2;
 161+ texture->colororder = 1;
 162+ texture->colorsizes[0] = 7;
 163+ texture->colorsizes[1] = 7;
 164+ texture->colorsizes[2] = 3;
 165+ texture->colorsizes[3] = 1;
 166+ texture->colorbits[0] = 3;
 167+ texture->colorbits[1] = 3;
 168+ texture->colorbits[2] = 2;
 169+ texture->colorbits[3] = 0;
 170+ break;
 171+ case 2: // 16-bit RGB555
 172+ texture->internalformats[0] = GL_RGB5_A1;
 173+ texture->internalformats[1] = GL_RGBA8;
 174+ texture->format = GL_BGRA;
 175+ texture->type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
 176+ texture->colororder = 1;
 177+ texture->colorsizes[0] = 31;
 178+ texture->colorsizes[1] = 31;
 179+ texture->colorsizes[2] = 31;
 180+ texture->colorsizes[3] = 1;
 181+ texture->colorbits[0] = 5;
 182+ texture->colorbits[1] = 5;
 183+ texture->colorbits[2] = 5;
 184+ texture->colorbits[3] = 1;
 185+ break;
 186+ case 3: // 16-bit RGB565
 187+ texture->internalformats[0] = GL_RGB565;
 188+ texture->internalformats[1] = GL_RGB8;
 189+ texture->internalformats[2] = GL_RGBA8;
 190+ texture->format = GL_RGB;
 191+ texture->type = GL_UNSIGNED_SHORT_5_6_5;
 192+ texture->colororder = 1;
 193+ texture->colorsizes[0] = 31;
 194+ texture->colorsizes[1] = 63;
 195+ texture->colorsizes[2] = 31;
 196+ texture->colorsizes[3] = 1;
 197+ texture->colorbits[0] = 5;
 198+ texture->colorbits[1] = 6;
 199+ texture->colorbits[2] = 5;
 200+ texture->colorbits[3] = 0;
 201+ break;
 202+ case 4: // 24-bit RGB888
 203+ texture->internalformats[0] = GL_RGB8;
 204+ texture->internalformats[1] = GL_RGBA8;
 205+ texture->format = GL_BGR;
 206+ texture->type = GL_UNSIGNED_BYTE;
 207+ texture->colororder = 1;
 208+ texture->colorsizes[0] = 255;
 209+ texture->colorsizes[1] = 255;
 210+ texture->colorsizes[2] = 255;
 211+ texture->colorsizes[3] = 1;
 212+ texture->colorbits[0] = 8;
 213+ texture->colorbits[1] = 8;
 214+ texture->colorbits[2] = 8;
 215+ texture->colorbits[3] = 0;
 216+ break;
 217+ case 5: // 32-bit RGB888
 218+ texture->internalformats[0] = GL_RGBA8;
 219+ texture->format = GL_BGRA;
 220+ texture->type = GL_UNSIGNED_INT_8_8_8_8_REV;
 221+ texture->colororder = 1;
 222+ texture->colorsizes[0] = 255;
 223+ texture->colorsizes[1] = 255;
 224+ texture->colorsizes[2] = 255;
 225+ texture->colorsizes[3] = 1;
 226+ texture->colorbits[0] = 8;
 227+ texture->colorbits[1] = 8;
 228+ texture->colorbits[2] = 8;
 229+ texture->colorbits[3] = 0;
 230+ break;
 231+ case 6: // 32-bit BGR888
 232+ texture->internalformats[0] = GL_RGBA8;
 233+ texture->format = GL_RGBA;
 234+ texture->type = GL_UNSIGNED_INT_8_8_8_8_REV;
 235+ texture->colororder = 0;
 236+ texture->colorsizes[0] = 255;
 237+ texture->colorsizes[1] = 255;
 238+ texture->colorsizes[2] = 255;
 239+ texture->colorsizes[3] = 1;
 240+ texture->colorbits[0] = 8;
 241+ texture->colorbits[1] = 8;
 242+ texture->colorbits[2] = 8;
 243+ texture->colorbits[3] = 0;
 244+ break;
 245+ case 7: // 16-bit RGBA8332
 246+ FIXME("Unusual texture format RGBA8332 not supported");
 247+ texture->colororder = 1;
 248+ texture->colorsizes[0] = 7;
 249+ texture->colorsizes[1] = 7;
 250+ texture->colorsizes[2] = 3;
 251+ texture->colorsizes[3] = 255;
 252+ texture->colorbits[0] = 3;
 253+ texture->colorbits[1] = 3;
 254+ texture->colorbits[2] = 2;
 255+ texture->colorbits[3] = 8;
 256+ break;
 257+ case 8: // 16-bit RGBA4444
 258+ texture->internalformats[0] = GL_RGBA4;
 259+ texture->internalformats[1] = GL_RGBA8;
 260+ texture->format = GL_BGRA;
 261+ texture->type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
 262+ texture->colororder = 1;
 263+ texture->colorsizes[0] = 15;
 264+ texture->colorsizes[1] = 15;
 265+ texture->colorsizes[2] = 15;
 266+ texture->colorsizes[3] = 15;
 267+ texture->colorbits[0] = 4;
 268+ texture->colorbits[1] = 4;
 269+ texture->colorbits[2] = 4;
 270+ texture->colorbits[3] = 4;
 271+ break;
 272+ case 9: // 16-bit RGBA1555
 273+ texture->internalformats[0] = GL_RGB5_A1;
 274+ texture->internalformats[1] = GL_RGBA8;
 275+ texture->format = GL_BGRA;
 276+ texture->type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
 277+ texture->colorbits[0] = 5;
 278+ texture->colorbits[1] = 5;
 279+ texture->colorbits[2] = 5;
 280+ texture->colorbits[3] = 1;
 281+ break;
 282+ case 10: // 32-bit RGBA8888
 283+ texture->internalformats[0] = GL_RGBA8;
 284+ texture->format = GL_BGRA;
 285+ texture->type = GL_UNSIGNED_INT_8_8_8_8_REV;
 286+ texture->colororder = 1;
 287+ texture->colorsizes[0] = 255;
 288+ texture->colorsizes[1] = 255;
 289+ texture->colorsizes[2] = 255;
 290+ texture->colorsizes[3] = 255;
 291+ texture->colorbits[0] = 8;
 292+ texture->colorbits[1] = 8;
 293+ texture->colorbits[2] = 8;
 294+ texture->colorbits[3] = 8;
 295+ break;
 296+ case 11: // 8-bit Luminance
 297+ texture->internalformats[0] = GL_LUMINANCE8;
 298+ texture->internalformats[1] = GL_RGB8;
 299+ texture->internalformats[2] = GL_RGBA8;
 300+ texture->format = GL_LUMINANCE;
 301+ texture->type = GL_UNSIGNED_BYTE;
 302+ texture->colororder = 5;
 303+ texture->colorsizes[0] = 255;
 304+ texture->colorsizes[1] = 255;
 305+ texture->colorsizes[2] = 255;
 306+ texture->colorsizes[3] = 255;
 307+ texture->colorbits[0] = 8;
 308+ texture->colorbits[1] = 0;
 309+ texture->colorbits[2] = 0;
 310+ texture->colorbits[3] = 0;
 311+ break;
 312+ case 12: // 8-bit Alpha
 313+ texture->internalformats[0] = GL_ALPHA8;
 314+ texture->format = GL_ALPHA;
 315+ texture->type = GL_UNSIGNED_BYTE;
 316+ texture->colororder = 6;
 317+ texture->colorsizes[0] = 255;
 318+ texture->colorsizes[1] = 255;
 319+ texture->colorsizes[2] = 255;
 320+ texture->colorsizes[3] = 255;
 321+ texture->colorbits[0] = 0;
 322+ texture->colorbits[1] = 0;
 323+ texture->colorbits[2] = 0;
 324+ texture->colorbits[3] = 8;
 325+ break;
 326+ case 13: // 16-bit Luminance Alpha
 327+ texture->internalformats[0] = GL_LUMINANCE8_ALPHA8;
 328+ texture->internalformats[1] = GL_RGBA8;
 329+ texture->format = GL_LUMINANCE_ALPHA;
 330+ texture->type = GL_UNSIGNED_BYTE;
 331+ texture->colororder = 7;
 332+ texture->colorsizes[0] = 255;
 333+ texture->colorsizes[1] = 255;
 334+ texture->colorsizes[2] = 255;
 335+ texture->colorsizes[3] = 255;
 336+ texture->colorbits[0] = 8;
 337+ texture->colorbits[1] = 0;
 338+ texture->colorbits[2] = 0;
 339+ texture->colorbits[3] = 8;
 340+ break;
 341+ case 14: // 16-bit Z buffer
 342+ texture->internalformats[0] = GL_DEPTH_COMPONENT16;
 343+ texture->format = GL_DEPTH_COMPONENT;
 344+ texture->type = GL_UNSIGNED_SHORT;
 345+ texture->colororder = 4;
 346+ texture->colorsizes[0] = 65535;
 347+ texture->colorsizes[1] = 65535;
 348+ texture->colorsizes[2] = 65535;
 349+ texture->colorsizes[3] = 65535;
 350+ texture->colorbits[0] = 16;
 351+ texture->colorbits[1] = 0;
 352+ texture->colorbits[2] = 0;
 353+ texture->colorbits[3] = 0;
 354+ break;
 355+ case 15: // 24-bit Z buffer
 356+ texture->internalformats[0] = GL_DEPTH_COMPONENT24;
 357+ texture->format = GL_DEPTH_COMPONENT;
 358+ texture->type = GL_UNSIGNED_INT;
 359+ texture->colororder = 4;
 360+ texture->colorsizes[0] = 16777215;
 361+ texture->colorsizes[1] = 16777215;
 362+ texture->colorsizes[2] = 16777215;
 363+ texture->colorsizes[3] = 16777215;
 364+ texture->colorbits[0] = 24;
 365+ texture->colorbits[1] = 0;
 366+ texture->colorbits[2] = 0;
 367+ texture->colorbits[3] = 0;
 368+ break;
 369+ case 16: // 32/24 bit Z buffer
 370+ texture->internalformats[0] = GL_DEPTH_COMPONENT24;
 371+ texture->format = GL_DEPTH_COMPONENT;
 372+ texture->type = GL_UNSIGNED_INT;
 373+ texture->colororder = 4;
 374+ texture->colorsizes[0] = 16777215;
 375+ texture->colorsizes[1] = 16777215;
 376+ texture->colorsizes[2] = 16777215;
 377+ texture->colorsizes[3] = 16777215;
 378+ texture->colorbits[0] = 24;
 379+ texture->colorbits[1] = 0;
 380+ texture->colorbits[2] = 0;
 381+ texture->colorbits[3] = 0;
 382+ break;
 383+ case 17: // 32-bit Z buffer
 384+ texture->internalformats[0] = GL_DEPTH_COMPONENT32;
 385+ texture->format = GL_DEPTH_COMPONENT;
 386+ texture->type = GL_UNSIGNED_INT;
 387+ texture->colororder = 4;
 388+ texture->colorsizes[0] = 4294967295;
 389+ texture->colorsizes[1] = 4294967295;
 390+ texture->colorsizes[2] = 4294967295;
 391+ texture->colorsizes[3] = 4294967295;
 392+ texture->colorbits[0] = 32;
 393+ texture->colorbits[1] = 0;
 394+ texture->colorbits[2] = 0;
 395+ texture->colorbits[3] = 0;
 396+ break;
 397+ case 18: // 32-bit Z/Stencil buffer, depth LSB
 398+ texture->internalformats[0] = GL_DEPTH24_STENCIL8;
 399+ texture->format = GL_DEPTH_STENCIL;
 400+ texture->type = GL_UNSIGNED_INT_24_8;
 401+ texture->colororder = 7;
 402+ texture->colorsizes[0] = 16777215;
 403+ texture->colorsizes[1] = 16777215;
 404+ texture->colorsizes[2] = 16777215;
 405+ texture->colorsizes[3] = 255;
 406+ texture->colorbits[0] = 24;
 407+ texture->colorbits[1] = 0;
 408+ texture->colorbits[2] = 0;
 409+ texture->colorbits[3] = 8;
 410+ break;
 411+ case 19: // 32-bit Z/Stencil buffer, depth MSB
 412+ texture->internalformats[0] = GL_DEPTH24_STENCIL8;
 413+ texture->format = GL_DEPTH_STENCIL;
 414+ texture->type = GL_UNSIGNED_INT_24_8;
 415+ texture->colororder = 7;
 416+ texture->colorsizes[0] = 16777215;
 417+ texture->colorsizes[1] = 16777215;
 418+ texture->colorsizes[2] = 16777215;
 419+ texture->colorsizes[3] = 255;
 420+ texture->colorbits[0] = 24;
 421+ texture->colorbits[1] = 0;
 422+ texture->colorbits[2] = 0;
 423+ texture->colorbits[3] = 8;
 424+ break;
 425+ }
 426+ texture->width = width;
 427+ texture->height = height;
 428+ glGenTextures(1,&texture->id);
 429+ glUtil_SetTexture(util,0,texture);
 430+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texture->minfilter);
 431+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texture->magfilter);
 432+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texture->wraps);
 433+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texture->wrapt);
 434+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, texture->miplevel - 1);
 435+ x = texture->width;
 436+ y = texture->height;
 437+ for (i = 0; i < texture->miplevel; i++)
 438+ {
 439+ do
 440+ {
 441+ ClearError();
 442+ glTexImage2D(GL_TEXTURE_2D, i, texture->internalformats[0], x, y, 0, texture->format, texture->type, NULL);
 443+ ShrinkMip(&x, &y);
 444+ error = glGetError();
 445+ if (error != GL_NO_ERROR)
 446+ {
 447+ if (texture->internalformats[1] == 0)
 448+ {
 449+ FIXME("Failed to create texture, cannot find internal format");
 450+ break;
 451+ }
 452+ memmove(&texture->internalformats[0], &texture->internalformats[1], 7 * sizeof(GLint));
 453+ texture->internalformats[7] = 0;
 454+ }
 455+ else break;
 456+ } while (1);
 457+ }
 458+}
 459+
 460+void TextureManager_DeleteTexture(TextureManager *This, TEXTURE *texture)
 461+{
 462+ glDeleteTextures(1,&texture->id);
 463+ texture->bordercolor = texture->format = texture->type = texture->width =
 464+ texture->height = texture->magfilter = texture->minfilter =
 465+ texture->miplevel = texture->wraps = texture->wrapt =
 466+ texture->id = 0;
 467+ texture->pboPack = texture->pboUnpack = NULL;
 468+ ZeroMemory(texture->internalformats, 8 * sizeof(GLint));
 469+}
 470+
 471+void TextureManager_UploadTextureClassic(TextureManager *This, TEXTURE *texture, int level, const void *data, int width, int height, BOOL checkerror, BOOL realloc, glUtil *util)
 472+{
 473+ GLenum error;
 474+ texture->width = width;
 475+ texture->height = height;
 476+ if (checkerror)
 477+ {
 478+ do
 479+ {
 480+ ClearError();
 481+ if (This->ext->GLEXT_EXT_direct_state_access)
 482+ {
 483+ if (realloc)This->ext->glTextureImage2DEXT(texture->id, GL_TEXTURE_2D, level, texture->internalformats[0],
 484+ width, height, 0, texture->format, texture->type, data);
 485+ else This->ext->glTextureSubImage2DEXT(texture->id, GL_TEXTURE_2D, level, 0, 0, width, height, texture->format, texture->type, data);
 486+ }
 487+ else
 488+ {
 489+ glUtil_SetActiveTexture(util, 0);
 490+ glUtil_SetTexture(util, 0, texture);
 491+ if (realloc)glTexImage2D(GL_TEXTURE_2D, level, texture->internalformats[0], width, height, 0, texture->format, texture->type, data);
 492+ else glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, texture->format, texture->type, data);
 493+ }
 494+ error = glGetError();
 495+ if (error != GL_NO_ERROR)
 496+ {
 497+ if (texture->internalformats[1] == 0)
 498+ {
 499+ FIXME("Failed to update texture, cannot find internal format");
 500+ break;
 501+ }
 502+ memmove(&texture->internalformats[0], &texture->internalformats[1], 7 * sizeof(GLint));
 503+ texture->internalformats[7] = 0;
 504+ }
 505+ else break;
 506+ } while (1);
 507+ }
 508+ else
 509+ {
 510+ if (This->ext->GLEXT_EXT_direct_state_access)
 511+ {
 512+ if (realloc)This->ext->glTextureImage2DEXT(texture->id, GL_TEXTURE_2D, level, texture->internalformats[0],
 513+ width, height, 0, texture->format, texture->type, data);
 514+ else This->ext->glTextureSubImage2DEXT(texture->id, GL_TEXTURE_2D, level, 0, 0, width, height, texture->format, texture->type, data);
 515+ }
 516+ else
 517+ {
 518+ glUtil_SetActiveTexture(util, 0);
 519+ glUtil_SetTexture(util, 0, texture);
 520+ if (realloc)glTexImage2D(GL_TEXTURE_2D, level, texture->internalformats[0], width, height, 0, texture->format, texture->type, data);
 521+ else glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, texture->format, texture->type, data);
 522+ }
 523+ }
 524+}
 525+
 526+void TextureManager_DownloadTextureClassic(TextureManager *This, TEXTURE *texture, int level, void *data, glUtil *util)
 527+{
 528+ if(This->ext->GLEXT_EXT_direct_state_access) This->ext->glGetTextureImageEXT(texture->id,GL_TEXTURE_2D,level,texture->format,texture->type,data);
 529+ else
 530+ {
 531+ glUtil_SetActiveTexture(util, 0);
 532+ glUtil_SetTexture(util, 0,texture);
 533+ glGetTexImage(GL_TEXTURE_2D,level,texture->format,texture->type,data);
 534+ }
 535+}
 536+
 537+BOOL TextureManager_FixTexture(TextureManager *This, TEXTURE *texture, void *data, DWORD *dirty, GLint level, glUtil *util)
 538+{
 539+ // data should be null to create uninitialized texture or be pointer to top-level
 540+ // buffer to retain texture data
 541+ TEXTURE newtexture;
 542+ GLenum error;
 543+ memcpy(&newtexture, texture, sizeof(TEXTURE));
 544+ if (texture->internalformats[1] == 0) return FALSE;
 545+ glGenTextures(1, &newtexture.id);
 546+ glUtil_SetActiveTexture(util, 0);
 547+ if (data)
 548+ {
 549+ glUtil_SetTexture(util, 0, texture);
 550+ glGetTexImage(GL_TEXTURE_2D, level, texture->format, texture->type, data);
 551+ if (dirty) *dirty |= 2;
 552+ }
 553+ glUtil_SetTexture(util, 0, &newtexture);
 554+ do
 555+ {
 556+ memmove(&newtexture.internalformats[0], &newtexture.internalformats[1], 7 * sizeof(GLint));
 557+ newtexture.internalformats[7] = 0;
 558+ ClearError();
 559+ glTexImage2D(GL_TEXTURE_2D, level, newtexture.internalformats[0], newtexture.width, newtexture.height,
 560+ 0, newtexture.format, newtexture.type, data);
 561+ error = glGetError();
 562+ if (error != GL_NO_ERROR)
 563+ {
 564+ if (newtexture.internalformats[1] == 0)
 565+ {
 566+ FIXME("Failed to repair texture, cannot find internal format");
 567+ break;
 568+ }
 569+ }
 570+ else break;
 571+ } while (1);
 572+ TextureManager__DeleteTexture(This, texture);
 573+ memcpy(texture, &newtexture, sizeof(TEXTURE));
 574+ return TRUE;
 575+}
 576+
 577+}
\ No newline at end of file
Index: ddraw/TextureManager.h
@@ -61,27 +61,25 @@
6262 extern const DDPIXELFORMAT texformats[];
6363 extern int numtexformats;
6464
 65+struct glUtil;
 66+
6567 typedef struct TextureManager
6668 {
6769 glExtensions *ext;
68 - GLint texlevel;
69 - GLuint textures[16];
7070 } TextureManager;
7171
7272 DWORD CalculateMipLevels(DWORD width, DWORD height);
7373
7474 TextureManager *TextureManager_Create(glExtensions *glext);
75 -void TextureManager_SetActiveTexture(TextureManager *This, int level);
76 -void TextureManager_SetTexture(TextureManager *This, unsigned int level, TEXTURE *texture);
77 -void TextureManager__CreateTexture(TextureManager *This, TEXTURE *texture, int width, int height);
 75+void TextureManager__CreateTexture(TextureManager *This, TEXTURE *texture, int width, int height, glUtil *util);
7876 void TextureManager__DeleteTexture(TextureManager *This, TEXTURE *texture);
79 -void TextureManager__UploadTexture(TextureManager *This, TEXTURE *texture, int level, const void *data, int width, int height, BOOL checkerror, BOOL realloc);
80 -void TextureManager__DownloadTexture(TextureManager *This, TEXTURE *texture, int level, void *data);
81 -void TextureManager_CreateTextureClassic(TextureManager *This, TEXTURE *texture, int width, int height);
 77+void TextureManager__UploadTexture(TextureManager *This, TEXTURE *texture, int level, const void *data, int width, int height, BOOL checkerror, BOOL realloc, glUtil *util);
 78+void TextureManager__DownloadTexture(TextureManager *This, TEXTURE *texture, int level, void *data, glUtil *util);
 79+void TextureManager_CreateTextureClassic(TextureManager *This, TEXTURE *texture, int width, int height, glUtil *util);
8280 void TextureManager_DeleteTexture(TextureManager *This, TEXTURE *texture);
83 -void TextureManager_UploadTextureClassic(TextureManager *This, TEXTURE *texture, int level, const void *data, int width, int height, BOOL checkerror, BOOL realloc);
84 -void TextureManager_DownloadTextureClassic(TextureManager *This, TEXTURE *texture, int level, void *data);
85 -BOOL TextureManager_FixTexture(TextureManager *This, TEXTURE *texture, void *data, DWORD *dirty, GLint level);
 81+void TextureManager_UploadTextureClassic(TextureManager *This, TEXTURE *texture, int level, const void *data, int width, int height, BOOL checkerror, BOOL realloc, glUtil *util);
 82+void TextureManager_DownloadTextureClassic(TextureManager *This, TEXTURE *texture, int level, void *data, glUtil *util);
 83+BOOL TextureManager_FixTexture(TextureManager *This, TEXTURE *texture, void *data, DWORD *dirty, GLint level, glUtil *util);
8684
8785 #ifdef __cplusplus
8886 }
Index: ddraw/ddraw.vcxproj
@@ -419,7 +419,7 @@
420420 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug no DXGL|Win32'">NotUsing</PrecompiledHeader>
421421 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug No MSVCRT|Win32'">NotUsing</PrecompiledHeader>
422422 </ClCompile>
423 - <ClCompile Include="TextureManager.c">
 423+ <ClCompile Include="TextureManager.cpp">
424424 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release no DXGL|Win32'">NotUsing</PrecompiledHeader>
425425 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug Trace|Win32'">NotUsing</PrecompiledHeader>
426426 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
Index: ddraw/ddraw.vcxproj.filters
@@ -220,9 +220,6 @@
221221 <ClCompile Include="glDirectDrawPalette.c">
222222 <Filter>Source Files</Filter>
223223 </ClCompile>
224 - <ClCompile Include="TextureManager.c">
225 - <Filter>Source Files</Filter>
226 - </ClCompile>
227224 <ClCompile Include="util.c">
228225 <Filter>Source Files</Filter>
229226 </ClCompile>
@@ -253,6 +250,9 @@
254251 <ClCompile Include="BufferObject.cpp">
255252 <Filter>Source Files</Filter>
256253 </ClCompile>
 254+ <ClCompile Include="TextureManager.cpp">
 255+ <Filter>Source Files</Filter>
 256+ </ClCompile>
257257 </ItemGroup>
258258 <ItemGroup>
259259 <ResourceCompile Include="ddraw.rc">
Index: ddraw/glDirectDrawSurface.cpp
@@ -1702,7 +1702,7 @@
17031703 if(!this) TRACE_RET(HRESULT,23,DDERR_INVALIDOBJECT);
17041704 TRACE_RET(HRESULT,23,Unlock((LPRECT)lpSurfaceData));
17051705 }
1706 -void glDirectDrawSurface7::SetFilter(int level, GLint mag, GLint min, glExtensions *ext, glUtil *util, TextureManager *texman)
 1706+void glDirectDrawSurface7::SetFilter(int level, GLint mag, GLint min, glExtensions *ext, glUtil *util)
17071707 {
17081708 TRACE_ENTER(4,14,this,11,level,11,mag,11,min);
17091709 switch(dxglcfg.texfilter)
@@ -1751,7 +1751,7 @@
17521752 }
17531753 else
17541754 {
1755 - TextureManager_SetTexture(texman,level,texture);
 1755+ glUtil_SetTexture(util,level,texture);
17561756 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,mag);
17571757 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,min);
17581758 }
Index: ddraw/glDirectDrawSurface.h
@@ -110,7 +110,7 @@
111111 ULONG WINAPI ReleaseGamma();
112112 ULONG WINAPI AddRefColor();
113113 ULONG WINAPI ReleaseColor();
114 - void SetFilter(int level, GLint mag, GLint min, glExtensions *ext, glUtil *util, TextureManager *texman);
 114+ void SetFilter(int level, GLint mag, GLint min, glExtensions *ext, glUtil *util);
115115 TEXTURE *GetTexture(){
116116 return texture;
117117 }
Index: ddraw/glRenderer.cpp
@@ -108,7 +108,7 @@
109109 if(bpp == 15) bpp = 16;
110110 if((x == bigx && y == bigy) || !bigbuffer)
111111 {
112 - TextureManager__UploadTexture(This->texman, texture, miplevel, buffer, x, y, FALSE, FALSE);
 112+ TextureManager__UploadTexture(This->texman, texture, miplevel, buffer, x, y, FALSE, FALSE, This->util);
113113 }
114114 else
115115 {
@@ -128,7 +128,7 @@
129129 break;
130130 break;
131131 }
132 - TextureManager__UploadTexture(This->texman, texture, miplevel, bigbuffer, bigx, bigy, FALSE, FALSE);
 132+ TextureManager__UploadTexture(This->texman, texture, miplevel, bigbuffer, bigx, bigy, FALSE, FALSE, This->util);
133133 }
134134 }
135135
@@ -161,11 +161,11 @@
162162 {
163163 if((bigx == x && bigy == y) || !bigbuffer)
164164 {
165 - TextureManager__DownloadTexture(This->texman,texture,miplevel,buffer);
 165+ TextureManager__DownloadTexture(This->texman,texture,miplevel,buffer,This->util);
166166 }
167167 else
168168 {
169 - TextureManager__DownloadTexture(This->texman,texture,miplevel,bigbuffer);
 169+ TextureManager__DownloadTexture(This->texman,texture,miplevel,bigbuffer,This->util);
170170 switch(bpp)
171171 {
172172 case 8:
@@ -1123,7 +1123,7 @@
11241124 glEnable(GL_CULL_FACE);
11251125 SwapBuffers(This->hDC);
11261126 This->texman = TextureManager_Create(This->ext);
1127 - TextureManager_SetActiveTexture(This->texman,0);
 1127+ glUtil_SetActiveTexture(This->util,0);
11281128 glRenderer__SetFogColor(This,0);
11291129 glRenderer__SetFogStart(This,0);
11301130 glRenderer__SetFogEnd(This,1);
@@ -1393,7 +1393,7 @@
13941394 {
13951395 if (glUtil_SetFBOSurface(This->util, dest) == GL_FRAMEBUFFER_COMPLETE) break;
13961396 if (!dest->texture->internalformats[1]) break;
1397 - TextureManager_FixTexture(This->texman, dest->texture, (dest->bigbuffer ? dest->bigbuffer : dest->buffer), &dest->dirty, dest->miplevel);
 1397+ TextureManager_FixTexture(This->texman, dest->texture, (dest->bigbuffer ? dest->bigbuffer : dest->buffer), &dest->dirty, dest->miplevel, This->util);
13981398 glUtil_SetFBO(This->util, NULL);
13991399 dest->fbo.fbcolor = NULL;
14001400 dest->fbo.fbz = NULL;
@@ -1461,19 +1461,19 @@
14621462 }
14631463 if (usedest && (shader->shader.uniforms[2] != -1))
14641464 {
1465 - TextureManager_SetTexture(This->texman, 1, This->backbuffer);
 1465+ glUtil_SetTexture(This->util, 1, This->backbuffer);
14661466 This->ext->glUniform1i(shader->shader.uniforms[2], 1);
14671467 }
14681468 if (usepattern && (shader->shader.uniforms[3] != -1))
14691469 {
14701470 glDirectDrawSurface7 *pattern = (glDirectDrawSurface7*)lpDDBltFx->lpDDSPattern;
1471 - TextureManager_SetTexture(This->texman, 2, pattern->texture);
 1471+ glUtil_SetTexture(This->util, 2, pattern->texture);
14721472 This->ext->glUniform1i(shader->shader.uniforms[3], 2);
14731473 This->ext->glUniform2i(shader->shader.uniforms[9], pattern->texture->width, pattern->texture->height);
14741474 }
14751475 if (dwFlags & 0x10000000) // Use clipper
14761476 {
1477 - TextureManager_SetTexture(This->texman, 3, dest->stencil);
 1477+ glUtil_SetTexture(This->util, 3, dest->stencil);
14781478 This->ext->glUniform1i(shader->shader.uniforms[4],3);
14791479 glUtil_EnableArray(This->util, shader->shader.attribs[5], TRUE);
14801480 This->ext->glVertexAttribPointer(shader->shader.attribs[5], 2, GL_FLOAT, GL_FALSE, sizeof(BltVertex), &This->bltvertices[0].stencils);
@@ -1480,15 +1480,15 @@
14811481 }
14821482 if(src)
14831483 {
1484 - TextureManager_SetTexture(This->texman,0,src->GetTexture());
 1484+ glUtil_SetTexture(This->util,0,src->GetTexture());
14851485 if(This->ext->GLEXT_ARB_sampler_objects)
14861486 {
14871487 if((dxglcfg.scalingfilter == 0) || (This->ddInterface->GetBPP() == 8))
1488 - src->SetFilter(0,GL_NEAREST,GL_NEAREST,This->ext,This->util,This->texman);
1489 - else src->SetFilter(0,GL_LINEAR,GL_LINEAR,This->ext,This->util,This->texman);
 1488+ src->SetFilter(0,GL_NEAREST,GL_NEAREST,This->ext,This->util);
 1489+ else src->SetFilter(0,GL_LINEAR,GL_LINEAR,This->ext,This->util);
14901490 }
14911491 }
1492 - else TextureManager_SetTexture(This->texman,0,NULL);
 1492+ else glUtil_SetTexture(This->util,0,NULL);
14931493 This->ext->glUniform4f(shader->shader.uniforms[0],0,(GLfloat)dest->fakex,0,(GLfloat)dest->fakey);
14941494 if(src) This->ext->glUniform4i(shader->shader.uniforms[10], src->texture->colorsizes[0], src->texture->colorsizes[1],
14951495 src->texture->colorsizes[2], src->texture->colorsizes[3]);
@@ -1522,13 +1522,13 @@
15231523
15241524 void glRenderer__MakeTexture(glRenderer *This, TEXTURE *texture, DWORD width, DWORD height)
15251525 {
1526 - TextureManager__CreateTexture(This->texman,texture,width,height);
 1526+ TextureManager__CreateTexture(This->texman,texture,width,height,This->util);
15271527 }
15281528
15291529 void glRenderer__DrawBackbuffer(glRenderer *This, TEXTURE **texture, int x, int y, int progtype)
15301530 {
15311531 GLfloat view[4];
1532 - TextureManager_SetActiveTexture(This->texman,0);
 1532+ glUtil_SetActiveTexture(This->util,0);
15331533 if(!This->backbuffer)
15341534 {
15351535 This->backbuffer = (TEXTURE*)malloc(sizeof(TEXTURE));
@@ -1540,13 +1540,13 @@
15411541 This->backbuffer->pixelformat.dwGBitMask = 0xFF00;
15421542 This->backbuffer->pixelformat.dwRBitMask = 0xFF0000;
15431543 This->backbuffer->pixelformat.dwRGBBitCount = 32;
1544 - TextureManager__CreateTexture(This->texman,This->backbuffer,x,y);
 1544+ TextureManager__CreateTexture(This->texman,This->backbuffer,x,y,This->util);
15451545 This->backx = x;
15461546 This->backy = y;
15471547 }
15481548 if((This->backx != x) || (This->backy != y))
15491549 {
1550 - TextureManager__UploadTexture(This->texman,This->backbuffer,0,NULL,x,y, FALSE, TRUE);
 1550+ TextureManager__UploadTexture(This->texman, This->backbuffer, 0, NULL, x, y, FALSE, TRUE, This->util);
15511551 This->backx = x;
15521552 This->backy = y;
15531553 }
@@ -1556,9 +1556,9 @@
15571557 view[3] = (GLfloat)y;
15581558 glUtil_SetViewport(This->util,0,0,x,y);
15591559 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
1560 - TextureManager_SetTexture(This->texman,0,*texture);
 1560+ glUtil_SetTexture(This->util,0,*texture);
15611561 *texture = This->backbuffer;
1562 - if(This->ext->GLEXT_ARB_sampler_objects) ((glDirectDrawSurface7*)NULL)->SetFilter(0,GL_LINEAR,GL_LINEAR,This->ext,This->util,This->texman);
 1562+ if(This->ext->GLEXT_ARB_sampler_objects) ((glDirectDrawSurface7*)NULL)->SetFilter(0,GL_LINEAR,GL_LINEAR,This->ext,This->util);
15631563 This->ext->glUniform4f(This->shaders->shaders[progtype].view,view[0],view[1],view[2],view[3]);
15641564 This->bltvertices[0].s = This->bltvertices[0].t = This->bltvertices[1].t = This->bltvertices[2].s = 1.;
15651565 This->bltvertices[1].s = This->bltvertices[2].t = This->bltvertices[3].s = This->bltvertices[3].t = 0.;
@@ -1580,7 +1580,7 @@
15811581 GLfloat view[4];
15821582 int x = srcrect.right - srcrect.left;
15831583 int y = srcrect.bottom - srcrect.top;
1584 - TextureManager_SetActiveTexture(This->texman, 0);
 1584+ glUtil_SetActiveTexture(This->util, 0);
15851585 if (!This->backbuffer)
15861586 {
15871587 This->backbuffer = (TEXTURE*)malloc(sizeof(TEXTURE));
@@ -1592,7 +1592,7 @@
15931593 This->backbuffer->pixelformat.dwGBitMask = 0xFF00;
15941594 This->backbuffer->pixelformat.dwRBitMask = 0xFF0000;
15951595 This->backbuffer->pixelformat.dwRGBBitCount = 32;
1596 - TextureManager__CreateTexture(This->texman, This->backbuffer, x, y);
 1596+ TextureManager__CreateTexture(This->texman, This->backbuffer, x, y, This->util);
15971597 This->backx = x;
15981598 This->backy = y;
15991599 }
@@ -1600,7 +1600,7 @@
16011601 {
16021602 if (This->backx > x) x = This->backx;
16031603 if (This->backx > y) y = This->backx;
1604 - TextureManager__UploadTexture(This->texman, This->backbuffer, 0, NULL, x, y, FALSE, TRUE);
 1604+ TextureManager__UploadTexture(This->texman, This->backbuffer, 0, NULL, x, y, FALSE, TRUE, This->util);
16051605 This->backx = x;
16061606 This->backy = y;
16071607 }
@@ -1612,7 +1612,7 @@
16131613 glUtil_SetScissor(This->util, TRUE, 0, 0, This->backx, This->backy);
16141614 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
16151615 glUtil_SetScissor(This->util, FALSE, 0, 0, 0, 0);
1616 - TextureManager_SetTexture(This->texman, 0, texture);
 1616+ glUtil_SetTexture(This->util, 0, texture);
16171617 This->ext->glUniform4f(This->shaders->shaders[progtype].view, view[0], view[1], view[2], view[3]);
16181618 This->bltvertices[1].s = This->bltvertices[3].s = (GLfloat)srcrect.left / (GLfloat)texture->width;
16191619 This->bltvertices[0].s = This->bltvertices[2].s = (GLfloat)srcrect.right / (GLfloat)texture->width;
@@ -1697,23 +1697,23 @@
16981698 {
16991699 ShaderManager_SetShader(This->shaders,PROG_PAL256,NULL,0);
17001700 progtype = PROG_PAL256;
1701 - TextureManager__UploadTexture(This->texman,paltex,0,glDirectDrawPalette_GetPalette(dest->palette,NULL),256,1,FALSE,FALSE);
 1701+ TextureManager__UploadTexture(This->texman,paltex,0,glDirectDrawPalette_GetPalette(dest->palette,NULL),256,1,FALSE,FALSE,This->util);
17021702 This->ext->glUniform1i(This->shaders->shaders[progtype].tex0,0);
17031703 This->ext->glUniform1i(This->shaders->shaders[progtype].pal,1);
1704 - TextureManager_SetTexture(This->texman,0,texture);
1705 - TextureManager_SetTexture(This->texman,1,paltex);
 1704+ glUtil_SetTexture(This->util,0,texture);
 1705+ glUtil_SetTexture(This->util,1,paltex);
17061706 if(dxglcfg.scalingfilter)
17071707 {
17081708 glRenderer__DrawBackbuffer(This,&texture,dest->fakex,dest->fakey,progtype);
17091709 ShaderManager_SetShader(This->shaders,PROG_TEXTURE,NULL,0);
17101710 progtype = PROG_TEXTURE;
1711 - TextureManager_SetTexture(This->texman,0,texture);
 1711+ glUtil_SetTexture(This->util,0,texture);
17121712 This->ext->glUniform1i(This->shaders->shaders[progtype].tex0,0);
17131713 }
17141714 if(This->ext->GLEXT_ARB_sampler_objects)
17151715 {
1716 - ((glDirectDrawSurface7*)NULL)->SetFilter(0,GL_NEAREST,GL_NEAREST,This->ext,This->util,This->texman);
1717 - ((glDirectDrawSurface7*)NULL)->SetFilter(1,GL_NEAREST,GL_NEAREST,This->ext,This->util,This->texman);
 1716+ ((glDirectDrawSurface7*)NULL)->SetFilter(0,GL_NEAREST,GL_NEAREST,This->ext,This->util);
 1717+ ((glDirectDrawSurface7*)NULL)->SetFilter(1,GL_NEAREST,GL_NEAREST,This->ext,This->util);
17181718 }
17191719 }
17201720 else
@@ -1720,13 +1720,13 @@
17211721 {
17221722 ShaderManager_SetShader(This->shaders,PROG_TEXTURE,NULL,0);
17231723 progtype = PROG_TEXTURE;
1724 - TextureManager_SetTexture(This->texman,0,texture);
 1724+ glUtil_SetTexture(This->util,0,texture);
17251725 This->ext->glUniform1i(This->shaders->shaders[progtype].tex0,0);
17261726 }
17271727 if(dxglcfg.scalingfilter && This->ext->GLEXT_ARB_sampler_objects)
1728 - ((glDirectDrawSurface7*)NULL)->SetFilter(0,GL_LINEAR,GL_LINEAR,This->ext,This->util,This->texman);
 1728+ ((glDirectDrawSurface7*)NULL)->SetFilter(0,GL_LINEAR,GL_LINEAR,This->ext,This->util);
17291729 else if(This->ext->GLEXT_ARB_sampler_objects)
1730 - ((glDirectDrawSurface7*)NULL)->SetFilter(0,GL_NEAREST,GL_NEAREST,This->ext,This->util,This->texman);
 1730+ ((glDirectDrawSurface7*)NULL)->SetFilter(0,GL_NEAREST,GL_NEAREST,This->ext,This->util);
17311731 glUtil_SetViewport(This->util,viewport[0],viewport[1],viewport[2],viewport[3]);
17321732 This->ext->glUniform4f(This->shaders->shaders[progtype].view,view[0],view[1],view[2],view[3]);
17331733 if(This->ddInterface->GetFullscreen())
@@ -1928,7 +1928,7 @@
19291929 {
19301930 if (glUtil_SetFBOSurface(This->util, target) == GL_FRAMEBUFFER_COMPLETE) break;
19311931 if (!target->texture->internalformats[1]) break;
1932 - TextureManager_FixTexture(This->texman, target->texture, (target->bigbuffer ? target->bigbuffer : target->buffer), &target->dirty, target->miplevel);
 1932+ TextureManager_FixTexture(This->texman, target->texture, (target->bigbuffer ? target->bigbuffer : target->buffer), &target->dirty, target->miplevel,This->util);
19331933 glUtil_SetFBO(This->util, NULL);
19341934 target->fbo.fbcolor = NULL;
19351935 target->fbo.fbz = NULL;
@@ -2303,12 +2303,12 @@
23042304 This->texstages[i].texture->dirty &= ~1;
23052305 }
23062306 if(This->texstages[i].texture)
2307 - This->texstages[i].texture->SetFilter(i,This->texstages[i].glmagfilter,This->texstages[i].glminfilter,This->ext,This->util,This->texman);
2308 - TextureManager_SetTexture(This->texman,i,This->texstages[i].texture->texture);
2309 - glUtil_SetWrap(This->util, i, 0, This->texstages[i].addressu, This->texman);
2310 - glUtil_SetWrap(This->util, i, 1, This->texstages[i].addressv, This->texman);
 2307+ This->texstages[i].texture->SetFilter(i,This->texstages[i].glmagfilter,This->texstages[i].glminfilter,This->ext,This->util);
 2308+ glUtil_SetTexture(This->util,i,This->texstages[i].texture->texture);
 2309+ glUtil_SetWrap(This->util, i, 0, This->texstages[i].addressu);
 2310+ glUtil_SetWrap(This->util, i, 1, This->texstages[i].addressv);
23112311 }
2312 - TextureManager_SetTexture(This->texman,i,0);
 2312+ glUtil_SetTexture(This->util,i,0);
23132313 This->ext->glUniform1i(prog->uniforms[128+i],i);
23142314 if(This->renderstate[D3DRENDERSTATE_COLORKEYENABLE] && This->texstages[i].texture && (prog->uniforms[142+i] != -1))
23152315 {
@@ -2335,7 +2335,7 @@
23362336 if (glUtil_SetFBOSurface(This->util, device->glDDS7) == GL_FRAMEBUFFER_COMPLETE) break;
23372337 if (!device->glDDS7->texture->internalformats[1]) break;
23382338 TextureManager_FixTexture(This->texman, device->glDDS7->texture,
2339 - (device->glDDS7->bigbuffer ? device->glDDS7->bigbuffer : device->glDDS7->buffer), &device->glDDS7->dirty, device->glDDS7->miplevel);
 2339+ (device->glDDS7->bigbuffer ? device->glDDS7->bigbuffer : device->glDDS7->buffer), &device->glDDS7->dirty, device->glDDS7->miplevel,This->util);
23402340 glUtil_SetFBO(This->util, NULL);
23412341 device->glDDS7->fbo.fbcolor = NULL;
23422342 device->glDDS7->fbo.fbz = NULL;
@@ -2386,12 +2386,12 @@
23872387 surface->stencil->pixelformat.dwRBitMask = 0xF00;
23882388 surface->stencil->pixelformat.dwZBitMask = 0xF000;
23892389 surface->stencil->pixelformat.dwRGBBitCount = 16;
2390 - TextureManager__CreateTexture(This->texman, surface->stencil, surface->ddsd.dwWidth, surface->ddsd.dwHeight);
 2390+ TextureManager__CreateTexture(This->texman, surface->stencil, surface->ddsd.dwWidth, surface->ddsd.dwHeight, This->util);
23912391 }
23922392 if ((surface->ddsd.dwWidth != surface->stencil->width) ||
23932393 (surface->ddsd.dwHeight != surface->stencil->height))
23942394 TextureManager__UploadTexture(This->texman, surface->stencil, 0, NULL,
2395 - surface->ddsd.dwWidth, surface->ddsd.dwHeight, FALSE, TRUE);
 2395+ surface->ddsd.dwWidth, surface->ddsd.dwHeight, FALSE, TRUE, This->util);
23962396 glUtil_SetFBOTextures(This->util, &surface->stencilfbo, surface->stencil, 0, FALSE);
23972397 view[0] = view[2] = 0;
23982398 view[1] = (GLfloat)surface->ddsd.dwWidth;
@@ -2433,7 +2433,7 @@
24342434 if (!dest->attachparent->texture->internalformats[1]) break;
24352435 TextureManager_FixTexture(This->texman, dest->attachparent->texture,
24362436 (dest->attachparent->bigbuffer ? dest->attachparent->bigbuffer : dest->attachparent->buffer),
2437 - &dest->attachparent->dirty, dest->attachparent->miplevel);
 2437+ &dest->attachparent->dirty, dest->attachparent->miplevel, This->util);
24382438 glUtil_SetFBO(This->util, NULL);
24392439 dest->attachparent->fbo.fbcolor = NULL;
24402440 dest->attachparent->fbo.fbz = NULL;
@@ -2453,12 +2453,12 @@
24542454 dest->dummycolor->pixelformat.dwRBitMask = 0xF00;
24552455 dest->dummycolor->pixelformat.dwZBitMask = 0xF000;
24562456 dest->dummycolor->pixelformat.dwRGBBitCount = 16;
2457 - TextureManager__CreateTexture(This->texman, dest->dummycolor, dest->ddsd.dwWidth, dest->ddsd.dwHeight);
 2457+ TextureManager__CreateTexture(This->texman, dest->dummycolor, dest->ddsd.dwWidth, dest->ddsd.dwHeight, This->util);
24582458 }
24592459 if ((dest->ddsd.dwWidth != dest->dummycolor->width) ||
24602460 (dest->ddsd.dwHeight != dest->dummycolor->height))
24612461 TextureManager__UploadTexture(This->texman, dest->dummycolor, 0, NULL,
2462 - dest->ddsd.dwWidth, dest->ddsd.dwHeight, FALSE, TRUE);
 2462+ dest->ddsd.dwWidth, dest->ddsd.dwHeight, FALSE, TRUE, This->util);
24632463 glUtil_SetFBOTextures(This->util, &dest->zfbo, dest->dummycolor, dest->texture, FALSE);
24642464 }
24652465 glUtil_SetViewport(This->util, 0, 0, dest->ddsd.dwWidth, dest->ddsd.dwHeight);
Index: ddraw/glUtil.cpp
@@ -85,7 +85,8 @@
8686 glext->glSamplerParameteri(util->samplers[i].id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
8787 }
8888 }
89 -
 89+ util->texlevel = 0;
 90+ ZeroMemory(util->textures, 16 * sizeof(GLuint));
9091 }
9192
9293 void glUtil_AddRef(glUtil *This)
@@ -254,7 +255,7 @@
255256 else return fbo->status;
256257 }
257258
258 -void glUtil_SetWrap(glUtil *This, int level, DWORD coord, DWORD address, TextureManager *texman)
 259+void glUtil_SetWrap(glUtil *This, int level, DWORD coord, DWORD address)
259260 {
260261 if(level == -1)
261262 {
@@ -308,7 +309,7 @@
309310 }
310311 else
311312 {
312 - TextureManager_SetActiveTexture(texman,level);
 313+ glUtil_SetActiveTexture(This,level);
313314 if(coord) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,wrapmode);
314315 else glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,wrapmode);
315316 }
@@ -616,4 +617,27 @@
617618 This->LastBoundBuffer = NULL;
618619 }
619620
 621+void glUtil_SetActiveTexture(glUtil *This, int level)
 622+{
 623+ if (level != This->texlevel)
 624+ {
 625+ This->texlevel = level;
 626+ This->ext->glActiveTexture(GL_TEXTURE0 + level);
 627+ }
 628+}
 629+
 630+
 631+void glUtil_SetTexture(glUtil *This, unsigned int level, TEXTURE *texture)
 632+{
 633+ GLuint texname;
 634+ if (level >= 16) return;
 635+ if (!texture) texname = 0;
 636+ else texname = texture->id;
 637+ if (texname != This->textures[level])
 638+ {
 639+ glUtil_SetActiveTexture(This, level);
 640+ glBindTexture(GL_TEXTURE_2D, texname);
 641+ }
 642+}
 643+
620644 };
\ No newline at end of file
Index: ddraw/glUtil.h
@@ -104,6 +104,8 @@
105105 D3DSHADEMODE shademode;
106106 BufferObject *LastBoundBuffer;
107107 SAMPLER samplers[8];
 108+ GLint texlevel;
 109+ GLuint textures[16];
108110 } glUtil;
109111
110112 void glUtil_Create(glExtensions *glext, glUtil **out);
@@ -112,7 +114,7 @@
113115 void glUtil_InitFBO(glUtil *This, FBO *fbo);
114116 void glUtil_DeleteFBO(glUtil *This, FBO *fbo);
115117 void glUtil_SetFBOTexture(glUtil *This, FBO *fbo, TEXTURE *color, TEXTURE *z, BOOL stencil);
116 -void glUtil_SetWrap(glUtil *This, int level, DWORD coord, DWORD address, TextureManager *texman);
 118+void glUtil_SetWrap(glUtil *This, int level, DWORD coord, DWORD address);
117119 GLenum glUtil_SetFBOSurface(glUtil *This, glDirectDrawSurface7 *surface);
118120 GLenum glUtil_SetFBO(glUtil *This, FBO *fbo);
119121 GLenum glUtil_SetFBOTextures(glUtil *This, FBO *fbo, TEXTURE *color, TEXTURE *z, BOOL stencil);
@@ -137,6 +139,8 @@
138140 void glUtil_SetShadeMode(glUtil *This, D3DSHADEMODE mode);
139141 void glUtil_BindBuffer(glUtil *This, BufferObject *buffer, GLenum target);
140142 void glUtil_UndoBindBuffer(glUtil *This, GLenum target);
 143+void glUtil_SetActiveTexture(glUtil *This, int level);
 144+void glUtil_SetTexture(glUtil *This, unsigned int level, TEXTURE *texture);
141145
142146 #ifdef __cplusplus
143147 }