diff options
author | Omniscient <omniscient.oce@gmail.com> | 2024-06-15 22:20:15 +1000 |
---|---|---|
committer | Omniscient <omniscient.oce@gmail.com> | 2024-06-15 22:20:15 +1000 |
commit | d52654dfdbe16345023fed4c61261dc4c66b96fe (patch) | |
tree | 49b9dc298ec8d01533c13c909d827f96192551a1 | |
parent | b3b19f081231c7c13322c7b0d577afb6084d48df (diff) |
trying to figure out uniform structs..
-rw-r--r-- | assets/shaders/pbr_params.frag | 68 | ||||
-rw-r--r-- | assets/shaders/pbr_params.vert | 3 | ||||
-rw-r--r-- | examples/pbr_params/ex_pbr_params.c | 21 | ||||
-rw-r--r-- | src/maths/primitives.c | 5 | ||||
-rw-r--r-- | src/renderer/backends/opengl/backend_opengl.c | 16 | ||||
-rw-r--r-- | src/renderer/builtin_materials.h | 18 | ||||
-rw-r--r-- | src/renderer/render.c | 4 |
7 files changed, 85 insertions, 50 deletions
diff --git a/assets/shaders/pbr_params.frag b/assets/shaders/pbr_params.frag index 068fb3c..88cbd0d 100644 --- a/assets/shaders/pbr_params.frag +++ b/assets/shaders/pbr_params.frag @@ -12,12 +12,6 @@ struct PointLight { }; // --- Uniforms -// Lights data -#define NUM_POINT_LIGHTS 4 -uniform Scene_Lights { - vec3 viewPos; - PointLight pointLights[NUM_POINT_LIGHTS]; -} scene; // Material properties uniform PBR_Params { uniform vec3 albedo; @@ -25,6 +19,12 @@ uniform PBR_Params { uniform float roughness; uniform float ao; } pbr; +// Lights data +#define NUM_POINT_LIGHTS 4 +uniform Scene_Lights { + vec3 viewPos; + // PointLight pointLights[NUM_POINT_LIGHTS]; +} scene; const float PI = 3.14; @@ -42,39 +42,39 @@ void main() { F0 = mix(F0, pbr.albedo, pbr.metallic); vec3 Lo = vec3(0.0); // denoted L in the radiance equation - for (int i = 0; i < 4; i++) { - vec3 lightVec = normalize(scene.pointLights[i].position - fragWorldPos); - vec3 halfway = normalize(viewDir + lightVec); - float distance = length(scene.pointLights[i].position - fragWorldPos); - float attenuation = 1.0 / (distance * distance); - vec3 radiance = scene.pointLights[i].color * attenuation; - - // cook-torrance brdf - float NDF = DistributionGGX(norm, halfway, pbr.roughness); - float G = GeometrySmith(norm, viewDir, lightVec, pbr.roughness); - vec3 F = fresnelSchlick(max(dot(halfway, viewDir), 0.0), F0); - - vec3 kS = F; - vec3 kD = vec3(1.0) - kS; - kD *= 1.0 - pbr.metallic; - - vec3 numerator = NDF * G * F; - float denominator = 4.0 * max(dot(norm, viewDir), 0.0) * max(dot(norm, lightVec), 0.0) + 0.0001; - vec3 specular = numerator / denominator; - - // add to outgoing radiance Lo - float NdotL = max(dot(norm, lightVec), 0.0); - Lo += (kD * pbr.albedo / PI + specular) * radiance * NdotL; - } + // for (int i = 0; i < 4; i++) { + // vec3 lightVec = normalize(scene.pointLights[i].position - fragWorldPos); + // vec3 halfway = normalize(viewDir + lightVec); + // float distance = length(scene.pointLights[i].position - fragWorldPos); + // float attenuation = 1.0 / (distance * distance); + // vec3 radiance = scene.pointLights[i].color * attenuation; + + // // cook-torrance brdf + // float NDF = DistributionGGX(norm, halfway, pbr.roughness); + // float G = GeometrySmith(norm, viewDir, lightVec, pbr.roughness); + // vec3 F = fresnelSchlick(max(dot(halfway, viewDir), 0.0), F0); + + // vec3 kS = F; + // vec3 kD = vec3(1.0) - kS; + // kD *= 1.0 - pbr.metallic; + + // vec3 numerator = NDF * G * F; + // float denominator = 4.0 * max(dot(norm, viewDir), 0.0) * max(dot(norm, lightVec), 0.0) + 0.0001; + // vec3 specular = numerator / denominator; + + // // add to outgoing radiance Lo + // float NdotL = max(dot(norm, lightVec), 0.0); + // Lo += (kD * pbr.albedo / PI + specular) * radiance * NdotL; + // } vec3 ambient = vec3(0.03) * pbr.albedo * pbr.ao; vec3 color = ambient + Lo; - // color = color / (color + vec3(1.0)); - // color = pow(color, vec3(1.0/2.2)); + color = color / (color + vec3(1.0)); + color = pow(color, vec3(1.0/2.2)); - // FragColor = vec4(color, 1.0); - FragColor = vec4(1.0); + FragColor = vec4(scene.viewPos, 1.0); + // FragColor = vec4(1.0); } /* The below are from https://learnopengl.com/PBR/Lighting */ diff --git a/assets/shaders/pbr_params.vert b/assets/shaders/pbr_params.vert index e3cbe96..01379d7 100644 --- a/assets/shaders/pbr_params.vert +++ b/assets/shaders/pbr_params.vert @@ -19,7 +19,8 @@ layout(location = 2) out vec2 fragTexCoords; void main() { fragWorldPos = vec3(mvp.model * vec4(inPosition, 1.0)); - // TODO: fragNormal + mat3 normalMatrix = transpose(inverse(mat3(mvp.model))); + fragNormal = normalMatrix * inNormal; fragTexCoords = inTexCoords; gl_Position = mvp.proj * mvp.view * mvp.model * vec4(inPosition, 1.0); diff --git a/examples/pbr_params/ex_pbr_params.c b/examples/pbr_params/ex_pbr_params.c index 0ac784d..e7b932f 100644 --- a/examples/pbr_params/ex_pbr_params.c +++ b/examples/pbr_params/ex_pbr_params.c @@ -16,21 +16,21 @@ extern core g_core; const vec3 pointlight_positions[4] = { - { 10.0, 10.0, 10.0 }, - { -10.0, 10.0, 10.0 }, - { 10.0, -10.0, 10.0 }, - { -10.0, -10.0, 10.0 }, + { 10.0 / 5, 10.0 / 5, 10.0 / 5 }, + { -10.0 / 5, 10.0 / 5, 10.0 }, + { 10.0/ 5, -10.0 / 5, 10.0 }, + { -10.0 / 5, -10.0 / 5, 10.0 }, }; pbr_point_light point_lights[4]; // Lets define some constant PBR parameters here to test on one sphere with first const rgba ALBEDO = RED_700; -const f32 metallic = 0.0; -const f32 roughness = 0.8; +const f32 metallic = 1.0; +const f32 roughness = 0.2; const f32 ao = 0.2; const pbr_params_material_uniforms pbr_params = { - .albedo = (vec3){ 0.5, 0.5, 0.5 }, .metallic = metallic, .roughness = roughness, .ao = ao + .albedo = (vec3){ 1.0, 0.0, 0.0 }, .metallic = metallic, .roughness = roughness, .ao = ao }; int main() { @@ -76,7 +76,7 @@ int main() { .code = fragment_shader.contents, .is_spirv = false }, .renderpass = renderpass, - .wireframe = true, + .wireframe = false, .depth_test = false }; gpu_pipeline* pbr_pipeline = gpu_graphics_pipeline_create(pipeline_description); @@ -110,8 +110,9 @@ int main() { pbr_bind_data.material = pbr_params; pbr_bind_data.lights = (pbr_params_light_uniforms){ .viewPos = cam.position, - .pointLights = { point_lights[0], point_lights[1], - point_lights[2], point_lights[3] } }; + /* .pointLights = { point_lights[0], point_lights[1], */ + /* point_lights[2], point_lights[3] } */ + }; pbr_uniforms.data = &pbr_bind_data; encode_bind_shader_data(enc, 0, &pbr_uniforms); diff --git a/src/maths/primitives.c b/src/maths/primitives.c index 86f9261..86855db 100644 --- a/src/maths/primitives.c +++ b/src/maths/primitives.c @@ -234,6 +234,11 @@ geometry_data geo_create_uvsphere(f32 radius, u32 north_south_lines, u32 east_we } } + for (int i = 0; i < vertices->len; i++) { + + print_vec3(vertices->data[i].static_3d.normal); + } + geometry_data geo = { .format = VERTEX_STATIC_3D, .vertices = vertices, diff --git a/src/renderer/backends/opengl/backend_opengl.c b/src/renderer/backends/opengl/backend_opengl.c index 478ba0c..eb9ad83 100644 --- a/src/renderer/backends/opengl/backend_opengl.c +++ b/src/renderer/backends/opengl/backend_opengl.c @@ -1,6 +1,8 @@ #include <stddef.h> #include <stdio.h> +#include "builtin_materials.h" #include "colours.h" +#include "maths.h" #include "opengl_helpers.h" #include "ral_types.h" #define CEL_REND_BACKEND_OPENGL @@ -67,6 +69,7 @@ void gpu_device_destroy() { /* No-op in OpenGL */ } // --- Render Pipeline gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc description) { + static u32 ubo_count = 0; gpu_pipeline* pipeline = pipeline_pool_alloc(&context.gpu_pools.pipelines, NULL); // Create shader program @@ -182,16 +185,25 @@ void encode_bind_pipeline(gpu_cmd_encoder* encoder, pipeline_kind kind, gpu_pipe } void encode_bind_shader_data(gpu_cmd_encoder* encoder, u32 group, shader_data* data) { shader_data_layout sdl = data->shader_data_get_layout(data->data); + printf("Binding %s shader data\n", sdl.name); for (u32 i = 0; i < sdl.bindings_count; i++) { shader_binding binding = sdl.bindings[i]; - /* print_shader_binding(binding); */ + print_shader_binding(binding); if (binding.type == SHADER_BINDING_BYTES) { buffer_handle b = encoder->pipeline->uniform_bindings[i]; gpu_buffer* ubo_buf = BUFFER_GET(b); glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo); - glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo_buf->size, data->data); + if (i == 2) { + vec3* v = binding.data.bytes.data; + print_vec3(*v); + (*v).x = 0.0; + (*v).y = 0.0; + (*v).z = 1.0; + } + + glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo_buf->size, binding.data.bytes.data); } else if (binding.type == SHADER_BINDING_TEXTURE) { gpu_texture* tex = TEXTURE_GET(binding.data.texture.handle); glActiveTexture(GL_TEXTURE0); diff --git a/src/renderer/builtin_materials.h b/src/renderer/builtin_materials.h index 740ab3d..8d84e53 100644 --- a/src/renderer/builtin_materials.h +++ b/src/renderer/builtin_materials.h @@ -10,8 +10,10 @@ */ #pragma once +#include <assert.h> #include "colours.h" #include "defines.h" +#include "maths.h" #include "ral.h" #include "ral_types.h" @@ -51,7 +53,7 @@ typedef struct pbr_point_light { typedef struct pbr_params_light_uniforms { vec3 viewPos; - pbr_point_light pointLights[4]; + /* pbr_point_light pointLights[4]; */ } pbr_params_light_uniforms; typedef struct pbr_params_bindgroup { @@ -79,7 +81,21 @@ static shader_data_layout pbr_params_shader_layout(void* data) { .stores_data = has_data, .data = { .bytes = { .size = sizeof(pbr_params_light_uniforms) } } }; + printf("Size %d \n", b3.data.bytes.size); + if (has_data) { + b1.data.bytes.data = &d->mvp_matrices; + b2.data.bytes.data = &d->material; + /* d->lights.viewPos = vec3(0, 1, 0); */ + b3.data.bytes.data = &d->lights; + print_vec3(d->lights.viewPos); + } + return (shader_data_layout){ .name = "pbr_params", .bindings = { b1, b2, b3 }, .bindings_count = 3 }; } + +static void* shader_layout_get_binding(shader_data_layout* layout, u32 nth_binding) { + assert(nth_binding < layout->bindings_count); + return &layout->bindings[nth_binding].data; +} diff --git a/src/renderer/render.c b/src/renderer/render.c index 351a7e0..1c98b0b 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -62,7 +62,7 @@ bool renderer_init(renderer* ren) { resource_pools_init(&pool_arena, ren->resource_pools); // Create default rendering pipeline - default_pipelines_init(ren); + /* default_pipelines_init(ren); */ return true; } @@ -284,4 +284,4 @@ material pbr_material_load(char* albedo_path, char* normal_path, bool metal_roug m.mat_data.pbr.metallic_map = texture_data_upload(tex_data, true); return m; -}
\ No newline at end of file +} |