From da90931967434116522be552f82c8bc164a835bf Mon Sep 17 00:00:00 2001 From: Peter Gal Date: Fri, 28 Aug 2015 20:51:00 +0200 Subject: [PATCH] ShaderRenderCase: Reworked the way to add uniforms to a test instance --- .../shaderrendercase/vktShaderRenderCase.cpp | 203 ++++++++++----------- .../shaderrendercase/vktShaderRenderCase.hpp | 40 +++- .../shaderrendercase/vktShaderRenderCaseTests.cpp | 36 +++- 3 files changed, 164 insertions(+), 115 deletions(-) diff --git a/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCase.cpp b/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCase.cpp index f6f4837..3cb9736 100644 --- a/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCase.cpp +++ b/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCase.cpp @@ -47,7 +47,6 @@ #include "vkQueryUtil.hpp" #include "vkDeviceUtil.hpp" - #include #include @@ -294,12 +293,14 @@ void ShaderEvaluator::evaluate (ShaderEvalContext& ctx) // ShaderRenderCaseInstance. -ShaderRenderCaseInstance::ShaderRenderCaseInstance (Context& context, const string& name, bool isVertexCase, ShaderEvaluator& evaluator) +ShaderRenderCaseInstance::ShaderRenderCaseInstance (Context& context, const string& name, bool isVertexCase, ShaderEvaluator& evaluator, UniformSetupFunc uniformFunc) : vkt::TestInstance(context) , m_clearColor(DEFAULT_CLEAR_COLOR) + , memAlloc(m_context.getDeviceInterface(), m_context.getDevice(), getPhysicalDeviceMemoryProperties(m_context.getInstanceInterface(), m_context.getPhysicalDevice())) , m_name(name) , m_isVertexCase(isVertexCase) , m_evaluator(evaluator) + , m_uniformFunc(uniformFunc) , m_renderSize(100, 100) , m_colorFormat(VK_FORMAT_R8G8B8A8_UNORM) { @@ -338,6 +339,94 @@ tcu::TestStatus ShaderRenderCaseInstance::iterate (void) return tcu::TestStatus::fail("Image mismatch"); } +void ShaderRenderCaseInstance::setupUniformData (deUint32 size, void* dataPtr) +{ + const VkDevice vkDevice = m_context.getDevice(); + const DeviceInterface& vk = m_context.getDeviceInterface(); + const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex(); + + const VkBufferCreateInfo uniformBufferParams = + { + VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType; + DE_NULL, // const void* pNext; + size, // VkDeviceSize size; + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, // VkBufferUsageFlags usage; + 0u, // VkBufferCreateFlags flags; + VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode; + 1u, // deUint32 queueFamilyCount; + &queueFamilyIndex // const deUint32* pQueueFamilyIndices; + }; + + Move buffer = createBuffer(vk, vkDevice, &uniformBufferParams); + de::MovePtr alloc = memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *buffer), MemoryRequirement::HostVisible); + VK_CHECK(vk.bindBufferMemory(vkDevice, *buffer, alloc->getMemory(), 0)); + + void* bufferPtr; + VK_CHECK(vk.mapMemory(vkDevice, alloc->getMemory(), 0, size, 0, &bufferPtr)); + deMemcpy(bufferPtr, dataPtr, size); + VK_CHECK(vk.unmapMemory(vkDevice, alloc->getMemory())); + + const VkBufferViewCreateInfo viewInfo = + { + VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO, // VkStructureType sType; + DE_NULL, // void* pNext; + *buffer, // VkBuffer buffer; + VK_BUFFER_VIEW_TYPE_FORMATTED, // VkBufferViewType viewType; + VK_FORMAT_R32_SFLOAT, // VkFormat format; + 0u, // VkDeviceSize offset; + size // VkDeviceSize range; + }; + + m_uniformBufferViews.push_back(createBufferView(vk, vkDevice, &viewInfo).disown()); + + m_uniformBuffers.push_back(buffer.disown()); + m_uniformBufferAllocs.push_back(alloc.release()); +} + + +void ShaderRenderCaseInstance::addUniform (deUint32 bindingLocation, VkDescriptorType descriptorType, float data) +{ + m_descriptorSetLayoutBuilder.addSingleBinding(descriptorType, VK_SHADER_STAGE_VERTEX_BIT); + m_descriptorPoolBuilder.addType(descriptorType); + + setupUniformData(sizeof(data), &data); + + const VkDescriptorInfo view = + { + m_uniformBufferViews[m_uniformBufferViews.size() - 1], // VkBufferView bufferView; + 0, // VkSampler sampler; + 0, // VkImageView imageView; + 0, // VkAttachmentView attachmentView; + (VkImageLayout)0, // VkImageLayout imageLayout; + }; + + m_uniformDescriptorInfos.push_back(view); + + m_uniformLocations.push_back(bindingLocation); +} + +void ShaderRenderCaseInstance::addUniform (deUint32 bindingLocation, VkDescriptorType descriptorType, tcu::Vec4 data) +{ + m_descriptorSetLayoutBuilder.addSingleBinding(descriptorType, VK_SHADER_STAGE_VERTEX_BIT); + m_descriptorPoolBuilder.addType(descriptorType); + + setupUniformData(sizeof(data), &data[0]); + + const VkDescriptorInfo view = + { + m_uniformBufferViews[m_uniformBufferViews.size() - 1], // VkBufferView bufferView; + 0, // VkSampler sampler; + 0, // VkImageView imageView; + 0, // VkAttachmentView attachmentView; + (VkImageLayout)0, // VkImageLayout imageLayout; + }; + + m_uniformDescriptorInfos.push_back(view); + + m_uniformLocations.push_back(bindingLocation); +} + + void ShaderRenderCaseInstance::setupShaderData (void) { // TODO!!! @@ -352,6 +441,7 @@ void ShaderRenderCaseInstance::setupUniforms (const Vec4& constCoords) { // TODO!! DE_UNREF(constCoords); + m_uniformFunc(*this); } tcu::IVec2 ShaderRenderCaseInstance::getViewportSize (void) const @@ -373,7 +463,7 @@ void ShaderRenderCaseInstance::render (Surface& result, const QuadGrid& quadGrid const DeviceInterface& vk = m_context.getDeviceInterface(); const VkQueue queue = m_context.getUniversalQueue(); const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex(); - SimpleAllocator memAlloc (vk, vkDevice, getPhysicalDeviceMemoryProperties(m_context.getInstanceInterface(), m_context.getPhysicalDevice())); + //SimpleAllocator memAlloc (vk, vkDevice, getPhysicalDeviceMemoryProperties(m_context.getInstanceInterface(), m_context.getPhysicalDevice())); // Create color image { @@ -501,111 +591,18 @@ void ShaderRenderCaseInstance::render (Surface& result, const QuadGrid& quadGrid // Create descriptors { - const VkDescriptorSetLayoutBinding layoutBindings[1] = - { - { - VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // VkDescriptorType descriptorType; - 1u, // deUint32 arraySize; - VK_SHADER_STAGE_VERTEX_BIT, // VkShaderStageFlags stageFlags; - DE_NULL // const VkSampler* pImmutableSamplers; - }, - }; - - const VkDescriptorSetLayoutCreateInfo descriptorLayoutParams = - { - VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, // VkStructureType sType; - DE_NULL, // cost void* pNexŧ; - DE_LENGTH_OF_ARRAY(layoutBindings), // deUint32 count; - layoutBindings // const VkDescriptorSetLayoutBinding pBinding; - }; - - m_descriptorSetLayout = createDescriptorSetLayout(vk, vkDevice, &descriptorLayoutParams); - - - const float uniformData[] = { 1.0f }; - const VkDeviceSize uniformSize = DE_LENGTH_OF_ARRAY(uniformData) * sizeof(float); - const VkBufferCreateInfo uniformBufferParams = - { - VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, // VkStructureType sType; - DE_NULL, // const void* pNext; - uniformSize, // VkDeviceSize size; - VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, // VkBufferUsageFlags usage; - 0u, // VkBufferCreateFlags flags; - VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode; - 1u, // deUint32 queueFamilyCount; - &queueFamilyIndex // const deUint32* pQueueFamilyIndices; - }; - - m_uniformBuffer = createBuffer(vk, vkDevice, &uniformBufferParams); - m_uniformBufferAlloc = memAlloc.allocate(getBufferMemoryRequirements(vk, vkDevice, *m_uniformBuffer), MemoryRequirement::HostVisible); - - VK_CHECK(vk.bindBufferMemory(vkDevice, *m_uniformBuffer, m_uniformBufferAlloc->getMemory(), 0)); - - void* bufferPtr; - VK_CHECK(vk.mapMemory(vkDevice, m_uniformBufferAlloc->getMemory(), 0, uniformSize, 0, &bufferPtr)); - deMemcpy(bufferPtr, uniformData, uniformSize); - VK_CHECK(vk.unmapMemory(vkDevice, m_uniformBufferAlloc->getMemory())); - - const VkBufferViewCreateInfo viewInfo = - { - VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO, // VkStructureType sType; - DE_NULL, // void* pNext; - *m_uniformBuffer, // VkBuffer buffer; - VK_BUFFER_VIEW_TYPE_FORMATTED, // VkBufferViewType viewType; - VK_FORMAT_R32_SFLOAT, // VkFormat format; - 0u, // VkDeviceSize offset; - uniformSize // VkDeviceSize range; - }; - - m_uniformBufferView = createBufferView(vk, vkDevice, &viewInfo); - - const VkDescriptorTypeCount descriptorTypes[] = - { - { - VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // VkDescriptorType type; - 1 // deUint32 count; - } - }; - - const VkDescriptorPoolCreateInfo descriptorPoolParams = - { - VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, // VkStructureType sType; - DE_NULL, // void* pNext; - DE_LENGTH_OF_ARRAY(descriptorTypes), // deUint32 count; - descriptorTypes // const VkDescriptorTypeCount* pTypeCount - }; - - m_descriptorPool = createDescriptorPool(vk, vkDevice, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, &descriptorPoolParams); + setupUniforms(quadGrid.getConstCoords()); + m_descriptorSetLayout = m_descriptorSetLayoutBuilder.build(vk, vkDevice); + m_descriptorPool = m_descriptorPoolBuilder.build(vk, vkDevice, VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1u); m_descriptorSet = allocDescriptorSet(vk, vkDevice, *m_descriptorPool, VK_DESCRIPTOR_SET_USAGE_STATIC, *m_descriptorSetLayout); - const VkDescriptorInfo descriptorInfos[] = - { - { - *m_uniformBufferView, // VkBufferView bufferView; - 0, // VkSampler sampler; - 0, // VkImageView imageView; - 0, // VkAttachmentView attachmentView; - (VkImageLayout)0, // VkImageLayout imageLayout; - - } - }; - - const VkWriteDescriptorSet writeDescritporSets[] = + for(deUint32 i = 0; i < m_uniformDescriptorInfos.size(); i++) { - { - VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, // VkStructureType sType; - DE_NULL, // const void* pNext; - *m_descriptorSet, // VkDescriptorSet destSet; - 0, // deUint32 destBinding; - 0, // deUint32 destArrayElement; - 1, // deUint32 count; - VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // VkDescriptorType descriptorType; - descriptorInfos, // const VkDescriptorInfo* pDescriptors; - } - }; + m_descriptorSetUpdateBuilder.writeSingle(*m_descriptorSet, DescriptorSetUpdateBuilder::Location::binding(m_uniformLocations[i]), VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &m_uniformDescriptorInfos[i]); + } - VK_CHECK(vk.updateDescriptorSets(vkDevice, 1, writeDescritporSets, 0u, DE_NULL)); + m_descriptorSetUpdateBuilder.update(vk, vkDevice); } // Create pipeline layout diff --git a/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCase.hpp b/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCase.hpp index 5be14db..e63df11 100644 --- a/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCase.hpp +++ b/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCase.hpp @@ -44,6 +44,7 @@ #include "vkPrograms.hpp" #include "vkRef.hpp" #include "vkMemUtil.hpp" +#include "vkBuilderUtil.hpp" namespace vkt { @@ -138,6 +139,9 @@ private: ShaderEvalFunc m_evalFunc; }; +class ShaderRenderCaseInstance; + +typedef void (*UniformSetupFunc) (ShaderRenderCaseInstance& instance); template class ShaderRenderCase : public vkt::TestCase @@ -147,20 +151,24 @@ public: const std::string& name, const std::string& description, bool isVertexCase, - ShaderEvalFunc evalFunc) + ShaderEvalFunc evalFunc, + UniformSetupFunc uniformFunc) : vkt::TestCase(testCtx, name, description) , m_isVertexCase(isVertexCase) , m_evaluator(new ShaderEvaluator(evalFunc)) + , m_uniformFunc(uniformFunc) {} ShaderRenderCase (tcu::TestContext& testCtx, const std::string& name, const std::string& description, bool isVertexCase, - ShaderEvaluator* evaluator) + ShaderEvaluator* evaluator, + UniformSetupFunc uniformFunc) : vkt::TestCase(testCtx, name, description) , m_isVertexCase(isVertexCase) , m_evaluator(evaluator) + , m_uniformFunc(uniformFunc) {} @@ -171,7 +179,7 @@ public: programCollection.add(m_name + "_frag") << glu::FragmentSource(m_fragShaderSource); } - virtual TestInstance* createInstance (Context& context) const { return new Instance(context, m_name, m_isVertexCase, *m_evaluator); } + virtual TestInstance* createInstance (Context& context) const { return new Instance(context, m_name, m_isVertexCase, *m_evaluator, m_uniformFunc); } protected: std::string m_vertShaderSource; @@ -180,18 +188,23 @@ protected: private: bool m_isVertexCase; ShaderEvaluator* m_evaluator; - + UniformSetupFunc m_uniformFunc; }; + + // ShaderRenderCaseInstance. class ShaderRenderCaseInstance : public vkt::TestInstance { public: - ShaderRenderCaseInstance (Context& context, const std::string& name, bool isVertexCase, ShaderEvaluator& evaluator); + ShaderRenderCaseInstance (Context& context, const std::string& name, bool isVertexCase, ShaderEvaluator& evaluator, UniformSetupFunc uniformFunc); virtual ~ShaderRenderCaseInstance (void); virtual tcu::TestStatus iterate (void); + void addUniform (deUint32 bindingLocation, vk::VkDescriptorType descriptorType, float data); + void addUniform (deUint32 bindingLocation, vk::VkDescriptorType descriptorType, tcu::Vec4 data); + protected: virtual void setupShaderData (void); virtual void setup (void); @@ -202,8 +215,11 @@ protected: std::vector m_userAttribTransforms; tcu::Vec4 m_clearColor; + vk::SimpleAllocator memAlloc; + private: + void setupUniformData (deUint32 size, void* dataPtr); void setupDefaultInputs (void); void render (tcu::Surface& result, const QuadGrid& quadGrid); @@ -214,6 +230,7 @@ private: std::string m_name; bool m_isVertexCase; ShaderEvaluator& m_evaluator; + UniformSetupFunc m_uniformFunc; const tcu::IVec2 m_renderSize; const vk::VkFormat m_colorFormat; @@ -243,9 +260,6 @@ private: de::MovePtr m_indiceBufferAlloc; vk::Move m_descriptorSetLayout; - vk::Move m_uniformBuffer; - de::MovePtr m_uniformBufferAlloc; - vk::Move m_uniformBufferView; vk::Move m_descriptorPool; vk::Move m_descriptorSet; @@ -254,6 +268,16 @@ private: vk::Move m_cmdBuffer; vk::Move m_fence; + + vk::DescriptorSetLayoutBuilder m_descriptorSetLayoutBuilder; + vk::DescriptorPoolBuilder m_descriptorPoolBuilder; + vk::DescriptorSetUpdateBuilder m_descriptorSetUpdateBuilder; + + std::vector m_uniformLocations; + std::vector m_uniformBuffers; + std::vector m_uniformBufferAllocs; + std::vector m_uniformBufferViews; + std::vector m_uniformDescriptorInfos; }; diff --git a/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCaseTests.cpp b/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCaseTests.cpp index 976b3a2..323d3dc 100644 --- a/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCaseTests.cpp +++ b/external/vulkancts/modules/vulkan/shaderrendercase/vktShaderRenderCaseTests.cpp @@ -12,6 +12,18 @@ namespace shaderrendercase inline void eval_DEBUG (ShaderEvalContext& c) { c.color = tcu::Vec4(1, 0, 1, 1); } +void empty_uniform (ShaderRenderCaseInstance& instance) {} + + +void dummy_uniforms (ShaderRenderCaseInstance& instance) +{ + instance.addUniform(0u, vk::VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1.0f); + instance.addUniform(1u, vk::VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0.5f); + instance.addUniform(2u, vk::VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, tcu::Vec4(1, 0.5f, 1.0f, 0.5f)); +} + +class DummyShaderRenderCaseInstance; + class DummyTestRenderCase : public ShaderRenderCase { public: @@ -22,7 +34,7 @@ public: ShaderEvalFunc evalFunc, std::string vertexShader, std::string fragmentShader) - : ShaderRenderCase(testCtx, name, description, isVertexCase, evalFunc) + : ShaderRenderCase(testCtx, name, description, isVertexCase, evalFunc, dummy_uniforms) { m_vertShaderSource = vertexShader; m_fragShaderSource = fragmentShader; @@ -46,18 +58,34 @@ tcu::TestCaseGroup* createTests (tcu::TestContext& testCtx) " float item;\n" "};\n" + "layout (set=0, binding=1) uniform buf2 {\n" + " float item2;\n" + "};\n" + + "layout (set=0, binding=2) uniform buf3 {\n" + " vec4 item3;\n" + "};\n" + "out mediump vec4 v_color;\n" - "void main (void) { gl_Position = a_position; v_color = vec4(a_coords.xyz, item); }\n"; + "void main (void) { gl_Position = a_position; v_color = vec4(a_coords.xyz, item3.x); }\n"; std::string base_fragment = "#version 300 es\n" "layout(location = 0) out lowp vec4 o_color;\n" "in mediump vec4 v_color;\n" "void main (void) { o_color = v_color; }\n"; - std::string debug_fragment = "#version 300 es\n" + std::string debug_fragment = "#version 140 \n" + "#extension GL_ARB_separate_shader_objects : enable\n" + "#extension GL_ARB_shading_language_420pack : enable\n" + "layout(location = 0) out lowp vec4 o_color;\n" + + "layout (set=0, binding=2) uniform buf {\n" + " float item[4];\n" + "};\n" + "in mediump vec4 v_color;\n" - "void main (void) { o_color = vec4(1,0,1,1); }\n"; + "void main (void) { o_color = vec4(1,0,item[0],1); }\n"; shaderRenderCaseTests->addChild(new DummyTestRenderCase(testCtx, "testVertex", "testVertex", true, evalCoordsPassthrough, base_vertex, base_fragment)); -- 2.7.4