载入中...
搜索中...
未找到
StyleChain.cpp
浏览该文件的文档.
2
3#include "stylize/StylePass.h"
4
5#include "common/Exception.h"
6#include "graphics/Canvas.h"
7#include "graphics/Graphics.h"
8#include "graphics/Texture.h"
9
10#include <glm/vec4.hpp>
11
12namespace eve::stylize {
13
14void StyleChain::clear() { passes_.clear(); }
15
17 if (!pass) throw eve::Exception("StyleChain.add: null pass");
18 passes_.push_back(pass);
19}
20
21StylePass *StyleChain::getPass(int index) const {
22 if (index < 0 || index >= int(passes_.size())) return nullptr;
23 return passes_[size_t(index)];
24}
25
28 if (!gfx) throw eve::Exception("StyleChain.apply: null graphics");
29 if (!source) throw eve::Exception("StyleChain.apply: null source");
30 if (!dest) throw eve::Exception("StyleChain.apply: null dest");
31 if (passes_.empty()) throw eve::Exception("StyleChain.apply: no passes");
32
33 if (passes_.size() == 1) {
34 passes_[0]->applyTo(gfx, source, dest);
35 return;
36 }
37 if (!temp) throw eve::Exception("StyleChain.apply: temp canvas required for multi-pass");
38 if (!temp->getTexture())
39 throw eve::Exception("StyleChain.apply: temp canvas has no sampleable texture");
40 if (!dest->getTexture())
41 throw eve::Exception("StyleChain.apply: dest canvas has no sampleable texture");
42
43 // Alternate temp / dest. Final result must live in dest.
44 // n even: … → temp → dest
45 // n odd : … → dest → temp, then blit temp → dest
46 graphics::Texture *in = source;
47 const int n = int(passes_.size());
48 for (int i = 0; i < n; ++i) {
49 graphics::Canvas *out = (i & 1) ? dest : temp;
50 passes_[size_t(i)]->applyTo(gfx, in, out);
51 in = out->getTexture();
52 if (!in) throw eve::Exception("StyleChain.apply: intermediate texture missing");
53 }
54 if ((n & 1) != 0) {
55 // Last write was temp — blit into dest (no style shader).
56 graphics::Canvas *prev = gfx->getCanvas();
57 gfx->setCanvas(dest);
58 gfx->drawTexturedRect(temp->getTexture(), 0.f, 0.f, float(dest->getWidth()),
59 float(dest->getHeight()), glm::vec4(1.f, 1.f, 1.f, 1.f));
60 gfx->setCanvas(prev);
61 }
62}
63
66 if (!source) throw eve::Exception("StyleChain.applyCanvas: null source");
67 graphics::Texture *tex = source->getTexture();
68 if (!tex) throw eve::Exception("StyleChain.applyCanvas: source has no texture");
69 apply(gfx, tex, dest, temp);
70}
71
72} // namespace eve::stylize
glm::vec3 n
Definition Grass.cpp:64
int temp
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.
virtual void drawTexturedRect(Texture *texture, float x, float y, float w, float h, const Color &color)=0
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:17
void applyCanvas(graphics::Graphics *gfx, graphics::Canvas *source, graphics::Canvas *dest, graphics::Canvas *temp=nullptr)
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
void add(StylePass *pass)
One stylized post-process pass bound to a style id (built-in or custom label). Draws a full-quad of t...
Definition StylePass.h:22