1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2015 The Khronos Group Inc.
6 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Copyright (c) 2016 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Vulkan ShaderRenderCase
24 *//*--------------------------------------------------------------------*/
25
26 #include "vktShaderRender.hpp"
27
28 #include "tcuImageCompare.hpp"
29 #include "tcuImageIO.hpp"
30 #include "tcuTestLog.hpp"
31 #include "tcuTextureUtil.hpp"
32 #include "tcuSurface.hpp"
33 #include "tcuVector.hpp"
34
35 #include "deFilePath.hpp"
36 #include "deMath.h"
37 #include "deUniquePtr.hpp"
38
39 #include "vkDeviceUtil.hpp"
40 #include "vkImageUtil.hpp"
41 #include "vkPlatform.hpp"
42 #include "vkQueryUtil.hpp"
43 #include "vkRef.hpp"
44 #include "vkRefUtil.hpp"
45 #include "vkStrUtil.hpp"
46 #include "vkTypeUtil.hpp"
47 #include "vkCmdUtil.hpp"
48 #include "vkObjUtil.hpp"
49
50 #include <vector>
51 #include <string>
52
53 namespace vkt
54 {
55 namespace sr
56 {
57
58 using namespace vk;
59
textureTypeToImageViewType(TextureBinding::Type type)60 VkImageViewType textureTypeToImageViewType (TextureBinding::Type type)
61 {
62 switch (type)
63 {
64 case TextureBinding::TYPE_1D: return VK_IMAGE_VIEW_TYPE_1D;
65 case TextureBinding::TYPE_2D: return VK_IMAGE_VIEW_TYPE_2D;
66 case TextureBinding::TYPE_3D: return VK_IMAGE_VIEW_TYPE_3D;
67 case TextureBinding::TYPE_CUBE_MAP: return VK_IMAGE_VIEW_TYPE_CUBE;
68 case TextureBinding::TYPE_1D_ARRAY: return VK_IMAGE_VIEW_TYPE_1D_ARRAY;
69 case TextureBinding::TYPE_2D_ARRAY: return VK_IMAGE_VIEW_TYPE_2D_ARRAY;
70 case TextureBinding::TYPE_CUBE_ARRAY: return VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
71
72 default:
73 DE_FATAL("Impossible");
74 return (VkImageViewType)0;
75 }
76 }
77
viewTypeToImageType(VkImageViewType type)78 VkImageType viewTypeToImageType (VkImageViewType type)
79 {
80 switch (type)
81 {
82 case VK_IMAGE_VIEW_TYPE_1D:
83 case VK_IMAGE_VIEW_TYPE_1D_ARRAY: return VK_IMAGE_TYPE_1D;
84 case VK_IMAGE_VIEW_TYPE_2D:
85 case VK_IMAGE_VIEW_TYPE_2D_ARRAY: return VK_IMAGE_TYPE_2D;
86 case VK_IMAGE_VIEW_TYPE_3D: return VK_IMAGE_TYPE_3D;
87 case VK_IMAGE_VIEW_TYPE_CUBE:
88 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: return VK_IMAGE_TYPE_2D;
89
90 default:
91 DE_FATAL("Impossible");
92 return (VkImageType)0;
93 }
94 }
95
textureUsageFlags(void)96 vk::VkImageUsageFlags textureUsageFlags (void)
97 {
98 return (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT);
99 }
100
textureCreateFlags(vk::VkImageViewType viewType,ShaderRenderCaseInstance::ImageBackingMode backingMode)101 vk::VkImageCreateFlags textureCreateFlags (vk::VkImageViewType viewType, ShaderRenderCaseInstance::ImageBackingMode backingMode)
102 {
103 const bool isCube = (viewType == VK_IMAGE_VIEW_TYPE_CUBE || viewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY);
104 VkImageCreateFlags imageCreateFlags = (isCube ? static_cast<VkImageCreateFlags>(VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) : 0u);
105
106 if (backingMode == ShaderRenderCaseInstance::IMAGE_BACKING_MODE_SPARSE)
107 imageCreateFlags |= (VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT);
108
109 return imageCreateFlags;
110 }
111
112 namespace
113 {
114
115 static const deUint32 MAX_RENDER_WIDTH = 128;
116 static const deUint32 MAX_RENDER_HEIGHT = 128;
117 static const tcu::Vec4 DEFAULT_CLEAR_COLOR = tcu::Vec4(0.125f, 0.25f, 0.5f, 1.0f);
118
119 /*! Gets the next multiple of a given divisor */
getNextMultiple(deUint32 divisor,deUint32 value)120 static deUint32 getNextMultiple (deUint32 divisor, deUint32 value)
121 {
122 if (value % divisor == 0)
123 {
124 return value;
125 }
126 return value + divisor - (value % divisor);
127 }
128
129 /*! Gets the next value that is multiple of all given divisors */
getNextMultiple(const std::vector<deUint32> & divisors,deUint32 value)130 static deUint32 getNextMultiple (const std::vector<deUint32>& divisors, deUint32 value)
131 {
132 deUint32 nextMultiple = value;
133 bool nextMultipleFound = false;
134
135 while (true)
136 {
137 nextMultipleFound = true;
138
139 for (size_t divNdx = 0; divNdx < divisors.size(); divNdx++)
140 nextMultipleFound = nextMultipleFound && (nextMultiple % divisors[divNdx] == 0);
141
142 if (nextMultipleFound)
143 break;
144
145 DE_ASSERT(nextMultiple < ~((deUint32)0u));
146 nextMultiple = getNextMultiple(divisors[0], nextMultiple + 1);
147 }
148
149 return nextMultiple;
150 }
151
152 } // anonymous
153
154 // QuadGrid.
155
156 class QuadGrid
157 {
158 public:
159 QuadGrid (int gridSize,
160 int screenWidth,
161 int screenHeight,
162 const tcu::Vec4& constCoords,
163 const std::vector<tcu::Mat4>& userAttribTransforms,
164 const std::vector<TextureBindingSp>& textures);
165 ~QuadGrid (void);
166
getGridSize(void) const167 int getGridSize (void) const { return m_gridSize; }
getNumVertices(void) const168 int getNumVertices (void) const { return m_numVertices; }
getNumTriangles(void) const169 int getNumTriangles (void) const { return m_numTriangles; }
getConstCoords(void) const170 const tcu::Vec4& getConstCoords (void) const { return m_constCoords; }
getUserAttribTransforms(void) const171 const std::vector<tcu::Mat4> getUserAttribTransforms (void) const { return m_userAttribTransforms; }
getTextures(void) const172 const std::vector<TextureBindingSp>& getTextures (void) const { return m_textures; }
173
getPositions(void) const174 const tcu::Vec4* getPositions (void) const { return &m_positions[0]; }
getAttribOne(void) const175 const float* getAttribOne (void) const { return &m_attribOne[0]; }
getCoords(void) const176 const tcu::Vec4* getCoords (void) const { return &m_coords[0]; }
getUnitCoords(void) const177 const tcu::Vec4* getUnitCoords (void) const { return &m_unitCoords[0]; }
178
getUserAttrib(int attribNdx) const179 const tcu::Vec4* getUserAttrib (int attribNdx) const { return &m_userAttribs[attribNdx][0]; }
getIndices(void) const180 const deUint16* getIndices (void) const { return &m_indices[0]; }
181
182 tcu::Vec4 getCoords (float sx, float sy) const;
183 tcu::Vec4 getUnitCoords (float sx, float sy) const;
184
getNumUserAttribs(void) const185 int getNumUserAttribs (void) const { return (int)m_userAttribTransforms.size(); }
186 tcu::Vec4 getUserAttrib (int attribNdx, float sx, float sy) const;
187
188 private:
189 const int m_gridSize;
190 const int m_numVertices;
191 const int m_numTriangles;
192 const tcu::Vec4 m_constCoords;
193 const std::vector<tcu::Mat4> m_userAttribTransforms;
194
195 const std::vector<TextureBindingSp>& m_textures;
196
197 std::vector<tcu::Vec4> m_screenPos;
198 std::vector<tcu::Vec4> m_positions;
199 std::vector<tcu::Vec4> m_coords; //!< Near-unit coordinates, roughly [-2.0 .. 2.0].
200 std::vector<tcu::Vec4> m_unitCoords; //!< Positive-only coordinates [0.0 .. 1.5].
201 std::vector<float> m_attribOne;
202 std::vector<tcu::Vec4> m_userAttribs[ShaderEvalContext::MAX_TEXTURES];
203 std::vector<deUint16> m_indices;
204 };
205
QuadGrid(int gridSize,int width,int height,const tcu::Vec4 & constCoords,const std::vector<tcu::Mat4> & userAttribTransforms,const std::vector<TextureBindingSp> & textures)206 QuadGrid::QuadGrid (int gridSize,
207 int width,
208 int height,
209 const tcu::Vec4& constCoords,
210 const std::vector<tcu::Mat4>& userAttribTransforms,
211 const std::vector<TextureBindingSp>& textures)
212 : m_gridSize (gridSize)
213 , m_numVertices ((gridSize + 1) * (gridSize + 1))
214 , m_numTriangles (gridSize * gridSize * 2)
215 , m_constCoords (constCoords)
216 , m_userAttribTransforms (userAttribTransforms)
217 , m_textures (textures)
218 {
219 const tcu::Vec4 viewportScale ((float)width, (float)height, 0.0f, 0.0f);
220
221 // Compute vertices.
222 m_screenPos.resize(m_numVertices);
223 m_positions.resize(m_numVertices);
224 m_coords.resize(m_numVertices);
225 m_unitCoords.resize(m_numVertices);
226 m_attribOne.resize(m_numVertices);
227
228 // User attributes.
229 for (int attrNdx = 0; attrNdx < DE_LENGTH_OF_ARRAY(m_userAttribs); attrNdx++)
230 m_userAttribs[attrNdx].resize(m_numVertices);
231
232 for (int y = 0; y < gridSize+1; y++)
233 for (int x = 0; x < gridSize+1; x++)
234 {
235 float sx = (float)x / (float)gridSize;
236 float sy = (float)y / (float)gridSize;
237 float fx = 2.0f * sx - 1.0f;
238 float fy = 2.0f * sy - 1.0f;
239 int vtxNdx = ((y * (gridSize+1)) + x);
240
241 m_positions[vtxNdx] = tcu::Vec4(fx, fy, 0.0f, 1.0f);
242 m_coords[vtxNdx] = getCoords(sx, sy);
243 m_unitCoords[vtxNdx] = getUnitCoords(sx, sy);
244 m_attribOne[vtxNdx] = 1.0f;
245
246 m_screenPos[vtxNdx] = tcu::Vec4(sx, sy, 0.0f, 1.0f) * viewportScale;
247
248 for (int attribNdx = 0; attribNdx < getNumUserAttribs(); attribNdx++)
249 m_userAttribs[attribNdx][vtxNdx] = getUserAttrib(attribNdx, sx, sy);
250 }
251
252 // Compute indices.
253 m_indices.resize(3 * m_numTriangles);
254 for (int y = 0; y < gridSize; y++)
255 for (int x = 0; x < gridSize; x++)
256 {
257 int stride = gridSize + 1;
258 int v00 = (y * stride) + x;
259 int v01 = (y * stride) + x + 1;
260 int v10 = ((y+1) * stride) + x;
261 int v11 = ((y+1) * stride) + x + 1;
262
263 int baseNdx = ((y * gridSize) + x) * 6;
264 m_indices[baseNdx + 0] = (deUint16)v10;
265 m_indices[baseNdx + 1] = (deUint16)v00;
266 m_indices[baseNdx + 2] = (deUint16)v01;
267
268 m_indices[baseNdx + 3] = (deUint16)v10;
269 m_indices[baseNdx + 4] = (deUint16)v01;
270 m_indices[baseNdx + 5] = (deUint16)v11;
271 }
272 }
273
~QuadGrid(void)274 QuadGrid::~QuadGrid (void)
275 {
276 }
277
getCoords(float sx,float sy) const278 inline tcu::Vec4 QuadGrid::getCoords (float sx, float sy) const
279 {
280 const float fx = 2.0f * sx - 1.0f;
281 const float fy = 2.0f * sy - 1.0f;
282 return tcu::Vec4(fx, fy, -fx + 0.33f*fy, -0.275f*fx - fy);
283 }
284
getUnitCoords(float sx,float sy) const285 inline tcu::Vec4 QuadGrid::getUnitCoords (float sx, float sy) const
286 {
287 return tcu::Vec4(sx, sy, 0.33f*sx + 0.5f*sy, 0.5f*sx + 0.25f*sy);
288 }
289
getUserAttrib(int attribNdx,float sx,float sy) const290 inline tcu::Vec4 QuadGrid::getUserAttrib (int attribNdx, float sx, float sy) const
291 {
292 // homogeneous normalized screen-space coordinates
293 return m_userAttribTransforms[attribNdx] * tcu::Vec4(sx, sy, 0.0f, 1.0f);
294 }
295
296 // TextureBinding
297
TextureBinding(const tcu::Archive & archive,const char * filename,const Type type,const tcu::Sampler & sampler)298 TextureBinding::TextureBinding (const tcu::Archive& archive,
299 const char* filename,
300 const Type type,
301 const tcu::Sampler& sampler)
302 : m_type (type)
303 , m_sampler (sampler)
304 {
305 switch(m_type)
306 {
307 case TYPE_2D: m_binding.tex2D = loadTexture2D(archive, filename).release(); break;
308 default:
309 DE_FATAL("Unsupported texture type");
310 }
311 }
312
TextureBinding(const tcu::Texture1D * tex1D,const tcu::Sampler & sampler)313 TextureBinding::TextureBinding (const tcu::Texture1D* tex1D, const tcu::Sampler& sampler)
314 : m_type (TYPE_1D)
315 , m_sampler (sampler)
316 {
317 m_binding.tex1D = tex1D;
318 }
319
TextureBinding(const tcu::Texture2D * tex2D,const tcu::Sampler & sampler)320 TextureBinding::TextureBinding (const tcu::Texture2D* tex2D, const tcu::Sampler& sampler)
321 : m_type (TYPE_2D)
322 , m_sampler (sampler)
323 {
324 m_binding.tex2D = tex2D;
325 }
326
TextureBinding(const tcu::Texture3D * tex3D,const tcu::Sampler & sampler)327 TextureBinding::TextureBinding (const tcu::Texture3D* tex3D, const tcu::Sampler& sampler)
328 : m_type (TYPE_3D)
329 , m_sampler (sampler)
330 {
331 m_binding.tex3D = tex3D;
332 }
333
TextureBinding(const tcu::TextureCube * texCube,const tcu::Sampler & sampler)334 TextureBinding::TextureBinding (const tcu::TextureCube* texCube, const tcu::Sampler& sampler)
335 : m_type (TYPE_CUBE_MAP)
336 , m_sampler (sampler)
337 {
338 m_binding.texCube = texCube;
339 }
340
TextureBinding(const tcu::Texture1DArray * tex1DArray,const tcu::Sampler & sampler)341 TextureBinding::TextureBinding (const tcu::Texture1DArray* tex1DArray, const tcu::Sampler& sampler)
342 : m_type (TYPE_1D_ARRAY)
343 , m_sampler (sampler)
344 {
345 m_binding.tex1DArray = tex1DArray;
346 }
347
TextureBinding(const tcu::Texture2DArray * tex2DArray,const tcu::Sampler & sampler)348 TextureBinding::TextureBinding (const tcu::Texture2DArray* tex2DArray, const tcu::Sampler& sampler)
349 : m_type (TYPE_2D_ARRAY)
350 , m_sampler (sampler)
351 {
352 m_binding.tex2DArray = tex2DArray;
353 }
354
TextureBinding(const tcu::TextureCubeArray * texCubeArray,const tcu::Sampler & sampler)355 TextureBinding::TextureBinding (const tcu::TextureCubeArray* texCubeArray, const tcu::Sampler& sampler)
356 : m_type (TYPE_CUBE_ARRAY)
357 , m_sampler (sampler)
358 {
359 m_binding.texCubeArray = texCubeArray;
360 }
361
~TextureBinding(void)362 TextureBinding::~TextureBinding (void)
363 {
364 switch(m_type)
365 {
366 case TYPE_1D: delete m_binding.tex1D; break;
367 case TYPE_2D: delete m_binding.tex2D; break;
368 case TYPE_3D: delete m_binding.tex3D; break;
369 case TYPE_CUBE_MAP: delete m_binding.texCube; break;
370 case TYPE_1D_ARRAY: delete m_binding.tex1DArray; break;
371 case TYPE_2D_ARRAY: delete m_binding.tex2DArray; break;
372 case TYPE_CUBE_ARRAY: delete m_binding.texCubeArray; break;
373 default: break;
374 }
375 }
376
loadTexture2D(const tcu::Archive & archive,const char * filename)377 de::MovePtr<tcu::Texture2D> TextureBinding::loadTexture2D (const tcu::Archive& archive, const char* filename)
378 {
379 tcu::TextureLevel level;
380 tcu::ImageIO::loadImage(level, archive, filename);
381
382 TCU_CHECK_INTERNAL(level.getFormat() == tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8) ||
383 level.getFormat() == tcu::TextureFormat(tcu::TextureFormat::RGB, tcu::TextureFormat::UNORM_INT8));
384
385 // \todo [2015-10-08 elecro] for some reason we get better when using RGBA texture even in RGB case, this needs to be investigated
386 de::MovePtr<tcu::Texture2D> texture(new tcu::Texture2D(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), level.getWidth(), level.getHeight()));
387
388 // Fill level 0.
389 texture->allocLevel(0);
390 tcu::copy(texture->getLevel(0), level.getAccess());
391
392 return texture;
393 }
394
395 // ShaderEvalContext.
396
ShaderEvalContext(const QuadGrid & quadGrid)397 ShaderEvalContext::ShaderEvalContext (const QuadGrid& quadGrid)
398 : constCoords (quadGrid.getConstCoords())
399 , isDiscarded (false)
400 , m_quadGrid (quadGrid)
401 {
402 const std::vector<TextureBindingSp>& bindings = m_quadGrid.getTextures();
403 DE_ASSERT((int)bindings.size() <= MAX_TEXTURES);
404
405 // Fill in texture array.
406 for (int ndx = 0; ndx < (int)bindings.size(); ndx++)
407 {
408 const TextureBinding& binding = *bindings[ndx];
409
410 if (binding.getType() == TextureBinding::TYPE_NONE)
411 continue;
412
413 textures[ndx].sampler = binding.getSampler();
414
415 switch (binding.getType())
416 {
417 case TextureBinding::TYPE_1D: textures[ndx].tex1D = &binding.get1D(); break;
418 case TextureBinding::TYPE_2D: textures[ndx].tex2D = &binding.get2D(); break;
419 case TextureBinding::TYPE_3D: textures[ndx].tex3D = &binding.get3D(); break;
420 case TextureBinding::TYPE_CUBE_MAP: textures[ndx].texCube = &binding.getCube(); break;
421 case TextureBinding::TYPE_1D_ARRAY: textures[ndx].tex1DArray = &binding.get1DArray(); break;
422 case TextureBinding::TYPE_2D_ARRAY: textures[ndx].tex2DArray = &binding.get2DArray(); break;
423 case TextureBinding::TYPE_CUBE_ARRAY: textures[ndx].texCubeArray = &binding.getCubeArray(); break;
424 default:
425 TCU_THROW(InternalError, "Handling of texture binding type not implemented");
426 }
427 }
428 }
429
~ShaderEvalContext(void)430 ShaderEvalContext::~ShaderEvalContext (void)
431 {
432 }
433
reset(float sx,float sy)434 void ShaderEvalContext::reset (float sx, float sy)
435 {
436 // Clear old values
437 color = tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f);
438 isDiscarded = false;
439
440 // Compute coords
441 coords = m_quadGrid.getCoords(sx, sy);
442 unitCoords = m_quadGrid.getUnitCoords(sx, sy);
443
444 // Compute user attributes.
445 const int numAttribs = m_quadGrid.getNumUserAttribs();
446 DE_ASSERT(numAttribs <= MAX_USER_ATTRIBS);
447 for (int attribNdx = 0; attribNdx < numAttribs; attribNdx++)
448 in[attribNdx] = m_quadGrid.getUserAttrib(attribNdx, sx, sy);
449 }
450
texture2D(int unitNdx,const tcu::Vec2 & texCoords)451 tcu::Vec4 ShaderEvalContext::texture2D (int unitNdx, const tcu::Vec2& texCoords)
452 {
453 if (textures[unitNdx].tex2D)
454 return textures[unitNdx].tex2D->sample(textures[unitNdx].sampler, texCoords.x(), texCoords.y(), 0.0f);
455 else
456 return tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f);
457 }
458
459 // ShaderEvaluator.
460
ShaderEvaluator(void)461 ShaderEvaluator::ShaderEvaluator (void)
462 : m_evalFunc(DE_NULL)
463 {
464 }
465
ShaderEvaluator(ShaderEvalFunc evalFunc)466 ShaderEvaluator::ShaderEvaluator (ShaderEvalFunc evalFunc)
467 : m_evalFunc(evalFunc)
468 {
469 }
470
~ShaderEvaluator(void)471 ShaderEvaluator::~ShaderEvaluator (void)
472 {
473 }
474
evaluate(ShaderEvalContext & ctx) const475 void ShaderEvaluator::evaluate (ShaderEvalContext& ctx) const
476 {
477 DE_ASSERT(m_evalFunc);
478 m_evalFunc(ctx);
479 }
480
481 // UniformSetup.
482
UniformSetup(void)483 UniformSetup::UniformSetup (void)
484 : m_setupFunc(DE_NULL)
485 {
486 }
487
UniformSetup(UniformSetupFunc setupFunc)488 UniformSetup::UniformSetup (UniformSetupFunc setupFunc)
489 : m_setupFunc(setupFunc)
490 {
491 }
492
~UniformSetup(void)493 UniformSetup::~UniformSetup (void)
494 {
495 }
496
setup(ShaderRenderCaseInstance & instance,const tcu::Vec4 & constCoords) const497 void UniformSetup::setup (ShaderRenderCaseInstance& instance, const tcu::Vec4& constCoords) const
498 {
499 if (m_setupFunc)
500 m_setupFunc(instance, constCoords);
501 }
502
503 // ShaderRenderCase.
504
ShaderRenderCase(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const bool isVertexCase,const ShaderEvalFunc evalFunc,const UniformSetup * uniformSetup,const AttributeSetupFunc attribFunc)505 ShaderRenderCase::ShaderRenderCase (tcu::TestContext& testCtx,
506 const std::string& name,
507 const std::string& description,
508 const bool isVertexCase,
509 const ShaderEvalFunc evalFunc,
510 const UniformSetup* uniformSetup,
511 const AttributeSetupFunc attribFunc)
512 : vkt::TestCase (testCtx, name, description)
513 , m_isVertexCase (isVertexCase)
514 , m_evaluator (new ShaderEvaluator(evalFunc))
515 , m_uniformSetup (uniformSetup ? uniformSetup : new UniformSetup())
516 , m_attribFunc (attribFunc)
517 {}
518
ShaderRenderCase(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const bool isVertexCase,const ShaderEvaluator * evaluator,const UniformSetup * uniformSetup,const AttributeSetupFunc attribFunc)519 ShaderRenderCase::ShaderRenderCase (tcu::TestContext& testCtx,
520 const std::string& name,
521 const std::string& description,
522 const bool isVertexCase,
523 const ShaderEvaluator* evaluator,
524 const UniformSetup* uniformSetup,
525 const AttributeSetupFunc attribFunc)
526 : vkt::TestCase (testCtx, name, description)
527 , m_isVertexCase (isVertexCase)
528 , m_evaluator (evaluator)
529 , m_uniformSetup (uniformSetup ? uniformSetup : new UniformSetup())
530 , m_attribFunc (attribFunc)
531 {}
532
~ShaderRenderCase(void)533 ShaderRenderCase::~ShaderRenderCase (void)
534 {
535 }
536
initPrograms(vk::SourceCollections & programCollection) const537 void ShaderRenderCase::initPrograms (vk::SourceCollections& programCollection) const
538 {
539 programCollection.glslSources.add("vert") << glu::VertexSource(m_vertShaderSource);
540 programCollection.glslSources.add("frag") << glu::FragmentSource(m_fragShaderSource);
541 }
542
createInstance(Context & context) const543 TestInstance* ShaderRenderCase::createInstance (Context& context) const
544 {
545 DE_ASSERT(m_evaluator != DE_NULL);
546 DE_ASSERT(m_uniformSetup != DE_NULL);
547 return new ShaderRenderCaseInstance(context, m_isVertexCase, *m_evaluator, *m_uniformSetup, m_attribFunc);
548 }
549
550 // ShaderRenderCaseInstance.
551
ShaderRenderCaseInstance(Context & context)552 ShaderRenderCaseInstance::ShaderRenderCaseInstance (Context& context)
553 : vkt::TestInstance (context)
554 , m_imageBackingMode (IMAGE_BACKING_MODE_REGULAR)
555 , m_quadGridSize (static_cast<deUint32>(GRID_SIZE_DEFAULT_FRAGMENT))
556 , m_memAlloc (getAllocator())
557 , m_clearColor (DEFAULT_CLEAR_COLOR)
558 , m_isVertexCase (false)
559 , m_vertexShaderName ("vert")
560 , m_fragmentShaderName ("frag")
561 , m_renderSize (MAX_RENDER_WIDTH, MAX_RENDER_HEIGHT)
562 , m_colorFormat (VK_FORMAT_R8G8B8A8_UNORM)
563 , m_evaluator (DE_NULL)
564 , m_uniformSetup (DE_NULL)
565 , m_attribFunc (DE_NULL)
566 , m_sampleCount (VK_SAMPLE_COUNT_1_BIT)
567 , m_fuzzyCompare (true)
568 {
569 }
570
571
ShaderRenderCaseInstance(Context & context,const bool isVertexCase,const ShaderEvaluator & evaluator,const UniformSetup & uniformSetup,const AttributeSetupFunc attribFunc,const ImageBackingMode imageBackingMode,const deUint32 gridSize,const bool fuzzyCompare)572 ShaderRenderCaseInstance::ShaderRenderCaseInstance (Context& context,
573 const bool isVertexCase,
574 const ShaderEvaluator& evaluator,
575 const UniformSetup& uniformSetup,
576 const AttributeSetupFunc attribFunc,
577 const ImageBackingMode imageBackingMode,
578 const deUint32 gridSize,
579 const bool fuzzyCompare)
580 : vkt::TestInstance (context)
581 , m_imageBackingMode (imageBackingMode)
582 , m_quadGridSize (gridSize == static_cast<deUint32>(GRID_SIZE_DEFAULTS)
583 ? (isVertexCase
584 ? static_cast<deUint32>(GRID_SIZE_DEFAULT_VERTEX)
585 : static_cast<deUint32>(GRID_SIZE_DEFAULT_FRAGMENT))
586 : gridSize)
587 , m_memAlloc (getAllocator())
588 , m_clearColor (DEFAULT_CLEAR_COLOR)
589 , m_isVertexCase (isVertexCase)
590 , m_vertexShaderName ("vert")
591 , m_fragmentShaderName ("frag")
592 , m_renderSize (MAX_RENDER_WIDTH, MAX_RENDER_HEIGHT)
593 , m_colorFormat (VK_FORMAT_R8G8B8A8_UNORM)
594 , m_evaluator (&evaluator)
595 , m_uniformSetup (&uniformSetup)
596 , m_attribFunc (attribFunc)
597 , m_sampleCount (VK_SAMPLE_COUNT_1_BIT)
598 , m_fuzzyCompare (fuzzyCompare)
599 {
600 }
601
ShaderRenderCaseInstance(Context & context,const bool isVertexCase,const ShaderEvaluator * evaluator,const UniformSetup * uniformSetup,const AttributeSetupFunc attribFunc,const ImageBackingMode imageBackingMode,const deUint32 gridSize)602 ShaderRenderCaseInstance::ShaderRenderCaseInstance (Context& context,
603 const bool isVertexCase,
604 const ShaderEvaluator* evaluator,
605 const UniformSetup* uniformSetup,
606 const AttributeSetupFunc attribFunc,
607 const ImageBackingMode imageBackingMode,
608 const deUint32 gridSize)
609 : vkt::TestInstance (context)
610 , m_imageBackingMode (imageBackingMode)
611 , m_quadGridSize (gridSize == static_cast<deUint32>(GRID_SIZE_DEFAULTS)
612 ? (isVertexCase
613 ? static_cast<deUint32>(GRID_SIZE_DEFAULT_VERTEX)
614 : static_cast<deUint32>(GRID_SIZE_DEFAULT_FRAGMENT))
615 : gridSize)
616 , m_memAlloc (getAllocator())
617 , m_clearColor (DEFAULT_CLEAR_COLOR)
618 , m_isVertexCase (isVertexCase)
619 , m_vertexShaderName ("vert")
620 , m_fragmentShaderName ("frag")
621 , m_renderSize (MAX_RENDER_WIDTH, MAX_RENDER_HEIGHT)
622 , m_colorFormat (VK_FORMAT_R8G8B8A8_UNORM)
623 , m_evaluator (evaluator)
624 , m_uniformSetup (uniformSetup)
625 , m_attribFunc (attribFunc)
626 , m_sampleCount (VK_SAMPLE_COUNT_1_BIT)
627 , m_fuzzyCompare (false)
628 {
629 }
630
getAllocator(void) const631 vk::Allocator& ShaderRenderCaseInstance::getAllocator (void) const
632 {
633 return m_context.getDefaultAllocator();
634 }
635
~ShaderRenderCaseInstance(void)636 ShaderRenderCaseInstance::~ShaderRenderCaseInstance (void)
637 {
638 }
639
getDevice(void) const640 VkDevice ShaderRenderCaseInstance::getDevice (void) const
641 {
642 return m_context.getDevice();
643 }
644
getUniversalQueueFamilyIndex(void) const645 deUint32 ShaderRenderCaseInstance::getUniversalQueueFamilyIndex (void) const
646 {
647 return m_context.getUniversalQueueFamilyIndex();
648 }
649
getSparseQueueFamilyIndex(void) const650 deUint32 ShaderRenderCaseInstance::getSparseQueueFamilyIndex (void) const
651 {
652 return m_context.getSparseQueueFamilyIndex();
653 }
654
getDeviceInterface(void) const655 const DeviceInterface& ShaderRenderCaseInstance::getDeviceInterface (void) const
656 {
657 return m_context.getDeviceInterface();
658 }
659
getUniversalQueue(void) const660 VkQueue ShaderRenderCaseInstance::getUniversalQueue (void) const
661 {
662 return m_context.getUniversalQueue();
663 }
664
getSparseQueue(void) const665 VkQueue ShaderRenderCaseInstance::getSparseQueue (void) const
666 {
667 return m_context.getSparseQueue();
668 }
669
getPhysicalDevice(void) const670 VkPhysicalDevice ShaderRenderCaseInstance::getPhysicalDevice (void) const
671 {
672 return m_context.getPhysicalDevice();
673 }
674
getInstanceInterface(void) const675 const InstanceInterface& ShaderRenderCaseInstance::getInstanceInterface (void) const
676 {
677 return m_context.getInstanceInterface();
678 }
679
iterate(void)680 tcu::TestStatus ShaderRenderCaseInstance::iterate (void)
681 {
682 setup();
683
684 // Create quad grid.
685 const tcu::UVec2 viewportSize = getViewportSize();
686 const int width = viewportSize.x();
687 const int height = viewportSize.y();
688
689 m_quadGrid = de::MovePtr<QuadGrid>(new QuadGrid(m_quadGridSize, width, height, getDefaultConstCoords(), m_userAttribTransforms, m_textures));
690
691 // Render result.
692 tcu::Surface resImage (width, height);
693
694 render(m_quadGrid->getNumVertices(), m_quadGrid->getNumTriangles(), m_quadGrid->getIndices(), m_quadGrid->getConstCoords());
695 tcu::copy(resImage.getAccess(), m_resultImage.getAccess());
696
697 // Compute reference.
698 tcu::Surface refImage (width, height);
699 if (m_isVertexCase)
700 computeVertexReference(refImage, *m_quadGrid);
701 else
702 computeFragmentReference(refImage, *m_quadGrid);
703
704 // Compare.
705 const bool compareOk = compareImages(resImage, refImage, 0.2f);
706
707 if (compareOk)
708 return tcu::TestStatus::pass("Result image matches reference");
709 else
710 return tcu::TestStatus::fail("Image mismatch");
711 }
712
setup(void)713 void ShaderRenderCaseInstance::setup (void)
714 {
715 m_resultImage = tcu::TextureLevel();
716 m_descriptorSetLayoutBuilder = de::MovePtr<DescriptorSetLayoutBuilder> (new DescriptorSetLayoutBuilder());
717 m_descriptorPoolBuilder = de::MovePtr<DescriptorPoolBuilder> (new DescriptorPoolBuilder());
718 m_descriptorSetUpdateBuilder = de::MovePtr<DescriptorSetUpdateBuilder> (new DescriptorSetUpdateBuilder());
719
720 m_uniformInfos.clear();
721 m_vertexBindingDescription.clear();
722 m_vertexAttributeDescription.clear();
723 m_vertexBuffers.clear();
724 m_vertexBufferAllocs.clear();
725 m_pushConstantRanges.clear();
726 }
727
setupUniformData(deUint32 bindingLocation,size_t size,const void * dataPtr)728 void ShaderRenderCaseInstance::setupUniformData (deUint32 bindingLocation, size_t size, const void* dataPtr)
729 {
730 const VkDevice vkDevice = getDevice();
731 const DeviceInterface& vk = getDeviceInterface();
732 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
733
734 const VkBufferCreateInfo uniformBufferParams =
735 {
736 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
737 DE_NULL, // const void* pNext;
738 0u, // VkBufferCreateFlags flags;
739 size, // VkDeviceSize size;
740 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, // VkBufferUsageFlags usage;
741 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
742 1u, // deUint32 queueFamilyCount;
743 &queueFamilyIndex // const deUint32* pQueueFamilyIndices;
744 };
745
746 Move<VkBuffer> buffer = createBuffer(vk, vkDevice, &uniformBufferParams);
747 de::MovePtr<Allocation> alloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), MemoryRequirement::HostVisible);
748 VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, alloc->getMemory(), alloc->getOffset()));
749
750 deMemcpy(alloc->getHostPtr(), dataPtr, size);
751 flushAlloc(vk, vkDevice, *alloc);
752
753 de::MovePtr<BufferUniform> uniformInfo(new BufferUniform());
754 uniformInfo->type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
755 uniformInfo->descriptor = makeDescriptorBufferInfo(*buffer, 0u, size);
756 uniformInfo->location = bindingLocation;
757 uniformInfo->buffer = VkBufferSp(new vk::Unique<VkBuffer>(buffer));
758 uniformInfo->alloc = AllocationSp(alloc.release());
759
760 m_uniformInfos.push_back(UniformInfoSp(new de::UniquePtr<UniformInfo>(uniformInfo)));
761 }
762
addUniform(deUint32 bindingLocation,vk::VkDescriptorType descriptorType,size_t dataSize,const void * data)763 void ShaderRenderCaseInstance::addUniform (deUint32 bindingLocation, vk::VkDescriptorType descriptorType, size_t dataSize, const void* data)
764 {
765 m_descriptorSetLayoutBuilder->addSingleBinding(descriptorType, vk::VK_SHADER_STAGE_ALL);
766 m_descriptorPoolBuilder->addType(descriptorType);
767
768 setupUniformData(bindingLocation, dataSize, data);
769 }
770
addAttribute(deUint32 bindingLocation,vk::VkFormat format,deUint32 sizePerElement,deUint32 count,const void * dataPtr)771 void ShaderRenderCaseInstance::addAttribute (deUint32 bindingLocation,
772 vk::VkFormat format,
773 deUint32 sizePerElement,
774 deUint32 count,
775 const void* dataPtr)
776 {
777 // Portability requires stride to be multiply of minVertexInputBindingStrideAlignment
778 // this value is usually 4 and current tests meet this requirement but
779 // if this changes in future then this limit should be verified in checkSupport
780 if (m_context.isDeviceFunctionalitySupported("VK_KHR_portability_subset") &&
781 ((sizePerElement % m_context.getPortabilitySubsetProperties().minVertexInputBindingStrideAlignment) != 0))
782 {
783 DE_FATAL("stride is not multiply of minVertexInputBindingStrideAlignment");
784 }
785
786 // Add binding specification
787 const deUint32 binding = (deUint32)m_vertexBindingDescription.size();
788 const VkVertexInputBindingDescription bindingDescription =
789 {
790 binding, // deUint32 binding;
791 sizePerElement, // deUint32 stride;
792 VK_VERTEX_INPUT_RATE_VERTEX // VkVertexInputRate stepRate;
793 };
794
795 m_vertexBindingDescription.push_back(bindingDescription);
796
797 // Add location and format specification
798 const VkVertexInputAttributeDescription attributeDescription =
799 {
800 bindingLocation, // deUint32 location;
801 binding, // deUint32 binding;
802 format, // VkFormat format;
803 0u, // deUint32 offset;
804 };
805
806 m_vertexAttributeDescription.push_back(attributeDescription);
807
808 // Upload data to buffer
809 const VkDevice vkDevice = getDevice();
810 const DeviceInterface& vk = getDeviceInterface();
811 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
812
813 const VkDeviceSize inputSize = sizePerElement * count;
814 const VkBufferCreateInfo vertexBufferParams =
815 {
816 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
817 DE_NULL, // const void* pNext;
818 0u, // VkBufferCreateFlags flags;
819 inputSize, // VkDeviceSize size;
820 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, // VkBufferUsageFlags usage;
821 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
822 1u, // deUint32 queueFamilyCount;
823 &queueFamilyIndex // const deUint32* pQueueFamilyIndices;
824 };
825
826 Move<VkBuffer> buffer = createBuffer(vk, vkDevice, &vertexBufferParams);
827 de::MovePtr<vk::Allocation> alloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), MemoryRequirement::HostVisible);
828 VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, alloc->getMemory(), alloc->getOffset()));
829
830 deMemcpy(alloc->getHostPtr(), dataPtr, (size_t)inputSize);
831 flushAlloc(vk, vkDevice, *alloc);
832
833 m_vertexBuffers.push_back(VkBufferSp(new vk::Unique<VkBuffer>(buffer)));
834 m_vertexBufferAllocs.push_back(AllocationSp(alloc.release()));
835 }
836
useAttribute(deUint32 bindingLocation,BaseAttributeType type)837 void ShaderRenderCaseInstance::useAttribute (deUint32 bindingLocation, BaseAttributeType type)
838 {
839 const EnabledBaseAttribute attribute =
840 {
841 bindingLocation, // deUint32 location;
842 type // BaseAttributeType type;
843 };
844 m_enabledBaseAttributes.push_back(attribute);
845 }
846
setupUniforms(const tcu::Vec4 & constCoords)847 void ShaderRenderCaseInstance::setupUniforms (const tcu::Vec4& constCoords)
848 {
849 if (m_uniformSetup)
850 m_uniformSetup->setup(*this, constCoords);
851 }
852
useUniform(deUint32 bindingLocation,BaseUniformType type)853 void ShaderRenderCaseInstance::useUniform (deUint32 bindingLocation, BaseUniformType type)
854 {
855 #define UNIFORM_CASE(type, value) case type: addUniform(bindingLocation, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, value); break
856
857 switch(type)
858 {
859 // Bool
860 UNIFORM_CASE(UB_FALSE, 0);
861 UNIFORM_CASE(UB_TRUE, 1);
862
863 // BVec4
864 UNIFORM_CASE(UB4_FALSE, tcu::Vec4(0));
865 UNIFORM_CASE(UB4_TRUE, tcu::Vec4(1));
866
867 // Integer
868 UNIFORM_CASE(UI_ZERO, 0);
869 UNIFORM_CASE(UI_ONE, 1);
870 UNIFORM_CASE(UI_TWO, 2);
871 UNIFORM_CASE(UI_THREE, 3);
872 UNIFORM_CASE(UI_FOUR, 4);
873 UNIFORM_CASE(UI_FIVE, 5);
874 UNIFORM_CASE(UI_SIX, 6);
875 UNIFORM_CASE(UI_SEVEN, 7);
876 UNIFORM_CASE(UI_EIGHT, 8);
877 UNIFORM_CASE(UI_ONEHUNDREDONE, 101);
878
879 // IVec2
880 UNIFORM_CASE(UI2_MINUS_ONE, tcu::IVec2(-1));
881 UNIFORM_CASE(UI2_ZERO, tcu::IVec2(0));
882 UNIFORM_CASE(UI2_ONE, tcu::IVec2(1));
883 UNIFORM_CASE(UI2_TWO, tcu::IVec2(2));
884 UNIFORM_CASE(UI2_THREE, tcu::IVec2(3));
885 UNIFORM_CASE(UI2_FOUR, tcu::IVec2(4));
886 UNIFORM_CASE(UI2_FIVE, tcu::IVec2(5));
887
888 // IVec3
889 UNIFORM_CASE(UI3_MINUS_ONE, tcu::IVec3(-1));
890 UNIFORM_CASE(UI3_ZERO, tcu::IVec3(0));
891 UNIFORM_CASE(UI3_ONE, tcu::IVec3(1));
892 UNIFORM_CASE(UI3_TWO, tcu::IVec3(2));
893 UNIFORM_CASE(UI3_THREE, tcu::IVec3(3));
894 UNIFORM_CASE(UI3_FOUR, tcu::IVec3(4));
895 UNIFORM_CASE(UI3_FIVE, tcu::IVec3(5));
896
897 // IVec4
898 UNIFORM_CASE(UI4_MINUS_ONE, tcu::IVec4(-1));
899 UNIFORM_CASE(UI4_ZERO, tcu::IVec4(0));
900 UNIFORM_CASE(UI4_ONE, tcu::IVec4(1));
901 UNIFORM_CASE(UI4_TWO, tcu::IVec4(2));
902 UNIFORM_CASE(UI4_THREE, tcu::IVec4(3));
903 UNIFORM_CASE(UI4_FOUR, tcu::IVec4(4));
904 UNIFORM_CASE(UI4_FIVE, tcu::IVec4(5));
905
906 // Float
907 UNIFORM_CASE(UF_ZERO, 0.0f);
908 UNIFORM_CASE(UF_ONE, 1.0f);
909 UNIFORM_CASE(UF_TWO, 2.0f);
910 UNIFORM_CASE(UF_THREE, 3.0f);
911 UNIFORM_CASE(UF_FOUR, 4.0f);
912 UNIFORM_CASE(UF_FIVE, 5.0f);
913 UNIFORM_CASE(UF_SIX, 6.0f);
914 UNIFORM_CASE(UF_SEVEN, 7.0f);
915 UNIFORM_CASE(UF_EIGHT, 8.0f);
916
917 UNIFORM_CASE(UF_HALF, 1.0f / 2.0f);
918 UNIFORM_CASE(UF_THIRD, 1.0f / 3.0f);
919 UNIFORM_CASE(UF_FOURTH, 1.0f / 4.0f);
920 UNIFORM_CASE(UF_FIFTH, 1.0f / 5.0f);
921 UNIFORM_CASE(UF_SIXTH, 1.0f / 6.0f);
922 UNIFORM_CASE(UF_SEVENTH, 1.0f / 7.0f);
923 UNIFORM_CASE(UF_EIGHTH, 1.0f / 8.0f);
924
925 // Vec2
926 UNIFORM_CASE(UV2_MINUS_ONE, tcu::Vec2(-1.0f));
927 UNIFORM_CASE(UV2_ZERO, tcu::Vec2(0.0f));
928 UNIFORM_CASE(UV2_ONE, tcu::Vec2(1.0f));
929 UNIFORM_CASE(UV2_TWO, tcu::Vec2(2.0f));
930 UNIFORM_CASE(UV2_THREE, tcu::Vec2(3.0f));
931
932 UNIFORM_CASE(UV2_HALF, tcu::Vec2(1.0f / 2.0f));
933
934 // Vec3
935 UNIFORM_CASE(UV3_MINUS_ONE, tcu::Vec3(-1.0f));
936 UNIFORM_CASE(UV3_ZERO, tcu::Vec3(0.0f));
937 UNIFORM_CASE(UV3_ONE, tcu::Vec3(1.0f));
938 UNIFORM_CASE(UV3_TWO, tcu::Vec3(2.0f));
939 UNIFORM_CASE(UV3_THREE, tcu::Vec3(3.0f));
940
941 UNIFORM_CASE(UV3_HALF, tcu::Vec3(1.0f / 2.0f));
942
943 // Vec4
944 UNIFORM_CASE(UV4_MINUS_ONE, tcu::Vec4(-1.0f));
945 UNIFORM_CASE(UV4_ZERO, tcu::Vec4(0.0f));
946 UNIFORM_CASE(UV4_ONE, tcu::Vec4(1.0f));
947 UNIFORM_CASE(UV4_TWO, tcu::Vec4(2.0f));
948 UNIFORM_CASE(UV4_THREE, tcu::Vec4(3.0f));
949
950 UNIFORM_CASE(UV4_HALF, tcu::Vec4(1.0f / 2.0f));
951
952 UNIFORM_CASE(UV4_BLACK, tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
953 UNIFORM_CASE(UV4_GRAY, tcu::Vec4(0.5f, 0.5f, 0.5f, 1.0f));
954 UNIFORM_CASE(UV4_WHITE, tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
955
956 default:
957 m_context.getTestContext().getLog() << tcu::TestLog::Message << "Unknown Uniform type: " << type << tcu::TestLog::EndMessage;
958 break;
959 }
960
961 #undef UNIFORM_CASE
962 }
963
getViewportSize(void) const964 const tcu::UVec2 ShaderRenderCaseInstance::getViewportSize (void) const
965 {
966 return tcu::UVec2(de::min(m_renderSize.x(), MAX_RENDER_WIDTH),
967 de::min(m_renderSize.y(), MAX_RENDER_HEIGHT));
968 }
969
setSampleCount(VkSampleCountFlagBits sampleCount)970 void ShaderRenderCaseInstance::setSampleCount (VkSampleCountFlagBits sampleCount)
971 {
972 m_sampleCount = sampleCount;
973 }
974
isMultiSampling(void) const975 bool ShaderRenderCaseInstance::isMultiSampling (void) const
976 {
977 return m_sampleCount != VK_SAMPLE_COUNT_1_BIT;
978 }
979
uploadImage(const tcu::TextureFormat & texFormat,const TextureData & textureData,const tcu::Sampler & refSampler,deUint32 mipLevels,deUint32 arrayLayers,VkImage destImage)980 void ShaderRenderCaseInstance::uploadImage (const tcu::TextureFormat& texFormat,
981 const TextureData& textureData,
982 const tcu::Sampler& refSampler,
983 deUint32 mipLevels,
984 deUint32 arrayLayers,
985 VkImage destImage)
986 {
987 const VkDevice vkDevice = getDevice();
988 const DeviceInterface& vk = getDeviceInterface();
989 const VkQueue queue = getUniversalQueue();
990 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
991
992 const bool isShadowSampler = refSampler.compare != tcu::Sampler::COMPAREMODE_NONE;
993 const VkImageAspectFlags aspectMask = isShadowSampler ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
994 deUint32 bufferSize = 0u;
995 Move<VkBuffer> buffer;
996 de::MovePtr<Allocation> bufferAlloc;
997 Move<VkCommandPool> cmdPool;
998 Move<VkCommandBuffer> cmdBuffer;
999 std::vector<VkBufferImageCopy> copyRegions;
1000 std::vector<deUint32> offsetMultiples;
1001
1002 offsetMultiples.push_back(4u);
1003 offsetMultiples.push_back(texFormat.getPixelSize());
1004
1005 // Calculate buffer size
1006 for (TextureData::const_iterator mit = textureData.begin(); mit != textureData.end(); ++mit)
1007 {
1008 for (TextureLayerData::const_iterator lit = mit->begin(); lit != mit->end(); ++lit)
1009 {
1010 const tcu::ConstPixelBufferAccess& access = *lit;
1011
1012 bufferSize = getNextMultiple(offsetMultiples, bufferSize);
1013 bufferSize += access.getWidth() * access.getHeight() * access.getDepth() * access.getFormat().getPixelSize();
1014 }
1015 }
1016
1017 // Create source buffer
1018 {
1019 const VkBufferCreateInfo bufferParams =
1020 {
1021 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
1022 DE_NULL, // const void* pNext;
1023 0u, // VkBufferCreateFlags flags;
1024 bufferSize, // VkDeviceSize size;
1025 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, // VkBufferUsageFlags usage;
1026 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
1027 0u, // deUint32 queueFamilyIndexCount;
1028 DE_NULL, // const deUint32* pQueueFamilyIndices;
1029 };
1030
1031 buffer = createBuffer(vk, vkDevice, &bufferParams);
1032 bufferAlloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), MemoryRequirement::HostVisible);
1033 VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, bufferAlloc->getMemory(), bufferAlloc->getOffset()));
1034 }
1035
1036 // Get copy regions and write buffer data
1037 {
1038 deUint32 layerDataOffset = 0;
1039 deUint8* destPtr = (deUint8*)bufferAlloc->getHostPtr();
1040
1041 for (size_t levelNdx = 0; levelNdx < textureData.size(); levelNdx++)
1042 {
1043 const TextureLayerData& layerData = textureData[levelNdx];
1044
1045 for (size_t layerNdx = 0; layerNdx < layerData.size(); layerNdx++)
1046 {
1047 layerDataOffset = getNextMultiple(offsetMultiples, layerDataOffset);
1048
1049 const tcu::ConstPixelBufferAccess& access = layerData[layerNdx];
1050 const tcu::PixelBufferAccess destAccess (access.getFormat(), access.getSize(), destPtr + layerDataOffset);
1051
1052 const VkBufferImageCopy layerRegion =
1053 {
1054 layerDataOffset, // VkDeviceSize bufferOffset;
1055 (deUint32)access.getWidth(), // deUint32 bufferRowLength;
1056 (deUint32)access.getHeight(), // deUint32 bufferImageHeight;
1057 { // VkImageSubresourceLayers imageSubresource;
1058 aspectMask, // VkImageAspectFlags aspectMask;
1059 (deUint32)levelNdx, // uint32_t mipLevel;
1060 (deUint32)layerNdx, // uint32_t baseArrayLayer;
1061 1u // uint32_t layerCount;
1062 },
1063 { 0u, 0u, 0u }, // VkOffset3D imageOffset;
1064 { // VkExtent3D imageExtent;
1065 (deUint32)access.getWidth(),
1066 (deUint32)access.getHeight(),
1067 (deUint32)access.getDepth()
1068 }
1069 };
1070
1071 copyRegions.push_back(layerRegion);
1072 tcu::copy(destAccess, access);
1073
1074 layerDataOffset += access.getWidth() * access.getHeight() * access.getDepth() * access.getFormat().getPixelSize();
1075 }
1076 }
1077 }
1078
1079 flushAlloc(vk, vkDevice, *bufferAlloc);
1080
1081 copyBufferToImage(vk, vkDevice, queue, queueFamilyIndex, *buffer, bufferSize, copyRegions, DE_NULL, aspectMask, mipLevels, arrayLayers, destImage);
1082 }
1083
clearImage(const tcu::Sampler & refSampler,deUint32 mipLevels,deUint32 arrayLayers,VkImage destImage)1084 void ShaderRenderCaseInstance::clearImage (const tcu::Sampler& refSampler,
1085 deUint32 mipLevels,
1086 deUint32 arrayLayers,
1087 VkImage destImage)
1088 {
1089 const VkDevice vkDevice = m_context.getDevice();
1090 const DeviceInterface& vk = m_context.getDeviceInterface();
1091 const VkQueue queue = m_context.getUniversalQueue();
1092 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
1093
1094 const bool isShadowSampler = refSampler.compare != tcu::Sampler::COMPAREMODE_NONE;
1095 const VkImageAspectFlags aspectMask = isShadowSampler ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
1096 Move<VkCommandPool> cmdPool;
1097 Move<VkCommandBuffer> cmdBuffer;
1098
1099 VkClearValue clearValue;
1100 deMemset(&clearValue, 0, sizeof(clearValue));
1101
1102
1103 // Create command pool and buffer
1104 cmdPool = createCommandPool(vk, vkDevice, VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, queueFamilyIndex);
1105 cmdBuffer = allocateCommandBuffer(vk, vkDevice, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1106
1107 const VkImageMemoryBarrier preImageBarrier =
1108 {
1109 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType;
1110 DE_NULL, // const void* pNext;
1111 0u, // VkAccessFlags srcAccessMask;
1112 VK_ACCESS_TRANSFER_WRITE_BIT, // VkAccessFlags dstAccessMask;
1113 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout oldLayout;
1114 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // VkImageLayout newLayout;
1115 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
1116 VK_QUEUE_FAMILY_IGNORED, // deUint32 dstQueueFamilyIndex;
1117 destImage, // VkImage image;
1118 { // VkImageSubresourceRange subresourceRange;
1119 aspectMask, // VkImageAspect aspect;
1120 0u, // deUint32 baseMipLevel;
1121 mipLevels, // deUint32 mipLevels;
1122 0u, // deUint32 baseArraySlice;
1123 arrayLayers // deUint32 arraySize;
1124 }
1125 };
1126
1127 const VkImageMemoryBarrier postImageBarrier =
1128 {
1129 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType;
1130 DE_NULL, // const void* pNext;
1131 VK_ACCESS_TRANSFER_WRITE_BIT, // VkAccessFlags srcAccessMask;
1132 VK_ACCESS_SHADER_READ_BIT, // VkAccessFlags dstAccessMask;
1133 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // VkImageLayout oldLayout;
1134 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // VkImageLayout newLayout;
1135 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
1136 VK_QUEUE_FAMILY_IGNORED, // deUint32 dstQueueFamilyIndex;
1137 destImage, // VkImage image;
1138 { // VkImageSubresourceRange subresourceRange;
1139 aspectMask, // VkImageAspect aspect;
1140 0u, // deUint32 baseMipLevel;
1141 mipLevels, // deUint32 mipLevels;
1142 0u, // deUint32 baseArraySlice;
1143 arrayLayers // deUint32 arraySize;
1144 }
1145 };
1146
1147 const VkImageSubresourceRange clearRange =
1148 {
1149 aspectMask, // VkImageAspectFlags aspectMask;
1150 0u, // deUint32 baseMipLevel;
1151 mipLevels, // deUint32 levelCount;
1152 0u, // deUint32 baseArrayLayer;
1153 arrayLayers // deUint32 layerCount;
1154 };
1155
1156 // Copy buffer to image
1157 beginCommandBuffer(vk, *cmdBuffer);
1158 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 0, (const VkBufferMemoryBarrier*)DE_NULL, 1, &preImageBarrier);
1159 if (aspectMask == VK_IMAGE_ASPECT_COLOR_BIT)
1160 {
1161 vk.cmdClearColorImage(*cmdBuffer, destImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearValue.color, 1, &clearRange);
1162 }
1163 else
1164 {
1165 vk.cmdClearDepthStencilImage(*cmdBuffer, destImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearValue.depthStencil, 1, &clearRange);
1166 }
1167 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 0, (const VkBufferMemoryBarrier*)DE_NULL, 1, &postImageBarrier);
1168 endCommandBuffer(vk, *cmdBuffer);
1169
1170 submitCommandsAndWait(vk, vkDevice, queue, cmdBuffer.get());
1171 }
1172
mipLevelExtents(const VkExtent3D & baseExtents,const deUint32 mipLevel)1173 VkExtent3D mipLevelExtents (const VkExtent3D& baseExtents, const deUint32 mipLevel)
1174 {
1175 VkExtent3D result;
1176
1177 result.width = std::max(baseExtents.width >> mipLevel, 1u);
1178 result.height = std::max(baseExtents.height >> mipLevel, 1u);
1179 result.depth = std::max(baseExtents.depth >> mipLevel, 1u);
1180
1181 return result;
1182 }
1183
alignedDivide(const VkExtent3D & extent,const VkExtent3D & divisor)1184 tcu::UVec3 alignedDivide (const VkExtent3D& extent, const VkExtent3D& divisor)
1185 {
1186 tcu::UVec3 result;
1187
1188 result.x() = extent.width / divisor.width + ((extent.width % divisor.width != 0) ? 1u : 0u);
1189 result.y() = extent.height / divisor.height + ((extent.height % divisor.height != 0) ? 1u : 0u);
1190 result.z() = extent.depth / divisor.depth + ((extent.depth % divisor.depth != 0) ? 1u : 0u);
1191
1192 return result;
1193 }
1194
isImageSizeSupported(const VkImageType imageType,const tcu::UVec3 & imageSize,const vk::VkPhysicalDeviceLimits & limits)1195 bool isImageSizeSupported (const VkImageType imageType, const tcu::UVec3& imageSize, const vk::VkPhysicalDeviceLimits& limits)
1196 {
1197 switch (imageType)
1198 {
1199 case VK_IMAGE_TYPE_1D:
1200 return (imageSize.x() <= limits.maxImageDimension1D
1201 && imageSize.y() == 1
1202 && imageSize.z() == 1);
1203 case VK_IMAGE_TYPE_2D:
1204 return (imageSize.x() <= limits.maxImageDimension2D
1205 && imageSize.y() <= limits.maxImageDimension2D
1206 && imageSize.z() == 1);
1207 case VK_IMAGE_TYPE_3D:
1208 return (imageSize.x() <= limits.maxImageDimension3D
1209 && imageSize.y() <= limits.maxImageDimension3D
1210 && imageSize.z() <= limits.maxImageDimension3D);
1211 default:
1212 DE_FATAL("Unknown image type");
1213 return false;
1214 }
1215 }
1216
checkSparseSupport(const VkImageCreateInfo & imageInfo) const1217 void ShaderRenderCaseInstance::checkSparseSupport (const VkImageCreateInfo& imageInfo) const
1218 {
1219 const InstanceInterface& instance = getInstanceInterface();
1220 const VkPhysicalDevice physicalDevice = getPhysicalDevice();
1221 const VkPhysicalDeviceFeatures deviceFeatures = getPhysicalDeviceFeatures(instance, physicalDevice);
1222
1223 const std::vector<VkSparseImageFormatProperties> sparseImageFormatPropVec = getPhysicalDeviceSparseImageFormatProperties(
1224 instance, physicalDevice, imageInfo.format, imageInfo.imageType, imageInfo.samples, imageInfo.usage, imageInfo.tiling);
1225
1226 if (!deviceFeatures.shaderResourceResidency)
1227 TCU_THROW(NotSupportedError, "Required feature: shaderResourceResidency.");
1228
1229 if (!deviceFeatures.sparseBinding)
1230 TCU_THROW(NotSupportedError, "Required feature: sparseBinding.");
1231
1232 if (imageInfo.imageType == VK_IMAGE_TYPE_2D && !deviceFeatures.sparseResidencyImage2D)
1233 TCU_THROW(NotSupportedError, "Required feature: sparseResidencyImage2D.");
1234
1235 if (imageInfo.imageType == VK_IMAGE_TYPE_3D && !deviceFeatures.sparseResidencyImage3D)
1236 TCU_THROW(NotSupportedError, "Required feature: sparseResidencyImage3D.");
1237
1238 if (sparseImageFormatPropVec.size() == 0)
1239 TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
1240 }
1241
uploadSparseImage(const tcu::TextureFormat & texFormat,const TextureData & textureData,const tcu::Sampler & refSampler,const deUint32 mipLevels,const deUint32 arrayLayers,const VkImage sparseImage,const VkImageCreateInfo & imageCreateInfo,const tcu::UVec3 texSize)1242 void ShaderRenderCaseInstance::uploadSparseImage (const tcu::TextureFormat& texFormat,
1243 const TextureData& textureData,
1244 const tcu::Sampler& refSampler,
1245 const deUint32 mipLevels,
1246 const deUint32 arrayLayers,
1247 const VkImage sparseImage,
1248 const VkImageCreateInfo& imageCreateInfo,
1249 const tcu::UVec3 texSize)
1250 {
1251 const VkDevice vkDevice = getDevice();
1252 const DeviceInterface& vk = getDeviceInterface();
1253 const VkPhysicalDevice physicalDevice = getPhysicalDevice();
1254 const VkQueue queue = getUniversalQueue();
1255 const VkQueue sparseQueue = getSparseQueue();
1256 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
1257 const InstanceInterface& instance = getInstanceInterface();
1258 const VkPhysicalDeviceProperties deviceProperties = getPhysicalDeviceProperties(instance, physicalDevice);
1259 const bool isShadowSampler = refSampler.compare != tcu::Sampler::COMPAREMODE_NONE;
1260 const VkImageAspectFlags aspectMask = isShadowSampler ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
1261 const Unique<VkSemaphore> imageMemoryBindSemaphore(createSemaphore(vk, vkDevice));
1262 Move<VkBuffer> buffer;
1263 deUint32 bufferSize = 0u;
1264 de::MovePtr<Allocation> bufferAlloc;
1265 std::vector<VkBufferImageCopy> copyRegions;
1266 std::vector<deUint32> offsetMultiples;
1267
1268 offsetMultiples.push_back(4u);
1269 offsetMultiples.push_back(texFormat.getPixelSize());
1270
1271 if (isImageSizeSupported(imageCreateInfo.imageType, texSize, deviceProperties.limits) == false)
1272 TCU_THROW(NotSupportedError, "Image size not supported for device.");
1273
1274 allocateAndBindSparseImage(vk, vkDevice, physicalDevice, instance, imageCreateInfo, *imageMemoryBindSemaphore, sparseQueue, m_memAlloc, m_allocations, texFormat, sparseImage);
1275
1276 // Calculate buffer size
1277 for (TextureData::const_iterator mit = textureData.begin(); mit != textureData.end(); ++mit)
1278 {
1279 for (TextureLayerData::const_iterator lit = mit->begin(); lit != mit->end(); ++lit)
1280 {
1281 const tcu::ConstPixelBufferAccess& access = *lit;
1282
1283 bufferSize = getNextMultiple(offsetMultiples, bufferSize);
1284 bufferSize += access.getWidth() * access.getHeight() * access.getDepth() * access.getFormat().getPixelSize();
1285 }
1286 }
1287
1288 {
1289 // Create source buffer
1290 const VkBufferCreateInfo bufferParams =
1291 {
1292 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
1293 DE_NULL, // const void* pNext;
1294 0u, // VkBufferCreateFlags flags;
1295 bufferSize, // VkDeviceSize size;
1296 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, // VkBufferUsageFlags usage;
1297 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
1298 0u, // deUint32 queueFamilyIndexCount;
1299 DE_NULL, // const deUint32* pQueueFamilyIndices;
1300 };
1301
1302 buffer = createBuffer(vk, vkDevice, &bufferParams);
1303 bufferAlloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), MemoryRequirement::HostVisible);
1304
1305 VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, bufferAlloc->getMemory(), bufferAlloc->getOffset()));
1306 }
1307
1308 // Get copy regions and write buffer data
1309 {
1310 deUint32 layerDataOffset = 0;
1311 deUint8* destPtr = (deUint8*)bufferAlloc->getHostPtr();
1312
1313 for (size_t levelNdx = 0; levelNdx < textureData.size(); levelNdx++)
1314 {
1315 const TextureLayerData& layerData = textureData[levelNdx];
1316
1317 for (size_t layerNdx = 0; layerNdx < layerData.size(); layerNdx++)
1318 {
1319 layerDataOffset = getNextMultiple(offsetMultiples, layerDataOffset);
1320
1321 const tcu::ConstPixelBufferAccess& access = layerData[layerNdx];
1322 const tcu::PixelBufferAccess destAccess (access.getFormat(), access.getSize(), destPtr + layerDataOffset);
1323
1324 const VkBufferImageCopy layerRegion =
1325 {
1326 layerDataOffset, // VkDeviceSize bufferOffset;
1327 (deUint32)access.getWidth(), // deUint32 bufferRowLength;
1328 (deUint32)access.getHeight(), // deUint32 bufferImageHeight;
1329 { // VkImageSubresourceLayers imageSubresource;
1330 aspectMask, // VkImageAspectFlags aspectMask;
1331 (deUint32)levelNdx, // uint32_t mipLevel;
1332 (deUint32)layerNdx, // uint32_t baseArrayLayer;
1333 1u // uint32_t layerCount;
1334 },
1335 { 0u, 0u, 0u }, // VkOffset3D imageOffset;
1336 { // VkExtent3D imageExtent;
1337 (deUint32)access.getWidth(),
1338 (deUint32)access.getHeight(),
1339 (deUint32)access.getDepth()
1340 }
1341 };
1342
1343 copyRegions.push_back(layerRegion);
1344 tcu::copy(destAccess, access);
1345
1346 layerDataOffset += access.getWidth() * access.getHeight() * access.getDepth() * access.getFormat().getPixelSize();
1347 }
1348 }
1349 }
1350 copyBufferToImage(vk, vkDevice, queue, queueFamilyIndex, *buffer, bufferSize, copyRegions, &(*imageMemoryBindSemaphore), aspectMask, mipLevels, arrayLayers, sparseImage);
1351 }
1352
useSampler(deUint32 bindingLocation,deUint32 textureId)1353 void ShaderRenderCaseInstance::useSampler (deUint32 bindingLocation, deUint32 textureId)
1354 {
1355 DE_ASSERT(textureId < m_textures.size());
1356
1357 const TextureBinding& textureBinding = *m_textures[textureId];
1358 const TextureBinding::Type textureType = textureBinding.getType();
1359 const tcu::Sampler& refSampler = textureBinding.getSampler();
1360 const TextureBinding::Parameters& textureParams = textureBinding.getParameters();
1361 const bool isMSTexture = textureParams.samples != vk::VK_SAMPLE_COUNT_1_BIT;
1362 deUint32 mipLevels = 1u;
1363 deUint32 arrayLayers = 1u;
1364 tcu::TextureFormat texFormat;
1365 tcu::UVec3 texSize;
1366 TextureData textureData;
1367
1368 if (textureType == TextureBinding::TYPE_2D)
1369 {
1370 const tcu::Texture2D& texture = textureBinding.get2D();
1371
1372 texFormat = texture.getFormat();
1373 texSize = tcu::UVec3(texture.getWidth(), texture.getHeight(), 1u);
1374 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1375 arrayLayers = 1u;
1376
1377 textureData.resize(mipLevels);
1378
1379 for (deUint32 level = 0; level < mipLevels; ++level)
1380 {
1381 if (texture.isLevelEmpty(level))
1382 continue;
1383
1384 textureData[level].push_back(texture.getLevel(level));
1385 }
1386 }
1387 else if (textureType == TextureBinding::TYPE_CUBE_MAP)
1388 {
1389 const tcu::TextureCube& texture = textureBinding.getCube();
1390
1391 texFormat = texture.getFormat();
1392 texSize = tcu::UVec3(texture.getSize(), texture.getSize(), 1u);
1393 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1394 arrayLayers = 6u;
1395
1396 static const tcu::CubeFace cubeFaceMapping[tcu::CUBEFACE_LAST] =
1397 {
1398 tcu::CUBEFACE_POSITIVE_X,
1399 tcu::CUBEFACE_NEGATIVE_X,
1400 tcu::CUBEFACE_POSITIVE_Y,
1401 tcu::CUBEFACE_NEGATIVE_Y,
1402 tcu::CUBEFACE_POSITIVE_Z,
1403 tcu::CUBEFACE_NEGATIVE_Z
1404 };
1405
1406 textureData.resize(mipLevels);
1407
1408 for (deUint32 level = 0; level < mipLevels; ++level)
1409 {
1410 for (int faceNdx = 0; faceNdx < tcu::CUBEFACE_LAST; ++faceNdx)
1411 {
1412 tcu::CubeFace face = cubeFaceMapping[faceNdx];
1413
1414 if (texture.isLevelEmpty(face, level))
1415 continue;
1416
1417 textureData[level].push_back(texture.getLevelFace(level, face));
1418 }
1419 }
1420 }
1421 else if (textureType == TextureBinding::TYPE_2D_ARRAY)
1422 {
1423 const tcu::Texture2DArray& texture = textureBinding.get2DArray();
1424
1425 texFormat = texture.getFormat();
1426 texSize = tcu::UVec3(texture.getWidth(), texture.getHeight(), 1u);
1427 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1428 arrayLayers = (deUint32)texture.getNumLayers();
1429
1430 textureData.resize(mipLevels);
1431
1432 for (deUint32 level = 0; level < mipLevels; ++level)
1433 {
1434 if (texture.isLevelEmpty(level))
1435 continue;
1436
1437 const tcu::ConstPixelBufferAccess& levelLayers = texture.getLevel(level);
1438 const deUint32 layerSize = levelLayers.getWidth() * levelLayers.getHeight() * levelLayers.getFormat().getPixelSize();
1439
1440 for (deUint32 layer = 0; layer < arrayLayers; ++layer)
1441 {
1442 const deUint32 layerOffset = layerSize * layer;
1443 tcu::ConstPixelBufferAccess layerData (levelLayers.getFormat(), levelLayers.getWidth(), levelLayers.getHeight(), 1, (deUint8*)levelLayers.getDataPtr() + layerOffset);
1444 textureData[level].push_back(layerData);
1445 }
1446 }
1447 }
1448 else if (textureType == TextureBinding::TYPE_3D)
1449 {
1450 const tcu::Texture3D& texture = textureBinding.get3D();
1451
1452 texFormat = texture.getFormat();
1453 texSize = tcu::UVec3(texture.getWidth(), texture.getHeight(), texture.getDepth());
1454 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1455 arrayLayers = 1u;
1456
1457 textureData.resize(mipLevels);
1458
1459 for (deUint32 level = 0; level < mipLevels; ++level)
1460 {
1461 if (texture.isLevelEmpty(level))
1462 continue;
1463
1464 textureData[level].push_back(texture.getLevel(level));
1465 }
1466 }
1467 else if (textureType == TextureBinding::TYPE_1D)
1468 {
1469 const tcu::Texture1D& texture = textureBinding.get1D();
1470
1471 texFormat = texture.getFormat();
1472 texSize = tcu::UVec3(texture.getWidth(), 1, 1);
1473 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1474 arrayLayers = 1u;
1475
1476 textureData.resize(mipLevels);
1477
1478 for (deUint32 level = 0; level < mipLevels; ++level)
1479 {
1480 if (texture.isLevelEmpty(level))
1481 continue;
1482
1483 textureData[level].push_back(texture.getLevel(level));
1484 }
1485 }
1486 else if (textureType == TextureBinding::TYPE_1D_ARRAY)
1487 {
1488 const tcu::Texture1DArray& texture = textureBinding.get1DArray();
1489
1490 texFormat = texture.getFormat();
1491 texSize = tcu::UVec3(texture.getWidth(), 1, 1);
1492 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1493 arrayLayers = (deUint32)texture.getNumLayers();
1494
1495 textureData.resize(mipLevels);
1496
1497 for (deUint32 level = 0; level < mipLevels; ++level)
1498 {
1499 if (texture.isLevelEmpty(level))
1500 continue;
1501
1502 const tcu::ConstPixelBufferAccess& levelLayers = texture.getLevel(level);
1503 const deUint32 layerSize = levelLayers.getWidth() * levelLayers.getFormat().getPixelSize();
1504
1505 for (deUint32 layer = 0; layer < arrayLayers; ++layer)
1506 {
1507 const deUint32 layerOffset = layerSize * layer;
1508 tcu::ConstPixelBufferAccess layerData (levelLayers.getFormat(), levelLayers.getWidth(), 1, 1, (deUint8*)levelLayers.getDataPtr() + layerOffset);
1509 textureData[level].push_back(layerData);
1510 }
1511 }
1512 }
1513 else if (textureType == TextureBinding::TYPE_CUBE_ARRAY)
1514 {
1515 const tcu::TextureCubeArray& texture = textureBinding.getCubeArray();
1516 texFormat = texture.getFormat();
1517 texSize = tcu::UVec3(texture.getSize(), texture.getSize(), 1);
1518 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1519 arrayLayers = texture.getDepth();
1520
1521 textureData.resize(mipLevels);
1522
1523 for (deUint32 level = 0; level < mipLevels; ++level)
1524 {
1525 if (texture.isLevelEmpty(level))
1526 continue;
1527
1528 const tcu::ConstPixelBufferAccess& levelLayers = texture.getLevel(level);
1529 const deUint32 layerSize = levelLayers.getWidth() * levelLayers.getHeight() * levelLayers.getFormat().getPixelSize();
1530
1531 for (deUint32 layer = 0; layer < arrayLayers; ++layer)
1532 {
1533 const deUint32 layerOffset = layerSize * layer;
1534 tcu::ConstPixelBufferAccess layerData (levelLayers.getFormat(), levelLayers.getWidth(), levelLayers.getHeight(), 1, (deUint8*)levelLayers.getDataPtr() + layerOffset);
1535 textureData[level].push_back(layerData);
1536 }
1537 }
1538 }
1539 else
1540 {
1541 TCU_THROW(InternalError, "Invalid texture type");
1542 }
1543
1544 createSamplerUniform(bindingLocation, textureType, textureBinding.getParameters().initialization, texFormat, texSize, textureData, refSampler, mipLevels, arrayLayers, textureParams);
1545 }
1546
setPushConstantRanges(const deUint32 rangeCount,const vk::VkPushConstantRange * const pcRanges)1547 void ShaderRenderCaseInstance::setPushConstantRanges (const deUint32 rangeCount, const vk::VkPushConstantRange* const pcRanges)
1548 {
1549 m_pushConstantRanges.clear();
1550 for (deUint32 i = 0; i < rangeCount; ++i)
1551 {
1552 m_pushConstantRanges.push_back(pcRanges[i]);
1553 }
1554 }
1555
updatePushConstants(vk::VkCommandBuffer,vk::VkPipelineLayout)1556 void ShaderRenderCaseInstance::updatePushConstants (vk::VkCommandBuffer, vk::VkPipelineLayout)
1557 {
1558 }
1559
createSamplerUniform(deUint32 bindingLocation,TextureBinding::Type textureType,TextureBinding::Init textureInit,const tcu::TextureFormat & texFormat,const tcu::UVec3 texSize,const TextureData & textureData,const tcu::Sampler & refSampler,deUint32 mipLevels,deUint32 arrayLayers,TextureBinding::Parameters textureParams)1560 void ShaderRenderCaseInstance::createSamplerUniform (deUint32 bindingLocation,
1561 TextureBinding::Type textureType,
1562 TextureBinding::Init textureInit,
1563 const tcu::TextureFormat& texFormat,
1564 const tcu::UVec3 texSize,
1565 const TextureData& textureData,
1566 const tcu::Sampler& refSampler,
1567 deUint32 mipLevels,
1568 deUint32 arrayLayers,
1569 TextureBinding::Parameters textureParams)
1570 {
1571 const VkDevice vkDevice = getDevice();
1572 const DeviceInterface& vk = getDeviceInterface();
1573 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
1574 const deUint32 sparseFamilyIndex = (m_imageBackingMode == IMAGE_BACKING_MODE_SPARSE) ? getSparseQueueFamilyIndex() : queueFamilyIndex;
1575
1576 const bool isShadowSampler = refSampler.compare != tcu::Sampler::COMPAREMODE_NONE;
1577
1578 // when isShadowSampler is true mapSampler utill will set compareEnabled in
1579 // VkSamplerCreateInfo to true and in portability this functionality is under
1580 // feature flag - note that this is safety check as this is known at the
1581 // TestCase level and NotSupportedError should be thrown from checkSupport
1582 if (isShadowSampler &&
1583 m_context.isDeviceFunctionalitySupported("VK_KHR_portability_subset") &&
1584 !m_context.getPortabilitySubsetFeatures().mutableComparisonSamplers)
1585 {
1586 DE_FATAL("mutableComparisonSamplers support should be checked in checkSupport");
1587 }
1588
1589 const VkImageAspectFlags aspectMask = isShadowSampler ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
1590 const VkImageViewType imageViewType = textureTypeToImageViewType(textureType);
1591 const VkImageType imageType = viewTypeToImageType(imageViewType);
1592 const VkSharingMode sharingMode = (queueFamilyIndex != sparseFamilyIndex) ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE;
1593 const VkFormat format = mapTextureFormat(texFormat);
1594 const VkImageUsageFlags imageUsageFlags = textureUsageFlags();
1595 const VkImageCreateFlags imageCreateFlags = textureCreateFlags(imageViewType, m_imageBackingMode);
1596
1597 const deUint32 queueIndexCount = (queueFamilyIndex != sparseFamilyIndex) ? 2 : 1;
1598 const deUint32 queueIndices[] =
1599 {
1600 queueFamilyIndex,
1601 sparseFamilyIndex
1602 };
1603
1604 Move<VkImage> vkTexture;
1605 de::MovePtr<Allocation> allocation;
1606
1607 // Create image
1608 const VkImageCreateInfo imageParams =
1609 {
1610 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType;
1611 DE_NULL, // const void* pNext;
1612 imageCreateFlags, // VkImageCreateFlags flags;
1613 imageType, // VkImageType imageType;
1614 format, // VkFormat format;
1615 { // VkExtent3D extent;
1616 texSize.x(),
1617 texSize.y(),
1618 texSize.z()
1619 },
1620 mipLevels, // deUint32 mipLevels;
1621 arrayLayers, // deUint32 arrayLayers;
1622 textureParams.samples, // VkSampleCountFlagBits samples;
1623 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling;
1624 imageUsageFlags, // VkImageUsageFlags usage;
1625 sharingMode, // VkSharingMode sharingMode;
1626 queueIndexCount, // deUint32 queueFamilyIndexCount;
1627 queueIndices, // const deUint32* pQueueFamilyIndices;
1628 VK_IMAGE_LAYOUT_UNDEFINED // VkImageLayout initialLayout;
1629 };
1630
1631 if (m_imageBackingMode == IMAGE_BACKING_MODE_SPARSE)
1632 {
1633 checkSparseSupport(imageParams);
1634 }
1635
1636 vkTexture = createImage(vk, vkDevice, &imageParams);
1637 allocation = m_memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *vkTexture), MemoryRequirement::Any);
1638
1639 if (m_imageBackingMode != IMAGE_BACKING_MODE_SPARSE)
1640 {
1641 VK_CHECK(vk.bindImageMemory(vkDevice, *vkTexture, allocation->getMemory(), allocation->getOffset()));
1642 }
1643
1644 switch (textureInit)
1645 {
1646 case TextureBinding::INIT_UPLOAD_DATA:
1647 {
1648 // upload*Image functions use cmdCopyBufferToImage, which is invalid for multisample images
1649 DE_ASSERT(textureParams.samples == VK_SAMPLE_COUNT_1_BIT);
1650
1651 if (m_imageBackingMode == IMAGE_BACKING_MODE_SPARSE)
1652 {
1653 uploadSparseImage(texFormat, textureData, refSampler, mipLevels, arrayLayers, *vkTexture, imageParams, texSize);
1654 }
1655 else
1656 {
1657 // Upload texture data
1658 uploadImage(texFormat, textureData, refSampler, mipLevels, arrayLayers, *vkTexture);
1659 }
1660 break;
1661 }
1662 case TextureBinding::INIT_CLEAR:
1663 clearImage(refSampler, mipLevels, arrayLayers, *vkTexture);
1664 break;
1665 default:
1666 DE_FATAL("Impossible");
1667 }
1668
1669 // Create sampler
1670 const auto& minMaxLod = textureParams.minMaxLod;
1671 const VkSamplerCreateInfo samplerParams = (minMaxLod
1672 ? mapSampler(refSampler, texFormat, minMaxLod.get().minLod, minMaxLod.get().maxLod)
1673 : mapSampler(refSampler, texFormat));
1674 Move<VkSampler> sampler = createSampler(vk, vkDevice, &samplerParams);
1675 const deUint32 baseMipLevel = textureParams.baseMipLevel;
1676 const vk::VkComponentMapping components = textureParams.componentMapping;
1677 const VkImageViewCreateInfo viewParams =
1678 {
1679 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
1680 NULL, // const voide* pNext;
1681 0u, // VkImageViewCreateFlags flags;
1682 *vkTexture, // VkImage image;
1683 imageViewType, // VkImageViewType viewType;
1684 format, // VkFormat format;
1685 components, // VkChannelMapping channels;
1686 {
1687 aspectMask, // VkImageAspectFlags aspectMask;
1688 baseMipLevel, // deUint32 baseMipLevel;
1689 mipLevels - baseMipLevel, // deUint32 mipLevels;
1690 0, // deUint32 baseArraySlice;
1691 arrayLayers // deUint32 arraySize;
1692 }, // VkImageSubresourceRange subresourceRange;
1693 };
1694
1695 Move<VkImageView> imageView = createImageView(vk, vkDevice, &viewParams);
1696
1697 const vk::VkDescriptorImageInfo descriptor =
1698 {
1699 sampler.get(), // VkSampler sampler;
1700 imageView.get(), // VkImageView imageView;
1701 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // VkImageLayout imageLayout;
1702 };
1703
1704 de::MovePtr<SamplerUniform> uniform(new SamplerUniform());
1705 uniform->type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1706 uniform->descriptor = descriptor;
1707 uniform->location = bindingLocation;
1708 uniform->image = VkImageSp(new vk::Unique<VkImage>(vkTexture));
1709 uniform->imageView = VkImageViewSp(new vk::Unique<VkImageView>(imageView));
1710 uniform->sampler = VkSamplerSp(new vk::Unique<VkSampler>(sampler));
1711 uniform->alloc = AllocationSp(allocation.release());
1712
1713 m_descriptorSetLayoutBuilder->addSingleSamplerBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, vk::VK_SHADER_STAGE_ALL, DE_NULL);
1714 m_descriptorPoolBuilder->addType(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1715
1716 m_uniformInfos.push_back(UniformInfoSp(new de::UniquePtr<UniformInfo>(uniform)));
1717 }
1718
setupDefaultInputs(void)1719 void ShaderRenderCaseInstance::setupDefaultInputs (void)
1720 {
1721 /* Configuration of the vertex input attributes:
1722 a_position is at location 0
1723 a_coords is at location 1
1724 a_unitCoords is at location 2
1725 a_one is at location 3
1726
1727 User attributes starts from at the location 4.
1728 */
1729
1730 DE_ASSERT(m_quadGrid);
1731 const QuadGrid& quadGrid = *m_quadGrid;
1732
1733 addAttribute(0u, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(tcu::Vec4), quadGrid.getNumVertices(), quadGrid.getPositions());
1734 addAttribute(1u, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(tcu::Vec4), quadGrid.getNumVertices(), quadGrid.getCoords());
1735 addAttribute(2u, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(tcu::Vec4), quadGrid.getNumVertices(), quadGrid.getUnitCoords());
1736 addAttribute(3u, VK_FORMAT_R32_SFLOAT, sizeof(float), quadGrid.getNumVertices(), quadGrid.getAttribOne());
1737
1738 static const struct
1739 {
1740 BaseAttributeType type;
1741 int userNdx;
1742 } userAttributes[] =
1743 {
1744 { A_IN0, 0 },
1745 { A_IN1, 1 },
1746 { A_IN2, 2 },
1747 { A_IN3, 3 }
1748 };
1749
1750 static const struct
1751 {
1752 BaseAttributeType matrixType;
1753 int numCols;
1754 int numRows;
1755 } matrices[] =
1756 {
1757 { MAT2, 2, 2 },
1758 { MAT2x3, 2, 3 },
1759 { MAT2x4, 2, 4 },
1760 { MAT3x2, 3, 2 },
1761 { MAT3, 3, 3 },
1762 { MAT3x4, 3, 4 },
1763 { MAT4x2, 4, 2 },
1764 { MAT4x3, 4, 3 },
1765 { MAT4, 4, 4 }
1766 };
1767
1768 for (size_t attrNdx = 0; attrNdx < m_enabledBaseAttributes.size(); attrNdx++)
1769 {
1770 for (int userNdx = 0; userNdx < DE_LENGTH_OF_ARRAY(userAttributes); userNdx++)
1771 {
1772 if (userAttributes[userNdx].type != m_enabledBaseAttributes[attrNdx].type)
1773 continue;
1774
1775 addAttribute(m_enabledBaseAttributes[attrNdx].location, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(tcu::Vec4), quadGrid.getNumVertices(), quadGrid.getUserAttrib(userNdx));
1776 }
1777
1778 for (int matNdx = 0; matNdx < DE_LENGTH_OF_ARRAY(matrices); matNdx++)
1779 {
1780
1781 if (matrices[matNdx].matrixType != m_enabledBaseAttributes[attrNdx].type)
1782 continue;
1783
1784 const int numCols = matrices[matNdx].numCols;
1785
1786 for (int colNdx = 0; colNdx < numCols; colNdx++)
1787 {
1788 addAttribute(m_enabledBaseAttributes[attrNdx].location + colNdx, VK_FORMAT_R32G32B32A32_SFLOAT, (deUint32)(4 * sizeof(float)), quadGrid.getNumVertices(), quadGrid.getUserAttrib(colNdx));
1789 }
1790 }
1791 }
1792 }
1793
render(deUint32 numVertices,deUint32 numTriangles,const deUint16 * indices,const tcu::Vec4 & constCoords)1794 void ShaderRenderCaseInstance::render (deUint32 numVertices,
1795 deUint32 numTriangles,
1796 const deUint16* indices,
1797 const tcu::Vec4& constCoords)
1798 {
1799 render(numVertices, numTriangles * 3, indices, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, constCoords);
1800 }
1801
render(deUint32 numVertices,deUint32 numIndices,const deUint16 * indices,VkPrimitiveTopology topology,const tcu::Vec4 & constCoords)1802 void ShaderRenderCaseInstance::render (deUint32 numVertices,
1803 deUint32 numIndices,
1804 const deUint16* indices,
1805 VkPrimitiveTopology topology,
1806 const tcu::Vec4& constCoords)
1807 {
1808 const VkDevice vkDevice = getDevice();
1809 const DeviceInterface& vk = getDeviceInterface();
1810 const VkQueue queue = getUniversalQueue();
1811 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
1812
1813 vk::Move<vk::VkImage> colorImage;
1814 de::MovePtr<vk::Allocation> colorImageAlloc;
1815 vk::Move<vk::VkImageView> colorImageView;
1816 vk::Move<vk::VkImage> resolvedImage;
1817 de::MovePtr<vk::Allocation> resolvedImageAlloc;
1818 vk::Move<vk::VkImageView> resolvedImageView;
1819 vk::Move<vk::VkRenderPass> renderPass;
1820 vk::Move<vk::VkFramebuffer> framebuffer;
1821 vk::Move<vk::VkPipelineLayout> pipelineLayout;
1822 vk::Move<vk::VkPipeline> graphicsPipeline;
1823 vk::Move<vk::VkShaderModule> vertexShaderModule;
1824 vk::Move<vk::VkShaderModule> fragmentShaderModule;
1825 vk::Move<vk::VkBuffer> indexBuffer;
1826 de::MovePtr<vk::Allocation> indexBufferAlloc;
1827 vk::Move<vk::VkDescriptorSetLayout> descriptorSetLayout;
1828 vk::Move<vk::VkDescriptorPool> descriptorPool;
1829 vk::Move<vk::VkDescriptorSet> descriptorSet;
1830 vk::Move<vk::VkCommandPool> cmdPool;
1831 vk::Move<vk::VkCommandBuffer> cmdBuffer;
1832
1833 // Create color image
1834 {
1835 const VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1836 VkImageFormatProperties properties;
1837
1838 if ((getInstanceInterface().getPhysicalDeviceImageFormatProperties(getPhysicalDevice(),
1839 m_colorFormat,
1840 VK_IMAGE_TYPE_2D,
1841 VK_IMAGE_TILING_OPTIMAL,
1842 imageUsage,
1843 0u,
1844 &properties) == VK_ERROR_FORMAT_NOT_SUPPORTED))
1845 {
1846 TCU_THROW(NotSupportedError, "Format not supported");
1847 }
1848
1849 if ((properties.sampleCounts & m_sampleCount) != m_sampleCount)
1850 {
1851 TCU_THROW(NotSupportedError, "Format not supported");
1852 }
1853
1854 const VkImageCreateInfo colorImageParams =
1855 {
1856 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType;
1857 DE_NULL, // const void* pNext;
1858 0u, // VkImageCreateFlags flags;
1859 VK_IMAGE_TYPE_2D, // VkImageType imageType;
1860 m_colorFormat, // VkFormat format;
1861 { m_renderSize.x(), m_renderSize.y(), 1u }, // VkExtent3D extent;
1862 1u, // deUint32 mipLevels;
1863 1u, // deUint32 arraySize;
1864 m_sampleCount, // deUint32 samples;
1865 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling;
1866 imageUsage, // VkImageUsageFlags usage;
1867 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
1868 1u, // deUint32 queueFamilyCount;
1869 &queueFamilyIndex, // const deUint32* pQueueFamilyIndices;
1870 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout;
1871 };
1872
1873 colorImage = createImage(vk, vkDevice, &colorImageParams);
1874
1875 // Allocate and bind color image memory
1876 colorImageAlloc = m_memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *colorImage), MemoryRequirement::Any);
1877 VK_CHECK(vk.bindImageMemory(vkDevice, *colorImage, colorImageAlloc->getMemory(), colorImageAlloc->getOffset()));
1878 }
1879
1880 // Create color attachment view
1881 {
1882 const VkImageViewCreateInfo colorImageViewParams =
1883 {
1884 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
1885 DE_NULL, // const void* pNext;
1886 0u, // VkImageViewCreateFlags flags;
1887 *colorImage, // VkImage image;
1888 VK_IMAGE_VIEW_TYPE_2D, // VkImageViewType viewType;
1889 m_colorFormat, // VkFormat format;
1890 {
1891 VK_COMPONENT_SWIZZLE_R, // VkChannelSwizzle r;
1892 VK_COMPONENT_SWIZZLE_G, // VkChannelSwizzle g;
1893 VK_COMPONENT_SWIZZLE_B, // VkChannelSwizzle b;
1894 VK_COMPONENT_SWIZZLE_A // VkChannelSwizzle a;
1895 }, // VkChannelMapping channels;
1896 {
1897 VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
1898 0, // deUint32 baseMipLevel;
1899 1, // deUint32 mipLevels;
1900 0, // deUint32 baseArraySlice;
1901 1 // deUint32 arraySize;
1902 }, // VkImageSubresourceRange subresourceRange;
1903 };
1904
1905 colorImageView = createImageView(vk, vkDevice, &colorImageViewParams);
1906 }
1907
1908 if (isMultiSampling())
1909 {
1910 // Resolved Image
1911 {
1912 const VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1913 VkImageFormatProperties properties;
1914
1915 if ((getInstanceInterface().getPhysicalDeviceImageFormatProperties(getPhysicalDevice(),
1916 m_colorFormat,
1917 VK_IMAGE_TYPE_2D,
1918 VK_IMAGE_TILING_OPTIMAL,
1919 imageUsage,
1920 0,
1921 &properties) == VK_ERROR_FORMAT_NOT_SUPPORTED))
1922 {
1923 TCU_THROW(NotSupportedError, "Format not supported");
1924 }
1925
1926 const VkImageCreateInfo imageCreateInfo =
1927 {
1928 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType;
1929 DE_NULL, // const void* pNext;
1930 0u, // VkImageCreateFlags flags;
1931 VK_IMAGE_TYPE_2D, // VkImageType imageType;
1932 m_colorFormat, // VkFormat format;
1933 { m_renderSize.x(), m_renderSize.y(), 1u }, // VkExtent3D extent;
1934 1u, // deUint32 mipLevels;
1935 1u, // deUint32 arrayLayers;
1936 VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples;
1937 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling;
1938 imageUsage, // VkImageUsageFlags usage;
1939 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
1940 1u, // deUint32 queueFamilyIndexCount;
1941 &queueFamilyIndex, // const deUint32* pQueueFamilyIndices;
1942 VK_IMAGE_LAYOUT_UNDEFINED // VkImageLayout initialLayout;
1943 };
1944
1945 resolvedImage = vk::createImage(vk, vkDevice, &imageCreateInfo, DE_NULL);
1946 resolvedImageAlloc = m_memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *resolvedImage), MemoryRequirement::Any);
1947 VK_CHECK(vk.bindImageMemory(vkDevice, *resolvedImage, resolvedImageAlloc->getMemory(), resolvedImageAlloc->getOffset()));
1948 }
1949
1950 // Resolved Image View
1951 {
1952 const VkImageViewCreateInfo imageViewCreateInfo =
1953 {
1954 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
1955 DE_NULL, // const void* pNext;
1956 0u, // VkImageViewCreateFlags flags;
1957 *resolvedImage, // VkImage image;
1958 VK_IMAGE_VIEW_TYPE_2D, // VkImageViewType viewType;
1959 m_colorFormat, // VkFormat format;
1960 {
1961 VK_COMPONENT_SWIZZLE_R, // VkChannelSwizzle r;
1962 VK_COMPONENT_SWIZZLE_G, // VkChannelSwizzle g;
1963 VK_COMPONENT_SWIZZLE_B, // VkChannelSwizzle b;
1964 VK_COMPONENT_SWIZZLE_A // VkChannelSwizzle a;
1965 },
1966 {
1967 VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
1968 0u, // deUint32 baseMipLevel;
1969 1u, // deUint32 mipLevels;
1970 0u, // deUint32 baseArrayLayer;
1971 1u, // deUint32 arraySize;
1972 }, // VkImageSubresourceRange subresourceRange;
1973 };
1974
1975 resolvedImageView = vk::createImageView(vk, vkDevice, &imageViewCreateInfo, DE_NULL);
1976 }
1977 }
1978
1979 // Create render pass
1980 {
1981 const VkAttachmentDescription attachmentDescription[] =
1982 {
1983 {
1984 (VkAttachmentDescriptionFlags)0, // VkAttachmentDescriptionFlags flags;
1985 m_colorFormat, // VkFormat format;
1986 m_sampleCount, // deUint32 samples;
1987 VK_ATTACHMENT_LOAD_OP_CLEAR, // VkAttachmentLoadOp loadOp;
1988 VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp storeOp;
1989 VK_ATTACHMENT_LOAD_OP_DONT_CARE, // VkAttachmentLoadOp stencilLoadOp;
1990 VK_ATTACHMENT_STORE_OP_DONT_CARE, // VkAttachmentStoreOp stencilStoreOp;
1991 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout initialLayout;
1992 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout finalLayout;
1993 },
1994 {
1995 (VkAttachmentDescriptionFlags)0, // VkAttachmentDescriptionFlags flags;
1996 m_colorFormat, // VkFormat format;
1997 VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples;
1998 VK_ATTACHMENT_LOAD_OP_DONT_CARE, // VkAttachmentLoadOp loadOp;
1999 VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp storeOp;
2000 VK_ATTACHMENT_LOAD_OP_DONT_CARE, // VkAttachmentLoadOp stencilLoadOp;
2001 VK_ATTACHMENT_STORE_OP_DONT_CARE, // VkAttachmentStoreOp stencilStoreOp;
2002 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout initialLayout;
2003 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout finalLayout;
2004 }
2005 };
2006
2007 const VkAttachmentReference attachmentReference =
2008 {
2009 0u, // deUint32 attachment;
2010 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL // VkImageLayout layout;
2011 };
2012
2013 const VkAttachmentReference resolveAttachmentRef =
2014 {
2015 1u, // deUint32 attachment;
2016 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL // VkImageLayout layout;
2017 };
2018
2019 const VkSubpassDescription subpassDescription =
2020 {
2021 0u, // VkSubpassDescriptionFlags flags;
2022 VK_PIPELINE_BIND_POINT_GRAPHICS, // VkPipelineBindPoint pipelineBindPoint;
2023 0u, // deUint32 inputCount;
2024 DE_NULL, // constVkAttachmentReference* pInputAttachments;
2025 1u, // deUint32 colorCount;
2026 &attachmentReference, // constVkAttachmentReference* pColorAttachments;
2027 isMultiSampling() ? &resolveAttachmentRef : DE_NULL,// constVkAttachmentReference* pResolveAttachments;
2028 DE_NULL, // VkAttachmentReference depthStencilAttachment;
2029 0u, // deUint32 preserveCount;
2030 DE_NULL // constVkAttachmentReference* pPreserveAttachments;
2031 };
2032
2033 const VkRenderPassCreateInfo renderPassParams =
2034 {
2035 VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, // VkStructureType sType;
2036 DE_NULL, // const void* pNext;
2037 0u, // VkRenderPassCreateFlags flags;
2038 isMultiSampling() ? 2u : 1u, // deUint32 attachmentCount;
2039 attachmentDescription, // const VkAttachmentDescription* pAttachments;
2040 1u, // deUint32 subpassCount;
2041 &subpassDescription, // const VkSubpassDescription* pSubpasses;
2042 0u, // deUint32 dependencyCount;
2043 DE_NULL // const VkSubpassDependency* pDependencies;
2044 };
2045
2046 renderPass = createRenderPass(vk, vkDevice, &renderPassParams);
2047 }
2048
2049 // Create framebuffer
2050 {
2051 const VkImageView attachments[] =
2052 {
2053 *colorImageView,
2054 *resolvedImageView
2055 };
2056
2057 const VkFramebufferCreateInfo framebufferParams =
2058 {
2059 VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, // VkStructureType sType;
2060 DE_NULL, // const void* pNext;
2061 (VkFramebufferCreateFlags)0,
2062 *renderPass, // VkRenderPass renderPass;
2063 isMultiSampling() ? 2u : 1u, // deUint32 attachmentCount;
2064 attachments, // const VkImageView* pAttachments;
2065 (deUint32)m_renderSize.x(), // deUint32 width;
2066 (deUint32)m_renderSize.y(), // deUint32 height;
2067 1u // deUint32 layers;
2068 };
2069
2070 framebuffer = createFramebuffer(vk, vkDevice, &framebufferParams);
2071 }
2072
2073 // Create descriptors
2074 {
2075 setupUniforms(constCoords);
2076
2077 descriptorSetLayout = m_descriptorSetLayoutBuilder->build(vk, vkDevice);
2078 if (!m_uniformInfos.empty())
2079 {
2080 descriptorPool = m_descriptorPoolBuilder->build(vk, vkDevice, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
2081 const VkDescriptorSetAllocateInfo allocInfo =
2082 {
2083 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2084 DE_NULL,
2085 *descriptorPool,
2086 1u,
2087 &descriptorSetLayout.get(),
2088 };
2089
2090 descriptorSet = allocateDescriptorSet(vk, vkDevice, &allocInfo);
2091 }
2092
2093 for (deUint32 i = 0; i < m_uniformInfos.size(); i++)
2094 {
2095 const UniformInfo* uniformInfo = m_uniformInfos[i].get()->get();
2096 deUint32 location = uniformInfo->location;
2097
2098 if (uniformInfo->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
2099 {
2100 const BufferUniform* bufferInfo = dynamic_cast<const BufferUniform*>(uniformInfo);
2101
2102 m_descriptorSetUpdateBuilder->writeSingle(*descriptorSet, DescriptorSetUpdateBuilder::Location::binding(location), uniformInfo->type, &bufferInfo->descriptor);
2103 }
2104 else if (uniformInfo->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
2105 {
2106 const SamplerUniform* samplerInfo = dynamic_cast<const SamplerUniform*>(uniformInfo);
2107
2108 m_descriptorSetUpdateBuilder->writeSingle(*descriptorSet, DescriptorSetUpdateBuilder::Location::binding(location), uniformInfo->type, &samplerInfo->descriptor);
2109 }
2110 else
2111 DE_FATAL("Impossible");
2112 }
2113
2114 m_descriptorSetUpdateBuilder->update(vk, vkDevice);
2115 }
2116
2117 // Create pipeline layout
2118 {
2119 const VkPushConstantRange* const pcRanges = m_pushConstantRanges.empty() ? DE_NULL : &m_pushConstantRanges[0];
2120 const VkPipelineLayoutCreateInfo pipelineLayoutParams =
2121 {
2122 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, // VkStructureType sType;
2123 DE_NULL, // const void* pNext;
2124 (VkPipelineLayoutCreateFlags)0,
2125 1u, // deUint32 descriptorSetCount;
2126 &*descriptorSetLayout, // const VkDescriptorSetLayout* pSetLayouts;
2127 deUint32(m_pushConstantRanges.size()), // deUint32 pushConstantRangeCount;
2128 pcRanges // const VkPushConstantRange* pPushConstantRanges;
2129 };
2130
2131 pipelineLayout = createPipelineLayout(vk, vkDevice, &pipelineLayoutParams);
2132 }
2133
2134 // Create shaders
2135 {
2136 vertexShaderModule = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get(m_vertexShaderName), 0);
2137 fragmentShaderModule = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get(m_fragmentShaderName), 0);
2138 }
2139
2140 // Create pipeline
2141 {
2142 // Add test case specific attributes
2143 if (m_attribFunc)
2144 m_attribFunc(*this, numVertices);
2145
2146 // Add base attributes
2147 setupDefaultInputs();
2148
2149 const VkPipelineVertexInputStateCreateInfo vertexInputStateParams =
2150 {
2151 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, // VkStructureType sType;
2152 DE_NULL, // const void* pNext;
2153 (VkPipelineVertexInputStateCreateFlags)0,
2154 (deUint32)m_vertexBindingDescription.size(), // deUint32 bindingCount;
2155 &m_vertexBindingDescription[0], // const VkVertexInputBindingDescription* pVertexBindingDescriptions;
2156 (deUint32)m_vertexAttributeDescription.size(), // deUint32 attributeCount;
2157 &m_vertexAttributeDescription[0], // const VkVertexInputAttributeDescription* pVertexAttributeDescriptions;
2158 };
2159
2160 const std::vector<VkViewport> viewports (1, makeViewport(m_renderSize));
2161 const std::vector<VkRect2D> scissors (1, makeRect2D(m_renderSize));
2162
2163 const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
2164 {
2165 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, // VkStructureType sType;
2166 DE_NULL, // const void* pNext;
2167 0u, // VkPipelineMultisampleStateCreateFlags flags;
2168 m_sampleCount, // VkSampleCountFlagBits rasterizationSamples;
2169 VK_FALSE, // VkBool32 sampleShadingEnable;
2170 0.0f, // float minSampleShading;
2171 DE_NULL, // const VkSampleMask* pSampleMask;
2172 VK_FALSE, // VkBool32 alphaToCoverageEnable;
2173 VK_FALSE // VkBool32 alphaToOneEnable;
2174 };
2175
2176 graphicsPipeline = makeGraphicsPipeline(vk, // const DeviceInterface& vk
2177 vkDevice, // const VkDevice device
2178 *pipelineLayout, // const VkPipelineLayout pipelineLayout
2179 *vertexShaderModule, // const VkShaderModule vertexShaderModule
2180 DE_NULL, // const VkShaderModule tessellationControlShaderModule
2181 DE_NULL, // const VkShaderModule tessellationEvalShaderModule
2182 DE_NULL, // const VkShaderModule geometryShaderModule
2183 *fragmentShaderModule, // const VkShaderModule fragmentShaderModule
2184 *renderPass, // const VkRenderPass renderPass
2185 viewports, // const std::vector<VkViewport>& viewports
2186 scissors, // const std::vector<VkRect2D>& scissors
2187 topology, // const VkPrimitiveTopology topology
2188 0u, // const deUint32 subpass
2189 0u, // const deUint32 patchControlPoints
2190 &vertexInputStateParams, // const VkPipelineVertexInputStateCreateInfo* vertexInputStateCreateInfo
2191 DE_NULL, // const VkPipelineRasterizationStateCreateInfo* rasterizationStateCreateInfo
2192 &multisampleStateParams); // const VkPipelineMultisampleStateCreateInfo* multisampleStateCreateInfo
2193 }
2194
2195 // Create vertex indices buffer
2196 if (numIndices != 0)
2197 {
2198 const VkDeviceSize indexBufferSize = numIndices * sizeof(deUint16);
2199 const VkBufferCreateInfo indexBufferParams =
2200 {
2201 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
2202 DE_NULL, // const void* pNext;
2203 0u, // VkBufferCreateFlags flags;
2204 indexBufferSize, // VkDeviceSize size;
2205 VK_BUFFER_USAGE_INDEX_BUFFER_BIT, // VkBufferUsageFlags usage;
2206 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
2207 1u, // deUint32 queueFamilyCount;
2208 &queueFamilyIndex // const deUint32* pQueueFamilyIndices;
2209 };
2210
2211 indexBuffer = createBuffer(vk, vkDevice, &indexBufferParams);
2212 indexBufferAlloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *indexBuffer), MemoryRequirement::HostVisible);
2213
2214 VK_CHECK(vk.bindBufferMemory(vkDevice, *indexBuffer, indexBufferAlloc->getMemory(), indexBufferAlloc->getOffset()));
2215
2216 // Load vertice indices into buffer
2217 deMemcpy(indexBufferAlloc->getHostPtr(), indices, (size_t)indexBufferSize);
2218 flushAlloc(vk, vkDevice, *indexBufferAlloc);
2219 }
2220
2221 // Create command pool
2222 cmdPool = createCommandPool(vk, vkDevice, VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, queueFamilyIndex);
2223
2224 // Create command buffer
2225 {
2226 cmdBuffer = allocateCommandBuffer(vk, vkDevice, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
2227
2228 beginCommandBuffer(vk, *cmdBuffer);
2229
2230 {
2231 const VkImageMemoryBarrier imageBarrier =
2232 {
2233 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType;
2234 DE_NULL, // const void* pNext;
2235 0u, // VkAccessFlags srcAccessMask;
2236 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, // VkAccessFlags dstAccessMask;
2237 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout oldLayout;
2238 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout newLayout;
2239 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
2240 VK_QUEUE_FAMILY_IGNORED, // deUint32 dstQueueFamilyIndex;
2241 *colorImage, // VkImage image;
2242 { // VkImageSubresourceRange subresourceRange;
2243 VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
2244 0u, // deUint32 baseMipLevel;
2245 1u, // deUint32 mipLevels;
2246 0u, // deUint32 baseArrayLayer;
2247 1u, // deUint32 arraySize;
2248 }
2249 };
2250
2251 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 0, DE_NULL, 1, &imageBarrier);
2252
2253 if (isMultiSampling()) {
2254 // add multisample barrier
2255 const VkImageMemoryBarrier multiSampleImageBarrier =
2256 {
2257 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType;
2258 DE_NULL, // const void* pNext;
2259 0u, // VkAccessFlags srcAccessMask;
2260 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, // VkAccessFlags dstAccessMask;
2261 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout oldLayout;
2262 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout newLayout;
2263 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
2264 VK_QUEUE_FAMILY_IGNORED, // deUint32 dstQueueFamilyIndex;
2265 *resolvedImage, // VkImage image;
2266 { // VkImageSubresourceRange subresourceRange;
2267 VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
2268 0u, // deUint32 baseMipLevel;
2269 1u, // deUint32 mipLevels;
2270 0u, // deUint32 baseArrayLayer;
2271 1u, // deUint32 arraySize;
2272 }
2273 };
2274
2275 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, (VkDependencyFlags)0, 0, (const VkMemoryBarrier*)DE_NULL, 0, DE_NULL, 1, &multiSampleImageBarrier);
2276 }
2277 }
2278
2279 beginRenderPass(vk, *cmdBuffer, *renderPass, *framebuffer, makeRect2D(0, 0, m_renderSize.x(), m_renderSize.y()), m_clearColor);
2280
2281 updatePushConstants(*cmdBuffer, *pipelineLayout);
2282 vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *graphicsPipeline);
2283 if (!m_uniformInfos.empty())
2284 vk.cmdBindDescriptorSets(*cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *pipelineLayout, 0u, 1, &*descriptorSet, 0u, DE_NULL);
2285
2286 const deUint32 numberOfVertexAttributes = (deUint32)m_vertexBuffers.size();
2287 const std::vector<VkDeviceSize> offsets(numberOfVertexAttributes, 0);
2288
2289 std::vector<VkBuffer> buffers(numberOfVertexAttributes);
2290 for (size_t i = 0; i < numberOfVertexAttributes; i++)
2291 {
2292 buffers[i] = m_vertexBuffers[i].get()->get();
2293 }
2294
2295 vk.cmdBindVertexBuffers(*cmdBuffer, 0, numberOfVertexAttributes, &buffers[0], &offsets[0]);
2296 if (numIndices != 0)
2297 {
2298 vk.cmdBindIndexBuffer(*cmdBuffer, *indexBuffer, 0, VK_INDEX_TYPE_UINT16);
2299 vk.cmdDrawIndexed(*cmdBuffer, numIndices, 1, 0, 0, 0);
2300 }
2301 else
2302 vk.cmdDraw(*cmdBuffer, numVertices, 1, 0, 0);
2303
2304 endRenderPass(vk, *cmdBuffer);
2305 endCommandBuffer(vk, *cmdBuffer);
2306 }
2307
2308 // Execute Draw
2309 submitCommandsAndWait(vk, vkDevice, queue, cmdBuffer.get());
2310
2311 // Read back the result
2312 {
2313 const tcu::TextureFormat resultFormat = mapVkFormat(m_colorFormat);
2314 const VkDeviceSize imageSizeBytes = (VkDeviceSize)(resultFormat.getPixelSize() * m_renderSize.x() * m_renderSize.y());
2315 const VkBufferCreateInfo readImageBufferParams =
2316 {
2317 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
2318 DE_NULL, // const void* pNext;
2319 0u, // VkBufferCreateFlags flags;
2320 imageSizeBytes, // VkDeviceSize size;
2321 VK_BUFFER_USAGE_TRANSFER_DST_BIT, // VkBufferUsageFlags usage;
2322 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
2323 1u, // deUint32 queueFamilyCount;
2324 &queueFamilyIndex, // const deUint32* pQueueFamilyIndices;
2325 };
2326 const Unique<VkBuffer> readImageBuffer (createBuffer(vk, vkDevice, &readImageBufferParams));
2327 const de::UniquePtr<Allocation> readImageBufferMemory (m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *readImageBuffer), MemoryRequirement::HostVisible));
2328
2329 VK_CHECK(vk.bindBufferMemory(vkDevice, *readImageBuffer, readImageBufferMemory->getMemory(), readImageBufferMemory->getOffset()));
2330
2331 // Copy image to buffer
2332 const Move<VkCommandBuffer> resultCmdBuffer = allocateCommandBuffer(vk, vkDevice, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
2333
2334 beginCommandBuffer(vk, *resultCmdBuffer);
2335
2336 copyImageToBuffer(vk, *resultCmdBuffer, isMultiSampling() ? *resolvedImage : *colorImage, *readImageBuffer, tcu::IVec2(m_renderSize.x(), m_renderSize.y()));
2337
2338 endCommandBuffer(vk, *resultCmdBuffer);
2339
2340 submitCommandsAndWait(vk, vkDevice, queue, resultCmdBuffer.get());
2341
2342 invalidateAlloc(vk, vkDevice, *readImageBufferMemory);
2343
2344 const tcu::ConstPixelBufferAccess resultAccess (resultFormat, m_renderSize.x(), m_renderSize.y(), 1, readImageBufferMemory->getHostPtr());
2345
2346 m_resultImage.setStorage(resultFormat, m_renderSize.x(), m_renderSize.y());
2347 tcu::copy(m_resultImage.getAccess(), resultAccess);
2348 }
2349 }
2350
computeVertexReference(tcu::Surface & result,const QuadGrid & quadGrid)2351 void ShaderRenderCaseInstance::computeVertexReference (tcu::Surface& result, const QuadGrid& quadGrid)
2352 {
2353 DE_ASSERT(m_evaluator);
2354
2355 // Buffer info.
2356 const int width = result.getWidth();
2357 const int height = result.getHeight();
2358 const int gridSize = quadGrid.getGridSize();
2359 const int stride = gridSize + 1;
2360 const bool hasAlpha = true; // \todo [2015-09-07 elecro] add correct alpha check
2361 ShaderEvalContext evalCtx (quadGrid);
2362
2363 // Evaluate color for each vertex.
2364 std::vector<tcu::Vec4> colors ((gridSize + 1) * (gridSize + 1));
2365 for (int y = 0; y < gridSize+1; y++)
2366 for (int x = 0; x < gridSize+1; x++)
2367 {
2368 const float sx = (float)x / (float)gridSize;
2369 const float sy = (float)y / (float)gridSize;
2370 const int vtxNdx = ((y * (gridSize+1)) + x);
2371
2372 evalCtx.reset(sx, sy);
2373 m_evaluator->evaluate(evalCtx);
2374 DE_ASSERT(!evalCtx.isDiscarded); // Discard is not available in vertex shader.
2375 tcu::Vec4 color = evalCtx.color;
2376
2377 if (!hasAlpha)
2378 color.w() = 1.0f;
2379
2380 colors[vtxNdx] = color;
2381 }
2382
2383 // Render quads.
2384 for (int y = 0; y < gridSize; y++)
2385 for (int x = 0; x < gridSize; x++)
2386 {
2387 const float x0 = (float)x / (float)gridSize;
2388 const float x1 = (float)(x + 1) / (float)gridSize;
2389 const float y0 = (float)y / (float)gridSize;
2390 const float y1 = (float)(y + 1) / (float)gridSize;
2391
2392 const float sx0 = x0 * (float)width;
2393 const float sx1 = x1 * (float)width;
2394 const float sy0 = y0 * (float)height;
2395 const float sy1 = y1 * (float)height;
2396 const float oosx = 1.0f / (sx1 - sx0);
2397 const float oosy = 1.0f / (sy1 - sy0);
2398
2399 const int ix0 = deCeilFloatToInt32(sx0 - 0.5f);
2400 const int ix1 = deCeilFloatToInt32(sx1 - 0.5f);
2401 const int iy0 = deCeilFloatToInt32(sy0 - 0.5f);
2402 const int iy1 = deCeilFloatToInt32(sy1 - 0.5f);
2403
2404 const int v00 = (y * stride) + x;
2405 const int v01 = (y * stride) + x + 1;
2406 const int v10 = ((y + 1) * stride) + x;
2407 const int v11 = ((y + 1) * stride) + x + 1;
2408 const tcu::Vec4 c00 = colors[v00];
2409 const tcu::Vec4 c01 = colors[v01];
2410 const tcu::Vec4 c10 = colors[v10];
2411 const tcu::Vec4 c11 = colors[v11];
2412
2413 //printf("(%d,%d) -> (%f..%f, %f..%f) (%d..%d, %d..%d)\n", x, y, sx0, sx1, sy0, sy1, ix0, ix1, iy0, iy1);
2414
2415 for (int iy = iy0; iy < iy1; iy++)
2416 for (int ix = ix0; ix < ix1; ix++)
2417 {
2418 DE_ASSERT(deInBounds32(ix, 0, width));
2419 DE_ASSERT(deInBounds32(iy, 0, height));
2420
2421 const float sfx = (float)ix + 0.5f;
2422 const float sfy = (float)iy + 0.5f;
2423 const float fx1 = deFloatClamp((sfx - sx0) * oosx, 0.0f, 1.0f);
2424 const float fy1 = deFloatClamp((sfy - sy0) * oosy, 0.0f, 1.0f);
2425
2426 // Triangle quad interpolation.
2427 const bool tri = fx1 + fy1 <= 1.0f;
2428 const float tx = tri ? fx1 : (1.0f-fx1);
2429 const float ty = tri ? fy1 : (1.0f-fy1);
2430 const tcu::Vec4& t0 = tri ? c00 : c11;
2431 const tcu::Vec4& t1 = tri ? c01 : c10;
2432 const tcu::Vec4& t2 = tri ? c10 : c01;
2433 const tcu::Vec4 color = t0 + (t1-t0)*tx + (t2-t0)*ty;
2434
2435 result.setPixel(ix, iy, tcu::RGBA(color));
2436 }
2437 }
2438 }
2439
computeFragmentReference(tcu::Surface & result,const QuadGrid & quadGrid)2440 void ShaderRenderCaseInstance::computeFragmentReference (tcu::Surface& result, const QuadGrid& quadGrid)
2441 {
2442 DE_ASSERT(m_evaluator);
2443
2444 // Buffer info.
2445 const int width = result.getWidth();
2446 const int height = result.getHeight();
2447 const bool hasAlpha = true; // \todo [2015-09-07 elecro] add correct alpha check
2448 ShaderEvalContext evalCtx (quadGrid);
2449
2450 // Render.
2451 for (int y = 0; y < height; y++)
2452 for (int x = 0; x < width; x++)
2453 {
2454 const float sx = ((float)x + 0.5f) / (float)width;
2455 const float sy = ((float)y + 0.5f) / (float)height;
2456
2457 evalCtx.reset(sx, sy);
2458 m_evaluator->evaluate(evalCtx);
2459 // Select either clear color or computed color based on discarded bit.
2460 tcu::Vec4 color = evalCtx.isDiscarded ? m_clearColor : evalCtx.color;
2461
2462 if (!hasAlpha)
2463 color.w() = 1.0f;
2464
2465 result.setPixel(x, y, tcu::RGBA(color));
2466 }
2467 }
2468
compareImages(const tcu::Surface & resImage,const tcu::Surface & refImage,float errorThreshold)2469 bool ShaderRenderCaseInstance::compareImages (const tcu::Surface& resImage, const tcu::Surface& refImage, float errorThreshold)
2470 {
2471 if (m_fuzzyCompare)
2472 return tcu::fuzzyCompare(m_context.getTestContext().getLog(), "ComparisonResult", "Image comparison result", refImage, resImage, errorThreshold, tcu::COMPARE_LOG_EVERYTHING);
2473 else
2474 return tcu::pixelThresholdCompare(m_context.getTestContext().getLog(), "ComparisonResult", "Image comparison result", refImage, resImage, tcu::RGBA(1, 1, 1, 1), tcu::COMPARE_LOG_EVERYTHING);
2475 }
2476
2477 } // sr
2478 } // vkt
2479