Skip to content

Commit

Permalink
gh-43: add compute pipeline support & bloom and tonemap on compute now
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorOrachyov committed Oct 31, 2023
1 parent ef55c81 commit c115d31
Show file tree
Hide file tree
Showing 109 changed files with 2,352 additions and 2,194 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
template/logs
template/debug
template/cache
template/.wgengine

# logs, caches and debug data
games/flappyowl/logs
games/flappyowl/debug
games/flappyowl/cache
games/flappyowl/.wgengine
3 changes: 3 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ add_library(wmoge STATIC
gfx/gfx_defs.hpp
gfx/gfx_desc_set.cpp
gfx/gfx_desc_set.hpp
gfx/gfx_ctx.cpp
gfx/gfx_ctx.hpp
gfx/gfx_driver.hpp
gfx/gfx_dynamic_buffers.cpp
Expand Down Expand Up @@ -301,6 +302,8 @@ add_library(wmoge STATIC
render/geometry/pass_gbuffer.hpp
render/post_process/pass_bloom.cpp
render/post_process/pass_bloom.hpp
render/post_process/pass_composition.cpp
render/post_process/pass_composition.hpp
render/post_process/pass_tonemap.cpp
render/post_process/pass_tonemap.hpp
resource/audio_stream.cpp
Expand Down
2 changes: 1 addition & 1 deletion engine/core/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace wmoge {
std::time_t time = engine->get_time();
std::stringstream log_file_name;

log_file_name << "root://logs/log_"
log_file_name << "logs://log_"
<< m_name << " "
<< engine->get_time_formatted("%Y-%m-%d %H-%M-%S", time)
<< ".log";
Expand Down
36 changes: 36 additions & 0 deletions engine/gfx/gfx_ctx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**********************************************************************************/
/* Wmoge game engine */
/* Available at github https://github.com/EgorOrachyov/wmoge */
/**********************************************************************************/
/* MIT License */
/* */
/* Copyright (c) 2023 Egor Orachyov */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a copy */
/* of this software and associated documentation files (the "Software"), to deal */
/* in the Software without restriction, including without limitation the rights */
/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
/* copies of the Software, and to permit persons to whom the Software is */
/* furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all */
/* copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/**********************************************************************************/

#include "gfx_ctx.hpp"

namespace wmoge {

Vec3i GfxCtx::group_size(int x, int y, int local_size) {
return Vec3i(int(Math::div_up(x, local_size)), int(Math::div_up(y, local_size)), 1);
}

}// namespace wmoge
24 changes: 16 additions & 8 deletions engine/gfx/gfx_ctx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ namespace wmoge {
virtual void update_texture_2d_array(const Ref<GfxTexture>& texture, int mip, int slice, Rect2i region, const Ref<Data>& data) = 0;
virtual void update_texture_cube(const Ref<GfxTexture>& texture, int mip, int face, Rect2i region, const Ref<Data>& data) = 0;

virtual void* map_vert_buffer(const Ref<GfxVertBuffer>& buffer) = 0;
virtual void* map_index_buffer(const Ref<GfxIndexBuffer>& buffer) = 0;
virtual void* map_uniform_buffer(const Ref<GfxUniformBuffer>& buffer) = 0;
virtual void* map_storage_buffer(const Ref<GfxStorageBuffer>& buffer) = 0;
virtual void unmap_vert_buffer(const Ref<GfxVertBuffer>& buffer) = 0;
virtual void unmap_index_buffer(const Ref<GfxIndexBuffer>& buffer) = 0;
virtual void unmap_uniform_buffer(const Ref<GfxUniformBuffer>& buffer) = 0;
virtual void unmap_storage_buffer(const Ref<GfxStorageBuffer>& buffer) = 0;
virtual void* map_vert_buffer(const Ref<GfxVertBuffer>& buffer) = 0;
virtual void* map_index_buffer(const Ref<GfxIndexBuffer>& buffer) = 0;
virtual void* map_uniform_buffer(const Ref<GfxUniformBuffer>& buffer) = 0;
virtual void* map_storage_buffer(const Ref<GfxStorageBuffer>& buffer) = 0;

virtual void unmap_vert_buffer(const Ref<GfxVertBuffer>& buffer) = 0;
virtual void unmap_index_buffer(const Ref<GfxIndexBuffer>& buffer) = 0;
virtual void unmap_uniform_buffer(const Ref<GfxUniformBuffer>& buffer) = 0;
virtual void unmap_storage_buffer(const Ref<GfxStorageBuffer>& buffer) = 0;

virtual void barrier_image(const Ref<GfxTexture>& texture, GfxTexBarrierType barrier_type) = 0;
virtual void barrier_buffer(const Ref<GfxStorageBuffer>& buffer) = 0;

virtual void begin_render_pass(const GfxRenderPassDesc& pass_desc, const StringId& name = StringId()) = 0;
virtual void bind_target(const Ref<Window>& window) = 0;
Expand All @@ -89,12 +93,14 @@ namespace wmoge {
virtual void clear(int target, const Vec4f& color) = 0;
virtual void clear(float depth, int stencil) = 0;
virtual bool bind_pipeline(const Ref<GfxPipeline>& pipeline) = 0;
virtual bool bind_comp_pipeline(const Ref<GfxCompPipeline>& pipeline) = 0;
virtual void bind_vert_buffer(const Ref<GfxVertBuffer>& buffer, int index, int offset = 0) = 0;
virtual void bind_index_buffer(const Ref<GfxIndexBuffer>& buffer, GfxIndexType index_type, int offset = 0) = 0;
virtual void bind_desc_set(const Ref<GfxDescSet>& set, int index) = 0;
virtual void bind_desc_sets(const ArrayView<GfxDescSet*>& sets, int offset = 0) = 0;
virtual void draw(int vertex_count, int base_vertex, int instance_count) = 0;
virtual void draw_indexed(int index_count, int base_vertex, int instance_count) = 0;
virtual void dispatch(Vec3i group_count) = 0;
virtual void end_render_pass() = 0;

virtual void execute(const std::function<void(GfxCtx* thread_ctx)>& functor) = 0;
Expand All @@ -108,6 +114,8 @@ namespace wmoge {

[[nodiscard]] virtual const Mat4x4f& clip_matrix() const = 0;
[[nodiscard]] virtual GfxCtxType ctx_type() const = 0;

static Vec3i group_size(int x, int y, int local_size);
};

/**
Expand Down
20 changes: 19 additions & 1 deletion engine/gfx/gfx_defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ namespace wmoge {
GpuDedicated
};

/** @brief Texture sub region */
struct GfxTexRegion {
int base_mip = 0;
int num_mips = 0;
int base_array_slice = 0;
int num_array_slices = 0;
};

/** @brief Texture manual barrier type */
enum class GfxTexBarrierType : int {
RenderTarget,
Sampling,
Storage
};

/** @brief Texture type */
enum class GfxTex : int {
Unknown = 0,
Expand All @@ -115,6 +130,8 @@ namespace wmoge {
DepthTarget = 2,
/** Texture can be used as render target color attachment */
ColorTarget = 3,
/** Texture can be used in compute shaders as storage image */
Storage = 3,
/** Texture can be sampled within shader */
Sampling = 4
};
Expand Down Expand Up @@ -432,7 +449,8 @@ namespace wmoge {
Unknown = 0,
SampledTexture,
UniformBuffer,
StorageBuffer
StorageBuffer,
StorageImage
};

/** @brief How to bind resource to gfx pipeline */
Expand Down
11 changes: 7 additions & 4 deletions engine/gfx/gfx_driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ namespace wmoge {
virtual Ref<GfxUniformBuffer> make_uniform_buffer(int size, GfxMemUsage usage, const StringId& name = StringId()) = 0;
virtual Ref<GfxStorageBuffer> make_storage_buffer(int size, GfxMemUsage usage, const StringId& name = StringId()) = 0;
virtual Ref<GfxShader> make_shader(std::string vertex, std::string fragment, const GfxDescSetLayouts& layouts, const StringId& name = StringId()) = 0;
virtual Ref<GfxShader> make_shader(std::string compute, const GfxDescSetLayouts& layouts, const StringId& name = StringId()) = 0;
virtual Ref<GfxShader> make_shader(Ref<Data> code, const StringId& name = StringId()) = 0;
virtual Ref<GfxTexture> make_texture_2d(int width, int height, int mips, GfxFormat format, GfxTexUsages usages, GfxMemUsage mem_usage, GfxTexSwizz swizz, const StringId& name = StringId()) = 0;
virtual Ref<GfxTexture> make_texture_2d_array(int width, int height, int mips, int slices, GfxFormat format, GfxTexUsages usages, GfxMemUsage mem_usage, const StringId& name = StringId()) = 0;
virtual Ref<GfxTexture> make_texture_cube(int width, int height, int mips, GfxFormat format, GfxTexUsages usages, GfxMemUsage mem_usage, const StringId& name = StringId()) = 0;
virtual Ref<GfxSampler> make_sampler(const GfxSamplerDesc& desc, const StringId& name = StringId()) = 0;
virtual Ref<GfxPipeline> make_pipeline(const GfxPipelineState& state, const StringId& name = StringId()) = 0;
virtual Ref<GfxCompPipeline> make_comp_pipeline(const GfxCompPipelineState& state, const StringId& name = StringId()) = 0;
virtual Ref<GfxRenderPass> make_render_pass(const GfxRenderPassDesc& pass_desc, const StringId& name = StringId()) = 0;
virtual Ref<GfxDynVertBuffer> make_dyn_vert_buffer(int chunk_size, const StringId& name = StringId()) = 0;
virtual Ref<GfxDynIndexBuffer> make_dyn_index_buffer(int chunk_size, const StringId& name = StringId()) = 0;
Expand All @@ -95,10 +97,11 @@ namespace wmoge {
virtual void prepare_window(const Ref<Window>& window) = 0;
virtual void swap_buffers(const Ref<Window>& window) = 0;

[[nodiscard]] virtual class GfxCtx* ctx_immediate() = 0;
[[nodiscard]] virtual class GfxCtx* ctx_async() = 0;
[[nodiscard]] virtual GfxPipelineCache* pso_cache() = 0;
[[nodiscard]] virtual GfxVertFormatCache* vert_fmt_cache() = 0;
[[nodiscard]] virtual class GfxCtx* ctx_immediate() = 0;
[[nodiscard]] virtual class GfxCtx* ctx_async() = 0;
[[nodiscard]] virtual GfxPipelineCache* pso_cache() = 0;
[[nodiscard]] virtual GfxCompPipelineCache* comp_pso_cache() = 0;
[[nodiscard]] virtual GfxVertFormatCache* vert_fmt_cache() = 0;

[[nodiscard]] virtual GfxUniformPool* uniform_pool() = 0;
[[nodiscard]] virtual GfxDynVertBuffer* dyn_vert_buffer() = 0;
Expand Down
7 changes: 7 additions & 0 deletions engine/gfx/gfx_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ namespace wmoge {
return static_cast<std::size_t>(Crc32::hash(this, sizeof(GfxPipelineState)));
}

bool GfxCompPipelineState::operator==(const GfxCompPipelineState& other) const {
return shader == other.shader;
}
std::size_t GfxCompPipelineState::hash() const {
return static_cast<std::size_t>(Crc32::hash(this, sizeof(GfxCompPipelineState)));
}

}// namespace wmoge
35 changes: 35 additions & 0 deletions engine/gfx/gfx_pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ namespace wmoge {
int blending; // = false;
};

/**
* @class GfxCompPipelineState
* @brief Gfx compute pipeline state description
*/
struct GfxCompPipelineState {
GfxCompPipelineState() = default;
bool operator==(const GfxCompPipelineState& other) const;
std::size_t hash() const;

Ref<GfxShader> shader;// = nullptr;
};

/**
* @class GfxPipeline
* @brief Represents created and compiled graphics pipeline state object
Expand All @@ -86,6 +98,22 @@ namespace wmoge {
virtual const GfxPipelineState& state() const = 0;
};

/**
* @class GfxCompPipeline
* @brief Represents created and compiled compute pipeline state object
*
* Pipeline is a compete object which can be directly bound to the command list for the compute dispatch.
* Pipeline creation is asynchronous and done in the background.
* When pipeline created it will be used in the rendering.
*/
class GfxCompPipeline : public GfxResource {
public:
~GfxCompPipeline() override = default;
virtual GfxPipelineStatus status() const = 0;
virtual std::string message() const = 0;
virtual const GfxCompPipelineState& state() const = 0;
};

}// namespace wmoge

namespace std {
Expand All @@ -97,6 +125,13 @@ namespace std {
}
};

template<>
struct hash<wmoge::GfxCompPipelineState> {
std::size_t operator()(const wmoge::GfxCompPipelineState& desc) const {
return desc.hash();
}
};

}// namespace std

#endif//WMOGE_GFX_PIPELINE_HPP
14 changes: 14 additions & 0 deletions engine/gfx/gfx_pipeline_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ namespace wmoge {
m_cache[state] = pipeline;
}

std::optional<Ref<GfxCompPipeline>> GfxCompPipelineCache::get(const GfxCompPipelineState& state) {
std::lock_guard guard(m_mutex);
auto it = m_cache.find(state);
if (it != m_cache.end()) {
return it->second;
}
return std::nullopt;
}

void GfxCompPipelineCache::add(const GfxCompPipelineState& state, const Ref<GfxCompPipeline>& pipeline) {
std::lock_guard guard(m_mutex);
m_cache[state] = pipeline;
}

}// namespace wmoge
21 changes: 20 additions & 1 deletion engine/gfx/gfx_pipeline_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace wmoge {

/**
* @class GfxPipelineCache
* @brief Runtime cache of pipelines
* @brief Runtime cache of graphics pipelines
*/
class GfxPipelineCache final {
public:
Expand All @@ -56,6 +56,25 @@ namespace wmoge {
SpinMutex m_mutex;
};

/**
* @class GfxCompPipelineCache
* @brief Runtime cache of compute pipelines
*/
class GfxCompPipelineCache final {
public:
GfxCompPipelineCache() = default;
GfxCompPipelineCache(const GfxCompPipelineCache&) = delete;
GfxCompPipelineCache(GfxCompPipelineCache&&) = delete;
~GfxCompPipelineCache() = default;

std::optional<Ref<GfxCompPipeline>> get(const GfxCompPipelineState& state);
void add(const GfxCompPipelineState& state, const Ref<GfxCompPipeline>& pipeline);

private:
fast_map<GfxCompPipelineState, Ref<GfxCompPipeline>> m_cache;
SpinMutex m_mutex;
};

}// namespace wmoge

#endif//WMOGE_GFX_PIPELINE_CACHE_HPP
11 changes: 6 additions & 5 deletions engine/gfx/gfx_shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include <array>
#include <cinttypes>
#include <optional>

namespace wmoge {

Expand Down Expand Up @@ -90,11 +91,11 @@ namespace wmoge {
*/
class GfxShader : public GfxResource {
public:
~GfxShader() override = default;
virtual GfxShaderStatus status() const = 0;
virtual std::string message() const = 0;
virtual const GfxShaderReflection* reflection() const = 0;
virtual Ref<Data> byte_code() const = 0;
~GfxShader() override = default;
virtual GfxShaderStatus status() const = 0;
virtual std::string message() const = 0;
virtual std::optional<const GfxShaderReflection*> reflection() const = 0;
virtual Ref<Data> byte_code() const = 0;
};

}// namespace wmoge
Expand Down
15 changes: 15 additions & 0 deletions engine/gfx/threaded/gfx_ctx_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ namespace wmoge {
m_stream->push([=]() { m_ctx->unmap_storage_buffer(buffer); });
}

void GfxCtxWrapper::barrier_image(const Ref<GfxTexture>& texture, GfxTexBarrierType barrier_type) {
m_stream->push([=]() { m_ctx->barrier_image(texture, barrier_type); });
}
void GfxCtxWrapper::barrier_buffer(const Ref<GfxStorageBuffer>& buffer) {
m_stream->push([=]() { m_ctx->barrier_buffer(buffer); });
}

void GfxCtxWrapper::begin_render_pass(const GfxRenderPassDesc& pass_desc, const StringId& name) {
m_stream->push([=]() { m_ctx->begin_render_pass(pass_desc, name); });
}
Expand All @@ -129,6 +136,11 @@ namespace wmoge {
m_stream->push_and_wait([&]() { is_bound = m_ctx->bind_pipeline(pipeline); });
return is_bound;
}
bool GfxCtxWrapper::bind_comp_pipeline(const Ref<GfxCompPipeline>& pipeline) {
bool is_bound = false;
m_stream->push_and_wait([&]() { is_bound = m_ctx->bind_comp_pipeline(pipeline); });
return is_bound;
}
void GfxCtxWrapper::bind_vert_buffer(const Ref<GfxVertBuffer>& buffer, int index, int offset) {
m_stream->push([=]() { m_ctx->bind_vert_buffer(buffer, index, offset); });
}
Expand All @@ -147,6 +159,9 @@ namespace wmoge {
void GfxCtxWrapper::draw_indexed(int index_count, int base_vertex, int instance_count) {
m_stream->push([=]() { m_ctx->draw_indexed(index_count, base_vertex, instance_count); });
}
void GfxCtxWrapper::dispatch(Vec3i group_count) {
m_stream->push([=]() { m_ctx->dispatch(group_count); });
}
void GfxCtxWrapper::end_render_pass() {
m_stream->push([=]() { m_ctx->end_render_pass(); });
}
Expand Down
Loading

0 comments on commit c115d31

Please sign in to comment.