Skip to content
This repository was archived by the owner on Sep 24, 2022. It is now read-only.

Commit dab010b

Browse files
committed
chore: removed caching
1 parent 160df21 commit dab010b

12 files changed

Lines changed: 480 additions & 578 deletions

Sandbox/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ target_sources(mvimporter_sandbox
3030
"src/renderer/mvBuffers.cpp"
3131
"src/renderer/mvRenderer.cpp"
3232
"src/renderer/mvCamera.cpp"
33-
"src/renderer/mvVertexLayout.cpp"
3433
"src/renderer/mvMesh.cpp"
3534
"src/renderer/mvTextures.cpp"
3635
"src/renderer/mvPipeline.cpp"
3736
"src/renderer/mvMaterials.cpp"
3837
"src/renderer/mvLights.cpp"
3938
"src/renderer/mvEnvironment.cpp"
40-
"src/renderer/mvShader.cpp"
4139

4240
# imgui
4341
"vendor/imgui/misc/cpp/imgui_stdlib.cpp"

Sandbox/main.cpp

Lines changed: 27 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,12 @@
1515
#include "mvAssetManager.h"
1616
#include "mvViewport.h"
1717

18-
#define MODEL_CACHE_SIZE 5
19-
#define ENV_CACHE_SIZE 3
20-
2118
mvViewport* window = nullptr;
2219
ImVec2 oldContentRegion = ImVec2(500, 500);
2320

2421
// scenes & models
25-
i32 cacheIndex = -1; // current model from list
26-
i32 modelIndex = 27; // current model from list
27-
i32 cacheRingIndex = 0; // current cache index
28-
i32 cachedModelIndex[MODEL_CACHE_SIZE];
29-
mvModel cachedModel[MODEL_CACHE_SIZE];
30-
31-
// environments
32-
i32 activeEnv = 5;
33-
i32 envMapIndex = 5;
34-
i32 envCacheIndex = 0;
35-
mvEnvironment cachedEnvironments[ENV_CACHE_SIZE];
36-
i32 cachedEnvironmentsID[ENV_CACHE_SIZE];
22+
i32 modelIndex = 27; // current model from list
23+
i32 envMapIndex = 5;
3724

3825
// misc
3926
f32 currentTime = 0.0f;
@@ -56,15 +43,6 @@ bool changeScene = true;
5643

5744
int main()
5845
{
59-
for (int i = 0; i < MODEL_CACHE_SIZE; i++)
60-
{
61-
cachedModelIndex[i] = -1;
62-
cachedModel[i] = {};
63-
}
64-
65-
for (int i = 0; i < ENV_CACHE_SIZE; i++)
66-
cachedEnvironmentsID[i] = -1;
67-
6846
create_context();
6947
GContext->IO.shaderDirectory = "../../Sandbox/shaders/";
7048
GContext->IO.resourceDirectory = "../../Resources/";
@@ -86,6 +64,10 @@ int main()
8664
mvAssetManager am{};
8765
initialize_asset_manager(&am);
8866
Renderer::setup_common_assets(am);
67+
68+
mvEnvironment environment = create_environment("../../data/glTF-Sample-Environments/" + std::string(env_maps[envMapIndex]) + ".hdr", 1024, 1024, 1.0f, 7);
69+
mvGLTFModel gltfmodel0 = mvLoadGLTF(gltf_directories[modelIndex], gltf_models[modelIndex]);
70+
mvModel model = load_gltf_assets(am, gltfmodel0);
8971

9072
mvRendererContext renderCtx = Renderer::create_renderer_context(am);
9173

@@ -142,62 +124,22 @@ int main()
142124
else
143125
{
144126
showSkybox = true;
145-
b8 cacheFound = false;
146-
for (int i = 0; i < ENV_CACHE_SIZE; i++)
147-
{
148-
if (cachedEnvironmentsID[i] == envMapIndex)
149-
{
150-
activeEnv = i;
151-
cacheFound = true;
152-
break;
153-
}
154-
}
155-
156-
if (!cacheFound)
157-
{
158-
std::string newMap = "../../data/glTF-Sample-Environments/" + std::string(env_maps[envMapIndex]) + ".hdr";
159-
cleanup_environment(cachedEnvironments[envCacheIndex]);
160-
cachedEnvironments[envCacheIndex] = create_environment(newMap, 1024, 1024, 1.0f, 7);
161-
cachedEnvironmentsID[envCacheIndex] = envMapIndex;
162-
activeEnv = envCacheIndex;
163-
envCacheIndex++;
164-
if (envCacheIndex == ENV_CACHE_SIZE) envCacheIndex = 0;
165-
}
127+
std::string newMap = "../../data/glTF-Sample-Environments/" + std::string(env_maps[envMapIndex]) + ".hdr";
128+
cleanup_environment(environment);
129+
environment = create_environment(newMap, 1024, 1024, 1.0f, 7);
166130
}
167131
}
168132
if (changeScene)
169133
{
170134
changeScene = false;
171-
b8 cacheFound = false;
172-
for (int i = 0; i < MODEL_CACHE_SIZE; i++)
173-
{
174-
if (cachedModelIndex[i] == modelIndex)
175-
{
176-
cacheIndex = cachedModelIndex[i];
177-
cacheFound = true;
178-
break;
179-
}
180-
}
181-
182-
if (!cacheFound)
183-
{
135+
unload_gltf_assets(am, model);
184136

185-
i32 modelToRemove = cachedModelIndex[cacheRingIndex];
137+
gltfmodel0 = mvLoadGLTF(gltf_directories[modelIndex], gltf_models[modelIndex]);
138+
model = load_gltf_assets(am, gltfmodel0);
139+
mvCleanupGLTF(gltfmodel0);
186140

187-
if (modelToRemove > -1)
188-
unload_gltf_assets(am, cachedModel[modelToRemove]);
189-
190-
mvGLTFModel gltfmodel0 = mvLoadGLTF(gltf_directories[modelIndex], gltf_models[modelIndex]);
191-
cachedModel[cacheRingIndex] = load_gltf_assets(am, gltfmodel0);
192-
mvCleanupGLTF(gltfmodel0);
193-
cachedModelIndex[cacheRingIndex] = modelToRemove;
194-
cacheIndex = cacheRingIndex;
195-
cacheRingIndex++;
196-
if (cacheRingIndex == MODEL_CACHE_SIZE) cacheRingIndex = 0;
197-
198-
}
199-
camera.minBound = mvVec3{ cachedModel[cacheIndex].minBoundary[0], cachedModel[cacheIndex].minBoundary[1], cachedModel[cacheIndex].minBoundary[2] };
200-
camera.maxBound = mvVec3{ cachedModel[cacheIndex].maxBoundary[0], cachedModel[cacheIndex].maxBoundary[1], cachedModel[cacheIndex].maxBoundary[2] };
141+
camera.minBound = mvVec3{ model.minBoundary[0], model.minBoundary[1], model.minBoundary[2] };
142+
camera.maxBound = mvVec3{ model.maxBoundary[0], model.maxBoundary[1], model.maxBoundary[2] };
201143

202144
camera.target.x = (camera.minBound.x + camera.maxBound.x) / 2.0f;
203145
camera.target.y = (camera.minBound.y + camera.maxBound.y) / 2.0f;
@@ -226,9 +168,6 @@ int main()
226168
for (i32 i = 0; i < 3; i++)
227169
target[i] = (camera.maxBound[i] + camera.minBound[i]) / 2.0f;
228170
camera.pos = mvVec3{ target[0], target[1], target[2] + camera.distance };
229-
230-
231-
232171
}
233172

234173
//-----------------------------------------------------------------------------
@@ -257,9 +196,7 @@ int main()
257196
for(int i = 0; i < 12; i++)
258197
ctx->PSSetShaderResources(i, 6, pSRV);
259198

260-
i32 activeScene = -1;
261-
if (cacheIndex > -1)
262-
activeScene = cachedModel[cacheIndex].defaultScene;
199+
i32 activeScene = model.defaultScene;
263200

264201
//-----------------------------------------------------------------------------
265202
// offscreen pass
@@ -282,14 +219,14 @@ int main()
282219
ctx->PSSetConstantBuffers(2u, 1u, &directionalLight.buffer.buffer);
283220
ctx->PSSetConstantBuffers(3u, 1u, &renderCtx.globalInfoBuffer.buffer);
284221

285-
if (activeEnv > -1)
222+
if (envMapIndex > -1)
286223
{
287-
ctx->PSSetSamplers(12u, 1, &cachedEnvironments[activeEnv].sampler);
288-
ctx->PSSetSamplers(13u, 1, &cachedEnvironments[activeEnv].sampler);
289-
ctx->PSSetSamplers(14u, 1, &cachedEnvironments[activeEnv].brdfSampler);
290-
ctx->PSSetShaderResources(12u, 1, &cachedEnvironments[activeEnv].irradianceMap.textureView);
291-
ctx->PSSetShaderResources(13u, 1, &cachedEnvironments[activeEnv].specularMap.textureView);
292-
ctx->PSSetShaderResources(14u, 1, &cachedEnvironments[activeEnv].brdfLUT.textureView);
224+
ctx->PSSetSamplers(12u, 1, &environment.sampler);
225+
ctx->PSSetSamplers(13u, 1, &environment.sampler);
226+
ctx->PSSetSamplers(14u, 1, &environment.brdfSampler);
227+
ctx->PSSetShaderResources(12u, 1, &environment.irradianceMap.textureView);
228+
ctx->PSSetShaderResources(13u, 1, &environment.specularMap.textureView);
229+
ctx->PSSetShaderResources(14u, 1, &environment.brdfLUT.textureView);
293230
}
294231

295232
Renderer::render_mesh_solid(am, pointlight.mesh, translate(identity_mat4(), pointlight.info.viewLightPos.xyz()), viewMatrix, projMatrix);
@@ -316,10 +253,8 @@ int main()
316253
Renderer::render_scenes(am, renderCtx, viewMatrix, projMatrix);
317254
}
318255

