载入中...
搜索中...
未找到
Bloom.cpp
浏览该文件的文档.
1#include "graphics/Bloom.h"
2#include "common/Profile.h"
3
4#include <algorithm>
5#include <string>
6#include <vector>
7
8#include "common/Exception.h"
10#include "graphics/Canvas.h"
11#include "graphics/Color.h"
12#include "graphics/Graphics.h"
13#include "graphics/Shader.h"
14#include "graphics/Texture.h"
16#include "graphics/shaders/bloom_downsample_frag_spv.inc"
17#include "graphics/shaders/bloom_upsample_frag_spv.inc"
18
19namespace eve::graphics {
20namespace {
21
22std::vector<uint32_t> copySpv(const uint32_t *data, size_t count) {
23 return std::vector<uint32_t>(data, data + count);
24}
25
26Shader *newPostShader(Graphics *gfx, const uint32_t *spv, size_t count, const char *wgsl) {
27 Shader *shader = gfx->getBackendName() == "webgpu"
28 ? gfx->newShaderFromWgsl({}, std::string(shaders::kPostCommon) + wgsl)
29 : gfx->newShaderFromSpv({}, copySpv(spv, count));
30 if (!shader || !shader->gpuHandle) throw eve::Exception("Bloom: failed to create shader");
31 return shader;
32}
33
34} // namespace
35
36Bloom::Bloom(Graphics *gfx) : gfx_(gfx) {
37 if (!gfx_) throw eve::Exception("Bloom: null graphics");
38 downsample_ = newPostShader(gfx_, bloom_downsample_frag_spv,
39 bloom_downsample_frag_spv_count, shaders::kBloomDownsample);
40 downsample_->declareFloat("texelW");
41 downsample_->declareFloat("texelH");
42 downsample_->declareFloat("firstPass");
43 downsample_->declareFloat("threshold");
44 downsample_->declareFloat("knee");
45
46 upsample_ = newPostShader(gfx_, bloom_upsample_frag_spv, bloom_upsample_frag_spv_count,
48 upsample_->declareFloat("texelW");
49 upsample_->declareFloat("texelH");
50 upsample_->declareFloat("scatter");
51}
52
53void Bloom::ensureTargets(int sourceWidth, int sourceHeight) {
54 sourceWidth = std::max(sourceWidth, 1);
55 sourceHeight = std::max(sourceHeight, 1);
56 if (levels_[0] && sourceWidth_ == sourceWidth && sourceHeight_ == sourceHeight) return;
57
58 sourceWidth_ = sourceWidth;
59 sourceHeight_ = sourceHeight;
60 composite_ = gfx_->newHDRCanvas(sourceWidth, sourceHeight);
61 if (!composite_) throw eve::Exception("Bloom: failed to allocate HDR composite target");
62 int width = sourceWidth;
63 int height = sourceHeight;
64 for (Canvas *&level : levels_) {
65 width = std::max(width / 2, 1);
66 height = std::max(height / 2, 1);
68 if (!level) throw eve::Exception("Bloom: failed to allocate HDR pyramid target");
69 }
70}
71
72void Bloom::configureDownsample(Texture *source, bool firstPass, float threshold) {
73 downsample_->sendFloat("texelW", 1.f / float(std::max(source->getWidth(), 1)));
74 downsample_->sendFloat("texelH", 1.f / float(std::max(source->getHeight(), 1)));
75 downsample_->sendFloat("firstPass", firstPass ? 1.f : 0.f);
76 downsample_->sendFloat("threshold", std::max(threshold, 0.f));
77 downsample_->sendFloat("knee", std::max(threshold * 0.5f, 1e-4f));
78}
79
80void Bloom::configureUpsample(Texture *source, float scatter) {
81 upsample_->sendFloat("texelW", 1.f / float(std::max(source->getWidth(), 1)));
82 upsample_->sendFloat("texelH", 1.f / float(std::max(source->getHeight(), 1)));
83 upsample_->sendFloat("scatter", std::clamp(scatter, 0.5f, 2.f));
84}
85
86Texture *Bloom::build(Texture *source, float threshold, float scatter) {
87 if (!source) throw eve::Exception("Bloom.build: null source");
88 ensureTargets(source->getWidth(), source->getHeight());
89 if (filterSettings_.filter != BloomFilter::KarisTent) return buildGaussian(source, threshold);
90 Canvas *previous = gfx_->getCanvas();
91
93 for (size_t i = 0; i < levels_.size(); ++i) {
94 Canvas *target = levels_[i];
95 target->clear(Color(0.f, 0.f, 0.f, 0.f), {}, {});
96 gfx_->setCanvas(target);
97 configureDownsample(input, i == 0, threshold);
98 gfx_->drawTexturedRectShaderUV(input, downsample_, 0.f, 0.f, float(target->getWidth()),
99 float(target->getHeight()), 0.f, 0.f, 1.f, 1.f,
100 Color(1.f, 1.f, 1.f, 1.f), false, BlendMode::Opaque);
101 input = target->getTexture();
102 if (!input) throw eve::Exception("Bloom: pyramid texture missing");
103 }
104
105 for (size_t i = levels_.size() - 1; i > 0; --i) {
106 Texture *low = levels_[i]->getTexture();
107 Canvas *high = levels_[i - 1];
108 configureUpsample(low, scatter);
109 gfx_->setCanvas(high);
110 gfx_->drawTexturedRectShaderUV(low, upsample_, 0.f, 0.f, float(high->getWidth()),
111 float(high->getHeight()), 0.f, 0.f, 1.f, 1.f,
112 Color(1.f, 1.f, 1.f, 1.f), false, BlendMode::Additive);
113 }
114
115 gfx_->setCanvas(previous);
116 return levels_[0]->getTexture();
117}
118
119Texture *Bloom::apply(Texture *source, float intensity, float threshold, float scatter) {
120 EV_PROFILE_MODULE("graphics", "PostProcess.Bloom");
121 if (!source) throw eve::Exception("Bloom.apply: null source");
122 if (intensity <= 0.f) return source;
123 Texture *bloom = build(source, threshold, scatter);
124 if (filterSettings_.filter != BloomFilter::KarisTent) return compositeGaussian(source, bloom, intensity);
125 Canvas *previous = gfx_->getCanvas();
126 composite_->clear(Color(0.f, 0.f, 0.f, 0.f), {}, {});
127 gfx_->setCanvas(composite_);
128 gfx_->drawTexturedRectShaderUV(source, nullptr, 0.f, 0.f, float(sourceWidth_),
129 float(sourceHeight_), 0.f, 0.f, 1.f, 1.f,
130 Color(1.f, 1.f, 1.f, 1.f), false, BlendMode::Opaque);
131 gfx_->drawTexturedRectShaderUV(bloom, nullptr, 0.f, 0.f, float(sourceWidth_),
132 float(sourceHeight_), 0.f, 0.f, 1.f, 1.f,
133 Color(intensity, intensity, intensity, 1.f), false,
135 gfx_->setCanvas(previous);
136 Texture *result = composite_->getTexture();
137 if (!result) throw eve::Exception("Bloom: composite texture missing");
138 return result;
139}
140
141} // namespace eve::graphics
LogicalId target
EvpackChunkInput input
Definition Evpack.cpp:170
std::uint32_t height
std::uint32_t width
Poco::JSON::Object::Ptr result
int level
std::shared_ptr< const OceanSprayFrame > previous
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
std::string build
Shader * shader
std::string_view source
int count
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Texture * build(Texture *source, float threshold, float scatter=1.f)
Build the selected bloom pyramid from an HDR source.
Definition Bloom.cpp:86
Bloom(Graphics *gfx)
Create the cross-backend bloom shaders.
Definition Bloom.cpp:36
Texture * apply(Texture *source, float intensity, float threshold, float scatter=1.f)
Add the reconstructed bloom to the HDR source in a full-size target.
Definition Bloom.cpp:119
Canvas public API.
Definition Canvas.h:17
virtual int getWidth() const =0
Returns the width.
virtual void clear(std::optional< Color > color, std::optional< int > stencil, std::optional< double > depth)=0
Clears .
virtual Texture * getTexture()=0
Sampleable color buffer; screen Canvas returns nullptr.
virtual int getHeight() const =0
Returns the height.
virtual void drawTexturedRectShaderUV(Texture *texture, Shader *shader, float x, float y, float w, float h, float u0, float v0, float u1, float v1, const Color &color, bool rotatedUV=false, BlendMode blend=BlendMode::Alpha)=0
UV draw with an explicit Shader (nullptr = default textured pipeline).
virtual Canvas * getCanvas() const =0
Returns the canvas.
virtual void setCanvas(Canvas *canvas)=0
nullptr or this → screen. Switching flushes pending draws to the previous target.
virtual Canvas * newHDRCanvas(int width, int height)=0
Create an engine-internal linear RGBA16F post-process target.
void sendFloat(const std::string &name, float x)
Definition Shader.cpp:107
int declareFloat(const std::string &name)
Reserve sequential float slots in the push-constant block. Returns start index.
Definition Shader.cpp:55
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
constexpr const char * kBloomDownsample
constexpr const char * kBloomUpsample
constexpr const char * kPostCommon
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
eve::Color Color
RGBA color used by every graphics draw call. Lives inside eve::graphics so including a graphics heade...
Definition Color.h:13