Skip to content

Commit

Permalink
Minor backend improvments, started adding variable rate shading, othe…
Browse files Browse the repository at this point in the history
…r minor changes and improvements
  • Loading branch information
voldien committed Feb 27, 2025
1 parent 7e72a05 commit ac103e7
Show file tree
Hide file tree
Showing 29 changed files with 356 additions and 326 deletions.
14 changes: 9 additions & 5 deletions Samples/Mandelbrot/mandelbrot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ namespace glsample {
const std::vector<uint32_t> mandelbrot_binary =
IOUtil::readFileData<uint32_t>(this->computeMandelbrotShaderPath, this->getFileSystem());

const std::vector<uint32_t> julia_binary =
IOUtil::readFileData<uint32_t>(this->computeJuliaShaderPath, this->getFileSystem());

/* */
fragcore::ShaderCompiler::CompilerConvertOption compilerOptions;
compilerOptions.target = fragcore::ShaderLanguage::GLSL;
Expand All @@ -100,9 +103,6 @@ namespace glsample {
/* Load shader */
this->mandelbrot_program = ShaderLoader::loadComputeProgram(compilerOptions, &mandelbrot_binary);

const std::vector<uint32_t> julia_binary =
IOUtil::readFileData<uint32_t>(this->computeJuliaShaderPath, this->getFileSystem());

/* Load shader */
this->julia_program = ShaderLoader::loadComputeProgram(compilerOptions, &julia_binary);
}
Expand Down Expand Up @@ -196,8 +196,12 @@ namespace glsample {

glBindImageTexture(0, this->mandelbrot_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8);

glDispatchCompute(std::ceil(this->mandelbrot_texture_width / (float)localWorkGroupSize[0]),
std::ceil(this->mandelbrot_texture_height / (float)localWorkGroupSize[1]), 1);
const unsigned int WorkGroupX =
std::ceil(this->mandelbrot_texture_width / (float)localWorkGroupSize[0]);
const unsigned int WorkGroupY =
std::ceil(this->mandelbrot_texture_height / (float)localWorkGroupSize[1]);

glDispatchCompute(WorkGroupX, WorkGroupY, 1);

glUseProgram(0);
/* Wait in till image has been written. */
Expand Down
58 changes: 36 additions & 22 deletions Samples/ModelViewer/ModelViewer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ModelViewer.h"
#include "ImageImport.h"
#include "ModelImporter.h"
#include "Scene.h"
#include "Skybox.h"
#include <GL/glew.h>
#include <GLSample.h>
Expand All @@ -15,12 +16,12 @@ namespace glsample {
ModelViewer::ModelViewer() {
this->setTitle("Model Viewer");

this->modelviewerSettingComponent = std::make_shared<ModelViewerSettingComponent>(this->uniformStageBuffer);
this->modelviewerSettingComponent = std::make_shared<ModelViewerSettingComponent>(*this);
this->addUIComponent(this->modelviewerSettingComponent);

/* Default camera position and orientation. */
this->camera.setPosition(glm::vec3(-2.5f));
this->camera.lookAt(glm::vec3(0.f));
this->cameraController.setPosition(glm::vec3(20.5f));
this->cameraController.lookAt(glm::vec3(0.f));
}

void ModelViewer::Release() {}
Expand All @@ -47,9 +48,8 @@ namespace glsample {
compilerOptions.glslVersion = this->getShaderVersion();

/* Load shader */
this->physical_based_rendering_program =
ShaderLoader::loadGraphicProgram(compilerOptions, &pbr_vertex_binary, &pbr_fragment_binary, nullptr,
&pbr_control_binary, &pbr_evolution_binary);
this->physical_based_rendering_program = ShaderLoader::loadGraphicProgram(
compilerOptions, &pbr_vertex_binary, &pbr_fragment_binary, nullptr, nullptr, nullptr);

/* Create skybox graphic pipeline program. */
this->skybox_program = Skybox::loadDefaultProgram(this->getFileSystem());
Expand All @@ -59,8 +59,20 @@ namespace glsample {
glUseProgram(this->physical_based_rendering_program);
int uniform_buffer_index = glGetUniformBlockIndex(this->physical_based_rendering_program, "UniformBufferBlock");

glUniform1i(glGetUniformLocation(this->physical_based_rendering_program, "albedoMap"), 0);
glUniform1i(glGetUniformLocation(this->physical_based_rendering_program, "normalMap"), 1);
glUniform1iARB(glGetUniformLocation(this->physical_based_rendering_program, "DiffuseTexture"),
(int)TextureType::Diffuse);
glUniform1iARB(glGetUniformLocation(this->physical_based_rendering_program, "NormalTexture"),
(int)TextureType::Normal);
glUniform1iARB(glGetUniformLocation(this->physical_based_rendering_program, "AOTexture"),
(int)TextureType::AmbientOcclusion);
glUniform1iARB(glGetUniformLocation(this->physical_based_rendering_program, "RoughnessTexture"),
(int)TextureType::Specular);
glUniform1iARB(glGetUniformLocation(this->physical_based_rendering_program, "DisplacementTexture"),
(int)TextureType::Displacement);
glUniform1iARB(glGetUniformLocation(this->physical_based_rendering_program, "EmissionTexture"),
(int)TextureType::Emission);
glUniform1iARB(glGetUniformLocation(this->physical_based_rendering_program, "IrradianceTexture"),
(int)TextureType::Irradiance);
glUniformBlockBinding(this->physical_based_rendering_program, uniform_buffer_index, 0);

glUseProgram(0);
Expand All @@ -70,6 +82,9 @@ namespace glsample {
unsigned int skytexture = textureImporter.loadImage2D(skyboxPath);
this->skybox.Init(skytexture, this->skybox_program);

ProcessData util(this->getFileSystem());
util.computeIrradiance(skytexture, this->irradiance_texture, 256, 128);

/* */
ModelImporter *modelLoader = new ModelImporter(this->getFileSystem());
modelLoader->loadContent(modelPath, 0);
Expand Down Expand Up @@ -98,43 +113,42 @@ namespace glsample {
(this->getFrameCount() % nrUniformBuffer) * this->uniformAlignBufferSize,
this->uniformAlignBufferSize);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->getDefaultFramebuffer());
/* Set render viewport size in pixels. */
glViewport(0, 0, width, height);
/* Clear default framebuffer color attachment. */
glClearColor(0.095f, 0.095f, 0.095f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT);

{
glActiveTexture(GL_TEXTURE0 + TextureType::Irradiance);
glBindTexture(GL_TEXTURE_2D, this->irradiance_texture);

glCullFace(GL_BACK);

/* Bind shader pipeline. */
glUseProgram(this->physical_based_rendering_program);
this->scene.render();
this->scene.render(&this->cameraController);
glUseProgram(0);
}

this->skybox.Render(this->camera);
this->skybox.Render(this->cameraController);
}

void ModelViewer::update() {
/* Update Camera. */
const float elapsedTime = this->getTimer().getElapsed<float>();
this->camera.update(this->getTimer().deltaTime<float>());
this->cameraController.update(this->getTimer().deltaTime<float>());
this->scene.update(this->getTimer().deltaTime<float>());

/* */
{
this->uniformStageBuffer.model = glm::mat4(1.0f);
this->uniformStageBuffer.model = glm::rotate(
this->uniformStageBuffer.model, glm::radians(elapsedTime * 12.0f), glm::vec3(0.0f, 1.0f, 0.0f));
this->uniformStageBuffer.model = glm::scale(this->uniformStageBuffer.model, glm::vec3(2.95f));
this->uniformStageBuffer.model = glm::scale(this->uniformStageBuffer.model, glm::vec3(1.0f));

this->uniformStageBuffer.view = this->camera.getViewMatrix();
this->uniformStageBuffer.proj = this->camera.getProjectionMatrix();
this->uniformStageBuffer.view = this->cameraController.getViewMatrix();
this->uniformStageBuffer.proj = this->cameraController.getProjectionMatrix();
this->uniformStageBuffer.viewProjection = this->uniformStageBuffer.proj * this->uniformStageBuffer.view;
this->uniformStageBuffer.modelViewProjection =
this->uniformStageBuffer.proj * this->uniformStageBuffer.view * this->uniformStageBuffer.model;

this->uniformStageBuffer.camera.gEyeWorldPos = glm::vec4(this->camera.getPosition(), 0);
}

/* */
Expand Down
65 changes: 13 additions & 52 deletions Samples/ModelViewer/ModelViewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ namespace glsample {
public:
ModelViewer();

struct light_settings {

DirectionalLight directionalLight;
/* */
PointLightInstance pointLights[4];
};

struct camera_settings {
glm::vec4 gEyeWorldPos;
};

struct tessellation_settings {
float tessLevel = 1;
float gDispFactor = 1;
Expand All @@ -40,79 +29,51 @@ namespace glsample {
glm::mat4 viewProjection{};
glm::mat4 modelViewProjection{};

struct light_settings lightsettings;
struct tessellation_settings tessellation;
/* Camera settings. */
struct camera_settings camera {};

} uniformStageBuffer;

int physical_based_rendering_program;
int skybox_program;
unsigned int physical_based_rendering_program;
unsigned int skybox_program;
unsigned int irradiance_texture;

Skybox skybox;
Scene scene;
CameraController camera;
CameraController cameraController;

/* */
unsigned int uniform_buffer_binding = 0;
unsigned int uniform_buffer;
const size_t nrUniformBuffer = 3;
size_t uniformAlignBufferSize = sizeof(uniform_buffer_block);

/* Simple */
const std::string vertexPBRShaderPath = "Shaders/pbr/simplephysicalbasedrendering.vert.spv";
const std::string fragmentPBRShaderPath = "Shaders/pbr/simplephysicalbasedrendering.frag.spv";

/* Advanced. */
const std::string PBRvertexShaderPath = "Shaders/pbr/physicalbasedrendering.vert.spv";
const std::string PBRfragmentShaderPath = "Shaders/pbr/physicalbasedrendering.frag.spv";
const std::string PBRControlShaderPath = "Shaders/pbr/physicalbasedrendering.tesc.spv";
const std::string PBREvoluationShaderPath = "Shaders/pbr/physicalbasedrendering.tese.spv";

class ModelViewerSettingComponent : public nekomimi::UIComponent {
class ModelViewerSettingComponent : public GLUIComponent<ModelViewer> {

public:
ModelViewerSettingComponent(struct uniform_buffer_block &uniform) : uniform(uniform) {
ModelViewerSettingComponent(ModelViewer &sample)
: GLUIComponent(sample), uniform(sample.uniformStageBuffer) {
this->setName("Model Viewer");
}
void draw() override {

//
// ImGui::ColorEdit4("Light", &this->uniform.lightColor[0],
// ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
// ImGui::ColorEdit4("Ambient", &this->uniform.ambientColor[0],
// ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
// ImGui::DragFloat3("Direction", &this->uniform.direction[0]);

ImGui::TextUnformatted("Light Settings");
for (size_t i = 0;
i < sizeof(uniform.lightsettings.pointLights) / sizeof(uniform.lightsettings.pointLights[0]);
i++) {
ImGui::PushID(1000 + i);
if (ImGui::CollapsingHeader(fmt::format("Light {}", i).c_str(), &lightvisible[i],
ImGuiTreeNodeFlags_CollapsingHeader)) {

ImGui::ColorEdit4("Color", &this->uniform.lightsettings.pointLights[i].color[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
ImGui::DragFloat3("Position", &this->uniform.lightsettings.pointLights[i].position[0]);
ImGui::DragFloat3("Attenuation",
&this->uniform.lightsettings.pointLights[i].constant_attenuation);
ImGui::DragFloat("Range", &this->uniform.lightsettings.pointLights[i].range);
ImGui::DragFloat("Intensity", &this->uniform.lightsettings.pointLights[i].intensity);
}
ImGui::PopID();
}

ImGui::TextUnformatted("Light");
// ImGui::ColorEdit4("Color", &this->uniform.lightsettings.lightColor[0],
// ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR);
// ImGui::ColorEdit4("Ambient Color", &this->uniform.lightsettings.ambientColor[0],
// ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
// ImGui::DragFloat3("Direction", &this->uniform.lightsettings.direction[0]);

ImGui::TextUnformatted("Tessellation");
ImGui::DragFloat("Displacement", &this->uniform.tessellation.gDispFactor, 1, 0.0f, 100.0f);
ImGui::DragFloat("Levels", &this->uniform.tessellation.tessLevel, 1, 0.0f, 10.0f);

ImGui::TextUnformatted("Debugging");
ImGui::Checkbox("WireFrame", &this->showWireFrame);

this->getRefSample().scene.renderUI();
}

bool showWireFrame = false;
Expand Down
24 changes: 12 additions & 12 deletions Samples/Normal/Normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ namespace glsample {
this->camera.lookAt(glm::vec3(0.f));
}

struct uniform_buffer_block {
glm::mat4 model;
glm::mat4 view;
glm::mat4 proj;
glm::mat4 modelView;
glm::mat4 ViewProj;
glm::mat4 modelViewProjection;
struct alignas(16) uniform_buffer_block {
glm::mat4 model{};
glm::mat4 view{};
glm::mat4 proj{};
glm::mat4 modelView{};
glm::mat4 ViewProj{};
glm::mat4 modelViewProjection{};

/* */
float normalLength = 1.0f;
Expand All @@ -44,16 +44,16 @@ namespace glsample {
std::vector<MeshObject> refObj;

/* Textures. */
unsigned int diffuse_texture;
unsigned int diffuse_texture{};

/* */
unsigned int graphic_program;
unsigned int normal_vertex_program;
unsigned int normal_triangle_program;
unsigned int graphic_program{};
unsigned int normal_vertex_program{};
unsigned int normal_triangle_program{};

/* Uniform buffer. */
unsigned int uniform_buffer_binding = 0;
unsigned int uniform_buffer;
unsigned int uniform_buffer{};
const size_t nrUniformBuffer = 3;
size_t uniformAlignBufferSize = sizeof(uniform_buffer_block);

Expand Down
5 changes: 1 addition & 4 deletions Samples/PhysicalBasedRendering/PhysicalBasedRendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,6 @@ namespace glsample {
(int)TextureType::Displacement);
glUniform1iARB(glGetUniformLocation(this->physical_based_rendering_program, "IrradianceTexture"),
(int)TextureType::Irradiance);
// glUniform1iARB(glGetUniformLocation(this->physical_based_rendering_program, "IrradianceTexture"),
// (int)TextureType::Irradiance);

glUniformBlockBinding(this->physical_based_rendering_program, uniform_buffer_index,
this->uniform_buffer_binding);
uniform_buffer_index = glGetUniformBlockIndex(this->physical_based_rendering_program, "UniformBufferBlock");
Expand Down Expand Up @@ -187,7 +184,7 @@ namespace glsample {
glUniformBlockBinding(this->simple_physical_based_rendering_program, uniform_buffer_index,
this->uniform_buffer_binding);
glUseProgram(0);

/* */
glUseProgram(this->skybox_program);
uniform_buffer_index = glGetUniformBlockIndex(this->skybox_program, "UniformBufferBlock");
Expand Down
5 changes: 1 addition & 4 deletions Samples/ShadowMapping/ShadowMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ namespace glsample {
glViewport(0, 0, width, height);

/* */
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);

/* */
if (this->shadowSettingComponent->use_pcf) {
Expand All @@ -383,9 +383,6 @@ namespace glsample {
}

glCullFace(GL_BACK);
// glDisable(GL_CULL_FACE);
/* Optional - to display wireframe. */
glPolygonMode(GL_FRONT_AND_BACK, this->shadowSettingComponent->showWireFrame ? GL_LINE : GL_FILL);

/* */
glActiveTexture(GL_TEXTURE0 + shadowBinding);
Expand Down
Loading

0 comments on commit ac103e7

Please sign in to comment.