summaryrefslogtreecommitdiff
path: root/src/render
diff options
context:
space:
mode:
Diffstat (limited to 'src/render')
-rw-r--r--src/render/immdraw.c94
-rw-r--r--src/render/immdraw.h29
-rw-r--r--src/render/render.c21
-rw-r--r--src/render/render.h2
-rw-r--r--src/render/render_types.h2
5 files changed, 123 insertions, 25 deletions
diff --git a/src/render/immdraw.c b/src/render/immdraw.c
index c711b0c..dfe189a 100644
--- a/src/render/immdraw.c
+++ b/src/render/immdraw.c
@@ -1,5 +1,8 @@
#include "immdraw.h"
+#include "core.h"
+#include "file.h"
#include "log.h"
+#include "maths.h"
#include "primitives.h"
#include "ral_common.h"
#include "ral_impl.h"
@@ -9,20 +12,91 @@
void Immdraw_Init(Immdraw_Storage* storage) {
INFO("Immediate drawing initialisation");
- // meshes
- // Geometry sphere_geo = Geo_CreateUVsphere(1.0, 8, 8);
- // storage->sphere = Mesh_Create(&sphere_geo, false);
- // pipeline / material
+ // Meshes
+ Geometry sphere_geo = Geo_CreateUVsphere(1.0, 8, 8);
+ storage->sphere = Mesh_Create(&sphere_geo, false);
+
+ Geometry cube_geo = Geo_CreateCuboid(f32x3(2.0, 2.0, 2.0));
+ storage->cube = Mesh_Create(&cube_geo, false);
+
+ Geometry plane_geo = Geo_CreatePlane(f32x2(2.0, 2.0));
+ storage->plane = Mesh_Create(&plane_geo, false);
+
+ // Pipeline / material
+ VertexDescription vertex_desc = {
+ .debug_label = "Immdraw Vertex",
+ .use_full_vertex_size = true,
+ };
+ VertexDesc_AddAttr(&vertex_desc, "position", ATTR_F32x3);
+ VertexDesc_AddAttr(&vertex_desc, "normal", ATTR_F32x3);
+
+ arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024);
+ Str8 vert_path = str8("assets/shaders/immdraw.vert");
+ Str8 frag_path = str8("assets/shaders/immdraw.frag");
+ str8_opt vertex_shader = str8_from_file(&scratch, vert_path);
+ str8_opt fragment_shader = str8_from_file(&scratch, frag_path);
+ if (!vertex_shader.has_value || !fragment_shader.has_value) {
+ ERROR_EXIT("Failed to load shaders from disk");
+ }
+
ShaderDataLayout camera_data = Binding_Camera_GetLayout(NULL);
+ ShaderDataLayout imm_uniform_data = ImmediateUniforms_GetLayout(NULL);
+
GraphicsPipelineDesc pipeline_desc = {
.debug_name = "Immediate Draw Pipeline",
- .data_layouts = { camera_data },
- .data_layouts_count = 1,
-
+ .vertex_desc = static_3d_vertex_description(),
+ .data_layouts = { camera_data, imm_uniform_data },
+ .data_layouts_count = 2,
+ .vs = { .debug_name = "Immdraw Vertex Shader",
+ .filepath = vert_path,
+ .code = vertex_shader.contents },
+ .fs = { .debug_name = "Immdraw Fragment Shader",
+ .filepath = frag_path,
+ .code = fragment_shader.contents },
+ .depth_test = true,
+ .wireframe = false,
};
- // storage->colour_pipeline = GPU_GraphicsPipeline_Create(pipeline_desc,
- // GPU_GetDefaultRenderpass());
+ GPU_Renderpass* rpass =
+ GPU_Renderpass_Create((GPU_RenderpassDesc){ .default_framebuffer = true });
+ storage->colour_pipeline = GPU_GraphicsPipeline_Create(pipeline_desc, rpass);
}
-void Immdraw_Sphere(Transform tf, f32 size, Vec4 colour, bool wireframe) {} \ No newline at end of file
+void Immdraw_Shutdown(Immdraw_Storage* storage) {
+ GraphicsPipeline_Destroy(storage->colour_pipeline);
+}
+
+void Immdraw_Sphere(Transform tf, f32 size, Vec4 colour, bool wireframe) {
+ INFO("Draw sphere");
+ Immdraw_Storage* imm = Render_GetImmdrawStorage();
+ GPU_CmdEncoder* enc = GPU_GetDefaultEncoder();
+
+ // begin renderpass
+ GPU_CmdEncoder_BeginRender(enc, imm->colour_pipeline->renderpass);
+ // bind pipeline
+ GPU_EncodeBindPipeline(enc, imm->colour_pipeline);
+
+ // update uniforms
+ ImmediateUniforms uniforms = {
+ .model = transform_to_mat(&tf),
+ .colour = colour,
+ };
+ Mat4 view, proj;
+ u32x2 dimensions = GPU_Swapchain_GetDimensions();
+ RenderScene* scene = Render_GetScene();
+ Camera_ViewProj(&scene->camera, (f32)dimensions.x, (f32)dimensions.y, &view, &proj);
+ Binding_Camera camera_data = { .view = view,
+ .projection = proj,
+ .viewPos = vec4(scene->camera.position.x, scene->camera.position.y,
+ scene->camera.position.z, 1.0) };
+ GPU_EncodeBindShaderData(enc, 0, Binding_Camera_GetLayout(&camera_data));
+ GPU_EncodeBindShaderData(enc, 1, ImmediateUniforms_GetLayout(&uniforms));
+
+ // draw call
+ GPU_EncodeSetVertexBuffer(enc, imm->plane.vertex_buffer);
+ GPU_EncodeSetIndexBuffer(enc, imm->plane.index_buffer);
+ GPU_EncodeDrawIndexed(enc, imm->plane.geometry.index_count);
+
+ // end renderpass
+ GPU_CmdEncoder_EndRender(enc);
+}
diff --git a/src/render/immdraw.h b/src/render/immdraw.h
index 0d58375..e635531 100644
--- a/src/render/immdraw.h
+++ b/src/render/immdraw.h
@@ -6,15 +6,21 @@
#include "defines.h"
#include "maths_types.h"
#include "ral_impl.h"
+#include "ral_types.h"
#include "render_types.h"
typedef struct Immdraw_Storage {
Mesh plane;
Mesh cube;
Mesh sphere;
- GPU_Pipeline* colour_pipeline;
+ GPU_Pipeline* colour_pipeline; /** @brief Pipeline for drawing geometry that has vertex colours */
} Immdraw_Storage;
+typedef struct ImmediateUniforms {
+ Mat4 model;
+ Vec4 colour;
+} ImmediateUniforms;
+
// --- Public API
PUB void Immdraw_Init(Immdraw_Storage* storage);
@@ -24,4 +30,25 @@ PUB void Immdraw_Shutdown(Immdraw_Storage* storage);
PUB void Immdraw_Plane(Transform tf, Vec4 colour, bool wireframe);
PUB void Immdraw_Cuboid(Transform tf, Vec4 colour, bool wireframe);
PUB void Immdraw_Sphere(Transform tf, f32 size, Vec4 colour, bool wireframe);
+
PUB void Immdraw_TransformGizmo(Transform tf, f32 size);
+
+// --- Internal
+
+static ShaderDataLayout ImmediateUniforms_GetLayout(void* data) {
+ ImmediateUniforms* d = (ImmediateUniforms*)data;
+ bool has_data = data != NULL;
+
+ ShaderBinding b1 = {
+ .label = "ImmUniforms",
+ .kind = BINDING_BYTES,
+ // .vis = VISIBILITY_VERTEX,
+ .data.bytes.size = sizeof(ImmediateUniforms)
+ };
+
+ if (has_data) {
+ b1.data.bytes.data = d;
+ }
+
+ return (ShaderDataLayout) {.bindings = { b1 }, .binding_count = 1};
+}
diff --git a/src/render/render.c b/src/render/render.c
index bad245a..bf8ac36 100644
--- a/src/render/render.c
+++ b/src/render/render.c
@@ -100,22 +100,12 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window
}
}
- // #if defined(CEL_REND_BACKEND_OPENGL)
- // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
- // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- // #elif defined(CEL_REND_BACKEND_VULKAN)
- // glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
- // #endif
-
ren->window = window;
*out_window = window;
glfwMakeContextCurrent(ren->window);
- // FIXME
- // DEBUG("Set up GLFW window callbacks");
+ DEBUG("Set up GLFW window callbacks");
glfwSetWindowSizeCallback(window, Render_WindowSizeChanged);
// set the RAL backend up
@@ -143,8 +133,8 @@ bool Renderer_Init(RendererConfig config, Renderer* ren, GLFWwindow** out_window
ren->terrain = calloc(1, sizeof(Terrain_Storage));
Terrain_Init(ren->terrain);
- ren->grid = calloc(1, sizeof(Grid_Storage));
- Grid_Init(ren->grid);
+ // ren->grid = calloc(1, sizeof(Grid_Storage));
+ // Grid_Init(ren->grid);
ren->immediate = calloc(1, sizeof(Immdraw_Storage));
Immdraw_Init(ren->immediate);
@@ -329,6 +319,11 @@ Grid_Storage* Render_GetGridStorage() {
return ren->grid;
}
+Immdraw_Storage* Render_GetImmdrawStorage() {
+ Renderer* ren = Core_GetRenderer(&g_core);
+ return ren->immediate;
+}
+
TextureHandle Render_GetWhiteTexture() {
Renderer* ren = Core_GetRenderer(&g_core);
return ren->white_1x1;
diff --git a/src/render/render.h b/src/render/render.h
index 5dc3853..785c140 100644
--- a/src/render/render.h
+++ b/src/render/render.h
@@ -6,6 +6,7 @@
#include "camera.h"
#include "defines.h"
#include "grid.h"
+#include "immdraw.h"
#include "maths_types.h"
#include "ral_types.h"
#include "render_types.h"
@@ -101,6 +102,7 @@ RenderScene* Render_GetScene();
Shadow_Storage* Render_GetShadowStorage();
Terrain_Storage* Render_GetTerrainStorage();
Grid_Storage* Render_GetGridStorage();
+Immdraw_Storage* Render_GetImmdrawStorage();
TextureHandle Render_GetWhiteTexture();
arena* Render_GetFrameArena();
Mesh_pool* Render_GetMeshPool();
diff --git a/src/render/render_types.h b/src/render/render_types.h
index 5fdca8a..924777a 100644
--- a/src/render/render_types.h
+++ b/src/render/render_types.h
@@ -41,8 +41,8 @@ typedef struct Mesh {
BufferHandle vertex_buffer;
BufferHandle index_buffer;
Geometry geometry; // NULL means it has been freed CPU-side
- // i32 material_index; // -1 => no material
MaterialHandle material;
+ bool is_skinned; // false = its static
bool is_uploaded; // has the data been uploaded to the GPU
} Mesh;
#ifndef TYPED_MESH_CONTAINERS