| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Direct3D bitmap driver |
| 12 |
* |
| 13 |
* By Trent Gamblin. |
| 14 |
* |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <string.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <math.h> |
| 20 |
|
| 21 |
#include "allegro5/allegro.h" |
| 22 |
#include "allegro5/system.h" |
| 23 |
#include "allegro5/internal/aintern.h" |
| 24 |
#include "allegro5/internal/aintern_bitmap.h" |
| 25 |
#include "allegro5/internal/aintern_display.h" |
| 26 |
#include "allegro5/internal/aintern_memblit.h" |
| 27 |
#include "allegro5/internal/aintern_pixels.h" |
| 28 |
#include "allegro5/internal/aintern_system.h" |
| 29 |
#include "allegro5/internal/aintern_tri_soft.h" // For ALLEGRO_VERTEX |
| 30 |
#include "allegro5/platform/aintwin.h" |
| 31 |
|
| 32 |
#include "d3d.h" |
| 33 |
|
| 34 |
ALLEGRO_DEBUG_CHANNEL("d3d") |
| 35 |
|
| 36 |
|
| 37 |
static ALLEGRO_BITMAP_INTERFACE *vt; |
| 38 |
|
| 39 |
// C++ needs to cast void pointers |
| 40 |
#define get_extra(b) ((ALLEGRO_BITMAP_EXTRA_D3D *)\ |
| 41 |
(b->parent ? b->parent->extra : b->extra)) |
| 42 |
|
| 43 |
static bool convert_compressed(LPDIRECT3DTEXTURE9 dest, LPDIRECT3DTEXTURE9 src, |
| 44 |
int x, int y, int width, int height) { |
| 45 |
#ifdef ALLEGRO_CFG_D3DX9 |
| 46 |
bool ok = true; |
| 47 |
LPDIRECT3DSURFACE9 dest_texture_surface = NULL; |
| 48 |
LPDIRECT3DSURFACE9 src_texture_surface = NULL; |
| 49 |
|
| 50 |
if (dest->GetSurfaceLevel(0, &dest_texture_surface) != D3D_OK) { |
| 51 |
ALLEGRO_ERROR("convert_compressed: GetSurfaceLevel failed on dest.\n"); |
| 52 |
ok = false; |
| 53 |
} |
| 54 |
|
| 55 |
if (ok && src->GetSurfaceLevel(0, &src_texture_surface) != D3D_OK) { |
| 56 |
ALLEGRO_ERROR("convert_compressed: GetSurfaceLevel failed on src.\n"); |
| 57 |
ok = false; |
| 58 |
} |
| 59 |
|
| 60 |
RECT rect; |
| 61 |
rect.left = x; |
| 62 |
rect.top = y; |
| 63 |
rect.right = x + width; |
| 64 |
rect.bottom = y + height; |
| 65 |
|
| 66 |
if (ok && _al_imp_D3DXLoadSurfaceFromSurface && |
| 67 |
_al_imp_D3DXLoadSurfaceFromSurface(dest_texture_surface, |
| 68 |
NULL, |
| 69 |
&rect, |
| 70 |
src_texture_surface, |
| 71 |
NULL, |
| 72 |
&rect, |
| 73 |
D3DX_FILTER_NONE, |
| 74 |
0) != D3D_OK) { |
| 75 |
ALLEGRO_ERROR("convert_compressed: D3DXLoadSurfaceFromSurface failed.\n"); |
| 76 |
ok = false; |
| 77 |
} |
| 78 |
|
| 79 |
int i; |
| 80 |
if (src_texture_surface) { |
| 81 |
if ((i = src_texture_surface->Release()) != 0) { |
| 82 |
ALLEGRO_DEBUG("convert_compressed (src) ref count == %d\n", i); |
| 83 |
} |
| 84 |
} |
| 85 |
if (dest_texture_surface) { |
| 86 |
if ((i = dest_texture_surface->Release()) != 0) { |
| 87 |
// This can be non-zero |
| 88 |
ALLEGRO_DEBUG("convert_compressed (dest) ref count == %d\n", i); |
| 89 |
} |
| 90 |
} |
| 91 |
return ok; |
| 92 |
#else |
| 93 |
(void)dest; |
| 94 |
(void)src; |
| 95 |
(void)x; |
| 96 |
(void)y; |
| 97 |
(void)width; |
| 98 |
(void)height; |
| 99 |
return false; |
| 100 |
#endif |
| 101 |
} |
| 102 |
|
| 103 |
/* Function: al_get_d3d_texture_size |
| 104 |
*/ |
| 105 |
bool al_get_d3d_texture_size(ALLEGRO_BITMAP *bitmap, int *width, int *height) |
| 106 |
{ |
| 107 |
int bitmap_flags = al_get_bitmap_flags(bitmap); |
| 108 |
ASSERT(bitmap); |
| 109 |
ASSERT(width); |
| 110 |
ASSERT(height); |
| 111 |
|
| 112 |
if (!(bitmap_flags & _ALLEGRO_INTERNAL_OPENGL) && |
| 113 |
!(bitmap_flags & ALLEGRO_MEMORY_BITMAP)) { |
| 114 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bmp = get_extra(bitmap); |
| 115 |
*width = d3d_bmp->texture_w; |
| 116 |
*height = d3d_bmp->texture_h; |
| 117 |
return true; |
| 118 |
} |
| 119 |
else { |
| 120 |
*width = 0; |
| 121 |
*height = 0; |
| 122 |
return false; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
void _al_d3d_bmp_destroy(void) |
| 127 |
{ |
| 128 |
al_free(vt); |
| 129 |
vt = NULL; |
| 130 |
} |
| 131 |
|
| 132 |
static INLINE void transform_vertex(float* x, float* y, float* z) |
| 133 |
{ |
| 134 |
al_transform_coordinates_3d(al_get_current_transform(), x, y, z); |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
* Draw a textured quad |
| 139 |
*/ |
| 140 |
static void d3d_draw_textured_quad( |
| 141 |
ALLEGRO_DISPLAY_D3D *disp, ALLEGRO_BITMAP *bmp, ALLEGRO_COLOR tint, |
| 142 |
float sx, float sy, float sw, float sh, int flags) |
| 143 |
{ |
| 144 |
float right; |
| 145 |
float bottom; |
| 146 |
float tu_start; |
| 147 |
float tv_start; |
| 148 |
float tu_end; |
| 149 |
float tv_end; |
| 150 |
int texture_w; |
| 151 |
int texture_h; |
| 152 |
ALLEGRO_BITMAP_EXTRA_D3D *extra = get_extra(bmp); |
| 153 |
|
| 154 |
const float z = 0.0f; |
| 155 |
|
| 156 |
ALLEGRO_DISPLAY* aldisp = (ALLEGRO_DISPLAY*)disp; |
| 157 |
|
| 158 |
if (aldisp->num_cache_vertices != 0 && (uintptr_t)bmp != aldisp->cache_texture) { |
| 159 |
aldisp->vt->flush_vertex_cache(aldisp); |
| 160 |
} |
| 161 |
aldisp->cache_texture = (uintptr_t)bmp; |
| 162 |
|
| 163 |
right = sw; |
| 164 |
bottom = sh; |
| 165 |
|
| 166 |
texture_w = extra->texture_w; |
| 167 |
texture_h = extra->texture_h; |
| 168 |
tu_start = sx / texture_w; |
| 169 |
tv_start = sy / texture_h; |
| 170 |
tu_end = sw / texture_w + tu_start; |
| 171 |
tv_end = sh / texture_h + tv_start; |
| 172 |
|
| 173 |
if (flags & ALLEGRO_FLIP_HORIZONTAL) { |
| 174 |
float temp = tu_start; |
| 175 |
tu_start = tu_end; |
| 176 |
tu_end = temp; |
| 177 |
} |
| 178 |
if (flags & ALLEGRO_FLIP_VERTICAL) { |
| 179 |
float temp = tv_start; |
| 180 |
tv_start = tv_end; |
| 181 |
tv_end = temp; |
| 182 |
} |
| 183 |
|
| 184 |
#define ALLEGRO_COLOR_TO_D3D(c) D3DCOLOR_COLORVALUE(c.r, c.g, c.b, c.a) |
| 185 |
|
| 186 |
#define SET(f) \ |
| 187 |
vertices[0].x = 0; \ |
| 188 |
vertices[0].y = 0; \ |
| 189 |
vertices[0].z = z; \ |
| 190 |
vertices[0].color = f(tint); \ |
| 191 |
vertices[0].u = tu_start; \ |
| 192 |
vertices[0].v = tv_start; \ |
| 193 |
\ |
| 194 |
vertices[1].x = right; \ |
| 195 |
vertices[1].y = 0; \ |
| 196 |
vertices[1].z = z; \ |
| 197 |
vertices[1].color = f(tint); \ |
| 198 |
vertices[1].u = tu_end; \ |
| 199 |
vertices[1].v = tv_start; \ |
| 200 |
\ |
| 201 |
vertices[2].x = right; \ |
| 202 |
vertices[2].y = bottom; \ |
| 203 |
vertices[2].z = z; \ |
| 204 |
vertices[2].color = f(tint); \ |
| 205 |
vertices[2].u = tu_end; \ |
| 206 |
vertices[2].v = tv_end; \ |
| 207 |
\ |
| 208 |
vertices[5].x = 0; \ |
| 209 |
vertices[5].y = bottom; \ |
| 210 |
vertices[5].z = z; \ |
| 211 |
vertices[5].color = f(tint); \ |
| 212 |
vertices[5].u = tu_start; \ |
| 213 |
vertices[5].v = tv_end; \ |
| 214 |
\ |
| 215 |
if (aldisp->cache_enabled) { \ |
| 216 |
transform_vertex(&vertices[0].x, &vertices[0].y, &vertices[0].z); \ |
| 217 |
transform_vertex(&vertices[1].x, &vertices[1].y, &vertices[1].z); \ |
| 218 |
transform_vertex(&vertices[2].x, &vertices[2].y, &vertices[2].z); \ |
| 219 |
transform_vertex(&vertices[5].x, &vertices[5].y, &vertices[5].z); \ |
| 220 |
} \ |
| 221 |
\ |
| 222 |
vertices[3] = vertices[0]; \ |
| 223 |
vertices[4] = vertices[2]; |
| 224 |
|
| 225 |
bool pp = (aldisp->flags & ALLEGRO_PROGRAMMABLE_PIPELINE) != 0; |
| 226 |
|
| 227 |
if (pp) { |
| 228 |
ALLEGRO_VERTEX *vertices = (ALLEGRO_VERTEX *)aldisp->vt->prepare_vertex_cache(aldisp, 6); |
| 229 |
SET(ALLEGRO_COLOR) |
| 230 |
} |
| 231 |
else { |
| 232 |
D3D_FIXED_VERTEX *vertices = (D3D_FIXED_VERTEX *)aldisp->vt->prepare_vertex_cache(aldisp, 6); |
| 233 |
SET(ALLEGRO_COLOR_TO_D3D) |
| 234 |
} |
| 235 |
|
| 236 |
if (!aldisp->cache_enabled) |
| 237 |
aldisp->vt->flush_vertex_cache(aldisp); |
| 238 |
} |
| 239 |
|
| 240 |
/* Copy texture memory to bitmap->memory */ |
| 241 |
static void d3d_sync_bitmap_memory(ALLEGRO_BITMAP *bitmap) |
| 242 |
{ |
| 243 |
D3DLOCKED_RECT locked_rect; |
| 244 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bmp = get_extra(bitmap); |
| 245 |
LPDIRECT3DTEXTURE9 texture; |
| 246 |
int bitmap_format = al_get_bitmap_format(bitmap); |
| 247 |
|
| 248 |
if (_al_d3d_render_to_texture_supported() && |
| 249 |
!_al_pixel_format_is_compressed(bitmap_format)) |
| 250 |
texture = d3d_bmp->system_texture; |
| 251 |
else |
| 252 |
texture = d3d_bmp->video_texture; |
| 253 |
|
| 254 |
if (texture->LockRect(0, &locked_rect, NULL, 0) == D3D_OK) { |
| 255 |
int block_size = al_get_pixel_block_size(bitmap_format); |
| 256 |
int block_width = al_get_pixel_block_width(bitmap_format); |
| 257 |
int block_height = al_get_pixel_block_height(bitmap_format); |
| 258 |
int mem_pitch = _al_get_least_multiple(bitmap->w, block_width) * |
| 259 |
block_size / block_width; |
| 260 |
_al_copy_bitmap_data(locked_rect.pBits, locked_rect.Pitch, |
| 261 |
bitmap->memory, mem_pitch, |
| 262 |
0, 0, 0, 0, _al_get_least_multiple(bitmap->w, block_width), |
| 263 |
_al_get_least_multiple(bitmap->h, block_height), bitmap_format); |
| 264 |
texture->UnlockRect(0); |
| 265 |
} |
| 266 |
else { |
| 267 |
ALLEGRO_ERROR("d3d_sync_bitmap_memory: Couldn't lock texture.\n"); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/* Copy bitmap->memory to texture memory */ |
| 272 |
static void d3d_sync_bitmap_texture(ALLEGRO_BITMAP *bitmap, |
| 273 |
int x, int y, int width, int height) |
| 274 |
{ |
| 275 |
D3DLOCKED_RECT locked_rect; |
| 276 |
RECT rect; |
| 277 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bmp = get_extra(bitmap); |
| 278 |
LPDIRECT3DTEXTURE9 texture; |
| 279 |
int bitmap_format = al_get_bitmap_format(bitmap); |
| 280 |
|
| 281 |
if (bitmap->parent) return; |
| 282 |
|
| 283 |
rect.left = x; |
| 284 |
rect.top = y; |
| 285 |
rect.right = x + width; |
| 286 |
rect.bottom = y + height; |
| 287 |
|
| 288 |
if (_al_d3d_render_to_texture_supported() && |
| 289 |
!_al_pixel_format_is_compressed(bitmap_format)) |
| 290 |
texture = d3d_bmp->system_texture; |
| 291 |
else |
| 292 |
texture = d3d_bmp->video_texture; |
| 293 |
|
| 294 |
if (texture->LockRect(0, &locked_rect, &rect, 0) == D3D_OK) { |
| 295 |
int block_size = al_get_pixel_block_size(bitmap_format); |
| 296 |
int block_width = al_get_pixel_block_width(bitmap_format); |
| 297 |
int mem_pitch = _al_get_least_multiple(bitmap->w, block_width) * |
| 298 |
block_size / block_width; |
| 299 |
_al_copy_bitmap_data(bitmap->memory, mem_pitch, |
| 300 |
locked_rect.pBits, locked_rect.Pitch, |
| 301 |
x, y, 0, 0, width, height, bitmap_format); |
| 302 |
texture->UnlockRect(0); |
| 303 |
} |
| 304 |
else { |
| 305 |
ALLEGRO_ERROR("d3d_sync_bitmap_texture: Couldn't lock texture to upload.\n"); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
static void d3d_do_upload(ALLEGRO_BITMAP *bmp, int x, int y, int width, |
| 310 |
int height, bool sync_from_memory) |
| 311 |
{ |
| 312 |
if (sync_from_memory) { |
| 313 |
d3d_sync_bitmap_texture(bmp, x, y, width, height); |
| 314 |
} |
| 315 |
|
| 316 |
if (_al_d3d_render_to_texture_supported() |
| 317 |
&& !_al_pixel_format_is_compressed(al_get_bitmap_format(bmp))) { |
| 318 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bitmap = get_extra(bmp); |
| 319 |
if (d3d_bitmap->display->device->UpdateTexture( |
| 320 |
(IDirect3DBaseTexture9 *)d3d_bitmap->system_texture, |
| 321 |
(IDirect3DBaseTexture9 *)d3d_bitmap->video_texture) != D3D_OK) { |
| 322 |
ALLEGRO_ERROR("d3d_do_upload: Couldn't update texture.\n"); |
| 323 |
return; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
/* |
| 329 |
* Release all default pool textures. This must be done before |
| 330 |
* resetting the device. |
| 331 |
*/ |
| 332 |
void _al_d3d_release_default_pool_textures(ALLEGRO_DISPLAY *disp) |
| 333 |
{ |
| 334 |
unsigned int i; |
| 335 |
|
| 336 |
for (i = 0; i < disp->bitmaps._size; i++) { |
| 337 |
ALLEGRO_BITMAP **bptr = (ALLEGRO_BITMAP **)_al_vector_ref(&disp->bitmaps, i); |
| 338 |
ALLEGRO_BITMAP *albmp = *bptr; |
| 339 |
|
| 340 |
if ((al_get_bitmap_flags(albmp) & ALLEGRO_MEMORY_BITMAP) || (albmp->parent)) |
| 341 |
continue; |
| 342 |
|
| 343 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bmp = get_extra(albmp); |
| 344 |
if (!d3d_bmp->is_backbuffer && d3d_bmp->render_target) { |
| 345 |
d3d_bmp->render_target->Release(); |
| 346 |
d3d_bmp->render_target = NULL; |
| 347 |
} |
| 348 |
if (d3d_bmp->video_texture) { |
| 349 |
d3d_bmp->video_texture->Release(); |
| 350 |
d3d_bmp->video_texture = NULL; |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
static bool d3d_create_textures(ALLEGRO_DISPLAY_D3D *disp, int w, int h, |
| 356 |
int flags, |
| 357 |
LPDIRECT3DTEXTURE9 *video_texture, LPDIRECT3DTEXTURE9 *system_texture, |
| 358 |
int video_format, int system_format) |
| 359 |
{ |
| 360 |
int levels; |
| 361 |
int autogenmipmap; |
| 362 |
int err; |
| 363 |
bool compressed = _al_pixel_format_is_compressed(video_format); |
| 364 |
|
| 365 |
if (flags & ALLEGRO_MIPMAP) { |
| 366 |
/* "0" for all possible levels, required for auto mipmap generation. */ |
| 367 |
levels = 0; |
| 368 |
autogenmipmap = D3DUSAGE_AUTOGENMIPMAP; |
| 369 |
} |
| 370 |
else { |
| 371 |
levels = 1; |
| 372 |
autogenmipmap = 0; |
| 373 |
} |
| 374 |
|
| 375 |
if (_al_d3d_render_to_texture_supported()) { |
| 376 |
if (video_texture) { |
| 377 |
int usage = compressed ? 0 : D3DUSAGE_RENDERTARGET; |
| 378 |
/* XXX: Compressed video bitmaps are managed, so in principle |
| 379 |
* there is no need to manually sync them for device loss. |
| 380 |
* It is still necessary to do so for resizing purposes, |
| 381 |
* however... not sure there's any real savings to be had here. |
| 382 |
*/ |
| 383 |
D3DPOOL pool = compressed ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT; |
| 384 |
err = disp->device->CreateTexture(w, h, levels, |
| 385 |
usage | autogenmipmap, |
| 386 |
(D3DFORMAT)_al_pixel_format_to_d3d(video_format), pool, |
| 387 |
video_texture, NULL); |
| 388 |
if (err != D3D_OK && err != D3DOK_NOAUTOGEN) { |
| 389 |
ALLEGRO_ERROR("d3d_create_textures: Unable to create video texture.\n"); |
| 390 |
return false; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
if (system_texture) { |
| 395 |
err = disp->device->CreateTexture(w, h, 1, |
| 396 |
0, (D3DFORMAT)_al_pixel_format_to_d3d(system_format), D3DPOOL_SYSTEMMEM, |
| 397 |
system_texture, NULL); |
| 398 |
if (err != D3D_OK) { |
| 399 |
ALLEGRO_ERROR("d3d_create_textures: Unable to create system texture.\n"); |
| 400 |
if (video_texture && (*video_texture)) { |
| 401 |
(*video_texture)->Release(); |
| 402 |
*video_texture = NULL; |
| 403 |
} |
| 404 |
return false; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
return true; |
| 409 |
} |
| 410 |
else { |
| 411 |
if (video_texture) { |
| 412 |
err = disp->device->CreateTexture(w, h, 1, |
| 413 |
0, (D3DFORMAT)_al_pixel_format_to_d3d(video_format), D3DPOOL_MANAGED, |
| 414 |
video_texture, NULL); |
| 415 |
if (err != D3D_OK) { |
| 416 |
ALLEGRO_ERROR("d3d_create_textures: Unable to create video texture (no render-to-texture).\n"); |
| 417 |
return false; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
return true; |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
static ALLEGRO_BITMAP |
| 426 |
*d3d_create_bitmap_from_surface(LPDIRECT3DSURFACE9 surface, |
| 427 |
int format, int flags) |
| 428 |
{ |
| 429 |
ALLEGRO_BITMAP *bitmap; |
| 430 |
ALLEGRO_BITMAP_EXTRA_D3D *extra; |
| 431 |
D3DSURFACE_DESC desc; |
| 432 |
D3DLOCKED_RECT surf_locked_rect; |
| 433 |
D3DLOCKED_RECT sys_locked_rect; |
| 434 |
unsigned int y; |
| 435 |
ASSERT(!_al_pixel_format_is_compressed(format)); |
| 436 |
|
| 437 |
if (surface->GetDesc(&desc) != D3D_OK) { |
| 438 |
ALLEGRO_ERROR("d3d_create_bitmap_from_surface: GetDesc failed.\n"); |
| 439 |
return NULL; |
| 440 |
} |
| 441 |
|
| 442 |
if (surface->LockRect(&surf_locked_rect, 0, D3DLOCK_READONLY) != D3D_OK) { |
| 443 |
ALLEGRO_ERROR("d3d_create_bitmap_from_surface: LockRect failed.\n"); |
| 444 |
return NULL; |
| 445 |
} |
| 446 |
|
| 447 |
bitmap = _al_create_bitmap_params(al_get_current_display(), |
| 448 |
desc.Width, desc.Height, format, flags, 0, 0); |
| 449 |
if (!bitmap) { |
| 450 |
surface->UnlockRect(); |
| 451 |
return NULL; |
| 452 |
} |
| 453 |
|
| 454 |
extra = get_extra(bitmap); |
| 455 |
|
| 456 |
if (extra->system_texture->LockRect(0, &sys_locked_rect, 0, 0) != D3D_OK) { |
| 457 |
surface->UnlockRect(); |
| 458 |
al_destroy_bitmap(bitmap); |
| 459 |
ALLEGRO_ERROR("d3d_create_bitmap_from_surface: Lock system texture failed.\n"); |
| 460 |
return NULL; |
| 461 |
} |
| 462 |
|
| 463 |
for (y = 0; y < desc.Height; y++) { |
| 464 |
memcpy( |
| 465 |
((char*)sys_locked_rect.pBits)+(sys_locked_rect.Pitch*y), |
| 466 |
((char*)surf_locked_rect.pBits)+(surf_locked_rect.Pitch*y), |
| 467 |
desc.Width*al_get_pixel_size(format) |
| 468 |
); |
| 469 |
} |
| 470 |
|
| 471 |
surface->UnlockRect(); |
| 472 |
extra->system_texture->UnlockRect(0); |
| 473 |
|
| 474 |
|
| 475 |
if (extra->display->device->UpdateTexture( |
| 476 |
(IDirect3DBaseTexture9 *)extra->system_texture, |
| 477 |
(IDirect3DBaseTexture9 *)extra->video_texture) != D3D_OK) { |
| 478 |
ALLEGRO_ERROR("d3d_create_bitmap_from_surface: Couldn't update texture.\n"); |
| 479 |
} |
| 480 |
|
| 481 |
return bitmap; |
| 482 |
} |
| 483 |
|
| 484 |
/* Copies video texture to system texture and bitmap->memory */ |
| 485 |
static bool _al_d3d_sync_bitmap(ALLEGRO_BITMAP *dest) |
| 486 |
{ |
| 487 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_dest; |
| 488 |
LPDIRECT3DSURFACE9 system_texture_surface; |
| 489 |
LPDIRECT3DSURFACE9 video_texture_surface; |
| 490 |
bool ok; |
| 491 |
UINT i; |
| 492 |
|
| 493 |
if (!_al_d3d_render_to_texture_supported()) { |
| 494 |
ALLEGRO_ERROR("Render-to-texture not supported.\n"); |
| 495 |
return false; |
| 496 |
} |
| 497 |
|
| 498 |
if (dest->locked) { |
| 499 |
ALLEGRO_ERROR("Already locked.\n"); |
| 500 |
return false; |
| 501 |
} |
| 502 |
|
| 503 |
d3d_dest = get_extra(dest); |
| 504 |
|
| 505 |
if (d3d_dest->system_texture == NULL || d3d_dest->video_texture == NULL) { |
| 506 |
ALLEGRO_ERROR("A texture is null.\n"); |
| 507 |
return false; |
| 508 |
} |
| 509 |
|
| 510 |
if (dest->parent) { |
| 511 |
dest = dest->parent; |
| 512 |
} |
| 513 |
|
| 514 |
ok = true; |
| 515 |
system_texture_surface = NULL; |
| 516 |
video_texture_surface = NULL; |
| 517 |
|
| 518 |
if (d3d_dest->system_texture->GetSurfaceLevel( |
| 519 |
0, &system_texture_surface) != D3D_OK) { |
| 520 |
ALLEGRO_ERROR("_al_d3d_sync_bitmap: GetSurfaceLevel failed while updating system texture.\n"); |
| 521 |
ok = false; |
| 522 |
} |
| 523 |
|
| 524 |
if (ok && d3d_dest->video_texture->GetSurfaceLevel( |
| 525 |
0, &video_texture_surface) != D3D_OK) { |
| 526 |
ALLEGRO_ERROR("_al_d3d_sync_bitmap: GetSurfaceLevel failed while updating system texture.\n"); |
| 527 |
ok = false; |
| 528 |
} |
| 529 |
|
| 530 |
if (ok && d3d_dest->display->device->GetRenderTargetData( |
| 531 |
video_texture_surface, |
| 532 |
system_texture_surface) != D3D_OK) { |
| 533 |
ALLEGRO_ERROR("_al_d3d_sync_bitmap: GetRenderTargetData failed.\n"); |
| 534 |
ok = false; |
| 535 |
} |
| 536 |
|
| 537 |
if (system_texture_surface) { |
| 538 |
if ((i = system_texture_surface->Release()) != 0) { |
| 539 |
ALLEGRO_DEBUG("_al_d3d_sync_bitmap (system) ref count == %d\n", i); |
| 540 |
} |
| 541 |
} |
| 542 |
if (video_texture_surface) { |
| 543 |
if ((i = video_texture_surface->Release()) != 0) { |
| 544 |
// This can be non-zero |
| 545 |
ALLEGRO_DEBUG("_al_d3d_sync_bitmap (video) ref count == %d\n", i); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
if (ok) { |
| 550 |
d3d_sync_bitmap_memory(dest); |
| 551 |
} |
| 552 |
|
| 553 |
return ok; |
| 554 |
} |
| 555 |
|
| 556 |
// Copy texture to system memory |
| 557 |
static void d3d_backup_dirty_bitmap(ALLEGRO_BITMAP *bitmap) |
| 558 |
{ |
| 559 |
ALLEGRO_DISPLAY *display = bitmap->_display; |
| 560 |
ALLEGRO_DISPLAY_D3D *d3d_display = (ALLEGRO_DISPLAY_D3D *)display; |
| 561 |
|
| 562 |
if (d3d_display->device_lost) |
| 563 |
return; |
| 564 |
|
| 565 |
if (!_al_d3d_render_to_texture_supported()) |
| 566 |
return; |
| 567 |
|
| 568 |
al_lock_mutex(_al_d3d_lost_device_mutex); |
| 569 |
|
| 570 |
ALLEGRO_BITMAP_EXTRA_D3D *extra = get_extra(bitmap); |
| 571 |
int bitmap_flags = al_get_bitmap_flags(bitmap); |
| 572 |
if (!( |
| 573 |
(bitmap_flags & ALLEGRO_MEMORY_BITMAP) || |
| 574 |
(bitmap_flags & ALLEGRO_NO_PRESERVE_TEXTURE) || |
| 575 |
!bitmap->dirty || |
| 576 |
extra->is_backbuffer || |
| 577 |
bitmap->parent |
| 578 |
)) |
| 579 |
{ |
| 580 |
if (_al_pixel_format_is_compressed(al_get_bitmap_format(bitmap))) |
| 581 |
d3d_sync_bitmap_memory(bitmap); |
| 582 |
else |
| 583 |
_al_d3d_sync_bitmap(bitmap); |
| 584 |
} |
| 585 |
|
| 586 |
bitmap->dirty = false; |
| 587 |
|
| 588 |
al_unlock_mutex(_al_d3d_lost_device_mutex); |
| 589 |
} |
| 590 |
|
| 591 |
/* |
| 592 |
* Called after the resize is done. |
| 593 |
*/ |
| 594 |
bool _al_d3d_recreate_bitmap_textures(ALLEGRO_DISPLAY_D3D *disp) |
| 595 |
{ |
| 596 |
unsigned int i; |
| 597 |
ALLEGRO_DISPLAY *display = (ALLEGRO_DISPLAY *)disp; |
| 598 |
|
| 599 |
for (i = 0; i < display->bitmaps._size; i++) { |
| 600 |
ALLEGRO_BITMAP **bptr = (ALLEGRO_BITMAP **)_al_vector_ref(&display->bitmaps, i); |
| 601 |
ALLEGRO_BITMAP *bmp = *bptr; |
| 602 |
ALLEGRO_BITMAP_EXTRA_D3D *extra = get_extra(bmp); |
| 603 |
|
| 604 |
if ((void *)_al_get_bitmap_display(bmp) == (void *)disp) { |
| 605 |
int block_width = |
| 606 |
al_get_pixel_block_width(al_get_bitmap_format(bmp)); |
| 607 |
int block_height = |
| 608 |
al_get_pixel_block_height(al_get_bitmap_format(bmp)); |
| 609 |
if (!d3d_create_textures(disp, extra->texture_w, |
| 610 |
extra->texture_h, |
| 611 |
al_get_bitmap_flags(bmp), |
| 612 |
&extra->video_texture, |
| 613 |
&extra->system_texture, |
| 614 |
al_get_bitmap_format(bmp), |
| 615 |
extra->system_format)) |
| 616 |
return false; |
| 617 |
d3d_do_upload(bmp, 0, 0, |
| 618 |
_al_get_least_multiple(bmp->w, block_width), |
| 619 |
_al_get_least_multiple(bmp->h, block_height), true); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
return true; |
| 624 |
} |
| 625 |
|
| 626 |
/* |
| 627 |
* Refresh the texture memory. This must be done after a device is |
| 628 |
* lost or after it is reset. |
| 629 |
*/ |
| 630 |
void _al_d3d_refresh_texture_memory(ALLEGRO_DISPLAY *display) |
| 631 |
{ |
| 632 |
unsigned int i; |
| 633 |
|
| 634 |
/* Refresh video hardware textures */ |
| 635 |
for (i = 0; i < display->bitmaps._size; i++) { |
| 636 |
ALLEGRO_BITMAP **bptr = (ALLEGRO_BITMAP **)_al_vector_ref(&display->bitmaps, i); |
| 637 |
ALLEGRO_BITMAP *bmp = *bptr; |
| 638 |
ALLEGRO_BITMAP_EXTRA_D3D *extra = get_extra(bmp); |
| 639 |
ALLEGRO_DISPLAY_D3D *bmps_display = |
| 640 |
(ALLEGRO_DISPLAY_D3D *)_al_get_bitmap_display(bmp); |
| 641 |
int bitmap_flags = al_get_bitmap_flags(bmp); |
| 642 |
|
| 643 |
if ((bitmap_flags & ALLEGRO_MEMORY_BITMAP) || bmp->parent) { |
| 644 |
continue; |
| 645 |
} |
| 646 |
|
| 647 |
d3d_create_textures(bmps_display, extra->texture_w, |
| 648 |
extra->texture_h, bitmap_flags, |
| 649 |
&extra->video_texture, /*&bmp->system_texture*/0, al_get_bitmap_format(bmp), 0); |
| 650 |
if (!(bitmap_flags & ALLEGRO_NO_PRESERVE_TEXTURE)) { |
| 651 |
int block_width = al_get_pixel_block_width(al_get_bitmap_format(bmp)); |
| 652 |
int block_height = al_get_pixel_block_height(al_get_bitmap_format(bmp)); |
| 653 |
d3d_sync_bitmap_texture(bmp, |
| 654 |
0, 0, |
| 655 |
_al_get_least_multiple(bmp->w, block_width), |
| 656 |
_al_get_least_multiple(bmp->h, block_height)); |
| 657 |
if (_al_d3d_render_to_texture_supported() |
| 658 |
&& !_al_pixel_format_is_compressed(al_get_bitmap_format(bmp))) { |
| 659 |
extra->display->device->UpdateTexture( |
| 660 |
(IDirect3DBaseTexture9 *)extra->system_texture, |
| 661 |
(IDirect3DBaseTexture9 *)extra->video_texture); |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
static bool d3d_upload_bitmap(ALLEGRO_BITMAP *bitmap) |
| 668 |
{ |
| 669 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bmp = get_extra(bitmap); |
| 670 |
ALLEGRO_SYSTEM *system = al_get_system_driver(); |
| 671 |
int bitmap_format = al_get_bitmap_format(bitmap); |
| 672 |
int system_format = d3d_bmp->system_format; |
| 673 |
int block_width = al_get_pixel_block_width(bitmap_format); |
| 674 |
int block_height = al_get_pixel_block_height(bitmap_format); |
| 675 |
int w = _al_get_least_multiple(bitmap->w, block_width); |
| 676 |
int h = _al_get_least_multiple(bitmap->h, block_height); |
| 677 |
|
| 678 |
if (d3d_bmp->display->device_lost) |
| 679 |
return false; |
| 680 |
|
| 681 |
if (d3d_bmp->initialized != true) { |
| 682 |
bool non_pow2 = al_have_d3d_non_pow2_texture_support(); |
| 683 |
bool non_square = al_have_d3d_non_square_texture_support(); |
| 684 |
|
| 685 |
if (non_pow2 && non_square) { |
| 686 |
// Any shape and size |
| 687 |
d3d_bmp->texture_w = w; |
| 688 |
d3d_bmp->texture_h = h; |
| 689 |
} |
| 690 |
else if (non_pow2) { |
| 691 |
// Must be square |
| 692 |
int max = _ALLEGRO_MAX(w, h); |
| 693 |
d3d_bmp->texture_w = max; |
| 694 |
d3d_bmp->texture_h = max; |
| 695 |
} |
| 696 |
else { |
| 697 |
// Must be POW2 |
| 698 |
d3d_bmp->texture_w = pot(w); |
| 699 |
d3d_bmp->texture_h = pot(h); |
| 700 |
} |
| 701 |
|
| 702 |
// Some cards/drivers don't like small textures |
| 703 |
if ((int)d3d_bmp->texture_w < system->min_bitmap_size) d3d_bmp->texture_w = system->min_bitmap_size; |
| 704 |
if ((int)d3d_bmp->texture_h < system->min_bitmap_size) d3d_bmp->texture_h = system->min_bitmap_size; |
| 705 |
|
| 706 |
ASSERT(d3d_bmp->texture_w % block_width == 0); |
| 707 |
ASSERT(d3d_bmp->texture_h % block_height == 0); |
| 708 |
|
| 709 |
if (d3d_bmp->video_texture == 0) |
| 710 |
if (!d3d_create_textures(d3d_bmp->display, |
| 711 |
d3d_bmp->texture_w, |
| 712 |
d3d_bmp->texture_h, |
| 713 |
al_get_bitmap_flags(bitmap), |
| 714 |
&d3d_bmp->video_texture, |
| 715 |
&d3d_bmp->system_texture, |
| 716 |
bitmap_format, |
| 717 |
system_format)) { |
| 718 |
return false; |
| 719 |
} |
| 720 |
|
| 721 |
d3d_bmp->initialized = true; |
| 722 |
} |
| 723 |
|
| 724 |
d3d_do_upload(bitmap, 0, 0, w, h, false); |
| 725 |
|
| 726 |
return true; |
| 727 |
} |
| 728 |
|
| 729 |
static void d3d_draw_bitmap_region( |
| 730 |
ALLEGRO_BITMAP *src, |
| 731 |
ALLEGRO_COLOR tint, |
| 732 |
float sx, float sy, float sw, float sh, int flags) |
| 733 |
{ |
| 734 |
ALLEGRO_BITMAP *dest = al_get_target_bitmap(); |
| 735 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_dest = get_extra(dest); |
| 736 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_src = get_extra(src); |
| 737 |
|
| 738 |
ASSERT(src->parent == NULL); |
| 739 |
|
| 740 |
if (!_al_d3d_render_to_texture_supported()) { |
| 741 |
_al_draw_bitmap_region_memory(src, tint, |
| 742 |
(int)sx, (int)sy, (int)sw, (int)sh, 0, 0, |
| 743 |
(int)flags); |
| 744 |
return; |
| 745 |
} |
| 746 |
|
| 747 |
if (d3d_dest->display->device_lost) |
| 748 |
return; |
| 749 |
|
| 750 |
_al_d3d_set_bitmap_clip(dest); |
| 751 |
|
| 752 |
/* For sub-bitmaps */ |
| 753 |
if (dest->parent) { |
| 754 |
dest = dest->parent; |
| 755 |
d3d_dest = get_extra(dest); |
| 756 |
} |
| 757 |
|
| 758 |
if (d3d_src->is_backbuffer) { |
| 759 |
IDirect3DSurface9 *surface; |
| 760 |
D3DSURFACE_DESC desc; |
| 761 |
if (d3d_src->display->render_target->GetDesc(&desc) != D3D_OK) { |
| 762 |
ALLEGRO_ERROR("d3d_draw_bitmap_region: GetDesc failed.\n"); |
| 763 |
return; |
| 764 |
} |
| 765 |
if (desc.MultiSampleType == D3DMULTISAMPLE_NONE) { |
| 766 |
surface = d3d_src->display->render_target; |
| 767 |
} |
| 768 |
else { |
| 769 |
RECT r; |
| 770 |
if (d3d_src->display->device->CreateRenderTarget( |
| 771 |
desc.Width, |
| 772 |
desc.Height, |
| 773 |
desc.Format, |
| 774 |
D3DMULTISAMPLE_NONE, |
| 775 |
0, |
| 776 |
TRUE, |
| 777 |
&surface, |
| 778 |
NULL |
| 779 |
) != D3D_OK) { |
| 780 |
ALLEGRO_ERROR( |
| 781 |
"d3d_draw_bitmap_region: CreateRenderTarget failed.\n"); |
| 782 |
return; |
| 783 |
} |
| 784 |
r.top = 0; |
| 785 |
r.left = 0; |
| 786 |
r.right = desc.Width; |
| 787 |
r.bottom = desc.Height; |
| 788 |
if (d3d_src->display->device->StretchRect( |
| 789 |
d3d_src->display->render_target, |
| 790 |
&r, |
| 791 |
surface, |
| 792 |
&r, |
| 793 |
D3DTEXF_NONE |
| 794 |
) != D3D_OK) { |
| 795 |
ALLEGRO_ERROR("d3d_draw_bitmap_region: StretchRect failed.\n"); |
| 796 |
surface->Release(); |
| 797 |
return; |
| 798 |
} |
| 799 |
} |
| 800 |
ALLEGRO_BITMAP *tmp_bmp = d3d_create_bitmap_from_surface( |
| 801 |
surface, |
| 802 |
al_get_bitmap_format(src), |
| 803 |
al_get_bitmap_flags(src) |
| 804 |
); |
| 805 |
if (tmp_bmp) { |
| 806 |
d3d_draw_bitmap_region(tmp_bmp, tint, |
| 807 |
sx, sy, sw, sh, flags); |
| 808 |
al_destroy_bitmap(tmp_bmp); |
| 809 |
if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) { |
| 810 |
surface->Release(); |
| 811 |
} |
| 812 |
} |
| 813 |
return; |
| 814 |
} |
| 815 |
|
| 816 |
_al_d3d_set_blender(d3d_dest->display); |
| 817 |
|
| 818 |
d3d_draw_textured_quad( |
| 819 |
d3d_dest->display, src, tint, |
| 820 |
sx, sy, sw, sh, flags); |
| 821 |
} |
| 822 |
|
| 823 |
static ALLEGRO_LOCKED_REGION *d3d_lock_region(ALLEGRO_BITMAP *bitmap, |
| 824 |
int x, int y, int w, int h, int format, |
| 825 |
int flags) |
| 826 |
{ |
| 827 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bmp = get_extra(bitmap); |
| 828 |
int bitmap_format = al_get_bitmap_format(bitmap); |
| 829 |
int system_format = d3d_bmp->system_format; |
| 830 |
|
| 831 |
if (d3d_bmp->display->device_lost) |
| 832 |
return NULL; |
| 833 |
|
| 834 |
RECT rect; |
| 835 |
DWORD Flags = flags & ALLEGRO_LOCK_READONLY ? D3DLOCK_READONLY : 0; |
| 836 |
int f = _al_get_real_pixel_format(al_get_current_display(), format); |
| 837 |
if (f < 0) { |
| 838 |
return NULL; |
| 839 |
} |
| 840 |
|
| 841 |
rect.left = x; |
| 842 |
rect.right = x + w; |
| 843 |
rect.top = y; |
| 844 |
rect.bottom = y + h; |
| 845 |
|
| 846 |
if (d3d_bmp->is_backbuffer) { |
| 847 |
ALLEGRO_DISPLAY_D3D *d3d_disp = |
| 848 |
(ALLEGRO_DISPLAY_D3D *)_al_get_bitmap_display(bitmap); |
| 849 |
if (d3d_disp->render_target->LockRect(&d3d_bmp->locked_rect, &rect, Flags) != D3D_OK) { |
| 850 |
ALLEGRO_ERROR("LockRect failed in d3d_lock_region.\n"); |
| 851 |
return NULL; |
| 852 |
} |
| 853 |
} |
| 854 |
else { |
| 855 |
LPDIRECT3DTEXTURE9 texture; |
| 856 |
if (_al_pixel_format_is_compressed(bitmap_format)) { |
| 857 |
if (!(flags & ALLEGRO_LOCK_WRITEONLY)) { |
| 858 |
if(!convert_compressed( |
| 859 |
d3d_bmp->system_texture, d3d_bmp->video_texture, |
| 860 |
x, y, w, h)) { |
| 861 |
ALLEGRO_ERROR("Could not decompress.\n"); |
| 862 |
return NULL; |
| 863 |
} |
| 864 |
} |
| 865 |
texture = d3d_bmp->system_texture; |
| 866 |
} |
| 867 |
else if (_al_d3d_render_to_texture_supported()) { |
| 868 |
/* |
| 869 |
* Sync bitmap->memory with texture |
| 870 |
*/ |
| 871 |
bitmap->locked = false; |
| 872 |
if (!(flags & ALLEGRO_LOCK_WRITEONLY) && !_al_d3d_sync_bitmap(bitmap)) { |
| 873 |
return NULL; |
| 874 |
} |
| 875 |
bitmap->locked = true; |
| 876 |
texture = d3d_bmp->system_texture; |
| 877 |
} |
| 878 |
else { |
| 879 |
texture = d3d_bmp->video_texture; |
| 880 |
} |
| 881 |
if (texture->LockRect(0, &d3d_bmp->locked_rect, &rect, Flags) != D3D_OK) { |
| 882 |
ALLEGRO_ERROR("LockRect failed in d3d_lock_region.\n"); |
| 883 |
return NULL; |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
if (format == ALLEGRO_PIXEL_FORMAT_ANY || system_format == format || system_format == f) { |
| 888 |
bitmap->locked_region.data = d3d_bmp->locked_rect.pBits; |
| 889 |
bitmap->locked_region.format = system_format; |
| 890 |
bitmap->locked_region.pitch = d3d_bmp->locked_rect.Pitch; |
| 891 |
bitmap->locked_region.pixel_size = al_get_pixel_size(system_format); |
| 892 |
} |
| 893 |
else { |
| 894 |
bitmap->locked_region.pitch = al_get_pixel_size(f) * w; |
| 895 |
bitmap->locked_region.data = al_malloc(bitmap->locked_region.pitch*h); |
| 896 |
bitmap->locked_region.format = f; |
| 897 |
bitmap->locked_region.pixel_size = al_get_pixel_size(f); |
| 898 |
if (!(bitmap->lock_flags & ALLEGRO_LOCK_WRITEONLY)) { |
| 899 |
_al_convert_bitmap_data( |
| 900 |
d3d_bmp->locked_rect.pBits, system_format, d3d_bmp->locked_rect.Pitch, |
| 901 |
bitmap->locked_region.data, f, bitmap->locked_region.pitch, |
| 902 |
0, 0, 0, 0, w, h); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
return &bitmap->locked_region; |
| 907 |
} |
| 908 |
|
| 909 |
static void d3d_unlock_region(ALLEGRO_BITMAP *bitmap) |
| 910 |
{ |
| 911 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bmp = get_extra(bitmap); |
| 912 |
int system_format = d3d_bmp->system_format; |
| 913 |
|
| 914 |
if (bitmap->locked_region.format != 0 && bitmap->locked_region.format != system_format) { |
| 915 |
if (!(bitmap->lock_flags & ALLEGRO_LOCK_READONLY)) { |
| 916 |
_al_convert_bitmap_data( |
| 917 |
bitmap->locked_region.data, bitmap->locked_region.format, bitmap->locked_region.pitch, |
| 918 |
d3d_bmp->locked_rect.pBits, system_format, d3d_bmp->locked_rect.Pitch, |
| 919 |
0, 0, 0, 0, bitmap->lock_w, bitmap->lock_h); |
| 920 |
} |
| 921 |
al_free(bitmap->locked_region.data); |
| 922 |
} |
| 923 |
|
| 924 |
if (d3d_bmp->is_backbuffer) { |
| 925 |
ALLEGRO_DISPLAY_D3D *d3d_disp = |
| 926 |
(ALLEGRO_DISPLAY_D3D *)_al_get_bitmap_display(bitmap); |
| 927 |
d3d_disp->render_target->UnlockRect(); |
| 928 |
} |
| 929 |
else { |
| 930 |
LPDIRECT3DTEXTURE9 texture; |
| 931 |
int bitmap_format = al_get_bitmap_format(bitmap); |
| 932 |
bool compressed = _al_pixel_format_is_compressed(bitmap_format); |
| 933 |
if (_al_d3d_render_to_texture_supported() || compressed) |
| 934 |
texture = d3d_bmp->system_texture; |
| 935 |
else |
| 936 |
texture = d3d_bmp->video_texture; |
| 937 |
texture->UnlockRect(0); |
| 938 |
if (bitmap->lock_flags & ALLEGRO_LOCK_READONLY) |
| 939 |
return; |
| 940 |
|
| 941 |
if (compressed) { |
| 942 |
int block_width = al_get_pixel_block_width(bitmap_format); |
| 943 |
int block_height = al_get_pixel_block_height(bitmap_format); |
| 944 |
int xc = (bitmap->lock_x / block_width) * block_width; |
| 945 |
int yc = (bitmap->lock_y / block_height) * block_height; |
| 946 |
int wc = |
| 947 |
_al_get_least_multiple(bitmap->lock_x + bitmap->lock_w, block_width) - xc; |
| 948 |
int hc = |
| 949 |
_al_get_least_multiple(bitmap->lock_y + bitmap->lock_h, block_height) - yc; |
| 950 |
if(!convert_compressed( |
| 951 |
d3d_bmp->video_texture, d3d_bmp->system_texture, xc, yc, wc, hc)) { |
| 952 |
ALLEGRO_ERROR("Could not compress.\n"); |
| 953 |
} |
| 954 |
} |
| 955 |
else { |
| 956 |
d3d_do_upload(bitmap, bitmap->lock_x, bitmap->lock_y, |
| 957 |
bitmap->lock_w, bitmap->lock_h, false); |
| 958 |
} |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
static void d3d_update_clipping_rectangle(ALLEGRO_BITMAP *bitmap) |
| 964 |
{ |
| 965 |
_al_d3d_set_bitmap_clip(bitmap); |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
static ALLEGRO_LOCKED_REGION *d3d_lock_compressed_region( |
| 970 |
ALLEGRO_BITMAP *bitmap, int x, int y, int w, int h, |
| 971 |
int flags) |
| 972 |
{ |
| 973 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bmp = get_extra(bitmap); |
| 974 |
int bitmap_format = al_get_bitmap_format(bitmap); |
| 975 |
|
| 976 |
if (d3d_bmp->display->device_lost) |
| 977 |
return NULL; |
| 978 |
|
| 979 |
ASSERT(_al_pixel_format_is_compressed(bitmap_format)); |
| 980 |
|
| 981 |
RECT rect; |
| 982 |
DWORD Flags = flags & ALLEGRO_LOCK_READONLY ? D3DLOCK_READONLY : 0; |
| 983 |
|
| 984 |
rect.left = x; |
| 985 |
rect.right = x + w; |
| 986 |
rect.top = y; |
| 987 |
rect.bottom = y + h; |
| 988 |
|
| 989 |
if (d3d_bmp->video_texture->LockRect(0, &d3d_bmp->locked_rect, &rect, Flags) != D3D_OK) { |
| 990 |
ALLEGRO_ERROR("LockRect failed in d3d_lock_region.\n"); |
| 991 |
return NULL; |
| 992 |
} |
| 993 |
|
| 994 |
bitmap->locked_region.data = d3d_bmp->locked_rect.pBits; |
| 995 |
bitmap->locked_region.format = bitmap_format; |
| 996 |
bitmap->locked_region.pitch = d3d_bmp->locked_rect.Pitch; |
| 997 |
bitmap->locked_region.pixel_size = al_get_pixel_block_size(bitmap_format); |
| 998 |
|
| 999 |
return &bitmap->locked_region; |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
static void d3d_unlock_compressed_region(ALLEGRO_BITMAP *bitmap) |
| 1004 |
{ |
| 1005 |
ALLEGRO_BITMAP_EXTRA_D3D *d3d_bmp = get_extra(bitmap); |
| 1006 |
ASSERT(_al_pixel_format_is_compressed(al_get_bitmap_format(bitmap))); |
| 1007 |
|
| 1008 |
d3d_bmp->video_texture->UnlockRect(0); |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
/* Obtain a reference to this driver. */ |
| 1013 |
ALLEGRO_BITMAP_INTERFACE *_al_bitmap_d3d_driver(void) |
| 1014 |
{ |
| 1015 |
if (vt) |
| 1016 |
return vt; |
| 1017 |
|
| 1018 |
vt = (ALLEGRO_BITMAP_INTERFACE *)al_malloc(sizeof *vt); |
| 1019 |
memset(vt, 0, sizeof *vt); |
| 1020 |
|
| 1021 |
vt->draw_bitmap_region = d3d_draw_bitmap_region; |
| 1022 |
vt->upload_bitmap = d3d_upload_bitmap; |
| 1023 |
vt->update_clipping_rectangle = NULL; |
| 1024 |
vt->destroy_bitmap = _al_d3d_destroy_bitmap; |
| 1025 |
vt->lock_region = d3d_lock_region; |
| 1026 |
vt->unlock_region = d3d_unlock_region; |
| 1027 |
vt->lock_compressed_region = d3d_lock_compressed_region; |
| 1028 |
vt->unlock_compressed_region = d3d_unlock_compressed_region; |
| 1029 |
vt->update_clipping_rectangle = d3d_update_clipping_rectangle; |
| 1030 |
vt->backup_dirty_bitmap = d3d_backup_dirty_bitmap; |
| 1031 |
|
| 1032 |
return vt; |
| 1033 |
} |
| 1034 |
|