summaryrefslogtreecommitdiff
path: root/model.cpp
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2025-02-13 23:32:28 +1100
committerquou <quou@disroot.org>2025-02-13 23:33:54 +1100
commit2e4ecca19aadc09d5c3d927724f8004b6a0ff0b0 (patch)
tree156c747452788f5221158f6e2a9151eaeb61b672 /model.cpp
parent42cd6a572a35c354dc7d7fd390e86e7cff191617 (diff)
refactoring; prep for shadows
Diffstat (limited to 'model.cpp')
-rw-r--r--model.cpp90
1 files changed, 45 insertions, 45 deletions
diff --git a/model.cpp b/model.cpp
index e864ff5..caa1422 100644
--- a/model.cpp
+++ b/model.cpp
@@ -14,9 +14,9 @@ extern "C" {
#include <stdio.h>
#include <algorithm>
-struct MVP_Cbuffer {
+struct Model_Cbuffer {
m4f model;
- m4f view_projection;
+ m4f reserved;
};
struct Mat_Cbuffer {
@@ -130,13 +130,18 @@ Asset* Model_Loader::load(
assert(mesh.material != 0);
mesh.shader = shader->id;
mesh.depth_shader = depth_shader->id;
- mesh.mvp_binding = shader->descriptor_binding("c_mvp");
- mesh.mvp_binding_depth = depth_shader->descriptor_binding("c_mvp");
+ mesh.model_binding = shader->descriptor_binding("c_model");
+ mesh.model_binding_depth = depth_shader->descriptor_binding("c_model");
+ mesh.vp_binding = shader->descriptor_binding("c_vp");
+ mesh.vp_binding_depth = depth_shader->descriptor_binding("c_vp");
mesh.mat_binding = shader->descriptor_binding("material");
mesh.light_binding = shader->descriptor_binding("lights");
mesh.env_cube_binding = shader->descriptor_binding("env_cube");
mesh.mesh_binding = shader->binding_index("mesh");
- assert(mesh.mvp_binding >= 0);
+ assert(mesh.model_binding >= 0);
+ assert(mesh.model_binding_depth >= 0);
+ assert(mesh.vp_binding >= 0);
+ assert(mesh.vp_binding_depth >= 0);
assert(mesh.mat_binding >= 0);
assert(mesh.light_binding >= 0);
assert(mesh.mesh_binding >= 0);
@@ -264,9 +269,9 @@ void Material_Loader::unload(Asset* a) {
void Model_Instance::init(Device* dev, Heap* h, Model* model) {
m = model;
- mvp = dev->create_buffer(
- "Model instance MVP",
- sizeof(MVP_Cbuffer) * m->mesh_count,
+ mcb = dev->create_buffer(
+ "Model instance Model",
+ sizeof(Model_Cbuffer) * m->mesh_count,
Buffer_Flags::constant_buffer |
Buffer_Flags::cpu_readwrite
);
@@ -284,7 +289,7 @@ void Model_Instance::init(Device* dev, Heap* h, Model* model) {
void Model_Instance::destroy(Device* dev, Heap* h) {
dev->destroy_buffer(mat);
- dev->destroy_buffer(mvp);
+ dev->destroy_buffer(mcb);
heap_free(h, bounds);
}
@@ -305,22 +310,20 @@ void Model_Instance::update_cbuffers(
) {
int i, c = m->mesh_count;
Mesh* meshes = m->get_meshes();
- MVP_Cbuffer* mvps = (MVP_Cbuffer*)dev->map_buffer(
- mvp,
+ Model_Cbuffer* mcbs = (Model_Cbuffer*)dev->map_buffer(
+ mcb,
0,
- c * sizeof *mvps
+ c * sizeof *mcbs
);
Mat_Cbuffer* mats = (Mat_Cbuffer*)dev->map_buffer(
mat,
0,
c * sizeof *mats
);
- m4f view_projection = cam.get_proj() * cam.get_view();
for (i = 0; i < c; i++) {
Mat_Cbuffer& mat = mats[i];
Material& sm = *meshes[i].material;
- mvps[i].view_projection = view_projection;
- mvps[i].model = transform * meshes[i].world;
+ mcbs[i].model = transform * meshes[i].world;
mat.albedo[0] = sm.albedo.x;
mat.albedo[1] = sm.albedo.y;
mat.albedo[2] = sm.albedo.z;
@@ -332,7 +335,7 @@ void Model_Instance::update_cbuffers(
mat.camera_pos[1] = cam.position.y;
mat.camera_pos[2] = cam.position.z;
}
- dev->unmap_buffer(mvp);
+ dev->unmap_buffer(mcb);
dev->unmap_buffer(mat);
}
@@ -341,8 +344,7 @@ void Model_Instance::render(
Arena* a,
Render_Pass& pass,
const Lighting* lighting,
- Texture_Id env_cubemap,
- Sampler_Id sampler
+ const Model_Resources& res
) {
int i, c = m->mesh_count;
Mesh* meshes = m->get_meshes();
@@ -368,21 +370,25 @@ void Model_Instance::render(
draw.instance_count = 1;
draw.first_vertex = mesh.offset;
draw.vertex_offset = mesh.vbo_offset;
- pb.begin();
+ pb.begin(&pass);
if (depth_only) {
pb.depth(true, true, Depth_Mode::less);
pb.shader(mesh.depth_shader);
pb.cbuffer(
- mesh.mvp_binding_depth,
- mvp,
- i * sizeof(MVP_Cbuffer),
- sizeof(MVP_Cbuffer)
+ mesh.model_binding_depth,
+ mcb,
+ i * sizeof(Model_Cbuffer),
+ sizeof(Model_Cbuffer)
+ );
+ pb.cbuffer(
+ mesh.vp_binding_depth,
+ res.vp
);
} else {
pb.depth(true, false, Depth_Mode::equal);
pb.shader(mesh.shader);
pb.sbuffer(mesh.light_binding, lighting->lights.gpuonly);
- mesh.material->use(pb, sampler, dev->get_shader(mesh.shader));
+ mesh.material->use(pb, res.sampler, dev->get_shader(mesh.shader));
pb.cbuffer(
mesh.mat_binding,
mat,
@@ -390,15 +396,19 @@ void Model_Instance::render(
sizeof(Mat_Cbuffer)
);
pb.cbuffer(
- mesh.mvp_binding,
- mvp,
- i * sizeof(MVP_Cbuffer),
- sizeof(MVP_Cbuffer)
+ mesh.model_binding,
+ mcb,
+ i * sizeof(Model_Cbuffer),
+ sizeof(Model_Cbuffer)
+ );
+ pb.cbuffer(
+ mesh.vp_binding,
+ res.vp
);
pb.texture(
mesh.env_cube_binding,
- env_cubemap,
- sampler
+ res.env_cubemap,
+ res.sampler
);
}
pb.cull(Cull_Mode::back);
@@ -417,7 +427,11 @@ void Model_Scene::init(
h = (Heap*)arena_alloc(arena, sizeof *h);
hs = arena->size - arena->ptr - allocation_default_alignment - 1;
init_heap(h, arena_alloc(arena, hs), hs);
- instances = (Model_Instance*)heap_alloc(h, max_instances);
+ instances = (Model_Instance*)heap_alloc(
+ h,
+ max_instances * sizeof *instances
+ );
+ assert(instances != 0);
count = 0;
max = max_instances;
sampler = s;
@@ -457,20 +471,6 @@ void Model_Scene::update(
}
}
-void Model_Scene::render(
- Device* dev,
- Arena* a,
- Render_Pass& pass,
- const Lighting* lighting,
- Texture_Id env_cubemap,
- Sampler_Id sampler
-) {
- int i;
- Model_Instance* instance = instances;
- for (i = 0; i < count; i++, instance++)
- instance->render(dev, a, pass, lighting, env_cubemap, sampler);
-}
-
void Model_Scene::destroy(Device* dev) {
int i;
Model_Instance* instance = instances;