1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2016 The Khronos Group Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Texture utility class
22 *//*--------------------------------------------------------------------*/
23
24 #include "vktImageTexture.hpp"
25
26 namespace vkt
27 {
28 namespace image
29 {
30
checkInvariants(void) const31 void Texture::checkInvariants (void) const
32 {
33 DE_ASSERT((m_numSamples == 1) || (m_numSamples == 2) || (m_numSamples == 4) || (m_numSamples == 8) ||
34 (m_numSamples == 16) || (m_numSamples == 32) || (m_numSamples == 64));
35 DE_ASSERT(m_numLayers >= 1);
36 DE_ASSERT(m_layerSize.x() >= 1 && m_layerSize.y() >= 1 && m_layerSize.z() >= 1);
37
38 switch (m_type)
39 {
40 case IMAGE_TYPE_1D:
41 case IMAGE_TYPE_BUFFER:
42 DE_ASSERT(m_numLayers == 1);
43 DE_ASSERT(m_numSamples == 1);
44 DE_ASSERT(m_layerSize.y() == 1 && m_layerSize.z() == 1);
45 break;
46
47 case IMAGE_TYPE_1D_ARRAY:
48 DE_ASSERT(m_numSamples == 1);
49 DE_ASSERT(m_layerSize.y() == 1 && m_layerSize.z() == 1);
50 break;
51
52 case IMAGE_TYPE_2D:
53 DE_ASSERT(m_numLayers == 1);
54 DE_ASSERT(m_layerSize.z() == 1);
55 break;
56
57 case IMAGE_TYPE_2D_ARRAY:
58 DE_ASSERT(m_layerSize.z() == 1);
59 break;
60
61 case IMAGE_TYPE_CUBE:
62 DE_ASSERT(m_numSamples == 1);
63 DE_ASSERT(m_numLayers == 6);
64 DE_ASSERT(m_layerSize.z() == 1);
65 break;
66
67 case IMAGE_TYPE_CUBE_ARRAY:
68 DE_ASSERT(m_numSamples == 1);
69 DE_ASSERT(m_numLayers >= 6 && m_numLayers % 6 == 0);
70 DE_ASSERT(m_layerSize.z() == 1);
71 break;
72
73 case IMAGE_TYPE_3D:
74 DE_ASSERT(m_numSamples == 1);
75 DE_ASSERT(m_numLayers == 1);
76 break;
77
78 default:
79 DE_FATAL("Internal error");
80 break;
81 }
82 }
83
Texture(const ImageType imageType,const tcu::IVec3 & imageLayerSize,const int layers,const int samples,const int levels)84 Texture::Texture (const ImageType imageType, const tcu::IVec3& imageLayerSize, const int layers, const int samples, const int levels)
85 : m_layerSize (imageLayerSize)
86 , m_type (imageType)
87 , m_numLayers (layers)
88 , m_numSamples (samples)
89 , m_numMipmapLevels (levels)
90 {
91 checkInvariants();
92 }
93
Texture(const Texture & other,const int samples)94 Texture::Texture (const Texture& other, const int samples)
95 : m_layerSize (other.m_layerSize)
96 , m_type (other.m_type)
97 , m_numLayers (other.m_numLayers)
98 , m_numSamples (samples)
99 , m_numMipmapLevels (other.m_numMipmapLevels)
100 {
101 checkInvariants();
102 }
103
minify(deUint32 value,deUint32 mipmapLevel)104 static inline deUint32 minify (deUint32 value, deUint32 mipmapLevel)
105 {
106 return deMax32(value >> mipmapLevel, 1);
107 }
108
layerSize(const int mipmapLevel) const109 tcu::IVec3 Texture::layerSize (const int mipmapLevel) const
110 {
111 tcu::IVec3 size = m_layerSize;
112
113 DE_ASSERT(mipmapLevel < numMipmapLevels());
114
115 if (mipmapLevel == 0)
116 return size;
117
118 switch (m_type)
119 {
120 case IMAGE_TYPE_3D:
121 size.z() = minify(size.z(), mipmapLevel);
122 /* fall-through */
123 case IMAGE_TYPE_CUBE:
124 case IMAGE_TYPE_CUBE_ARRAY:
125 case IMAGE_TYPE_2D_ARRAY:
126 case IMAGE_TYPE_2D:
127 size.y() = minify(size.y(), mipmapLevel);
128 /* fall-through */
129 case IMAGE_TYPE_1D_ARRAY:
130 case IMAGE_TYPE_1D:
131 size.x() = minify(size.x(), mipmapLevel);
132 break;
133 default:
134 DE_FATAL("Not supported image type");
135 }
136 return size;
137 }
138
size(const int mipmapLevel) const139 tcu::IVec3 Texture::size (const int mipmapLevel) const
140 {
141 // texture.size() includes number of layers in one component. Minify only the relevant component for the mipmap level.
142 tcu::IVec3 size = layerSize(mipmapLevel);
143
144 switch (m_type)
145 {
146 case IMAGE_TYPE_1D:
147 case IMAGE_TYPE_BUFFER:
148 case IMAGE_TYPE_2D:
149 case IMAGE_TYPE_3D:
150 return size;
151
152 case IMAGE_TYPE_1D_ARRAY:
153 return tcu::IVec3(size.x(), m_numLayers, 1);
154
155 case IMAGE_TYPE_2D_ARRAY:
156 case IMAGE_TYPE_CUBE:
157 case IMAGE_TYPE_CUBE_ARRAY:
158 return tcu::IVec3(size.x(), size.y(), m_numLayers);
159
160 default:
161 DE_FATAL("Internal error");
162 return tcu::IVec3();
163 }
164 }
165
dimension(void) const166 int Texture::dimension (void) const
167 {
168 switch (m_type)
169 {
170 case IMAGE_TYPE_1D:
171 case IMAGE_TYPE_BUFFER:
172 return 1;
173
174 case IMAGE_TYPE_1D_ARRAY:
175 case IMAGE_TYPE_2D:
176 return 2;
177
178 case IMAGE_TYPE_2D_ARRAY:
179 case IMAGE_TYPE_CUBE:
180 case IMAGE_TYPE_CUBE_ARRAY:
181 case IMAGE_TYPE_3D:
182 return 3;
183
184 default:
185 DE_FATAL("Internal error");
186 return 0;
187 }
188 }
189
layerDimension(void) const190 int Texture::layerDimension (void) const
191 {
192 switch (m_type)
193 {
194 case IMAGE_TYPE_1D:
195 case IMAGE_TYPE_BUFFER:
196 case IMAGE_TYPE_1D_ARRAY:
197 return 1;
198
199 case IMAGE_TYPE_2D:
200 case IMAGE_TYPE_2D_ARRAY:
201 case IMAGE_TYPE_CUBE:
202 case IMAGE_TYPE_CUBE_ARRAY:
203 return 2;
204
205 case IMAGE_TYPE_3D:
206 return 3;
207
208 default:
209 DE_FATAL("Internal error");
210 return 0;
211 }
212 }
213
214 } // image
215 } // vkt
216