载入中...
搜索中...
未找到
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"
8
9#include <glm/vec4.hpp>
10
11namespace eve::stylize {
12
13StylePass::StylePass(const std::string &style, graphics::Shader *shader)
14 : style_(style), shader_(shader) {
15 if (!shader_) throw eve::Exception("StylePass: null shader for style '%s'", style.c_str());
16}
17
18bool StylePass::hasParam(const std::string &name) const {
19 return shader_ && shader_->hasUniform(name);
20}
21
22void StylePass::setFloat(const std::string &name, float value) {
23 if (!shader_) throw eve::Exception("StylePass.setFloat: null shader");
24 shader_->sendFloat(name, value);
25}
26
27float StylePass::getFloat(const std::string &name) const {
28 if (!shader_) throw eve::Exception("StylePass.getFloat: null shader");
29 float v = 0.f;
30 if (shader_->getFromVar(name, &v, sizeof(v)) != int(sizeof(v)))
31 throw eve::Exception("StylePass.getFloat: missing param '%s'", name.c_str());
32 return v;
33}
34
35void StylePass::setTime(float seconds) {
36 if (hasParam("time")) setFloat("time", seconds);
37}
38
39float StylePass::getTime() const {
40 if (!hasParam("time")) return 0.f;
41 return getFloat("time");
42}
43
44void StylePass::uploadScreenUniforms(int width, int height) {
45 if (width < 1) width = 1;
46 if (height < 1) height = 1;
47 if (hasParam("texelW")) setFloat("texelW", 1.f / float(width));
48 if (hasParam("texelH")) setFloat("texelH", 1.f / float(height));
49 if (hasParam("screenW")) setFloat("screenW", float(width));
50 if (hasParam("screenH")) setFloat("screenH", float(height));
51}
52
54 if (!gfx) throw eve::Exception("StylePass.apply: null graphics");
55 if (!source) throw eve::Exception("StylePass.apply: null source texture");
56 if (!shader_) throw eve::Exception("StylePass.apply: null shader");
57
58 const int w = source->getWidth();
59 const int h = source->getHeight();
60 uploadScreenUniforms(w, h);
61
62 // Prefer destination canvas size for pixel grid when available.
63 if (auto *dst = gfx->getCanvas()) {
64 if (hasParam("screenW")) setFloat("screenW", float(dst->getWidth()));
65 if (hasParam("screenH")) setFloat("screenH", float(dst->getHeight()));
66 } else {
67 if (hasParam("screenW")) setFloat("screenW", float(gfx->getWidth()));
68 if (hasParam("screenH")) setFloat("screenH", float(gfx->getHeight()));
69 }
70
71 const float dw = gfx->getCanvas() ? float(gfx->getCanvas()->getWidth()) : float(gfx->getWidth());
72 const float dh =
73 gfx->getCanvas() ? float(gfx->getCanvas()->getHeight()) : float(gfx->getHeight());
74
75 gfx->drawTexturedRectShader(source, shader_, 0.f, 0.f, dw, dh, glm::vec4(1.f, 1.f, 1.f, 1.f));
76}
77
79 if (!source) throw eve::Exception("StylePass.applyCanvas: null canvas");
80 graphics::Texture *tex = source->getTexture();
81 if (!tex) throw eve::Exception("StylePass.applyCanvas: canvas has no sampleable texture");
82 apply(gfx, tex);
83}
84
86 graphics::Canvas *dest) {
87 if (!gfx) throw eve::Exception("StylePass.applyTo: null graphics");
88 if (!dest) throw eve::Exception("StylePass.applyTo: null dest");
89 graphics::Canvas *prev = gfx->getCanvas();
90 gfx->setCanvas(dest);
91 apply(gfx, source);
92 gfx->setCanvas(prev);
93}
94
96 graphics::Canvas *dest) {
97 if (!source) throw eve::Exception("StylePass.applyCanvasTo: null source");
98 graphics::Texture *tex = source->getTexture();
99 if (!tex) throw eve::Exception("StylePass.applyCanvasTo: source has no sampleable texture");
100 applyTo(gfx, tex, dest);
101}
102
103} // namespace eve::stylize
std::string value
float height
Definition Grass.cpp:235
int h
int w
int width
Shader * shader
const char * name
Definition RockMesh.cpp:21
int v
virtual int getWidth() const =0
virtual Texture * getTexture()=0
Sampleable color buffer; screen Canvas returns nullptr.
virtual int getHeight() const =0
virtual Canvas * getCanvas() const =0
virtual void setCanvas(Canvas *canvas)=0
nullptr or this → screen. Switching flushes pending draws to the previous target.
int getHeight() const
Definition Graphics.h:118
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:30
bool hasUniform(const std::string &name) const
Definition Shader.cpp:46
void sendFloat(const std::string &name, float x)
Definition Shader.cpp:82
int getFromVar(const std::string &name, void *data, size_t size) const
Definition Shader.cpp:71
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:17
int getWidth() const
Definition Texture.h:26
int getHeight() const
Definition Texture.h:27
void applyCanvasTo(graphics::Graphics *gfx, graphics::Canvas *source, graphics::Canvas *dest)
Definition StylePass.cpp:95
bool hasParam(const std::string &name) const
Definition StylePass.cpp:18
float getFloat(const std::string &name) const
Definition StylePass.cpp:27
void setFloat(const std::string &name, float value)
Definition StylePass.cpp:22
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 ...
Definition StylePass.cpp:85
void applyCanvas(graphics::Graphics *gfx, graphics::Canvas *source)
Definition StylePass.cpp:78
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:53
void setTime(float seconds)
Advance time-driven knobs (watercolor warp / ink jitter).
Definition StylePass.cpp:35
float getTime() const
Definition StylePass.cpp:39
StylePass(const std::string &style, graphics::Shader *shader)
Definition StylePass.cpp:13