30 IDirectDrawSurface7 *surface = NULL;
33 if (capabilities_device->lpVtbl->GetCaps(capabilities_device, &caps) < 0)
36 DDSURFACEDESC2 texture_surface_desc = {0};
37 texture_surface_desc.dwSize =
sizeof(texture_surface_desc);
38 texture_surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_TEXTURESTAGE;
39 texture_surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
42 texture_surface_desc.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
44 texture_surface_desc.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
46 uint32_t power_of_two_width = 1;
48 texture_surface_desc.dwWidth = 1;
53 power_of_two_width <<= 1;
55 while (width > power_of_two_width);
57 texture_surface_desc.dwWidth = power_of_two_width;
60 uint32_t power_of_two_height = 1;
62 texture_surface_desc.dwHeight = 1;
67 power_of_two_height <<= 1;
69 while (height > power_of_two_height);
71 texture_surface_desc.dwHeight = power_of_two_height;
90 if (caps.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
92 if (power_of_two_width <= power_of_two_height)
94 power_of_two_width = power_of_two_height;
95 texture_surface_desc.dwWidth = power_of_two_width;
99 power_of_two_height = power_of_two_width;
100 texture_surface_desc.dwHeight = power_of_two_height;
105 IDirect3DDevice7 *format_enumeration_device =
g_d3dDevice7;
109 &texture_surface_desc.ddpfPixelFormat);
111 int32_t green_bits_to_discard = 8;
113 if (texture_surface_desc.ddpfPixelFormat.dwRGBBitCount)
115 uint32_t dw_flags = texture_surface_desc.dwFlags;
118 dw_flags |= DDSD_PIXELFORMAT;
119 texture_surface_desc.dwFlags = dw_flags;
128 message =
"Texture Error";
129 category =
"No 4444 RGBA texture formats!";
134 category =
"No 16-bit RGB texture formats!";
138 __builtin_memset(&texture_surface_desc.ddpfPixelFormat, 0, 0x20);
142 uint32_t dw_r_bit_mask = texture_surface_desc.ddpfPixelFormat.dwRBitMask;
147 uint32_t dw_g_bit_mask = texture_surface_desc.ddpfPixelFormat.dwGBitMask;
153 int32_t green_shift = 0;
154 int32_t red_shift = 0;
155 int32_t red_bits_to_discard = 8;
156 int32_t alpha_bits_to_discard = 8;
178 if ((uint8_t)dw_r_bit_mask & 1)
186 red_bits_to_discard -= 1;
188 }
while (dw_r_bit_mask);
208 uint32_t remaining_green_mask = dw_g_bit_mask;
214 if ((uint8_t)remaining_green_mask & 1)
218 if (remaining_green_mask)
222 green_bits_to_discard -= 1;
223 remaining_green_mask >>= 1;
224 }
while (remaining_green_mask);
233 remaining_green_mask >>= 1;
235 if (!remaining_green_mask)
244 uint32_t dw_b_bit_mask = texture_surface_desc.ddpfPixelFormat.dwBBitMask;
253 if ((uint8_t)dw_b_bit_mask & 1)
264 blue_bits_to_discard -= 1;
266 }
while (dw_b_bit_mask);
287 uint32_t dw_rgb_alpha_bit_mask = texture_surface_desc.ddpfPixelFormat.dwRGBAlphaBitMask;
289 if (dw_rgb_alpha_bit_mask)
295 if ((uint8_t)dw_rgb_alpha_bit_mask & 1)
299 if (dw_rgb_alpha_bit_mask)
303 alpha_bits_to_discard -= 1;
304 dw_rgb_alpha_bit_mask >>= 1;
305 }
while (dw_rgb_alpha_bit_mask);
314 dw_rgb_alpha_bit_mask >>= 1;
316 if (!dw_rgb_alpha_bit_mask)
329 HRESULT error_code = surface_factory->lpVtbl->CreateSurface(surface_factory, &texture_surface_desc,
339 char *creation_error_context =
"DirectX Error: CreateTexture();";
353 if (!pixel_format || !selected_format_context)
356 uint32_t dw_flags = pixel_format->dwFlags;
358 if (dw_flags & (DDPF_LUMINANCE | DDPF_BUMPLUMINANCE | DDPF_BUMPDUDV))
361 uint32_t dw_rgb_bit_count = pixel_format->dwRGBBitCount;
363 if (dw_rgb_bit_count < 0x10 || pixel_format->dwFourCC)
366 uint32_t dw_rgb_alpha_bit_mask = pixel_format->dwRGBAlphaBitMask;
369 if (dw_rgb_alpha_bit_mask == 0xf000)
371 if (requires_alpha != 1)
374 else if (requires_alpha || (dw_flags & DDPF_ALPHAPIXELS) || dw_rgb_bit_count != 16)
378 const volatile uint32_t *source = (
const volatile uint32_t *)pixel_format;
379 volatile uint32_t *destination = selected_format_context;
380 for (
unsigned i = 0; i <
sizeof(*pixel_format) /
sizeof(*destination); ++i)
381 destination[i] = source[i];
390 DDSURFACEDESC2 create_desc;
391 memset(&create_desc, 0, 0x7c);
392 DDSURFACEDESC2 source_desc;
393 source_desc.dwSize = 0x7c;
394 create_desc.dwSize = 0x7c;
398 DDSURFACEDESC2 *source_description_output = &source_desc;
399 IDirectDrawSurface7Vtbl *lp_vtbl = source_surface->lpVtbl;
400 source_desc.dwFlags = DDSD_PIXELFORMAT;
401 lp_vtbl->GetSurfaceDesc(source_surface, source_description_output);
402 memcpy(&create_desc.ddpfPixelFormat, &source_desc.ddpfPixelFormat, 0x20);
407 create_desc.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH);
409 if (!implicit_format)
410 create_desc.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
412 create_desc.dwWidth = width;
413 create_desc.dwHeight = height;
415 IUnknown *outer_unknown = NULL;
416 IDirectDrawSurface7 *surface;
417 IDirectDrawSurface7 **surface_output = &surface;
418 create_desc.ddsCaps.dwCaps = (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY);
421 surface_factory->lpVtbl->CreateSurface(surface_factory, &create_desc, surface_output, outer_unknown);
430 char *creation_error_context =
"DirectXError: CreateWorkSurface();";
451 alpha_enable_device->lpVtbl->SetRenderState(alpha_enable_device, D3DRENDERSTATE_ALPHABLENDENABLE, 1);
453 IDirect3DDevice7 *alpha_operation_device =
g_d3dDevice7;
455 DWORD alpha_argument_state;
457 IDirect3DDevice7 *alpha_argument_device;
458 IDirect3DDevice7Vtbl *lp_vtbl;
462 alpha_operation_device->lpVtbl->SetTextureStageState(alpha_operation_device, 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
466 alpha_argument_state = D3DTSS_ALPHAARG1;
467 lp_vtbl = alpha_argument_device->lpVtbl;
471 alpha_operation_device->lpVtbl->SetTextureStageState(alpha_operation_device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
473 IDirect3DDevice7 *alpha_first_argument_device =
g_d3dDevice7;
475 alpha_first_argument_device->lpVtbl->SetTextureStageState(alpha_first_argument_device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
478 alpha_argument_state = D3DTSS_ALPHAARG2;
479 lp_vtbl = alpha_argument_device->lpVtbl;
482 lp_vtbl->SetTextureStageState(alpha_argument_device, 0, alpha_argument_state, D3DTA_DIFFUSE);
486 bool blend_mode_unchanged;
488 IDirect3DDevice7 *destination_blend_device;
494 if (previous_blend_mode != blend_mode)
496 IDirect3DDevice7 *additive_source_blend_device =
g_d3dDevice7;
498 additive_source_blend_device->lpVtbl->SetRenderState(additive_source_blend_device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
501 destination_blend_device->lpVtbl->SetRenderState(destination_blend_device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_ONE);
507 blend_mode_unchanged = previous_blend_mode == blend_mode;
509 if (!blend_mode_unchanged)
511 IDirect3DDevice7 *alpha_source_blend_device =
g_d3dDevice7;
513 alpha_source_blend_device->lpVtbl->SetRenderState(alpha_source_blend_device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
516 destination_blend_device->lpVtbl->SetRenderState(destination_blend_device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
522 IDirect3DDevice7 *default_alpha_enable_device =
g_d3dDevice7;
526 default_alpha_enable_device->lpVtbl->SetRenderState(default_alpha_enable_device, D3DRENDERSTATE_ALPHABLENDENABLE, 0);
527 IDirect3DDevice7 *text_alpha_operation_device =
g_d3dDevice7;
529 text_alpha_operation_device->lpVtbl->SetTextureStageState(text_alpha_operation_device, 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
534 default_alpha_enable_device->lpVtbl->SetRenderState(default_alpha_enable_device, D3DRENDERSTATE_ALPHABLENDENABLE, 1);
536 IDirect3DDevice7 *default_alpha_operation_device =
g_d3dDevice7;
538 default_alpha_operation_device->lpVtbl->SetTextureStageState(default_alpha_operation_device, blend_mode, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
540 IDirect3DDevice7 *default_alpha_argument_device =
g_d3dDevice7;
542 default_alpha_argument_device->lpVtbl->SetTextureStageState(default_alpha_argument_device, blend_mode, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
543 IDirect3DDevice7 *default_source_blend_device =
g_d3dDevice7;
545 default_source_blend_device->lpVtbl->SetRenderState(default_source_blend_device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
546 IDirect3DDevice7 *default_destination_blend_device =
g_d3dDevice7;
548 default_destination_blend_device->lpVtbl->SetRenderState(default_destination_blend_device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
557 if (text_rendering_mode)