diff --git a/Runtime/Graphics/CMakeLists.txt b/Runtime/Graphics/CMakeLists.txt index 3284d36b3..c2f2c3a89 100644 --- a/Runtime/Graphics/CMakeLists.txt +++ b/Runtime/Graphics/CMakeLists.txt @@ -68,4 +68,5 @@ runtime_add_hsh(Graphics Shaders/CParticleSwooshShaders.cpp Shaders/CPhazonSuitFilter.cpp Shaders/CRadarPaintShader.cpp + Shaders/CRandomStaticFilter.cpp ) diff --git a/Runtime/Graphics/Shaders/CRandomStaticFilter.cpp b/Runtime/Graphics/Shaders/CRandomStaticFilter.cpp index 5a7e3dbad..97379a331 100644 --- a/Runtime/Graphics/Shaders/CRandomStaticFilter.cpp +++ b/Runtime/Graphics/Shaders/CRandomStaticFilter.cpp @@ -6,40 +6,78 @@ #include "Runtime/Camera/CCameraFilter.hpp" #include "Runtime/Graphics/CBooRenderer.hpp" -namespace urde { +#include "CRandomStaticFilter.cpp.hshhead" -static boo::ObjToken SelectPipeline(EFilterType type) { - switch (type) { - case EFilterType::Blend: - return s_AlphaPipeline; - case EFilterType::Add: - return s_AddPipeline; - case EFilterType::Multiply: - return s_MultPipeline; - default: - return {}; +namespace urde { +using namespace hsh::pipeline; + +template +struct FilterTypeAttachmentExt { + using type = color_attachment<>; +}; +template +struct FilterTypeAttachmentExt { + using type = BlendAttachmentExt; +}; +template +struct FilterTypeAttachmentExt { + using type = AdditiveAttachmentExt; +}; +template +struct FilterTypeAttachmentExt { + using type = MultiplyAttachmentExt; +}; +template +struct FilterTypeAttachmentExt { + using type = NoColorAttachmentExt; +}; +template +using FilterTypeAttachment = typename FilterTypeAttachmentExt::type; + +template +struct CRandomStaticFilterPipeline +: pipeline, FilterTypeAttachment, + depth_compare, depth_write> { + CRandomStaticFilterPipeline(hsh::vertex_buffer vbo, + hsh::uniform_buffer ubo, hsh::texture2d tex) { + this->position = hsh::float4(vbo->m_pos, 0.f, 1.f); + hsh::float4 color = tex.read(Lookup8BPP(vbo->m_uv, ubo->randOff)) * ubo->color; + if constexpr (CookieCutter) { + if (color.w < ubo->discardThres) { + hsh::discard(); + } + } else { + color.w = ubo->color.w; + } + this->color_out[0] = color; } -} + + static constexpr hsh::uint2 Lookup8BPP(hsh::float2 uv, float randOff) { + int bx = int(uv.x) >> 3; + int rx = int(uv.x) & 0x7; + int by = int(uv.y) >> 2; + int ry = int(uv.y) & 0x3; + int bidx = by * 128 + bx; + int addr = bidx * 32 + ry * 8 + rx + int(randOff); + return hsh::uint2(addr & 0x3ff, addr >> 10); + } +}; +template struct CRandomStaticFilterPipeline; +template struct CRandomStaticFilterPipeline; +template struct CRandomStaticFilterPipeline; +template struct CRandomStaticFilterPipeline; CRandomStaticFilter::CRandomStaticFilter(EFilterType type, bool cookieCutter) : m_cookieCutter(cookieCutter) { - CGraphics::CommitResources([&](boo::IGraphicsDataFactory::Context& ctx) { - const std::array verts{{ - {{-1.f, -1.f}, {0.f, 0.f}}, - {{-1.f, 1.f}, {0.f, 448.f}}, - {{1.f, -1.f}, {640.f, 0.f}}, - {{1.f, 1.f}, {640.f, 448.f}}, - }}; - m_vbo = ctx.newStaticBuffer(boo::BufferUse::Vertex, verts.data(), sizeof(Vert), verts.size()); - m_uniBuf = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(Uniform), 1); - - const std::array, 1> bufs{m_uniBuf.get()}; - constexpr std::array stages{boo::PipelineStage::Vertex}; - const std::array, 1> texs{g_Renderer->GetRandomStaticEntropyTex()}; - m_dataBind = ctx.newShaderDataBinding(m_cookieCutter ? s_CookieCutterPipeline : SelectPipeline(type), m_vbo.get(), - nullptr, nullptr, bufs.size(), bufs.data(), stages.data(), nullptr, nullptr, - texs.size(), texs.data(), nullptr, nullptr); - return true; - } BooTrace); + constexpr std::array verts{{ + {{-1.f, -1.f}, {0.f, 0.f}}, + {{-1.f, 1.f}, {0.f, 448.f}}, + {{1.f, -1.f}, {640.f, 0.f}}, + {{1.f, 1.f}, {640.f, 448.f}}, + }}; + m_vbo = hsh::create_vertex_buffer(verts); + m_uniBuf = hsh::create_dynamic_uniform_buffer(); + m_dataBind.hsh_bind(CRandomStaticFilterPipeline(m_vbo.get(), m_uniBuf.get(), + g_Renderer->GetRandomStaticEntropyTex())); } void CRandomStaticFilter::draw(const zeus::CColor& color, float t) { @@ -48,11 +86,9 @@ void CRandomStaticFilter::draw(const zeus::CColor& color, float t) { m_uniform.color = color; m_uniform.randOff = ROUND_UP_32(int64_t(rand()) * 32767 / RAND_MAX); m_uniform.discardThres = 1.f - t; + m_uniBuf.load(m_uniform); - m_uniBuf->load(&m_uniform, sizeof(Uniform)); - - CGraphics::SetShaderDataBinding(m_dataBind); - CGraphics::DrawArray(0, 4); + m_dataBind.draw(0, 4); } } // namespace urde diff --git a/Runtime/Graphics/Shaders/CRandomStaticFilter.hpp b/Runtime/Graphics/Shaders/CRandomStaticFilter.hpp index 1138a7d99..cf38b7606 100644 --- a/Runtime/Graphics/Shaders/CRandomStaticFilter.hpp +++ b/Runtime/Graphics/Shaders/CRandomStaticFilter.hpp @@ -1,5 +1,7 @@ #pragma once +#include "hsh/hsh.h" + #include "Runtime/CToken.hpp" namespace urde { @@ -9,6 +11,7 @@ enum class EFilterShape; enum class EFilterType; class CRandomStaticFilter { +public: struct Uniform { hsh::float4 color; float randOff; @@ -18,6 +21,8 @@ class CRandomStaticFilter { hsh::float2 m_pos; hsh::float2 m_uv; }; + +private: hsh::owner> m_vbo; hsh::dynamic_owner> m_uniBuf; hsh::binding m_dataBind;