Unify texture creation checking, and defer dynamic vertex buffer allocation for rain/snow until actually needed

This commit is contained in:
UnknownShadow200 2023-11-03 08:06:16 +11:00
parent 4bb4f89781
commit 3fa4ef6d44
15 changed files with 38 additions and 48 deletions

View File

@ -451,7 +451,11 @@ void EnvRenderer_RenderWeather(double deltaTime) {
weather = Env.Weather;
if (weather == WEATHER_SUNNY) return;
if (!Weather_Heightmap) InitWeatherHeightmap();
if (!Weather_Heightmap)
InitWeatherHeightmap();
if (!weather_vb)
weather_vb = Gfx_CreateDynamicVb(VERTEX_FORMAT_TEXTURED, WEATHER_VERTS_COUNT);
IVec3_Floor(&pos, &Camera.CurrentPos);
moved = pos.X != lastPos.X || pos.Y != lastPos.Y || pos.Z != lastPos.Z;
@ -834,9 +838,8 @@ static void UpdateAll(void) {
EnvRenderer_UpdateFog();
Gfx_DeleteDynamicVb(&weather_vb);
/* TODO: Unnecessary to delete the weather VB? */
if (Gfx.LostContext) return;
/* TODO: Don't allocate unless used? */
Gfx_RecreateDynamicVb(&weather_vb, VERTEX_FORMAT_TEXTURED, WEATHER_VERTS_COUNT);
/* TODO: Don't need to do this on every new map */
UpdateBorderTextures();
}

View File

@ -228,7 +228,7 @@ static void ToMortonTexture(C3D_Tex* tex, int originX, int originY,
}
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
C3D_Tex* tex = Mem_Alloc(1, sizeof(C3D_Tex), "GPU texture desc");
bool success = C3D_TexInit(tex, bmp->width, bmp->height, GPU_RGBA8);
//if (!success) Logger_Abort("Failed to create 3DS texture");

View File

@ -2,7 +2,6 @@
#ifdef CC_BUILD_D3D11
#include "_GraphicsBase.h"
#include "Errors.h"
#include "Logger.h"
#include "Window.h"
#include "_D3D11Shaders.h"
@ -202,16 +201,11 @@ static void D3D11_DoMipmaps(ID3D11Resource* texture, int x, int y, struct Bitmap
if (prev != bmp->scan0) Mem_Free(prev);
}
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
ID3D11Texture2D* tex = NULL;
ID3D11ShaderResourceView* view = NULL;
HRESULT hr;
if (!Math_IsPowOf2(bmp->width) || !Math_IsPowOf2(bmp->height)) {
Logger_Abort("Textures must have power of two dimensions");
}
if (Gfx.LostContext) return 0;
D3D11_TEXTURE2D_DESC desc = { 0 };
desc.Width = bmp->width;
desc.Height = bmp->height;

View File

@ -2,7 +2,6 @@
#ifdef CC_BUILD_D3D9
#include "_GraphicsBase.h"
#include "Errors.h"
#include "Logger.h"
#include "Window.h"
/* Avoid pointless includes */
@ -19,7 +18,7 @@
/* https://docs.microsoft.com/en-us/windows/win32/dxtecharts/the-direct3d-transformation-pipeline */
/* https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dfvf-texcoordsizen */
static DWORD d3d9_formatMappings[2] = { D3DFVF_XYZ | D3DFVF_DIFFUSE, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 };
static DWORD d3d9_formatMappings[] = { D3DFVF_XYZ | D3DFVF_DIFFUSE, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 };
/* Current format and size of vertices */
static int gfx_stride, gfx_format = -1;
@ -332,7 +331,7 @@ static IDirect3DTexture9* DoCreateTexture(struct Bitmap* bmp, int levels, int po
return tex;
}
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
IDirect3DTexture9* tex;
IDirect3DTexture9* sys;
cc_result res;
@ -340,11 +339,6 @@ GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipm
int mipmapsLevels = CalcMipmapsLevels(bmp->width, bmp->height);
int levels = 1 + (mipmaps ? mipmapsLevels : 0);
if (!Math_IsPowOf2(bmp->width) || !Math_IsPowOf2(bmp->height)) {
Logger_Abort("Textures must have power of two dimensions");
}
if (Gfx.LostContext) return 0;
if (flags & TEXTURE_FLAG_MANAGED) {
while ((res = IDirect3DDevice9_CreateTexture(device, bmp->width, bmp->height, levels,
0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &tex, NULL)))

View File

@ -319,12 +319,9 @@ static void ConvertTexture(cc_uint16* dst, struct Bitmap* bmp) {
}
}
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
GLuint texId = gldcGenTexture();
gldcBindTexture(texId);
if (!Math_IsPowOf2(bmp->width) || !Math_IsPowOf2(bmp->height)) {
Logger_Abort("Textures must have power of two dimensions");
}
gldcAllocTexture(bmp->width, bmp->height, GL_RGBA,
GL_UNSIGNED_SHORT_4_4_4_4_REV_TWID_KOS);
@ -335,6 +332,7 @@ GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipm
ConvertTexture(pixels, bmp);
return texId;
}
// TODO: struct GPUTexture ??
static void ConvertSubTexture(cc_uint16* dst, int texWidth, int texHeight,
int originX, int originY,

View File

@ -136,7 +136,7 @@ static void ReorderPixels(CCTexture* tex, struct Bitmap* bmp,
}
}
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
if (bmp->width < 4 || bmp->height < 4) {
Platform_LogConst("ERROR: Tried to create texture smaller than 4x4");
return 0;

View File

@ -2,7 +2,6 @@
#if defined CC_BUILD_GL && !defined CC_BUILD_GLMODERN
#include "_GraphicsBase.h"
#include "Errors.h"
#include "Logger.h"
#include "Window.h"
/* The OpenGL backend is a bit of a mess, since it's really 2 backends in one:
* - OpenGL 1.1 (completely lacking GPU, fallbacks to say Windows built-in software rasteriser)

View File

@ -2,7 +2,6 @@
#if defined CC_BUILD_GL && defined CC_BUILD_GLMODERN
#include "_GraphicsBase.h"
#include "Errors.h"
#include "Logger.h"
#include "Window.h"
/* OpenGL 2.0 backend (alternative modern-ish backend) */

View File

@ -568,7 +568,7 @@ typedef struct CCTexture_ {
cc_uint32 pixels[];
} CCTexture;
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
int size = bmp->width * bmp->height * 4;
CCTexture* tex = (CCTexture*)rsxMemalign(128, 128 + size);

View File

@ -114,7 +114,7 @@ typedef struct CCTexture_ {
cc_uint32 pixels[];
} CCTexture;
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
int size = bmp->width * bmp->height * 4;
CCTexture* tex = (CCTexture*)memalign(16, 16 + size);

View File

@ -661,7 +661,7 @@ static void GPUTextures_DeleteUnreferenced(void) {
/*########################################################################################################################*
*---------------------------------------------------------Textures--------------------------------------------------------*
*#########################################################################################################################*/
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
int size = bmp->width * bmp->height * 4;
struct GPUTexture* tex = GPUTexture_Alloc(size);
Mem_Copy(tex->data, bmp->scan0, size);

View File

@ -198,7 +198,7 @@ static void ConvertTexture(cc_uint32* dst, struct Bitmap* bmp) {
}
}
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
int size = 16 + bmp->width * bmp->height * 4;
CCTexture* tex = MmAllocateContiguousMemoryEx(size, 0, MAX_RAM_ADDR, 0, 0x404);

View File

@ -108,7 +108,6 @@ static int selections_count;
static struct SelectionBox selections_list[SELECTIONS_MAX];
static cc_uint8 selections_ids[SELECTIONS_MAX];
static GfxResourceID selections_VB, selections_LineVB;
static cc_bool selections_used;
void Selections_Add(cc_uint8 id, const IVec3* p1, const IVec3* p2, PackedCol color) {
struct SelectionBox sel;
@ -142,10 +141,9 @@ static void Selections_ContextLost(void* obj) {
Gfx_DeleteDynamicVb(&selections_LineVB);
}
static void Selections_ContextRecreated(void* obj) {
if (!selections_used) return;
Gfx_RecreateDynamicVb(&selections_VB, VERTEX_FORMAT_COLOURED, SELECTIONS_MAX_VERTICES);
Gfx_RecreateDynamicVb(&selections_LineVB, VERTEX_FORMAT_COLOURED, SELECTIONS_MAX_VERTICES);
static void AllocateVertexBuffers(void) {
selections_VB = Gfx_CreateDynamicVb(VERTEX_FORMAT_COLOURED, SELECTIONS_MAX_VERTICES);
selections_LineVB = Gfx_CreateDynamicVb(VERTEX_FORMAT_COLOURED, SELECTIONS_MAX_VERTICES);
}
static void Selections_QuickSort(int left, int right) {
@ -181,10 +179,9 @@ void Selections_Render(void) {
}
Selections_QuickSort(0, selections_count - 1);
if (!selections_VB) { /* lazy init as most servers don't use this */
selections_used = true;
Selections_ContextRecreated(NULL);
}
/* lazy init as most servers don't use this */
if (!selections_VB) AllocateVertexBuffers();
count = selections_count * SELECTIONS_VERTICES;
Gfx_SetVertexFormat(VERTEX_FORMAT_COLOURED);
@ -216,7 +213,6 @@ void Selections_Render(void) {
*#########################################################################################################################*/
static void OnInit(void) {
Event_Register_(&GfxEvents.ContextLost, NULL, Selections_ContextLost);
Event_Register_(&GfxEvents.ContextRecreated, NULL, Selections_ContextRecreated);
}
static void OnReset(void) { selections_count = 0; }

View File

@ -102,17 +102,12 @@ static void Gfx_DoMipmaps(int x, int y, struct Bitmap* bmp, int rowWidth, cc_boo
if (prev != bmp->scan0) Mem_Free(prev);
}
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
GLuint texId;
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
if (!Math_IsPowOf2(bmp->width) || !Math_IsPowOf2(bmp->height)) {
Logger_Abort("Textures must have power of two dimensions");
}
if (Gfx.LostContext) return 0;
if (mipmaps) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
if (customMipmapsLevels) {

View File

@ -9,13 +9,14 @@
#include "Options.h"
#include "Bitmap.h"
#include "Chat.h"
#include "Logger.h"
struct _GfxData Gfx;
GfxResourceID Gfx_defaultIb;
GfxResourceID Gfx_quadVb, Gfx_texVb;
const cc_string Gfx_LowPerfMessage = String_FromConst("&eRunning in reduced performance mode (game minimised or hidden)");
static const int strideSizes[2] = { SIZEOF_VERTEX_COLOURED, SIZEOF_VERTEX_TEXTURED };
static const int strideSizes[] = { SIZEOF_VERTEX_COLOURED, SIZEOF_VERTEX_TEXTURED };
/* Whether mipmaps must be created for all dimensions down to 1x1 or not */
static cc_bool customMipmapsLevels;
@ -336,6 +337,17 @@ static CC_NOINLINE int CalcMipmapsLevels(int width, int height) {
}
}
static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps);
GfxResourceID Gfx_CreateTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) {
if (!Math_IsPowOf2(bmp->width) || !Math_IsPowOf2(bmp->height)) {
Logger_Abort("Textures must have power of two dimensions");
}
if (Gfx.LostContext) return 0;
return Gfx_AllocTexture(bmp, flags, mipmaps);
}
void Texture_Render(const struct Texture* tex) {
Gfx_BindTexture(tex->ID);
Gfx_Draw2DTexture(tex, PACKEDCOL_WHITE);