319-
if (showSkybox && activeEnv > -1)
320-
Renderer::render_skybox(am,
321-
blur ? cachedEnvironments[activeEnv].specularMap : cachedEnvironments[activeEnv].skyMap,
322-
cachedEnvironments[activeEnv].sampler, viewMatrix, projMatrix);
256+
if (showSkybox && envMapIndex > -1)
257+
Renderer::render_skybox(am,blur ? environment.specularMap : environment.skyMap, environment.sampler, viewMatrix, projMatrix);
323258

324259
//-----------------------------------------------------------------------------
325260
// ui
@@ -464,13 +399,8 @@ int main()
464399
// Cleanup
465400
renderCtx.finalBlendState->Release();
466401
cleanup_asset_manager(&am);
467-
468-
for (int i = 0; i < ENV_CACHE_SIZE; i++)
469-
cleanup_environment(cachedEnvironments[i]);
470-
471-
for (int i = 0; i < MODEL_CACHE_SIZE; i++)
472-
unload_gltf_assets(am, cachedModel[i]);
473-
402+
cleanup_environment(environment);
403+
unload_gltf_assets(am, model);
474404
ImGui_ImplDX11_Shutdown();
475405
ImGui_ImplWin32_Shutdown();
476406
ImGui::DestroyContext();

