diff --git a/src/EnvRenderer.c b/src/EnvRenderer.c index 9d46dbadc..5fbad21af 100644 --- a/src/EnvRenderer.c +++ b/src/EnvRenderer.c @@ -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(); } diff --git a/src/Graphics_3DS.c b/src/Graphics_3DS.c index 6191e3d61..94610292c 100644 --- a/src/Graphics_3DS.c +++ b/src/Graphics_3DS.c @@ -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"); diff --git a/src/Graphics_D3D11.c b/src/Graphics_D3D11.c index 1684acb01..b52fbf9fa 100644 --- a/src/Graphics_D3D11.c +++ b/src/Graphics_D3D11.c @@ -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; diff --git a/src/Graphics_D3D9.c b/src/Graphics_D3D9.c index a4f3ab2a0..bb2c5d1a1 100644 --- a/src/Graphics_D3D9.c +++ b/src/Graphics_D3D9.c @@ -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,18 +331,13 @@ 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; 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, diff --git a/src/Graphics_Dreamcast.c b/src/Graphics_Dreamcast.c index ce13e58d0..38b088dd8 100644 --- a/src/Graphics_Dreamcast.c +++ b/src/Graphics_Dreamcast.c @@ -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, diff --git a/src/Graphics_GCWii.c b/src/Graphics_GCWii.c index ca9b9871d..e5522a465 100644 --- a/src/Graphics_GCWii.c +++ b/src/Graphics_GCWii.c @@ -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; diff --git a/src/Graphics_GL1.c b/src/Graphics_GL1.c index 94c7c8ded..a7dce3e2f 100644 --- a/src/Graphics_GL1.c +++ b/src/Graphics_GL1.c @@ -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) diff --git a/src/Graphics_GL2.c b/src/Graphics_GL2.c index 7bf785b7b..1d3d1eeef 100644 --- a/src/Graphics_GL2.c +++ b/src/Graphics_GL2.c @@ -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) */ diff --git a/src/Graphics_PS3.c b/src/Graphics_PS3.c index ca6cfd688..810ebbe77 100644 --- a/src/Graphics_PS3.c +++ b/src/Graphics_PS3.c @@ -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); diff --git a/src/Graphics_PSP.c b/src/Graphics_PSP.c index f3282ae6c..fd5bfc68c 100644 --- a/src/Graphics_PSP.c +++ b/src/Graphics_PSP.c @@ -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); diff --git a/src/Graphics_PSVita.c b/src/Graphics_PSVita.c index a1aaaf2c3..909389c11 100644 --- a/src/Graphics_PSVita.c +++ b/src/Graphics_PSVita.c @@ -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); diff --git a/src/Graphics_Xbox.c b/src/Graphics_Xbox.c index bbf63ff1b..d2810c987 100644 --- a/src/Graphics_Xbox.c +++ b/src/Graphics_Xbox.c @@ -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); diff --git a/src/SelectionBox.c b/src/SelectionBox.c index 8d855d181..3a5b42709 100644 --- a/src/SelectionBox.c +++ b/src/SelectionBox.c @@ -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); @@ -215,8 +212,7 @@ void Selections_Render(void) { *--------------------------------------------------Selections component---------------------------------------------------* *#########################################################################################################################*/ static void OnInit(void) { - Event_Register_(&GfxEvents.ContextLost, NULL, Selections_ContextLost); - Event_Register_(&GfxEvents.ContextRecreated, NULL, Selections_ContextRecreated); + Event_Register_(&GfxEvents.ContextLost, NULL, Selections_ContextLost); } static void OnReset(void) { selections_count = 0; } diff --git a/src/_GLShared.h b/src/_GLShared.h index fda94d846..9394f82d2 100644 --- a/src/_GLShared.h +++ b/src/_GLShared.h @@ -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) { diff --git a/src/_GraphicsBase.h b/src/_GraphicsBase.h index 8561a34ff..e659c8b4d 100644 --- a/src/_GraphicsBase.h +++ b/src/_GraphicsBase.h @@ -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);