• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_IMAGE_VIEW_HPP_
16 #define VK_IMAGE_VIEW_HPP_
17 
18 #include "VkFormat.hpp"
19 #include "VkImage.hpp"
20 #include "VkObject.hpp"
21 
22 #include "System/Debug.hpp"
23 
24 #include <atomic>
25 
26 namespace vk {
27 
28 class SamplerYcbcrConversion;
29 
30 // Uniquely identifies state used by sampling routine generation.
31 // ID space shared by image views and buffer views.
32 union Identifier
33 {
34 	// Image view identifier
35 	Identifier(const Image *image, VkImageViewType type, VkFormat format, VkComponentMapping mapping);
36 
37 	// Buffer view identifier
38 	Identifier(VkFormat format);
39 
operator uint32_t() const40 	operator uint32_t() const
41 	{
42 		static_assert(sizeof(Identifier) == sizeof(uint32_t), "Identifier must be 32-bit");
43 		return id;
44 	}
45 
46 	uint32_t id = 0;
47 
48 	struct
49 	{
50 		uint32_t imageViewType : 3;
51 		uint32_t format : 8;
52 		uint32_t r : 3;
53 		uint32_t g : 3;
54 		uint32_t b : 3;
55 		uint32_t a : 3;
56 	};
57 };
58 
59 class ImageView : public Object<ImageView, VkImageView>
60 {
61 public:
62 	// Image usage:
63 	// RAW: Use the base image as is
64 	// SAMPLING: Image used for texture sampling
65 	enum Usage
66 	{
67 		RAW,
68 		SAMPLING
69 	};
70 
71 	ImageView(const VkImageViewCreateInfo *pCreateInfo, void *mem, const vk::SamplerYcbcrConversion *ycbcrConversion);
72 	void destroy(const VkAllocationCallbacks *pAllocator);
73 
74 	static size_t ComputeRequiredAllocationSize(const VkImageViewCreateInfo *pCreateInfo);
75 
76 	void clear(const VkClearValue &clearValues, VkImageAspectFlags aspectMask, const VkRect2D &renderArea);
77 	void clear(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkClearRect &renderArea);
78 	void clearWithLayerMask(const VkClearValue &clearValue, VkImageAspectFlags aspectMask, const VkRect2D &renderArea, uint32_t layerMask);
79 	void resolve(ImageView *resolveAttachment);
80 	void resolve(ImageView *resolveAttachment, int layer);
81 	void resolveWithLayerMask(ImageView *resolveAttachment, uint32_t layerMask);
82 	void resolveDepthStencil(ImageView *resolveAttachment, const VkSubpassDescriptionDepthStencilResolve &dsResolve);
83 
getType() const84 	VkImageViewType getType() const { return viewType; }
85 	Format getFormat(Usage usage = RAW) const;
getFormat(VkImageAspectFlagBits aspect) const86 	Format getFormat(VkImageAspectFlagBits aspect) const { return image->getFormat(aspect); }
87 	int rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const;
88 	int slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const;
89 	int getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel, Usage usage = RAW) const;
90 	int layerPitchBytes(VkImageAspectFlagBits aspect, Usage usage = RAW) const;
91 	VkExtent2D getMipLevelExtent(uint32_t mipLevel) const;
92 	VkExtent2D getMipLevelExtent(uint32_t mipLevel, VkImageAspectFlagBits aspect) const;
93 	int getDepthOrLayerCount(uint32_t mipLevel) const;
94 
getSampleCount() const95 	int getSampleCount() const
96 	{
97 		switch(image->getSampleCountFlagBits())
98 		{
99 			case VK_SAMPLE_COUNT_1_BIT: return 1;
100 			case VK_SAMPLE_COUNT_4_BIT: return 4;
101 			default:
102 				UNSUPPORTED("Sample count flags %d", image->getSampleCountFlagBits());
103 				return 1;
104 		}
105 	}
106 
107 	void *getOffsetPointer(const VkOffset3D &offset, VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer, Usage usage = RAW) const;
hasDepthAspect() const108 	bool hasDepthAspect() const { return (subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0; }
hasStencilAspect() const109 	bool hasStencilAspect() const { return (subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0; }
110 
111 	// This function is only called from the renderer, so use the USING_STORAGE flag,
112 	// as it is required in order to write to an image from a shader
contentsChanged()113 	void contentsChanged() { image->contentsChanged(subresourceRange, Image::USING_STORAGE); }
114 
prepareForSampling()115 	void prepareForSampling() { image->prepareForSampling(subresourceRange); }
116 
getComponentMapping() const117 	const VkComponentMapping &getComponentMapping() const { return components; }
getSubresourceRange() const118 	const VkImageSubresourceRange &getSubresourceRange() const { return subresourceRange; }
getSizeInBytes() const119 	size_t getSizeInBytes() const { return image->getSizeInBytes(subresourceRange); }
120 
121 private:
122 	bool imageTypesMatch(VkImageType imageType) const;
123 	const Image *getImage(Usage usage) const;
124 
125 	Image *const image = nullptr;
126 	const VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D;
127 	const Format format = VK_FORMAT_UNDEFINED;
128 	const VkComponentMapping components = {};
129 	const VkImageSubresourceRange subresourceRange = {};
130 
131 	const vk::SamplerYcbcrConversion *ycbcrConversion = nullptr;
132 
133 public:
134 	const Identifier id;
135 };
136 
137 VkComponentMapping ResolveIdentityMapping(VkComponentMapping mapping);
138 VkComponentMapping ResolveComponentMapping(VkComponentMapping mapping, vk::Format format);
139 VkImageSubresourceRange ResolveRemainingLevelsLayers(VkImageSubresourceRange range, const vk::Image *image);
140 
Cast(VkImageView object)141 static inline ImageView *Cast(VkImageView object)
142 {
143 	return ImageView::Cast(object);
144 }
145 
146 }  // namespace vk
147 
148 #endif  // VK_IMAGE_VIEW_HPP_
149