10#include "graphics/shaders/aa_fxaa_frag_spv.inc"
11#include "graphics/shaders/aa_nfaa_frag_spv.inc"
12#include "graphics/shaders/aa_smaa_frag_spv.inc"
13#include "graphics/shaders/aa_ssaa_frag_spv.inc"
14#include "graphics/shaders/aa_taa_frag_spv.inc"
22#include <glm/vec4.hpp>
23#include <glm/geometric.hpp>
24#include <glm/gtc/matrix_inverse.hpp>
25#include <glm/gtc/type_ptr.hpp>
30std::vector<uint32_t> copySpv(
const uint32_t *data,
size_t count) {
31 return std::vector<uint32_t>(data, data +
count);
34Shader *newPostShader(Graphics *gfx,
const std::vector<uint32_t> &
frag,
const char *wgsl) {
35 if (gfx->getBackendName() ==
"webgpu")
37 return gfx->newShaderFromSpv({},
frag);
40Shader *createFxaaShader(Graphics *gfx) {
42 auto frag = copySpv(aa_fxaa_frag_spv, aa_fxaa_frag_spv_count);
44 if (!sh || !sh->gpuHandle)
throw eve::Exception(
"AntiAliasing: failed to create FXAA shader");
45 sh->declareFloat(
"texelW");
46 sh->declareFloat(
"texelH");
47 sh->declareFloat(
"edgeThreshold");
48 sh->declareFloat(
"edgeThresholdMin");
49 sh->declareFloat(
"subpix");
50 sh->declareFloat(
"quality");
51 sh->sendFloat(
"texelW", 1.f / 256.f);
52 sh->sendFloat(
"texelH", 1.f / 256.f);
53 sh->sendFloat(
"edgeThreshold", 0.166f);
54 sh->sendFloat(
"edgeThresholdMin", 0.0833f);
55 sh->sendFloat(
"subpix", 0.75f);
56 sh->sendFloat(
"quality", 1.f);
60Shader *createSmaaShader(Graphics *gfx) {
62 auto frag = copySpv(aa_smaa_frag_spv, aa_smaa_frag_spv_count);
64 if (!sh || !sh->gpuHandle)
throw eve::Exception(
"AntiAliasing: failed to create SMAA shader");
65 sh->declareFloat(
"texelW");
66 sh->declareFloat(
"texelH");
67 sh->declareFloat(
"threshold");
68 sh->declareFloat(
"localContrast");
69 sh->declareFloat(
"maxSearch");
70 sh->declareFloat(
"cornerRounding");
71 sh->sendFloat(
"texelW", 1.f / 256.f);
72 sh->sendFloat(
"texelH", 1.f / 256.f);
73 sh->sendFloat(
"threshold", 0.1f);
74 sh->sendFloat(
"localContrast", 0.5f);
75 sh->sendFloat(
"maxSearch", 8.f);
76 sh->sendFloat(
"cornerRounding", 0.25f);
80Shader *createSsaaShader(Graphics *gfx) {
82 auto frag = copySpv(aa_ssaa_frag_spv, aa_ssaa_frag_spv_count);
84 if (!sh || !sh->gpuHandle)
throw eve::Exception(
"AntiAliasing: failed to create SSAA shader");
85 sh->declareFloat(
"texelW");
86 sh->declareFloat(
"texelH");
87 sh->declareFloat(
"scale");
88 sh->declareFloat(
"kernel");
89 sh->declareFloat(
"sampleRadius");
90 sh->sendFloat(
"texelW", 1.f / 512.f);
91 sh->sendFloat(
"texelH", 1.f / 512.f);
92 sh->sendFloat(
"scale", 2.f);
93 sh->sendFloat(
"kernel", 1.f);
94 sh->sendFloat(
"sampleRadius", 1.f);
98Shader *createNfaaShader(Graphics *gfx) {
100 auto frag = copySpv(aa_nfaa_frag_spv, aa_nfaa_frag_spv_count);
102 if (!sh || !sh->gpuHandle)
throw eve::Exception(
"AntiAliasing: failed to create NFAA shader");
103 sh->declareFloat(
"texelW");
104 sh->declareFloat(
"texelH");
105 sh->declareFloat(
"strength");
106 sh->declareFloat(
"power");
107 sh->declareFloat(
"blurScale");
108 sh->sendFloat(
"texelW", 1.f / 256.f);
109 sh->sendFloat(
"texelH", 1.f / 256.f);
110 sh->sendFloat(
"strength", 1.f);
111 sh->sendFloat(
"power", 1.f);
112 sh->sendFloat(
"blurScale", 1.f);
116Shader *createTaaShader(Graphics *gfx) {
118 auto frag = copySpv(aa_taa_frag_spv, aa_taa_frag_spv_count);
120 if (!sh || !sh->gpuHandle)
throw eve::Exception(
"AntiAliasing: failed to create TAA shader");
121 sh->declareFloat(
"texelW");
122 sh->declareFloat(
"texelH");
123 sh->declareFloat(
"blendCurrent");
124 sh->declareFloat(
"clampAmount");
125 sh->declareFloat(
"historyValid");
126 sh->declareFloat(
"jitterDeltaX");
127 sh->declareFloat(
"jitterDeltaY");
128 for (
int i = 0; i < 16; ++i) sh->declareFloat(
"reprojection" + std::to_string(i));
129 sh->declareFloat(
"temporalNearZ");
130 sh->declareFloat(
"temporalFarZ");
131 sh->declareFloat(
"reprojectionValid");
132 sh->declareFloat(
"motionValid");
133 sh->sendFloat(
"texelW", 1.f / 256.f);
134 sh->sendFloat(
"texelH", 1.f / 256.f);
135 sh->sendFloat(
"blendCurrent", 0.92f);
136 sh->sendFloat(
"clampAmount", 0.85f);
137 sh->sendFloat(
"historyValid", 0.f);
138 sh->sendFloat(
"jitterDeltaX", 0.f);
139 sh->sendFloat(
"jitterDeltaY", 0.f);
140 for (
int i = 0; i < 16; ++i)
141 sh->sendFloat(
"reprojection" + std::to_string(i), i % 5 == 0 ? 1.f : 0.f);
142 sh->sendFloat(
"temporalNearZ", 0.1f);
143 sh->sendFloat(
"temporalFarZ", 100.f);
144 sh->sendFloat(
"reprojectionValid", 0.f);
145 sh->sendFloat(
"motionValid", 0.f);
152 fxaa_ = createFxaaShader(gfx);
153 smaa_ = createSmaaShader(gfx);
154 ssaa_ = createSsaaShader(gfx);
155 nfaa_ = createNfaaShader(gfx);
156 taa_ = createTaaShader(gfx);
157 applyQualityDefaults();
162void AntiAliasing::applyQualityDefaults() {
163 if (quality_ ==
"low") {
164 fxaa_->
sendFloat(
"edgeThreshold", 0.25f);
165 fxaa_->
sendFloat(
"edgeThresholdMin", 0.0833f);
169 smaa_->
sendFloat(
"localContrast", 0.55f);
171 smaa_->
sendFloat(
"cornerRounding", 0.1f);
182 if (quality_ ==
"high") {
183 fxaa_->
sendFloat(
"edgeThreshold", 0.125f);
184 fxaa_->
sendFloat(
"edgeThresholdMin", 0.0312f);
190 smaa_->
sendFloat(
"cornerRounding", 0.4f);
203 fxaa_->
sendFloat(
"edgeThreshold", 0.166f);
204 fxaa_->
sendFloat(
"edgeThresholdMin", 0.0833f);
210 smaa_->
sendFloat(
"cornerRounding", 0.25f);
222 if (quality ==
"low" || quality ==
"medium" || quality ==
"high")
226 applyQualityDefaults();
230 const std::string next =
231 (mode ==
"fxaa" || mode ==
"smaa" || mode ==
"ssaa" || mode ==
"nfaa" || mode ==
"taa")
234 if (next ==
"taa" && mode_ !=
"taa") resetHistory();
238void AntiAliasing::resetHistory() {
239 taaHistoryValid_ =
false;
242void AntiAliasing::ensureHistoryCanvases(
int width,
int height) {
245 if (!taaHistoryA_ || !taaHistoryB_ || historyW_ !=
width || historyH_ !=
height) {
248 if (!taaHistoryA_ || !taaHistoryB_)
249 throw eve::Exception(
"AntiAliasing: failed to allocate temporal history canvases");
250 historyRead_ = taaHistoryA_;
251 historyWrite_ = taaHistoryB_;
256 if (!historyRead_) historyRead_ = taaHistoryA_;
257 if (!historyWrite_) historyWrite_ = taaHistoryB_;
260void AntiAliasing::swapHistoryBuffers() {
261 std::swap(historyRead_, historyWrite_);
264Shader *AntiAliasing::shaderForMode()
const {
265 if (mode_ ==
"taa")
return taa_;
266 if (mode_ ==
"smaa")
return smaa_;
267 if (mode_ ==
"ssaa")
return ssaa_;
268 if (mode_ ==
"nfaa")
return nfaa_;
275 Shader *sh = shaderForMode();
280 Shader *sh = shaderForMode();
281 if (!sh)
throw eve::Exception(
"AntiAliasing.setFloat: null shader");
286 Shader *sh = shaderForMode();
287 if (!sh)
throw eve::Exception(
"AntiAliasing.getFloat: null shader");
295 if (mode_ !=
"ssaa")
return 1.f;
296 if (quality_ ==
"high")
return 4.f;
301 if (destSize < 1) destSize = 1;
303 return std::max(1,
int(std::floor(
float(destSize) *
s)));
308 const int w = std::max(1,
source->getWidth());
309 const int h = std::max(1,
source->getHeight());
310 const float tw = 1.f / float(
w);
311 const float th = 1.f / float(
h);
312 Shader *sh = shaderForMode();
315 if (mode_ ==
"ssaa" && sh->
hasUniform(
"scale")) {
324void AntiAliasing::drawFullscreen(Graphics *gfx, Texture *
source, Shader *
shader) {
328 uploadScreenUniforms(
source);
329 const float dw = gfx->getCanvas() ? float(gfx->getCanvas()->getWidth()) : float(gfx->getWidth());
331 gfx->getCanvas() ? float(gfx->getCanvas()->getHeight()) : float(gfx->getHeight());
334 gfx->drawTexturedRectShaderUV(
source,
shader, 0.f, 0.f, dw, dh, 0.f, 0.f, 1.f, 1.f, glm::vec4(1.f, 1.f, 1.f, 1.f),
342 ensureHistoryCanvases(std::max(1,
source->getWidth()), std::max(1,
source->getHeight()));
343 if (!historyRead_ || !historyWrite_)
344 throw eve::Exception(
"AntiAliasing.beginTemporalFrame: history canvases missing");
345 uploadScreenUniforms(
source);
346 taa_->
sendFloat(
"historyValid", taaHistoryValid_ ? 1.f : 0.f);
347 const glm::vec2 jitterDelta = (previousJitterNdc_ - temporalJitterNdc_) * 0.5f;
348 taa_->
sendFloat(
"jitterDeltaX", jitterDelta.x);
349 taa_->
sendFloat(
"jitterDeltaY", jitterDelta.y);
350 const glm::mat4 reprojection = previousViewProj_ * glm::inverse(temporalViewProj_);
351 const float *matrixData = glm::value_ptr(reprojection);
352 for (
int i = 0; i < 16; ++i)
353 taa_->
sendFloat(
"reprojection" + std::to_string(i), matrixData[i]);
354 taa_->
sendFloat(
"temporalNearZ", temporalNearZ_);
355 taa_->
sendFloat(
"temporalFarZ", temporalFarZ_);
357 taaHistoryValid_ && temporalViewValid_ ? 1.f : 0.f);
358 taa_->
sendFloat(
"motionValid", motion ? 1.f : 0.f);
359 return historyWrite_;
363 return taaHistoryValid_ && historyRead_ ? historyRead_->
getTexture() :
nullptr;
367 taaHistoryValid_ =
true;
368 previousJitterNdc_ = temporalJitterNdc_;
369 previousViewProj_ = temporalViewProj_;
370 temporalViewValid_ =
true;
371 temporalObjectHistory_ = std::move(temporalObjectPending_);
372 temporalObjectPending_.clear();
373 ++temporalFrameIndex_;
374 swapHistoryBuffers();
378 auto halton = [](uint32_t
index, uint32_t base) {
388 const uint32_t sample = temporalFrameIndex_ % 8u + 1u;
389 temporalJitterNdc_.x = 2.f * (halton(sample, 2u) - 0.5f) /
float(std::max(1,
width));
390 temporalJitterNdc_.y = 2.f * (halton(sample, 3u) - 0.5f) /
float(std::max(1,
height));
391 return temporalJitterNdc_;
397 const float forwardLength = glm::length(
forward);
398 if (forwardLength > 1e-5f)
forward /= forwardLength;
399 if (temporalCameraValid_) {
400 const float positionCut = std::max(1.f, forwardLength * 0.25f);
401 const bool cut = glm::length(
eye - temporalEye_) > positionCut ||
402 glm::dot(
forward, temporalForward_) < 0.94f ||
403 std::fabs(fovYDeg - temporalFovY_) > 5.f;
411 temporalFovY_ = fovYDeg;
412 temporalCameraValid_ =
true;
422 temporalViewProj_ = viewProjection;
423 temporalNearZ_ = std::max(nearZ, 1e-4f);
424 temporalFarZ_ = std::max(farZ, temporalNearZ_ + 1e-3f);
428 const glm::mat4 &
model) {
429 if (!objectKey)
return glm::vec2(0.f);
430 temporalObjectPending_[objectKey] =
model;
431 const auto previous = temporalObjectHistory_.find(objectKey);
432 if (!taaHistoryValid_ || !temporalViewValid_ ||
previous == temporalObjectHistory_.end())
433 return glm::vec2(0.f);
434 const glm::vec4
origin(0.f, 0.f, 0.f, 1.f);
435 const glm::vec4 staticPrevious = previousViewProj_ *
model *
origin;
436 const glm::vec4 actualPrevious = previousViewProj_ *
previous->second *
origin;
437 if (std::fabs(staticPrevious.w) < 1e-6f || std::fabs(actualPrevious.w) < 1e-6f)
438 return glm::vec2(0.f);
439 const glm::vec2 staticUv = glm::vec2(staticPrevious) / staticPrevious.w * 0.5f + 0.5f;
440 const glm::vec2 actualUv = glm::vec2(actualPrevious) / actualPrevious.w * 0.5f + 0.5f;
441 return glm::clamp(actualUv - staticUv, glm::vec2(-1.f), glm::vec2(1.f));
445 if (!gfx)
throw eve::Exception(
"AntiAliasing.applyTemporal: null graphics");
449 const float dw = finalCanvas ? float(finalCanvas->
getWidth()) : float(gfx->
getWidth());
450 const float dh = finalCanvas ? float(finalCanvas->
getHeight()) : float(gfx->
getHeight());
454 if (!sh)
throw eve::Exception(
"AntiAliasing.applyTemporal: missing taa shader");
460 glm::vec4(1.f, 1.f, 1.f, 1.f));
465 gfx->
drawTexturedRectShaderUV(
resolved,
nullptr, 0.f, 0.f, dw, dh, 0.f, 0.f, 1.f, 1.f,
466 glm::vec4(1.f, 1.f, 1.f, 1.f),
false,
472 if (!gfx)
throw eve::Exception(
"AntiAliasing.resolveTemporal: null graphics");
483 glm::vec4(1.f, 1.f, 1.f, 1.f));
492 float height,
float r,
float g,
float b,
float a) {
493 if (!gfx)
throw eve::Exception(
"AntiAliasing.applyTemporalRect: null graphics");
500 if (!sh)
throw eve::Exception(
"AntiAliasing.applyTemporal: missing taa shader");
507 glm::vec4(1.f, 1.f, 1.f, 1.f));
513 gfx->
drawTexturedRectShaderUV(
resolved,
nullptr,
x,
y,
width,
height, 0.f, 0.f, 1.f, 1.f,
523 drawFullscreen(gfx,
source, shaderForMode());
527 if (!gfx)
throw eve::Exception(
"AntiAliasing.applyTo: null graphics");
528 if (!dest)
throw eve::Exception(
"AntiAliasing.applyTo: null dest");
539 if (!tex)
throw eve::Exception(
"AntiAliasing.applyCanvas: source has no sampleable texture");
546 if (!tex)
throw eve::Exception(
"AntiAliasing.applyCanvasTo: source has no sampleable texture");
std::shared_ptr< const OceanSprayFrame > previous
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
EVENGINE_API_FOUNDATION public API.
void setTemporalCamera(const glm::vec3 &eye, const glm::vec3 &target, float fovYDeg)
Detect camera cuts and invalidate incompatible temporal history.
bool hasParam(const std::string &name) const
True when param.
void apply(Graphics *gfx, Texture *source)
Apply current mode to source, writing into the currently bound canvas / screen. Uploads texelW/texelH...
Texture * getTemporalReadTexture() const
Previous resolved frame, or nullptr when history is invalid. @lifetime Returned texture is borrowed f...
glm::vec2 prepareTemporalJitter(int width, int height)
Return this frame's Halton projection jitter in NDC units.
Canvas * beginTemporalFrame(Texture *source, Texture *motion=nullptr)
Prepare uniforms/resources for a backend-recorded temporal pass. @lifetime Returned canvas is borrowe...
void setMode(const std::string &mode)
"fxaa" | "smaa" | "ssaa" | "nfaa" | "taa" (unknown → fxaa).
void invalidateTemporalHistory()
Discard TAA history after a render-chain discontinuity.
void prepareSource(Texture *source)
Upload texel uniforms from source without drawing (3D scene-color resolve).
Shader * getTaaShader() const
Returns the taa shader.
glm::vec2 prepareTemporalObjectMotion(const void *objectKey, const glm::mat4 &model)
Per-object UV correction relative to static-world camera reprojection.
Texture * resolveTemporal(Graphics *gfx, Texture *source, Texture *motion=nullptr)
Resolve TAA into history and return the linear HDR result without compositing it. @lifetime Returned ...
float suggestScale() const
Suggested supersample scale for the active quality when using "ssaa" (2 for low/medium,...
void applyTo(Graphics *gfx, Texture *source, Canvas *dest)
Applies to.
void setTemporalViewProjection(const glm::mat4 &viewProjection, float nearZ, float farZ)
Set the jittered camera matrix used for depth-based history reprojection.
~AntiAliasing()
Anti aliasing.
float getFloat(const std::string &name) const
Returns the float.
void applyTemporal(Graphics *gfx, Texture *source, Texture *motion=nullptr)
Applies temporal.
Shader * getShader() const
Returns the shader.
void setFloat(const std::string &name, float value)
Sets the float.
AntiAliasing(Graphics *gfx)
Anti aliasing.
void applyCanvas(Graphics *gfx, Canvas *source)
Applies canvas.
int resolutionFor(int destSize) const
Resolution for.
void applyCanvasTo(Graphics *gfx, Canvas *source, Canvas *dest)
Applies canvas to.
void endTemporalFrame()
Mark the prepared write target valid and rotate temporal buffers.
void applyTemporalRect(Graphics *gfx, Texture *source, float x, float y, float width, float height, float r=1.f, float g=1.f, float b=1.f, float a=1.f)
Resolve TAA history and composite it into an arbitrary destination rectangle.
void setQuality(const std::string &quality)
"low" | "medium" | "high" (unknown → medium).
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.
void invalidateHistory()
Invalidate eye-adaptation history, for camera cuts and mode changes.
int getWidth() const
Returns the width.
virtual void drawTexturedRectShaderUV(Texture *texture, Shader *shader, float x, float y, float w, float h, float u0, float v0, float u1, float v1, const Color &color, bool rotatedUV=false, BlendMode blend=BlendMode::Alpha)=0
UV draw with an explicit Shader (nullptr = default textured pipeline).
Exposure * pipelineExposure()
Pipeline-owned GPU exposure metering and eye adaptation. @lifetime Returned effect remains valid unti...
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 drawTexturedRectShaderDepthMotion(Texture *color, Texture *depth, Texture *motion, Shader *shader, float x, float y, float w, float h, const Color &tint)=0
Post draw with color, depth/history and motion/reactive textures.
virtual Canvas * newHDRCanvas(int width, int height)=0
Create an engine-internal linear RGBA16F post-process target.
int getHeight() const
Returns the height.
bool hasUniform(const std::string &name) const
void sendFloat(const std::string &name, float x)
int getFromVar(const std::string &name, void *data, size_t size) const
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
constexpr const char * kSmaa
constexpr const char * kPostCommon
constexpr const char * kTaa
constexpr const char * kFxaa
constexpr const char * kNfaa
constexpr const char * kSsaa
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...