载入中...
搜索中...
未找到
GraphicsShaderReload.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cstring>
5#include <exception>
6#include <utility>
7
8#if defined(__EMSCRIPTEN__)
9#include <emscripten/emscripten.h>
10#endif
11
12#include "common/Diagnostic.h"
13#include "graphics/Shader.h"
14
15namespace eve::graphics::webgpu {
16namespace {
17
18} // namespace
19
20Result<void> Graphics::replaceShaderFromSpv(Shader &, const std::vector<uint32_t> &,
21 const std::vector<uint32_t> &) {
22 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Unsupported, "SPIR-V shader replacement is unavailable on the WebGPU backend", "source", {},
23 "graphics.webgpu.shader_reload"));
24}
25
27 const std::string &fragWgsl) {
28 auto shaderIt = std::find_if(ownedShaders.begin(), ownedShaders.end(),
29 [&](const std::unique_ptr<Shader> &owned) {
30 return owned.get() == &shader;
31 });
32 if (shaderIt == ownedShaders.end() || !shader.gpuHandle)
33 return Result<void>::failure(Diagnostic::error(DiagnosticCode::StaleHandle, "shader is not a live resource owned by this Graphics instance", "shader", {},
34 "graphics.webgpu.shader_reload"));
35 if (fragWgsl.empty())
36 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "fragment WGSL must not be empty", "fragWgsl", {},
37 "graphics.webgpu.shader_reload"));
38
39 auto *current = static_cast<GpuShader *>(shader.gpuHandle);
40 auto gpuIt = std::find_if(ownedGpuShaders.begin(), ownedGpuShaders.end(),
41 [&](const std::unique_ptr<GpuShader> &owned) {
42 return owned.get() == current;
43 });
44 if (gpuIt == ownedGpuShaders.end())
45 return Result<void>::failure(Diagnostic::error(DiagnosticCode::StaleHandle, "shader GPU resource is no longer owned by this Graphics instance", "shader.gpuHandle", {},
46 "graphics.webgpu.shader_reload"));
47
48 GpuShader candidate;
49 candidate.isMesh3D = current->isMesh3D;
50 candidate.isHair3D = current->isHair3D;
51 candidate.pipelineLayout = current->pipelineLayout;
52 candidate.setLayout = current->setLayout;
53 candidate.wgslVert = vertWgsl.empty() ? current->wgslVert : vertWgsl;
54 candidate.wgslFrag = fragWgsl;
55 if (candidate.wgslVert.empty())
56 return Result<void>::failure(Diagnostic::error(DiagnosticCode::PreconditionViolation, "shader has no reusable default vertex WGSL", "vertWgsl", {},
57 "graphics.webgpu.shader_reload"));
58
59 std::string preparationError;
60 device.PushErrorScope(wgpu::ErrorFilter::Validation);
61 try {
62 if (!candidate.isMesh3D) {
63 candidate.swapchainPipeline = createPipelineForShader(
64 &candidate, wgpu::TextureFormat(surfaceFormat), false, false, false, false,
65 false, tex2DPipelineLayout);
66 candidate.offscreenPipeline = createPipelineForShader(
67 &candidate, wgpu::TextureFormat::RGBA8Unorm, false, false, false, false,
68 false, tex2DPipelineLayout);
69 } else {
70 candidate.mesh3dPipeline = createPipelineForShader(
71 &candidate, static_cast<wgpu::TextureFormat>(sceneColorFormat), true, true,
72 candidate.isHair3D, false, false, mesh3dPipelineLayout);
73 if (!candidate.isHair3D)
74 candidate.mesh3dXrayPipeline = createPipelineForShader(
75 &candidate, static_cast<wgpu::TextureFormat>(sceneColorFormat), false, true,
76 false, false, false, mesh3dPipelineLayout);
77 }
78 } catch (const std::exception &error) {
79 preparationError = error.what();
80 } catch (...) {
81 preparationError = "unknown pipeline preparation error";
82 }
83
84 bool done = false;
85 wgpu::ErrorType validationError = wgpu::ErrorType::Unknown;
86 std::string validationMessage;
87 device.PopErrorScope(wgpu::CallbackMode::AllowProcessEvents, [&](wgpu::PopErrorScopeStatus status,
88 wgpu::ErrorType type, wgpu::StringView message) {
89 validationError = status == wgpu::PopErrorScopeStatus::Success ? type : wgpu::ErrorType::Unknown;
90 if (message.data) {
91 const auto length = message.length == WGPU_STRLEN ? std::strlen(message.data) : message.length;
92 validationMessage.assign(message.data, length);
93 }
94 done = true;
95 });
96 while (!done) {
97#if defined(__EMSCRIPTEN__)
98 emscripten_sleep(0);
99#endif
100 wgpuInstanceProcessEvents(instance.Get());
101 }
102 const bool missingPipeline =
103 candidate.isMesh3D ? !candidate.mesh3dPipeline || (!candidate.isHair3D && !candidate.mesh3dXrayPipeline)
104 : !candidate.swapchainPipeline || !candidate.offscreenPipeline;
105 if (!preparationError.empty() || validationError != wgpu::ErrorType::NoError || missingPipeline)
107 !preparationError.empty() ? preparationError
108 : !validationMessage.empty()
109 ? validationMessage
110 : "WebGPU rejected replacement pipeline",
111 "pipeline", {}, "graphics.webgpu.shader_reload"));
112
113 current->swapchainPipeline = std::move(candidate.swapchainPipeline);
114 current->offscreenPipeline = std::move(candidate.offscreenPipeline);
115 current->mesh3dPipeline = std::move(candidate.mesh3dPipeline);
116 current->mesh3dXrayPipeline = std::move(candidate.mesh3dXrayPipeline);
117 current->wgslVert = std::move(candidate.wgslVert);
118 current->wgslFrag = std::move(candidate.wgslFrag);
119 return Result<void>::success();
120}
121
123 const std::string &) {
124 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Unsupported, "runtime GLSL replacement is unavailable on the WebGPU backend", "source", {},
125 "graphics.webgpu.shader_reload"));
126}
127
128} // namespace eve::graphics::webgpu
ActiveSource owned
Stable, structured diagnostics shared by engine modules.
std::string message
wgpu::PopErrorScopeStatus status
bool done
std::string error
Definition Package.cpp:174
Shader * shader
double current
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Custom GPU program.
Definition Shader.h:41
Result< void > replaceShaderFromGlsl(Shader &shader, const std::string &vertGlsl, const std::string &fragGlsl) override
Replace shader from glsl.
Result< void > replaceShaderFromSpv(Shader &shader, const std::vector< uint32_t > &vertSpv, const std::vector< uint32_t > &fragSpv) override
Replace shader from spv.
Result< void > replaceShaderFromWgsl(Shader &shader, const std::string &vertWgsl, const std::string &fragWgsl) override
Replace shader from wgsl.
A compiled shader: one WebGPU pipeline + layout. Also holds the WGSL sources so custom shaders can be...
Definition Graphics.h:177
wgpu::RenderPipeline mesh3dPipeline
Definition Graphics.h:184
wgpu::RenderPipeline swapchainPipeline
Definition Graphics.h:178
wgpu::BindGroupLayout setLayout
Definition Graphics.h:189
wgpu::PipelineLayout pipelineLayout
Definition Graphics.h:188
wgpu::RenderPipeline mesh3dXrayPipeline
Definition Graphics.h:185
wgpu::RenderPipeline offscreenPipeline
Definition Graphics.h:179