载入中...
搜索中...
未找到
StyleRecipe.cpp
浏览该文件的文档.
2
5#include "stylize/StylePass.h"
6
7#include "common/Exception.h"
8#include "graphics/Canvas.h"
9#include "graphics/Graphics.h"
10#include "graphics/Texture.h"
11
12#include <algorithm>
13
14namespace eve::stylize {
15
17
19 instances_.clear();
20 passes_.clear();
21 stage_.clear();
22 compiled_ = false;
23}
24
26 if (!instance) throw eve::Exception("StyleRecipe.add: null instance");
27 instances_.push_back(instance);
28 compiled_ = false;
29}
30
32 if (index < 0 || index >= int(instances_.size())) return nullptr;
33 return instances_[size_t(index)];
34}
35
37 if (!gfx) throw eve::Exception("StyleRecipe.compile: null graphics");
38 if (instances_.empty()) throw eve::Exception("StyleRecipe.compile: no styles");
39
40 if (graphics_ != gfx) {
41 scratch_ = nullptr;
42 scratchWidth_ = 0;
43 scratchHeight_ = 0;
44 }
45 std::stable_sort(instances_.begin(), instances_.end(),
46 [](StyleInstance* a, StyleInstance* b) { return a->getPriority() < b->getPriority(); });
47 stage_ = instances_.front()->getStage();
48 passes_.clear();
49 for (StyleInstance* instance : instances_) {
50 if (instance->getStage() != stage_)
51 throw eve::Exception("StyleRecipe.compile: cannot mix stages '%s' and '%s'", stage_.c_str(),
52 instance->getStage().c_str());
53 passes_.emplace_back(instance->newPass(gfx));
54 }
55 graphics_ = gfx;
56 compiled_ = true;
57}
58
59void StyleRecipe::ensureScratch(graphics::Graphics* gfx, graphics::Canvas* dest) {
60 if (passes_.size() < 2) return;
61 const int width = dest->getWidth();
62 const int height = dest->getHeight();
63 if (graphics_ != gfx || !scratch_ || scratchWidth_ != width || scratchHeight_ != height) {
64 scratch_ = gfx->newCanvas(width, height);
65 scratchWidth_ = width;
66 scratchHeight_ = height;
67 graphics_ = gfx;
68 }
69}
70
72 if (!compiled_) throw eve::Exception("StyleRecipe.apply: compile first");
73 if (graphics_ != gfx) throw eve::Exception("StyleRecipe.apply: compiled for another Graphics");
74 if (!source || !dest) throw eve::Exception("StyleRecipe.apply: null source or destination");
75 ensureScratch(gfx, dest);
76
77 StyleChain chain;
78 for (const auto& pass : passes_) chain.add(pass.get());
79 chain.apply(gfx, source, dest, scratch_);
80}
81
83 if (!source || !source->getTexture()) throw eve::Exception("StyleRecipe.applyCanvas: source has no texture");
84 apply(gfx, source->getTexture(), dest);
85}
86
87} // namespace eve::stylize
std::uint32_t height
std::uint32_t width
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
uint32_t index
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 Texture * getTexture()=0
Sampleable color buffer; screen Canvas returns nullptr.
virtual int getHeight() const =0
Returns the height.
virtual Canvas * newCanvas(int width, int height)=0
Create an offscreen render target (sampleable). Owned by Graphics.
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:18
Ordered multi-pass stylize pipeline (extension point).
Definition StyleChain.h:29
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...
void add(StylePass *pass)
Adds .
Mutable parameter instance of an immutable built-in style definition.
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 .