Commit 73404816 authored by Johannes Braun's avatar Johannes Braun
Browse files

Added batching

parent 25e1e19d
#version 430
#extension GL_ARB_shading_language_include : require
#include <util/globals.glsl>
layout(location = 0) in vec4 in_position;
layout(location = 1) in vec4 in_normal;
layout(location = 2) in vec2 in_texcoord;
uniform mat4 u_inv_view_projection;
uniform vec3 cam_position;
out vec3 out_texcoord;
void main()
{
gl_Position = in_position;
vec4 sky_vec = vec4(in_position.x, in_position.y, 1, 1);
sky_vec = u_inv_view_projection * sky_vec;
sky_vec = globals.camera.inv_view_projection_matrix * sky_vec;
sky_vec /= sky_vec.w;
vec3 direction = sky_vec.xyz - cam_position;
vec3 direction = sky_vec.xyz - globals.camera.position;
out_texcoord = direction;
}
\ No newline at end of file
}
#version 430
#extension GL_ARB_shading_language_include : require
#include <util/globals.glsl>
layout(location=0) in vec4 in_position;
layout(location=1) in vec4 in_normal;
layout(location=2) in vec2 in_texcoord;
layout(location=3) in vec4 in_tangent;
layout(location=4) in vec4 in_bitangent;
uniform mat4 u_view_projection;
uniform mat4 u_model_view;
uniform mat4 u_model;
uniform mat4 u_normal;
uniform vec3 u_camera_position;
void main()
{
gl_Position = u_view_projection * u_model * in_position;
gl_Position = globals.camera.model_view_projection_matrix * in_position;
}
......@@ -4,6 +4,7 @@
#extension GL_ARB_shading_language_include : require
#include <util/scene/material.glsl>
#include <util/globals.glsl>
in vec4 out_world_position;
in vec4 out_view_position;
......@@ -17,8 +18,6 @@ in vec2 out_texcoord;
uniform Material u_material;
uniform vec3 u_camera_position;
const vec3 luminance = vec3(0.299, 0.587, 0.114);
const float height_scale = 1;
......
#version 430
#extension GL_ARB_shading_language_include : require
#include <util/globals.glsl>
layout(location=0) in vec4 in_position;
layout(location=1) in vec4 in_normal;
layout(location=2) in vec2 in_texcoord;
layout(location=3) in vec4 in_tangent;
layout(location=4) in vec4 in_bitangent;
uniform mat4 u_view_projection;
uniform mat4 u_model_view;
uniform mat4 u_model;
uniform mat4 u_normal;
uniform vec3 u_camera_position;
out vec4 out_position;
out vec4 out_world_position;
out vec4 out_view_position;
......@@ -25,25 +23,25 @@ out vec2 out_texcoord;
void main()
{
out_position = in_position;
out_normal = u_model * in_normal;
out_normal = globals.camera.model_matrix * in_normal;
vec4 norm = normalize(in_normal);
out_tangent = normalize(in_tangent);
vec3 T = normalize((u_model * in_tangent).xyz);
vec3 B = normalize((u_model * in_bitangent).xyz);
vec3 N = normalize((u_model * in_normal).xyz);
vec3 T = normalize((globals.camera.model_matrix * in_tangent).xyz);
vec3 B = normalize((globals.camera.model_matrix * in_bitangent).xyz);
vec3 N = normalize((globals.camera.model_matrix * in_normal).xyz);
//out_tangent_space = mat3(out_tangent.xyz, in_bitangent.xyz, norm.xyz);
out_tangent_space = transpose(inverse(mat3(T,B,N)));
mat3 iTBN = transpose(mat3(T,B,N));
out_texcoord = in_texcoord;
out_world_position = u_model * in_position;
gl_Position = u_view_projection * out_world_position;
out_view_position = u_model_view * in_position;
out_world_position = globals.camera.model_matrix * in_position;
gl_Position = globals.camera.view_projection_matrix * out_world_position;
out_view_position = globals.camera.model_view_matrix * in_position;
out_camera_position = iTBN * u_camera_position;
out_camera_position = iTBN * globals.camera.position;
out_fragment_position = iTBN * in_position.xyz;
}
\ No newline at end of file
}
#ifndef INCLUDE_UTIL_GLOBALS_GLSL
#define INCLUDE_UTIL_GLOBALS_GLSL
struct CameraParameters
{
mat4 model_matrix;
mat4 view_matrix;
mat4 projection_matrix;
mat4 model_view_matrix;
mat4 view_projection_matrix;
mat4 inv_view_projection_matrix;
mat4 model_view_projection_matrix;
mat4 normal_matrix;
vec3 position;
};
struct TimeParameters
{
float delta;
float current;
};
struct ScreenParameters
{
vec2 size;
vec2 inv_size;
};
uniform struct
{
CameraParameters camera;
TimeParameters time;
ScreenParameters screen;
} globals;
#endif //!INCLUDE_UTIL_GLOBALS_GLSL
#include <core/state.h>
#include <core/res/resources.h>
#include <core/objects/skybox.h>
#include <components/PlayerController.h>
#include <core/rendering/mesh_drawable.h>
#include <core/rendering/batch_renderer.h>
#include <core/rendering/batch_render_list.h>
using namespace glare;
int main(int argc, char* argv[])
{
// Initialize engine from settings xml
core::state::initialize(files::asset("/preferences/default.xml"));
// Load a scene and attach it to the constant root
core::state::graph_root->attach(core::Resources::getInstance().getColladaFile(files::asset("meshes/scenery/benchmark_stfd_bunny_diff.dae")));
// Create a skybox
core::state::skybox->reset(core::Skybox::collectFilesFrom(files::asset("textures/ryfjallet/")));
// Add a PlayerController to move around
core::state::camera->owner()->makeComponent<component::PlayerController>();
// Render the scene with the pathtracer
core::state::mainLoop([](double delta) { core::state::draw(core::state::DrawMode::eGBuffer); });
}
\ No newline at end of file
......@@ -25,7 +25,5 @@ int main(int argc, char* argv[])
const auto pathtracer = std::make_unique<raytrace::Pathtracer>(core::state::graph_root);
// Render the scene with the pathtracer
core::state::mainLoop([&pathtracer](double delta) {
pathtracer->renderToScreen(core::state::window->getWidth(), core::state::window->getHeight());
});
core::state::mainLoop([&pathtracer](double delta) { pathtracer->draw(); });
}
\ No newline at end of file
......@@ -7,6 +7,7 @@
#include <core/audio/sound_source.h>
#include <core/objects/skybox.h>
#include <core/rendering/gbuffer.h>
#include <core/rendering/batch_render_list.h>
#include <core/res/resources.h>
#include <raytrace/tracer/pathtracer.h>
......@@ -30,7 +31,7 @@ const fs::path engine_settings_path = files::asset("/preferences/default.xml");
const fs::path pathtracer_settings_path = files::asset("/preferences/pathtracer_default.xml");
const fs::path skybox_files_path = files::asset("/textures/ryfjallet/");
const fs::path env_audio_path = files::asset("/audio/env.wav");
const fs::path startup_scene_path = files::asset("/meshes/scenery/House.dae");
const fs::path startup_scene_path = files::asset("/meshes/scenery/crystal.dae");
// My little all-enclosing pathtracer pointer
std::unique_ptr<raytrace::Pathtracer> pathtracer = nullptr;
......@@ -76,31 +77,17 @@ int main(int argc, char* argv[])
switch (render_mode)
{
case RenderMode::eLines:
{
gl::polygonMode(gl::Face::eFrontAndBack, gl::PolygonMode::eLine);
bool cull = gl::isEnabled(gl::EnableParameter::eCullFace);
gl::setEnabled(gl::EnableParameter::eCullFace, false);
core::state::graph_root->draw();
gl::setEnabled(gl::EnableParameter::eCullFace, cull);
gl::polygonMode(gl::Face::eFrontAndBack, gl::PolygonMode::eFill);
}
break;
core::state::draw(core::state::DrawMode::eWireframe);
break;
case RenderMode::eRaw:
core::state::graph_root->draw();
core::state::draw(core::state::DrawMode::eColors);
break;
case RenderMode::eGBuffer:
core::state::gbuffer->activate();
core::state::graph_root->draw();
core::state::gbuffer->draw();
core::state::draw(core::state::DrawMode::eGBuffer);
break;
case RenderMode::ePathtrace:
{
// If it's not manually initialized via the GUI button, initialize the Pathtracer here.
if (!pathtracer->collector())
pathtracer->reload(core::state::graph_root);
pathtracer->renderToScreen(core::state::window->getWidth(), core::state::window->getHeight());
}
break;
pathtracer->draw();
break;
}
drawDebugGui();
......
......@@ -188,7 +188,7 @@ namespace glare
template <typename NodeType, typename ComponentType>
std::shared_ptr<NodeType> Node<NodeType, ComponentType>::child(const std::string& name)
{
auto it = std::find(m_children.begin(), m_children.end(), [&name](const std::shared_ptr<NodeType>& node)
auto it = std::find_if(m_children.begin(), m_children.end(), [&name](const std::shared_ptr<NodeType>& node)
{
return node->name() == name;
});
......
......@@ -88,7 +88,7 @@ namespace glare
callAndTraverse(&GraphNodeComponent::onTransformChanged, &GraphNode::onAbsoluteTransformChanged);
}
const math::Transform &GraphNode::worldTransform() const
const math::Transform& GraphNode::worldTransform() const
{
return m_world_transform;
}
......
......@@ -14,7 +14,7 @@ namespace glare
enum class DrawMode
{
eShaded = 0,
eNoColor = 1
eShadowMap = 1
};
class GraphNode : public Node<GraphNode, GraphNodeComponent>
......
......@@ -96,26 +96,25 @@ namespace glare
void Camera::setProjection(Projection projection)
{
m_projection = projection;
}
glm::mat4 Camera::getViewProjection() const
{
return m_projection_matrix * m_view_matrix;
}
glm::mat4 Camera::getProjection() const
{
return m_projection_matrix;
}
glm::mat4 Camera::getView() const
{
return m_view_matrix;
}
glm::mat4 Camera::getScreenMatrix() const
}
glm::mat4 Camera::viewMatrix() const
{
return m_view_matrix;
}
glm::mat4 Camera::projectionMatrix() const
{
return m_projection_matrix;
}
glm::mat4 Camera::viewProjectionMatrix() const
{
return m_projection_matrix * m_view_matrix;
}
glm::mat4 Camera::screenMatrix() const
{
//translate the center (0, 0) to the bottom left and then scale it by 2 to stretch it over the screen. After that, scale it by the screen resolution.
return glm::translate(glm::vec3(-1, -1, 0)) *
glm::scale(glm::vec3(2 / static_cast<float>(m_width), 2 / static_cast<float>(m_height), 2));
}
......
......@@ -45,11 +45,11 @@ namespace glare
void setProjection(Projection projection);
glm::mat4 makeProjectionMatrix() const;
glm::mat4 getViewProjection() const;
glm::mat4 getProjection() const;
glm::mat4 getView() const;
glm::mat4 getScreenMatrix() const;
glm::mat4 viewMatrix() const;
glm::mat4 projectionMatrix() const;
glm::mat4 viewProjectionMatrix() const;
glm::mat4 screenMatrix() const;
float getAspectRatio() const;
float getWidth() const;
float getFieldOfView() const;
......@@ -61,6 +61,8 @@ namespace glare
float getDofSize() const;
private:
glm::mat4 makeProjectionMatrix() const;
bool m_adapt_to_screen = false;
float m_focus_distance = 10.f;
float m_dof_size = 50.f;
......
......@@ -5,6 +5,8 @@
#include <core/numeric/geometry.h>
#include "material.h"
#include "mesh.h"
#include <core/rendering/batch_render_list.h>
#include <core/rendering/batch_drawable.h>
namespace glare
{
......@@ -196,21 +198,19 @@ namespace glare
messaging::Handler::getInstance().submit(tags::light, LightUpdate{ m_id, this });
}
static std::unique_ptr<core::Material> m_material = std::make_unique<Material>("__light_mat", std::make_shared<Program>(std::vector<std::shared_ptr<Shader>>{
static auto light_shader = std::make_shared<Program>(std::vector<std::shared_ptr<Shader>>{
std::make_shared<Shader>(gl::ShaderType::eVertex, files::shader("lamp/lamp.vert")),
std::make_shared<Shader>(gl::ShaderType::eFragment, files::shader("lamp/lamp.frag"))
}));
static std::unique_ptr<Mesh> m_mesh = std::make_unique<Mesh>();
m_material->activate();
m_material->getShader()->uniform("u_view_projection", state::camera->getViewProjection());
m_material->getShader()->uniform("u_model_view", state::camera->getView()*owner()->worldTransform().matrix());
m_material->getShader()->uniform("u_model", glm::scale(owner()->worldTransform().matrix(), glm::vec3(0.1f)));
m_material->getShader()->uniform("u_color", m_color);
m_material->getShader()->uniform("u_normal", glm::inverse(glm::transpose(state::camera->getView()*owner()->worldTransform().matrix())));
m_mesh->draw();
});
static std::unique_ptr<Mesh> mesh = std::make_unique<Mesh>();
state::renderer->submit(light_shader, std::make_shared<BatchDrawable>([this](std::shared_ptr<Program> program)
{
program->uniform("u_color", m_color);
mesh->draw();
}), glm::scale(owner()->worldTransform().matrix(), glm::vec3(0.1f)));
} break;
case DrawMode::eNoColor:
case DrawMode::eShadowMap:
// Don't rebuild shadow maps if a shadow map is currently built to prevent recursive loops.
return;
default:
......
......@@ -14,16 +14,9 @@ namespace glare
std::atomic<size_t> Material::m_current_id = 0;
Material::Material(std::string name)
: Material(name, std::make_shared<Program>(std::vector<std::shared_ptr<Shader>>{
std::make_shared<Shader>(gl::ShaderType::eVertex, files::shader("simple/simple.vert")),
std::make_shared<Shader>(gl::ShaderType::eFragment, files::shader("simple/simple.frag"))
}))
: m_name(name), m_material_id(m_current_id++)
{ }
Material::Material(std::string name, std::shared_ptr<Program> shader)
: m_material_id(m_current_id++), m_name(name), m_shader(shader)
{}
Material::~Material()
{
......@@ -62,45 +55,41 @@ namespace glare
ImGui::PopID();
}
void Material::activate() const
void Material::uniformUpdate(const Program& program, const std::string& name) const
{
m_shader->use();
m_shader->uniform("u_material.base.value", base.value);
program.uniform(name+".base.value", base.value);
if (base.texture)
m_shader->uniform("u_material.base.texture", base.texture->makeTextureResident());
m_shader->uniform("u_material.base.texture_available", static_cast<uint32_t>(static_cast<bool>(base.texture)));
program.uniform(name+".base.texture", base.texture->makeTextureResident());
program.uniform(name+".base.texture_available", static_cast<uint32_t>(static_cast<bool>(base.texture)));
m_shader->uniform("u_material.roughness.value", roughness.value);
program.uniform(name+".roughness.value", roughness.value);
if (base.texture)
m_shader->uniform("u_material.roughness.texture", roughness.texture->makeTextureResident());
m_shader->uniform("u_material.roughness.texture_available", static_cast<uint32_t>(static_cast<bool>(roughness.texture)));
program.uniform(name+".roughness.texture", roughness.texture->makeTextureResident());
program.uniform(name+".roughness.texture_available", static_cast<uint32_t>(static_cast<bool>(roughness.texture)));
m_shader->uniform("u_material.metallic.value", metallic.value);
program.uniform(name+".metallic.value", metallic.value);
if (base.texture)
m_shader->uniform("u_material.metallic.texture", metallic.texture->makeTextureResident());
m_shader->uniform("u_material.metallic.texture_available", static_cast<uint32_t>(static_cast<bool>(metallic.texture)));
program.uniform(name+".metallic.texture", metallic.texture->makeTextureResident());
program.uniform(name+".metallic.texture_available", static_cast<uint32_t>(static_cast<bool>(metallic.texture)));
m_shader->uniform("u_material.transmission.value", transmissive.value);
program.uniform(name+".transmission.value", transmissive.value);
if (base.texture)
m_shader->uniform("u_material.transmission.texture", transmissive.texture->makeTextureResident());
m_shader->uniform("u_material.transmission.texture_available", static_cast<uint32_t>(static_cast<bool>(transmissive.texture)));
program.uniform(name+".transmission.texture", transmissive.texture->makeTextureResident());
program.uniform(name+".transmission.texture_available", static_cast<uint32_t>(static_cast<bool>(transmissive.texture)));
m_shader->uniform("u_material.emission.value", emission.value);
program.uniform(name+".emission.value", emission.value);
if (base.texture)
m_shader->uniform("u_material.emission.texture", emission.texture->makeTextureResident());
m_shader->uniform("u_material.emission.texture_available", static_cast<uint32_t>(static_cast<bool>(emission.texture)));
if (map_normal)
m_shader->uniform("u_material.normal_map", map_normal->makeTextureResident());
if (map_displacement)
m_shader->uniform("u_material.displacement_map", map_displacement->makeTextureResident());
m_shader->uniform("u_material.has_displacement_map", static_cast<bool>(map_displacement));
m_shader->uniform("u_material.has_normal_map", static_cast<bool>(emission.texture));
m_shader->uniform("u_material.ior", index_of_refraction);
m_shader->uniform("u_camera_position", glm::vec3(state::camera->owner()->worldTransform().position));
program.uniform(name+".emission.texture", emission.texture->makeTextureResident());
program.uniform(name+".emission.texture_available", static_cast<uint32_t>(static_cast<bool>(emission.texture)));
if (map_normal)
program.uniform(name+".normal_map", map_normal->makeTextureResident());
if (map_displacement)
program.uniform(name+".displacement_map", map_displacement->makeTextureResident());
program.uniform(name+".has_displacement_map", static_cast<bool>(map_displacement));
program.uniform(name+".has_normal_map", static_cast<bool>(emission.texture));
program.uniform(name+".ior", index_of_refraction);
}
const std::string& Material::getName() const
......@@ -108,11 +97,6 @@ namespace glare
return m_name;
}
std::shared_ptr<Program> Material::getShader() const
{
return m_shader;
}
size_t Material::id() const
{
return m_material_id;
......
......@@ -31,17 +31,14 @@ namespace glare
{
public:
explicit Material(std::string name);
Material(std::string name, std::shared_ptr<Program> shader);
~Material();
void activate() const;
const std::string& getName() const;
void gui();
void uniformUpdate(const Program& program, const std::string& name) const;
size_t id() const;
std::shared_ptr<Program> getShader() const;
MaterialParameter3f base = { glm::vec3(1) };
MaterialParameter1f roughness = { 0 };
......@@ -58,16 +55,7 @@ namespace glare
const size_t m_material_id;
std::string m_name;
std::shared_ptr<Program> m_shader;
};
inline static std::shared_ptr<Material> makeDepthMaterial()
{
return std::make_shared<Material>("only_depth", std::make_shared<Program>(std::vector<std::shared_ptr<Shader>>{
std::make_shared<Shader>(gl::ShaderType::eVertex, files::shader("simple/simple.vert")),
std::make_shared<Shader>(gl::ShaderType::eFragment, files::shader("simple/no_color.frag"))
}));
}
}
}
#endif //INCLUDE_MATERIAL_H
......@@ -182,7 +182,6 @@ namespace glare
void Mesh::draw()
{
m_vertex_array->bind();
m_element_array_buffer->bind();
gl::drawElements(gl::PrimitiveType::eTriangles, m_indices.size(), gl::Type::eUInt, nullptr);
m_vertex_array->unbind();
}
......
......@@ -40,14 +40,14 @@ namespace glare
gl::viewport(0, 0, m_size.x, m_size.y);
gl::clear(gl::ClearBufferBits::eColor | gl::ClearBufferBits::eDepth);
gl::setEnabled(gl::EnableParameter::eCullFace, false);
scene_root->draw(DrawMode::eNoColor);
scene_root->draw(DrawMode::eShadowMap);
gl::setEnabled(gl::EnableParameter::eCullFace, true);
core::state::camera = main_camera;
gl::viewport(0, 0, static_cast<unsigned>(core::state::camera->getWidth()),
static_cast<unsigned>(core::state::camera->getWidth() / core::state::camera->getAspectRatio()));
gl::bindFramebuffer(gl::FramebufferTarget::eDefault, current_fb);
m_matrix = m_bias_matrix * m_camera->getViewProjection();
m_matrix = m_bias_matrix * m_camera->viewProjectionMatrix();
gl::generateTextureMipmap(m_framebuffer->depthAttachment().id());
}
......
......@@ -5,6 +5,7 @@
#include <util/images.h>
#include <core/state.h>
#include <core/rendering/batch_render_list.h>
namespace glare
{
......@@ -65,6 +66,16 @@ namespace glare
const auto frag = std::make_shared<Shader>(gl::ShaderType::eFragment, files::shader("/cubemap/triangle_skybox.frag"));
m_cubemap_shader = std::make_shared<Program>(std::vector<std::shared_ptr<Shader>>{ frag, vert });
m_cubemap_shader->uniform("u_cube_map", makeResident());
m_drawable = std::make_shared<core::BatchDrawable>([this](std::shared_ptr<Program> program) {
gl::depthMask(false);
gl::cullFace(gl::Face::eFront);
gl::depthFunc(gl::CompareFunc::eLessEqual);