载入中...
搜索中...
未找到
AntiAliasing.cpp
浏览该文件的文档.
2#include "common/Profile.h"
3
4#include "common/Exception.h"
5#include "graphics/Canvas.h"
6#include "graphics/Exposure.h"
7#include "graphics/Graphics.h"
8#include "graphics/Shader.h"
9#include "graphics/Texture.h"
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"
16
17#include <algorithm>
18#include <cmath>
19#include <cstdint>
20#include <vector>
21
22#include <glm/vec4.hpp>
23#include <glm/geometric.hpp>
24#include <glm/gtc/matrix_inverse.hpp>
25#include <glm/gtc/type_ptr.hpp>
26
27namespace eve::graphics {
28namespace {
29
30std::vector<uint32_t> copySpv(const uint32_t *data, size_t count) {
31 return std::vector<uint32_t>(data, data + count);
32}
33
34Shader *newPostShader(Graphics *gfx, const std::vector<uint32_t> &frag, const char *wgsl) {
35 if (gfx->getBackendName() == "webgpu")
36 return gfx->newShaderFromWgsl({}, std::string(shaders::kPostCommon) + wgsl);
37 return gfx->newShaderFromSpv({}, frag);
38}
39
40Shader *createFxaaShader(Graphics *gfx) {
41 if (!gfx) throw eve::Exception("AntiAliasing: null graphics");
42 auto frag = copySpv(aa_fxaa_frag_spv, aa_fxaa_frag_spv_count);
43 Shader *sh = newPostShader(gfx, frag, shaders::kFxaa);
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);
57 return sh;
58}
59
60Shader *createSmaaShader(Graphics *gfx) {
61 if (!gfx) throw eve::Exception("AntiAliasing: null graphics");
62 auto frag = copySpv(aa_smaa_frag_spv, aa_smaa_frag_spv_count);
63 Shader *sh = newPostShader(gfx, frag, shaders::kSmaa);
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);
77 return sh;
78}
79
80Shader *createSsaaShader(Graphics *gfx) {
81 if (!gfx) throw eve::Exception("AntiAliasing: null graphics");
82 auto frag = copySpv(aa_ssaa_frag_spv, aa_ssaa_frag_spv_count);
83 Shader *sh = newPostShader(gfx, frag, shaders::kSsaa);
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); // tent
94 sh->sendFloat("sampleRadius", 1.f);
95 return sh;
96}
97
98Shader *createNfaaShader(Graphics *gfx) {
99 if (!gfx) throw eve::Exception("AntiAliasing: null graphics");
100 auto frag = copySpv(aa_nfaa_frag_spv, aa_nfaa_frag_spv_count);
101 Shader *sh = newPostShader(gfx, frag, shaders::kNfaa);
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);
113 return sh;
114}
115
116Shader *createTaaShader(Graphics *gfx) {
117 if (!gfx) throw eve::Exception("AntiAliasing: null graphics");
118 auto frag = copySpv(aa_taa_frag_spv, aa_taa_frag_spv_count);
119 Shader *sh = newPostShader(gfx, frag, shaders::kTaa);
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);
146 return sh;
147}
148
149} // namespace
150
152 fxaa_ = createFxaaShader(gfx);
153 smaa_ = createSmaaShader(gfx);
154 ssaa_ = createSsaaShader(gfx);
155 nfaa_ = createNfaaShader(gfx);
156 taa_ = createTaaShader(gfx);
157 applyQualityDefaults();
158}
159
161
162void AntiAliasing::applyQualityDefaults() {
163 if (quality_ == "low") {
164 fxaa_->sendFloat("edgeThreshold", 0.25f);
165 fxaa_->sendFloat("edgeThresholdMin", 0.0833f);
166 fxaa_->sendFloat("subpix", 0.5f);
167 fxaa_->sendFloat("quality", 0.f);
168 smaa_->sendFloat("threshold", 0.15f);
169 smaa_->sendFloat("localContrast", 0.55f);
170 smaa_->sendFloat("maxSearch", 4.f);
171 smaa_->sendFloat("cornerRounding", 0.1f);
172 ssaa_->sendFloat("scale", 2.f);
173 ssaa_->sendFloat("kernel", 0.f); // box
174 ssaa_->sendFloat("sampleRadius", 0.75f);
175 nfaa_->sendFloat("strength", 0.7f);
176 nfaa_->sendFloat("power", 1.2f);
177 nfaa_->sendFloat("blurScale", 0.85f);
178 taa_->sendFloat("blendCurrent", 0.20f);
179 taa_->sendFloat("clampAmount", 0.7f);
180 return;
181 }
182 if (quality_ == "high") {
183 fxaa_->sendFloat("edgeThreshold", 0.125f);
184 fxaa_->sendFloat("edgeThresholdMin", 0.0312f);
185 fxaa_->sendFloat("subpix", 0.85f);
186 fxaa_->sendFloat("quality", 2.f);
187 smaa_->sendFloat("threshold", 0.05f);
188 smaa_->sendFloat("localContrast", 0.4f);
189 smaa_->sendFloat("maxSearch", 16.f);
190 smaa_->sendFloat("cornerRounding", 0.4f);
191 ssaa_->sendFloat("scale", 4.f);
192 ssaa_->sendFloat("kernel", 2.f); // gaussian
193 ssaa_->sendFloat("sampleRadius", 2.f);
194 nfaa_->sendFloat("strength", 1.25f);
195 nfaa_->sendFloat("power", 0.85f);
196 nfaa_->sendFloat("blurScale", 1.35f);
197 taa_->sendFloat("blendCurrent", 0.08f);
198 taa_->sendFloat("clampAmount", 0.95f);
199 return;
200 }
201 // medium (default / unknown)
202 quality_ = "medium";
203 fxaa_->sendFloat("edgeThreshold", 0.166f);
204 fxaa_->sendFloat("edgeThresholdMin", 0.0833f);
205 fxaa_->sendFloat("subpix", 0.75f);
206 fxaa_->sendFloat("quality", 1.f);
207 smaa_->sendFloat("threshold", 0.1f);
208 smaa_->sendFloat("localContrast", 0.5f);
209 smaa_->sendFloat("maxSearch", 8.f);
210 smaa_->sendFloat("cornerRounding", 0.25f);
211 ssaa_->sendFloat("scale", 2.f);
212 ssaa_->sendFloat("kernel", 1.f); // tent
213 ssaa_->sendFloat("sampleRadius", 1.25f);
214 nfaa_->sendFloat("strength", 1.f);
215 nfaa_->sendFloat("power", 1.f);
216 nfaa_->sendFloat("blurScale", 1.f);
217 taa_->sendFloat("blendCurrent", 0.12f);
218 taa_->sendFloat("clampAmount", 0.8f);
219}
220
221void AntiAliasing::setQuality(const std::string &quality) {
222 if (quality == "low" || quality == "medium" || quality == "high")
223 quality_ = quality;
224 else
225 quality_ = "medium";
226 applyQualityDefaults();
227}
228
229void AntiAliasing::setMode(const std::string &mode) {
230 const std::string next =
231 (mode == "fxaa" || mode == "smaa" || mode == "ssaa" || mode == "nfaa" || mode == "taa")
232 ? mode
233 : "fxaa";
234 if (next == "taa" && mode_ != "taa") resetHistory();
235 mode_ = next;
236}
237
238void AntiAliasing::resetHistory() {
239 taaHistoryValid_ = false;
240}
241
242void AntiAliasing::ensureHistoryCanvases(int width, int height) {
243 width = std::max(1, width);
244 height = std::max(1, height);
245 if (!taaHistoryA_ || !taaHistoryB_ || historyW_ != width || historyH_ != height) {
246 taaHistoryA_ = gfx_ ? gfx_->newHDRCanvas(width, height) : nullptr;
247 taaHistoryB_ = gfx_ ? gfx_->newHDRCanvas(width, height) : nullptr;
248 if (!taaHistoryA_ || !taaHistoryB_)
249 throw eve::Exception("AntiAliasing: failed to allocate temporal history canvases");
250 historyRead_ = taaHistoryA_;
251 historyWrite_ = taaHistoryB_;
252 resetHistory();
253 }
254 historyW_ = width;
255 historyH_ = height;
256 if (!historyRead_) historyRead_ = taaHistoryA_;
257 if (!historyWrite_) historyWrite_ = taaHistoryB_;
258}
259
260void AntiAliasing::swapHistoryBuffers() {
261 std::swap(historyRead_, historyWrite_);
262}
263
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_;
269 return fxaa_;
270}
271
272Shader *AntiAliasing::getShader() const { return shaderForMode(); }
273
274bool AntiAliasing::hasParam(const std::string &name) const {
275 Shader *sh = shaderForMode();
276 return sh && sh->hasUniform(name);
277}
278
279void AntiAliasing::setFloat(const std::string &name, float value) {
280 Shader *sh = shaderForMode();
281 if (!sh) throw eve::Exception("AntiAliasing.setFloat: null shader");
282 sh->sendFloat(name, value);
283}
284
285float AntiAliasing::getFloat(const std::string &name) const {
286 Shader *sh = shaderForMode();
287 if (!sh) throw eve::Exception("AntiAliasing.getFloat: null shader");
288 float v = 0.f;
289 if (sh->getFromVar(name, &v, sizeof(v)) != int(sizeof(v)))
290 throw eve::Exception("AntiAliasing.getFloat: missing param '%s'", name.c_str());
291 return v;
292}
293
295 if (mode_ != "ssaa") return 1.f;
296 if (quality_ == "high") return 4.f;
297 return 2.f;
298}
299
300int AntiAliasing::resolutionFor(int destSize) const {
301 if (destSize < 1) destSize = 1;
302 const float s = suggestScale();
303 return std::max(1, int(std::floor(float(destSize) * s)));
304}
305
306void AntiAliasing::uploadScreenUniforms(Texture *source) {
307 if (!source) throw eve::Exception("AntiAliasing: null source texture");
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();
313 if (sh->hasUniform("texelW")) sh->sendFloat("texelW", tw);
314 if (sh->hasUniform("texelH")) sh->sendFloat("texelH", th);
315 if (mode_ == "ssaa" && sh->hasUniform("scale")) {
316 // Keep user override if they set scale explicitly after setQuality;
317 // still ensure scale reflects suggestScale when it matches the quality default.
318 // Prefer the quality-driven suggestScale unless caller already changed it away
319 // from {2,4}. Here we always sync scale to suggestScale for predictability.
320 sh->sendFloat("scale", suggestScale());
321 }
322}
323
324void AntiAliasing::drawFullscreen(Graphics *gfx, Texture *source, Shader *shader) {
325 if (!gfx) throw eve::Exception("AntiAliasing: null graphics");
326 if (!source) throw eve::Exception("AntiAliasing: null source texture");
327 if (!shader) throw eve::Exception("AntiAliasing: null shader");
328 uploadScreenUniforms(source);
329 const float dw = gfx->getCanvas() ? float(gfx->getCanvas()->getWidth()) : float(gfx->getWidth());
330 const float dh =
331 gfx->getCanvas() ? float(gfx->getCanvas()->getHeight()) : float(gfx->getHeight());
332 // Spatial AA writes alpha=1 and replaces the whole destination. Blending
333 // would read undefined contents from a fresh or resized HDR target.
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),
335 false, BlendMode::Opaque);
336}
337
338void AntiAliasing::prepareSource(Texture *source) { uploadScreenUniforms(source); }
339
341 if (!source) throw eve::Exception("AntiAliasing.beginTemporalFrame: null source");
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_);
356 taa_->sendFloat("reprojectionValid",
357 taaHistoryValid_ && temporalViewValid_ ? 1.f : 0.f);
358 taa_->sendFloat("motionValid", motion ? 1.f : 0.f);
359 return historyWrite_;
360}
361
363 return taaHistoryValid_ && historyRead_ ? historyRead_->getTexture() : nullptr;
364}
365
367 taaHistoryValid_ = true;
368 previousJitterNdc_ = temporalJitterNdc_;
369 previousViewProj_ = temporalViewProj_;
370 temporalViewValid_ = true;
371 temporalObjectHistory_ = std::move(temporalObjectPending_);
372 temporalObjectPending_.clear();
373 ++temporalFrameIndex_;
374 swapHistoryBuffers();
375}
376
378 auto halton = [](uint32_t index, uint32_t base) {
379 float result = 0.f;
380 float fraction = 1.f / float(base);
381 while (index > 0) {
382 result += fraction * float(index % base);
383 index /= base;
384 fraction /= float(base);
385 }
386 return result;
387 };
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_;
392}
393
394void AntiAliasing::setTemporalCamera(const glm::vec3 &eye, const glm::vec3 &target,
395 float fovYDeg) {
396 glm::vec3 forward = target - eye;
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;
404 if (cut) {
405 resetHistory();
406 if (gfx_) gfx_->pipelineExposure()->invalidateHistory();
407 }
408 }
409 temporalEye_ = eye;
410 temporalForward_ = forward;
411 temporalFovY_ = fovYDeg;
412 temporalCameraValid_ = true;
413}
414
416 resetHistory();
417 if (gfx_) gfx_->pipelineExposure()->invalidateHistory();
418}
419
420void AntiAliasing::setTemporalViewProjection(const glm::mat4 &viewProjection, float nearZ,
421 float farZ) {
422 temporalViewProj_ = viewProjection;
423 temporalNearZ_ = std::max(nearZ, 1e-4f);
424 temporalFarZ_ = std::max(farZ, temporalNearZ_ + 1e-3f);
425}
426
427glm::vec2 AntiAliasing::prepareTemporalObjectMotion(const void *objectKey,
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));
442}
443
445 if (!gfx) throw eve::Exception("AntiAliasing.applyTemporal: null graphics");
446 if (!source) throw eve::Exception("AntiAliasing.applyTemporal: null source");
447
448 Canvas *finalCanvas = gfx->getCanvas();
449 const float dw = finalCanvas ? float(finalCanvas->getWidth()) : float(gfx->getWidth());
450 const float dh = finalCanvas ? float(finalCanvas->getHeight()) : float(gfx->getHeight());
451 Canvas *writeTarget = beginTemporalFrame(source, motion);
452 Texture *prevTex = getTemporalReadTexture();
453 Shader *sh = getTaaShader();
454 if (!sh) throw eve::Exception("AntiAliasing.applyTemporal: missing taa shader");
455
456 gfx->setCanvas(writeTarget);
458 source, prevTex ? prevTex : source, motion ? motion : source, sh, 0.f, 0.f,
459 float(writeTarget->getWidth()), float(writeTarget->getHeight()),
460 glm::vec4(1.f, 1.f, 1.f, 1.f));
461
462 Texture *resolved = writeTarget->getTexture();
463 if (!resolved) throw eve::Exception("AntiAliasing.applyTemporal: resolved texture missing");
464 gfx->setCanvas(finalCanvas);
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,
469}
470
472 if (!gfx) throw eve::Exception("AntiAliasing.resolveTemporal: null graphics");
473 if (!source) throw eve::Exception("AntiAliasing.resolveTemporal: null source");
474 Canvas *finalCanvas = gfx->getCanvas();
475 Canvas *writeTarget = beginTemporalFrame(source, motion);
478 if (!shader) throw eve::Exception("AntiAliasing.resolveTemporal: missing taa shader");
479 gfx->setCanvas(writeTarget);
481 source, previous ? previous : source, motion ? motion : source, shader, 0.f, 0.f,
482 float(writeTarget->getWidth()), float(writeTarget->getHeight()),
483 glm::vec4(1.f, 1.f, 1.f, 1.f));
484 Texture *resolved = writeTarget->getTexture();
485 if (!resolved) throw eve::Exception("AntiAliasing.resolveTemporal: result missing");
486 gfx->setCanvas(finalCanvas);
488 return resolved;
489}
490
492 float height, float r, float g, float b, float a) {
493 if (!gfx) throw eve::Exception("AntiAliasing.applyTemporalRect: null graphics");
494 if (!source) throw eve::Exception("AntiAliasing.applyTemporalRect: null source");
495
496 Canvas *finalCanvas = gfx->getCanvas();
497 Canvas *writeTarget = beginTemporalFrame(source);
498 Texture *prevTex = getTemporalReadTexture();
499 Shader *sh = getTaaShader();
500 if (!sh) throw eve::Exception("AntiAliasing.applyTemporal: missing taa shader");
501
502 // Pass 1: merge current + previous to history-write target.
503 gfx->setCanvas(writeTarget);
505 source, prevTex ? prevTex : source, source, sh, 0.f, 0.f,
506 float(writeTarget->getWidth()), float(writeTarget->getHeight()),
507 glm::vec4(1.f, 1.f, 1.f, 1.f));
508
509 // Pass 2: composite resolved temporal result to the requested final target.
510 Texture *resolved = writeTarget->getTexture();
511 if (!resolved) throw eve::Exception("AntiAliasing.applyTemporal: resolved texture missing");
512 gfx->setCanvas(finalCanvas);
513 gfx->drawTexturedRectShaderUV(resolved, nullptr, x, y, width, height, 0.f, 0.f, 1.f, 1.f,
514 glm::vec4(r, g, b, a), false, BlendMode::Opaque);
515
517}
518
520 if (mode_ == "taa")
521 applyTemporal(gfx, source);
522 else
523 drawFullscreen(gfx, source, shaderForMode());
524}
525
527 if (!gfx) throw eve::Exception("AntiAliasing.applyTo: null graphics");
528 if (!dest) throw eve::Exception("AntiAliasing.applyTo: null dest");
529 EV_PROFILE_MODULE("graphics", "PostProcess.Antialiasing");
530 Canvas *prev = gfx->getCanvas();
531 gfx->setCanvas(dest);
532 apply(gfx, source);
533 gfx->setCanvas(prev);
534}
535
537 if (!source) throw eve::Exception("AntiAliasing.applyCanvas: null source");
538 Texture *tex = source->getTexture();
539 if (!tex) throw eve::Exception("AntiAliasing.applyCanvas: source has no sampleable texture");
540 apply(gfx, tex);
541}
542
544 if (!source) throw eve::Exception("AntiAliasing.applyCanvasTo: null source");
545 Texture *tex = source->getTexture();
546 if (!tex) throw eve::Exception("AntiAliasing.applyCanvasTo: source has no sampleable texture");
547 applyTo(gfx, tex, dest);
548}
549
550} // namespace eve::graphics
double y
double x
LogicalId target
float w
Definition AnimClip.cpp:738
const std::string & s
std::string name
Definition ECS.cpp:154
tensor::Graph g
Definition GpuGraph.cpp:7
vk::ShaderModule frag
double r
float v
int h
std::uint32_t height
std::uint32_t width
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 origin
std::shared_ptr< const OceanSprayFrame > previous
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
Object::Ptr value
glm::vec3 eye
Shader * shader
glm::mat4 model
RoadNetwork resolved
std::string_view source
int count
uint32_t index
float fraction
V forward
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
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).
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.
void invalidateHistory()
Invalidate eye-adaptation history, for camera cuts and mode changes.
Definition Exposure.h:49
int getWidth() const
Returns the width.
Definition Graphics.h:489
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.
Definition Graphics.h:491
Custom GPU program.
Definition Shader.h:41
bool hasUniform(const std::string &name) const
Definition Shader.cpp:71
void sendFloat(const std::string &name, float x)
Definition Shader.cpp:107
int getFromVar(const std::string &name, void *data, size_t size) const
Definition Shader.cpp:96
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
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:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25