Sandbox/src/renderer/mvEnvironment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "mvSandbox.h"
44
#include <assert.h>
55
#include "stb_image.h"
6-
#include "mvShader.h"
6+
#include "mvPipeline.h"
77

88
static mvCubeTexture
99
create_cube_map_from_hdr(const std::string& path)

Sandbox/src/renderer/mvMaterials.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "mvAssetManager.h"
33
#include <assert.h>
44
#include "mvSandbox.h"
5-
#include "mvVertexLayout.h"
5+
#include "mvPipeline.h"
66

77
std::string hash_material(const mvMaterial& material, const mvVertexLayout& layout, const std::string& pixelShader, const std::string& vertexShader)
88
{

Sandbox/src/renderer/mvMesh.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <string>
44
#include "mvMath.h"
55
#include "mvBuffers.h"
6-
#include "mvVertexLayout.h"
6+
#include "mvPipeline.h"
77
#include "mvTextures.h"
88

99
// forward declarations
@@ -14,6 +14,7 @@ struct mvMesh;
1414
struct mvSkin;
1515
struct mvNode;
1616

17+
1718
mvMesh create_cube (mvAssetManager& assetManager, f32 size = 1.0f);
1819
mvMesh create_textured_cube(mvAssetManager& assetManager, f32 size = 1.0f);
1920
mvMesh create_textured_quad(mvAssetManager& assetManager, f32 size = 1.0f);

0 commit comments

Comments
 (0)