23 void Print(
const char* fmt, ...);
26 #define TEXDX9_VERBOSE 0
27 #define DX9_TEXTURE_CACHE_SIZE 256
30 #define RELEASE(x) if (x) { x->Release(); x=NULL; }
55 : video(v), count(0), mru(0)
71 if (cache[i].bitmap_id)
80 Print(
"TexCacheDX9: final used count = %d\n", used);
88 if (!d3ddevice)
return false;
94 hr = d3ddevice->CreateTexture(bmp->
Width(),
103 if (FAILED(hr) || !entry->
texture) {
110 D3DLOCKED_RECT locked_rect;
111 hr = entry->
texture->LockRect(0, &locked_rect, 0, 0);
114 VideoDX9Error(
"LoadTexture - could not lock texture surface", hr);
120 for (
int i = 0; i < bmp->
Height(); i++) {
122 BYTE* dst = (BYTE*) locked_rect.pBits + i * locked_rect.Pitch;
124 CopyMemory(dst, src, bmp->
Width() *
sizeof(
Color));
138 TexCacheDX9::CreateNormalMap(
int index,
float amp)
140 if (d3ddevice && cache[index].texture && !cache[index].normal) {
145 IDirect3DTexture9* normal_map = 0;
148 hr = d3ddevice->CreateTexture(bmp->
Width(),
157 if (FAILED(hr) || !normal_map) {
158 VideoDX9Error(
"CreateNormalMap - could not create texture", hr);
162 D3DXComputeNormalMap(normal_map, entry->
texture ,NULL, 0, D3DX_CHANNEL_RED, amp);
174 D3DLOCKED_RECT locked_rect;
175 hr = normal_map->LockRect(0, &locked_rect, 0, 0);
178 VideoDX9Error(
"CreateNormalMap - could not insert height channel", hr);
183 for (
int i = 0; i < bmp->
Height(); i++) {
185 BYTE* dst = (BYTE*) locked_rect.pBits + i * locked_rect.Pitch;
190 for (
int n = 0; n < bmp->
Width(); n++) {
199 normal_map->UnlockRect(0);
214 if (cache[mru].bitmap_id == bmp->
Handle()) {
218 if (cache[mru].last_modified < bmp->LastModified()) {
220 cache[mru].
normal =
false;
228 if (cache[i].bitmap_id == bmp->
Handle()) {
233 if (cache[mru].last_modified < bmp->LastModified()) {
235 cache[mru].
normal =
false;
240 else if (avail < 0 && cache[i].bitmap_id == 0)
256 Print(
" Tex %3d: id=%2d, size=%3d, type=%d, hTex=%3d, frame=%6d vmf=%8d\n",
261 cache[mru].
normal =
false;
284 if (cache[mru].bitmap_id == bmp->
Handle()) {
288 if (cache[mru].last_modified < bmp->LastModified()) {
290 cache[mru].
normal =
false;
293 if (!cache[mru].normal)
294 CreateNormalMap(mru, amp);
301 if (cache[i].bitmap_id == bmp->
Handle()) {
306 if (cache[i].last_modified < bmp->LastModified()) {
311 if (!cache[i].normal)
312 CreateNormalMap(i, amp);
316 else if (avail < 0 && cache[i].bitmap_id == 0)
332 Print(
" Tex %3d: id=%2d, size=%3d, type=%d, hTex=%3d, frame=%6d vmf=%8d\n",
337 CreateNormalMap(mru, amp);
352 TexCacheDX9::FreeLRU(
int tries)
358 DWORD old = frame_number;
363 if (ul && ul < old && ul != frame_number) {
380 Print(
" FreeLRU() frame=%6d unloaded=%2d vmf=%8d\n", frame_number, unloaded, vidmem);
389 TexCacheDX9::FreeUpCache()
394 if (cache[i].used_last && cache[i].used_last < frame_number) {
402 Print(
" FreeUpCache() frame=%6d unloaded=%2d vmf=%8d\n", frame_number, unloaded, vidmem);