summaryrefslogtreecommitdiff
path: root/src/video_core/texture_cache
diff options
context:
space:
mode:
authorGravatar Wollnashorn2023-06-16 13:26:44 +0200
committerGravatar Wollnashorn2023-06-16 13:45:14 +0200
commit2dc0ff79ece34286c7078922668fd9f0a19b47b7 (patch)
treea474956c3fdc432f7259c70337d2c304baf80740 /src/video_core/texture_cache
parentvideo_core: Fallback to default anisotropy instead to 1x anisotropy (diff)
downloadyuzu-2dc0ff79ece34286c7078922668fd9f0a19b47b7.tar.gz
yuzu-2dc0ff79ece34286c7078922668fd9f0a19b47b7.tar.xz
yuzu-2dc0ff79ece34286c7078922668fd9f0a19b47b7.zip
video_core: Use sampler IDs instead pointers in the pipeline config
The previous approach of storing pointers returned by `GetGraphicsSampler`/`GetComputeSampler` caused UB, as these functions can cause reallocation of the sampler slot vector and therefore invalidate the pointers
Diffstat (limited to 'src/video_core/texture_cache')
-rw-r--r--src/video_core/texture_cache/image_view_base.cpp1
-rw-r--r--src/video_core/texture_cache/texture_cache.h30
-rw-r--r--src/video_core/texture_cache/texture_cache_base.h12
3 files changed, 37 insertions, 6 deletions
diff --git a/src/video_core/texture_cache/image_view_base.cpp b/src/video_core/texture_cache/image_view_base.cpp
index 5ebe9b9bb..000eec869 100644
--- a/src/video_core/texture_cache/image_view_base.cpp
+++ b/src/video_core/texture_cache/image_view_base.cpp
@@ -46,7 +46,6 @@ ImageViewBase::ImageViewBase(const ImageInfo& info, const ImageViewInfo& view_in
46ImageViewBase::ImageViewBase(const NullImageViewParams&) : image_id{NULL_IMAGE_ID} {} 46ImageViewBase::ImageViewBase(const NullImageViewParams&) : image_id{NULL_IMAGE_ID} {}
47 47
48bool ImageViewBase::SupportsAnisotropy() const noexcept { 48bool ImageViewBase::SupportsAnisotropy() const noexcept {
49 using namespace VideoCommon;
50 switch (format) { 49 switch (format) {
51 case PixelFormat::R8_UNORM: 50 case PixelFormat::R8_UNORM:
52 case PixelFormat::R8_SNORM: 51 case PixelFormat::R8_SNORM:
diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h
index c7f7448e9..4027d860b 100644
--- a/src/video_core/texture_cache/texture_cache.h
+++ b/src/video_core/texture_cache/texture_cache.h
@@ -222,30 +222,50 @@ void TextureCache<P>::CheckFeedbackLoop(std::span<const ImageViewInOut> views) {
222 222
223template <class P> 223template <class P>
224typename P::Sampler* TextureCache<P>::GetGraphicsSampler(u32 index) { 224typename P::Sampler* TextureCache<P>::GetGraphicsSampler(u32 index) {
225 return &slot_samplers[GetGraphicsSamplerId(index)];
226}
227
228template <class P>
229typename P::Sampler* TextureCache<P>::GetComputeSampler(u32 index) {
230 return &slot_samplers[GetComputeSamplerId(index)];
231}
232
233template <class P>
234SamplerId TextureCache<P>::GetGraphicsSamplerId(u32 index) {
225 if (index > channel_state->graphics_sampler_table.Limit()) { 235 if (index > channel_state->graphics_sampler_table.Limit()) {
226 LOG_DEBUG(HW_GPU, "Invalid sampler index={}", index); 236 LOG_DEBUG(HW_GPU, "Invalid sampler index={}", index);
227 return &slot_samplers[NULL_SAMPLER_ID]; 237 return NULL_SAMPLER_ID;
228 } 238 }
229 const auto [descriptor, is_new] = channel_state->graphics_sampler_table.Read(index); 239 const auto [descriptor, is_new] = channel_state->graphics_sampler_table.Read(index);
230 SamplerId& id = channel_state->graphics_sampler_ids[index]; 240 SamplerId& id = channel_state->graphics_sampler_ids[index];
231 if (is_new) { 241 if (is_new) {
232 id = FindSampler(descriptor); 242 id = FindSampler(descriptor);
233 } 243 }
234 return &slot_samplers[id]; 244 return id;
235} 245}
236 246
237template <class P> 247template <class P>
238typename P::Sampler* TextureCache<P>::GetComputeSampler(u32 index) { 248SamplerId TextureCache<P>::GetComputeSamplerId(u32 index) {
239 if (index > channel_state->compute_sampler_table.Limit()) { 249 if (index > channel_state->compute_sampler_table.Limit()) {
240 LOG_DEBUG(HW_GPU, "Invalid sampler index={}", index); 250 LOG_DEBUG(HW_GPU, "Invalid sampler index={}", index);
241 return &slot_samplers[NULL_SAMPLER_ID]; 251 return NULL_SAMPLER_ID;
242 } 252 }
243 const auto [descriptor, is_new] = channel_state->compute_sampler_table.Read(index); 253 const auto [descriptor, is_new] = channel_state->compute_sampler_table.Read(index);
244 SamplerId& id = channel_state->compute_sampler_ids[index]; 254 SamplerId& id = channel_state->compute_sampler_ids[index];
245 if (is_new) { 255 if (is_new) {
246 id = FindSampler(descriptor); 256 id = FindSampler(descriptor);
247 } 257 }
248 return &slot_samplers[id]; 258 return id;
259}
260
261template <class P>
262const typename P::Sampler& TextureCache<P>::GetSampler(SamplerId id) const noexcept {
263 return slot_samplers[id];
264}
265
266template <class P>
267typename P::Sampler& TextureCache<P>::GetSampler(SamplerId id) noexcept {
268 return slot_samplers[id];
249} 269}
250 270
251template <class P> 271template <class P>
diff --git a/src/video_core/texture_cache/texture_cache_base.h b/src/video_core/texture_cache/texture_cache_base.h
index 3bfa92154..d96ddea9d 100644
--- a/src/video_core/texture_cache/texture_cache_base.h
+++ b/src/video_core/texture_cache/texture_cache_base.h
@@ -159,6 +159,18 @@ public:
159 /// Get the sampler from the compute descriptor table in the specified index 159 /// Get the sampler from the compute descriptor table in the specified index
160 Sampler* GetComputeSampler(u32 index); 160 Sampler* GetComputeSampler(u32 index);
161 161
162 /// Get the sampler id from the graphics descriptor table in the specified index
163 SamplerId GetGraphicsSamplerId(u32 index);
164
165 /// Get the sampler id from the compute descriptor table in the specified index
166 SamplerId GetComputeSamplerId(u32 index);
167
168 /// Return a constant reference to the given sampler id
169 [[nodiscard]] const Sampler& GetSampler(SamplerId id) const noexcept;
170
171 /// Return a reference to the given sampler id
172 [[nodiscard]] Sampler& GetSampler(SamplerId id) noexcept;
173
162 /// Refresh the state for graphics image view and sampler descriptors 174 /// Refresh the state for graphics image view and sampler descriptors
163 void SynchronizeGraphicsDescriptors(); 175 void SynchronizeGraphicsDescriptors();
164 176