diff --git a/source/internal.h b/source/internal.h index abdaf26..3ccab72 100644 --- a/source/internal.h +++ b/source/internal.h @@ -124,6 +124,18 @@ static inline bool C3Di_TexIs2D(C3D_Tex* tex) return !typeIsCube(C3D_TexGetType(tex)); } +static inline bool addrIsVRAM(const void* addr) +{ + u32 vaddr = (u32)addr; + return vaddr >= OS_VRAM_VADDR && vaddr < OS_VRAM_VADDR + OS_VRAM_SIZE; +} + +static inline vramAllocPos addrGetVRAMBank(const void* addr) +{ + u32 vaddr = (u32)addr; + return vaddr < OS_VRAM_VADDR + OS_VRAM_SIZE/2 ? VRAM_ALLOC_A : VRAM_ALLOC_B; +} + void C3Di_UpdateContext(void); void C3Di_AttrInfoBind(C3D_AttrInfo* info); void C3Di_BufInfoBind(C3D_BufInfo* info); diff --git a/source/renderqueue.c b/source/renderqueue.c index 97a001c..209d69d 100644 --- a/source/renderqueue.c +++ b/source/renderqueue.c @@ -284,7 +284,10 @@ C3D_RenderTarget* C3D_RenderTargetCreate(int width, int height, GPU_COLORBUF col if (C3D_DEPTHTYPE_OK(depthFmt)) { depthFmtReal = C3D_DEPTHTYPE_VAL(depthFmt); - depthBuf = vramAlloc(C3D_CalcDepthBufSize(width,height,depthFmtReal)); + size_t depthSize = C3D_CalcDepthBufSize(width,height,depthFmtReal); + vramAllocPos vramBank = addrGetVRAMBank(colorBuf); + depthBuf = vramAllocAt(depthSize, vramBank ^ VRAM_ALLOC_ANY); // Attempt opposite bank first... + if (!depthBuf) depthBuf = vramAllocAt(depthSize, vramBank); // ... if that fails, attempt same bank if (!depthBuf) goto _fail1; } @@ -313,6 +316,7 @@ C3D_RenderTarget* C3D_RenderTargetCreate(int width, int height, GPU_COLORBUF col C3D_RenderTarget* C3D_RenderTargetCreateFromTex(C3D_Tex* tex, GPU_TEXFACE face, int level, C3D_DEPTHTYPE depthFmt) { + if (!addrIsVRAM(tex->data)) return NULL; // Render targets must be in VRAM C3D_RenderTarget* target = C3Di_RenderTargetNew(); if (!target) return NULL; @@ -322,7 +326,10 @@ C3D_RenderTarget* C3D_RenderTargetCreateFromTex(C3D_Tex* tex, GPU_TEXFACE face, if (C3D_DEPTHTYPE_OK(depthFmt)) { GPU_DEPTHBUF depthFmtReal = C3D_DEPTHTYPE_VAL(depthFmt); - void* depthBuf = vramAlloc(C3D_CalcDepthBufSize(fb->width,fb->height,depthFmtReal)); + size_t depthSize = C3D_CalcDepthBufSize(fb->width,fb->height,depthFmtReal); + vramAllocPos vramBank = addrGetVRAMBank(tex->data); + void* depthBuf = vramAllocAt(depthSize, vramBank ^ VRAM_ALLOC_ANY); // Attempt opposite bank first... + if (!depthBuf) depthBuf = vramAllocAt(depthSize, vramBank); // ... if that fails, attempt same bank if (!depthBuf) { free(target); diff --git a/source/texture.c b/source/texture.c index 5d7fb79..d1340ae 100644 --- a/source/texture.c +++ b/source/texture.c @@ -30,12 +30,6 @@ static inline size_t fmtSize(GPU_TEXCOLOR fmt) } } -static inline bool addrIsVRAM(const void* addr) -{ - u32 vaddr = (u32)addr; - return vaddr >= 0x1F000000 && vaddr < 0x1F600000; -} - static inline bool checkTexSize(u32 size) { if (size < 8 || size > 1024)