Commit 6e14f7b3 authored by Florian Oetke's avatar Florian Oetke
Browse files

API for debug drawing [fixes 34]; fixed too small point light volumn

parent dbeedbdd
......@@ -3,6 +3,10 @@ shader: = shader/
vert_shader:blit = shader/bin/fullscreen.vert.spv
frag_shader:blit = shader/bin/blit.frag.spv
vert_shader:debug_draw = shader/bin/debug_draw.vert.spv
frag_shader:debug_draw = shader/bin/debug_draw.frag.spv
vert_shader:light_directional = shader/bin/fullscreen.vert.spv
frag_shader:light_directional = shader/bin/light_directional.frag.spv
......
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
#include "global_uniforms.glsl"
#include "normal_encoding.glsl"
layout(early_fragment_tests) in;
layout(location = 0) in vec3 color;
layout(location = 0) out vec4 color_out;
void main() {
color_out = vec4(color, 1.0);
}
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
#include "global_uniforms.glsl"
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
layout(location = 0) out vec3 out_color;
out gl_PerVertex {
vec4 gl_Position;
};
void main() {
gl_Position = global_uniforms.view_proj_mat * vec4(position, 1.0);
out_color = color;
}
......@@ -44,7 +44,7 @@ void main() {
float att = 1.0 / max(dist*dist, 0.01*0.01);
float intensity = model_uniforms.light_color.a * calc_att(dist);
intensity = max(0.0, intensity-0.005) / (1.0 - 0.01);
intensity = max(0.0, intensity-0.01) / (1.0 - 0.01);
if(intensity<=0.0) {
out_color = vec4(0,0,0,0);
out_color_diff = vec4(0,0,0,0);
......
......@@ -4,6 +4,7 @@
#include <mirrage/renderer/pass/animation_pass.hpp>
#include <mirrage/renderer/pass/blit_pass.hpp>
#include <mirrage/renderer/pass/bloom_pass.hpp>
#include <mirrage/renderer/pass/debug_draw_pass.hpp>
#include <mirrage/renderer/pass/deferred_pass.hpp>
#include <mirrage/renderer/pass/frustum_culling_pass.hpp>
#include <mirrage/renderer/pass/gen_mipmap_pass.hpp>
......@@ -39,6 +40,7 @@ namespace mirrage {
renderer::make_pass_factory<renderer::Taa_pass_factory>(),
renderer::make_pass_factory<renderer::Bloom_pass_factory>(),
renderer::make_pass_factory<renderer::Tone_mapping_pass_factory>(),
renderer::make_pass_factory<renderer::Debug_draw_pass_factory>(),
renderer::make_pass_factory<renderer::Blit_pass_factory>(),
renderer::make_pass_factory<renderer::Gui_pass_factory>())))
{
......
......@@ -359,6 +359,9 @@ namespace mirrage {
}
}
_meta_system.renderer().debug_draw({renderer::Debug_geometry{{0, 1, 0}, {0, 5, 0}, {1, 0, 0}},
renderer::Debug_geometry{{1, 1, 0}, {1, 5, 0}, {0, 0, 1}}});
_meta_system.draw();
}
void Test_screen::_draw_settings_window()
......
......@@ -12,6 +12,7 @@ add_library(mirrage_renderer STATIC
src/pass/animation_pass.cpp
src/pass/blit_pass.cpp
src/pass/bloom_pass.cpp
src/pass/debug_draw_pass.cpp
src/pass/deferred_geometry_subpass.cpp
src/pass/deferred_lighting_subpass.cpp
src/pass/deferred_pass.cpp
......
......@@ -54,6 +54,7 @@ namespace mirrage::renderer {
bool dynamic_shadows = false;
int debug_gi_layer = -1;
bool debug_geometry = true;
};
#ifdef sf2_structDef
......@@ -191,6 +192,14 @@ namespace mirrage::renderer {
void save_settings() { _factory->save_settings(); }
void settings(const Renderer_settings& s, bool apply = true) { _factory->settings(s, apply); }
void debug_draw(const std::vector<Debug_geometry>& lines)
{
if(_factory->settings().debug_geometry) {
auto& queue = _frame_data.debug_geometry_queue;
queue.insert(queue.end(), lines.begin(), lines.end());
}
}
auto profiler() const noexcept -> auto& { return _profiler; }
auto profiler() noexcept -> auto& { return _profiler; }
......
......@@ -12,8 +12,9 @@ namespace mirrage::renderer {
std::int32_t mip_levels;
vk::Format depth_format;
graphic::Render_target_2D depth; // depth-buffer
graphic::Render_target_2D prev_depth; // depth-buffer from previouse frame
graphic::Render_target_2D depth; // depth-buffer
graphic::Render_target_2D prev_depth; // depth-buffer from previouse frame
graphic::Render_target_2D depth_buffer; // actual depth-buffer for Z-tests
vk::Format albedo_mat_id_format;
graphic::Render_target_2D albedo_mat_id; // RGB of objects + Material-ID
......
#pragma once
#include <mirrage/renderer/deferred_renderer.hpp>
#include <mirrage/graphic/render_pass.hpp>
#include <mirrage/graphic/streamed_buffer.hpp>
namespace mirrage::renderer {
class Debug_draw_pass : public Render_pass {
public:
Debug_draw_pass(Deferred_renderer&, graphic::Render_target_2D& src);
void update(util::Time dt) override;
void draw(Frame_data&) override;
auto name() const noexcept -> const char* override { return "Debug_draw"; }
private:
Deferred_renderer& _renderer;
graphic::Streamed_buffer _vertices;
graphic::Framebuffer _framebuffer;
graphic::Render_pass _render_pass;
};
class Debug_draw_pass_factory : public Render_pass_factory {
public:
auto create_pass(Deferred_renderer&, ecs::Entity_manager&, Engine&, bool& write_first_pp_buffer)
-> std::unique_ptr<Render_pass> override;
auto rank_device(vk::PhysicalDevice, util::maybe<std::uint32_t> graphics_queue, int current_score)
-> int override;
void configure_device(vk::PhysicalDevice,
util::maybe<std::uint32_t> graphics_queue,
graphic::Device_create_info&) override;
};
} // namespace mirrage::renderer
......@@ -36,8 +36,6 @@ namespace mirrage::renderer {
private:
Deferred_renderer& _renderer;
graphic::Render_target_2D _depth;
Deferred_geometry_subpass _gpass;
Deferred_lighting_subpass _lpass;
......
......@@ -94,14 +94,22 @@ namespace mirrage::renderer {
}
};
struct Debug_geometry {
glm::vec3 start;
glm::vec3 end;
util::Rgb color;
};
class Frame_data {
public:
vk::CommandBuffer main_command_buffer;
vk::DescriptorSet global_uniform_set;
std::size_t swapchain_image;
std::vector<Geometry> geometry_queue;
std::vector<Light> light_queue;
std::vector<Geometry> geometry_queue;
std::vector<Light> light_queue;
std::vector<Debug_geometry> debug_geometry_queue;
auto partition_geometry(std::uint32_t mask) -> util::vector_range<Geometry>;
};
......
......@@ -160,8 +160,6 @@ namespace mirrage::renderer {
_frame_data.main_command_buffer = main_command_buffer;
_frame_data.global_uniform_set = *_global_uniform_descriptor_set;
_frame_data.swapchain_image = _factory->_aquire_next_image();
_frame_data.geometry_queue.clear();
_frame_data.light_queue.clear();
// draw subpasses
for(auto& pass : _passes) {
......@@ -170,6 +168,10 @@ namespace mirrage::renderer {
pass->draw(_frame_data);
}
_frame_data.geometry_queue.clear();
_frame_data.light_queue.clear();
_frame_data.debug_geometry_queue.clear();
// reset cached camera state
_active_camera = util::nothing;
}
......
......@@ -58,6 +58,13 @@ namespace mirrage::renderer {
vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferSrc
| vk::ImageUsageFlagBits::eTransferDst,
vk::ImageAspectFlagBits::eColor)
, depth_buffer(
device,
{width, height},
1,
device.get_depth_format(),
vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eInputAttachment,
vk::ImageAspectFlagBits::eDepth)
, albedo_mat_id_format(device.get_texture_rgba_format().get_or_throw("No rgba-format supported"))
, albedo_mat_id(device,
......
......@@ -61,7 +61,9 @@ namespace mirrage::renderer {
float Point_light_comp::calc_radius() const
{
constexpr auto cutoff = 0.01f;
return util::min(20.f, _source_radius.value() * std::sqrt(_intensity / 10000.f / cutoff));
auto r = _source_radius.value() * std::sqrt(_intensity / 10000.f / cutoff);
return util::min(20.f, r * 1.3f); // factor to compensate for coarse light volumn
}
void load_component(ecs::Deserializer& state, Point_light_comp& comp)
......
#include <mirrage/renderer/pass/debug_draw_pass.hpp>
namespace mirrage::renderer {
namespace {
struct Debug_vertex {
glm::vec3 position;
util::Rgb color;
};
auto build_render_pass(Deferred_renderer& renderer,
graphic::Render_target_2D& color_target,
graphic::Framebuffer& out_framebuffer)
{
auto builder = renderer.device().create_render_pass_builder();
auto color = builder.add_attachment(
vk::AttachmentDescription{vk::AttachmentDescriptionFlags{},
renderer.gbuffer().color_format,
vk::SampleCountFlagBits::e1,
vk::AttachmentLoadOp::eLoad,
vk::AttachmentStoreOp::eStore,
vk::AttachmentLoadOp::eDontCare,
vk::AttachmentStoreOp::eDontCare,
vk::ImageLayout::eShaderReadOnlyOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal});
auto depth = builder.add_attachment(
vk::AttachmentDescription{vk::AttachmentDescriptionFlags{},
renderer.gbuffer().depth_format,
vk::SampleCountFlagBits::e1,
vk::AttachmentLoadOp::eLoad,
vk::AttachmentStoreOp::eStore,
vk::AttachmentLoadOp::eDontCare,
vk::AttachmentStoreOp::eDontCare,
vk::ImageLayout::eShaderReadOnlyOptimal,
vk::ImageLayout::eShaderReadOnlyOptimal});
auto pipeline = graphic::Pipeline_description{};
pipeline.input_assembly.topology = vk::PrimitiveTopology::eLineList;
pipeline.multisample = vk::PipelineMultisampleStateCreateInfo{};
pipeline.color_blending = vk::PipelineColorBlendStateCreateInfo{};
pipeline.depth_stencil = vk::PipelineDepthStencilStateCreateInfo{
vk::PipelineDepthStencilStateCreateFlags{}, true, false, vk::CompareOp::eLess};
pipeline.rasterization.cullMode = vk::CullModeFlagBits::eNone;
pipeline.rasterization.depthBiasEnable = true;
pipeline.rasterization.depthBiasClamp = 0.f;
pipeline.rasterization.depthBiasConstantFactor = 0.01f;
if(renderer.device().physical_device().getFeatures().wideLines) {
pipeline.rasterization.lineWidth = 4.f;
}
pipeline.add_descriptor_set_layout(renderer.global_uniforms_layout());
pipeline.vertex<Debug_vertex>(0, false, 0, &Debug_vertex::position, 1, &Debug_vertex::color);
auto& pass =
builder.add_subpass(pipeline).color_attachment(color).depth_stencil_attachment(depth);
pass.stage("draw"_strid)
.shader("frag_shader:debug_draw"_aid, graphic::Shader_stage::fragment)
.shader("vert_shader:debug_draw"_aid, graphic::Shader_stage::vertex);
auto render_pass = builder.build();
auto attachments = std::array<graphic::Framebuffer_attachment_desc, 2>{
{{color_target.view(0), 1.f},
{renderer.gbuffer().depth_buffer.view(0), util::Rgba(1.f)}}};
out_framebuffer =
builder.build_framebuffer(attachments, color_target.width(), color_target.height());
return render_pass;
}
} // namespace
Debug_draw_pass::Debug_draw_pass(Deferred_renderer& renderer, graphic::Render_target_2D& src)
: _renderer(renderer)
, _vertices(renderer.device(), 64 * sizeof(Debug_vertex), vk::BufferUsageFlagBits::eVertexBuffer)
, _render_pass(build_render_pass(renderer, src, _framebuffer))
{
}
void Debug_draw_pass::update(util::Time) {}
void Debug_draw_pass::draw(Frame_data& frame)
{
if(frame.debug_geometry_queue.empty())
return;
auto vert_count = gsl::narrow<std::uint32_t>(frame.debug_geometry_queue.size() * 2);
_vertices.resize(std::int32_t(vert_count * sizeof(Debug_vertex)));
_vertices.update_objects<Debug_vertex>(0, [&](gsl::span<Debug_vertex> out) {
auto i = 0;
for(auto& g : frame.debug_geometry_queue) {
out[i++] = {g.start, g.color};
out[i++] = {g.end, g.color};
}
});
_vertices.flush(frame.main_command_buffer,
vk::PipelineStageFlagBits::eVertexInput,
vk::AccessFlagBits::eVertexAttributeRead);
_render_pass.execute(frame.main_command_buffer, _framebuffer, [&] {
_render_pass.bind_descriptor_set(0, frame.global_uniform_set);
auto buffer = _vertices.read_buffer();
auto offset = vk::DeviceSize(0);
frame.main_command_buffer.bindVertexBuffers(0, 1, &buffer, &offset);
frame.main_command_buffer.draw(vert_count, 1, 0, 0);
});
}
auto Debug_draw_pass_factory::create_pass(Deferred_renderer& renderer,
ecs::Entity_manager&,
Engine&,
bool& write_first_pp_buffer) -> std::unique_ptr<Render_pass>
{
auto& color_src = !write_first_pp_buffer ? renderer.gbuffer().colorA : renderer.gbuffer().colorB;
return std::make_unique<Debug_draw_pass>(renderer, color_src);
}
auto Debug_draw_pass_factory::rank_device(vk::PhysicalDevice,
util::maybe<std::uint32_t>,
int current_score) -> int
{
return current_score;
}
void Debug_draw_pass_factory::configure_device(vk::PhysicalDevice dev,
util::maybe<std::uint32_t>,
graphic::Device_create_info& dci)
{
dci.features.wideLines = dev.getFeatures().wideLines;
}
} // namespace mirrage::renderer
......@@ -195,18 +195,12 @@ namespace mirrage::renderer {
graphic::Render_target_2D& color_target,
graphic::Render_target_2D& color_target_diff)
: _renderer(renderer)
, _depth(renderer.device(),
{color_target.width(), color_target.height()},
1,
renderer.device().get_depth_format(),
vk::ImageUsageFlagBits::eDepthStencilAttachment | vk::ImageUsageFlagBits::eInputAttachment,
vk::ImageAspectFlagBits::eDepth)
, _gpass(renderer, entities)
, _lpass(renderer, entities, _depth)
, _lpass(renderer, entities, renderer.gbuffer().depth_buffer)
, _render_pass(build_render_pass(renderer,
color_target,
color_target_diff,
_depth,
renderer.gbuffer().depth_buffer,
renderer.device().get_depth_format(),
_gpass,
_lpass,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment