Defer allocation of more dynamic VBs

This commit is contained in:
UnknownShadow200 2023-11-05 09:53:32 +11:00
parent 46a1e4daf8
commit f63018b05a
6 changed files with 60 additions and 46 deletions

View File

@ -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 */

View File

@ -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]);
}
}
@ -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) {

View File

@ -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);
}
static void OnFree(void) { OnContextLost(NULL); }

View File

@ -88,6 +88,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);
Gfx_SetVertexFormat(VERTEX_FORMAT_COLOURED);
@ -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);
}
static void OnFree(void) { OnContextLost(NULL); }

View File

@ -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;

View File

@ -40,7 +40,6 @@ void _glInitContext() {
scissor_rect.height = vid_mode->height;
glClearDepth(1.0f);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
glShadeModel(GL_SMOOTH);