载入中...
搜索中...
未找到
StyleChain.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <glm/vec4.hpp>
5#include <limits>
6#include <utility>
7#include "common/Exception.h"
8#include "graphics/Canvas.h"
9#include "graphics/Graphics.h"
10#include "graphics/Shader.h"
11#include "graphics/Texture.h"
13#include "stylize/StylePass.h"
14#include "stylize/StyleRecipe.h"
16
17// StyleChain.cpp
18namespace eve::stylize {
19
20void StyleChain::clear() { passes_.clear(); }
21
23 if (!pass) throw eve::Exception("StyleChain.add: null pass");
24 passes_.push_back(pass);
25}
26
28 if (index < 0 || index >= int(passes_.size())) return nullptr;
29 return passes_[size_t(index)];
30}
31
33 graphics::Canvas* temp) {
34 if (!gfx) throw eve::Exception("StyleChain.apply: null graphics");
35 if (!source) throw eve::Exception("StyleChain.apply: null source");
36 if (!dest) throw eve::Exception("StyleChain.apply: null dest");
37 if (passes_.empty()) throw eve::Exception("StyleChain.apply: no passes");
38
39 if (passes_.size() == 1) {
40 passes_[0]->applyTo(gfx, source, dest);
41 return;
42 }
43 if (!temp) throw eve::Exception("StyleChain.apply: temp canvas required for multi-pass");
44 if (!temp->getTexture()) throw eve::Exception("StyleChain.apply: temp canvas has no sampleable texture");
45 if (!dest->getTexture()) throw eve::Exception("StyleChain.apply: dest canvas has no sampleable texture");
46
47 // Alternate temp / dest. Final result must live in dest.
48 // n even: … → temp → dest
49 // n odd : … → dest → temp, then blit temp → dest
51 const int n = int(passes_.size());
52 for (int i = 0; i < n; ++i) {
53 graphics::Canvas* out = (i & 1) ? dest : temp;
54 passes_[size_t(i)]->applyTo(gfx, in, out);
55 in = out->getTexture();
56 if (!in) throw eve::Exception("StyleChain.apply: intermediate texture missing");
57 }
58 if ((n & 1) != 0) {
59 // Last write was temp — blit into dest (no style shader).
60 graphics::Canvas* prev = gfx->getCanvas();
61 gfx->setCanvas(dest);
62 gfx->drawTexturedRect(temp->getTexture(), 0.f, 0.f, float(dest->getWidth()), float(dest->getHeight()),
63 glm::vec4(1.f, 1.f, 1.f, 1.f));
64 gfx->setCanvas(prev);
65 }
66}
67
69 graphics::Canvas* temp) {
70 if (!source) throw eve::Exception("StyleChain.applyCanvas: null source");
71 graphics::Texture* tex = source->getTexture();
72 if (!tex) throw eve::Exception("StyleChain.applyCanvas: source has no texture");
73 apply(gfx, tex, dest, temp);
74}
75
76} // namespace eve::stylize
77
78// StyleInstance.cpp
79namespace eve::stylize {
80
81namespace {
82const StyleParameterDesc& requireParam(const std::string& style, const std::string& name) {
83 const StyleParameterDesc* desc = findStyleParameter(style, name);
84 if (!desc) throw eve::Exception("StyleInstance: style '%s' has no parameter '%s'", style.c_str(), name.c_str());
85 return *desc;
86}
87} // namespace
88
89StyleInstance::StyleInstance(std::string style) : style_(std::move(style)) {
90 if (!findStyleDefinition(style_)) throw eve::Exception("StyleInstance: unknown style '%s'", style_.c_str());
91}
92
93StyleInstance::StyleInstance(std::string style, std::map<std::string, float> defaults)
94 : style_(std::move(style)), externalDefaults_(std::move(defaults)) {
95 if (!style_.starts_with("asset://")) throw eve::Exception("External mesh styles require an asset URI");
96 for (const auto& [name, value] : *externalDefaults_)
97 if (name.empty() || !std::isfinite(value)) throw eve::Exception("Invalid external style parameter");
98}
99
101 return externalDefaults_ ? int(externalDefaults_->size()) : styleParamCount(style_);
102}
103
104std::string StyleInstance::getStage() const {
107}
108
110 if (externalDefaults_) return priority_.value_or(40);
111 return priority_.value_or(findStyleDefinition(style_)->priority);
112}
113
114bool StyleInstance::requiresInput(const std::string& input) const {
115 if (externalDefaults_) return false;
116 const StyleDefinition* def = findStyleDefinition(style_);
117 if (input == "color") return def->post;
118 if (input == "depth") return def->depth;
119 if (input == "normal") return def->normal;
120 return false;
121}
122
123std::string StyleInstance::getParamName(int index) const {
124 if (externalDefaults_) {
125 if (index < 0 || std::size_t(index) >= externalDefaults_->size())
126 throw eve::Exception("Invalid parameter index");
127 return std::next(externalDefaults_->begin(), index)->first;
128 }
129 return styleParamName(style_, index);
130}
131
132float StyleInstance::getParamDefault(const std::string& name) const {
133 return externalDefaults_ ? externalDefaults_->at(name) : requireParam(style_, name).defaultValue;
134}
135
136float StyleInstance::getParamMin(const std::string& name) const {
137 if (externalDefaults_) {
138 if (!externalDefaults_->contains(name)) throw eve::Exception("Unknown external parameter");
139 return -std::numeric_limits<float>::max();
140 }
141 return requireParam(style_, name).minValue;
142}
143
144float StyleInstance::getParamMax(const std::string& name) const {
145 if (externalDefaults_) {
146 if (!externalDefaults_->contains(name)) throw eve::Exception("Unknown external parameter");
147 return std::numeric_limits<float>::max();
148 }
149 return requireParam(style_, name).maxValue;
150}
151
152bool StyleInstance::hasParam(const std::string& name) const {
153 return externalDefaults_ ? externalDefaults_->contains(name) : findStyleParameter(style_, name) != nullptr;
154}
155
156bool StyleInstance::isOverridden(const std::string& name) const { return overrides_.find(name) != overrides_.end(); }
157
158void StyleInstance::setFloat(const std::string& name, float value) {
159 if (externalDefaults_) {
160 if (!externalDefaults_->contains(name) || !std::isfinite(value))
161 throw eve::Exception("Invalid external style parameter");
162 overrides_[name] = value;
163 return;
164 }
165 const StyleParameterDesc& desc = requireParam(style_, name);
166 overrides_[name] = std::clamp(value, desc.minValue, desc.maxValue);
167}
168
169float StyleInstance::getFloat(const std::string& name) const {
170 const float defaultValue = getParamDefault(name);
171 const auto it = overrides_.find(name);
172 return it == overrides_.end() ? defaultValue : it->second;
173}
174
175void StyleInstance::reset(const std::string& name) {
177 overrides_.erase(name);
178}
179
180void StyleInstance::resetAll() { overrides_.clear(); }
181
183 if (!shader) throw eve::Exception("StyleInstance: null shader");
184 for (const auto& [name, value] : overrides_) {
185 if (shader->hasUniform(name)) shader->sendFloat(name, value);
186 }
187}
188
190 if (externalDefaults_) throw eve::Exception("External style requires its package renderer");
193 return new StylePass(style_, shader);
194}
195
197 if (externalDefaults_) throw eve::Exception("External style requires its package renderer");
200 return shader;
201}
202
203} // namespace eve::stylize
204
205// StylePass.cpp
206namespace eve::stylize {
207
208StylePass::StylePass(const std::string& style, graphics::Shader* shader) : style_(style), shader_(shader) {
209 if (!shader_) throw eve::Exception("StylePass: null shader for style '%s'", style.c_str());
210 if (const StyleDefinition* def = findStyleDefinition(style_)) {
211 desc_.stage = def->stage;
212 desc_.priority = def->priority;
213 if (def->depth) desc_.inputs |= graphics::PostInputDepth;
214 if (def->normal) desc_.inputs |= graphics::PostInputNormal;
215 }
216}
217
218std::string StylePass::getStage() const { return graphics::postEffectStageName(desc_.stage); }
219
220bool StylePass::requiresInput(const std::string& input) const {
221 if (input == "color") return (desc_.inputs & graphics::PostInputColor) != 0;
222 if (input == "depth") return (desc_.inputs & graphics::PostInputDepth) != 0;
223 if (input == "normal") return (desc_.inputs & graphics::PostInputNormal) != 0;
224 if (input == "motion") return (desc_.inputs & graphics::PostInputMotion) != 0;
225 if (input == "objectId") return (desc_.inputs & graphics::PostInputObjectId) != 0;
226 return false;
227}
228
229bool StylePass::hasParam(const std::string& name) const { return shader_ && shader_->hasUniform(name); }
230
231void StylePass::setFloat(const std::string& name, float value) {
232 if (!shader_) throw eve::Exception("StylePass.setFloat: null shader");
233 shader_->sendFloat(name, value);
234}
235
236float StylePass::getFloat(const std::string& name) const {
237 if (!shader_) throw eve::Exception("StylePass.getFloat: null shader");
238 float v = 0.f;
239 if (shader_->getFromVar(name, &v, sizeof(v)) != int(sizeof(v)))
240 throw eve::Exception("StylePass.getFloat: missing param '%s'", name.c_str());
241 return v;
242}
243
244void StylePass::setTime(float seconds) {
245 if (hasParam("time")) setFloat("time", seconds);
246}
247
248float StylePass::getTime() const {
249 if (!hasParam("time")) return 0.f;
250 return getFloat("time");
251}
252
253void StylePass::uploadScreenUniforms(int width, int height) {
254 if (width < 1) width = 1;
255 if (height < 1) height = 1;
256 if (hasParam("texelW")) setFloat("texelW", 1.f / float(width));
257 if (hasParam("texelH")) setFloat("texelH", 1.f / float(height));
258 if (hasParam("screenW")) setFloat("screenW", float(width));
259 if (hasParam("screenH")) setFloat("screenH", float(height));
260}
261
263 if (!gfx) throw eve::Exception("StylePass.apply: null graphics");
264 if (!source) throw eve::Exception("StylePass.apply: null source texture");
265 if (!shader_) throw eve::Exception("StylePass.apply: null shader");
266
267 const int w = source->getWidth();
268 const int h = source->getHeight();
269 uploadScreenUniforms(w, h);
270
271 // Prefer destination canvas size for pixel grid when available.
272 if (auto* dst = gfx->getCanvas()) {
273 if (hasParam("screenW")) setFloat("screenW", float(dst->getWidth()));
274 if (hasParam("screenH")) setFloat("screenH", float(dst->getHeight()));
275 } else {
276 if (hasParam("screenW")) setFloat("screenW", float(gfx->getWidth()));
277 if (hasParam("screenH")) setFloat("screenH", float(gfx->getHeight()));
278 }
279
280 const float dw = gfx->getCanvas() ? float(gfx->getCanvas()->getWidth()) : float(gfx->getWidth());
281 const float dh = gfx->getCanvas() ? float(gfx->getCanvas()->getHeight()) : float(gfx->getHeight());
282
283 gfx->drawTexturedRectShader(source, shader_, 0.f, 0.f, dw, dh, glm::vec4(1.f, 1.f, 1.f, 1.f));
284}
285
287 if (!source) throw eve::Exception("StylePass.applyCanvas: null canvas");
288 graphics::Texture* tex = source->getTexture();
289 if (!tex) throw eve::Exception("StylePass.applyCanvas: canvas has no sampleable texture");
290 apply(gfx, tex);
291}
292
294 if (!gfx) throw eve::Exception("StylePass.applyTo: null graphics");
295 if (!dest) throw eve::Exception("StylePass.applyTo: null dest");
296 graphics::Canvas* prev = gfx->getCanvas();
297 gfx->setCanvas(dest);
298 apply(gfx, source);
299 gfx->setCanvas(prev);
300}
301
303 if (!source) throw eve::Exception("StylePass.applyCanvasTo: null source");
304 graphics::Texture* tex = source->getTexture();
305 if (!tex) throw eve::Exception("StylePass.applyCanvasTo: source has no sampleable texture");
306 applyTo(gfx, tex, dest);
307}
308
309} // namespace eve::stylize
310
311// StyleRecipe.cpp
312namespace eve::stylize {
313
314StyleRecipe::~StyleRecipe() = default;
315
317 instances_.clear();
318 passes_.clear();
319 stage_.clear();
320 compiled_ = false;
321}
322
324 if (!instance) throw eve::Exception("StyleRecipe.add: null instance");
325 instances_.push_back(instance);
326 compiled_ = false;
327}
328
330 if (index < 0 || index >= int(instances_.size())) return nullptr;
331 return instances_[size_t(index)];
332}
333
335 if (!gfx) throw eve::Exception("StyleRecipe.compile: null graphics");
336 if (instances_.empty()) throw eve::Exception("StyleRecipe.compile: no styles");
337
338 if (graphics_ != gfx) {
339 scratch_ = nullptr;
340 scratchWidth_ = 0;
341 scratchHeight_ = 0;
342 }
343 std::stable_sort(instances_.begin(), instances_.end(),
344 [](StyleInstance* a, StyleInstance* b) { return a->getPriority() < b->getPriority(); });
345 stage_ = instances_.front()->getStage();
346 passes_.clear();
347 for (StyleInstance* instance : instances_) {
348 if (instance->getStage() != stage_)
349 throw eve::Exception("StyleRecipe.compile: cannot mix stages '%s' and '%s'", stage_.c_str(),
350 instance->getStage().c_str());
351 passes_.emplace_back(instance->newPass(gfx));
352 }
353 graphics_ = gfx;
354 compiled_ = true;
355}
356
357void StyleRecipe::ensureScratch(graphics::Graphics* gfx, graphics::Canvas* dest) {
358 if (passes_.size() < 2) return;
359 const int width = dest->getWidth();
360 const int height = dest->getHeight();
361 if (graphics_ != gfx || !scratch_ || scratchWidth_ != width || scratchHeight_ != height) {
362 scratch_ = gfx->newCanvas(width, height);
363 scratchWidth_ = width;
364 scratchHeight_ = height;
365 graphics_ = gfx;
366 }
367}
368
370 if (!compiled_) throw eve::Exception("StyleRecipe.apply: compile first");
371 if (graphics_ != gfx) throw eve::Exception("StyleRecipe.apply: compiled for another Graphics");
372 if (!source || !dest) throw eve::Exception("StyleRecipe.apply: null source or destination");
373 ensureScratch(gfx, dest);
374
375 StyleChain chain;
376 for (const auto& pass : passes_) chain.add(pass.get());
377 chain.apply(gfx, source, dest, scratch_);
378}
379
381 if (!source || !source->getTexture()) throw eve::Exception("StyleRecipe.applyCanvas: source has no texture");
382 apply(gfx, source->getTexture(), dest);
383}
384
385} // namespace eve::stylize
float w
Definition AnimClip.cpp:738
int priority
std::vector< Value > defaults
Definition ECS.cpp:49
std::string name
Definition ECS.cpp:154
scene::NodeDesc desc
EvpackChunkInput input
Definition Evpack.cpp:170
glm::vec3 n
Definition Grass.cpp:63
float v
int h
std::uint32_t height
std::uint32_t width
double defaultValue
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
Object::Ptr value
Shader * shader
ScriptStructure out
std::string_view source
uint32_t index
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Canvas public API.
Definition Canvas.h:17
virtual int getWidth() const =0
Returns the width.
virtual Texture * getTexture()=0
Sampleable color buffer; screen Canvas returns nullptr.
virtual int getHeight() const =0
Returns the height.
int getWidth() const
Returns the width.
Definition Graphics.h:489
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 void drawTexturedRect(Texture *texture, float x, float y, float w, float h, float r, float g, float b, float a=1.f)
RGBA-float overload matching the script-facing drawTexturedRect name.
virtual Canvas * newCanvas(int width, int height)=0
Create an offscreen render target (sampleable). Owned by Graphics.
int getHeight() const
Returns the height.
Definition Graphics.h:491
virtual void drawTexturedRectShader(Texture *texture, Shader *shader, float x, float y, float w, float h, const Color &color)=0
Draw with an explicit Shader (nullptr = default textured pipeline).
Custom GPU program.
Definition Shader.h:41
bool hasUniform(const std::string &name) const
Definition Shader.cpp:71
void sendFloat(const std::string &name, float x)
Definition Shader.cpp:107
int getFromVar(const std::string &name, void *data, size_t size) const
Definition Shader.cpp:96
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
Ordered multi-pass stylize pipeline (extension point).
Definition StyleChain.h:29
void applyCanvas(graphics::Graphics *gfx, graphics::Canvas *source, graphics::Canvas *dest, graphics::Canvas *temp=nullptr)
Applies canvas.
void apply(graphics::Graphics *gfx, graphics::Texture *source, graphics::Canvas *dest, graphics::Canvas *temp=nullptr)
Run passes in order into dest. When more than one pass is present, temp is required for ping-pong (sa...
StylePass * getPass(int index) const
Returns the pass.
void add(StylePass *pass)
Adds .
Mutable parameter instance of an immutable built-in style definition.
graphics::Shader * newMeshShader(graphics::Graphics *gfx) const
Create a mesh technique shader and apply compatible overrides.
float getFloat(const std::string &name) const
Returns the float.
std::string getParamName(int index) const
Returns the param name.
bool hasParam(const std::string &name) const
True when param.
StylePass * newPass(graphics::Graphics *gfx) const
Create a post pass and apply this instance's parameter overrides.
void reset(const std::string &name)
Resets .
StyleInstance(std::string style)
Style instance.
int getPriority() const
Returns the priority.
float getParamMin(const std::string &name) const
Returns the param min.
void setFloat(const std::string &name, float value)
Sets the float.
void applyToShader(graphics::Shader *shader) const
Apply current overrides to an existing compatible shader.
int getParamCount() const
Returns the param count.
void resetAll()
Resets all.
std::string getStage() const
Returns the stage.
float getParamDefault(const std::string &name) const
Returns the param default.
bool isOverridden(const std::string &name) const
True when overridden.
bool requiresInput(const std::string &input) const
Requires input.
float getParamMax(const std::string &name) const
Returns the param max.
One stylized post-process pass bound to a style id (built-in or custom label). Draws a full-quad of t...
Definition StylePass.h:26
void applyCanvasTo(graphics::Graphics *gfx, graphics::Canvas *source, graphics::Canvas *dest)
Applies canvas to.
bool requiresInput(const std::string &input) const
Requires input.
bool hasParam(const std::string &name) const
True when param.
float getFloat(const std::string &name) const
Returns the float.
void setFloat(const std::string &name, float value)
Sets the float.
std::string getStage() const
Returns the stage.
void applyTo(graphics::Graphics *gfx, graphics::Texture *source, graphics::Canvas *dest)
Apply into an explicit destination canvas (restores previous canvas bind). Preferred hook for chains ...
void applyCanvas(graphics::Graphics *gfx, graphics::Canvas *source)
Applies canvas.
void apply(graphics::Graphics *gfx, graphics::Texture *source)
Apply style into the currently bound canvas / screen. Automatically uploads texel size + screen size ...
void setTime(float seconds)
Advance time-driven knobs (watercolor warp / ink jitter).
float getTime() const
Returns the time.
StylePass(const std::string &style, graphics::Shader *shader)
Style pass.
StyleInstance * getStyle(int index) const
Returns the style.
~StyleRecipe()
Style recipe.
void applyCanvas(graphics::Graphics *gfx, graphics::Canvas *source, graphics::Canvas *dest)
Applies canvas.
void compile(graphics::Graphics *gfx)
Compiles .
void apply(graphics::Graphics *gfx, graphics::Texture *source, graphics::Canvas *dest)
Applies .
void add(StyleInstance *instance)
Adds .
const char * postEffectStageName(PostEffectStage stage)
Post effect stage name.
Definition PostEffect.h:28
graphics::Shader * createMeshShader(graphics::Graphics *gfx, const std::string &style)
Creates mesh shader.
graphics::Shader * createPostShader(graphics::Graphics *gfx, const std::string &style)
Creates post shader.
int styleParamCount(const std::string &style)
Style param count.
const StyleParameterDesc * findStyleParameter(const std::string &style, const std::string &name)
Finds style parameter.
std::string styleParamName(const std::string &style, int index)
Style param name.
const StyleDefinition * findStyleDefinition(const std::string &style)
Return a built-in definition, or nullptr when the id is unknown.
Immutable capability metadata for one built-in stylization recipe.
Tooling metadata for one user-facing float parameter.