217 void drawDeferredLighting()
override;
225 uint32_t gpuDrivenMeshRecord(
Mesh *
mesh)
override;
239 void gpuDrivenCullEmit(
const glm::mat4&
viewProj,
const glm::vec3&
eye,
float fovYDeg,
float nearZ,
240 float farZ)
override;
242 void gpuDrivenDrawOpaque()
override;
244 bool gpuDrivenResolveWanted()
const override;
246 void gpuDrivenRecordVisPass()
override;
248 void gpuDrivenResolve()
override;
252 uint32_t gpuDrivenVgAssetId(
Mesh *
mesh)
const override;
254 bool gpuDrivenVgAttachToMesh(
Mesh *
mesh, uint32_t vgAssetId)
override;
257 bool gpuDrivenVgSetInstance(uint32_t vgAssetId,
const glm::mat4&
model, uint32_t materialId)
override;
259 void gpuDrivenVgComputeSection(
const glm::mat4&
viewProj,
const glm::vec3&
eye,
float fovYDeg,
float nearZ,
260 float farZ)
override;
268 uint32_t debugGpuDrivenGpuVisibleCount();
276 uint32_t debugGpuDrivenVgGpuVisibleCount();
280 [[nodiscard]]
Result<void> debugValidatePbrVariantBaseSources();
299 void initWithWindow(
void *nativeWindow)
override;
303 void pushValidationScope()
override;
305 void popValidationScope()
override;
307 void setMsaaSamples(
int samples)
override;
311 void setVSync(
bool enabled)
override;
315 void setViewportSize(
int width,
int height,
int pixelwidth,
int pixelheight)
override;
317 void drawSolidRect(
float x,
float y,
float w,
float h,
const Color &
color,
322 void drawSolidRectRotated(
float cx,
float cy,
float w,
float h,
float degrees,
328 bool repeatV =
false)
override;
336 std::span<const uint8_t> rgba)
override;
341 std::span<const uint16_t> rgbaHalf)
override;
346 std::span<const uint8_t> rgba)
override;
348 Texture *newCubemap(
int faceSize,
const uint8_t *rgbaFaces)
override;
350 Texture *newCubemap(
int faceSize,
const uint8_t *rgbaFaces,
354 Texture *newHDRCubemap(
int faceSize)
override;
359 bool copyHDRCanvasesToCubemap(
Canvas *
const *
sources,
int faceCount,
363 bool filterHDRReflectionCubemap(
Texture *cubemap,
int sampleCount = 64)
override;
373 float getMaxAnisotropy()
const override;
376 bool reloadTextureFromFile(
const std::string &filename)
override;
383 const uint8_t *rgba)
override;
386 int height, std::span<const std::uint8_t> rgba,
387 std::size_t bytesPerRow = 0)
override;
390 Texture *
texture, std::span<const TextureRegionUpload> regions)
override;
399 void drawTexturedRectUV(
Texture *
texture,
float x,
float y,
float w,
float h,
float u0,
float v0,
400 float u1,
float v1,
const Color &
color)
override;
403 float h,
float u0,
float v0,
float u1,
float v1,
408 float w,
float h,
float degrees,
float u0,
float v0,
410 bool rotatedUV =
false,
438 Shader *newShaderFromSpv(
const std::vector<uint32_t> &vertSpv,
439 const std::vector<uint32_t> &fragSpv)
override;
441 Shader *newShaderFromSpvFile(
const std::string &vertPath,
const std::string &fragPath)
override;
443 Shader *newShaderFromWgsl(
const std::string &vertWgsl,
444 const std::string &fragWgsl)
override;
448 const std::vector<uint32_t> &fragSpv)
override;
452 const std::string &fragWgsl)
override;
456 const std::string &fragGlsl)
override;
458 Shader *newShader(
const std::string &vertGlsl,
const std::string &fragGlsl)
override;
460 Shader *newMeshShaderFromSpv(
const std::vector<uint32_t> &vertSpv,
461 const std::vector<uint32_t> &fragSpv)
override;
463 Shader *newMeshShaderFromWgsl(
const std::string &vertWgsl,
464 const std::string &fragWgsl)
override;
469 Shader *newMeshShader(
const std::string &vertGlsl,
const std::string &fragGlsl)
override;
471 Shader *newHairShaderFromSpv(
const std::vector<uint32_t> &vertSpv,
472 const std::vector<uint32_t> &fragSpv)
override;
474 Shader *newHairShaderFromWgsl(
const std::string &vertWgsl,
475 const std::string &fragWgsl)
override;
480 Mesh *newMeshFromAssimp(const ::aiMesh &
mesh)
override;
482 Mesh* newMeshFromAssimp(const ::aiMesh&
mesh,
const glm::mat4& worldTransform)
override;
484 Mesh *newMeshFromArrays(
const float *posXYZ,
const float *nrmXYZ,
const float *uvST,
493 Mesh *newMeshFromArraysColored(
const float *posXYZ,
const float *nrmXYZ,
const float *uvST,
498 bool bakeMeshMorph(
Mesh *
mesh)
override;
500 bool updateMeshVertices(
Mesh *
mesh,
const float *posXYZ,
const float *nrmXYZ,
const float *uvST,
503 bool setMeshSkinningData(
Mesh *
mesh,
const uint16_t *joints4,
const float *weights4,
506 Mesh *newMeshSphere(
int slices = 32,
int stacks = 16)
override;
508 Mesh *newMeshCylinder(
int slices = 32,
int stacks = 1,
bool caps =
true)
override;
510 bool releaseMesh(
Mesh *
mesh)
override;
513 void begin3DFrame()
override;
515 void begin3DFrameToCanvas(
Canvas *canvas)
override;
517 void end3DFrameToCanvas()
override;
522 return completedOffscreenTimestampMs.exchange(0.f);
525 void setMesh3DViewProj(
const glm::mat4 &
viewProj)
override;
527 void setMesh3DView(
const glm::mat4 &
view)
override;
529 void setMesh3DClip(
float nearZ,
float farZ)
override;
531 Texture *getSceneColorTexture()
override;
537 Texture *getSceneLinearDepthTexture()
override;
544 void drawVoxelFaceInstances(
const uint32_t *packed,
int count,
float originX,
float originY,
545 float originZ,
const std::string &faceDir,
Texture *atlas,
546 int tilesPerRow = 16,
const uint32_t *
ao =
nullptr)
override;
550 void setMesh3DPackedNormalMask(
bool enabled)
override;
554 void setMesh3DVirtualTexture(
bool enabled,
int pageCountX,
int pageCountY,
555 int atlasSlotsX,
int atlasSlotsY,
556 float borderFraction)
override;
562 [[nodiscard]] Mesh3DSceneColorCaptureStatus captureMesh3DSceneColor()
override;
570 const std::string &alphaTechnique =
"cutoff")
override;
572 void setMesh3DTexCellBomb(
float cellScale,
float strength,
float rotAmount = 1.f)
override;
574 void setMesh3DParallax(
float scale,
float minLayers = 8.f,
float maxLayers = 32.f)
override;
576 void setMesh3DLodDither(
float weight,
bool reverse,
bool enabled)
override;
580 void setCloudShadows(
float strength,
float worldCell,
float time,
float windSpeed,
float windAngle,
float coverage,
585 void setMesh3DClusteredActive(
bool active)
override;
587 void setMesh3DSSAO(
float intensity)
override;
589 void setMesh3DLight(
const glm::vec3 &
dir,
const glm::vec3 &
color)
override;
591 void setMesh3DCameraPos(
const glm::vec3 &
eye)
override;
593 void setMesh3DEnv(
Texture *cube,
float intensity)
override;
595 void setMesh3DEnvProbe(
const glm::vec3 &
center,
const glm::vec3 &extent)
override;
599 void setSceneExposure(
float exposure)
override { sceneExposure = std::max(exposure, 0.f); }
604 sceneColorFilter = glm::max(glm::vec3(
color.r,
color.g,
color.b), glm::vec3(0.f));
610 sceneLift = glm::vec3(lift.
r, lift.
g, lift.
b);
611 sceneInverseGamma = glm::max(glm::vec3(inverseGamma.
r, inverseGamma.
g, inverseGamma.
b), glm::vec3(0.001F));
618 return {sceneInverseGamma.
r, sceneInverseGamma.g, sceneInverseGamma.b};
624 float lensScale)
override {
625 sceneTransitionVignette = std::clamp(vignette, 0.0F, 1.0F);
626 sceneTransitionVignetteSmoothness = std::clamp(vignetteSmoothness, 0.01F, 1.0F);
627 sceneTransitionLensDistortion = std::clamp(lensDistortion, 0.0F, 1.0F);
628 sceneTransitionLensScale = std::clamp(lensScale, 0.01F, 5.0F);
641 sceneAutoExposureMinEV = minEV;
642 sceneAutoExposureMaxEV = maxEV;
652 sceneBloomIntensity = intensity;
653 sceneBloomThreshold = threshold;
661 float farZ)
override {
662 sceneDofFocusDistance = focusDistance;
663 sceneDofMaxBlurPx = maxBlurPx;
664 sceneDofFocusRange = focusRange;
665 sceneDofNearZ = nearZ;
679 void setMesh3DShadows(
const ShadowUpload &upload)
override;
681 void setMesh3DShadowReceive(
bool receive)
override;
685 void beginShadowPass(
int cascadeIndex)
override;
687 void drawMeshShadow(
Mesh *
mesh,
const glm::mat4 &lightMVP,
bool doubleSided =
true)
override;
693 void endShadowPass()
override;
696 void beginGBufferPass(
int width,
int height)
override;
698 void drawMeshGBuffer(
Mesh *
mesh,
const glm::mat4 &
mvp,
const glm::mat4 &
model,
float nearZ,
float farZ,
699 Texture *
albedo =
nullptr,
float tintR = 1.f,
float tintG = 1.f,
float tintB = 1.f,
700 float motionX = 0.f,
float motionY = 0.f,
float roughness = 0.45f,
float metallic = 0.f,
701 float tintA = 1.f)
override;
703 void drawMeshGBufferAlpha(
Mesh *
mesh,
const glm::mat4 &
mvp,
const glm::mat4 &
model,
float nearZ,
float farZ,
704 Texture *
albedo =
nullptr,
float tintR = 1.f,
float tintG = 1.f,
float tintB = 1.f,
705 float motionX = 0.f,
float motionY = 0.f,
float roughness = 0.45f,
float metallic = 0.f,
706 float tintA = 1.f)
override;
708 void endGBufferPass()
override;
710 image::ImageData *readGBufferToImageData(
const std::string &attachment)
override;
715 void beginDecalPass(
int width,
int height)
override;
717 void setDecalCamera(
const glm::mat4 &
viewProj,
float nearZ,
float farZ)
override;
720 const float uvRect[4],
float fade,
float normalStrength,
float roughnessStrength,
721 float metalStrength,
float emissiveStrength,
int blendMode = 0,
722 int projectionMode = 0,
float blendSharpness = 4.f,
723 float parallaxScale = 0.f,
float parallaxMinLayers = 8.f,
724 float parallaxMaxLayers = 24.f,
float edgeFadeWidth = 0.06f)
override;
726 void endDecalPass()
override;
728 image::ImageData *readDecalLayerToImageData(
const std::string &attachment)
override;
736 void setCanvas(
Canvas *canvas)
override;
738 bool isCanvasActive()
const override;
740 Canvas *getCanvas()
const override;
743 Texture *getTexture()
override;
747 bool beginFrameReadback(
const std::string &
path)
override;
749 int frameReadbackStatus()
const override;
757 void clear(std::optional<Color>
color, std::optional<int> stencil, std::optional<double>
depth)
override;
759 Color getPixel(
int x,
int y)
override;
818 Texture *normalTexture =
nullptr;
819 Texture *heightTexture =
nullptr;
820 glm::vec4 virtualTexture{0.f};
821 glm::vec4 virtualAtlas{0.f};
822 glm::mat4
model{1.f};
826 BlendMode surfaceBlend = BlendMode::Alpha;
827 bool depthWrite =
false;
829 bool shadowReceive =
true;
836 ShadowUpload shadows{};
837 Texture *environment =
nullptr;
838 float environmentIntensity = 0.f;
841 std::string alphaTechnique =
"cutoff";
842 glm::vec4 lodFade{1.f, 0.f, 0.f, 0.f};
843 std::optional<PbrSurface> pbrSurface;
844 std::optional<glm::vec4> gbufferClip;
845 glm::mat4 gbufferMvp{1.f};
846 wgpu::Buffer skinBuffer;
847 uint32_t frameUboOffset = 0;
848 uint32_t pushUboOffset = 0;
849 uint32_t shadowUboOffset = 0;
850 uint32_t clusteredUboOffset = 0;
856 bool alphaTest =
false;
858 glm::vec4 lodFade{1.f, 0.f, 0.f, 0.f};
862 std::optional<Mesh3dDraw> pbrDraw;
866 glm::mat4
model{1.f};
870 glm::vec2 motion{0.f};
873 bool alphaTest =
false;
875 uint32_t pushUboOffset = 0;
878 uint32_t instanceBufferOffset = 0;
879 uint32_t aoBufferOffset = 0;
881 GpuTexture *atlas =
nullptr;
883 glm::vec4 chunkOrigin{0.f};
884 glm::vec4 atlasInfo{16.f, 0.f, 0.f, 0.f};
886 uint32_t pushUboOffset = 0;
889 glm::mat4
model{1.f};
893 glm::vec4 uvRect{0.f, 0.f, 1.f, 1.f};
894 glm::vec4 fadeParams{1.f, 0.f, 0.f, 0.f};
895 glm::vec4 extraParams{0.f};
896 glm::vec4 surfaceParams{0.f, 8.f, 24.f, 0.06f};
900 void createDefaultTextures();
901 void createPipelineResources();
902 void create2DPipelines();
903 void createMesh3DPipelines();
904 wgpu::RenderPipeline get2DColorPipeline(BlendMode
blend,
bool offscreen);
905 wgpu::RenderPipeline get2DTexturedPipeline(BlendMode
blend,
bool offscreen);
906 wgpu::RenderPipeline get2DLitPipeline(BlendMode
blend, WGPUTextureFormat format);
907 wgpu::RenderPipeline getMesh3DPipeline(BlendMode
blend,
bool depthWrite,
bool doubleSided,
bool canvasTarget);
908 void createMesh3DClusteredPipeline();
909 void createShadowPipelines();
910 void createGbufferPipelines();
911 void createDecalPipeline();
912 void createVoxelPipelines();
913 void ensureGpuDrivenResources(uint32_t
instanceCount, uint32_t bucketCount);
914 void recordGpuDrivenCompute(wgpu::CommandEncoder encoder);
915 void flushGpuDrivenDraws(wgpu::RenderPassEncoder pass,
bool canvasTarget,
916 bool hdrCanvas =
false);
917 void ensureGpuDrivenVisibilityResources();
918 void recordGpuDrivenVisibility(wgpu::CommandEncoder encoder);
919 void flushGpuDrivenResolve(wgpu::RenderPassEncoder pass);
920 void ensureGpuDrivenVgResources();
921 void recordGpuDrivenVgCompute(wgpu::CommandEncoder encoder);
922 void recordGpuDrivenVgVisibility(wgpu::CommandEncoder encoder);
923 void flushGpuDrivenVgResolve(wgpu::RenderPassEncoder pass);
924 void createSceneColorResources(
int width,
int height);
925 void destroySceneColorResources();
926 void createShadowResources();
927 void destroyShadowResources();
928 void createGbufferResources(
int width,
int height);
929 void destroyGbufferResources();
931 void destroyDecalResources();
933 wgpu::RenderPipeline createPipelineForShader(GpuShader* gs, wgpu::TextureFormat format,
bool depth,
bool mesh3d,
934 bool hair,
bool shadow,
bool gbuffer, wgpu::PipelineLayout
layout);
935 wgpu::BindGroupLayout make2DBindGroupLayout();
936 wgpu::BindGroupLayout makeMesh3DBindGroupLayout();
937 wgpu::BindGroupLayout makeMesh3DClusteredBindGroupLayout();
938 wgpu::BindGroupLayout makeShadowBindGroupLayout();
939 wgpu::BindGroupLayout makeGbufferBindGroupLayout();
940 wgpu::BindGroupLayout makeDecalBindGroupLayout();
941 wgpu::BindGroupLayout makeVoxelBindGroupLayout();
942 wgpu::PipelineLayout make2DPipelineLayout();
943 wgpu::PipelineLayout makeMesh3DPipelineLayout();
944 wgpu::PipelineLayout makeMesh3DClusteredPipelineLayout();
945 wgpu::PipelineLayout makeShadowPipelineLayout();
946 wgpu::PipelineLayout makeGbufferPipelineLayout();
947 wgpu::PipelineLayout makeDecalPipelineLayout();
948 wgpu::PipelineLayout makeVoxelPipelineLayout();
950 GpuTexture *gpuForTexture(Texture *
t)
const;
951 GpuTexture *gpuForTextureOrWhite(Texture *
t)
const;
952 wgpu::BindGroup makeTex2DBindGroup(GpuTexture *
color, GpuTexture *
depth,
953 GpuTexture *motion =
nullptr, GpuTexture *extra =
nullptr,
954 GpuTexture *specular =
nullptr);
955 wgpu::BindGroup makeMeshBindGroup(GpuTexture*
albedo, GpuTexture*
normal, GpuTexture* env, GpuTexture*
height,
956 GpuTexture*
depth, GpuTexture* sceneColor, uint32_t frameUboOffset,
957 uint32_t shadowUboOffset, uint32_t pushUboOffset,
const wgpu::Buffer& skinBuffer);
958 wgpu::BindGroup makeMesh3DClusteredBindGroup(GpuTexture *
albedo, GpuTexture *
normal,
959 GpuTexture *env, GpuTexture *
height,
960 GpuTexture *
depth, wgpu::TextureView aoView,
961 uint32_t frameUboOffset, uint32_t shadowUboOffset);
962 void uploadClusteredLighting(
const ClusteredLightingUpload &upload);
963 void ensureMeshBindGroupsForDraw(Mesh3dDraw &
d);
964 void applyPendingResourceChanges()
override;
965 [[nodiscard]] ResultRef<Texture> requestFileTexture(
const std::string &filename)
override;
966 bool samplerChangesPending_ =
false;
967 struct CachedSampler {
968 TextureSampler
state;
972 std::vector<CachedSampler> cachedSamplers_;
973 wgpu::Sampler makeSampler(
const TextureSampler &
sampler, uint32_t mipLevels)
const;
975 void uploadTexturePixels(GpuTexture *gt,
const uint8_t *rgba,
int w,
int h,
976 const TextureCreateInfo &
info);
977 void uploadTexturePixelsMips(GpuTexture *gt,
const uint8_t *rgba,
int w,
int h);
978 void flush2D(wgpu::RenderPassEncoder pass,
int viewW,
int viewH, WGPUTextureFormat format);
979 void drawTexturedBatch(wgpu::RenderPassEncoder pass, TexturedBatch &
tb,
int viewW,
int viewH,
980 WGPUTextureFormat format,
bool offscreen);
981 void drawLitBatch(wgpu::RenderPassEncoder pass, LitBatch &lb,
int viewW,
int viewH,
982 WGPUTextureFormat format);
983 void flushMesh3D(wgpu::RenderPassEncoder pass, WGPUTextureFormat format,
984 bool canvasTarget =
false);
985 bool drawPbrMesh(wgpu::RenderPassEncoder pass, WGPUTextureFormat format,
986 bool canvasTarget, Mesh3dDraw &
draw, GpuMesh &
mesh);
987 void flushPrimitive3D(wgpu::RenderPassEncoder pass, WGPUTextureFormat format, uint32_t sampleCount);
988 wgpu::RenderPipeline getPrimitive3DPipeline(PrimitiveDepthMode
depth, BlendMode
blend, PrimitiveCullMode cull,
989 WGPUTextureFormat format, uint32_t sampleCount);
990 void flushShadowPass(wgpu::RenderPassEncoder pass,
int cascade);
991 void recordDirectionalShadowPasses(wgpu::CommandEncoder encoder);
992 void flushGbufferPass(wgpu::RenderPassEncoder pass);
993 void flushDecalPass(wgpu::RenderPassEncoder pass);
994 void submitPendingDeferredPasses();
995 void createDeferredLightingPipeline();
996 void destroyDeferredLightingResources();
997 void ensureDeferredLightingClusteredUpload();
999 void flushDeferredLighting(wgpu::RenderPassEncoder pass);
1005 bool flushGBufferPassInto(wgpu::CommandEncoder& encoder,
bool runAo);
1006 void flushVoxelDraws(wgpu::RenderPassEncoder pass, WGPUTextureFormat format);
1014 uint32_t alloc(uint64_t
size, uint64_t alignment);
1015 std::vector<wgpu::Buffer> palettes;
1016 size_t paletteIndex = 0;
1023 wgpu::Buffer uploadSkinPalette(Mesh*
mesh);
1024 UboArena ¤tUboArena();
1025 void ensureUboArena(UboArena &arena, uint64_t
bytes);
1028 struct VertexArena {
1033 uint64_t alloc(uint64_t
bytes);
1035 void reset() {
used = 0; }
1037 VertexArena ¤tVertexArena();
1038 void ensureVertexArena(VertexArena &arena, uint64_t
bytes);
1040 uint32_t frameSlotCount()
const {
return kFramesInFlight; }
1041 uint32_t currentFrameSlot()
const {
return frameIndex % kFramesInFlight; }
1044 VertexArena voxelInstanceArena;
1046 VertexArena voxelAoArena;
1049 bool initialized =
false;
1050 bool deviceInitDone =
false;
1051 bool headless_ =
false;
1052 void *sdlWindow =
nullptr;
1053 int logicalW = 0, logicalH = 0;
1054 int pixelW = 0, pixelH = 0;
1055 float maxSamplerAnisotropy = 1.f;
1057 wgpu::Instance instance;
1061 wgpu::Surface surface;
1063 WGPUTextureFormat surfaceFormat = WGPUTextureFormat_BGRA8Unorm;
1064 std::atomic<bool> surfaceNeedsRecreate{
false};
1065 bool swapchainConfigured =
false;
1068 uint32_t frameIndex = 0;
1070 uint32_t lastPresentSlot = 0;
1071 bool sceneColorHistoryValid =
false;
1072 std::optional<size_t> mesh3dSceneColorCaptureIndex;
1073 static constexpr uint32_t kFramesInFlight = 2;
1074 std::vector<UboArena> uboArenas;
1075 std::vector<VertexArena> vertexArenas;
1076 wgpu::Buffer mesh3dFrameUboPool;
1077 uint32_t mesh3dFrameUboSlots[kFramesInFlight]{};
1078 wgpu::Buffer shadowUboPool;
1079 uint32_t shadowUboSlots[kFramesInFlight]{};
1080 wgpu::Buffer pushUboPool;
1081 uint32_t pushUboSlots[kFramesInFlight]{};
1084 GpuTexture *whiteTexture =
nullptr;
1085 GpuTexture *flatNormalTexture =
nullptr;
1086 GpuTexture *flatNormalTexture3D =
nullptr;
1087 GpuTexture *flatHeightTexture3D =
nullptr;
1088 GpuTexture *flatDepthTexture3D =
nullptr;
1089 GpuTexture *defaultEnvCubemap =
nullptr;
1090 wgpu::BindGroupLayout reflectionProbeFilterSetLayout;
1091 wgpu::PipelineLayout reflectionProbeFilterPipelineLayout;
1092 wgpu::RenderPipeline reflectionProbeFilterPipeline;
1093 GpuTexture *shadowDepthArray =
nullptr;
1097 GpuTexture *defaultShadowTex =
nullptr;
1100 wgpu::PipelineLayout tex2DPipelineLayout;
1101 wgpu::PipelineLayout mesh3dPipelineLayout;
1102 wgpu::RenderPipeline mesh3dCanvasPipeline;
1103 wgpu::BindGroupLayout mesh3dClusteredSetLayout;
1104 wgpu::PipelineLayout mesh3dClusteredPipelineLayout;
1105 wgpu::RenderPipeline mesh3dClusteredPipeline;
1108 struct ClusteredStorage {
1112 uint64_t lightsCap = 0;
1113 uint64_t tableCap = 0;
1114 uint64_t indicesCap = 0;
1115 } clusteredStorage[kFramesInFlight];
1116 wgpu::PipelineLayout shadowPipelineLayout;
1117 wgpu::PipelineLayout gbufferPipelineLayout;
1118 wgpu::PipelineLayout decalPipelineLayout;
1119 wgpu::PipelineLayout voxelPipelineLayout;
1120 wgpu::BindGroupLayout tex2DSetLayout;
1121 wgpu::BindGroupLayout mesh3dSetLayout;
1122 wgpu::BindGroupLayout shadowSetLayout;
1123 wgpu::BindGroupLayout gbufferSetLayout;
1124 wgpu::BindGroupLayout decalSetLayout;
1125 wgpu::BindGroupLayout voxelSetLayout;
1127 wgpu::BindGroupLayout deferredLightingSetLayout;
1128 wgpu::PipelineLayout deferredLightingPipelineLayout;
1129 wgpu::RenderPipeline deferredLightingPipeline;
1130 wgpu::Sampler deferredLightingNearestSampler;
1131 bool deferredLightingPending_ =
false;
1136 wgpu::PipelineLayout aoPipelineLayout;
1137 wgpu::RenderPipeline aoPipeline;
1138 wgpu::BindGroupLayout aoSetLayout;
1139 wgpu::Texture aoTex[2];
1140 wgpu::TextureView aoView[2];
1141 uint32_t aoWriteIndex = 0;
1142 bool aoReady =
false;
1145 wgpu::BindGroup makeAOBindGroup(wgpu::TextureView depthView);
1148 wgpu::Sampler mainSampler;
1149 wgpu::RenderPipeline colorPipeline;
1150 wgpu::RenderPipeline texturedPipeline;
1151 wgpu::RenderPipeline colorAdditivePipeline;
1152 wgpu::RenderPipeline texturedAdditivePipeline;
1153 wgpu::RenderPipeline colorPremultipliedPipeline;
1154 wgpu::RenderPipeline texturedPremultipliedPipeline;
1155 wgpu::RenderPipeline colorMultiplyPipeline;
1156 wgpu::RenderPipeline texturedMultiplyPipeline;
1157 wgpu::RenderPipeline colorOpaquePipeline;
1158 wgpu::RenderPipeline texturedOpaquePipeline;
1159 wgpu::RenderPipeline sceneTonemapPipeline;
1160 float sceneExposure = 1.f;
1161 glm::vec3 sceneColorFilter{1.f};
1162 glm::vec3 sceneLift{0.f};
1163 glm::vec3 sceneInverseGamma{1.f};
1164 glm::vec3 sceneGain{1.f};
1165 float sceneTransitionVignette = 0.0F;
1166 float sceneTransitionVignetteSmoothness = 0.2F;
1167 float sceneTransitionLensDistortion = 0.0F;
1168 float sceneTransitionLensScale = 1.0F;
1169 bool sceneAutoExposure =
false;
1170 float sceneAutoExposureMinEV = -8.f;
1171 float sceneAutoExposureMaxEV = 8.f;
1172 float sceneBloomIntensity = 0.f;
1173 float sceneBloomThreshold = 1.f;
1174 float sceneDofFocusDistance = 0.f;
1175 float sceneDofMaxBlurPx = 0.f;
1176 float sceneDofFocusRange = 8.f;
1177 float sceneDofNearZ = 0.1f;
1178 float sceneDofFarZ = 100.f;
1179 wgpu::RenderPipeline mesh3dPipeline;
1180 wgpu::RenderPipeline mesh3dTransparentPipeline;
1181 static constexpr size_t kMeshPipelineVariants = 20;
1182 std::array<wgpu::RenderPipeline, kMeshPipelineVariants> mesh3dPipelines;
1183 std::array<wgpu::RenderPipeline, kMeshPipelineVariants> mesh3dCanvasPipelines;
1184 wgpu::RenderPipeline mesh3dShadowPipeline;
1185 wgpu::RenderPipeline mesh3dShadowSingleSidedPipeline;
1186 wgpu::RenderPipeline mesh3dShadowAlphaPipeline;
1187 wgpu::RenderPipeline mesh3dShadowAlphaSingleSidedPipeline;
1188 wgpu::RenderPipeline mesh3dGbufferPipeline;
1189 wgpu::RenderPipeline mesh3dGbufferAlphaPipeline;
1190 wgpu::RenderPipeline decalPipeline;
1191 wgpu::RenderPipeline voxelRectPipeline;
1192 wgpu::RenderPipeline lit2dPipeline;
1193 wgpu::RenderPipeline lit2dAdditivePipeline;
1194 wgpu::RenderPipeline lit2dPremultipliedPipeline;
1195 wgpu::RenderPipeline lit2dMultiplyPipeline;
1196 wgpu::RenderPipeline lit2dOpaquePipeline;
1198 wgpu::RenderPipeline offscreenColorPipeline;
1199 wgpu::RenderPipeline offscreenTexturedPipeline;
1200 wgpu::RenderPipeline offscreenColorAdditivePipeline;
1201 wgpu::RenderPipeline offscreenTexturedAdditivePipeline;
1202 wgpu::RenderPipeline offscreenColorPremultipliedPipeline;
1203 wgpu::RenderPipeline offscreenTexturedPremultipliedPipeline;
1204 wgpu::RenderPipeline offscreenColorMultiplyPipeline;
1205 wgpu::RenderPipeline offscreenTexturedMultiplyPipeline;
1206 wgpu::RenderPipeline offscreenColorOpaquePipeline;
1207 wgpu::RenderPipeline offscreenTexturedOpaquePipeline;
1208 wgpu::RenderPipeline hdrOffscreenTexturedPipeline;
1209 wgpu::RenderPipeline hdrOffscreenTexturedOpaquePipeline;
1210 wgpu::RenderPipeline offscreenLitPipeline;
1211 wgpu::RenderPipeline offscreenLitAdditivePipeline;
1212 wgpu::RenderPipeline offscreenLitPremultipliedPipeline;
1213 wgpu::RenderPipeline offscreenLitMultiplyPipeline;
1214 wgpu::RenderPipeline offscreenLitOpaquePipeline;
1215 wgpu::RenderPipeline hdrOffscreenLitPipeline;
1216 wgpu::RenderPipeline hdrOffscreenLitAdditivePipeline;
1217 wgpu::RenderPipeline hdrOffscreenLitPremultipliedPipeline;
1218 wgpu::RenderPipeline hdrOffscreenLitMultiplyPipeline;
1219 wgpu::RenderPipeline hdrOffscreenLitOpaquePipeline;
1221 wgpu::Buffer fullscreenQuadVb;
1222 wgpu::Buffer toneMapQuadVb;
1223 wgpu::Buffer fullscreenQuadIb;
1224 bool fullscreenQuadReady =
false;
1227 std::vector<SolidBatch> solidBatches;
1228 std::vector<TexturedBatch> texturedBatches;
1229 std::vector<LitBatch> litBatches;
1230 Lighting2DUBO lighting2dFrame{};
1231 enum class OverlayKind : uint8_t {
Solid, Textured,
Lit };
1232 struct OverlaySpan {
1233 OverlayKind
kind = OverlayKind::Solid;
1235 uint32_t vertBegin = 0;
1236 uint32_t vertCount = 0;
1238 std::vector<OverlaySpan> overlaySpans;
1239 bool sceneColorComposited =
false;
1240 void noteSolidOverlay(uint32_t batchIndex);
1241 void noteTexturedOverlay(Texture *tex, uint32_t batchIndex);
1242 void noteLitOverlay(uint32_t batchIndex);
1243 void clear2DBatches();
1246 bool frame3DStarted =
false;
1247 bool sceneColorPassOpen =
false;
1250 OffscreenCanvas *active3DCanvas =
nullptr;
1251 bool offscreenTimestampSupported =
false;
1252 wgpu::QuerySet offscreenTimestampQuerySet;
1253 wgpu::Buffer offscreenTimestampResolveBuffer;
1254 struct OffscreenTimestampReadback {
1259 std::array<OffscreenTimestampReadback, 3> offscreenTimestampReadbacks{};
1260 size_t offscreenTimestampReadbackCursor = 0;
1261 mutable std::atomic<float> completedOffscreenTimestampMs{0.f};
1265 wgpu::Texture presentedReadback;
1266 int presentedReadbackW = 0, presentedReadbackH = 0;
1267 WGPUTextureFormat presentedReadbackFormat = WGPUTextureFormat_Undefined;
1268 bool surfaceCanCopySrc =
false;
1269 void recordPresentedReadback(wgpu::CommandEncoder &encoder,
const wgpu::Texture &surfaceTexture);
1270 wgpu::Texture lastReadbackTex;
1271 int lastReadbackW = 0;
1272 int lastReadbackH = 0;
1273 glm::mat4 mesh3dViewProj{1.f};
1274 glm::mat4 mesh3dView{1.f};
1275 float mesh3dNear = 0.1f, mesh3dFar = 100.f;
1276 struct Primitive3DVertex {
1277 glm::vec4 clipPosition{0.f};
1278 glm::vec4
color{1.f};
1280 struct Primitive3DDraw {
1281 ScenePrimitivePaint paint;
1282 float averageDepth = 0.f;
1284 std::vector<Primitive3DVertex>
vertices;
1286 std::vector<Primitive3DDraw> primitive3DDraws;
1287 std::unordered_map<std::uint64_t, wgpu::RenderPipeline> primitive3DPipelines;
1288 Texture *sceneColorTexture =
nullptr;
1289 Texture *mesh3dNormalTexture =
nullptr;
1290 Texture *mesh3dHeightTexture =
nullptr;
1291 Texture *mesh3dEnvTexture =
nullptr;
1292 Texture *mesh3dSceneDepthTexture =
nullptr;
1293 Texture *mesh3dSceneColorTexture =
nullptr;
1294 float mesh3dEnvIntensity = 0.f;
1295 glm::vec3 mesh3dEnvProbeCenter{0.f};
1296 glm::vec3 mesh3dEnvProbeExtent{0.f};
1297 ReflectionProbeUpload mesh3dReflectionProbes{};
1298 float mesh3dMetallic = 0.f;
1299 float mesh3dRoughness = 0.45f;
1300 SurfaceMode mesh3dSurfaceMode = SurfaceMode::Opaque;
1301 BlendMode mesh3dSurfaceBlend = BlendMode::Alpha;
1302 bool mesh3dSurfaceDepthWrite =
false;
1303 bool mesh3dSurfaceDoubleSided =
false;
1304 float mesh3dAlphaCutoff = 0.5f;
1305 std::string mesh3dAlphaTechnique =
"cutoff";
1306 glm::vec4 mesh3dLodFade{1.f, 0.f, 0.f, 0.f};
1307 float mesh3dTexBombScale = 4.f, mesh3dTexBombStrength = 0.f, mesh3dTexBombRot = 1.f;
1308 float mesh3dParallaxScale = 0.f, mesh3dParallaxMin = 8.f, mesh3dParallaxMax = 32.f;
1309 glm::vec4 mesh3dVirtualTexture{0.f};
1310 glm::vec4 mesh3dVirtualAtlas{0.f};
1311 float mesh3dSsaoIntensity = 1.f;
1312 glm::vec4 mesh3dCloud{0.f, 1.5f, 0.f, 0.f};
1313 glm::vec4 mesh3dCloudWind{4.f, 0.f, 0.55f, 0.5f};
1314 Lighting3DPack mesh3dLighting{};
1315 ShadowUpload mesh3dShadows{};
1316 bool mesh3dShadowReceive =
true;
1317 int mesh3dSkinInfluenceLimit = 4;
1318 bool mesh3dClusteredActive =
false;
1319 ClusteredLightingUpload mesh3dClustered{};
1320 glm::vec3 mesh3dCameraPos{0.f, 0.f, 3.f};
1321 bool frameHad3DThisFrame =
false;
1322 std::vector<Mesh3dDraw> mesh3dDraws;
1323 std::optional<PbrSurface> mesh3dPbrSurface;
1324 struct PbrResources;
1325 static void deletePbrResources(PbrResources *
resources);
1326 std::unique_ptr<PbrResources, void (*)(PbrResources *)> pbrResources_{
1327 nullptr, &deletePbrResources};
1329 bool hasPendingClear =
true;
1332 int shadowPassCascade = -1;
1333 std::vector<ShadowDraw> shadowPassDraws;
1334 std::vector<ShadowDraw> shadowCascadeDraws[ShadowConfig::kTotalLayers];
1337 bool gbufferPassActive =
false;
1338 bool gbufferPassPending =
false;
1339 std::vector<GbufferDraw> gbufferPassDraws;
1342 std::vector<VoxelDraw> voxelDraws;
1343 wgpu::Buffer voxelUnitQuadVerts;
1344 wgpu::Buffer voxelUnitQuadIndices;
1346 struct SceneColorSlot {
1347 wgpu::Texture msaaColor;
1348 wgpu::TextureView msaaView;
1349 wgpu::Texture
color;
1350 wgpu::TextureView colorView;
1351 wgpu::Texture
depth;
1352 wgpu::TextureView depthView;
1353 GpuTexture colorGpu;
1355 uint32_t sampleCount = 1;
1357 int sceneColorWidth = 0, sceneColorHeight = 0;
1360 WGPUTextureFormat sceneColorFormat = WGPUTextureFormat_RGBA8Unorm;
1361 uint32_t sceneColorSamples = 1;
1362 std::vector<SceneColorSlot> sceneColorSlots;
1365 struct ShadowMapSlot {
1367 wgpu::TextureView
view;
1369 std::vector<ShadowMapSlot> shadowMaps;
1370 int shadowMapSize = ShadowConfig::kMapSize;
1373 struct GbufferSlot {
1375 wgpu::TextureView normalView;
1376 wgpu::Texture depthColor;
1377 wgpu::TextureView depthColorView;
1379 wgpu::TextureView albedoView;
1380 wgpu::Texture pbrParams;
1381 wgpu::TextureView pbrParamsView;
1382 wgpu::Texture emissive;
1383 wgpu::TextureView emissiveView;
1384 wgpu::Texture
depth;
1385 wgpu::TextureView depthView;
1386 wgpu::Texture visID;
1387 wgpu::TextureView visIDView;
1388 wgpu::Texture visBary;
1389 wgpu::TextureView visBaryView;
1390 GpuTexture normalGpu;
1391 GpuTexture depthColorGpu;
1392 GpuTexture albedoGpu;
1393 GpuTexture pbrParamsGpu;
1394 GpuTexture emissiveGpu;
1395 GpuTexture depthGpu;
1403 int gbufferWidth = 0, gbufferHeight = 0;
1404 std::vector<GbufferSlot> gbufferSlots;
1405 uint32_t lastGbufferSlot = 0;
1406 bool gbufferDepthValid_ =
false;
1410 wgpu::TextureView albedoView;
1412 wgpu::TextureView normalView;
1414 wgpu::TextureView paramsView;
1416 int decalWidth = 0, decalHeight = 0;
1417 std::vector<DecalSlot> decalSlots;
1418 std::vector<DecalDraw> decalPassDraws;
1419 glm::mat4 decalViewProj{1.f};
1420 bool decalPassActive =
false;
1421 bool decalPassPending =
false;
1422 bool decalReady =
false;
1423 uint32_t lastDecalSlot = 0;
1424 Texture *decalFlatAlbedo =
nullptr;
1425 Texture *decalFlatNormal =
nullptr;
1426 Texture *decalFlatParams =
nullptr;
1429 Canvas *activeCanvas =
nullptr;
1430 std::vector<std::unique_ptr<eve::graphics::Canvas>> ownedCanvases;
1433 std::vector<std::unique_ptr<Texture>> ownedTextures;
1434 std::vector<std::unique_ptr<GpuTexture>> ownedGpuTextures;
1435 std::unordered_map<std::string, Texture *> texturesByPath;
1436 std::unordered_map<std::string, Texture *> sharedTexturesByContent;
1437 std::vector<std::unique_ptr<Mesh>> ownedMeshes;
1438 std::vector<std::unique_ptr<GpuMesh>> ownedGpuMeshes;
1439 std::vector<std::unique_ptr<Shader>> ownedShaders;
1440 std::vector<std::unique_ptr<GpuShader>> ownedGpuShaders;
1445 bool gpuDrivenEnabled_ =
false;
1446 std::vector<Mesh *> gpuDrivenMeshes_;
1447 std::vector<Material *> gpuDrivenMaterials_;
1448 std::vector<uint32_t> gpuDrivenMaterialFree_;
1449 std::unordered_map<Mesh *, uint32_t> gpuDrivenMeshIds_;
1450 std::unordered_map<Material *, uint32_t> gpuDrivenMaterialIds_;
1451 std::vector<GpuInstance> gpuDrivenPending_;
1453 std::vector<GpuInstance> gpuDrivenVisible_;
1454 struct GpuDrivenBucket {
1458 uint32_t inputCount = 0;
1460 std::vector<GpuDrivenBucket> gpuDrivenBuckets_;
1461 wgpu::BindGroupLayout gpuDrivenComputeSetLayout_;
1462 wgpu::BindGroupLayout gpuDrivenRenderSetLayout_;
1463 wgpu::PipelineLayout gpuDrivenComputePipelineLayout_;
1464 wgpu::PipelineLayout gpuDrivenRenderPipelineLayout_;
1465 wgpu::ComputePipeline gpuDrivenCullPipeline_;
1466 wgpu::BindGroupLayout gpuDrivenHzbSetLayout_;
1467 wgpu::PipelineLayout gpuDrivenHzbPipelineLayout_;
1468 wgpu::ComputePipeline gpuDrivenHzbPipeline_;
1469 wgpu::Buffer gpuDrivenHzbBuffer_;
1470 wgpu::Buffer gpuDrivenHzbParamsBuffer_;
1471 wgpu::BindGroup gpuDrivenHzbBindGroup_;
1472 std::vector<uint32_t> gpuDrivenHzbOffsets_;
1473 uint32_t gpuDrivenHzbWidth_ = 0;
1474 uint32_t gpuDrivenHzbHeight_ = 0;
1475 uint64_t gpuDrivenHzbCapacity_ = 0;
1476 wgpu::RenderPipeline gpuDrivenRenderPipeline_;
1477 wgpu::RenderPipeline gpuDrivenCanvasPipeline_;
1478 wgpu::RenderPipeline gpuDrivenHdrCanvasPipeline_;
1479 wgpu::RenderPipeline gpuDrivenResidentRenderPipeline_;
1480 wgpu::RenderPipeline gpuDrivenResidentCanvasPipeline_;
1481 wgpu::RenderPipeline gpuDrivenResidentHdrCanvasPipeline_;
1482 wgpu::Buffer gpuDrivenParamsBuffer_;
1483 wgpu::Buffer gpuDrivenInputBuffer_;
1484 wgpu::Buffer gpuDrivenVisibleBuffer_;
1485 wgpu::Buffer gpuDrivenIndirectBuffer_;
1486 wgpu::Buffer gpuDrivenVisIndirectBuffer_;
1487 wgpu::BindGroup gpuDrivenComputeBindGroup_;
1488 wgpu::BindGroup gpuDrivenRenderBindGroup_;
1489 uint64_t gpuDrivenInputCapacity_ = 0;
1490 uint64_t gpuDrivenVisibleCapacity_ = 0;
1491 uint64_t gpuDrivenIndirectCapacity_ = 0;
1492 uint64_t gpuDrivenVisIndirectCapacity_ = 0;
1493 uint32_t gpuDrivenDispatchCount_ = 0;
1494 uint32_t gpuDrivenLastIndirectDrawCount_ = 0;
1495 uint32_t gpuDrivenLastBucketCount_ = 0;
1496 bool gpuDrivenComputePending_ =
false;
1497 bool gpuDrivenDrawPending_ =
false;
1498 bool gpuDrivenResidentDrawPending_ =
false;
1499 WGPUBuffer gpuDrivenResidentBuffer_ =
nullptr;
1500 uint64_t gpuDrivenResidentOffset_ = 0;
1501 uint64_t gpuDrivenResidentSize_ = 0;
1502 bool gpuDrivenVisPending_ =
false;
1503 bool gpuDrivenResolvePending_ =
false;
1504 wgpu::BindGroupLayout gpuDrivenVisSetLayout_;
1505 wgpu::BindGroupLayout gpuDrivenResolveSetLayout_;
1506 wgpu::PipelineLayout gpuDrivenVisPipelineLayout_;
1507 wgpu::PipelineLayout gpuDrivenResolvePipelineLayout_;
1508 wgpu::RenderPipeline gpuDrivenVisPipeline_;
1509 wgpu::RenderPipeline gpuDrivenResolvePipeline_;
1511 struct GpuDrivenVgAsset {
1514 wgpu::Buffer clusters;
1515 wgpu::Buffer indirect;
1518 uint32_t triangleCount = 0;
1519 uint32_t clusterCount = 0;
1521 glm::mat4
model{1.f};
1525 std::vector<GpuDrivenVgAsset> gpuDrivenVgAssets_;
1526 std::unordered_map<Mesh *, uint32_t> gpuDrivenVgMeshIds_;
1527 wgpu::BindGroupLayout gpuDrivenVgComputeSetLayout_;
1528 wgpu::BindGroupLayout gpuDrivenVgVisSetLayout_;
1529 wgpu::BindGroupLayout gpuDrivenVgResolveSetLayout_;
1530 wgpu::PipelineLayout gpuDrivenVgComputePipelineLayout_;
1531 wgpu::PipelineLayout gpuDrivenVgVisPipelineLayout_;
1532 wgpu::PipelineLayout gpuDrivenVgResolvePipelineLayout_;
1533 wgpu::ComputePipeline gpuDrivenVgCullPipeline_;
1534 wgpu::RenderPipeline gpuDrivenVgVisPipeline_;
1535 wgpu::RenderPipeline gpuDrivenVgResolvePipeline_;
1536 glm::mat4 gpuDrivenVgViewProj_{1.f};
1537 glm::vec3 gpuDrivenVgCameraPos_{0.f};
1538 float gpuDrivenVgFovYDeg_ = 60.f;
1539 float gpuDrivenVgNear_ = 0.1f;
1540 float gpuDrivenVgFar_ = 100.f;
1541 glm::vec4 gpuDrivenVgPlanes_[6]{};
1542 uint32_t gpuDrivenVgVisibleDiagnostic_ = 0;
1543 uint32_t gpuDrivenVgLastIndirectDrawCount_ = 0;
1544 bool gpuDrivenVgComputePending_ =
false;
1545 bool gpuDrivenVgVisPending_ =
false;
1546 bool gpuDrivenVgResolvePending_ =
false;
1550 struct PendingReadback {
1553 uint64_t bytesPerRow = 0;
1555 bool mapped =
false,
done =
false,
ok =
false;
1556 bool hdr =
false, bgra =
false;
1558 std::unique_ptr<PendingReadback> pendingReadback_;
1564 using MeshBindGroupKey =
1565 std::tuple<uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t,
1566 uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t>;
1567 std::map<MeshBindGroupKey, wgpu::BindGroup> meshBindGroupCache_;
1568 static constexpr size_t kMaxMeshBindGroupCache = 128;
1569 void clearMeshBindGroupCache() { meshBindGroupCache_.clear(); }
1571 void markSwapchainDirty()
override { swapchainConfigured =
false; }
1572 void rebuildSwapchainIfNeeded();
1573 bool acquireSurfaceTexture(wgpu::TextureView &
view, wgpu::Texture &
texture);