373 void initGpuTiming();
375 void writeGpuTimestampBegin();
377 void writeGpuTimestampEnd();
379 void readGpuFrameTiming();
382 bool gpuTimingAvailable()
const override;
384 float gpuFrameMs()
const override;
387 std::string getBackendName()
const override;
389 bool supportsRuntimeGlslCompilation()
const override;
392 void initWithWindow(
void *nativeWindow)
override;
402 void onNativeWindowDestroyed()
override;
408 ensurePresentCaptureHook();
412 if (vsyncEnabled ==
enabled)
return;
416 markSwapchainDirty();
420 const int raw = samples > 1 ? samples : 0;
421 if (
raw == msaaSamples)
return;
428 void setViewportSize(
int width,
int height,
int pixelwidth,
int pixelheight)
override;
449 void syncMaterialTable();
461 return gpuDrivenCaps_.gpuDrivenAvailable();
466 void drawDeferredLighting()
override;
469 return gpuDrivenEnabled_ && gpuDrivenCaps_.gpuDrivenAvailable();
474 uint32_t gpuDrivenMeshRecord(
Mesh *
mesh)
override;
478 return materialTableGetOrCreate(
material);
485 uint32_t gpuDrivenReflectionProbeSlot(
Texture *cubemap)
override;
493 Texture *getSceneLinearDepthTexture()
override;
501 bool canSubmitGpuParticles()
const override;
520 uint32_t debugBindlessIndex(
Texture *tex)
const;
522 uint32_t debugMeshRecordIndex(
Mesh *
mesh)
const;
526 void waitForSharedGpuResources();
529 return gpuDrivenEnabled_ && gpuDrivenCaps_.gpuDrivenCullAvailable();
536 void gpuDrivenCullEmit(
const glm::mat4 &
viewProj,
const glm::vec3 &
eye,
float fovYDeg,
537 float nearZ,
float farZ);
539 void gpuDrivenOpenScenePass();
541 void gpuDrivenDrawOpaque();
543 bool gpuDrivenResolveWanted()
const override;
545 void gpuDrivenRecordVisPass()
override;
547 void gpuDrivenResolve()
override;
551 uint32_t gpuDrivenVgAssetId(
Mesh *
mesh)
const override;
553 bool gpuDrivenVgAttachToMesh(
Mesh *
mesh, uint32_t vgAssetId)
override;
555 bool gpuDrivenVgSetInstance(uint32_t vgAssetId,
const glm::mat4 &
model,
556 uint32_t materialId)
override;
558 void gpuDrivenVgComputeSection(
const glm::mat4 &
viewProj,
const glm::vec3 &
eye,
559 float fovYDeg,
float nearZ,
float farZ)
override;
561 uint32_t debugGpuDrivenVgVisibleCount()
const;
563 uint32_t debugGpuDrivenVisibleCount()
const;
565 uint32_t debugGpuDrivenCulledDrawCount()
const;
568 void drawSolidRect(
float x,
float y,
float w,
float h,
const Color &
color,
573 void drawSolidRectRotated(
float cx,
float cy,
float w,
float h,
float degrees,
582 bool repeatV =
false)
override;
588 std::span<const uint8_t> rgba)
override;
591 std::span<const uint16_t> rgbaHalf)
override;
594 std::span<const uint8_t> rgba)
override;
596 Texture *newCubemap(
int faceSize,
const uint8_t *rgbaFaces)
override;
598 Texture *newCubemap(
int faceSize,
const uint8_t *rgbaFaces,
602 Texture *newHDRCubemap(
int faceSize)
override;
607 bool copyHDRCanvasesToCubemap(
Canvas *
const *
sources,
int faceCount,
611 bool filterHDRReflectionCubemap(
Texture *cubemap,
int sampleCount = 64)
override;
621 float getMaxAnisotropy()
const override;
624 bool reloadTextureFromFile(
const std::string &filename)
override;
631 const uint8_t *rgba)
override;
637 int height, std::span<const std::uint8_t> rgba,
638 std::size_t bytesPerRow = 0)
override;
641 Texture *
texture, std::span<const TextureRegionUpload> regions)
override;
645 bool replaceTexturePixelsRGBA(
Texture *tex,
int w,
int h,
const uint8_t *rgba);
652 void drawTexturedRectUV(
Texture *
texture,
float x,
float y,
float w,
float h,
float u0,
float v0,
653 float u1,
float v1,
const Color &
color)
override;
656 float h,
float u0,
float v0,
float u1,
float v1,
661 float w,
float h,
float degrees,
float u0,
float v0,
663 bool rotatedUV =
false,
681 SceneColorDistortionStatus drawSceneColorDistortionUVRotated(
683 float v0,
float u1,
float v1,
float strengthPixels,
float opacity,
684 bool rotatedUV =
false)
override;
695 Shader *newShaderFromSpv(
const std::vector<uint32_t> &vertSpv,
696 const std::vector<uint32_t> &fragSpv)
override;
698 Shader *newShaderFromSpvFile(
const std::string &vertPath,
const std::string &fragPath)
override;
700 Shader *newShader(
const std::string &vertGlsl,
const std::string &fragGlsl)
override;
702 Shader *newMeshShaderFromSpv(
const std::vector<uint32_t> &vertSpv,
703 const std::vector<uint32_t> &fragSpv)
override;
705 Shader *newMeshShaderFromWgsl(
const std::string &vertWgsl,
706 const std::string &fragWgsl)
override;
708 Shader *newShaderFromWgsl(
const std::string &vertWgsl,
709 const std::string &fragWgsl)
override;
713 const std::vector<uint32_t> &fragSpv)
override;
716 const std::vector<uint32_t> &fragSpv,
721 const std::string &fragWgsl)
override;
725 const std::string &fragGlsl)
override;
727 Shader *newMeshShader(
const std::string &vertGlsl,
const std::string &fragGlsl)
override;
729 Shader *newHairShaderFromSpv(
const std::vector<uint32_t> &vertSpv,
730 const std::vector<uint32_t> &fragSpv)
override;
732 Shader *newHairShaderFromWgsl(
const std::string &vertWgsl,
733 const std::string &fragWgsl)
override;
742 Mesh *newMeshFromAssimp(const ::aiMesh &
mesh)
override;
744 Mesh* newMeshFromAssimp(const ::aiMesh&
mesh,
const glm::mat4& worldTransform)
override;
746 Mesh *newMeshFromArrays(
const float *posXYZ,
const float *nrmXYZ,
const float *uvST,
755 Mesh *newMeshFromArraysColored(
const float *posXYZ,
const float *nrmXYZ,
const float *uvST,
768 bool bakeMeshMorph(
Mesh *
mesh)
override;
770 bool updateMeshVertices(
Mesh *
mesh,
const float *posXYZ,
const float *nrmXYZ,
const float *uvST,
773 bool setMeshSkinningData(
Mesh *
mesh,
const uint16_t *joints4,
const float *weights4,
776 Mesh *newMeshSphere(
int slices = 32,
int stacks = 16)
override;
778 Mesh *newMeshCylinder(
int slices = 32,
int stacks = 1,
bool caps =
true)
override;
780 bool releaseMesh(
Mesh *
mesh)
override;
784 void begin3DFrame()
override;
786 void begin3DFrameToCanvas(
Canvas *canvas)
override;
788 void end3DFrameToCanvas()
override;
793 return lastOffscreen3DGpuDurationMs;
796 void setMesh3DViewProj(
const glm::mat4 &
viewProj)
override;
798 void setMesh3DView(
const glm::mat4 &
view)
override;
800 void setMesh3DClip(
float nearZ,
float farZ)
override;
802 Texture *getSceneColorTexture()
override;
817 image::ImageData *readDecalLayerToImageData(
const std::string &attachment)
override;
832 std::uint32_t
count)
override;
834 void drawVoxelFaceInstances(
const uint32_t *packed,
int count,
float originX,
float originY,
float originZ,
835 const std::string &faceDir,
Texture *atlas,
int tilesPerRow = 16,
836 const uint32_t *
ao =
nullptr)
override;
840 void setMesh3DPackedNormalMask(
bool enabled)
override;
844 void setMesh3DVirtualTexture(
bool enabled,
int pageCountX,
int pageCountY,
845 int atlasSlotsX,
int atlasSlotsY,
846 float borderFraction)
override;
860 const std::string &alphaTechnique =
"cutoff")
override;
862 void setMesh3DTexCellBomb(
float cellScale,
float strength,
float rotAmount = 1.f)
override;
864 void setMesh3DParallax(
float scale,
float minLayers = 8.f,
float maxLayers = 32.f)
override;
866 void setMesh3DLodDither(
float weight,
bool reverse,
bool enabled)
override;
870 void setCloudShadows(
float strength,
float worldCell,
float time,
float windSpeed,
float windAngle,
float coverage,
875 void setMesh3DClusteredActive(
bool active)
override;
879 void setMesh3DLight(
const glm::vec3 &
dir,
const glm::vec3 &
color)
override;
881 void setMesh3DCameraPos(
const glm::vec3 &
eye)
override;
883 void setMesh3DEnv(
Texture *cube,
float intensity)
override;
885 void setMesh3DEnvProbe(
const glm::vec3 &
center,
const glm::vec3 &extent)
override;
889 [[nodiscard]]
Result<void> setSceneToneMapping(SceneToneMapping mode)
override;
893 [[nodiscard]]
Result<void> setDisplayOutputMode(DisplayOutputMode mode)
override;
895 [[nodiscard]]
Result<void> setDisplayHdrCalibration(
float paperWhiteNits,
float peakNits)
override;
899 [[nodiscard]]
Result<void> setScenePhotographicVignette(
float intensity)
override;
903 void setSceneExposure(
float exposure)
override { sceneExposure = std::max(exposure, 0.f); }
908 sceneColorFilter = glm::max(glm::vec3(
color.r,
color.g,
color.b), glm::vec3(0.f));
914 sceneLift = glm::vec3(lift.
r, lift.
g, lift.
b);
915 sceneInverseGamma = glm::max(glm::vec3(inverseGamma.
r, inverseGamma.
g, inverseGamma.
b), glm::vec3(0.001F));
922 return {sceneInverseGamma.
r, sceneInverseGamma.g, sceneInverseGamma.b};
928 float lensDistortion,
float lensScale)
override {
929 sceneTransitionVignette = std::clamp(vignette, 0.0F, 1.0F);
930 sceneTransitionVignetteSmoothness = std::clamp(vignetteSmoothness, 0.01F, 1.0F);
931 sceneTransitionLensDistortion = std::clamp(lensDistortion, 0.0F, 1.0F);
932 sceneTransitionLensScale = std::clamp(lensScale, 0.01F, 5.0F);
945 sceneAutoExposureMinEV = minEV;
946 sceneAutoExposureMaxEV = maxEV;
956 sceneBloomIntensity = intensity;
957 sceneBloomThreshold = threshold;
965 float farZ)
override {
966 sceneDofFocusDistance = focusDistance;
967 sceneDofMaxBlurPx = maxBlurPx;
968 sceneDofFocusRange = focusRange;
969 sceneDofNearZ = nearZ;
983 void setMesh3DShadows(
const ShadowUpload &upload)
override;
985 void setMesh3DShadowReceive(
bool receive)
override;
989 void beginShadowPass(
int cascadeIndex)
override;
991 void drawMeshShadow(
Mesh *
mesh,
const glm::mat4 &lightMVP,
bool doubleSided =
true)
override;
997 void endShadowPass()
override;
1000 void beginGBufferPass(
int width,
int height)
override;
1002 void drawMeshGBuffer(
Mesh *
mesh,
const glm::mat4 &
mvp,
const glm::mat4 &
model,
float nearZ,
float farZ,
1003 Texture *
albedo =
nullptr,
float tintR = 1.f,
float tintG = 1.f,
float tintB = 1.f,
1004 float motionX = 0.f,
float motionY = 0.f,
float roughness = 0.45f,
float metallic = 0.f,
1005 float tintA = 1.f)
override;
1007 void drawMeshGBufferAlpha(
Mesh *
mesh,
const glm::mat4 &
mvp,
const glm::mat4 &
model,
float nearZ,
float farZ,
1008 Texture *
albedo =
nullptr,
float tintR = 1.f,
float tintG = 1.f,
float tintB = 1.f,
1009 float motionX = 0.f,
float motionY = 0.f,
float roughness = 0.45f,
float metallic = 0.f,
1010 float tintA = 1.f)
override;
1012 void endGBufferPass()
override;
1017 void beginDecalPass(
int width,
int height)
override;
1019 void setDecalCamera(
const glm::mat4 &
viewProj,
float nearZ,
float farZ)
override;
1022 const float uvRect[4],
float fade,
float normalStrength,
float roughnessStrength,
1023 float metalStrength,
float emissiveStrength,
int blendMode = 0,
1024 int projectionMode = 0,
float blendSharpness = 4.f,
1025 float parallaxScale = 0.f,
float parallaxMinLayers = 8.f,
1026 float parallaxMaxLayers = 24.f,
float edgeFadeWidth = 0.06f)
override;
1028 void endDecalPass()
override;
1036 void setCanvas(
Canvas *canvas)
override;
1038 bool isCanvasActive()
const override;
1040 Canvas *getCanvas()
const override;
1043 Texture *getTexture()
override;
1052 void clear(std::optional<Color>
color, std::optional<int> stencil, std::optional<double>
depth)
override;
1054 Color getPixel(
int x,
int y)
override;
1071 void beginPathSceneOutput();
1075 void retirePathSceneOutput();
1097 return hdr ? hdrOffscreenRenderPass : offscreenRenderPass;
1101 return hdr ? hdrOffscreen3DRenderPass : offscreen3DRenderPass;
1114 createUiColorResources(
int(swapchain.extent.width),
int(swapchain.extent.height));
1128 void drawUiTextureRects(
void* commandBuffer,
const std::vector<UiTextureDraw>& draws, std::size_t bufferOffset = 0);
1150 std::uint32_t
first, std::uint32_t
count, std::span<const glm::mat4> models);
1151 struct GpuParticleDrawRequest;
1154 struct Mesh3dFrameSlots;
1155 struct Mesh3dClusteredFrameSlots;
1158 struct DecalSetKeyHash;
1160 void createSwapchainAndPipeline();
1161 void applyPreferredSwapchainFormats(vkb::SwapchainBuilder &builder);
1162 void applyPendingResourceChanges()
override;
1163 [[nodiscard]]
ResultRef<Texture> requestFileTexture(
const std::string &filename)
override;
1164 bool samplerChangesPending_ =
false;
1165 struct CachedSampler {
1170 std::vector<CachedSampler> cachedSamplers_;
1171 void createTexturedPipeline();
1172 void createLit2DPipeline();
1173 void createGpuParticlePipelines();
1174 void destroyGpuParticleResources();
1175 void recordGpuParticleCompute(vk::CommandBuffer cb);
1176 void drawGpuParticleRequest(vk::CommandBuffer cb,
const GpuParticleDrawRequest&
request);
1177 struct PbrResources;
1178 static void deletePbrResources(PbrResources*
resources);
1179 std::unique_ptr<PbrResources, void (*)(PbrResources*)> pbrResources_{
nullptr, &deletePbrResources};
1180 std::optional<PbrSurface> pbrSurface_;
1181 void destroyPbrResources();
1182 void destroyPbrGBufferResources();
1185 void createMesh3DPipeline();
1186 void createMesh3DClusteredPipeline();
1187 void createDeferredLightingPipeline();
1188 void destroyDeferredLightingResources();
1189 void createVoxelRectPipeline();
1190 vk::Pipeline buildVoxelRectPipeline(
const vkb::BuiltRenderPass &rp,
1191 vk::SampleCountFlagBits samples);
1192 void destroyVoxelRectResources();
1193 void ensureVoxelUnitQuad();
1194 vkb::BoundSet voxelRectSetFor(GpuTexture *gpuTex);
1195 void createShadowResources();
1196 void destroyShadowResources();
1197 void createGBufferResources(
int width,
int height);
1198 void destroyGBufferResources();
1200 void destroyDecalResources();
1201 void recordDecalPass();
1202 void recordDecalPassInto(vk::CommandBuffer cb, DecalSlot &
slot, GBufferSlot &gslot);
1203 void ensureDecalUnitBox();
1204 void ensureDecalPlaceholders();
1205 vkb::BoundSet decalSetFor(DecalSlot &
slot, GpuTexture *
albedo, GpuTexture *
normal,
1206 GpuTexture *
params, GpuTexture *
depth, GpuTexture *gbNormal);
1207 DecalSlot *currentDecalSlot();
1208 void createSceneColorResources(
int width,
int height);
1209 void destroySceneColorResources();
1210 void createUiColorResources(
int width,
int height);
1211 void destroyUiColorTargets();
1212 void destroyUiColorResources();
1213 void queueUiResolve();
1215 bool renderUiOverlayPass();
1217 void recordDeferredFrameGraph();
1218 void dropPendingOffscreenPasses();
1220 bool beginSwapchainRenderPass();
1222 void beginSwapchainColorPass();
1223 bool beginSceneColorRenderPass();
1224 void endSceneColorRenderPass();
1225 void ensureOffscreen3DResources();
1226 void destroyOffscreen3DResources();
1227 void queueSceneColorResolve();
1228 void materializeSceneColorResolve();
1229 void ensureClusteredBuffers(
size_t lightsBytes,
size_t tableBytes,
size_t indicesBytes);
1231 void ensureMesh3dStrides();
1232 void ensureMesh3dRing(Mesh3dFrameSlots &fslots);
1233 size_t uploadSkinPalette(
Mesh*
mesh, Mesh3dFrameSlots& fslots);
1234 vk::DescriptorSet skinPassSetFor(GpuTexture *
albedo, Mesh3dFrameSlots &fslots);
1236 const glm::mat4 &
model,
const glm::vec4 &
clip,
1237 vk::DescriptorSet &set, uint32_t &uboOffset);
1238 void ensureMesh3dClusteredRing(Mesh3dClusteredFrameSlots &fslots);
1239 vkb::BoundSet mesh3dClusteredSetFor(GpuTexture *gpuTex, GpuTexture *normalTex,
1240 GpuTexture *envTex, GpuTexture *heightTex,
1241 GpuTexture *decalAlbedo, GpuTexture *decalNormal,
1242 GpuTexture *decalParams,
1243 Mesh3dClusteredFrameSlots &fslots);
1244 void ensureOffscreenPipelines();
1245 void ensureHdrOffscreenPipelines();
1249 void destroyPresentGraphicsPipelines();
1251 void rebuildGpuParticleDrawPipelines(
const vkb::BuiltRenderPass &
target);
1253 void ensureHdrGpuParticleDrawPipelines();
1255 void ensurePresentComposeResources(
int width,
int height);
1256 void destroyPresentComposeResources();
1257 struct PresentComposeSlot;
1265 PresentComposeSlot *currentPresentComposeSlot();
1267 bool beginPresentComposePass();
1268 void endPresentComposePass();
1270 void encodePresentComposeToSwapchain(vk::CommandBuffer cb);
1271 vk::Pipeline createTexturedStylePipeline(
const std::vector<uint32_t> &
vert,
const std::vector<uint32_t> &
frag,
1272 const vkb::BuiltRenderPass &rp, vk::PipelineLayout
layout,
1274 vk::SampleCountFlagBits samples = vk::SampleCountFlagBits::e1);
1275 vk::Pipeline createMesh3DStylePipeline(
const std::vector<uint32_t> &
vert,
const std::vector<uint32_t> &
frag,
1276 vk::PipelineLayout
layout,
const vkb::BuiltRenderPass &rp,
1277 vk::SampleCountFlagBits samples,
BlendMode blend = BlendMode::Opaque,
1281 const MeshShaderRasterState &raster);
1282 static constexpr size_t kMesh3DPipelineVariants = 20;
1284 static constexpr size_t kPrimitive3DPipelineVariants = 45;
1285 static size_t primitive3DPipelineIndex(PrimitiveDepthMode
depth,
BlendMode blend, PrimitiveCullMode cull);
1287 vk::Pipeline createMesh3DXrayPipeline(
const std::vector<uint32_t> &
vert,
1288 const std::vector<uint32_t> &
frag,
1289 vk::PipelineLayout
layout,
1290 const vkb::BuiltRenderPass &rp,
1291 vk::SampleCountFlagBits samples);
1292 vk::Pipeline createMesh3DHairPipeline(
const std::vector<uint32_t> &
vert,
1293 const std::vector<uint32_t> &
frag,
1294 vk::PipelineLayout
layout,
1295 const vkb::BuiltRenderPass &rp,
1296 vk::SampleCountFlagBits samples);
1298 void ensureScenePassPipelines(
const vkb::BuiltRenderPass &
target,
1299 vk::SampleCountFlagBits samples);
1300 void rebuildPrimitive3DPipelines(
const vkb::BuiltRenderPass &
target, vk::SampleCountFlagBits samples);
1301 void buildPrimitive3DPipelines(
const vkb::BuiltRenderPass &
target, vk::SampleCountFlagBits samples,
1302 std::array<vk::Pipeline, kPrimitive3DPipelineVariants> &pipelines);
1304 int clampMsaaSamples(
int requested)
const;
1306 const vkb::BuiltRenderPass &activeScenePass()
const {
1307 return sceneColorRenderPass ? sceneColorRenderPass : renderpass;
1310 vk::SampleCountFlagBits activeSceneSamples()
const {
1311 return sceneColorRenderPass ? sceneColorSamples : vk::SampleCountFlagBits::e1;
1313 void destroySwapchainResources();
1315 void flushToSwapchain();
1316 void flushToOffscreen(OffscreenCanvas *canvas);
1317 void abortOpen3DFrame();
1318 void noteSolidOverlay(uint32_t batchIndex);
1319 void noteTexturedOverlay(Texture *tex, uint32_t batchIndex);
1320 void noteLitOverlay(uint32_t batchIndex);
1321 void clear2DBatches();
1322 void drawLitBatches(vk::CommandBuffer cb,
int viewW,
int viewH, std::vector<LitBatch> &batches,
1323 std::vector<vkb::HostVertexBuffer> &texBufs,
size_t &texBufIndex,
bool offscreen,
1325 vkb::BoundSet lit2dSetFor(GpuTexture *
albedo, GpuTexture *
normal,
bool offscreen);
1326 vkb::BoundSet post2SetFor(GpuTexture *
color, GpuTexture *
depth,
1327 GpuTexture *motion =
nullptr, GpuTexture *extra =
nullptr,
1328 GpuTexture *specular =
nullptr);
1329 void ensureFlatNormalTexture();
1331 void ensureReadbackSlots();
1333 bool recordSwapchainReadback(vk::CommandBuffer cb);
1335 void syncReadbackCpu();
1337 void destroyReadbackResources();
1338 void ensurePresentCaptureHook();
1339 vkb::BoundSet mesh3dSetFor(GpuTexture *gpuTex, GpuTexture *normalTex, GpuTexture *envTex, GpuTexture *heightTex,
1340 GpuTexture *depthTex, GpuTexture *sceneColorTex, GpuTexture *decalAlbedo,
1341 GpuTexture *decalNormal, GpuTexture *decalParams, Mesh3dFrameSlots &fslots,
1342 vk::DescriptorSetLayout resourceLayout);
1343 void ensureDefaultEnvCubemap();
1344 void ensureFlatNormalTexture3D();
1345 void ensureFlatHeightTexture3D();
1346 vk::Sampler createVkSampler(
const TextureSampler &
sampler, uint32_t mipLevels)
const;
1347 void writeCombinedImageDescriptor(GpuTexture *
gpu);
1354 void createInstanceAndDevice(
const std::vector<const char *> &instanceExtensions,
1355 void *nativeWindow, vk::SurfaceKHR *surfaceOut);
1357 bool rebuildSwapchainIfNeeded();
1359 bool beginPresentCommandBuffer();
1361 bool initialized =
false;
1362 bool headless_ =
false;
1363 bool hasPresentedFrame =
false;
1364 float maxSamplerAnisotropy = 1.f;
1365 std::vector<uint8_t> lastFrameRgba;
1366 struct CaptureCopy {
1368 vkb::GenericBuffer staging;
1370 vk::ImageLayout
layout{};
1371 vk::ImageAspectFlags aspect{};
1373 size_t storageIndex = 0;
1375 std::vector<CaptureCopy> captureCopies_;
1377 std::string captureToken_;
1378 vk::Fence captureFence_;
1379 uint64_t captureFrameId_ = 0;
1380 uint64_t captureTargetGeneration_ = 1;
1381 bool captureSubmitted_ =
false;
1382 void recordFrameCapture(vk::CommandBuffer cb);
1383 void markFrameCaptureSubmitted();
1384 void pollFrameCapture();
1385 void destroyFrameCapture();
1390 struct ScreenReadbackSlot {
1391 vkb::GenericBuffer staging;
1392 void *mapped =
nullptr;
1394 std::vector<ScreenReadbackSlot> screenReadbackSlots;
1395 size_t screenReadbackBytes = 0;
1396 size_t readbackWriteSlot = 0;
1397 bool readbackReady =
false;
1398 bool readbackCpuSynced =
false;
1399 bool readbackBgra =
false;
1400 Canvas *activeCanvas =
nullptr;
1401 bool swapchainDirty =
false;
1402 void markSwapchainDirty()
override { swapchainDirty =
true; }
1405 std::atomic<bool> surfaceNeedsRecreate{
false};
1406 void recreateSurfaceForResume();
1409 bool isRenderSurfaceReady()
const;
1413 bool isRenderSurfaceStable();
1414 int pendingSurfaceW = 0;
1415 int pendingSurfaceH = 0;
1416 int surfaceStableFrames = 0;
1417 void *sdlWindow =
nullptr;
1418 vk::SurfaceKHR surface;
1421#if defined(VKB_ENABLE_VMA)
1424 VmaAllocatorOwner vmaAllocatorOwner_;
1427 vkb::Swapchain swapchain;
1428 vkb::BuiltRenderPass renderpass;
1430 vk::Pipeline pipeline;
1431 vk::PipelineLayout pipelineLayout;
1432 vk::Pipeline solidAlphaPipeline;
1433 vk::Pipeline additiveSolidPipeline;
1434 vk::Pipeline premultipliedSolidPipeline;
1435 vk::Pipeline multiplySolidPipeline;
1437 vk::DescriptorSetLayout texSetLayout;
1438 vk::UniqueDescriptorSetLayout texSetLayoutUnique;
1439 vk::DescriptorPool descriptorPool;
1440 vk::Pipeline texPipeline;
1441 vk::Pipeline additiveTexPipeline;
1442 vk::Pipeline premultipliedTexPipeline;
1443 vk::Pipeline multiplyTexPipeline;
1444 vk::Pipeline opaqueTexPipeline;
1445 vk::Pipeline particleDistortionPipeline;
1446 vk::Pipeline sceneTonemapPipeline;
1448 vk::Format presentAttachmentFormat_ = vk::Format::eUndefined;
1449 SceneToneMapping sceneToneMapping_ = SceneToneMapping::Aces;
1450 vk::SurfaceFormatKHR selectedSurfaceFormat_{vk::Format::eB8G8R8A8Unorm,
1451 vk::ColorSpaceKHR::eSrgbNonlinear};
1452 float sceneExposure = 1.f;
1453 glm::vec3 sceneColorFilter{1.f};
1454 glm::vec3 sceneLift{0.f};
1455 glm::vec3 sceneInverseGamma{1.f};
1456 glm::vec3 sceneGain{1.f};
1457 float scenePhotographicVignette_ = 0.0F;
1458 float sceneTransitionVignette = 0.0F;
1459 float sceneTransitionVignetteSmoothness = 0.2F;
1460 float sceneTransitionLensDistortion = 0.0F;
1461 float sceneTransitionLensScale = 1.0F;
1462 bool sceneAutoExposure =
false;
1463 float sceneAutoExposureMinEV = -8.f;
1464 float sceneAutoExposureMaxEV = 8.f;
1465 float sceneBloomIntensity = 0.f;
1466 float sceneBloomThreshold = 1.f;
1467 float sceneDofFocusDistance = 0.f;
1468 float sceneDofMaxBlurPx = 0.f;
1469 float sceneDofFocusRange = 8.f;
1470 float sceneDofNearZ = 0.1f;
1471 float sceneDofFarZ = 100.f;
1472 vk::PipelineLayout texPipelineLayout;
1473 vk::PipelineLayout shaderPipelineLayout;
1474 vk::CommandPool uploadPool;
1476 vkb::BuiltRenderPass offscreenRenderPass;
1477 vkb::BuiltRenderPass hdrOffscreenRenderPass;
1478 vk::Pipeline offscreenSolidPipeline;
1479 vk::Pipeline offscreenSolidAlphaPipeline;
1480 vk::Pipeline offscreenAdditiveSolidPipeline;
1481 vk::Pipeline offscreenPremultipliedSolidPipeline;
1482 vk::Pipeline offscreenMultiplySolidPipeline;
1483 vk::Pipeline offscreenTexPipeline;
1484 vk::Pipeline offscreenAdditiveTexPipeline;
1485 vk::Pipeline offscreenPremultipliedTexPipeline;
1486 vk::Pipeline offscreenMultiplyTexPipeline;
1487 vk::Pipeline offscreenOpaqueTexPipeline;
1488 vk::Pipeline hdrOffscreenTexPipeline;
1489 vk::Pipeline hdrOffscreenOpaqueTexPipeline;
1490 vk::Pipeline hdrOffscreenAdditiveTexPipeline;
1491 vk::Pipeline hdrOffscreenPremultipliedTexPipeline;
1492 vk::Pipeline hdrOffscreenMultiplyTexPipeline;
1493 vk::Pipeline hdrOffscreenSolidPipeline;
1494 vk::Pipeline hdrOffscreenSolidAlphaPipeline;
1495 vk::Pipeline hdrOffscreenAdditiveSolidPipeline;
1496 vk::Pipeline hdrOffscreenPremultipliedSolidPipeline;
1497 vk::Pipeline hdrOffscreenMultiplySolidPipeline;
1498 vk::Pipeline hdrOffscreenLitPipeline;
1499 vk::Pipeline hdrOffscreenLitAdditivePipeline;
1500 vk::Pipeline hdrOffscreenLitPremultipliedPipeline;
1501 vk::Pipeline hdrOffscreenLitMultiplyPipeline;
1502 vk::Pipeline hdrOffscreenLitOpaquePipeline;
1503 vk::Pipeline hdrOffscreenTonemapPipeline;
1504 vk::Pipeline hdrOffscreenParticleDistortionPipeline;
1505 vk::Pipeline hdrGpuParticleAlphaPipeline_{};
1506 vk::Pipeline hdrGpuParticleAdditivePipeline_{};
1507 vk::Pipeline hdrGpuParticlePremultipliedPipeline_{};
1508 vk::Pipeline hdrGpuParticleMultiplyPipeline_{};
1509 vk::Pipeline hdrGpuParticleOpaquePipeline_{};
1510 bool presentComposeActive_ =
false;
1512 vk::DescriptorSetLayout mesh3dSetLayout;
1513 vk::UniqueDescriptorSetLayout mesh3dSetLayoutUnique;
1514 vk::PipelineLayout mesh3dPipelineLayout;
1515 vk::PipelineLayout mesh3dShaderPipelineLayout;
1516 vk::Pipeline mesh3dPipeline;
1517 vk::Pipeline mesh3dTransparentPipeline;
1518 std::array<vk::Pipeline, kMesh3DPipelineVariants> mesh3dSurfacePipelines{};
1519 std::array<vk::Pipeline, kPrimitive3DPipelineVariants> primitive3DPipelines{};
1520 std::array<vk::Pipeline, kPrimitive3DPipelineVariants> offscreenPrimitive3DPipelines{};
1521 std::array<vk::Pipeline, kPrimitive3DPipelineVariants> hdrOffscreenPrimitive3DPipelines{};
1522 std::vector<vkb::HostVertexBuffer> offscreenPrimitive3DBufs;
1523 std::size_t offscreenPrimitive3DDrawIndex = 0;
1527 struct Mesh3dSetKey {
1528 GpuTexture *
albedo =
nullptr;
1529 GpuTexture *
normal =
nullptr;
1530 GpuTexture *env =
nullptr;
1531 GpuTexture *reflectionProbe0 =
nullptr;
1532 GpuTexture *reflectionProbe1 =
nullptr;
1533 GpuTexture *
height =
nullptr;
1534 GpuTexture *
depth =
nullptr;
1535 GpuTexture *sceneColor =
nullptr;
1536 GpuTexture *decalAlbedo =
nullptr;
1537 GpuTexture *decalNormal =
nullptr;
1538 GpuTexture *decalParams =
nullptr;
1539 size_t paletteSlot = 0;
1540 bool resourceProgram =
false;
1542 bool operator==(
const Mesh3dSetKey &o)
const {
1543 return albedo == o.albedo &&
normal == o.normal && env == o.env && reflectionProbe0 == o.reflectionProbe0 &&
1544 reflectionProbe1 == o.reflectionProbe1 &&
height == o.height &&
depth == o.depth &&
1545 sceneColor == o.sceneColor && decalAlbedo == o.decalAlbedo && decalNormal == o.decalNormal &&
1546 decalParams == o.decalParams && paletteSlot == o.paletteSlot && resourceProgram == o.resourceProgram;
1549 struct Mesh3dSetKeyHash {
1551 size_t operator()(
const Mesh3dSetKey &k)
const {
1552 return std::hash<GpuTexture *>()(k.albedo) ^ (std::hash<GpuTexture *>()(k.normal) << 1) ^
1553 (std::hash<GpuTexture *>()(k.env) << 2) ^ (std::hash<GpuTexture *>()(k.height) << 3) ^
1554 (std::hash<GpuTexture *>()(k.reflectionProbe0) << 8) ^
1555 (std::hash<GpuTexture *>()(k.reflectionProbe1) << 9) ^ (std::hash<GpuTexture *>()(k.depth) << 4) ^
1556 (std::hash<GpuTexture *>()(k.sceneColor) << 10) ^ (std::hash<GpuTexture *>()(k.decalAlbedo) << 5) ^
1557 (std::hash<GpuTexture *>()(k.decalNormal) << 6) ^ (std::hash<GpuTexture *>()(k.decalParams) << 7) ^
1558 k.paletteSlot ^ (size_t(k.resourceProgram) << 11);
1567 struct Mesh3dFrameSlots {
1568 vkb::GenericBuffer uboRing;
1569 vkb::GenericBuffer shadowRing;
1571 size_t drawIndex = 0;
1572 size_t lastDrawCount = 0;
1573 std::unordered_map<Mesh3dSetKey, vkb::BoundSet, Mesh3dSetKeyHash> sets;
1574 std::map<std::pair<size_t, GpuTexture*>, vkb::BoundSet> skinSets;
1575 std::vector<vkb::GenericBuffer> palettes;
1576 size_t activePalette = 0;
1578 std::vector<Mesh3dFrameSlots> mesh3dFrameSlots;
1579 Texture *whiteTexture =
nullptr;
1580 Texture *defaultExtrasArray =
nullptr;
1581 Texture *defaultColorsArray =
nullptr;
1582 Texture *defaultVertexArray =
nullptr;
1583 Texture *defaultMotionArray =
nullptr;
1584 Texture *defaultVegetationFadeNoise =
nullptr;
1586 Texture *defaultBindlessCube =
nullptr;
1587 Texture *flatNormalTexture3D =
nullptr;
1588 Texture *flatHeightTexture3D =
nullptr;
1589 Texture *defaultEnvCubemap =
nullptr;
1590 Texture *mesh3dNormalTexture =
nullptr;
1591 Texture *mesh3dHeightTexture =
nullptr;
1592 Texture *mesh3dEnvTexture =
nullptr;
1593 Texture *mesh3dSceneDepthTexture =
nullptr;
1594 Texture *mesh3dSceneColorTexture =
nullptr;
1595 float mesh3dEnvIntensity = 0.f;
1596 glm::vec3 mesh3dEnvProbeCenter{0.f};
1597 glm::vec3 mesh3dEnvProbeExtent{0.f};
1598 ReflectionProbeUpload mesh3dReflectionProbes{};
1599 float mesh3dMetallic = 0.f;
1600 float mesh3dRoughness = 0.45f;
1601 SurfaceMode mesh3dSurfaceMode = SurfaceMode::Opaque;
1602 BlendMode mesh3dSurfaceBlend = BlendMode::Alpha;
1603 bool mesh3dSurfaceDepthWrite =
false;
1604 bool mesh3dSurfaceDoubleSided =
false;
1605 float mesh3dAlphaCutoff = 0.5f;
1606 std::string mesh3dAlphaTechnique =
"cutoff";
1607 glm::vec4 mesh3dLodFade{1.f, 0.f, 0.f, 0.f};
1608 float mesh3dTexBombScale = 4.f;
1609 float mesh3dTexBombStrength = 0.f;
1610 float mesh3dTexBombRot = 1.f;
1611 float mesh3dParallaxScale = 0.f;
1612 float mesh3dParallaxMinLayers = 8.f;
1613 float mesh3dParallaxMaxLayers = 32.f;
1614 glm::vec4 mesh3dVirtualTexture{0.f};
1615 glm::vec4 mesh3dVirtualAtlas{0.f};
1616 Lighting3DPack mesh3dLighting{};
1617 ShadowUpload mesh3dShadows{};
1618 bool mesh3dShadowReceive =
true;
1619 int mesh3dSkinInfluenceLimit = 4;
1622 bool mesh3dClusteredActive =
false;
1623 ClusteredLightingUpload mesh3dClustered{};
1624 vk::DescriptorSetLayout mesh3dClusteredSetLayout;
1625 vk::UniqueDescriptorSetLayout mesh3dClusteredSetLayoutUnique;
1626 vk::PipelineLayout mesh3dClusteredPipelineLayout;
1627 vk::Pipeline mesh3dClusteredPipeline;
1631 struct ClusteredStorage {
1632 vkb::GenericBuffer lightsBuf;
1633 vkb::GenericBuffer tableBuf;
1634 vkb::GenericBuffer indicesBuf;
1635 size_t lightsCap = 0;
1636 size_t tableCap = 0;
1637 size_t indicesCap = 0;
1639 std::vector<ClusteredStorage> clusteredStorages;
1640 struct Mesh3dClusteredFrameSlots {
1641 vkb::GenericBuffer uboRing;
1642 vkb::GenericBuffer shadowRing;
1644 size_t drawIndex = 0;
1645 size_t lastDrawCount = 0;
1646 std::unordered_map<Mesh3dSetKey, vkb::BoundSet, Mesh3dSetKeyHash> sets;
1648 std::vector<Mesh3dClusteredFrameSlots> mesh3dClusteredFrameSlots;
1651 uint32_t mesh3dUboStride = 0;
1652 uint32_t shadowUboStride = 0;
1653 uint32_t mesh3dClusteredUboStride = 0;
1655 vk::Pipeline lastMesh3dPipeline =
nullptr;
1656 vk::Pipeline lastMesh3dClusteredPipeline =
nullptr;
1659 static constexpr uint32_t kAsyncResourceCopies = 2;
1662 GpuDrivenCaps gpuDrivenCaps_{};
1663 RayTracingCaps rayTracingCaps_{};
1664 std::vector<FrameArena> frameArenas_;
1666 vk::UniqueDescriptorSetLayout bindlessSetLayoutUnique_{};
1667 vk::DescriptorSetLayout bindlessSetLayout_ =
nullptr;
1668 vk::DescriptorPool bindlessPool_ =
nullptr;
1675 std::vector<vk::DescriptorSet> bindlessSets_;
1676 std::vector<GpuTexture *> bindlessTextures2D_;
1677 std::vector<uint32_t> bindlessFree2D_;
1678 std::vector<GpuTexture *> bindlessCubemaps_;
1679 std::vector<uint32_t> bindlessFreeCube_;
1681 vkb::GenericBuffer meshTableBuffer_;
1682 std::vector<GpuMeshRecord> meshTableRecords_;
1684 std::vector<GpuMesh *> meshRecordOwners_;
1685 uint32_t meshTableCapacity_ = 0;
1687 vkb::GenericBuffer materialTableBuffer_;
1688 std::vector<GpuMaterialRecord> materialTableRecords_;
1689 std::vector<uint32_t> materialTableFree_;
1690 std::unordered_map<const Material *, uint32_t> materialTableIndex_;
1691 uint32_t materialTableCapacity_ = 0;
1693 uint32_t registerBindlessTexture2D(GpuTexture *tex);
1694 uint32_t registerBindlessTextureCube(GpuTexture *tex);
1695 void unregisterBindlessTexture(GpuTexture *tex);
1696 uint32_t registerMeshRecord(GpuMesh *
gpu,
const std::vector<MeshVertex> *
vertices =
nullptr,
1697 const std::vector<uint32_t> *
indices =
nullptr);
1698 void syncMeshTable();
1700 void createBindlessSet();
1702 vk::DescriptorSet bindlessSetForFrame()
const;
1706 struct GpuVertexPool {
1709 vkb::GenericBuffer uvs;
1714 GpuVertexPool gpuVertexPool_;
1715 void ensureGpuVertexPool();
1716 void growGpuVertexPool(uint32_t needVertices, uint32_t needIndices);
1718 void bindGpuVertexPoolBindless();
1719 void appendGpuMeshToPool(GpuMesh &
gpu,
const std::vector<MeshVertex> *
vertices =
nullptr,
1720 const std::vector<uint32_t> *
indices =
nullptr);
1723 static constexpr uint32_t kMaxVgAssets = 64;
1724 static constexpr uint32_t kMaxVgClusters = 65536;
1725 struct VgGpuBuffers {
1728 vkb::GenericBuffer clusters;
1729 vkb::GenericBuffer clusterAssets;
1731 vkb::GenericBuffer indirect;
1732 vkb::GenericBuffer assetMaterials;
1733 vkb::GenericBuffer assetModels;
1735 VgGpuBuffers vgGpu_;
1736 uint32_t vgClusterCount_ = 0;
1737 uint32_t vgAssetCount_ = 0;
1738 uint32_t vgVertexCount_ = 0;
1739 uint32_t vgTriangleCount_ = 0;
1740 uint32_t vgLastVisible_ = 0;
1741 bool vgAnyThisFrame_ =
false;
1742 ComputePass vgCullPass_;
1743 vk::Pipeline gbufferVgVisPipeline =
nullptr;
1744 void ensureVgBuffers();
1745 void growVgBuffers(uint32_t needClusters, uint32_t needVertices, uint32_t needTriangles);
1747 void bindVgPoolBindless();
1749 void bindVgFrameBindless(vk::DescriptorSet bindless,
size_t slot);
1751 void recordVgCull();
1753 void drawVgClusters(vk::CommandBuffer cb);
1756 bool gpuDrivenEnabled_ =
false;
1757 uint32_t lastGpuDrivenDrawCount_ = 0;
1758 vk::PipelineLayout mesh3dGpuDrivenPipelineLayout =
nullptr;
1759 vk::Pipeline mesh3dGpuDrivenPipeline =
nullptr;
1760 vk::Pipeline resolveVisPipeline =
nullptr;
1762 vk::DescriptorSetLayout deferredLightingSetLayout{};
1763 vk::UniqueDescriptorSetLayout deferredLightingSetLayoutUnique;
1764 vk::PipelineLayout deferredLightingPipelineLayout{};
1765 vk::Pipeline deferredLightingPipeline{};
1768 struct DeferredLightingFrame {
1769 vkb::GenericBuffer ubo;
1770 vk::DescriptorSet set{};
1772 std::vector<DeferredLightingFrame> deferredLightingFrames;
1773 vk::Sampler deferredLightingSampler{};
1774 void createMesh3DGpuDrivenPipeline();
1776 struct GpuDrivenFrameSet0 {
1777 vk::DescriptorSet set;
1778 uint32_t uboOffset = 0;
1779 uint32_t shadowOffset = 0;
1781 GpuDrivenFrameSet0 gpuDrivenFrameSet0();
1782 void createResolveVisPipeline(
const vkb::BuiltRenderPass &rp,
1783 vk::SampleCountFlagBits samples);
1785 void initGpuDrivenResources();
1787 void createGpuDrivenVisResources(
int width,
int height);
1790 struct GpuDrivenCullSlot {
1791 vkb::GenericBuffer visibleFlags;
1792 vkb::GenericBuffer compacted;
1793 vkb::GenericBuffer indirect;
1794 vkb::GenericBuffer indirectNI;
1795 vkb::GenericBuffer bucketCounters;
1796 vkb::GenericBuffer hzb;
1797 vkb::GenericBuffer cullParams;
1799 std::vector<GpuDrivenCullSlot> gpuDrivenCullSlots_;
1800 vkb::GenericBuffer gpuDrivenCullParamsPlaceholder_;
1801 int gpuDrivenCullWidth = 0;
1802 int gpuDrivenCullHeight = 0;
1803 uint32_t gpuDrivenCullMaxMip = 0;
1804 vk::PipelineLayout gpuDrivenComputeLayout =
nullptr;
1805 vk::DescriptorSetLayout gpuDrivenComputeEmptyLayout_ =
nullptr;
1806 ComputePass hzbBuildPass_;
1807 ComputePass cullPass_;
1808 ComputePass emitPass_;
1809 bool gpuDrivenCullReady_ =
false;
1810 bool gpuDrivenScenePassPending_ =
false;
1812 std::vector<uint32_t> gpuDrivenBucketIds_;
1813 std::vector<uint32_t> gpuDrivenBucketOffsets_;
1814 std::vector<uint32_t> gpuDrivenBucketMeshIds_;
1815 uint32_t gpuDrivenBucketCount_ = 0;
1816 uint32_t gpuDrivenCullInstanceCount_ = 0;
1817 uint32_t gpuDrivenLastCullSlot_ = 0;
1818 FrameArena::Alloc gpuDrivenInstAlloc_{};
1819 FrameArena::Alloc gpuDrivenBucketIdAlloc_{};
1820 FrameArena::Alloc gpuDrivenBucketOffAlloc_{};
1821 void ensureGpuDrivenCullResources(
int width,
int height);
1822 void recordGpuDrivenHzbBuild();
1823 void gpuDrivenRecordComputeSection(
const glm::mat4 &
viewProj,
const glm::vec3 &
eye,
1824 float fovYDeg,
float nearZ,
float farZ);
1825 void destroyGpuDrivenCullResources();
1826 GpuDrivenCullSlot ¤tGpuDrivenCullSlot();
1827 GpuDrivenCullSlot &gpuDrivenCullSlot(uint32_t frameSlot);
1830 struct ShadowMapSlot {
1831 vkb::DepthArrayImage
image;
1832 vk::Framebuffer framebuffers[ShadowConfig::kTotalLayers]{};
1834 std::vector<ShadowMapSlot> shadowMaps;
1836 int allocatedShadowMapSize = 0;
1837 vkb::DepthSampler shadowSampler{};
1838 vk::Sampler shadowRawSampler{};
1839 vkb::BuiltRenderPass shadowRenderPass{};
1840 vk::PipelineLayout shadowPipelineLayout{};
1841 vk::Pipeline shadowPipeline{};
1842 vk::Pipeline shadowSingleSidedPipeline{};
1843 vk::PipelineLayout shadowAlphaPipelineLayout{};
1844 vk::Pipeline shadowAlphaPipeline{};
1845 vk::Pipeline shadowAlphaSingleSidedPipeline{};
1846 vk::PipelineLayout skinPassPipelineLayout{};
1847 vk::DescriptorSetLayout skinPassSetLayout{};
1848 vk::UniqueDescriptorSetLayout skinPassSetLayoutUnique;
1849 vk::Pipeline shadowSkinPipeline{};
1850 vk::Pipeline shadowSkinSingleSidedPipeline{};
1851 vk::Pipeline shadowSkinAlphaPipeline{};
1852 vk::Pipeline shadowSkinAlphaSingleSidedPipeline{};
1853 int shadowPassCascade = -1;
1858 bool alphaTest =
false;
1860 glm::vec4 lodFade{1.f, 0.f, 0.f, 0.f};
1861 vk::DescriptorSet skinSet{};
1862 uint32_t skinUboOffset = 0;
1864 struct ShadowAlphaPush {
1866 glm::vec4 lodFade{1.f, 0.f, 0.f, 0.f};
1868 std::vector<ShadowDraw> shadowPassDraws;
1869 std::vector<ShadowDraw> shadowCascadeDraws[ShadowConfig::kTotalLayers];
1870 uint32_t shadowPendingMask = 0;
1871 ShadowMapSlot ¤tShadowMap();
1872 vk::ImageView currentShadowArrayView();
1875 struct GBufferPush {
1878 glm::vec4 modelR0{1.f, 0.f, 0.f, 0.f};
1879 glm::vec4 modelR1{0.f, 1.f, 0.f, 0.f};
1880 glm::vec4 modelR2{0.f, 0.f, 1.f, 0.f};
1881 glm::vec4
clip{0.1f, 100.f, 0.f, 0.f};
1884 static vk::VertexInputBindingDescription getBindingDescription(uint32_t
binding) {
1885 return {
binding,
sizeof(GBufferPush), vk::VertexInputRate::eInstance};
1890 static std::vector<vk::VertexInputAttributeDescription> getAttributeDescription(uint32_t
binding) {
1891 std::vector<vk::VertexInputAttributeDescription>
attributes;
1893 for (uint32_t i = 0; i < 8; ++i)
1894 attributes.push_back({7u + i, binding, vk::Format::eR32G32B32A32Sfloat, i * 16u});
1898 static_assert(
sizeof(GBufferPush) == 128,
"GBuffer push constants must be 128 bytes");
1899 struct GBufferDraw {
1904 vk::Pipeline pbrPipeline{};
1905 vk::PipelineLayout pbrLayout{};
1906 vk::DescriptorSet pbrSet{};
1908 std::array<vk::DeviceSize, 11> pbrUvOffsets{};
1909 glm::vec4 pbrClip{};
1910 glm::vec2 pbrFactors{};
1911 bool alphaTest =
false;
1912 vk::DescriptorSet skinSet{};
1913 uint32_t skinUboOffset = 0;
1915 struct GBufferSlot {
1916 vkb::GenericBuffer instanceBuffer;
1917 size_t instanceCapacity = 0;
1919 vkb::ColorTarget depthColor;
1921 vkb::ColorTarget pbrParams;
1922 vkb::ColorTarget emissive;
1923 vkb::ColorTarget visID;
1924 vkb::ColorTarget visBary;
1925 vkb::DepthTarget
depth;
1926 vk::Framebuffer framebuffer{};
1927 vk::Framebuffer visFramebuffer{};
1928 GpuTexture normalGpu{};
1929 GpuTexture depthColorGpu{};
1930 GpuTexture albedoGpu{};
1931 GpuTexture pbrParamsGpu{};
1932 GpuTexture emissiveGpu{};
1933 GpuTexture visIDGpu{};
1934 GpuTexture visBaryGpu{};
1935 GpuTexture depthGpu{};
1945 int gbufferWidth = 0;
1946 int gbufferHeight = 0;
1947 std::vector<GBufferSlot> gbufferSlots;
1948 vkb::BuiltRenderPass gbufferRenderPass{};
1949 vkb::BuiltRenderPass gbufferVisRenderPass{};
1950 vk::PipelineLayout gbufferPipelineLayout{};
1951 vk::Pipeline gbufferPipeline{};
1952 vk::Pipeline gbufferInstancedPipeline{};
1953 vk::Pipeline gbufferAlphaPipeline{};
1954 vk::Pipeline gbufferSkinPipeline{};
1955 vk::Pipeline gbufferSkinAlphaPipeline{};
1956 vk::Pipeline gbufferVisPipeline =
nullptr;
1957 bool gbufferPassActive =
false;
1958 bool gbufferPending =
false;
1959 std::vector<GBufferDraw> gbufferPassDraws;
1960 struct GBufferInstancedBatch {
1964 vk::DeviceSize
offset = 0;
1966 std::vector<GBufferInstancedBatch> gbufferInstancedBatches;
1967 void prepareGBufferInstanceBatches();
1968 GBufferSlot *currentGBufferSlot();
1973 struct DecalInstanceData {
1974 glm::mat4
model{1.f};
1975 glm::vec4 uvRect{0.f, 0.f, 1.f, 1.f};
1976 glm::vec4 fadeParams{1.f, 0.f, 0.f, 0.f};
1977 glm::vec4 extraParams{0.f, 0.f, 0.f, 0.f};
1978 glm::vec4 surfaceParams{0.f, 8.f, 24.f, 0.06f};
1980 static_assert(
sizeof(DecalInstanceData) == 128,
"DecalInstanceData must be 128 bytes");
1981 struct DecalCameraUBO {
1984 glm::vec4 nearFarTexel{0.1f, 100.f, 0.f, 0.f};
1987 static constexpr size_t kMaxDecalInstances = 512;
1989 glm::mat4
model{1.f};
1993 float uvRect[4] = {0.f, 0.f, 1.f, 1.f};
1996 float roughnessStrength = 0.f;
1997 float metalStrength = 0.f;
1998 float emissiveStrength = 0.f;
2000 int projectionMode = 0;
2001 float blendSharpness = 4.f;
2002 float parallaxScale = 0.f;
2003 float parallaxMinLayers = 8.f;
2004 float parallaxMaxLayers = 24.f;
2005 float edgeFadeWidth = 0.06f;
2007 struct DecalSetKey {
2008 GpuTexture *
albedo =
nullptr;
2009 GpuTexture *
normal =
nullptr;
2010 GpuTexture *
params =
nullptr;
2011 GpuTexture *
depth =
nullptr;
2012 GpuTexture *gbNormal =
nullptr;
2014 bool operator==(
const DecalSetKey &o)
const {
2016 depth == o.depth && gbNormal == o.gbNormal;
2019 struct DecalSetKeyHash {
2021 size_t operator()(
const DecalSetKey &k)
const {
2022 return std::hash<GpuTexture *>()(k.albedo) ^
2023 (std::hash<GpuTexture *>()(k.normal) << 1) ^
2024 (std::hash<GpuTexture *>()(k.params) << 2) ^
2025 (std::hash<GpuTexture *>()(k.depth) << 3) ^
2026 (std::hash<GpuTexture *>()(k.gbNormal) << 4);
2033 vk::Framebuffer framebuffer{};
2034 GpuTexture albedoGpu{};
2035 GpuTexture normalGpu{};
2036 GpuTexture paramsGpu{};
2040 vkb::GenericBuffer cameraUbo;
2041 std::unordered_map<DecalSetKey, vkb::BoundSet, DecalSetKeyHash> sets;
2044 int decalHeight = 0;
2045 std::vector<DecalSlot> decalSlots;
2046 vkb::BuiltRenderPass decalRenderPass{};
2047 vk::DescriptorSetLayout decalSetLayout;
2048 vk::UniqueDescriptorSetLayout decalSetLayoutUnique;
2049 vk::PipelineLayout decalPipelineLayout{};
2050 vk::Pipeline decalPipeline{};
2051 bool decalPassActive =
false;
2052 bool decalPending =
false;
2055 bool decalLayerFresh =
false;
2056 std::vector<DecalDraw> decalPassDraws;
2057 vk::Pipeline lastDecalPipeline =
nullptr;
2058 Mesh *decalUnitBox =
nullptr;
2060 Texture *decalFlatAlbedo =
nullptr;
2061 Texture *decalFlatNormal =
nullptr;
2062 Texture *decalFlatParams =
nullptr;
2063 glm::mat4 decalViewProj{1.f};
2064 float decalNear = 0.1f;
2065 float decalFar = 100.f;
2075 std::array<std::array<std::unique_ptr<vkb::FrameGraph>, 2>, kAsyncResourceCopies> deferredFrameGraphs_;
2076 FrameGraphRecordingState deferredRecordingState_;
2079 std::array<std::vector<Canvas *>, 2> framePostTargets_;
2080 Canvas &prepareFramePostTarget(FramePostTarget
target,
int width,
int height)
override;
2081 void releaseFramePostTargets();
2082 vkb::FrameGraph *currentDeferredFrameGraph();
2084 bool deferredGraphRecorded_ =
false;
2085 size_t deferredGraphRecordedSlot_ = 0;
2087 void buildDeferredFrameGraphs();
2092 void resetDeferredFrameGraphs();
2094 void recordShadowCascadePass(vkb::FrameGraphPassContext &ctx,
int cascade);
2095 void recordShadowCascadeDraws(vk::CommandBuffer cb,
int cascade, uint32_t
size);
2097 void recordGBufferPassDraws(vkb::FrameGraphPassContext &ctx);
2099 struct SceneColorSlot {
2100 vkb::ColorTarget msaaColor;
2101 vkb::ColorTarget
color;
2102 vkb::DepthTarget
depth;
2103 vk::Framebuffer framebuffer{};
2104 GpuTexture colorGpu{};
2107 int sceneColorWidth = 0;
2108 int sceneColorHeight = 0;
2109 vk::Format sceneColorFormat = vk::Format::eUndefined;
2110 vk::SampleCountFlagBits sceneColorSamples = vk::SampleCountFlagBits::e1;
2111 std::vector<SceneColorSlot> sceneColorSlots;
2112 size_t completedSceneColorSlot = 0;
2113 bool sceneColorHistoryValid =
false;
2114 vkb::BuiltRenderPass sceneColorRenderPass{};
2115 vk::RenderPass sceneColorResumeRenderPass{};
2116 bool sceneColorPassOpen =
false;
2117 vk::RenderPass scenePassPipelineTarget = vk::RenderPass{};
2118 vk::SampleCountFlagBits scenePassPipelineSamples = vk::SampleCountFlagBits::e1;
2119 int appliedMsaa = -1;
2120 SceneColorSlot *currentSceneColorSlot();
2125 vkb::BuiltRenderPass offscreen3DRenderPass{};
2126 vkb::BuiltRenderPass hdrOffscreen3DRenderPass{};
2127 vk::Pipeline offscreen3DMeshPipeline =
nullptr;
2128 vk::Pipeline hdrOffscreen3DMeshPipeline =
nullptr;
2129 std::array<vk::Pipeline, kMesh3DPipelineVariants> offscreen3DSurfacePipelines{};
2130 std::array<vk::Pipeline, kMesh3DPipelineVariants> hdrOffscreen3DSurfacePipelines{};
2131 vk::UniqueDescriptorSetLayout reflectionProbeFilterSetLayout;
2132 vk::UniquePipelineLayout reflectionProbeFilterPipelineLayout;
2133 ComputePass reflectionProbeFilterPass;
2134 vk::CommandPool offscreen3DPool =
nullptr;
2135 vk::CommandBuffer offscreen3DCB =
nullptr;
2136 vk::Fence offscreen3DFence =
nullptr;
2137 vk::QueryPool offscreen3DTimestampQueryPool =
nullptr;
2138 float offscreen3DTimestampPeriodNs = 0.f;
2139 float lastOffscreen3DGpuDurationMs = 0.f;
2140 bool offscreen3DPassOpen =
false;
2141 bool offscreen3DHDRActive =
false;
2142 OffscreenCanvas *offscreen3DCanvas =
nullptr;
2146 struct UiColorSlot {
2147 vkb::ColorTarget msaaColor;
2148 vkb::ColorTarget
color;
2149 vk::Framebuffer framebuffer{};
2150 GpuTexture colorGpu{};
2153 int uiColorWidth = 0;
2154 int uiColorHeight = 0;
2155 vk::Format uiColorFormat = vk::Format::eUndefined;
2156 vk::SampleCountFlagBits uiColorSamples = vk::SampleCountFlagBits::e1;
2157 std::vector<UiColorSlot> uiColorSlots;
2158 vkb::BuiltRenderPass uiRenderPass{};
2159 vk::Pipeline uiTexturePipeline{};
2160 vk::Pipeline uiTextureOpaquePipeline{};
2161 UiColorSlot *currentUiColorSlot();
2166 struct PresentComposeSlot {
2167 vkb::ColorTarget
color;
2168 vk::Framebuffer framebuffer{};
2169 GpuTexture colorGpu{};
2172 int presentComposeWidth = 0;
2173 int presentComposeHeight = 0;
2174 std::vector<PresentComposeSlot> presentComposeSlots;
2176 vkb::Present presentModel;
2177 vkb::RecordingCmd presentRecording;
2178 uint64_t presentSequence_ = 0;
2179 uint64_t submittedPresentSequence_ = 0;
2180 vkb::InRenderPass swapchainPass;
2181 vkb::DepthStencilImage depthImage;
2182 vk::Format depthFormat = vk::Format::eD32Sfloat;
2185 void submitGpuTimingPrefix();
2186 std::array<vk::CommandBuffer, kAsyncResourceCopies> gpuTimingPrefix_{};
2187 std::array<bool, kAsyncResourceCopies> gpuTimingPrefixSubmitted_{};
2188 vk::QueryPool gpuQueryPool_{};
2189 float timestampPeriod_ = 1.f;
2190 bool gpuTimingReady_ =
false;
2191 float gpuFrameMs_ = 0.f;
2197 std::vector<SolidBatch> solidBatches;
2198 struct TexturedBatch {
2200 enum class Effect {
Default, SceneColorDistortion, DisplayEncode };
2211 std::vector<TexturedBatch> texturedBatches;
2213 std::vector<LitBatch> litBatches;
2215 enum class OverlayKind : uint8_t {
Solid, Textured,
Lit, GpuParticles };
2216 struct OverlaySpan {
2217 OverlayKind
kind = OverlayKind::Solid;
2219 uint32_t vertBegin = 0;
2220 uint32_t vertCount = 0;
2222 std::vector<OverlaySpan> overlaySpans;
2223 std::vector<OverlaySpan> engine3DSpans;
2224 std::optional<TexturedBatch> pendingSceneResolve;
2225 Texture *pendingSceneResolveSource =
nullptr;
2226 std::optional<TexturedBatch> pendingUiResolve;
2227 bool sceneColorComposited =
false;
2229 struct GpuParticleDrawRequest {
2231 GpuParticleDraw
draw;
2233 std::unordered_map<GpuParticleHandle, GpuParticleResource*> gpuParticles_;
2234 std::vector<GpuParticleDrawRequest> gpuParticleDraws_;
2236 vk::DescriptorSetLayout gpuParticleComputeSetLayout_{};
2237 vk::DescriptorSetLayout gpuParticleDrawSetLayout_{};
2238 vk::DescriptorSetLayout gpuParticleSortSetLayout_{};
2239 vk::PipelineLayout gpuParticleComputeLayout_{};
2240 vk::PipelineLayout gpuParticleDrawLayout_{};
2241 vk::PipelineLayout gpuParticleSortLayout_{};
2242 vk::Pipeline gpuParticleComputePipeline_{};
2243 vk::Pipeline gpuParticleSortPipeline_{};
2244 vk::Pipeline gpuParticleAlphaPipeline_{};
2245 vk::Pipeline gpuParticleAdditivePipeline_{};
2246 vk::Pipeline gpuParticlePremultipliedPipeline_{};
2247 vk::Pipeline gpuParticleMultiplyPipeline_{};
2248 vk::Pipeline gpuParticleOpaquePipeline_{};
2252 struct Frame2DBuffers {
2253 std::vector<vkb::HostVertexBuffer> solidBufs;
2254 std::vector<vkb::HostVertexBuffer> texBufs;
2255 std::vector<vkb::HostVertexBuffer> uiTexBufs;
2256 std::vector<vkb::HostVertexBuffer> primitive3DBufs;
2257 std::size_t primitive3DDrawIndex = 0;
2259 std::vector<Frame2DBuffers> frame2dBuffers;
2260 Frame2DBuffers offscreenBuffers;
2262 vk::DescriptorSetLayout lit2dSetLayout;
2263 vk::UniqueDescriptorSetLayout lit2dSetLayoutUnique;
2264 vk::PipelineLayout lit2dPipelineLayout;
2265 vk::Pipeline lit2dPipeline;
2266 vk::Pipeline lit2dAdditivePipeline;
2267 vk::Pipeline lit2dPremultipliedPipeline;
2268 vk::Pipeline lit2dMultiplyPipeline;
2269 vk::Pipeline lit2dOpaquePipeline;
2270 vk::Pipeline offscreenLitPipeline;
2271 vk::Pipeline offscreenLitAdditivePipeline;
2272 vk::Pipeline offscreenLitPremultipliedPipeline;
2273 vk::Pipeline offscreenLitMultiplyPipeline;
2274 vk::Pipeline offscreenLitOpaquePipeline;
2276 vk::Pipeline selectLit2DPipeline(BlendMode
blend,
bool offscreen,
bool hdr =
false)
const;
2277 std::vector<vkb::GenericBuffer> lighting2dUboSlots;
2278 vkb::GenericBuffer offscreenLighting2dUbo;
2279 Lighting2DUBO lighting2dFrame{};
2281 GpuTexture *
albedo =
nullptr;
2282 GpuTexture *
normal =
nullptr;
2284 bool operator==(
const LitSetKey &o)
const {
2288 struct LitSetKeyHash {
2290 size_t operator()(
const LitSetKey &k)
const {
2291 return std::hash<void *>()(k.albedo) ^ (std::hash<void *>()(k.normal) << 1);
2295 GpuTexture *
color =
nullptr;
2296 GpuTexture *
depth =
nullptr;
2297 GpuTexture *motion =
nullptr;
2298 GpuTexture *extra =
nullptr;
2299 GpuTexture *specular =
nullptr;
2301 bool operator==(
const PostSetKey &o)
const {
2302 return color == o.color &&
depth == o.depth && motion == o.motion && extra == o.extra &&
2303 specular == o.specular;
2306 struct PostSetKeyHash {
2308 size_t operator()(
const PostSetKey &k)
const {
2309 return std::hash<void *>()(k.color) ^ (std::hash<void *>()(k.depth) << 1) ^
2310 (std::hash<void *>()(k.motion) << 2) ^
2311 (std::hash<void *>()(k.extra) << 3) ^
2312 (std::hash<void *>()(k.specular) << 4);
2315 std::vector<std::unordered_map<LitSetKey, vkb::BoundSet, LitSetKeyHash>> lit2dSets;
2316 std::unordered_map<LitSetKey, vkb::BoundSet, LitSetKeyHash> offscreenLit2dSets;
2317 std::unordered_map<PostSetKey, vkb::BoundSet, PostSetKeyHash> post2Sets;
2318 Texture *flatNormalTexture =
nullptr;
2321 bool hasPendingClear =
true;
2323 std::vector<std::unique_ptr<Texture>> ownedTextures;
2324 std::vector<std::unique_ptr<GpuTexture>> ownedGpuTextures;
2326 std::unordered_map<std::string, Texture *> texturesByPath;
2327 std::unordered_map<std::string, Texture *> sharedTexturesByContent;
2328 std::vector<std::unique_ptr<Mesh>> ownedMeshes;
2329 std::vector<std::unique_ptr<GpuMesh>> ownedGpuMeshes;
2330 std::vector<std::unique_ptr<Shader>> ownedShaders;
2331 std::vector<std::unique_ptr<GpuShader>> ownedGpuShaders;
2332 std::vector<std::unique_ptr<eve::graphics::Canvas>> ownedCanvases;
2334 bool swapchainPassOpen =
false;
2335 bool flushingSwapchain_ =
false;
2336 Mesh3DUBO mesh3dFrameUbo{};
2339 struct VoxelRectPC {
2341 glm::vec4 chunkOrigin{0.f};
2342 glm::vec4 atlasInfo{16.f, 0.f, 0.f, 0.f};
2343 glm::vec4
tint{1.f};
2345 vk::DescriptorSetLayout voxelRectSetLayout{};
2346 vk::UniqueDescriptorSetLayout voxelRectSetLayoutUnique;
2347 vk::PipelineLayout voxelRectPipelineLayout{};
2348 vk::Pipeline voxelRectPipeline{};
2349 vkb::GenericBuffer voxelUnitQuadVerts;
2350 vkb::GenericBuffer voxelUnitQuadIndices;
2351 bool voxelUnitQuadReady =
false;
2352 std::unordered_map<GpuTexture *, vkb::BoundSet> voxelRectSets;
2355 struct VoxelInstanceSlot {
2356 vkb::GenericBuffer
buffer;
2357 size_t capacityBytes = 0;
2358 vkb::GenericBuffer aoBuffer;
2359 size_t aoCapacityBytes = 0;
2361 struct VoxelInstanceFrame {
2362 std::vector<VoxelInstanceSlot>
slots;
2363 size_t drawIndex = 0;
2365 std::vector<VoxelInstanceFrame> voxelInstanceFrames;
2367 Frame2DBuffers ¤tFrame2DBuffers();
2368 Mesh3dFrameSlots ¤tMesh3dFrameSlots();
2369 Mesh3dClusteredFrameSlots ¤tMesh3dClusteredFrameSlots();
2370 vkb::GenericBuffer ¤tLighting2dUbo();
2371 std::unordered_map<LitSetKey, vkb::BoundSet, LitSetKeyHash> ¤tLit2dSets();
2372 ClusteredStorage ¤tClusteredStorage();
2373 VoxelInstanceFrame ¤tVoxelInstanceFrame();
2374 uint32_t frameSlotCount()
const;
2375 size_t currentFrameSlot()
const;
2376 vk::CommandBuffer ¤tPresentCb();
2377 vkb::FrameSlot frameToken()
const;
2378 void invalidateTextureBindings();
2380 struct InspectionService;
2381 std::shared_ptr<InspectionService> inspectionService_;
2382 void pollEntityInspection();
2383 void destroyEntityInspection();
2385 struct GuideCaptureService;
2386 std::shared_ptr<GuideCaptureService> guideCaptureService_;
2387 void pollGuideCapture();
2388 void destroyGuideCapture();