载入中...
搜索中...
未找到
StylePass.cpp
浏览该文件的文档.
1#include "stylize/StylePass.h"
2
3#include "common/Exception.h"
4#include "graphics/Canvas.h"
5#include "graphics/Graphics.h"
6#include "graphics/Shader.h"
7#include "graphics/Texture.h"
9
10#include <glm/vec4.hpp>
11
12namespace eve::stylize {
13
14StylePass::StylePass(const std::string &style, graphics::Shader *shader)
15 : style_(style), shader_(shader) {
16 if (!shader_) throw eve::Exception("StylePass: null shader for style '%s'", style.c_str());
17 if (const StyleDefinition *def = findStyleDefinition(style_)) {
18 desc_.stage = def->stage;
19 desc_.priority = def->priority;
20 if (def->depth) desc_.inputs |= graphics::PostInputDepth;
21 if (def->normal) desc_.inputs |= graphics::PostInputNormal;
22 }
23}
24
25std::string StylePass::getStage() const { return graphics::postEffectStageName(desc_.stage); }
26
27bool StylePass::requiresInput(const std::string &input) const {
28 if (input == "color") return (desc_.inputs & graphics::PostInputColor) != 0;
29 if (input == "depth") return (desc_.inputs & graphics::PostInputDepth) != 0;
30 if (input == "normal") return (desc_.inputs & graphics::PostInputNormal) != 0;
31 if (input == "motion") return (desc_.inputs & graphics::PostInputMotion) != 0;
32 if (input == "objectId") return (desc_.inputs & graphics::PostInputObjectId) != 0;
33 return false;
34}
35
36bool StylePass::hasParam(const std::string &name) const {
37 return shader_ && shader_->hasUniform(name);
38}
39
40void StylePass::setFloat(const std::string &name, float value) {
41 if (!shader_) throw eve::Exception("StylePass.setFloat: null shader");
42 shader_->sendFloat(name, value);
43}
44
45float StylePass::getFloat(const std::string &name) const {
46 if (!shader_) throw eve::Exception("StylePass.getFloat: null shader");
47 float v = 0.f;
48 if (shader_->getFromVar(name, &v, sizeof(v)) != int(sizeof(v)))
49 throw eve::Exception("StylePass.getFloat: missing param '%s'", name.c_str());
50 return v;
51}
52
53void StylePass::setTime(float seconds) {
54 if (hasParam("time")) setFloat("time", seconds);
55}
56
57float StylePass::getTime() const {
58 if (!hasParam("time")) return 0.f;
59 return getFloat("time");
60}
61
62void StylePass::uploadScreenUniforms(int width, int height) {
63 if (width < 1) width = 1;
64 if (height < 1) height = 1;
65 if (hasParam("texelW")) setFloat("texelW", 1.f / float(width));
66 if (hasParam("texelH")) setFloat("texelH", 1.f / float(height));
67 if (hasParam("screenW")) setFloat("screenW", float(width));
68 if (hasParam("screenH")) setFloat("screenH", float(height));
69}
70
72 if (!gfx) throw eve::Exception("StylePass.apply: null graphics");
73 if (!source) throw eve::Exception("StylePass.apply: null source texture");
74 if (!shader_) throw eve::Exception("StylePass.apply: null shader");
75
76 const int w = source->getWidth();
77 const int h = source->getHeight();
78 uploadScreenUniforms(w, h);
79
80 // Prefer destination canvas size for pixel grid when available.
81 if (auto *dst = gfx->getCanvas()) {
82 if (hasParam("screenW")) setFloat("screenW", float(dst->getWidth()));
83 if (hasParam("screenH")) setFloat("screenH", float(dst->getHeight()));
84 } else {
85 if (hasParam("screenW")) setFloat("screenW", float(gfx->getWidth()));
86 if (hasParam("screenH")) setFloat("screenH", float(gfx->getHeight()));
87 }
88
89 const float dw = gfx->getCanvas() ? float(gfx->getCanvas()->getWidth()) : float(gfx->getWidth());
90 const float dh =
91 gfx->getCanvas() ? float(gfx->getCanvas()->getHeight()) : float(gfx->getHeight());
92
93 gfx->drawTexturedRectShader(source, shader_, 0.f, 0.f, dw, dh, glm::vec4(1.f, 1.f, 1.f, 1.f));
94}
95
97 if (!source) throw eve::Exception("StylePass.applyCanvas: null canvas");
98 graphics::Texture *tex = source->getTexture();
99 if (!tex) throw eve::Exception("StylePass.applyCanvas: canvas has no sampleable texture");
100 apply(gfx, tex);
101}
102
104 graphics::Canvas *dest) {
105 if (!gfx) throw eve::Exception("StylePass.applyTo: null graphics");
106 if (!dest) throw eve::Exception("StylePass.applyTo: null dest");
107 graphics::Canvas *prev = gfx->getCanvas();
108 gfx->setCanvas(dest);
109 apply(gfx, source);
110 gfx->setCanvas(prev);
111}
112
114 graphics::Canvas *dest) {
115 if (!source) throw eve::Exception("StylePass.applyCanvasTo: null source");
116 graphics::Texture *tex = source->getTexture();
117 if (!tex) throw eve::Exception("StylePass.applyCanvasTo: source has no sampleable texture");
118 applyTo(gfx, tex, dest);
119}
120
121} // namespace eve::stylize
double value
float w
Definition AnimClip.cpp:738
EvpackChunkInput input
Definition Evpack.cpp:170
float v
int h
std::uint32_t height
std::uint32_t width
std::string name
Shader * shader
const UnitySourceAsset & source
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 int getHeight() const =0
Returns the height.
int getWidth() const
Returns the width.
Definition Graphics.h:502
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.
int getHeight() const
Returns the height.
Definition Graphics.h:504
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:39
bool hasUniform(const std::string &name) const
Definition Shader.cpp:59
void sendFloat(const std::string &name, float x)
Definition Shader.cpp:95
int getFromVar(const std::string &name, void *data, size_t size) const
Definition Shader.cpp:84
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:18
void applyCanvasTo(graphics::Graphics *gfx, graphics::Canvas *source, graphics::Canvas *dest)
Applies canvas to.
bool requiresInput(const std::string &input) const
Requires input.
Definition StylePass.cpp:27
bool hasParam(const std::string &name) const
True when param.
Definition StylePass.cpp:36
float getFloat(const std::string &name) const
Returns the float.
Definition StylePass.cpp:45
void setFloat(const std::string &name, float value)
Sets the float.
Definition StylePass.cpp:40
std::string getStage() const
Returns the stage.
Definition StylePass.cpp:25
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.
Definition StylePass.cpp:96
void apply(graphics::Graphics *gfx, graphics::Texture *source)
Apply style into the currently bound canvas / screen. Automatically uploads texel size + screen size ...
Definition StylePass.cpp:71
void setTime(float seconds)
Advance time-driven knobs (watercolor warp / ink jitter).
Definition StylePass.cpp:53
float getTime() const
Returns the time.
Definition StylePass.cpp:57
StylePass(const std::string &style, graphics::Shader *shader)
Style pass.
Definition StylePass.cpp:14
const char * postEffectStageName(PostEffectStage stage)
Post effect stage name.
Definition PostEffect.h:28
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.