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 {
628 }
629
getAllocator(void) const630 vk::Allocator& ShaderRenderCaseInstance::getAllocator (void) const
631 {
632 return m_context.getDefaultAllocator();
633 }
634
~ShaderRenderCaseInstance(void)635 ShaderRenderCaseInstance::~ShaderRenderCaseInstance (void)
636 {
637 }
638
getDevice(void) const639 VkDevice ShaderRenderCaseInstance::getDevice (void) const
640 {
641 return m_context.getDevice();
642 }
643
getUniversalQueueFamilyIndex(void) const644 deUint32 ShaderRenderCaseInstance::getUniversalQueueFamilyIndex (void) const
645 {
646 return m_context.getUniversalQueueFamilyIndex();
647 }
648
getSparseQueueFamilyIndex(void) const649 deUint32 ShaderRenderCaseInstance::getSparseQueueFamilyIndex (void) const
650 {
651 return m_context.getSparseQueueFamilyIndex();
652 }
653
getDeviceInterface(void) const654 const DeviceInterface& ShaderRenderCaseInstance::getDeviceInterface (void) const
655 {
656 return m_context.getDeviceInterface();
657 }
658
getUniversalQueue(void) const659 VkQueue ShaderRenderCaseInstance::getUniversalQueue (void) const
660 {
661 return m_context.getUniversalQueue();
662 }
663
getSparseQueue(void) const664 VkQueue ShaderRenderCaseInstance::getSparseQueue (void) const
665 {
666 return m_context.getSparseQueue();
667 }
668
getPhysicalDevice(void) const669 VkPhysicalDevice ShaderRenderCaseInstance::getPhysicalDevice (void) const
670 {
671 return m_context.getPhysicalDevice();
672 }
673
getInstanceInterface(void) const674 const InstanceInterface& ShaderRenderCaseInstance::getInstanceInterface (void) const
675 {
676 return m_context.getInstanceInterface();
677 }
678
iterate(void)679 tcu::TestStatus ShaderRenderCaseInstance::iterate (void)
680 {
681 setup();
682
683 // Create quad grid.
684 const tcu::UVec2 viewportSize = getViewportSize();
685 const int width = viewportSize.x();
686 const int height = viewportSize.y();
687
688 m_quadGrid = de::MovePtr<QuadGrid>(new QuadGrid(m_quadGridSize, width, height, getDefaultConstCoords(), m_userAttribTransforms, m_textures));
689
690 // Render result.
691 tcu::Surface resImage (width, height);
692
693 render(m_quadGrid->getNumVertices(), m_quadGrid->getNumTriangles(), m_quadGrid->getIndices(), m_quadGrid->getConstCoords());
694 tcu::copy(resImage.getAccess(), m_resultImage.getAccess());
695
696 // Compute reference.
697 tcu::Surface refImage (width, height);
698 if (m_isVertexCase)
699 computeVertexReference(refImage, *m_quadGrid);
700 else
701 computeFragmentReference(refImage, *m_quadGrid);
702
703 // Compare.
704 const bool compareOk = compareImages(resImage, refImage, 0.2f);
705
706 if (compareOk)
707 return tcu::TestStatus::pass("Result image matches reference");
708 else
709 return tcu::TestStatus::fail("Image mismatch");
710 }
711
setup(void)712 void ShaderRenderCaseInstance::setup (void)
713 {
714 m_resultImage = tcu::TextureLevel();
715 m_descriptorSetLayoutBuilder = de::MovePtr<DescriptorSetLayoutBuilder> (new DescriptorSetLayoutBuilder());
716 m_descriptorPoolBuilder = de::MovePtr<DescriptorPoolBuilder> (new DescriptorPoolBuilder());
717 m_descriptorSetUpdateBuilder = de::MovePtr<DescriptorSetUpdateBuilder> (new DescriptorSetUpdateBuilder());
718
719 m_uniformInfos.clear();
720 m_vertexBindingDescription.clear();
721 m_vertexAttributeDescription.clear();
722 m_vertexBuffers.clear();
723 m_vertexBufferAllocs.clear();
724 m_pushConstantRanges.clear();
725 }
726
setupUniformData(deUint32 bindingLocation,size_t size,const void * dataPtr)727 void ShaderRenderCaseInstance::setupUniformData (deUint32 bindingLocation, size_t size, const void* dataPtr)
728 {
729 const VkDevice vkDevice = getDevice();
730 const DeviceInterface& vk = getDeviceInterface();
731 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
732
733 const VkBufferCreateInfo uniformBufferParams =
734 {
735 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
736 DE_NULL, // const void* pNext;
737 0u, // VkBufferCreateFlags flags;
738 size, // VkDeviceSize size;
739 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, // VkBufferUsageFlags usage;
740 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
741 1u, // deUint32 queueFamilyCount;
742 &queueFamilyIndex // const deUint32* pQueueFamilyIndices;
743 };
744
745 Move<VkBuffer> buffer = createBuffer(vk, vkDevice, &uniformBufferParams);
746 de::MovePtr<Allocation> alloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), MemoryRequirement::HostVisible);
747 VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, alloc->getMemory(), alloc->getOffset()));
748
749 deMemcpy(alloc->getHostPtr(), dataPtr, size);
750 flushAlloc(vk, vkDevice, *alloc);
751
752 de::MovePtr<BufferUniform> uniformInfo(new BufferUniform());
753 uniformInfo->type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
754 uniformInfo->descriptor = makeDescriptorBufferInfo(*buffer, 0u, size);
755 uniformInfo->location = bindingLocation;
756 uniformInfo->buffer = VkBufferSp(new vk::Unique<VkBuffer>(buffer));
757 uniformInfo->alloc = AllocationSp(alloc.release());
758
759 m_uniformInfos.push_back(UniformInfoSp(new de::UniquePtr<UniformInfo>(uniformInfo)));
760 }
761
addUniform(deUint32 bindingLocation,vk::VkDescriptorType descriptorType,size_t dataSize,const void * data)762 void ShaderRenderCaseInstance::addUniform (deUint32 bindingLocation, vk::VkDescriptorType descriptorType, size_t dataSize, const void* data)
763 {
764 m_descriptorSetLayoutBuilder->addSingleBinding(descriptorType, vk::VK_SHADER_STAGE_ALL);
765 m_descriptorPoolBuilder->addType(descriptorType);
766
767 setupUniformData(bindingLocation, dataSize, data);
768 }
769
addAttribute(deUint32 bindingLocation,vk::VkFormat format,deUint32 sizePerElement,deUint32 count,const void * dataPtr)770 void ShaderRenderCaseInstance::addAttribute (deUint32 bindingLocation,
771 vk::VkFormat format,
772 deUint32 sizePerElement,
773 deUint32 count,
774 const void* dataPtr)
775 {
776 // Add binding specification
777 const deUint32 binding = (deUint32)m_vertexBindingDescription.size();
778 const VkVertexInputBindingDescription bindingDescription =
779 {
780 binding, // deUint32 binding;
781 sizePerElement, // deUint32 stride;
782 VK_VERTEX_INPUT_RATE_VERTEX // VkVertexInputRate stepRate;
783 };
784
785 m_vertexBindingDescription.push_back(bindingDescription);
786
787 // Add location and format specification
788 const VkVertexInputAttributeDescription attributeDescription =
789 {
790 bindingLocation, // deUint32 location;
791 binding, // deUint32 binding;
792 format, // VkFormat format;
793 0u, // deUint32 offset;
794 };
795
796 m_vertexAttributeDescription.push_back(attributeDescription);
797
798 // Upload data to buffer
799 const VkDevice vkDevice = getDevice();
800 const DeviceInterface& vk = getDeviceInterface();
801 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
802
803 const VkDeviceSize inputSize = sizePerElement * count;
804 const VkBufferCreateInfo vertexBufferParams =
805 {
806 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
807 DE_NULL, // const void* pNext;
808 0u, // VkBufferCreateFlags flags;
809 inputSize, // VkDeviceSize size;
810 VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, // VkBufferUsageFlags usage;
811 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
812 1u, // deUint32 queueFamilyCount;
813 &queueFamilyIndex // const deUint32* pQueueFamilyIndices;
814 };
815
816 Move<VkBuffer> buffer = createBuffer(vk, vkDevice, &vertexBufferParams);
817 de::MovePtr<vk::Allocation> alloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), MemoryRequirement::HostVisible);
818 VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, alloc->getMemory(), alloc->getOffset()));
819
820 deMemcpy(alloc->getHostPtr(), dataPtr, (size_t)inputSize);
821 flushAlloc(vk, vkDevice, *alloc);
822
823 m_vertexBuffers.push_back(VkBufferSp(new vk::Unique<VkBuffer>(buffer)));
824 m_vertexBufferAllocs.push_back(AllocationSp(alloc.release()));
825 }
826
useAttribute(deUint32 bindingLocation,BaseAttributeType type)827 void ShaderRenderCaseInstance::useAttribute (deUint32 bindingLocation, BaseAttributeType type)
828 {
829 const EnabledBaseAttribute attribute =
830 {
831 bindingLocation, // deUint32 location;
832 type // BaseAttributeType type;
833 };
834 m_enabledBaseAttributes.push_back(attribute);
835 }
836
setupUniforms(const tcu::Vec4 & constCoords)837 void ShaderRenderCaseInstance::setupUniforms (const tcu::Vec4& constCoords)
838 {
839 if (m_uniformSetup)
840 m_uniformSetup->setup(*this, constCoords);
841 }
842
useUniform(deUint32 bindingLocation,BaseUniformType type)843 void ShaderRenderCaseInstance::useUniform (deUint32 bindingLocation, BaseUniformType type)
844 {
845 #define UNIFORM_CASE(type, value) case type: addUniform(bindingLocation, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, value); break
846
847 switch(type)
848 {
849 // Bool
850 UNIFORM_CASE(UB_FALSE, 0);
851 UNIFORM_CASE(UB_TRUE, 1);
852
853 // BVec4
854 UNIFORM_CASE(UB4_FALSE, tcu::Vec4(0));
855 UNIFORM_CASE(UB4_TRUE, tcu::Vec4(1));
856
857 // Integer
858 UNIFORM_CASE(UI_ZERO, 0);
859 UNIFORM_CASE(UI_ONE, 1);
860 UNIFORM_CASE(UI_TWO, 2);
861 UNIFORM_CASE(UI_THREE, 3);
862 UNIFORM_CASE(UI_FOUR, 4);
863 UNIFORM_CASE(UI_FIVE, 5);
864 UNIFORM_CASE(UI_SIX, 6);
865 UNIFORM_CASE(UI_SEVEN, 7);
866 UNIFORM_CASE(UI_EIGHT, 8);
867 UNIFORM_CASE(UI_ONEHUNDREDONE, 101);
868
869 // IVec2
870 UNIFORM_CASE(UI2_MINUS_ONE, tcu::IVec2(-1));
871 UNIFORM_CASE(UI2_ZERO, tcu::IVec2(0));
872 UNIFORM_CASE(UI2_ONE, tcu::IVec2(1));
873 UNIFORM_CASE(UI2_TWO, tcu::IVec2(2));
874 UNIFORM_CASE(UI2_THREE, tcu::IVec2(3));
875 UNIFORM_CASE(UI2_FOUR, tcu::IVec2(4));
876 UNIFORM_CASE(UI2_FIVE, tcu::IVec2(5));
877
878 // IVec3
879 UNIFORM_CASE(UI3_MINUS_ONE, tcu::IVec3(-1));
880 UNIFORM_CASE(UI3_ZERO, tcu::IVec3(0));
881 UNIFORM_CASE(UI3_ONE, tcu::IVec3(1));
882 UNIFORM_CASE(UI3_TWO, tcu::IVec3(2));
883 UNIFORM_CASE(UI3_THREE, tcu::IVec3(3));
884 UNIFORM_CASE(UI3_FOUR, tcu::IVec3(4));
885 UNIFORM_CASE(UI3_FIVE, tcu::IVec3(5));
886
887 // IVec4
888 UNIFORM_CASE(UI4_MINUS_ONE, tcu::IVec4(-1));
889 UNIFORM_CASE(UI4_ZERO, tcu::IVec4(0));
890 UNIFORM_CASE(UI4_ONE, tcu::IVec4(1));
891 UNIFORM_CASE(UI4_TWO, tcu::IVec4(2));
892 UNIFORM_CASE(UI4_THREE, tcu::IVec4(3));
893 UNIFORM_CASE(UI4_FOUR, tcu::IVec4(4));
894 UNIFORM_CASE(UI4_FIVE, tcu::IVec4(5));
895
896 // Float
897 UNIFORM_CASE(UF_ZERO, 0.0f);
898 UNIFORM_CASE(UF_ONE, 1.0f);
899 UNIFORM_CASE(UF_TWO, 2.0f);
900 UNIFORM_CASE(UF_THREE, 3.0f);
901 UNIFORM_CASE(UF_FOUR, 4.0f);
902 UNIFORM_CASE(UF_FIVE, 5.0f);
903 UNIFORM_CASE(UF_SIX, 6.0f);
904 UNIFORM_CASE(UF_SEVEN, 7.0f);
905 UNIFORM_CASE(UF_EIGHT, 8.0f);
906
907 UNIFORM_CASE(UF_HALF, 1.0f / 2.0f);
908 UNIFORM_CASE(UF_THIRD, 1.0f / 3.0f);
909 UNIFORM_CASE(UF_FOURTH, 1.0f / 4.0f);
910 UNIFORM_CASE(UF_FIFTH, 1.0f / 5.0f);
911 UNIFORM_CASE(UF_SIXTH, 1.0f / 6.0f);
912 UNIFORM_CASE(UF_SEVENTH, 1.0f / 7.0f);
913 UNIFORM_CASE(UF_EIGHTH, 1.0f / 8.0f);
914
915 // Vec2
916 UNIFORM_CASE(UV2_MINUS_ONE, tcu::Vec2(-1.0f));
917 UNIFORM_CASE(UV2_ZERO, tcu::Vec2(0.0f));
918 UNIFORM_CASE(UV2_ONE, tcu::Vec2(1.0f));
919 UNIFORM_CASE(UV2_TWO, tcu::Vec2(2.0f));
920 UNIFORM_CASE(UV2_THREE, tcu::Vec2(3.0f));
921
922 UNIFORM_CASE(UV2_HALF, tcu::Vec2(1.0f / 2.0f));
923
924 // Vec3
925 UNIFORM_CASE(UV3_MINUS_ONE, tcu::Vec3(-1.0f));
926 UNIFORM_CASE(UV3_ZERO, tcu::Vec3(0.0f));
927 UNIFORM_CASE(UV3_ONE, tcu::Vec3(1.0f));
928 UNIFORM_CASE(UV3_TWO, tcu::Vec3(2.0f));
929 UNIFORM_CASE(UV3_THREE, tcu::Vec3(3.0f));
930
931 UNIFORM_CASE(UV3_HALF, tcu::Vec3(1.0f / 2.0f));
932
933 // Vec4
934 UNIFORM_CASE(UV4_MINUS_ONE, tcu::Vec4(-1.0f));
935 UNIFORM_CASE(UV4_ZERO, tcu::Vec4(0.0f));
936 UNIFORM_CASE(UV4_ONE, tcu::Vec4(1.0f));
937 UNIFORM_CASE(UV4_TWO, tcu::Vec4(2.0f));
938 UNIFORM_CASE(UV4_THREE, tcu::Vec4(3.0f));
939
940 UNIFORM_CASE(UV4_HALF, tcu::Vec4(1.0f / 2.0f));
941
942 UNIFORM_CASE(UV4_BLACK, tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
943 UNIFORM_CASE(UV4_GRAY, tcu::Vec4(0.5f, 0.5f, 0.5f, 1.0f));
944 UNIFORM_CASE(UV4_WHITE, tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
945
946 default:
947 m_context.getTestContext().getLog() << tcu::TestLog::Message << "Unknown Uniform type: " << type << tcu::TestLog::EndMessage;
948 break;
949 }
950
951 #undef UNIFORM_CASE
952 }
953
getViewportSize(void) const954 const tcu::UVec2 ShaderRenderCaseInstance::getViewportSize (void) const
955 {
956 return tcu::UVec2(de::min(m_renderSize.x(), MAX_RENDER_WIDTH),
957 de::min(m_renderSize.y(), MAX_RENDER_HEIGHT));
958 }
959
setSampleCount(VkSampleCountFlagBits sampleCount)960 void ShaderRenderCaseInstance::setSampleCount (VkSampleCountFlagBits sampleCount)
961 {
962 m_sampleCount = sampleCount;
963 }
964
isMultiSampling(void) const965 bool ShaderRenderCaseInstance::isMultiSampling (void) const
966 {
967 return m_sampleCount != VK_SAMPLE_COUNT_1_BIT;
968 }
969
uploadImage(const tcu::TextureFormat & texFormat,const TextureData & textureData,const tcu::Sampler & refSampler,deUint32 mipLevels,deUint32 arrayLayers,VkImage destImage)970 void ShaderRenderCaseInstance::uploadImage (const tcu::TextureFormat& texFormat,
971 const TextureData& textureData,
972 const tcu::Sampler& refSampler,
973 deUint32 mipLevels,
974 deUint32 arrayLayers,
975 VkImage destImage)
976 {
977 const VkDevice vkDevice = getDevice();
978 const DeviceInterface& vk = getDeviceInterface();
979 const VkQueue queue = getUniversalQueue();
980 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
981
982 const bool isShadowSampler = refSampler.compare != tcu::Sampler::COMPAREMODE_NONE;
983 const VkImageAspectFlags aspectMask = isShadowSampler ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
984 deUint32 bufferSize = 0u;
985 Move<VkBuffer> buffer;
986 de::MovePtr<Allocation> bufferAlloc;
987 Move<VkCommandPool> cmdPool;
988 Move<VkCommandBuffer> cmdBuffer;
989 std::vector<VkBufferImageCopy> copyRegions;
990 std::vector<deUint32> offsetMultiples;
991
992 offsetMultiples.push_back(4u);
993 offsetMultiples.push_back(texFormat.getPixelSize());
994
995 // Calculate buffer size
996 for (TextureData::const_iterator mit = textureData.begin(); mit != textureData.end(); ++mit)
997 {
998 for (TextureLayerData::const_iterator lit = mit->begin(); lit != mit->end(); ++lit)
999 {
1000 const tcu::ConstPixelBufferAccess& access = *lit;
1001
1002 bufferSize = getNextMultiple(offsetMultiples, bufferSize);
1003 bufferSize += access.getWidth() * access.getHeight() * access.getDepth() * access.getFormat().getPixelSize();
1004 }
1005 }
1006
1007 // Create source buffer
1008 {
1009 const VkBufferCreateInfo bufferParams =
1010 {
1011 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
1012 DE_NULL, // const void* pNext;
1013 0u, // VkBufferCreateFlags flags;
1014 bufferSize, // VkDeviceSize size;
1015 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, // VkBufferUsageFlags usage;
1016 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
1017 0u, // deUint32 queueFamilyIndexCount;
1018 DE_NULL, // const deUint32* pQueueFamilyIndices;
1019 };
1020
1021 buffer = createBuffer(vk, vkDevice, &bufferParams);
1022 bufferAlloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), MemoryRequirement::HostVisible);
1023 VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, bufferAlloc->getMemory(), bufferAlloc->getOffset()));
1024 }
1025
1026 // Get copy regions and write buffer data
1027 {
1028 deUint32 layerDataOffset = 0;
1029 deUint8* destPtr = (deUint8*)bufferAlloc->getHostPtr();
1030
1031 for (size_t levelNdx = 0; levelNdx < textureData.size(); levelNdx++)
1032 {
1033 const TextureLayerData& layerData = textureData[levelNdx];
1034
1035 for (size_t layerNdx = 0; layerNdx < layerData.size(); layerNdx++)
1036 {
1037 layerDataOffset = getNextMultiple(offsetMultiples, layerDataOffset);
1038
1039 const tcu::ConstPixelBufferAccess& access = layerData[layerNdx];
1040 const tcu::PixelBufferAccess destAccess (access.getFormat(), access.getSize(), destPtr + layerDataOffset);
1041
1042 const VkBufferImageCopy layerRegion =
1043 {
1044 layerDataOffset, // VkDeviceSize bufferOffset;
1045 (deUint32)access.getWidth(), // deUint32 bufferRowLength;
1046 (deUint32)access.getHeight(), // deUint32 bufferImageHeight;
1047 { // VkImageSubresourceLayers imageSubresource;
1048 aspectMask, // VkImageAspectFlags aspectMask;
1049 (deUint32)levelNdx, // uint32_t mipLevel;
1050 (deUint32)layerNdx, // uint32_t baseArrayLayer;
1051 1u // uint32_t layerCount;
1052 },
1053 { 0u, 0u, 0u }, // VkOffset3D imageOffset;
1054 { // VkExtent3D imageExtent;
1055 (deUint32)access.getWidth(),
1056 (deUint32)access.getHeight(),
1057 (deUint32)access.getDepth()
1058 }
1059 };
1060
1061 copyRegions.push_back(layerRegion);
1062 tcu::copy(destAccess, access);
1063
1064 layerDataOffset += access.getWidth() * access.getHeight() * access.getDepth() * access.getFormat().getPixelSize();
1065 }
1066 }
1067 }
1068
1069 flushAlloc(vk, vkDevice, *bufferAlloc);
1070
1071 copyBufferToImage(vk, vkDevice, queue, queueFamilyIndex, *buffer, bufferSize, copyRegions, DE_NULL, aspectMask, mipLevels, arrayLayers, destImage);
1072 }
1073
clearImage(const tcu::Sampler & refSampler,deUint32 mipLevels,deUint32 arrayLayers,VkImage destImage)1074 void ShaderRenderCaseInstance::clearImage (const tcu::Sampler& refSampler,
1075 deUint32 mipLevels,
1076 deUint32 arrayLayers,
1077 VkImage destImage)
1078 {
1079 const VkDevice vkDevice = m_context.getDevice();
1080 const DeviceInterface& vk = m_context.getDeviceInterface();
1081 const VkQueue queue = m_context.getUniversalQueue();
1082 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
1083
1084 const bool isShadowSampler = refSampler.compare != tcu::Sampler::COMPAREMODE_NONE;
1085 const VkImageAspectFlags aspectMask = isShadowSampler ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
1086 Move<VkCommandPool> cmdPool;
1087 Move<VkCommandBuffer> cmdBuffer;
1088
1089 VkClearValue clearValue;
1090 deMemset(&clearValue, 0, sizeof(clearValue));
1091
1092
1093 // Create command pool and buffer
1094 cmdPool = createCommandPool(vk, vkDevice, VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, queueFamilyIndex);
1095 cmdBuffer = allocateCommandBuffer(vk, vkDevice, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
1096
1097 const VkImageMemoryBarrier preImageBarrier =
1098 {
1099 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType;
1100 DE_NULL, // const void* pNext;
1101 0u, // VkAccessFlags srcAccessMask;
1102 VK_ACCESS_TRANSFER_WRITE_BIT, // VkAccessFlags dstAccessMask;
1103 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout oldLayout;
1104 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // VkImageLayout newLayout;
1105 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
1106 VK_QUEUE_FAMILY_IGNORED, // deUint32 dstQueueFamilyIndex;
1107 destImage, // VkImage image;
1108 { // VkImageSubresourceRange subresourceRange;
1109 aspectMask, // VkImageAspect aspect;
1110 0u, // deUint32 baseMipLevel;
1111 mipLevels, // deUint32 mipLevels;
1112 0u, // deUint32 baseArraySlice;
1113 arrayLayers // deUint32 arraySize;
1114 }
1115 };
1116
1117 const VkImageMemoryBarrier postImageBarrier =
1118 {
1119 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType;
1120 DE_NULL, // const void* pNext;
1121 VK_ACCESS_TRANSFER_WRITE_BIT, // VkAccessFlags srcAccessMask;
1122 VK_ACCESS_SHADER_READ_BIT, // VkAccessFlags dstAccessMask;
1123 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // VkImageLayout oldLayout;
1124 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // VkImageLayout newLayout;
1125 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
1126 VK_QUEUE_FAMILY_IGNORED, // deUint32 dstQueueFamilyIndex;
1127 destImage, // VkImage image;
1128 { // VkImageSubresourceRange subresourceRange;
1129 aspectMask, // VkImageAspect aspect;
1130 0u, // deUint32 baseMipLevel;
1131 mipLevels, // deUint32 mipLevels;
1132 0u, // deUint32 baseArraySlice;
1133 arrayLayers // deUint32 arraySize;
1134 }
1135 };
1136
1137 const VkImageSubresourceRange clearRange =
1138 {
1139 aspectMask, // VkImageAspectFlags aspectMask;
1140 0u, // deUint32 baseMipLevel;
1141 mipLevels, // deUint32 levelCount;
1142 0u, // deUint32 baseArrayLayer;
1143 arrayLayers // deUint32 layerCount;
1144 };
1145
1146 // Copy buffer to image
1147 beginCommandBuffer(vk, *cmdBuffer);
1148 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);
1149 if (aspectMask == VK_IMAGE_ASPECT_COLOR_BIT)
1150 {
1151 vk.cmdClearColorImage(*cmdBuffer, destImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearValue.color, 1, &clearRange);
1152 }
1153 else
1154 {
1155 vk.cmdClearDepthStencilImage(*cmdBuffer, destImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearValue.depthStencil, 1, &clearRange);
1156 }
1157 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);
1158 endCommandBuffer(vk, *cmdBuffer);
1159
1160 submitCommandsAndWait(vk, vkDevice, queue, cmdBuffer.get());
1161 }
1162
mipLevelExtents(const VkExtent3D & baseExtents,const deUint32 mipLevel)1163 VkExtent3D mipLevelExtents (const VkExtent3D& baseExtents, const deUint32 mipLevel)
1164 {
1165 VkExtent3D result;
1166
1167 result.width = std::max(baseExtents.width >> mipLevel, 1u);
1168 result.height = std::max(baseExtents.height >> mipLevel, 1u);
1169 result.depth = std::max(baseExtents.depth >> mipLevel, 1u);
1170
1171 return result;
1172 }
1173
alignedDivide(const VkExtent3D & extent,const VkExtent3D & divisor)1174 tcu::UVec3 alignedDivide (const VkExtent3D& extent, const VkExtent3D& divisor)
1175 {
1176 tcu::UVec3 result;
1177
1178 result.x() = extent.width / divisor.width + ((extent.width % divisor.width != 0) ? 1u : 0u);
1179 result.y() = extent.height / divisor.height + ((extent.height % divisor.height != 0) ? 1u : 0u);
1180 result.z() = extent.depth / divisor.depth + ((extent.depth % divisor.depth != 0) ? 1u : 0u);
1181
1182 return result;
1183 }
1184
isImageSizeSupported(const VkImageType imageType,const tcu::UVec3 & imageSize,const vk::VkPhysicalDeviceLimits & limits)1185 bool isImageSizeSupported (const VkImageType imageType, const tcu::UVec3& imageSize, const vk::VkPhysicalDeviceLimits& limits)
1186 {
1187 switch (imageType)
1188 {
1189 case VK_IMAGE_TYPE_1D:
1190 return (imageSize.x() <= limits.maxImageDimension1D
1191 && imageSize.y() == 1
1192 && imageSize.z() == 1);
1193 case VK_IMAGE_TYPE_2D:
1194 return (imageSize.x() <= limits.maxImageDimension2D
1195 && imageSize.y() <= limits.maxImageDimension2D
1196 && imageSize.z() == 1);
1197 case VK_IMAGE_TYPE_3D:
1198 return (imageSize.x() <= limits.maxImageDimension3D
1199 && imageSize.y() <= limits.maxImageDimension3D
1200 && imageSize.z() <= limits.maxImageDimension3D);
1201 default:
1202 DE_FATAL("Unknown image type");
1203 return false;
1204 }
1205 }
1206
checkSparseSupport(const VkImageCreateInfo & imageInfo) const1207 void ShaderRenderCaseInstance::checkSparseSupport (const VkImageCreateInfo& imageInfo) const
1208 {
1209 const InstanceInterface& instance = getInstanceInterface();
1210 const VkPhysicalDevice physicalDevice = getPhysicalDevice();
1211 const VkPhysicalDeviceFeatures deviceFeatures = getPhysicalDeviceFeatures(instance, physicalDevice);
1212
1213 const std::vector<VkSparseImageFormatProperties> sparseImageFormatPropVec = getPhysicalDeviceSparseImageFormatProperties(
1214 instance, physicalDevice, imageInfo.format, imageInfo.imageType, imageInfo.samples, imageInfo.usage, imageInfo.tiling);
1215
1216 if (!deviceFeatures.shaderResourceResidency)
1217 TCU_THROW(NotSupportedError, "Required feature: shaderResourceResidency.");
1218
1219 if (!deviceFeatures.sparseBinding)
1220 TCU_THROW(NotSupportedError, "Required feature: sparseBinding.");
1221
1222 if (imageInfo.imageType == VK_IMAGE_TYPE_2D && !deviceFeatures.sparseResidencyImage2D)
1223 TCU_THROW(NotSupportedError, "Required feature: sparseResidencyImage2D.");
1224
1225 if (imageInfo.imageType == VK_IMAGE_TYPE_3D && !deviceFeatures.sparseResidencyImage3D)
1226 TCU_THROW(NotSupportedError, "Required feature: sparseResidencyImage3D.");
1227
1228 if (sparseImageFormatPropVec.size() == 0)
1229 TCU_THROW(NotSupportedError, "The image format does not support sparse operations");
1230 }
1231
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)1232 void ShaderRenderCaseInstance::uploadSparseImage (const tcu::TextureFormat& texFormat,
1233 const TextureData& textureData,
1234 const tcu::Sampler& refSampler,
1235 const deUint32 mipLevels,
1236 const deUint32 arrayLayers,
1237 const VkImage sparseImage,
1238 const VkImageCreateInfo& imageCreateInfo,
1239 const tcu::UVec3 texSize)
1240 {
1241 const VkDevice vkDevice = getDevice();
1242 const DeviceInterface& vk = getDeviceInterface();
1243 const VkPhysicalDevice physicalDevice = getPhysicalDevice();
1244 const VkQueue queue = getUniversalQueue();
1245 const VkQueue sparseQueue = getSparseQueue();
1246 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
1247 const InstanceInterface& instance = getInstanceInterface();
1248 const VkPhysicalDeviceProperties deviceProperties = getPhysicalDeviceProperties(instance, physicalDevice);
1249 const bool isShadowSampler = refSampler.compare != tcu::Sampler::COMPAREMODE_NONE;
1250 const VkImageAspectFlags aspectMask = isShadowSampler ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
1251 const Unique<VkSemaphore> imageMemoryBindSemaphore(createSemaphore(vk, vkDevice));
1252 Move<VkBuffer> buffer;
1253 deUint32 bufferSize = 0u;
1254 de::MovePtr<Allocation> bufferAlloc;
1255 std::vector<VkBufferImageCopy> copyRegions;
1256 std::vector<deUint32> offsetMultiples;
1257
1258 offsetMultiples.push_back(4u);
1259 offsetMultiples.push_back(texFormat.getPixelSize());
1260
1261 if (isImageSizeSupported(imageCreateInfo.imageType, texSize, deviceProperties.limits) == false)
1262 TCU_THROW(NotSupportedError, "Image size not supported for device.");
1263
1264 allocateAndBindSparseImage(vk, vkDevice, physicalDevice, instance, imageCreateInfo, *imageMemoryBindSemaphore, sparseQueue, m_memAlloc, m_allocations, texFormat, sparseImage);
1265
1266 // Calculate buffer size
1267 for (TextureData::const_iterator mit = textureData.begin(); mit != textureData.end(); ++mit)
1268 {
1269 for (TextureLayerData::const_iterator lit = mit->begin(); lit != mit->end(); ++lit)
1270 {
1271 const tcu::ConstPixelBufferAccess& access = *lit;
1272
1273 bufferSize = getNextMultiple(offsetMultiples, bufferSize);
1274 bufferSize += access.getWidth() * access.getHeight() * access.getDepth() * access.getFormat().getPixelSize();
1275 }
1276 }
1277
1278 {
1279 // Create source buffer
1280 const VkBufferCreateInfo bufferParams =
1281 {
1282 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
1283 DE_NULL, // const void* pNext;
1284 0u, // VkBufferCreateFlags flags;
1285 bufferSize, // VkDeviceSize size;
1286 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, // VkBufferUsageFlags usage;
1287 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
1288 0u, // deUint32 queueFamilyIndexCount;
1289 DE_NULL, // const deUint32* pQueueFamilyIndices;
1290 };
1291
1292 buffer = createBuffer(vk, vkDevice, &bufferParams);
1293 bufferAlloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), MemoryRequirement::HostVisible);
1294
1295 VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, bufferAlloc->getMemory(), bufferAlloc->getOffset()));
1296 }
1297
1298 // Get copy regions and write buffer data
1299 {
1300 deUint32 layerDataOffset = 0;
1301 deUint8* destPtr = (deUint8*)bufferAlloc->getHostPtr();
1302
1303 for (size_t levelNdx = 0; levelNdx < textureData.size(); levelNdx++)
1304 {
1305 const TextureLayerData& layerData = textureData[levelNdx];
1306
1307 for (size_t layerNdx = 0; layerNdx < layerData.size(); layerNdx++)
1308 {
1309 layerDataOffset = getNextMultiple(offsetMultiples, layerDataOffset);
1310
1311 const tcu::ConstPixelBufferAccess& access = layerData[layerNdx];
1312 const tcu::PixelBufferAccess destAccess (access.getFormat(), access.getSize(), destPtr + layerDataOffset);
1313
1314 const VkBufferImageCopy layerRegion =
1315 {
1316 layerDataOffset, // VkDeviceSize bufferOffset;
1317 (deUint32)access.getWidth(), // deUint32 bufferRowLength;
1318 (deUint32)access.getHeight(), // deUint32 bufferImageHeight;
1319 { // VkImageSubresourceLayers imageSubresource;
1320 aspectMask, // VkImageAspectFlags aspectMask;
1321 (deUint32)levelNdx, // uint32_t mipLevel;
1322 (deUint32)layerNdx, // uint32_t baseArrayLayer;
1323 1u // uint32_t layerCount;
1324 },
1325 { 0u, 0u, 0u }, // VkOffset3D imageOffset;
1326 { // VkExtent3D imageExtent;
1327 (deUint32)access.getWidth(),
1328 (deUint32)access.getHeight(),
1329 (deUint32)access.getDepth()
1330 }
1331 };
1332
1333 copyRegions.push_back(layerRegion);
1334 tcu::copy(destAccess, access);
1335
1336 layerDataOffset += access.getWidth() * access.getHeight() * access.getDepth() * access.getFormat().getPixelSize();
1337 }
1338 }
1339 }
1340 copyBufferToImage(vk, vkDevice, queue, queueFamilyIndex, *buffer, bufferSize, copyRegions, &(*imageMemoryBindSemaphore), aspectMask, mipLevels, arrayLayers, sparseImage);
1341 }
1342
useSampler(deUint32 bindingLocation,deUint32 textureId)1343 void ShaderRenderCaseInstance::useSampler (deUint32 bindingLocation, deUint32 textureId)
1344 {
1345 DE_ASSERT(textureId < m_textures.size());
1346
1347 const TextureBinding& textureBinding = *m_textures[textureId];
1348 const TextureBinding::Type textureType = textureBinding.getType();
1349 const tcu::Sampler& refSampler = textureBinding.getSampler();
1350 const TextureBinding::Parameters& textureParams = textureBinding.getParameters();
1351 const bool isMSTexture = textureParams.samples != vk::VK_SAMPLE_COUNT_1_BIT;
1352 deUint32 mipLevels = 1u;
1353 deUint32 arrayLayers = 1u;
1354 tcu::TextureFormat texFormat;
1355 tcu::UVec3 texSize;
1356 TextureData textureData;
1357
1358 if (textureType == TextureBinding::TYPE_2D)
1359 {
1360 const tcu::Texture2D& texture = textureBinding.get2D();
1361
1362 texFormat = texture.getFormat();
1363 texSize = tcu::UVec3(texture.getWidth(), texture.getHeight(), 1u);
1364 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1365 arrayLayers = 1u;
1366
1367 textureData.resize(mipLevels);
1368
1369 for (deUint32 level = 0; level < mipLevels; ++level)
1370 {
1371 if (texture.isLevelEmpty(level))
1372 continue;
1373
1374 textureData[level].push_back(texture.getLevel(level));
1375 }
1376 }
1377 else if (textureType == TextureBinding::TYPE_CUBE_MAP)
1378 {
1379 const tcu::TextureCube& texture = textureBinding.getCube();
1380
1381 texFormat = texture.getFormat();
1382 texSize = tcu::UVec3(texture.getSize(), texture.getSize(), 1u);
1383 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1384 arrayLayers = 6u;
1385
1386 static const tcu::CubeFace cubeFaceMapping[tcu::CUBEFACE_LAST] =
1387 {
1388 tcu::CUBEFACE_POSITIVE_X,
1389 tcu::CUBEFACE_NEGATIVE_X,
1390 tcu::CUBEFACE_POSITIVE_Y,
1391 tcu::CUBEFACE_NEGATIVE_Y,
1392 tcu::CUBEFACE_POSITIVE_Z,
1393 tcu::CUBEFACE_NEGATIVE_Z
1394 };
1395
1396 textureData.resize(mipLevels);
1397
1398 for (deUint32 level = 0; level < mipLevels; ++level)
1399 {
1400 for (int faceNdx = 0; faceNdx < tcu::CUBEFACE_LAST; ++faceNdx)
1401 {
1402 tcu::CubeFace face = cubeFaceMapping[faceNdx];
1403
1404 if (texture.isLevelEmpty(face, level))
1405 continue;
1406
1407 textureData[level].push_back(texture.getLevelFace(level, face));
1408 }
1409 }
1410 }
1411 else if (textureType == TextureBinding::TYPE_2D_ARRAY)
1412 {
1413 const tcu::Texture2DArray& texture = textureBinding.get2DArray();
1414
1415 texFormat = texture.getFormat();
1416 texSize = tcu::UVec3(texture.getWidth(), texture.getHeight(), 1u);
1417 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1418 arrayLayers = (deUint32)texture.getNumLayers();
1419
1420 textureData.resize(mipLevels);
1421
1422 for (deUint32 level = 0; level < mipLevels; ++level)
1423 {
1424 if (texture.isLevelEmpty(level))
1425 continue;
1426
1427 const tcu::ConstPixelBufferAccess& levelLayers = texture.getLevel(level);
1428 const deUint32 layerSize = levelLayers.getWidth() * levelLayers.getHeight() * levelLayers.getFormat().getPixelSize();
1429
1430 for (deUint32 layer = 0; layer < arrayLayers; ++layer)
1431 {
1432 const deUint32 layerOffset = layerSize * layer;
1433 tcu::ConstPixelBufferAccess layerData (levelLayers.getFormat(), levelLayers.getWidth(), levelLayers.getHeight(), 1, (deUint8*)levelLayers.getDataPtr() + layerOffset);
1434 textureData[level].push_back(layerData);
1435 }
1436 }
1437 }
1438 else if (textureType == TextureBinding::TYPE_3D)
1439 {
1440 const tcu::Texture3D& texture = textureBinding.get3D();
1441
1442 texFormat = texture.getFormat();
1443 texSize = tcu::UVec3(texture.getWidth(), texture.getHeight(), texture.getDepth());
1444 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1445 arrayLayers = 1u;
1446
1447 textureData.resize(mipLevels);
1448
1449 for (deUint32 level = 0; level < mipLevels; ++level)
1450 {
1451 if (texture.isLevelEmpty(level))
1452 continue;
1453
1454 textureData[level].push_back(texture.getLevel(level));
1455 }
1456 }
1457 else if (textureType == TextureBinding::TYPE_1D)
1458 {
1459 const tcu::Texture1D& texture = textureBinding.get1D();
1460
1461 texFormat = texture.getFormat();
1462 texSize = tcu::UVec3(texture.getWidth(), 1, 1);
1463 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1464 arrayLayers = 1u;
1465
1466 textureData.resize(mipLevels);
1467
1468 for (deUint32 level = 0; level < mipLevels; ++level)
1469 {
1470 if (texture.isLevelEmpty(level))
1471 continue;
1472
1473 textureData[level].push_back(texture.getLevel(level));
1474 }
1475 }
1476 else if (textureType == TextureBinding::TYPE_1D_ARRAY)
1477 {
1478 const tcu::Texture1DArray& texture = textureBinding.get1DArray();
1479
1480 texFormat = texture.getFormat();
1481 texSize = tcu::UVec3(texture.getWidth(), 1, 1);
1482 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1483 arrayLayers = (deUint32)texture.getNumLayers();
1484
1485 textureData.resize(mipLevels);
1486
1487 for (deUint32 level = 0; level < mipLevels; ++level)
1488 {
1489 if (texture.isLevelEmpty(level))
1490 continue;
1491
1492 const tcu::ConstPixelBufferAccess& levelLayers = texture.getLevel(level);
1493 const deUint32 layerSize = levelLayers.getWidth() * levelLayers.getFormat().getPixelSize();
1494
1495 for (deUint32 layer = 0; layer < arrayLayers; ++layer)
1496 {
1497 const deUint32 layerOffset = layerSize * layer;
1498 tcu::ConstPixelBufferAccess layerData (levelLayers.getFormat(), levelLayers.getWidth(), 1, 1, (deUint8*)levelLayers.getDataPtr() + layerOffset);
1499 textureData[level].push_back(layerData);
1500 }
1501 }
1502 }
1503 else if (textureType == TextureBinding::TYPE_CUBE_ARRAY)
1504 {
1505 const tcu::TextureCubeArray& texture = textureBinding.getCubeArray();
1506 texFormat = texture.getFormat();
1507 texSize = tcu::UVec3(texture.getSize(), texture.getSize(), 1);
1508 mipLevels = isMSTexture ? 1u : (deUint32)texture.getNumLevels();
1509 arrayLayers = texture.getDepth();
1510
1511 textureData.resize(mipLevels);
1512
1513 for (deUint32 level = 0; level < mipLevels; ++level)
1514 {
1515 if (texture.isLevelEmpty(level))
1516 continue;
1517
1518 const tcu::ConstPixelBufferAccess& levelLayers = texture.getLevel(level);
1519 const deUint32 layerSize = levelLayers.getWidth() * levelLayers.getHeight() * levelLayers.getFormat().getPixelSize();
1520
1521 for (deUint32 layer = 0; layer < arrayLayers; ++layer)
1522 {
1523 const deUint32 layerOffset = layerSize * layer;
1524 tcu::ConstPixelBufferAccess layerData (levelLayers.getFormat(), levelLayers.getWidth(), levelLayers.getHeight(), 1, (deUint8*)levelLayers.getDataPtr() + layerOffset);
1525 textureData[level].push_back(layerData);
1526 }
1527 }
1528 }
1529 else
1530 {
1531 TCU_THROW(InternalError, "Invalid texture type");
1532 }
1533
1534 createSamplerUniform(bindingLocation, textureType, textureBinding.getParameters().initialization, texFormat, texSize, textureData, refSampler, mipLevels, arrayLayers, textureParams);
1535 }
1536
setPushConstantRanges(const deUint32 rangeCount,const vk::VkPushConstantRange * const pcRanges)1537 void ShaderRenderCaseInstance::setPushConstantRanges (const deUint32 rangeCount, const vk::VkPushConstantRange* const pcRanges)
1538 {
1539 m_pushConstantRanges.clear();
1540 for (deUint32 i = 0; i < rangeCount; ++i)
1541 {
1542 m_pushConstantRanges.push_back(pcRanges[i]);
1543 }
1544 }
1545
updatePushConstants(vk::VkCommandBuffer,vk::VkPipelineLayout)1546 void ShaderRenderCaseInstance::updatePushConstants (vk::VkCommandBuffer, vk::VkPipelineLayout)
1547 {
1548 }
1549
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)1550 void ShaderRenderCaseInstance::createSamplerUniform (deUint32 bindingLocation,
1551 TextureBinding::Type textureType,
1552 TextureBinding::Init textureInit,
1553 const tcu::TextureFormat& texFormat,
1554 const tcu::UVec3 texSize,
1555 const TextureData& textureData,
1556 const tcu::Sampler& refSampler,
1557 deUint32 mipLevels,
1558 deUint32 arrayLayers,
1559 TextureBinding::Parameters textureParams)
1560 {
1561 const VkDevice vkDevice = getDevice();
1562 const DeviceInterface& vk = getDeviceInterface();
1563 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
1564 const deUint32 sparseFamilyIndex = (m_imageBackingMode == IMAGE_BACKING_MODE_SPARSE) ? getSparseQueueFamilyIndex() : queueFamilyIndex;
1565
1566 const bool isShadowSampler = refSampler.compare != tcu::Sampler::COMPAREMODE_NONE;
1567 const VkImageAspectFlags aspectMask = isShadowSampler ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
1568 const VkImageViewType imageViewType = textureTypeToImageViewType(textureType);
1569 const VkImageType imageType = viewTypeToImageType(imageViewType);
1570 const VkSharingMode sharingMode = (queueFamilyIndex != sparseFamilyIndex) ? VK_SHARING_MODE_CONCURRENT : VK_SHARING_MODE_EXCLUSIVE;
1571 const VkFormat format = mapTextureFormat(texFormat);
1572 const VkImageUsageFlags imageUsageFlags = textureUsageFlags();
1573 const VkImageCreateFlags imageCreateFlags = textureCreateFlags(imageViewType, m_imageBackingMode);
1574
1575 const deUint32 queueIndexCount = (queueFamilyIndex != sparseFamilyIndex) ? 2 : 1;
1576 const deUint32 queueIndices[] =
1577 {
1578 queueFamilyIndex,
1579 sparseFamilyIndex
1580 };
1581
1582 Move<VkImage> vkTexture;
1583 de::MovePtr<Allocation> allocation;
1584
1585 // Create image
1586 const VkImageCreateInfo imageParams =
1587 {
1588 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType;
1589 DE_NULL, // const void* pNext;
1590 imageCreateFlags, // VkImageCreateFlags flags;
1591 imageType, // VkImageType imageType;
1592 format, // VkFormat format;
1593 { // VkExtent3D extent;
1594 texSize.x(),
1595 texSize.y(),
1596 texSize.z()
1597 },
1598 mipLevels, // deUint32 mipLevels;
1599 arrayLayers, // deUint32 arrayLayers;
1600 textureParams.samples, // VkSampleCountFlagBits samples;
1601 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling;
1602 imageUsageFlags, // VkImageUsageFlags usage;
1603 sharingMode, // VkSharingMode sharingMode;
1604 queueIndexCount, // deUint32 queueFamilyIndexCount;
1605 queueIndices, // const deUint32* pQueueFamilyIndices;
1606 VK_IMAGE_LAYOUT_UNDEFINED // VkImageLayout initialLayout;
1607 };
1608
1609 if (m_imageBackingMode == IMAGE_BACKING_MODE_SPARSE)
1610 {
1611 checkSparseSupport(imageParams);
1612 }
1613
1614 vkTexture = createImage(vk, vkDevice, &imageParams);
1615 allocation = m_memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *vkTexture), MemoryRequirement::Any);
1616
1617 if (m_imageBackingMode != IMAGE_BACKING_MODE_SPARSE)
1618 {
1619 VK_CHECK(vk.bindImageMemory(vkDevice, *vkTexture, allocation->getMemory(), allocation->getOffset()));
1620 }
1621
1622 switch (textureInit)
1623 {
1624 case TextureBinding::INIT_UPLOAD_DATA:
1625 {
1626 // upload*Image functions use cmdCopyBufferToImage, which is invalid for multisample images
1627 DE_ASSERT(textureParams.samples == VK_SAMPLE_COUNT_1_BIT);
1628
1629 if (m_imageBackingMode == IMAGE_BACKING_MODE_SPARSE)
1630 {
1631 uploadSparseImage(texFormat, textureData, refSampler, mipLevels, arrayLayers, *vkTexture, imageParams, texSize);
1632 }
1633 else
1634 {
1635 // Upload texture data
1636 uploadImage(texFormat, textureData, refSampler, mipLevels, arrayLayers, *vkTexture);
1637 }
1638 break;
1639 }
1640 case TextureBinding::INIT_CLEAR:
1641 clearImage(refSampler, mipLevels, arrayLayers, *vkTexture);
1642 break;
1643 default:
1644 DE_FATAL("Impossible");
1645 }
1646
1647 // Create sampler
1648 const auto& minMaxLod = textureParams.minMaxLod;
1649 const VkSamplerCreateInfo samplerParams = (minMaxLod
1650 ? mapSampler(refSampler, texFormat, minMaxLod.get().minLod, minMaxLod.get().maxLod)
1651 : mapSampler(refSampler, texFormat));
1652 Move<VkSampler> sampler = createSampler(vk, vkDevice, &samplerParams);
1653 const deUint32 baseMipLevel = textureParams.baseMipLevel;
1654 const vk::VkComponentMapping components = textureParams.componentMapping;
1655 const VkImageViewCreateInfo viewParams =
1656 {
1657 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
1658 NULL, // const voide* pNext;
1659 0u, // VkImageViewCreateFlags flags;
1660 *vkTexture, // VkImage image;
1661 imageViewType, // VkImageViewType viewType;
1662 format, // VkFormat format;
1663 components, // VkChannelMapping channels;
1664 {
1665 aspectMask, // VkImageAspectFlags aspectMask;
1666 baseMipLevel, // deUint32 baseMipLevel;
1667 mipLevels - baseMipLevel, // deUint32 mipLevels;
1668 0, // deUint32 baseArraySlice;
1669 arrayLayers // deUint32 arraySize;
1670 }, // VkImageSubresourceRange subresourceRange;
1671 };
1672
1673 Move<VkImageView> imageView = createImageView(vk, vkDevice, &viewParams);
1674
1675 const vk::VkDescriptorImageInfo descriptor =
1676 {
1677 sampler.get(), // VkSampler sampler;
1678 imageView.get(), // VkImageView imageView;
1679 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, // VkImageLayout imageLayout;
1680 };
1681
1682 de::MovePtr<SamplerUniform> uniform(new SamplerUniform());
1683 uniform->type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1684 uniform->descriptor = descriptor;
1685 uniform->location = bindingLocation;
1686 uniform->image = VkImageSp(new vk::Unique<VkImage>(vkTexture));
1687 uniform->imageView = VkImageViewSp(new vk::Unique<VkImageView>(imageView));
1688 uniform->sampler = VkSamplerSp(new vk::Unique<VkSampler>(sampler));
1689 uniform->alloc = AllocationSp(allocation.release());
1690
1691 m_descriptorSetLayoutBuilder->addSingleSamplerBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, vk::VK_SHADER_STAGE_ALL, DE_NULL);
1692 m_descriptorPoolBuilder->addType(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1693
1694 m_uniformInfos.push_back(UniformInfoSp(new de::UniquePtr<UniformInfo>(uniform)));
1695 }
1696
setupDefaultInputs(void)1697 void ShaderRenderCaseInstance::setupDefaultInputs (void)
1698 {
1699 /* Configuration of the vertex input attributes:
1700 a_position is at location 0
1701 a_coords is at location 1
1702 a_unitCoords is at location 2
1703 a_one is at location 3
1704
1705 User attributes starts from at the location 4.
1706 */
1707
1708 DE_ASSERT(m_quadGrid);
1709 const QuadGrid& quadGrid = *m_quadGrid;
1710
1711 addAttribute(0u, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(tcu::Vec4), quadGrid.getNumVertices(), quadGrid.getPositions());
1712 addAttribute(1u, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(tcu::Vec4), quadGrid.getNumVertices(), quadGrid.getCoords());
1713 addAttribute(2u, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(tcu::Vec4), quadGrid.getNumVertices(), quadGrid.getUnitCoords());
1714 addAttribute(3u, VK_FORMAT_R32_SFLOAT, sizeof(float), quadGrid.getNumVertices(), quadGrid.getAttribOne());
1715
1716 static const struct
1717 {
1718 BaseAttributeType type;
1719 int userNdx;
1720 } userAttributes[] =
1721 {
1722 { A_IN0, 0 },
1723 { A_IN1, 1 },
1724 { A_IN2, 2 },
1725 { A_IN3, 3 }
1726 };
1727
1728 static const struct
1729 {
1730 BaseAttributeType matrixType;
1731 int numCols;
1732 int numRows;
1733 } matrices[] =
1734 {
1735 { MAT2, 2, 2 },
1736 { MAT2x3, 2, 3 },
1737 { MAT2x4, 2, 4 },
1738 { MAT3x2, 3, 2 },
1739 { MAT3, 3, 3 },
1740 { MAT3x4, 3, 4 },
1741 { MAT4x2, 4, 2 },
1742 { MAT4x3, 4, 3 },
1743 { MAT4, 4, 4 }
1744 };
1745
1746 for (size_t attrNdx = 0; attrNdx < m_enabledBaseAttributes.size(); attrNdx++)
1747 {
1748 for (int userNdx = 0; userNdx < DE_LENGTH_OF_ARRAY(userAttributes); userNdx++)
1749 {
1750 if (userAttributes[userNdx].type != m_enabledBaseAttributes[attrNdx].type)
1751 continue;
1752
1753 addAttribute(m_enabledBaseAttributes[attrNdx].location, VK_FORMAT_R32G32B32A32_SFLOAT, sizeof(tcu::Vec4), quadGrid.getNumVertices(), quadGrid.getUserAttrib(userNdx));
1754 }
1755
1756 for (int matNdx = 0; matNdx < DE_LENGTH_OF_ARRAY(matrices); matNdx++)
1757 {
1758
1759 if (matrices[matNdx].matrixType != m_enabledBaseAttributes[attrNdx].type)
1760 continue;
1761
1762 const int numCols = matrices[matNdx].numCols;
1763
1764 for (int colNdx = 0; colNdx < numCols; colNdx++)
1765 {
1766 addAttribute(m_enabledBaseAttributes[attrNdx].location + colNdx, VK_FORMAT_R32G32B32A32_SFLOAT, (deUint32)(4 * sizeof(float)), quadGrid.getNumVertices(), quadGrid.getUserAttrib(colNdx));
1767 }
1768 }
1769 }
1770 }
1771
render(deUint32 numVertices,deUint32 numTriangles,const deUint16 * indices,const tcu::Vec4 & constCoords)1772 void ShaderRenderCaseInstance::render (deUint32 numVertices,
1773 deUint32 numTriangles,
1774 const deUint16* indices,
1775 const tcu::Vec4& constCoords)
1776 {
1777 render(numVertices, numTriangles * 3, indices, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, constCoords);
1778 }
1779
render(deUint32 numVertices,deUint32 numIndices,const deUint16 * indices,VkPrimitiveTopology topology,const tcu::Vec4 & constCoords)1780 void ShaderRenderCaseInstance::render (deUint32 numVertices,
1781 deUint32 numIndices,
1782 const deUint16* indices,
1783 VkPrimitiveTopology topology,
1784 const tcu::Vec4& constCoords)
1785 {
1786 const VkDevice vkDevice = getDevice();
1787 const DeviceInterface& vk = getDeviceInterface();
1788 const VkQueue queue = getUniversalQueue();
1789 const deUint32 queueFamilyIndex = getUniversalQueueFamilyIndex();
1790
1791 vk::Move<vk::VkImage> colorImage;
1792 de::MovePtr<vk::Allocation> colorImageAlloc;
1793 vk::Move<vk::VkImageView> colorImageView;
1794 vk::Move<vk::VkImage> resolvedImage;
1795 de::MovePtr<vk::Allocation> resolvedImageAlloc;
1796 vk::Move<vk::VkImageView> resolvedImageView;
1797 vk::Move<vk::VkRenderPass> renderPass;
1798 vk::Move<vk::VkFramebuffer> framebuffer;
1799 vk::Move<vk::VkPipelineLayout> pipelineLayout;
1800 vk::Move<vk::VkPipeline> graphicsPipeline;
1801 vk::Move<vk::VkShaderModule> vertexShaderModule;
1802 vk::Move<vk::VkShaderModule> fragmentShaderModule;
1803 vk::Move<vk::VkBuffer> indexBuffer;
1804 de::MovePtr<vk::Allocation> indexBufferAlloc;
1805 vk::Move<vk::VkDescriptorSetLayout> descriptorSetLayout;
1806 vk::Move<vk::VkDescriptorPool> descriptorPool;
1807 vk::Move<vk::VkDescriptorSet> descriptorSet;
1808 vk::Move<vk::VkCommandPool> cmdPool;
1809 vk::Move<vk::VkCommandBuffer> cmdBuffer;
1810
1811 // Create color image
1812 {
1813 const VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1814 VkImageFormatProperties properties;
1815
1816 if ((getInstanceInterface().getPhysicalDeviceImageFormatProperties(getPhysicalDevice(),
1817 m_colorFormat,
1818 VK_IMAGE_TYPE_2D,
1819 VK_IMAGE_TILING_OPTIMAL,
1820 imageUsage,
1821 0u,
1822 &properties) == VK_ERROR_FORMAT_NOT_SUPPORTED))
1823 {
1824 TCU_THROW(NotSupportedError, "Format not supported");
1825 }
1826
1827 if ((properties.sampleCounts & m_sampleCount) != m_sampleCount)
1828 {
1829 TCU_THROW(NotSupportedError, "Format not supported");
1830 }
1831
1832 const VkImageCreateInfo colorImageParams =
1833 {
1834 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType;
1835 DE_NULL, // const void* pNext;
1836 0u, // VkImageCreateFlags flags;
1837 VK_IMAGE_TYPE_2D, // VkImageType imageType;
1838 m_colorFormat, // VkFormat format;
1839 { m_renderSize.x(), m_renderSize.y(), 1u }, // VkExtent3D extent;
1840 1u, // deUint32 mipLevels;
1841 1u, // deUint32 arraySize;
1842 m_sampleCount, // deUint32 samples;
1843 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling;
1844 imageUsage, // VkImageUsageFlags usage;
1845 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
1846 1u, // deUint32 queueFamilyCount;
1847 &queueFamilyIndex, // const deUint32* pQueueFamilyIndices;
1848 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout;
1849 };
1850
1851 colorImage = createImage(vk, vkDevice, &colorImageParams);
1852
1853 // Allocate and bind color image memory
1854 colorImageAlloc = m_memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *colorImage), MemoryRequirement::Any);
1855 VK_CHECK(vk.bindImageMemory(vkDevice, *colorImage, colorImageAlloc->getMemory(), colorImageAlloc->getOffset()));
1856 }
1857
1858 // Create color attachment view
1859 {
1860 const VkImageViewCreateInfo colorImageViewParams =
1861 {
1862 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
1863 DE_NULL, // const void* pNext;
1864 0u, // VkImageViewCreateFlags flags;
1865 *colorImage, // VkImage image;
1866 VK_IMAGE_VIEW_TYPE_2D, // VkImageViewType viewType;
1867 m_colorFormat, // VkFormat format;
1868 {
1869 VK_COMPONENT_SWIZZLE_R, // VkChannelSwizzle r;
1870 VK_COMPONENT_SWIZZLE_G, // VkChannelSwizzle g;
1871 VK_COMPONENT_SWIZZLE_B, // VkChannelSwizzle b;
1872 VK_COMPONENT_SWIZZLE_A // VkChannelSwizzle a;
1873 }, // VkChannelMapping channels;
1874 {
1875 VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
1876 0, // deUint32 baseMipLevel;
1877 1, // deUint32 mipLevels;
1878 0, // deUint32 baseArraySlice;
1879 1 // deUint32 arraySize;
1880 }, // VkImageSubresourceRange subresourceRange;
1881 };
1882
1883 colorImageView = createImageView(vk, vkDevice, &colorImageViewParams);
1884 }
1885
1886 if (isMultiSampling())
1887 {
1888 // Resolved Image
1889 {
1890 const VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1891 VkImageFormatProperties properties;
1892
1893 if ((getInstanceInterface().getPhysicalDeviceImageFormatProperties(getPhysicalDevice(),
1894 m_colorFormat,
1895 VK_IMAGE_TYPE_2D,
1896 VK_IMAGE_TILING_OPTIMAL,
1897 imageUsage,
1898 0,
1899 &properties) == VK_ERROR_FORMAT_NOT_SUPPORTED))
1900 {
1901 TCU_THROW(NotSupportedError, "Format not supported");
1902 }
1903
1904 const VkImageCreateInfo imageCreateInfo =
1905 {
1906 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType;
1907 DE_NULL, // const void* pNext;
1908 0u, // VkImageCreateFlags flags;
1909 VK_IMAGE_TYPE_2D, // VkImageType imageType;
1910 m_colorFormat, // VkFormat format;
1911 { m_renderSize.x(), m_renderSize.y(), 1u }, // VkExtent3D extent;
1912 1u, // deUint32 mipLevels;
1913 1u, // deUint32 arrayLayers;
1914 VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples;
1915 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling;
1916 imageUsage, // VkImageUsageFlags usage;
1917 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
1918 1u, // deUint32 queueFamilyIndexCount;
1919 &queueFamilyIndex, // const deUint32* pQueueFamilyIndices;
1920 VK_IMAGE_LAYOUT_UNDEFINED // VkImageLayout initialLayout;
1921 };
1922
1923 resolvedImage = vk::createImage(vk, vkDevice, &imageCreateInfo, DE_NULL);
1924 resolvedImageAlloc = m_memAlloc.allocate(getImageMemoryRequirements(vk, vkDevice, *resolvedImage), MemoryRequirement::Any);
1925 VK_CHECK(vk.bindImageMemory(vkDevice, *resolvedImage, resolvedImageAlloc->getMemory(), resolvedImageAlloc->getOffset()));
1926 }
1927
1928 // Resolved Image View
1929 {
1930 const VkImageViewCreateInfo imageViewCreateInfo =
1931 {
1932 VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType;
1933 DE_NULL, // const void* pNext;
1934 0u, // VkImageViewCreateFlags flags;
1935 *resolvedImage, // VkImage image;
1936 VK_IMAGE_VIEW_TYPE_2D, // VkImageViewType viewType;
1937 m_colorFormat, // VkFormat format;
1938 {
1939 VK_COMPONENT_SWIZZLE_R, // VkChannelSwizzle r;
1940 VK_COMPONENT_SWIZZLE_G, // VkChannelSwizzle g;
1941 VK_COMPONENT_SWIZZLE_B, // VkChannelSwizzle b;
1942 VK_COMPONENT_SWIZZLE_A // VkChannelSwizzle a;
1943 },
1944 {
1945 VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
1946 0u, // deUint32 baseMipLevel;
1947 1u, // deUint32 mipLevels;
1948 0u, // deUint32 baseArrayLayer;
1949 1u, // deUint32 arraySize;
1950 }, // VkImageSubresourceRange subresourceRange;
1951 };
1952
1953 resolvedImageView = vk::createImageView(vk, vkDevice, &imageViewCreateInfo, DE_NULL);
1954 }
1955 }
1956
1957 // Create render pass
1958 {
1959 const VkAttachmentDescription attachmentDescription[] =
1960 {
1961 {
1962 (VkAttachmentDescriptionFlags)0, // VkAttachmentDescriptionFlags flags;
1963 m_colorFormat, // VkFormat format;
1964 m_sampleCount, // deUint32 samples;
1965 VK_ATTACHMENT_LOAD_OP_CLEAR, // VkAttachmentLoadOp loadOp;
1966 VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp storeOp;
1967 VK_ATTACHMENT_LOAD_OP_DONT_CARE, // VkAttachmentLoadOp stencilLoadOp;
1968 VK_ATTACHMENT_STORE_OP_DONT_CARE, // VkAttachmentStoreOp stencilStoreOp;
1969 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout initialLayout;
1970 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout finalLayout;
1971 },
1972 {
1973 (VkAttachmentDescriptionFlags)0, // VkAttachmentDescriptionFlags flags;
1974 m_colorFormat, // VkFormat format;
1975 VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples;
1976 VK_ATTACHMENT_LOAD_OP_DONT_CARE, // VkAttachmentLoadOp loadOp;
1977 VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp storeOp;
1978 VK_ATTACHMENT_LOAD_OP_DONT_CARE, // VkAttachmentLoadOp stencilLoadOp;
1979 VK_ATTACHMENT_STORE_OP_DONT_CARE, // VkAttachmentStoreOp stencilStoreOp;
1980 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout initialLayout;
1981 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout finalLayout;
1982 }
1983 };
1984
1985 const VkAttachmentReference attachmentReference =
1986 {
1987 0u, // deUint32 attachment;
1988 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL // VkImageLayout layout;
1989 };
1990
1991 const VkAttachmentReference resolveAttachmentRef =
1992 {
1993 1u, // deUint32 attachment;
1994 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL // VkImageLayout layout;
1995 };
1996
1997 const VkSubpassDescription subpassDescription =
1998 {
1999 0u, // VkSubpassDescriptionFlags flags;
2000 VK_PIPELINE_BIND_POINT_GRAPHICS, // VkPipelineBindPoint pipelineBindPoint;
2001 0u, // deUint32 inputCount;
2002 DE_NULL, // constVkAttachmentReference* pInputAttachments;
2003 1u, // deUint32 colorCount;
2004 &attachmentReference, // constVkAttachmentReference* pColorAttachments;
2005 isMultiSampling() ? &resolveAttachmentRef : DE_NULL,// constVkAttachmentReference* pResolveAttachments;
2006 DE_NULL, // VkAttachmentReference depthStencilAttachment;
2007 0u, // deUint32 preserveCount;
2008 DE_NULL // constVkAttachmentReference* pPreserveAttachments;
2009 };
2010
2011 const VkRenderPassCreateInfo renderPassParams =
2012 {
2013 VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, // VkStructureType sType;
2014 DE_NULL, // const void* pNext;
2015 0u, // VkRenderPassCreateFlags flags;
2016 isMultiSampling() ? 2u : 1u, // deUint32 attachmentCount;
2017 attachmentDescription, // const VkAttachmentDescription* pAttachments;
2018 1u, // deUint32 subpassCount;
2019 &subpassDescription, // const VkSubpassDescription* pSubpasses;
2020 0u, // deUint32 dependencyCount;
2021 DE_NULL // const VkSubpassDependency* pDependencies;
2022 };
2023
2024 renderPass = createRenderPass(vk, vkDevice, &renderPassParams);
2025 }
2026
2027 // Create framebuffer
2028 {
2029 const VkImageView attachments[] =
2030 {
2031 *colorImageView,
2032 *resolvedImageView
2033 };
2034
2035 const VkFramebufferCreateInfo framebufferParams =
2036 {
2037 VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, // VkStructureType sType;
2038 DE_NULL, // const void* pNext;
2039 (VkFramebufferCreateFlags)0,
2040 *renderPass, // VkRenderPass renderPass;
2041 isMultiSampling() ? 2u : 1u, // deUint32 attachmentCount;
2042 attachments, // const VkImageView* pAttachments;
2043 (deUint32)m_renderSize.x(), // deUint32 width;
2044 (deUint32)m_renderSize.y(), // deUint32 height;
2045 1u // deUint32 layers;
2046 };
2047
2048 framebuffer = createFramebuffer(vk, vkDevice, &framebufferParams);
2049 }
2050
2051 // Create descriptors
2052 {
2053 setupUniforms(constCoords);
2054
2055 descriptorSetLayout = m_descriptorSetLayoutBuilder->build(vk, vkDevice);
2056 if (!m_uniformInfos.empty())
2057 {
2058 descriptorPool = m_descriptorPoolBuilder->build(vk, vkDevice, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
2059 const VkDescriptorSetAllocateInfo allocInfo =
2060 {
2061 VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2062 DE_NULL,
2063 *descriptorPool,
2064 1u,
2065 &descriptorSetLayout.get(),
2066 };
2067
2068 descriptorSet = allocateDescriptorSet(vk, vkDevice, &allocInfo);
2069 }
2070
2071 for (deUint32 i = 0; i < m_uniformInfos.size(); i++)
2072 {
2073 const UniformInfo* uniformInfo = m_uniformInfos[i].get()->get();
2074 deUint32 location = uniformInfo->location;
2075
2076 if (uniformInfo->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)
2077 {
2078 const BufferUniform* bufferInfo = dynamic_cast<const BufferUniform*>(uniformInfo);
2079
2080 m_descriptorSetUpdateBuilder->writeSingle(*descriptorSet, DescriptorSetUpdateBuilder::Location::binding(location), uniformInfo->type, &bufferInfo->descriptor);
2081 }
2082 else if (uniformInfo->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
2083 {
2084 const SamplerUniform* samplerInfo = dynamic_cast<const SamplerUniform*>(uniformInfo);
2085
2086 m_descriptorSetUpdateBuilder->writeSingle(*descriptorSet, DescriptorSetUpdateBuilder::Location::binding(location), uniformInfo->type, &samplerInfo->descriptor);
2087 }
2088 else
2089 DE_FATAL("Impossible");
2090 }
2091
2092 m_descriptorSetUpdateBuilder->update(vk, vkDevice);
2093 }
2094
2095 // Create pipeline layout
2096 {
2097 const VkPushConstantRange* const pcRanges = m_pushConstantRanges.empty() ? DE_NULL : &m_pushConstantRanges[0];
2098 const VkPipelineLayoutCreateInfo pipelineLayoutParams =
2099 {
2100 VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, // VkStructureType sType;
2101 DE_NULL, // const void* pNext;
2102 (VkPipelineLayoutCreateFlags)0,
2103 1u, // deUint32 descriptorSetCount;
2104 &*descriptorSetLayout, // const VkDescriptorSetLayout* pSetLayouts;
2105 deUint32(m_pushConstantRanges.size()), // deUint32 pushConstantRangeCount;
2106 pcRanges // const VkPushConstantRange* pPushConstantRanges;
2107 };
2108
2109 pipelineLayout = createPipelineLayout(vk, vkDevice, &pipelineLayoutParams);
2110 }
2111
2112 // Create shaders
2113 {
2114 vertexShaderModule = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get(m_vertexShaderName), 0);
2115 fragmentShaderModule = createShaderModule(vk, vkDevice, m_context.getBinaryCollection().get(m_fragmentShaderName), 0);
2116 }
2117
2118 // Create pipeline
2119 {
2120 // Add test case specific attributes
2121 if (m_attribFunc)
2122 m_attribFunc(*this, numVertices);
2123
2124 // Add base attributes
2125 setupDefaultInputs();
2126
2127 const VkPipelineVertexInputStateCreateInfo vertexInputStateParams =
2128 {
2129 VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, // VkStructureType sType;
2130 DE_NULL, // const void* pNext;
2131 (VkPipelineVertexInputStateCreateFlags)0,
2132 (deUint32)m_vertexBindingDescription.size(), // deUint32 bindingCount;
2133 &m_vertexBindingDescription[0], // const VkVertexInputBindingDescription* pVertexBindingDescriptions;
2134 (deUint32)m_vertexAttributeDescription.size(), // deUint32 attributeCount;
2135 &m_vertexAttributeDescription[0], // const VkVertexInputAttributeDescription* pVertexAttributeDescriptions;
2136 };
2137
2138 const std::vector<VkViewport> viewports (1, makeViewport(m_renderSize));
2139 const std::vector<VkRect2D> scissors (1, makeRect2D(m_renderSize));
2140
2141 const VkPipelineMultisampleStateCreateInfo multisampleStateParams =
2142 {
2143 VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, // VkStructureType sType;
2144 DE_NULL, // const void* pNext;
2145 0u, // VkPipelineMultisampleStateCreateFlags flags;
2146 m_sampleCount, // VkSampleCountFlagBits rasterizationSamples;
2147 VK_FALSE, // VkBool32 sampleShadingEnable;
2148 0.0f, // float minSampleShading;
2149 DE_NULL, // const VkSampleMask* pSampleMask;
2150 VK_FALSE, // VkBool32 alphaToCoverageEnable;
2151 VK_FALSE // VkBool32 alphaToOneEnable;
2152 };
2153
2154 graphicsPipeline = makeGraphicsPipeline(vk, // const DeviceInterface& vk
2155 vkDevice, // const VkDevice device
2156 *pipelineLayout, // const VkPipelineLayout pipelineLayout
2157 *vertexShaderModule, // const VkShaderModule vertexShaderModule
2158 DE_NULL, // const VkShaderModule tessellationControlShaderModule
2159 DE_NULL, // const VkShaderModule tessellationEvalShaderModule
2160 DE_NULL, // const VkShaderModule geometryShaderModule
2161 *fragmentShaderModule, // const VkShaderModule fragmentShaderModule
2162 *renderPass, // const VkRenderPass renderPass
2163 viewports, // const std::vector<VkViewport>& viewports
2164 scissors, // const std::vector<VkRect2D>& scissors
2165 topology, // const VkPrimitiveTopology topology
2166 0u, // const deUint32 subpass
2167 0u, // const deUint32 patchControlPoints
2168 &vertexInputStateParams, // const VkPipelineVertexInputStateCreateInfo* vertexInputStateCreateInfo
2169 DE_NULL, // const VkPipelineRasterizationStateCreateInfo* rasterizationStateCreateInfo
2170 &multisampleStateParams); // const VkPipelineMultisampleStateCreateInfo* multisampleStateCreateInfo
2171 }
2172
2173 // Create vertex indices buffer
2174 if (numIndices != 0)
2175 {
2176 const VkDeviceSize indexBufferSize = numIndices * sizeof(deUint16);
2177 const VkBufferCreateInfo indexBufferParams =
2178 {
2179 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
2180 DE_NULL, // const void* pNext;
2181 0u, // VkBufferCreateFlags flags;
2182 indexBufferSize, // VkDeviceSize size;
2183 VK_BUFFER_USAGE_INDEX_BUFFER_BIT, // VkBufferUsageFlags usage;
2184 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
2185 1u, // deUint32 queueFamilyCount;
2186 &queueFamilyIndex // const deUint32* pQueueFamilyIndices;
2187 };
2188
2189 indexBuffer = createBuffer(vk, vkDevice, &indexBufferParams);
2190 indexBufferAlloc = m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *indexBuffer), MemoryRequirement::HostVisible);
2191
2192 VK_CHECK(vk.bindBufferMemory(vkDevice, *indexBuffer, indexBufferAlloc->getMemory(), indexBufferAlloc->getOffset()));
2193
2194 // Load vertice indices into buffer
2195 deMemcpy(indexBufferAlloc->getHostPtr(), indices, (size_t)indexBufferSize);
2196 flushAlloc(vk, vkDevice, *indexBufferAlloc);
2197 }
2198
2199 // Create command pool
2200 cmdPool = createCommandPool(vk, vkDevice, VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, queueFamilyIndex);
2201
2202 // Create command buffer
2203 {
2204 cmdBuffer = allocateCommandBuffer(vk, vkDevice, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
2205
2206 beginCommandBuffer(vk, *cmdBuffer);
2207
2208 {
2209 const VkImageMemoryBarrier imageBarrier =
2210 {
2211 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType;
2212 DE_NULL, // const void* pNext;
2213 0u, // VkAccessFlags srcAccessMask;
2214 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, // VkAccessFlags dstAccessMask;
2215 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout oldLayout;
2216 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout newLayout;
2217 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
2218 VK_QUEUE_FAMILY_IGNORED, // deUint32 dstQueueFamilyIndex;
2219 *colorImage, // VkImage image;
2220 { // VkImageSubresourceRange subresourceRange;
2221 VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
2222 0u, // deUint32 baseMipLevel;
2223 1u, // deUint32 mipLevels;
2224 0u, // deUint32 baseArrayLayer;
2225 1u, // deUint32 arraySize;
2226 }
2227 };
2228
2229 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);
2230
2231 if (isMultiSampling()) {
2232 // add multisample barrier
2233 const VkImageMemoryBarrier multiSampleImageBarrier =
2234 {
2235 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // VkStructureType sType;
2236 DE_NULL, // const void* pNext;
2237 0u, // VkAccessFlags srcAccessMask;
2238 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, // VkAccessFlags dstAccessMask;
2239 VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout oldLayout;
2240 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // VkImageLayout newLayout;
2241 VK_QUEUE_FAMILY_IGNORED, // deUint32 srcQueueFamilyIndex;
2242 VK_QUEUE_FAMILY_IGNORED, // deUint32 dstQueueFamilyIndex;
2243 *resolvedImage, // VkImage image;
2244 { // VkImageSubresourceRange subresourceRange;
2245 VK_IMAGE_ASPECT_COLOR_BIT, // VkImageAspectFlags aspectMask;
2246 0u, // deUint32 baseMipLevel;
2247 1u, // deUint32 mipLevels;
2248 0u, // deUint32 baseArrayLayer;
2249 1u, // deUint32 arraySize;
2250 }
2251 };
2252
2253 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);
2254 }
2255 }
2256
2257 beginRenderPass(vk, *cmdBuffer, *renderPass, *framebuffer, makeRect2D(0, 0, m_renderSize.x(), m_renderSize.y()), m_clearColor);
2258
2259 updatePushConstants(*cmdBuffer, *pipelineLayout);
2260 vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *graphicsPipeline);
2261 if (!m_uniformInfos.empty())
2262 vk.cmdBindDescriptorSets(*cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *pipelineLayout, 0u, 1, &*descriptorSet, 0u, DE_NULL);
2263
2264 const deUint32 numberOfVertexAttributes = (deUint32)m_vertexBuffers.size();
2265 const std::vector<VkDeviceSize> offsets(numberOfVertexAttributes, 0);
2266
2267 std::vector<VkBuffer> buffers(numberOfVertexAttributes);
2268 for (size_t i = 0; i < numberOfVertexAttributes; i++)
2269 {
2270 buffers[i] = m_vertexBuffers[i].get()->get();
2271 }
2272
2273 vk.cmdBindVertexBuffers(*cmdBuffer, 0, numberOfVertexAttributes, &buffers[0], &offsets[0]);
2274 if (numIndices != 0)
2275 {
2276 vk.cmdBindIndexBuffer(*cmdBuffer, *indexBuffer, 0, VK_INDEX_TYPE_UINT16);
2277 vk.cmdDrawIndexed(*cmdBuffer, numIndices, 1, 0, 0, 0);
2278 }
2279 else
2280 vk.cmdDraw(*cmdBuffer, numVertices, 1, 0, 0);
2281
2282 endRenderPass(vk, *cmdBuffer);
2283 endCommandBuffer(vk, *cmdBuffer);
2284 }
2285
2286 // Execute Draw
2287 submitCommandsAndWait(vk, vkDevice, queue, cmdBuffer.get());
2288
2289 // Read back the result
2290 {
2291 const tcu::TextureFormat resultFormat = mapVkFormat(m_colorFormat);
2292 const VkDeviceSize imageSizeBytes = (VkDeviceSize)(resultFormat.getPixelSize() * m_renderSize.x() * m_renderSize.y());
2293 const VkBufferCreateInfo readImageBufferParams =
2294 {
2295 VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType;
2296 DE_NULL, // const void* pNext;
2297 0u, // VkBufferCreateFlags flags;
2298 imageSizeBytes, // VkDeviceSize size;
2299 VK_BUFFER_USAGE_TRANSFER_DST_BIT, // VkBufferUsageFlags usage;
2300 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode;
2301 1u, // deUint32 queueFamilyCount;
2302 &queueFamilyIndex, // const deUint32* pQueueFamilyIndices;
2303 };
2304 const Unique<VkBuffer> readImageBuffer (createBuffer(vk, vkDevice, &readImageBufferParams));
2305 const de::UniquePtr<Allocation> readImageBufferMemory (m_memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *readImageBuffer), MemoryRequirement::HostVisible));
2306
2307 VK_CHECK(vk.bindBufferMemory(vkDevice, *readImageBuffer, readImageBufferMemory->getMemory(), readImageBufferMemory->getOffset()));
2308
2309 // Copy image to buffer
2310 const Move<VkCommandBuffer> resultCmdBuffer = allocateCommandBuffer(vk, vkDevice, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY);
2311
2312 beginCommandBuffer(vk, *resultCmdBuffer);
2313
2314 copyImageToBuffer(vk, *resultCmdBuffer, isMultiSampling() ? *resolvedImage : *colorImage, *readImageBuffer, tcu::IVec2(m_renderSize.x(), m_renderSize.y()));
2315
2316 endCommandBuffer(vk, *resultCmdBuffer);
2317
2318 submitCommandsAndWait(vk, vkDevice, queue, resultCmdBuffer.get());
2319
2320 invalidateAlloc(vk, vkDevice, *readImageBufferMemory);
2321
2322 const tcu::ConstPixelBufferAccess resultAccess (resultFormat, m_renderSize.x(), m_renderSize.y(), 1, readImageBufferMemory->getHostPtr());
2323
2324 m_resultImage.setStorage(resultFormat, m_renderSize.x(), m_renderSize.y());
2325 tcu::copy(m_resultImage.getAccess(), resultAccess);
2326 }
2327 }
2328
computeVertexReference(tcu::Surface & result,const QuadGrid & quadGrid)2329 void ShaderRenderCaseInstance::computeVertexReference (tcu::Surface& result, const QuadGrid& quadGrid)
2330 {
2331 DE_ASSERT(m_evaluator);
2332
2333 // Buffer info.
2334 const int width = result.getWidth();
2335 const int height = result.getHeight();
2336 const int gridSize = quadGrid.getGridSize();
2337 const int stride = gridSize + 1;
2338 const bool hasAlpha = true; // \todo [2015-09-07 elecro] add correct alpha check
2339 ShaderEvalContext evalCtx (quadGrid);
2340
2341 // Evaluate color for each vertex.
2342 std::vector<tcu::Vec4> colors ((gridSize + 1) * (gridSize + 1));
2343 for (int y = 0; y < gridSize+1; y++)
2344 for (int x = 0; x < gridSize+1; x++)
2345 {
2346 const float sx = (float)x / (float)gridSize;
2347 const float sy = (float)y / (float)gridSize;
2348 const int vtxNdx = ((y * (gridSize+1)) + x);
2349
2350 evalCtx.reset(sx, sy);
2351 m_evaluator->evaluate(evalCtx);
2352 DE_ASSERT(!evalCtx.isDiscarded); // Discard is not available in vertex shader.
2353 tcu::Vec4 color = evalCtx.color;
2354
2355 if (!hasAlpha)
2356 color.w() = 1.0f;
2357
2358 colors[vtxNdx] = color;
2359 }
2360
2361 // Render quads.
2362 for (int y = 0; y < gridSize; y++)
2363 for (int x = 0; x < gridSize; x++)
2364 {
2365 const float x0 = (float)x / (float)gridSize;
2366 const float x1 = (float)(x + 1) / (float)gridSize;
2367 const float y0 = (float)y / (float)gridSize;
2368 const float y1 = (float)(y + 1) / (float)gridSize;
2369
2370 const float sx0 = x0 * (float)width;
2371 const float sx1 = x1 * (float)width;
2372 const float sy0 = y0 * (float)height;
2373 const float sy1 = y1 * (float)height;
2374 const float oosx = 1.0f / (sx1 - sx0);
2375 const float oosy = 1.0f / (sy1 - sy0);
2376
2377 const int ix0 = deCeilFloatToInt32(sx0 - 0.5f);
2378 const int ix1 = deCeilFloatToInt32(sx1 - 0.5f);
2379 const int iy0 = deCeilFloatToInt32(sy0 - 0.5f);
2380 const int iy1 = deCeilFloatToInt32(sy1 - 0.5f);
2381
2382 const int v00 = (y * stride) + x;
2383 const int v01 = (y * stride) + x + 1;
2384 const int v10 = ((y + 1) * stride) + x;
2385 const int v11 = ((y + 1) * stride) + x + 1;
2386 const tcu::Vec4 c00 = colors[v00];
2387 const tcu::Vec4 c01 = colors[v01];
2388 const tcu::Vec4 c10 = colors[v10];
2389 const tcu::Vec4 c11 = colors[v11];
2390
2391 //printf("(%d,%d) -> (%f..%f, %f..%f) (%d..%d, %d..%d)\n", x, y, sx0, sx1, sy0, sy1, ix0, ix1, iy0, iy1);
2392
2393 for (int iy = iy0; iy < iy1; iy++)
2394 for (int ix = ix0; ix < ix1; ix++)
2395 {
2396 DE_ASSERT(deInBounds32(ix, 0, width));
2397 DE_ASSERT(deInBounds32(iy, 0, height));
2398
2399 const float sfx = (float)ix + 0.5f;
2400 const float sfy = (float)iy + 0.5f;
2401 const float fx1 = deFloatClamp((sfx - sx0) * oosx, 0.0f, 1.0f);
2402 const float fy1 = deFloatClamp((sfy - sy0) * oosy, 0.0f, 1.0f);
2403
2404 // Triangle quad interpolation.
2405 const bool tri = fx1 + fy1 <= 1.0f;
2406 const float tx = tri ? fx1 : (1.0f-fx1);
2407 const float ty = tri ? fy1 : (1.0f-fy1);
2408 const tcu::Vec4& t0 = tri ? c00 : c11;
2409 const tcu::Vec4& t1 = tri ? c01 : c10;
2410 const tcu::Vec4& t2 = tri ? c10 : c01;
2411 const tcu::Vec4 color = t0 + (t1-t0)*tx + (t2-t0)*ty;
2412
2413 result.setPixel(ix, iy, tcu::RGBA(color));
2414 }
2415 }
2416 }
2417
computeFragmentReference(tcu::Surface & result,const QuadGrid & quadGrid)2418 void ShaderRenderCaseInstance::computeFragmentReference (tcu::Surface& result, const QuadGrid& quadGrid)
2419 {
2420 DE_ASSERT(m_evaluator);
2421
2422 // Buffer info.
2423 const int width = result.getWidth();
2424 const int height = result.getHeight();
2425 const bool hasAlpha = true; // \todo [2015-09-07 elecro] add correct alpha check
2426 ShaderEvalContext evalCtx (quadGrid);
2427
2428 // Render.
2429 for (int y = 0; y < height; y++)
2430 for (int x = 0; x < width; x++)
2431 {
2432 const float sx = ((float)x + 0.5f) / (float)width;
2433 const float sy = ((float)y + 0.5f) / (float)height;
2434
2435 evalCtx.reset(sx, sy);
2436 m_evaluator->evaluate(evalCtx);
2437 // Select either clear color or computed color based on discarded bit.
2438 tcu::Vec4 color = evalCtx.isDiscarded ? m_clearColor : evalCtx.color;
2439
2440 if (!hasAlpha)
2441 color.w() = 1.0f;
2442
2443 result.setPixel(x, y, tcu::RGBA(color));
2444 }
2445 }
2446
compareImages(const tcu::Surface & resImage,const tcu::Surface & refImage,float errorThreshold)2447 bool ShaderRenderCaseInstance::compareImages (const tcu::Surface& resImage, const tcu::Surface& refImage, float errorThreshold)
2448 {
2449 if (m_fuzzyCompare)
2450 return tcu::fuzzyCompare(m_context.getTestContext().getLog(), "ComparisonResult", "Image comparison result", refImage, resImage, errorThreshold, tcu::COMPARE_LOG_EVERYTHING);
2451 else
2452 return tcu::pixelThresholdCompare(m_context.getTestContext().getLog(), "ComparisonResult", "Image comparison result", refImage, resImage, tcu::RGBA(1, 1, 1, 1), tcu::COMPARE_LOG_EVERYTHING);
2453 }
2454
2455 } // sr
2456 } // vkt
2457