-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathrtx_scene_manager.cpp
1531 lines (1272 loc) · 72.7 KB
/
rtx_scene_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <mutex>
#include <vector>
#include "rtx_asset_replacer.h"
#include "rtx_scene_manager.h"
#include "rtx_opacity_micromap_manager.h"
#include "dxvk_device.h"
#include "dxvk_context.h"
#include "dxvk_buffer.h"
#include "rtx_context.h"
#include "rtx_options.h"
#include "rtx_terrain_baker.h"
#include "rtx_texture_manager.h"
#include <assert.h>
#include "../d3d9/d3d9_state.h"
#include "vulkan/vulkan_core.h"
#include "rtx_game_capturer.h"
#include "rtx_matrix_helpers.h"
#include "rtx_intersection_test.h"
#include "dxvk_scoped_annotation.h"
#include "rtx_lights_data.h"
#include "rtx_light_utils.h"
namespace dxvk {
SceneManager::SceneManager(DxvkDevice* device)
: CommonDeviceObject(device)
, m_instanceManager(device, this)
, m_accelManager(device)
, m_lightManager(device)
, m_rayPortalManager(device, this)
, m_drawCallCache(device)
, m_bindlessResourceManager(device)
, m_volumeManager(device)
, m_pReplacer(new AssetReplacer())
, m_terrainBaker(new TerrainBaker())
, m_cameraManager(device)
, m_startTime(std::chrono::steady_clock::now())
, m_uniqueObjectSearchDistance(RtxOptions::uniqueObjectDistance()) {
InstanceEventHandler instanceEvents(this);
instanceEvents.onInstanceAddedCallback = [this](const RtInstance& instance) { onInstanceAdded(instance); };
instanceEvents.onInstanceUpdatedCallback = [this](RtInstance& instance, const RtSurfaceMaterial& material, bool hasTransformChanged, bool hasVerticesChanged) { onInstanceUpdated(instance, material, hasTransformChanged, hasVerticesChanged); };
instanceEvents.onInstanceDestroyedCallback = [this](const RtInstance& instance) { onInstanceDestroyed(instance); };
m_instanceManager.addEventHandler(instanceEvents);
if (env::getEnvVar("DXVK_RTX_CAPTURE_ENABLE_ON_FRAME") != "") {
m_beginUsdExportFrameNum = stoul(env::getEnvVar("DXVK_RTX_CAPTURE_ENABLE_ON_FRAME"));
}
if (env::getEnvVar("DXVK_DENOISER_NRD_FRAME_TIME_MS") != "") {
m_useFixedFrameTime = true;
}
}
SceneManager::~SceneManager() {
}
bool SceneManager::areReplacementsLoaded() const {
return m_pReplacer->areReplacementsLoaded();
}
bool SceneManager::areReplacementsLoading() const {
return m_pReplacer->areReplacementsLoading();
}
const std::string SceneManager::getReplacementStatus() const {
return m_pReplacer->getReplacementStatus();
}
// Returns wall time between start of app and current time.
uint64_t SceneManager::getGameTimeSinceStartMS() {
// Used in testing
if (m_useFixedFrameTime) {
const double deltaTimeMS = 1000.0 / 60.0; // Assume 60 fps
return static_cast<uint64_t>(static_cast<double>(m_device->getCurrentFrameId()) * deltaTimeMS);
}
// TODO(TREX-1004) find a way to 'pause' this when a game is paused.
// Note: steady_clock used here rather than system_clock as on Windows at least it uses a higher precision time source
// (QueryPerformanceCounter rather than GetSystemTimePreciseAsFileTime), and additionally it is monotonic which is better
// for this sort of game-based timekeeping (we don't care about NTP adjustments or other things that'd cause discontinuities).
const auto currTime = std::chrono::steady_clock::now();
const auto elapsedMs = std::chrono::duration_cast<std::chrono::milliseconds>(currTime - m_startTime);
return elapsedMs.count();
}
void SceneManager::initialize(Rc<DxvkContext> ctx) {
ScopedCpuProfileZone();
m_pReplacer->initialize(ctx);
auto& textureManager = m_device->getCommon()->getTextureManager();
textureManager.initialize(ctx);
}
void SceneManager::logStatistics() {
if (m_opacityMicromapManager.get()) {
m_opacityMicromapManager->logStatistics();
}
}
Vector3 SceneManager::getSceneUp() {
return RtxOptions::Get()->zUp() ? Vector3(0.f, 0.f, 1.f) : Vector3(0.f, 1.f, 0.f);
}
Vector3 SceneManager::getSceneForward() {
return RtxOptions::Get()->zUp() ? Vector3(0.f, 1.f, 0.f) : Vector3(0.f, 0.f, 1.f);
}
Vector3 SceneManager::calculateSceneRight() {
const Vector3 up = SceneManager::getSceneUp();
const Vector3 forward = SceneManager::getSceneForward();
return RtxOptions::Get()->isLeftHandedCoordinateSystem() ? cross(up, forward) : cross(forward, up);
}
Vector3 SceneManager::worldToSceneOrientedVector(const Vector3& worldVector) {
return RtxOptions::Get()->zUp() ? worldVector : Vector3(worldVector.x, worldVector.z, worldVector.y);
}
Vector3 SceneManager::sceneToWorldOrientedVector(const Vector3& sceneVector) {
// Same transform applies to and from
return worldToSceneOrientedVector(sceneVector);
}
float SceneManager::getTotalMipBias() {
auto& resourceManager = m_device->getCommon()->getResources();
const bool temporalUpscaling = RtxOptions::Get()->isDLSSOrRayReconstructionEnabled() || RtxOptions::Get()->isTAAEnabled();
float totalUpscaleMipBias = temporalUpscaling ? (log2(resourceManager.getUpscaleRatio()) + RtxOptions::Get()->upscalingMipBias()) : 0.0f;
return totalUpscaleMipBias + RtxOptions::Get()->getNativeMipBias();
}
void SceneManager::clear(Rc<DxvkContext> ctx, bool needWfi) {
ScopedCpuProfileZone();
auto& textureManager = m_device->getCommon()->getTextureManager();
// Only clear once after the scene disappears, to avoid adding a WFI on every frame through clear().
if (needWfi) {
if (ctx.ptr())
ctx->flushCommandList();
textureManager.synchronize(true);
m_device->waitForIdle();
}
// We still need to clear caches even if the scene wasn't rendered
m_bufferCache.clear();
m_surfaceMaterialCache.clear();
m_surfaceMaterialExtensionCache.clear();
m_volumeMaterialCache.clear();
// Called before instance manager's clear, so that it resets all tracked instances in Opacity Micromap manager at once
if (m_opacityMicromapManager.get())
m_opacityMicromapManager->clear();
m_instanceManager.clear();
m_lightManager.clear();
m_rayPortalManager.clear();
m_drawCallCache.clear();
textureManager.clear();
m_previousFrameSceneAvailable = false;
}
void SceneManager::garbageCollection() {
ScopedCpuProfileZone();
const size_t oldestFrame = m_device->getCurrentFrameId() - RtxOptions::Get()->numFramesToKeepGeometryData();
auto blasEntryGarbageCollection = [&](auto& iter, auto& entries) -> void {
if (iter->second.frameLastTouched < oldestFrame) {
onSceneObjectDestroyed(iter->second);
iter = entries.erase(iter);
} else {
++iter;
}
};
// Garbage collection for BLAS/Scene objects
//
// When anti-culling is enabled, we need to check if any instances are outside frustum. Because in such
// case the life of the instances will be extended and we need to keep the BLAS as well.
if (!RtxOptions::AntiCulling::Object::enable()) {
auto& entries = m_drawCallCache.getEntries();
if (m_device->getCurrentFrameId() > RtxOptions::Get()->numFramesToKeepGeometryData()) {
for (auto iter = entries.begin(); iter != entries.end(); ) {
blasEntryGarbageCollection(iter, entries);
}
}
}
else { // Implement anti-culling BLAS/Scene object GC
fast_unordered_cache<const RtInstance*> outsideFrustumInstancesCache;
auto& entries = m_drawCallCache.getEntries();
for (auto iter = entries.begin(); iter != entries.end();) {
bool isAllInstancesInCurrentBlasInsideFrustum = true;
for (const RtInstance* instance : iter->second.getLinkedInstances()) {
const Matrix4 objectToView = getCamera().getWorldToView(false) * instance->getTransform();
bool isInsideFrustum = true;
if (RtxOptions::Get()->needsMeshBoundingBox()) {
const AxisAlignedBoundingBox& boundingBox = instance->getBlas()->input.getGeometryData().boundingBox;
if (RtxOptions::AntiCulling::Object::enableHighPrecisionAntiCulling()) {
isInsideFrustum = boundingBoxIntersectsFrustumSAT(
getCamera(),
boundingBox.minPos,
boundingBox.maxPos,
objectToView,
RtxOptions::AntiCulling::Object::enableInfinityFarFrustum());
} else {
isInsideFrustum = boundingBoxIntersectsFrustum(getCamera().getFrustum(), boundingBox.minPos, boundingBox.maxPos, objectToView);
}
}
else {
// Fallback to check object center under view space
isInsideFrustum = getCamera().getFrustum().CheckSphere(float3(objectToView[3][0], objectToView[3][1], objectToView[3][2]), 0);
}
// Only GC the objects inside the frustum to anti-frustum culling, this could cause significant performance impact
// For the objects which can't be handled well with this algorithm, we will need game specific hash to force keeping them
if (isInsideFrustum && !instance->testCategoryFlags(InstanceCategories::IgnoreAntiCulling)) {
instance->markAsInsideFrustum();
} else {
instance->markAsOutsideFrustum();
isAllInstancesInCurrentBlasInsideFrustum = false;
// Anti-Culling GC extension:
// Eliminate duplicated instances that are outside of the game frustum.
// This is used to handle cases:
// 1. The game frustum is different to our frustum
// 2. The game culling method is NOT frustum culling
const XXH64_hash_t antiCullingHash = instance->calculateAntiCullingHash();
auto it = outsideFrustumInstancesCache.find(antiCullingHash);
if (it == outsideFrustumInstancesCache.end()) {
// No duplication, just cache the current instance
outsideFrustumInstancesCache[antiCullingHash] = instance;
} else {
const RtInstance* cachedInstance = it->second;
if (instance->getId() != cachedInstance->getId()) {
// Only keep the instance that is latest updated
if (instance->getFrameLastUpdated() < cachedInstance->getFrameLastUpdated()) {
instance->markAsInsideFrustum();
} else {
cachedInstance->markAsInsideFrustum();
it->second = instance;
}
}
}
}
}
// If all instances in current BLAS are inside the frustum, then use original GC logic to recycle BLAS Objects
if (isAllInstancesInCurrentBlasInsideFrustum &&
m_device->getCurrentFrameId() > RtxOptions::Get()->numFramesToKeepGeometryData()) {
blasEntryGarbageCollection(iter, entries);
} else { // If any instances are outside of the frustum in current BLAS, we need to keep the entity
++iter;
}
}
}
// Perform GC on the other managers
auto& textureManager = m_device->getCommon()->getTextureManager();
textureManager.garbageCollection();
m_instanceManager.garbageCollection();
m_accelManager.garbageCollection();
m_lightManager.garbageCollection(getCamera());
m_rayPortalManager.garbageCollection();
}
void SceneManager::onDestroy() {
m_accelManager.onDestroy();
if (m_opacityMicromapManager) {
m_opacityMicromapManager->onDestroy();
}
}
template<bool isNew>
SceneManager::ObjectCacheState SceneManager::processGeometryInfo(Rc<DxvkContext> ctx, const DrawCallState& drawCallState, RaytraceGeometry& inOutGeometry) {
ScopedCpuProfileZone();
ObjectCacheState result = ObjectCacheState::KBuildBVH;
const RasterGeometry& input = drawCallState.getGeometryData();
// Determine the optimal object state for this geometry
if (!isNew) {
// This is a geometry we've seen before, that requires updating
// 'inOutGeometry' has valid historical data
if (input.hashes[HashComponents::Indices] == inOutGeometry.hashes[HashComponents::Indices]) {
// Check if the vertex positions have changed, requiring a BVH refit
if (input.hashes[HashComponents::VertexPosition] == inOutGeometry.hashes[HashComponents::VertexPosition]
&& input.hashes[HashComponents::VertexShader] == inOutGeometry.hashes[HashComponents::VertexShader]
&& drawCallState.getSkinningState().boneHash == inOutGeometry.lastBoneHash) {
result = ObjectCacheState::kUpdateInstance;
} else {
result = ObjectCacheState::kUpdateBVH;
}
}
}
// Copy the input directly to the output as a starting point for our modified geometry data
RaytraceGeometry output = inOutGeometry;
output.lastBoneHash = drawCallState.getSkinningState().boneHash;
// Update draw parameters
output.cullMode = input.cullMode;
output.frontFace = input.frontFace;
// Copy the hashes over
output.hashes = input.hashes;
if (!input.positionBuffer.defined()) {
ONCE(Logger::err("processGeometryInfo: no position data on input detected"));
return ObjectCacheState::kInvalid;
}
if (input.vertexCount == 0) {
ONCE(Logger::err("processGeometryInfo: input data is violating some assumptions"));
return ObjectCacheState::kInvalid;
}
// Set to 1 if inspection of the GeometryData structures contents on CPU is desired
#define DEBUG_GEOMETRY_MEMORY 0
constexpr VkMemoryPropertyFlags memoryProperty = DEBUG_GEOMETRY_MEMORY ? (VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
// Assume we won't need this, and update the value if required
output.previousPositionBuffer = RaytraceBuffer();
const size_t vertexStride = (input.isVertexDataInterleaved() && input.areFormatsGpuFriendly()) ? input.positionBuffer.stride() : RtxGeometryUtils::computeOptimalVertexStride(input);
switch (result) {
case ObjectCacheState::KBuildBVH: {
// Set up the ideal vertex params, if input vertices are interleaved, it's safe to assume the positionBuffer stride is the vertex stride
output.vertexCount = input.vertexCount;
const size_t vertexBufferSize = output.vertexCount * vertexStride;
// Set up the ideal index params
output.indexCount = input.isTopologyRaytraceReady() ? input.indexCount : RtxGeometryUtils::getOptimalTriangleListSize(input);
const VkIndexType indexBufferType = input.isTopologyRaytraceReady() ? input.indexBuffer.indexType() : RtxGeometryUtils::getOptimalIndexFormat(output.vertexCount);
const size_t indexStride = (indexBufferType == VK_INDEX_TYPE_UINT16) ? 2 : 4;
// Make sure we're not stomping something else...
assert(output.indexCacheBuffer == nullptr && output.historyBuffer[0] == nullptr);
// Create a index buffer and vertex buffer we can use for raytracing.
DxvkBufferCreateInfo info = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
info.usage = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR;
info.stages = VK_PIPELINE_STAGE_TRANSFER_BIT | VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR;
info.access = VK_ACCESS_TRANSFER_WRITE_BIT;
info.size = align(output.indexCount * indexStride, CACHE_LINE_SIZE);
output.indexCacheBuffer = m_device->createBuffer(info, memoryProperty, DxvkMemoryStats::Category::RTXAccelerationStructure);
if (!RtxGeometryUtils::cacheIndexDataOnGPU(ctx, input, output)) {
ONCE(Logger::err("processGeometryInfo: failed to cache index data on GPU"));
return ObjectCacheState::kInvalid;
}
output.indexBuffer = RaytraceBuffer(DxvkBufferSlice(output.indexCacheBuffer), 0, indexStride, indexBufferType);
info.size = align(vertexBufferSize, CACHE_LINE_SIZE);
output.historyBuffer[0] = m_device->createBuffer(info, memoryProperty, DxvkMemoryStats::Category::RTXAccelerationStructure);
RtxGeometryUtils::cacheVertexDataOnGPU(ctx, input, output);
break;
}
case ObjectCacheState::kUpdateBVH: {
bool invalidateHistory = false;
// Stride changed, so we must recreate the previous buffer and use identical data
if (output.historyBuffer[0]->info().size != align(vertexStride * input.vertexCount, CACHE_LINE_SIZE)) {
auto desc = output.historyBuffer[0]->info();
desc.size = align(vertexStride * input.vertexCount, CACHE_LINE_SIZE);
output.historyBuffer[0] = m_device->createBuffer(desc, memoryProperty, DxvkMemoryStats::Category::RTXAccelerationStructure);
// Invalidate the current buffer
output.historyBuffer[1] = nullptr;
// Mark this object for realignment
invalidateHistory = true;
}
// Use the previous updates vertex data for previous position lookup
std::swap(output.historyBuffer[0], output.historyBuffer[1]);
if (output.historyBuffer[0].ptr() == nullptr) {
// First frame this object has been dynamic need to allocate a 2nd frame of data to preserve history.
output.historyBuffer[0] = m_device->createBuffer(output.historyBuffer[1]->info(), memoryProperty, DxvkMemoryStats::Category::RTXAccelerationStructure);
}
RtxGeometryUtils::cacheVertexDataOnGPU(ctx, input, output);
// Sometimes, we need to invalidate history, do that here by copying the current buffer to the previous..
if (invalidateHistory) {
ctx->copyBuffer(output.historyBuffer[1], 0, output.historyBuffer[0], 0, output.historyBuffer[1]->info().size);
}
// Assign the previous buffer using the last slice (copy most params from the position, just change buffer)
output.previousPositionBuffer = RaytraceBuffer(DxvkBufferSlice(output.historyBuffer[1], 0, output.positionBuffer.length()), output.positionBuffer.offsetFromSlice(), output.positionBuffer.stride(), output.positionBuffer.vertexFormat());
break;
}
}
// Update color buffer in BVH with DrawCallState
// The user can disable/enable color buffer for specific materials, so we manually sync the DrawCallState and BVH here to keep the color buffer in BVH updated.
// Note, we don't setup kUpdateBVH because it's too waste to update all buffers if only the color buffer needs to be updated.
if (output.color0Buffer.defined() && !drawCallState.geometryData.color0Buffer.defined()) {
// Remove the color buffer in BVH if the color buffer from drawcall is removed by ignoreBakedLighting
output.color0Buffer = RaytraceBuffer();
} else if (!output.color0Buffer.defined() && drawCallState.geometryData.color0Buffer.defined()) {
// Write the color buffer back to BVH if the color buffer is enabled again
const DxvkBufferSlice slice = DxvkBufferSlice(output.historyBuffer[0]);
const auto& colorBuffer = drawCallState.geometryData.color0Buffer;
output.color0Buffer = RaytraceBuffer(slice, colorBuffer.offsetFromSlice(), colorBuffer.stride(), colorBuffer.vertexFormat());
}
// Update buffers in the cache
updateBufferCache(output);
// Finalize our modified geometry data to the output
inOutGeometry = output;
return result;
}
void SceneManager::onFrameEnd(Rc<DxvkContext> ctx) {
ScopedCpuProfileZone();
if (m_enqueueDelayedClear) {
clear(ctx, true);
m_enqueueDelayedClear = false;
}
m_cameraManager.onFrameEnd();
m_instanceManager.onFrameEnd();
m_previousFrameSceneAvailable = true;
m_bufferCache.clear();
{
std::lock_guard lock { m_drawCallMeta.mutex };
const uint8_t curTick = m_drawCallMeta.ticker;
const uint8_t nextTick = (m_drawCallMeta.ticker + 1) % m_drawCallMeta.MaxTicks;
m_drawCallMeta.ready[curTick] = true;
m_drawCallMeta.infos[nextTick].clear();
m_drawCallMeta.ready[nextTick] = false;
m_drawCallMeta.ticker = nextTick;
}
m_terrainBaker->onFrameEnd(ctx);
if (m_opacityMicromapManager) {
m_opacityMicromapManager->onFrameEnd();
}
m_activePOMCount = 0;
if (m_uniqueObjectSearchDistance != RtxOptions::uniqueObjectDistance()) {
m_uniqueObjectSearchDistance = RtxOptions::uniqueObjectDistance();
m_drawCallCache.rebuildSpatialMaps();
}
}
void SceneManager::onFrameEndNoRTX() {
m_cameraManager.onFrameEnd();
}
std::unordered_set<XXH64_hash_t> uniqueHashes;
void SceneManager::submitDrawState(Rc<DxvkContext> ctx, const DrawCallState& input, const MaterialData* overrideMaterialData) {
ScopedCpuProfileZone();
if (m_bufferCache.getTotalCount() >= kBufferCacheLimit && m_bufferCache.getActiveCount() >= kBufferCacheLimit) {
ONCE(Logger::info("[RTX-Compatibility-Info] This application is pushing more unique buffers than is currently supported - some objects may not raytrace."));
return;
}
if (m_fog.mode == D3DFOG_NONE && input.getFogState().mode != D3DFOG_NONE) {
m_fog = input.getFogState();
}
// Get Material and Mesh replacements
// NOTE: Next refactor we move this into a material manager
std::optional<MaterialData> replacementMaterial {};
if (overrideMaterialData == nullptr) {
MaterialData* pReplacementMaterial = m_pReplacer->getReplacementMaterial(input.getMaterialData().getHash());
if (pReplacementMaterial != nullptr) {
// Make a copy
replacementMaterial.emplace(MaterialData(*pReplacementMaterial));
// merge in the input material from game
replacementMaterial->mergeLegacyMaterial(input.getMaterialData());
// bind as a material override for this draw
overrideMaterialData = &replacementMaterial.value();
}
}
const XXH64_hash_t activeReplacementHash = input.getHash(RtxOptions::Get()->GeometryAssetHashRule);
std::vector<AssetReplacement>* pReplacements = m_pReplacer->getReplacementsForMesh(activeReplacementHash);
// TODO (REMIX-656): Remove this once we can transition content to new hash
if ((RtxOptions::Get()->GeometryHashGenerationRule & rules::LegacyAssetHash0) == rules::LegacyAssetHash0) {
if (!pReplacements) {
const XXH64_hash_t legacyHash = input.getHashLegacy(rules::LegacyAssetHash0);
pReplacements = m_pReplacer->getReplacementsForMesh(legacyHash);
if (RtxOptions::Get()->logLegacyHashReplacementMatches() && pReplacements && uniqueHashes.find(legacyHash) == uniqueHashes.end()) {
uniqueHashes.insert(legacyHash);
Logger::info(str::format("[Legacy-Hash-Replacement] Found a mesh referenced from legacyHash0: ", std::hex, legacyHash, ", new hash: ", std::hex, activeReplacementHash));
}
}
}
if ((RtxOptions::Get()->GeometryHashGenerationRule & rules::LegacyAssetHash1) == rules::LegacyAssetHash1) {
if (!pReplacements) {
const XXH64_hash_t legacyHash = input.getHashLegacy(rules::LegacyAssetHash1);
pReplacements = m_pReplacer->getReplacementsForMesh(legacyHash);
if (RtxOptions::Get()->logLegacyHashReplacementMatches() && pReplacements && uniqueHashes.find(legacyHash) == uniqueHashes.end()) {
uniqueHashes.insert(legacyHash);
Logger::info(str::format("[Legacy-Hash-Replacement] Found a mesh referenced from legacyHash1: ", std::hex, legacyHash, ", new hash: ", std::hex, activeReplacementHash));
}
}
}
// Check if a Ray Portal override is needed
std::optional<MaterialData> rayPortalMaterialData {};
size_t rayPortalTextureIndex;
if (RtxOptions::Get()->getRayPortalTextureIndex(input.getMaterialData().getHash(), rayPortalTextureIndex)) {
assert(rayPortalTextureIndex < maxRayPortalCount);
assert(rayPortalTextureIndex < std::numeric_limits<uint8_t>::max());
// Mask texture is required for Portal
const bool materialHasMaskTexture = input.getMaterialData().getColorTexture2().isValid();
if (materialHasMaskTexture) {
const TextureRef& texture2 = input.getMaterialData().getColorTexture2();
if (overrideMaterialData == nullptr) {
// Note: Color texture used as mask texture for the Ray Portal
rayPortalMaterialData.emplace(RayPortalMaterialData { input.getMaterialData().getColorTexture(), texture2, static_cast<uint8_t>(rayPortalTextureIndex), 1, 1, 0, 0.f,true, 1.f, lss::Mdl::Filter::Linear, lss::Mdl::WrapMode::Repeat, lss::Mdl::WrapMode::Repeat });
// Note: A bit dirty but since we use a pointer to the material data in processDrawCallState, we need a pointer to this locally created one on the
// stack in a place that doesn't go out of scope without actually allocating any heap memory.
overrideMaterialData = &*rayPortalMaterialData;
}
}
}
// Detect meshes that would have unstable hashes due to the vertex hash using vertex data from a shared vertex buffer.
// TODO: Once the vertex hash only uses vertices referenced by the index buffer, this should be removed.
const bool highlightUnsafeAnchor = RtxOptions::Get()->getHighlightUnsafeAnchorModeEnabled() &&
input.getGeometryData().indexBuffer.defined() && input.getGeometryData().vertexCount > input.getGeometryData().indexCount;
if (highlightUnsafeAnchor) {
static MaterialData sHighlightMaterialData(OpaqueMaterialData(TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(),
0.f, 1.f, Vector3(0.2f, 0.2f, 0.2f), 1.0f, 0.1f, 0.1f, Vector3(0.46f, 0.26f, 0.31f), true, 1, 1, 0, false, false, 200.f, true, false, BlendType::kAlpha, false, AlphaTestType::kAlways, 0, 0.0f, 0.0f, Vector3(), 0.0f, Vector3(), 0.0f,
lss::Mdl::Filter::Nearest, lss::Mdl::WrapMode::Repeat, lss::Mdl::WrapMode::Repeat));
overrideMaterialData = &sHighlightMaterialData;
}
uint64_t instanceId = UINT64_MAX;
if (pReplacements != nullptr) {
instanceId = drawReplacements(ctx, &input, pReplacements, overrideMaterialData);
} else {
instanceId = processDrawCallState(ctx, input, overrideMaterialData);
}
}
void SceneManager::createEffectLight(Rc<DxvkContext> ctx, const DrawCallState& input, const RtInstance* instance) {
const float effectLightIntensity = RtxOptions::Get()->getEffectLightIntensity();
if (effectLightIntensity <= 0.f)
return;
const RasterGeometry& geometryData = input.getGeometryData();
const GeometryBufferData bufferData(geometryData);
if (!bufferData.indexData && geometryData.indexCount > 0 || !bufferData.positionData)
return;
// Find centroid of point cloud.
Vector3 centroid = Vector3();
uint32_t counter = 0;
if (geometryData.indexCount > 0) {
for (uint32_t i = 0; i < geometryData.indexCount; i++) {
const uint16_t index = bufferData.getIndex(i);
centroid += bufferData.getPosition(index);
++counter;
}
} else {
for (uint32_t i = 0; i < geometryData.vertexCount; i++) {
centroid += bufferData.getPosition(i);
++counter;
}
}
centroid /= (float) counter;
const Vector4 renderingPos = input.getTransformData().objectToView * Vector4(centroid.x, centroid.y, centroid.z, 1.0f);
// Note: False used in getViewToWorld since the renderingPos of the object is defined with respect to the game's object to view
// matrix, not our freecam's, and as such we want to convert it back to world space using the matching matrix.
const Vector4 worldPos{ getCamera().getViewToWorld(false) * Vector4d{ renderingPos } };
RtLightShaping shaping{};
const float lightRadius = std::max(RtxOptions::Get()->getEffectLightRadius(), 1e-3f);
const float surfaceArea = 4.f * kPi * lightRadius * lightRadius;
const float radianceFactor = 1e5f * effectLightIntensity / surfaceArea;
const Vector3 lightPosition{ worldPos.x, worldPos.y, worldPos.z };
Vector3 lightRadiance;
if (RtxOptions::Get()->getEffectLightPlasmaBall()) {
// Todo: Make these options more configurable via config options.
const double timeMilliseconds = static_cast<double>(getGameTimeSinceStartMS());
const double animationPhase = sin(timeMilliseconds * 0.006) * 0.5 + 0.5;
lightRadiance = lerp(Vector3(1.f, 0.921f, 0.738f), Vector3(1.f, 0.521f, 0.238f), animationPhase) * radianceFactor;
} else {
const D3DCOLORVALUE originalColor = input.getMaterialData().getLegacyMaterial().Diffuse;
lightRadiance = Vector3(originalColor.r, originalColor.g, originalColor.b) * radianceFactor;
}
RtLight rtLight(RtSphereLight(lightPosition, lightRadiance, lightRadius, shaping));
rtLight.isDynamic = true;
m_lightManager.addLight(rtLight, input, RtLightAntiCullingType::MeshReplacement);
}
uint64_t SceneManager::drawReplacements(Rc<DxvkContext> ctx, const DrawCallState* input, const std::vector<AssetReplacement>* pReplacements, const MaterialData* overrideMaterialData) {
ScopedCpuProfileZone();
uint64_t rootInstanceId = UINT64_MAX;
// Detect replacements of meshes that would have unstable hashes due to the vertex hash using vertex data from a shared vertex buffer.
// TODO: Once the vertex hash only uses vertices referenced by the index buffer, this should be removed.
const bool highlightUnsafeReplacement = RtxOptions::Get()->getHighlightUnsafeReplacementModeEnabled() &&
input->getGeometryData().indexBuffer.defined() && input->getGeometryData().vertexCount > input->getGeometryData().indexCount;
if (!pReplacements->empty() && (*pReplacements)[0].includeOriginal) {
DrawCallState newDrawCallState(*input);
newDrawCallState.categories = (*pReplacements)[0].categories.applyCategoryFlags(newDrawCallState.categories);
rootInstanceId = processDrawCallState(ctx, newDrawCallState, overrideMaterialData);
}
for (auto&& replacement : *pReplacements) {
if (replacement.type == AssetReplacement::eMesh) {
DrawCallTransforms transforms = input->getTransformData();
transforms.objectToWorld = transforms.objectToWorld * replacement.replacementToObject;
transforms.objectToView = transforms.objectToView * replacement.replacementToObject;
// Mesh replacements dont support these.
transforms.textureTransform = Matrix4();
transforms.texgenMode = TexGenMode::None;
DrawCallState newDrawCallState(*input);
newDrawCallState.geometryData = replacement.geometry->data; // Note: Geometry Data replaced
newDrawCallState.transformData = transforms;
newDrawCallState.categories = replacement.categories.applyCategoryFlags(newDrawCallState.categories);
// Note: Material Data replaced if a replacement is specified in the Mesh Replacement
if (replacement.materialData != nullptr) {
overrideMaterialData = replacement.materialData;
}
if (highlightUnsafeReplacement) {
static MaterialData sHighlightMaterialData(OpaqueMaterialData(TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(), TextureRef(),
0.f, 1.f, Vector3(0.2f, 0.2f, 0.2f), 1.f, 0.1f, 0.1f, Vector3(1.f, 0.f, 0.f), true, 1, 1, 0, false, false, 200.f, true, false, BlendType::kAlpha, false, AlphaTestType::kAlways, 0, 0.0f, 0.0f, Vector3(), 0.0f, Vector3(), 0.0f,
lss::Mdl::Filter::Nearest, lss::Mdl::WrapMode::Repeat, lss::Mdl::WrapMode::Repeat));
if (getGameTimeSinceStartMS() / 200 % 2 == 0) {
overrideMaterialData = &sHighlightMaterialData;
}
}
uint64_t instanceId = processDrawCallState(ctx, newDrawCallState, overrideMaterialData);
if (rootInstanceId == UINT64_MAX) {
rootInstanceId = instanceId;
}
}
}
for (auto&& replacement : *pReplacements) {
if (replacement.type == AssetReplacement::eLight) {
if (rootInstanceId == UINT64_MAX) {
// TODO(TREX-1141) if we refactor instancing to depend on the pre-replacement drawcall instead
// of the fully processed draw call, we can remove this requirement.
Logger::err(str::format(
"Light prims anchored to a mesh replacement must also include actual meshes. mesh hash: ",
std::hex, input->getHash(RtxOptions::Get()->GeometryHashGenerationRule)
));
break;
}
if (replacement.lightData.has_value()) {
RtLight localLight = replacement.lightData->toRtLight();
localLight.setRootInstanceId(rootInstanceId);
localLight.applyTransform(input->getTransformData().objectToWorld);
m_lightManager.addLight(localLight, *input, RtLightAntiCullingType::MeshReplacement);
}
}
}
return rootInstanceId;
}
void SceneManager::clearFogState() {
m_fog = FogState();
}
void SceneManager::updateBufferCache(RaytraceGeometry& newGeoData) {
ScopedCpuProfileZone();
if (newGeoData.indexBuffer.defined()) {
newGeoData.indexBufferIndex = m_bufferCache.track(newGeoData.indexBuffer);
} else {
newGeoData.indexBufferIndex = kSurfaceInvalidBufferIndex;
}
if (newGeoData.normalBuffer.defined()) {
newGeoData.normalBufferIndex = m_bufferCache.track(newGeoData.normalBuffer);
} else {
newGeoData.normalBufferIndex = kSurfaceInvalidBufferIndex;
}
if (newGeoData.color0Buffer.defined()) {
newGeoData.color0BufferIndex = m_bufferCache.track(newGeoData.color0Buffer);
} else {
newGeoData.color0BufferIndex = kSurfaceInvalidBufferIndex;
}
if (newGeoData.texcoordBuffer.defined()) {
newGeoData.texcoordBufferIndex = m_bufferCache.track(newGeoData.texcoordBuffer);
} else {
newGeoData.texcoordBufferIndex = kSurfaceInvalidBufferIndex;
}
if (newGeoData.positionBuffer.defined()) {
newGeoData.positionBufferIndex = m_bufferCache.track(newGeoData.positionBuffer);
} else {
newGeoData.positionBufferIndex = kSurfaceInvalidBufferIndex;
}
if (newGeoData.previousPositionBuffer.defined()) {
newGeoData.previousPositionBufferIndex = m_bufferCache.track(newGeoData.previousPositionBuffer);
} else {
newGeoData.previousPositionBufferIndex = kSurfaceInvalidBufferIndex;
}
}
SceneManager::ObjectCacheState SceneManager::onSceneObjectAdded(Rc<DxvkContext> ctx, const DrawCallState& drawCallState, BlasEntry* pBlas) {
// This is a new object.
ObjectCacheState result = processGeometryInfo<true>(ctx, drawCallState, pBlas->modifiedGeometryData);
assert(result == ObjectCacheState::KBuildBVH);
pBlas->frameLastUpdated = m_device->getCurrentFrameId();
return result;
}
SceneManager::ObjectCacheState SceneManager::onSceneObjectUpdated(Rc<DxvkContext> ctx, const DrawCallState& drawCallState, BlasEntry* pBlas) {
if (pBlas->frameLastTouched == m_device->getCurrentFrameId()) {
pBlas->cacheMaterial(drawCallState.getMaterialData());
return SceneManager::ObjectCacheState::kUpdateInstance;
}
// TODO: If mesh is static, no need to do any of the below, just use the existing modifiedGeometryData and set result to kInstanceUpdate.
ObjectCacheState result = processGeometryInfo<false>(ctx, drawCallState, pBlas->modifiedGeometryData);
// We dont expect to hit the rebuild path here - since this would indicate an index buffer or other topological change, and that *should* trigger a new scene object (since the hash would change)
assert(result != ObjectCacheState::KBuildBVH);
if (result == ObjectCacheState::kUpdateBVH)
pBlas->frameLastUpdated = m_device->getCurrentFrameId();
pBlas->clearMaterialCache();
pBlas->input = drawCallState; // cache the draw state for the next time.
return result;
}
void SceneManager::onSceneObjectDestroyed(const BlasEntry& blas) {
for (const RtInstance* instance : blas.getLinkedInstances()) {
instance->markForGarbageCollection();
instance->markAsUnlinkedFromBlasEntryForGarbageCollection();
}
}
void SceneManager::onInstanceAdded(const RtInstance& instance) {
BlasEntry* pBlas = instance.getBlas();
if (pBlas != nullptr) {
pBlas->linkInstance(&instance);
}
}
void SceneManager::onInstanceUpdated(RtInstance& instance, const RtSurfaceMaterial& material, const bool hasTransformChanged, const bool hasVerticesChanged) {
auto capturer = m_device->getCommon()->capturer();
if (hasTransformChanged) {
capturer->setInstanceUpdateFlag(instance, GameCapturer::InstFlag::XformUpdate);
}
if (hasVerticesChanged) {
capturer->setInstanceUpdateFlag(instance, GameCapturer::InstFlag::PositionsUpdate);
capturer->setInstanceUpdateFlag(instance, GameCapturer::InstFlag::NormalsUpdate);
}
// This is a ray portal!
if (material.getType() == RtSurfaceMaterialType::RayPortal) {
BlasEntry* pBlas = instance.getBlas();
m_rayPortalManager.processRayPortalData(instance, material);
}
}
void SceneManager::onInstanceDestroyed(const RtInstance& instance) {
BlasEntry* pBlas = instance.getBlas();
// Some BLAS were cleared in the SceneManager::garbageCollection().
// When a BLAS is destroyed, all instances that linked to it will be automatically unlinked. In such case we don't need to
// call onInstanceDestroyed to double unlink the instances.
// Note: This case often happens when BLAS are destroyed faster than instances. (e.g. numFramesToKeepGeometryData >= numFramesToKeepInstances)
if (pBlas != nullptr && !instance.isUnlinkedForGC()) {
pBlas->unlinkInstance(&instance);
}
}
// Helper to populate the texture cache with this resource (and patch sampler if required for texture)
void SceneManager::trackTexture(Rc<DxvkContext> ctx, TextureRef inputTexture, uint32_t& textureIndex, bool hasTexcoords, bool allowAsync) {
// If no texcoords, no need to bind the texture
if (!hasTexcoords) {
ONCE(Logger::info(str::format("[RTX-Compatibility-Info] Trying to bind a texture to a mesh without UVs. Was this intended?")));
return;
}
auto& textureManager = m_device->getCommon()->getTextureManager();
textureManager.addTexture(ctx, inputTexture, allowAsync, textureIndex);
}
uint64_t SceneManager::processDrawCallState(Rc<DxvkContext> ctx, const DrawCallState& drawCallState, const MaterialData* overrideMaterialData) {
ScopedCpuProfileZone();
const bool usingOverrideMaterial = overrideMaterialData != nullptr;
const MaterialData& renderMaterialData =
usingOverrideMaterial ? *overrideMaterialData : drawCallState.getMaterialData();
if (renderMaterialData.getIgnored()) {
return UINT64_MAX;
}
ObjectCacheState result = ObjectCacheState::kInvalid;
BlasEntry* pBlas = nullptr;
if (m_drawCallCache.get(drawCallState, &pBlas) == DrawCallCache::CacheState::kExisted) {
result = onSceneObjectUpdated(ctx, drawCallState, pBlas);
} else {
result = onSceneObjectAdded(ctx, drawCallState, pBlas);
}
// Update the input state, so we always have a reference to the original draw call state
pBlas->frameLastTouched = m_device->getCurrentFrameId();
if (drawCallState.getSkinningState().numBones > 0 &&
drawCallState.getGeometryData().numBonesPerVertex > 0 &&
(result == ObjectCacheState::KBuildBVH || result == ObjectCacheState::kUpdateBVH)) {
m_device->getCommon()->metaGeometryUtils().dispatchSkinning(drawCallState, pBlas->modifiedGeometryData);
pBlas->frameLastUpdated = pBlas->frameLastTouched;
}
assert(pBlas != nullptr);
assert(result != ObjectCacheState::kInvalid);
// Note: Use either the specified override Material Data or the original draw calls state's Material Data to create a Surface Material if no override is specified
const auto renderMaterialDataType = renderMaterialData.getType();
std::optional<RtSurfaceMaterial> surfaceMaterial{};
const bool hasTexcoords = drawCallState.hasTextureCoordinates();
// We're going to use this to create a modified sampler for replacement textures.
// Legacy and replacement materials should follow same filtering but due to lack of override capability per texture
// legacy textures use original sampler to stay true to the original intent while replacements use more advanced filtering
// for better quality by default.
const Rc<DxvkSampler>& originalSampler = drawCallState.getMaterialData().getSampler(); // convenience variable for debug
Rc<DxvkSampler> sampler = originalSampler;
const bool isLegacyMaterial = (renderMaterialDataType == MaterialDataType::Legacy);
// If the original sampler if valid and the new rendering material is not legacy type
// go ahead with patching and maybe merging the sampler states
if(originalSampler != nullptr && !isLegacyMaterial) {
DxvkSamplerCreateInfo samplerInfo = originalSampler->info(); // Use sampler create info struct as convenience
renderMaterialData.populateSamplerInfo(samplerInfo);
sampler = patchSampler(samplerInfo.magFilter,
samplerInfo.addressModeU, samplerInfo.addressModeV, samplerInfo.addressModeW,
samplerInfo.borderColor);
}
uint32_t samplerIndex = trackSampler(sampler);
if (isLegacyMaterial || renderMaterialDataType == MaterialDataType::Opaque || drawCallState.isUsingRaytracedRenderTarget) {
uint32_t albedoOpacityTextureIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t normalTextureIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t tangentTextureIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t heightTextureIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t roughnessTextureIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t metallicTextureIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t emissiveColorTextureIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t subsurfaceMaterialIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t subsurfaceTransmittanceTextureIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t subsurfaceThicknessTextureIndex = kSurfaceMaterialInvalidTextureIndex;
uint32_t subsurfaceSingleScatteringAlbedoTextureIndex = kSurfaceMaterialInvalidTextureIndex;
float anisotropy;
float emissiveIntensity;
Vector4 albedoOpacityConstant;
float roughnessConstant;
float metallicConstant;
Vector3 emissiveColorConstant;
bool enableEmissive;
bool thinFilmEnable = false;
bool alphaIsThinFilmThickness = false;
float thinFilmThicknessConstant = 0.0f;
float displaceIn = 0.0f;
float displaceOut = 0.0f;
bool isUsingRaytracedRenderTarget = drawCallState.isUsingRaytracedRenderTarget;
// Ignore colormap alpha of legacy texture if tagged as 'ignoreAlphaOnTextures'
bool ignoreAlphaChannel = lookupHash(RtxOptions::ignoreAlphaOnTextures(), drawCallState.getMaterialData().getHash());
Vector3 subsurfaceTransmittanceColor(0.0f, 0.0f, 0.0f);
float subsurfaceMeasurementDistance = 0.0f;
Vector3 subsurfaceSingleScatteringAlbedo(0.0f, 0.0f, 0.0f);
float subsurfaceVolumetricAnisotropy = 0.0f;
constexpr Vector4 kWhiteModeAlbedo = Vector4(0.7f, 0.7f, 0.7f, 1.0f);
if (renderMaterialDataType == MaterialDataType::Legacy) {
// Todo: In the future this path will construct a LegacySurfaceMaterial, for now it simply uses
// the OpaqueSurfaceMaterial path until we have a more established legacy material model in place.
const auto& legacyMaterialData = renderMaterialData.getLegacyMaterialData();
const LegacyMaterialDefaults& defaults = RtxOptions::Get()->legacyMaterial;
anisotropy = defaults.anisotropy();
emissiveIntensity = defaults.emissiveIntensity();
albedoOpacityConstant = Vector4(defaults.albedoConstant(), defaults.opacityConstant());
roughnessConstant = defaults.roughnessConstant();
metallicConstant = defaults.metallicConstant();
// Override these for legacy materials
emissiveColorConstant = defaults.emissiveColorConstant();
enableEmissive = defaults.enableEmissive();
if (RtxOptions::Get()->getWhiteMaterialModeEnabled()) {
albedoOpacityConstant = kWhiteModeAlbedo;
metallicConstant = 0.f;
roughnessConstant = 1.f;
} else {
if (defaults.useAlbedoTextureIfPresent()) {
// NOTE: Do not patch original sampler to preserve filtering behavior of the legacy material
trackTexture(ctx, legacyMaterialData.getColorTexture(), albedoOpacityTextureIndex, hasTexcoords);
}
}
if (RtxOptions::Get()->getHighlightLegacyModeEnabled()) {
enableEmissive = true;
// Flash every 20 frames, bright
emissiveIntensity = (sin((float) m_device->getCurrentFrameId()/20) + 1.f) * 2.f;
emissiveColorConstant = Vector3(1, 0, 0); // Red
}
// Todo: Incorporate this and the color texture into emissive conditionally
// emissiveColorTextureIndex != kSurfaceMaterialInvalidTextureIndex ? 100.0f
if (!ignoreAlphaChannel) {
ignoreAlphaChannel = defaults.ignoreAlphaChannel();
}
thinFilmEnable = defaults.enableThinFilm();
alphaIsThinFilmThickness = defaults.alphaIsThinFilmThickness();
thinFilmThicknessConstant = defaults.thinFilmThicknessConstant();
} else if (renderMaterialDataType == MaterialDataType::Opaque) {
const auto& opaqueMaterialData = renderMaterialData.getOpaqueMaterialData();
if (RtxOptions::Get()->getWhiteMaterialModeEnabled()) {
albedoOpacityConstant = kWhiteModeAlbedo;
metallicConstant = 0.f;
roughnessConstant = 1.f;
} else {
trackTexture(ctx, opaqueMaterialData.getAlbedoOpacityTexture(), albedoOpacityTextureIndex, hasTexcoords);
trackTexture(ctx, opaqueMaterialData.getRoughnessTexture(), roughnessTextureIndex, hasTexcoords);
trackTexture(ctx, opaqueMaterialData.getMetallicTexture(), metallicTextureIndex, hasTexcoords);
albedoOpacityConstant.xyz() = opaqueMaterialData.getAlbedoConstant();
albedoOpacityConstant.w = opaqueMaterialData.getOpacityConstant();
metallicConstant = opaqueMaterialData.getMetallicConstant();
roughnessConstant = opaqueMaterialData.getRoughnessConstant();
}