Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef VK_SAMPLER_HPP_ |
| 16 | #define VK_SAMPLER_HPP_ |
| 17 | |
| 18 | #include "VkDevice.hpp" |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 19 | #include "VkImageView.hpp" // For ResolveIdentityMapping() |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 20 | #include "Device/Config.hpp" |
| 21 | #include "System/Math.hpp" |
Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 22 | |
Ben Clayton | eac32c4 | 2019-04-26 11:25:57 +0100 | [diff] [blame] | 23 | #include <atomic> |
| 24 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 25 | namespace vk { |
Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 26 | |
| 27 | class Sampler : public Object<Sampler, VkSampler> |
| 28 | { |
| 29 | public: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 30 | Sampler(const VkSamplerCreateInfo *pCreateInfo, void *mem, const vk::SamplerYcbcrConversion *ycbcrConversion) |
| 31 | : magFilter(pCreateInfo->magFilter) |
| 32 | , minFilter(pCreateInfo->minFilter) |
| 33 | , mipmapMode(pCreateInfo->mipmapMode) |
| 34 | , addressModeU(pCreateInfo->addressModeU) |
| 35 | , addressModeV(pCreateInfo->addressModeV) |
| 36 | , addressModeW(pCreateInfo->addressModeW) |
| 37 | , mipLodBias(pCreateInfo->mipLodBias) |
| 38 | , anisotropyEnable(pCreateInfo->anisotropyEnable) |
| 39 | , maxAnisotropy(pCreateInfo->maxAnisotropy) |
| 40 | , compareEnable(pCreateInfo->compareEnable) |
| 41 | , compareOp(pCreateInfo->compareOp) |
| 42 | , minLod(ClampLod(pCreateInfo->minLod)) |
| 43 | , maxLod(ClampLod(pCreateInfo->maxLod)) |
| 44 | , borderColor(pCreateInfo->borderColor) |
| 45 | , unnormalizedCoordinates(pCreateInfo->unnormalizedCoordinates) |
| 46 | , ycbcrConversion(ycbcrConversion) |
Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 50 | static size_t ComputeRequiredAllocationSize(const VkSamplerCreateInfo *pCreateInfo) |
Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 51 | { |
| 52 | return 0; |
| 53 | } |
| 54 | |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 55 | // Prevents accessing mipmap levels out of range. |
| 56 | static float ClampLod(float lod) |
| 57 | { |
| 58 | return sw::clamp(lod, 0.0f, (float)(sw::MAX_TEXTURE_LOD)); |
| 59 | } |
| 60 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 61 | const uint32_t id = nextID++; |
| 62 | const VkFilter magFilter = VK_FILTER_NEAREST; |
| 63 | const VkFilter minFilter = VK_FILTER_NEAREST; |
| 64 | const VkSamplerMipmapMode mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST; |
Nicolas Capens | 7d86727 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 65 | const VkSamplerAddressMode addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; |
| 66 | const VkSamplerAddressMode addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; |
| 67 | const VkSamplerAddressMode addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 68 | const float mipLodBias = 0.0f; |
| 69 | const VkBool32 anisotropyEnable = VK_FALSE; |
| 70 | const float maxAnisotropy = 0.0f; |
| 71 | const VkBool32 compareEnable = VK_FALSE; |
| 72 | const VkCompareOp compareOp = VK_COMPARE_OP_NEVER; |
| 73 | const float minLod = 0.0f; |
| 74 | const float maxLod = 0.0f; |
| 75 | const VkBorderColor borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK; |
| 76 | const VkBool32 unnormalizedCoordinates = VK_FALSE; |
Ben Clayton | eac32c4 | 2019-04-26 11:25:57 +0100 | [diff] [blame] | 77 | |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 78 | const vk::SamplerYcbcrConversion *ycbcrConversion = nullptr; |
| 79 | |
Ben Clayton | eac32c4 | 2019-04-26 11:25:57 +0100 | [diff] [blame] | 80 | private: |
| 81 | static std::atomic<uint32_t> nextID; |
Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 82 | }; |
| 83 | |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 84 | class SamplerYcbcrConversion : public Object<SamplerYcbcrConversion, VkSamplerYcbcrConversion> |
| 85 | { |
| 86 | public: |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 87 | SamplerYcbcrConversion(const VkSamplerYcbcrConversionCreateInfo *pCreateInfo, void *mem) |
| 88 | : format(pCreateInfo->format) |
| 89 | , ycbcrModel(pCreateInfo->ycbcrModel) |
| 90 | , ycbcrRange(pCreateInfo->ycbcrRange) |
| 91 | , components(ResolveIdentityMapping(pCreateInfo->components)) |
| 92 | , xChromaOffset(pCreateInfo->xChromaOffset) |
| 93 | , yChromaOffset(pCreateInfo->yChromaOffset) |
| 94 | , chromaFilter(pCreateInfo->chromaFilter) |
| 95 | , forceExplicitReconstruction(pCreateInfo->forceExplicitReconstruction) |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 96 | { |
| 97 | } |
| 98 | |
| 99 | ~SamplerYcbcrConversion() = default; |
| 100 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 101 | static size_t ComputeRequiredAllocationSize(const VkSamplerYcbcrConversionCreateInfo *pCreateInfo) |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 102 | { |
| 103 | return 0; |
| 104 | } |
| 105 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 106 | const VkFormat format = VK_FORMAT_UNDEFINED; |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 107 | const VkSamplerYcbcrModelConversion ycbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY; |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 108 | const VkSamplerYcbcrRange ycbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL; |
| 109 | const VkComponentMapping components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A }; |
| 110 | const VkChromaLocation xChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; |
| 111 | const VkChromaLocation yChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN; |
| 112 | const VkFilter chromaFilter = VK_FILTER_NEAREST; |
| 113 | const VkBool32 forceExplicitReconstruction = VK_FALSE; |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 114 | }; |
| 115 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 116 | static inline Sampler *Cast(VkSampler object) |
Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 117 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 118 | return Sampler::Cast(object); |
Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 121 | static inline SamplerYcbcrConversion *Cast(VkSamplerYcbcrConversion object) |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 122 | { |
Alexis Hetu | bd4cf81 | 2019-06-14 15:14:07 -0400 | [diff] [blame] | 123 | return SamplerYcbcrConversion::Cast(object); |
Nicolas Capens | 6b63c80 | 2019-05-16 11:10:34 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 126 | } // namespace vk |
Alexis Hetu | 5174c57 | 2018-11-19 11:30:43 -0500 | [diff] [blame] | 127 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 128 | #endif // VK_SAMPLER_HPP_ |