载入中...
搜索中...
未找到
Exposure.cpp
浏览该文件的文档.
1#include "graphics/Exposure.h"
2#include "common/Profile.h"
3
4#include <algorithm>
5#include <glm/vec3.hpp>
6#include <string>
7#include <utility>
8#include <vector>
9
10#include "common/Exception.h"
11#include "graphics/BlendMode.h"
12#include "graphics/Canvas.h"
13#include "graphics/Color.h"
14#include "graphics/Graphics.h"
15#include "graphics/Shader.h"
16#include "graphics/Texture.h"
18#include "graphics/shaders/exposure_adapt_frag_spv.inc"
19#include "graphics/shaders/exposure_apply_frag_spv.inc"
20#include "graphics/shaders/exposure_meter_frag_spv.inc"
21
22namespace eve::graphics {
23namespace {
24
25std::vector<uint32_t> copySpv(const uint32_t *data, size_t count) {
26 return std::vector<uint32_t>(data, data + count);
27}
28
29Shader *newPostShader(Graphics *gfx, const uint32_t *spv, size_t count, const char *wgsl) {
30 Shader *shader = gfx->getBackendName() == "webgpu"
31 ? gfx->newShaderFromWgsl({}, std::string(shaders::kPostCommon) + wgsl)
32 : gfx->newShaderFromSpv({}, copySpv(spv, count));
33 if (!shader || !shader->gpuHandle) throw eve::Exception("Exposure: failed to create shader");
34 return shader;
35}
36
37} // namespace
38
39Exposure::Exposure(Graphics *gfx) : gfx_(gfx) {
40 if (!gfx_) throw eve::Exception("Exposure: null graphics");
41 meterShader_ = newPostShader(gfx_, exposure_meter_frag_spv, exposure_meter_frag_spv_count,
43 meterShader_->declareFloat("minEV");
44 meterShader_->declareFloat("maxEV");
45
46 adaptShader_ = newPostShader(gfx_, exposure_adapt_frag_spv, exposure_adapt_frag_spv_count,
48 adaptShader_->declareFloat("deltaSeconds");
49 adaptShader_->declareFloat("darkSpeed");
50 adaptShader_->declareFloat("brightSpeed");
51 adaptShader_->declareFloat("resetHistory");
52
53 applyShader_ = newPostShader(gfx_, exposure_apply_frag_spv, exposure_apply_frag_spv_count,
55 applyShader_->declareFloat("manualExposure");
56 applyShader_->declareFloat("automatic");
57 applyShader_->declareFloat("colorFilterR");
58 applyShader_->declareFloat("colorFilterG");
59 applyShader_->declareFloat("colorFilterB");
60 applyShader_->declareFloat("vignette");
61 applyShader_->declareFloat("lensDistortion");
62 applyShader_->declareFloat("liftR");
63 applyShader_->declareFloat("liftG");
64 applyShader_->declareFloat("liftB");
65 applyShader_->declareFloat("inverseGammaR");
66 applyShader_->declareFloat("inverseGammaG");
67 applyShader_->declareFloat("inverseGammaB");
68 applyShader_->declareFloat("gainR");
69 applyShader_->declareFloat("gainG");
70 applyShader_->declareFloat("gainB");
71 applyShader_->declareFloat("vignetteSmoothness");
72 applyShader_->declareFloat("lensScale");
73}
74
75void Exposure::ensureTargets(int width, int height) {
76 width = std::max(width, 1);
77 height = std::max(height, 1);
78 if (!meter_) {
79 meter_ = gfx_->newHDRCanvas(1, 1);
80 historyA_ = gfx_->newHDRCanvas(1, 1);
81 historyB_ = gfx_->newHDRCanvas(1, 1);
82 historyRead_ = historyA_;
83 historyWrite_ = historyB_;
84 }
85 const bool pipelineOwned = gfx_->pipelineExposure_.get() == this;
86 if (pipelineOwned) output_ = &gfx_->prepareFramePostTarget(Graphics::FramePostTarget::Exposure, width, height);
87 if (!output_ || width_ != width || height_ != height) {
88 if (!pipelineOwned) output_ = gfx_->newHDRCanvas(width, height);
89 width_ = width;
90 height_ = height;
91 historyValid_ = false;
92 }
93 if (!meter_ || !historyRead_ || !historyWrite_ || !output_)
94 throw eve::Exception("Exposure: failed to allocate HDR targets");
95}
96
97Texture *Exposure::apply(Texture *source, float manualExposure, bool automatic, float minEV,
98 float maxEV, const glm::vec3& colorFilter, const glm::vec3& lift,
99 const glm::vec3& inverseGamma, const glm::vec3& gain, float vignette,
100 float vignetteSmoothness, float lensDistortion, float lensScale,
101 Texture *meterSource,
102 float deltaSeconds) {
103 EV_PROFILE_MODULE("graphics", "PostProcess.Exposure");
104 if (!source) throw eve::Exception("Exposure.apply: null source");
105 ensureTargets(source->getWidth(), source->getHeight());
106 Canvas *previousCanvas = gfx_->getCanvas();
107 Texture *exposureTexture = source;
108 if (!meterSource) meterSource = source;
109
110 if (automatic) {
111 meterShader_->sendFloat("minEV", std::min(minEV, maxEV));
112 meterShader_->sendFloat("maxEV", std::max(minEV, maxEV));
113 meter_->clear(Color(0.f, 0.f, 0.f, 1.f), {}, {});
114 gfx_->setCanvas(meter_);
115 gfx_->drawTexturedRectShaderUV(meterSource, meterShader_, 0.f, 0.f, 1.f, 1.f, 0.f, 0.f,
116 1.f, 1.f, Color(1.f, 1.f, 1.f, 1.f), false,
118
119 adaptShader_->sendFloat("deltaSeconds", std::clamp(deltaSeconds, 0.f, 0.25f));
120 adaptShader_->sendFloat("darkSpeed", 1.5f);
121 adaptShader_->sendFloat("brightSpeed", 3.f);
122 adaptShader_->sendFloat("resetHistory", historyValid_ ? 0.f : 1.f);
123 historyWrite_->clear(Color(1.f, 0.f, 0.f, 1.f), {}, {});
124 gfx_->setCanvas(historyWrite_);
125 Texture *meterTexture = meter_->getTexture();
126 Texture *previousExposure = historyValid_ ? historyRead_->getTexture() : meterTexture;
127 gfx_->drawTexturedRectShaderDepthMotion(meterTexture, previousExposure, meterTexture,
128 adaptShader_, 0.f, 0.f, 1.f, 1.f,
129 Color(1.f, 1.f, 1.f, 1.f));
130 exposureTexture = historyWrite_->getTexture();
131 historyValid_ = true;
132 std::swap(historyRead_, historyWrite_);
133 }
134
135 applyShader_->sendFloat("manualExposure", std::max(manualExposure, 0.f));
136 applyShader_->sendFloat("automatic", automatic ? 1.f : 0.f);
137 applyShader_->sendFloat("colorFilterR", std::max(colorFilter.r, 0.f));
138 applyShader_->sendFloat("colorFilterG", std::max(colorFilter.g, 0.f));
139 applyShader_->sendFloat("colorFilterB", std::max(colorFilter.b, 0.f));
140 applyShader_->sendFloat("vignette", std::clamp(vignette, 0.0F, 1.0F));
141 applyShader_->sendFloat("lensDistortion", std::clamp(lensDistortion, 0.0F, 1.0F));
142 applyShader_->sendFloat("liftR", lift.r);
143 applyShader_->sendFloat("liftG", lift.g);
144 applyShader_->sendFloat("liftB", lift.b);
145 applyShader_->sendFloat("inverseGammaR", std::max(inverseGamma.r, 0.001F));
146 applyShader_->sendFloat("inverseGammaG", std::max(inverseGamma.g, 0.001F));
147 applyShader_->sendFloat("inverseGammaB", std::max(inverseGamma.b, 0.001F));
148 applyShader_->sendFloat("gainR", gain.r);
149 applyShader_->sendFloat("gainG", gain.g);
150 applyShader_->sendFloat("gainB", gain.b);
151 applyShader_->sendFloat("vignetteSmoothness", std::clamp(vignetteSmoothness, 0.01F, 1.0F));
152 applyShader_->sendFloat("lensScale", std::clamp(lensScale, 0.01F, 5.0F));
153 output_->clear(Color(0.f, 0.f, 0.f, 0.f), {}, {});
154 gfx_->setCanvas(output_);
155 gfx_->drawTexturedRectShaderDepthMotion(source, exposureTexture, source, applyShader_, 0.f, 0.f,
156 float(width_), float(height_),
157 Color(1.f, 1.f, 1.f, 1.f));
158 gfx_->setCanvas(previousCanvas);
159 Texture *result = output_->getTexture();
160 if (!result) throw eve::Exception("Exposure: output texture missing");
161 return result;
162}
163
164} // namespace eve::graphics
std::uint32_t height
std::uint32_t width
Poco::JSON::Object::Ptr result
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
Shader * shader
std::string_view source
int count
double gain
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Canvas public API.
Definition Canvas.h:17
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.
Texture * apply(Texture *source, float manualExposure, bool automatic, float minEV, float maxEV, const glm::vec3 &colorFilter, const glm::vec3 &lift, const glm::vec3 &inverseGamma, const glm::vec3 &gain, float vignette, float vignetteSmoothness, float lensDistortion, float lensScale, Texture *meterSource=nullptr, float deltaSeconds=1.f/60.f)
Apply manual and optional automatic exposure to a linear HDR source.
Definition Exposure.cpp:97
Exposure(Graphics *gfx)
Create cross-backend exposure shaders and 1x1 history targets.
Definition Exposure.cpp:39
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.
std::unique_ptr< Exposure > pipelineExposure_
Definition Graphics.h:2349
virtual void setCanvas(Canvas *canvas)=0
nullptr or this → screen. Switching flushes pending draws to the previous target.
virtual void drawTexturedRectShaderDepthMotion(Texture *color, Texture *depth, Texture *motion, Shader *shader, float x, float y, float w, float h, const Color &tint)=0
Post draw with color, depth/history and motion/reactive textures.
virtual Canvas * newHDRCanvas(int width, int height)=0
Create an engine-internal linear RGBA16F post-process target.
virtual Canvas & prepareFramePostTarget(FramePostTarget target, int width, int height)
Prepare a pipeline output for this acquired frame. @ownership Borrows backend-owned Canvas; render th...
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 * kExposureMeter
constexpr const char * kPostCommon
constexpr const char * kExposureAdapt
constexpr const char * kExposureApply
卡牌游戏 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