From f63018b05a293fbdb5c73ed23b0b6b3d3935bbf7 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 5 Nov 2023 09:53:32 +1100 Subject: [PATCH] Defer allocation of more dynamic VBs --- src/Graphics.h | 5 +++-- src/Graphics_D3D11.c | 43 ++++++++++++++++++++++++------------ src/Particle.c | 37 +++++++++++++++---------------- src/PickedPosRenderer.c | 11 ++++----- src/Platform_Dreamcast.c | 9 +++++--- third_party/gldc/src/state.c | 1 - 6 files changed, 60 insertions(+), 46 deletions(-) diff --git a/src/Graphics.h b/src/Graphics.h index 8bd0116c0..9751785f1 100644 --- a/src/Graphics.h +++ b/src/Graphics.h @@ -176,10 +176,11 @@ CC_API void* Gfx_LockDynamicVb(GfxResourceID vb, VertexFormat fmt, int count); /* Binds then submits the changed contents of a dynamic vertex buffer */ CC_API void Gfx_UnlockDynamicVb(GfxResourceID vb); -/* Sets the format of the rendered vertices */ -CC_API void Gfx_SetVertexFormat(VertexFormat fmt); /* Updates the data of a dynamic vertex buffer */ CC_API void Gfx_SetDynamicVbData(GfxResourceID vb, void* vertices, int vCount); + +/* Sets the format of the rendered vertices */ +CC_API void Gfx_SetVertexFormat(VertexFormat fmt); /* Renders vertices from the currently bound vertex buffer as lines */ CC_API void Gfx_DrawVb_Lines(int verticesCount); /* Renders vertices from the currently bound vertex and index buffer as triangles */ diff --git a/src/Graphics_D3D11.c b/src/Graphics_D3D11.c index f26fc3115..6c6a5ef78 100644 --- a/src/Graphics_D3D11.c +++ b/src/Graphics_D3D11.c @@ -173,7 +173,8 @@ static void D3D11_DoMipmaps(ID3D11Resource* texture, int x, int y, struct Bitmap int lvls = CalcMipmapsLevels(bmp->width, bmp->height); int lvl, width = bmp->width, height = bmp->height; - for (lvl = 1; lvl <= lvls; lvl++) { + for (lvl = 1; lvl <= lvls; lvl++) + { x /= 2; y /= 2; if (width > 1) width /= 2; if (height > 1) height /= 2; @@ -545,7 +546,8 @@ static const struct ShaderDesc vs_descs[] = { }; static void VS_CreateShaders(void) { - for (int i = 0; i < Array_Elems(vs_shaders); i++) { + for (int i = 0; i < Array_Elems(vs_shaders); i++) + { HRESULT hr = ID3D11Device_CreateVertexShader(device, vs_descs[i].data, vs_descs[i].len, NULL, &vs_shaders[i]); if (hr) Logger_Abort2(hr, "Failed to compile vertex shader"); } @@ -585,7 +587,8 @@ static void VS_UpdateShader(void) { } static void VS_FreeShaders(void) { - for (int i = 0; i < Array_Elems(vs_shaders); i++) { + for (int i = 0; i < Array_Elems(vs_shaders); i++) + { ID3D11VertexShader_Release(vs_shaders[i]); } } @@ -672,7 +675,8 @@ static void RS_UpdateRasterState(void) { } static void RS_FreeRasterStates(void) { - for (int i = 0; i < Array_Elems(rs_states); i++) { + for (int i = 0; i < Array_Elems(rs_states); i++) + { ID3D11RasterizerState_Release(rs_states[i]); } } @@ -725,7 +729,8 @@ static const struct ShaderDesc ps_descs[] = { }; static void PS_CreateShaders(void) { - for (int i = 0; i < Array_Elems(ps_shaders); i++) { + for (int i = 0; i < Array_Elems(ps_shaders); i++) + { HRESULT hr = ID3D11Device_CreatePixelShader(device, ps_descs[i].data, ps_descs[i].len, NULL, &ps_shaders[i]); if (hr) Logger_Abort2(hr, "Failed to compile pixel shader"); } @@ -749,7 +754,8 @@ static void PS_UpdateShader(void) { } static void PS_FreeShaders(void) { - for (int i = 0; i < Array_Elems(ps_shaders); i++) { + for (int i = 0; i < Array_Elems(ps_shaders); i++) + { ID3D11PixelShader_Release(ps_shaders[i]); } } @@ -780,7 +786,8 @@ static void PS_UpdateSampler(void) { } static void PS_FreeSamplers(void) { - for (int i = 0; i < Array_Elems(ps_samplers); i++) { + for (int i = 0; i < Array_Elems(ps_samplers); i++) + { ID3D11SamplerState_Release(ps_samplers[i]); } } @@ -904,6 +911,10 @@ static void OM_Clear(void) { ID3D11DeviceContext_ClearDepthStencilView(context, depthbufferView, D3D11_CLEAR_DEPTH, 0.0f, 0); } +static void OM_UpdateTarget(void) { + ID3D11DeviceContext_OMSetRenderTargets(context, 1, &backbuffer, depthbufferView); +} + static void OM_InitTargets(void) { // https://docs.microsoft.com/en-us/windows/win32/direct3d11/d3d10-graphics-programming-guide-depth-stencil D3D11_TEXTURE2D_DESC desc; @@ -926,8 +937,8 @@ static void OM_InitTargets(void) { hr = ID3D11Device_CreateDepthStencilView(device, depthbuffer, NULL, &depthbufferView); if (hr) Logger_Abort2(hr, "Failed to create depthbuffer view"); - ID3D11DeviceContext_OMSetRenderTargets(context, 1, &backbuffer, depthbufferView); ID3D11Texture2D_Release(pBackBuffer); + OM_UpdateTarget(); } static void OM_CreateDepthStates(void) { @@ -935,7 +946,8 @@ static void OM_CreateDepthStates(void) { HRESULT hr; desc.DepthFunc = D3D11_COMPARISON_GREATER_EQUAL; - for (int i = 0; i < Array_Elems(om_depthStates); i++) { + for (int i = 0; i < Array_Elems(om_depthStates); i++) + { desc.DepthEnable = (i & 1) != 0; desc.DepthWriteMask = (i & 2) != 0; @@ -950,7 +962,8 @@ static void OM_UpdateDepthState(void) { } static void OM_FreeDepthStates(void) { - for (int i = 0; i < Array_Elems(om_depthStates); i++) { + for (int i = 0; i < Array_Elems(om_depthStates); i++) + { ID3D11DepthStencilState_Release(om_depthStates[i]); } } @@ -966,7 +979,8 @@ static void OM_CreateBlendStates(void) { desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; - for (int i = 0; i < Array_Elems(om_blendStates); i++) { + for (int i = 0; i < Array_Elems(om_blendStates); i++) + { desc.RenderTarget[0].RenderTargetWriteMask = (i & 1) ? D3D11_COLOR_WRITE_ENABLE_ALL : 0; desc.RenderTarget[0].BlendEnable = (i & 2) != 0; @@ -981,7 +995,8 @@ static void OM_UpdateBlendState(void) { } static void OM_FreeBlendStates(void) { - for (int i = 0; i < Array_Elems(om_blendStates); i++) { + for (int i = 0; i < Array_Elems(om_blendStates); i++) + { ID3D11BlendState_Release(om_blendStates[i]); } } @@ -1063,7 +1078,7 @@ cc_result Gfx_TakeScreenshot(struct Stream* output) { D3D11_RENDER_TARGET_VIEW_DESC backbuffer_desc; D3D11_MAPPED_SUBRESOURCE buffer; ID3D11RenderTargetView_GetResource(backbuffer, &backbuffer_res); - ID3D11RenderTargetView_GetDesc(backbuffer, &backbuffer_desc); + ID3D11RenderTargetView_GetDesc(backbuffer, &backbuffer_desc); D3D11_TEXTURE2D_DESC desc = { 0 }; desc.Width = WindowInfo.Width; @@ -1097,7 +1112,7 @@ void Gfx_SetFpsLimit(cc_bool vsync, float minFrameMs) { gfx_minFrameMs = minFrameMs; gfx_vsync = vsync; } -void Gfx_BeginFrame(void) { } +void Gfx_BeginFrame(void) { OM_UpdateTarget(); } void Gfx_Clear(void) { OM_Clear(); } void Gfx_EndFrame(void) { diff --git a/src/Particle.c b/src/Particle.c index a6f92c5da..9d2fcd717 100644 --- a/src/Particle.c +++ b/src/Particle.c @@ -14,7 +14,7 @@ /*########################################################################################################################* *------------------------------------------------------Particle base------------------------------------------------------* *#########################################################################################################################*/ -static GfxResourceID Particles_TexId, Particles_VB; +static GfxResourceID particles_TexId, particles_VB; #define PARTICLES_MAX 600 static RNGState rnd; static cc_bool hitTerrain; @@ -159,15 +159,15 @@ static void Rain_Render(float t) { int i; if (!rain_count) return; - data = (struct VertexTextured*)Gfx_LockDynamicVb(Particles_VB, + data = (struct VertexTextured*)Gfx_LockDynamicVb(particles_VB, VERTEX_FORMAT_TEXTURED, rain_count * 4); for (i = 0; i < rain_count; i++) { RainParticle_Render(&rain_Particles[i], t, data); data += 4; } - Gfx_BindTexture(Particles_TexId); - Gfx_UnlockDynamicVb(Particles_VB); + Gfx_BindTexture(particles_TexId); + Gfx_UnlockDynamicVb(particles_VB); Gfx_DrawVb_IndexedTris(rain_count * 4); } @@ -253,7 +253,7 @@ static void Terrain_Render(float t) { int i, index; if (!terrain_count) return; - data = (struct VertexTextured*)Gfx_LockDynamicVb(Particles_VB, + data = (struct VertexTextured*)Gfx_LockDynamicVb(particles_VB, VERTEX_FORMAT_TEXTURED, terrain_count * 4); Terrain_Update1DCounts(); for (i = 0; i < terrain_count; i++) { @@ -264,7 +264,7 @@ static void Terrain_Render(float t) { terrain_1DIndices[index] += 4; } - Gfx_UnlockDynamicVb(Particles_VB); + Gfx_UnlockDynamicVb(particles_VB); for (i = 0; i < Atlas1D.Count; i++) { int partCount = terrain_1DCount[i]; if (!partCount) continue; @@ -360,15 +360,15 @@ static void Custom_Render(float t) { int i; if (!custom_count) return; - data = (struct VertexTextured*)Gfx_LockDynamicVb(Particles_VB, + data = (struct VertexTextured*)Gfx_LockDynamicVb(particles_VB, VERTEX_FORMAT_TEXTURED, custom_count * 4); for (i = 0; i < custom_count; i++) { CustomParticle_Render(&custom_particles[i], t, data); data += 4; } - Gfx_BindTexture(Particles_TexId); - Gfx_UnlockDynamicVb(Particles_VB); + Gfx_BindTexture(particles_TexId); + Gfx_UnlockDynamicVb(particles_VB); Gfx_DrawVb_IndexedTris(custom_count * 4); } @@ -394,7 +394,10 @@ static void Custom_Tick(double delta) { *#########################################################################################################################*/ void Particles_Render(float t) { if (!terrain_count && !rain_count && !custom_count) return; + if (Gfx.LostContext) return; + if (!particles_VB) + particles_VB = Gfx_CreateDynamicVb(VERTEX_FORMAT_TEXTURED, PARTICLES_MAX * 4); Gfx_SetAlphaTest(true); @@ -571,20 +574,18 @@ void Particles_CustomEffect(int effectID, float x, float y, float z, float origi *---------------------------------------------------Particles component---------------------------------------------------* *#########################################################################################################################*/ static void ParticlesPngProcess(struct Stream* stream, const cc_string* name) { - Game_UpdateTexture(&Particles_TexId, stream, name, NULL); + Game_UpdateTexture(&particles_TexId, stream, name, NULL); } static struct TextureEntry particles_entry = { "particles.png", ParticlesPngProcess }; static void OnContextLost(void* obj) { - Gfx_DeleteDynamicVb(&Particles_VB); + Gfx_DeleteDynamicVb(&particles_VB); if (Gfx.ManagedTextures) return; - Gfx_DeleteTexture(&Particles_TexId); -} -static void OnContextRecreated(void* obj) { - Gfx_RecreateDynamicVb(&Particles_VB, VERTEX_FORMAT_TEXTURED, PARTICLES_MAX * 4); + Gfx_DeleteTexture(&particles_TexId); } + static void OnBreakBlockEffect_Handler(void* obj, IVec3 coords, BlockID old, BlockID now) { Particles_BreakBlockEffect(coords, old, now); } @@ -592,12 +593,10 @@ static void OnBreakBlockEffect_Handler(void* obj, IVec3 coords, BlockID old, Blo static void OnInit(void) { ScheduledTask_Add(GAME_DEF_TICKS, Particles_Tick); Random_SeedFromCurrentTime(&rnd); - OnContextRecreated(NULL); TextureEntry_Register(&particles_entry); - Event_Register_(&UserEvents.BlockChanged, NULL, OnBreakBlockEffect_Handler); - Event_Register_(&GfxEvents.ContextLost, NULL, OnContextLost); - Event_Register_(&GfxEvents.ContextRecreated, NULL, OnContextRecreated); + Event_Register_(&UserEvents.BlockChanged, NULL, OnBreakBlockEffect_Handler); + Event_Register_(&GfxEvents.ContextLost, NULL, OnContextLost); } static void OnFree(void) { OnContextLost(NULL); } diff --git a/src/PickedPosRenderer.c b/src/PickedPosRenderer.c index bb5662f7b..3919dc8b6 100644 --- a/src/PickedPosRenderer.c +++ b/src/PickedPosRenderer.c @@ -87,6 +87,9 @@ static void BuildMesh(struct RayTracer* selected) { void PickedPosRenderer_Render(struct RayTracer* selected, cc_bool dirty) { if (Gfx.LostContext) return; + + if (!pickedPos_vb) + pickedPos_vb = Gfx_CreateDynamicVb(VERTEX_FORMAT_COLOURED, PICKEDPOS_NUM_VERTICES); Gfx_SetAlphaBlending(true); Gfx_SetDepthWrite(false); @@ -108,14 +111,8 @@ static void OnContextLost(void* obj) { Gfx_DeleteDynamicVb(&pickedPos_vb); } -static void OnContextRecreated(void* obj) { - Gfx_RecreateDynamicVb(&pickedPos_vb, VERTEX_FORMAT_COLOURED, PICKEDPOS_NUM_VERTICES); -} - static void OnInit(void) { - OnContextRecreated(NULL); - Event_Register_(&GfxEvents.ContextLost, NULL, OnContextLost); - Event_Register_(&GfxEvents.ContextRecreated, NULL, OnContextRecreated); + Event_Register_(&GfxEvents.ContextLost, NULL, OnContextLost); } static void OnFree(void) { OnContextLost(NULL); } diff --git a/src/Platform_Dreamcast.c b/src/Platform_Dreamcast.c index 0b96ac44d..088c0f0d2 100644 --- a/src/Platform_Dreamcast.c +++ b/src/Platform_Dreamcast.c @@ -55,10 +55,13 @@ TimeMS DateTime_CurrentUTC_MS(void) { } void DateTime_CurrentLocal(struct DateTime* t) { - struct timeval cur; + uint32 secs, ms; + time_t total_secs; struct tm loc_time; - gettimeofday(&cur, NULL); - localtime_r(&cur.tv_sec, &loc_time); + + timer_ms_gettime(&secs, &ms); + total_secs = rtc_boot_time() + secs; + localtime_r(&total_secs, &loc_time); t->year = loc_time.tm_year + 1900; t->month = loc_time.tm_mon + 1; diff --git a/third_party/gldc/src/state.c b/third_party/gldc/src/state.c index fdf6812b0..b3df4d942 100644 --- a/third_party/gldc/src/state.c +++ b/third_party/gldc/src/state.c @@ -40,7 +40,6 @@ void _glInitContext() { scissor_rect.height = vid_mode->height; glClearDepth(1.0f); - glDepthFunc(GL_LESS); glDepthMask(GL_TRUE); glShadeModel(GL_SMOOTH);