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,
85 const int levels)
86 : m_layerSize(imageLayerSize)
87 , m_type(imageType)
88 , m_numLayers(layers)
89 , m_numSamples(samples)
90 , m_numMipmapLevels(levels)
91 {
92 checkInvariants();
93 }
94
Texture(const Texture & other,const int samples)95 Texture::Texture(const Texture &other, const int samples)
96 : m_layerSize(other.m_layerSize)
97 , m_type(other.m_type)
98 , m_numLayers(other.m_numLayers)
99 , m_numSamples(samples)
100 , m_numMipmapLevels(other.m_numMipmapLevels)
101 {
102 checkInvariants();
103 }
104
minify(uint32_t value,uint32_t mipmapLevel)105 static inline uint32_t minify(uint32_t value, uint32_t mipmapLevel)
106 {
107 return deMax32(value >> mipmapLevel, 1);
108 }
109
layerSize(const int mipmapLevel) const110 tcu::IVec3 Texture::layerSize(const int mipmapLevel) const
111 {
112 tcu::IVec3 size = m_layerSize;
113
114 DE_ASSERT(mipmapLevel < numMipmapLevels());
115
116 if (mipmapLevel == 0)
117 return size;
118
119 switch (m_type)
120 {
121 case IMAGE_TYPE_3D:
122 size.z() = minify(size.z(), mipmapLevel);
123 /* fall-through */
124 case IMAGE_TYPE_CUBE:
125 case IMAGE_TYPE_CUBE_ARRAY:
126 case IMAGE_TYPE_2D_ARRAY:
127 case IMAGE_TYPE_2D:
128 size.y() = minify(size.y(), mipmapLevel);
129 /* fall-through */
130 case IMAGE_TYPE_1D_ARRAY:
131 case IMAGE_TYPE_1D:
132 size.x() = minify(size.x(), mipmapLevel);
133 break;
134 default:
135 DE_FATAL("Not supported image type");
136 }
137 return size;
138 }
139
size(const int mipmapLevel) const140 tcu::IVec3 Texture::size(const int mipmapLevel) const
141 {
142 // texture.size() includes number of layers in one component. Minify only the relevant component for the mipmap level.
143 tcu::IVec3 size = layerSize(mipmapLevel);
144
145 switch (m_type)
146 {
147 case IMAGE_TYPE_1D:
148 case IMAGE_TYPE_BUFFER:
149 case IMAGE_TYPE_2D:
150 case IMAGE_TYPE_3D:
151 return size;
152
153 case IMAGE_TYPE_1D_ARRAY:
154 return tcu::IVec3(size.x(), m_numLayers, 1);
155
156 case IMAGE_TYPE_2D_ARRAY:
157 case IMAGE_TYPE_CUBE:
158 case IMAGE_TYPE_CUBE_ARRAY:
159 return tcu::IVec3(size.x(), size.y(), m_numLayers);
160
161 default:
162 DE_FATAL("Internal error");
163 return tcu::IVec3();
164 }
165 }
166
dimension(void) const167 int Texture::dimension(void) const
168 {
169 switch (m_type)
170 {
171 case IMAGE_TYPE_1D:
172 case IMAGE_TYPE_BUFFER:
173 return 1;
174
175 case IMAGE_TYPE_1D_ARRAY:
176 case IMAGE_TYPE_2D:
177 return 2;
178
179 case IMAGE_TYPE_2D_ARRAY:
180 case IMAGE_TYPE_CUBE:
181 case IMAGE_TYPE_CUBE_ARRAY:
182 case IMAGE_TYPE_3D:
183 return 3;
184
185 default:
186 DE_FATAL("Internal error");
187 return 0;
188 }
189 }
190
layerDimension(void) const191 int Texture::layerDimension(void) const
192 {
193 switch (m_type)
194 {
195 case IMAGE_TYPE_1D:
196 case IMAGE_TYPE_BUFFER:
197 case IMAGE_TYPE_1D_ARRAY:
198 return 1;
199
200 case IMAGE_TYPE_2D:
201 case IMAGE_TYPE_2D_ARRAY:
202 case IMAGE_TYPE_CUBE:
203 case IMAGE_TYPE_CUBE_ARRAY:
204 return 2;
205
206 case IMAGE_TYPE_3D:
207 return 3;
208
209 default:
210 DE_FATAL("Internal error");
211 return 0;
212 }
213 }
214
215 } // namespace image
216 } // namespace vkt
217