Add rendering verification to dEQP-VK.api.smoke.*triangle
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / api / vktApiSmokeTests.cpp
index a694381..27c1bfb 100644 (file)
@@ -4,28 +4,17 @@
  *
  * Copyright (c) 2015 Google Inc.
  *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and/or associated documentation files (the
- * "Materials"), to deal in the Materials without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Materials, and to
- * permit persons to whom the Materials are furnished to do so, subject to
- * the following conditions:
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
- * The above copyright notice(s) and this permission notice shall be
- * included in all copies or substantial portions of the Materials.
+ *      http://www.apache.org/licenses/LICENSE-2.0
  *
- * The Materials are Confidential Information as defined by the
- * Khronos Membership Agreement until designated non-confidential by
- * Khronos, at which point this condition clause shall be removed.
- *
- * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  *
  *//*!
  * \file
 #include "vkDeviceUtil.hpp"
 #include "vkPrograms.hpp"
 #include "vkTypeUtil.hpp"
+#include "vkImageUtil.hpp"
 
 #include "tcuTestLog.hpp"
 #include "tcuFormatUtil.hpp"
+#include "tcuTextureUtil.hpp"
+#include "tcuImageCompare.hpp"
+
+#include "rrRenderer.hpp"
 
 #include "deUniquePtr.hpp"
 
@@ -107,7 +101,7 @@ tcu::TestStatus createSamplerTest (Context& context)
 void createShaderProgs (SourceCollections& dst)
 {
        dst.glslSources.add("test") << glu::VertexSource(
-               "#version 300 es\n"
+               "#version 310 es\n"
                "in highp vec4 a_position;\n"
                "void main (void) { gl_Position = a_position; }\n");
 }
@@ -190,15 +184,78 @@ void createTriangleAsmProgs (SourceCollections& dst)
 void createTriangleProgs (SourceCollections& dst)
 {
        dst.glslSources.add("vert") << glu::VertexSource(
-               "#version 300 es\n"
+               "#version 310 es\n"
                "layout(location = 0) in highp vec4 a_position;\n"
                "void main (void) { gl_Position = a_position; }\n");
        dst.glslSources.add("frag") << glu::FragmentSource(
-               "#version 300 es\n"
+               "#version 310 es\n"
                "layout(location = 0) out lowp vec4 o_color;\n"
                "void main (void) { o_color = vec4(1.0, 0.0, 1.0, 1.0); }\n");
 }
 
+class RefVertexShader : public rr::VertexShader
+{
+public:
+       RefVertexShader (void)
+               : rr::VertexShader(1, 0)
+       {
+               m_inputs[0].type = rr::GENERICVECTYPE_FLOAT;
+       }
+
+       void shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
+       {
+               for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
+               {
+                       packets[packetNdx]->position = rr::readVertexAttribFloat(inputs[0],
+                                                                                                                                        packets[packetNdx]->instanceNdx,
+                                                                                                                                        packets[packetNdx]->vertexNdx);
+               }
+       }
+};
+
+class RefFragmentShader : public rr::FragmentShader
+{
+public:
+       RefFragmentShader (void)
+               : rr::FragmentShader(0, 1)
+       {
+               m_outputs[0].type = rr::GENERICVECTYPE_FLOAT;
+       }
+
+       void shadeFragments (rr::FragmentPacket*, const int numPackets, const rr::FragmentShadingContext& context) const
+       {
+               for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
+               {
+                       for (int fragNdx = 0; fragNdx < rr::NUM_FRAGMENTS_PER_PACKET; ++fragNdx)
+                       {
+                               rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, tcu::Vec4(1.0f, 0.0f, 1.0f, 1.0f));
+                       }
+               }
+       }
+};
+
+void renderReferenceTriangle (const tcu::PixelBufferAccess& dst, const tcu::Vec4 (&vertices)[3])
+{
+       const RefVertexShader                                   vertShader;
+       const RefFragmentShader                                 fragShader;
+       const rr::Program                                               program                 (&vertShader, &fragShader);
+       const rr::MultisamplePixelBufferAccess  colorBuffer             = rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(dst);
+       const rr::RenderTarget                                  renderTarget    (colorBuffer);
+       const rr::RenderState                                   renderState             ((rr::ViewportState(colorBuffer)));
+       const rr::Renderer                                              renderer;
+       const rr::VertexAttrib                                  vertexAttribs[] =
+       {
+               rr::VertexAttrib(rr::VERTEXATTRIBTYPE_FLOAT, 4, sizeof(tcu::Vec4), 0, vertices[0].getPtr())
+       };
+
+       renderer.draw(rr::DrawCommand(renderState,
+                                                                 renderTarget,
+                                                                 program,
+                                                                 DE_LENGTH_OF_ARRAY(vertexAttribs),
+                                                                 &vertexAttribs[0],
+                                                                 rr::PrimitiveList(rr::PRIMITIVETYPE_TRIANGLES, DE_LENGTH_OF_ARRAY(vertices), 0)));
+}
+
 tcu::TestStatus renderTriangleTest (Context& context)
 {
        const VkDevice                                                  vkDevice                                = context.getDevice();
@@ -207,6 +264,8 @@ tcu::TestStatus renderTriangleTest (Context& context)
        const deUint32                                                  queueFamilyIndex                = context.getUniversalQueueFamilyIndex();
        SimpleAllocator                                                 memAlloc                                (vk, vkDevice, getPhysicalDeviceMemoryProperties(context.getInstanceInterface(), context.getPhysicalDevice()));
        const tcu::IVec2                                                renderSize                              (256, 256);
+       const VkFormat                                                  colorFormat                             = VK_FORMAT_R8G8B8A8_UNORM;
+       const tcu::Vec4                                                 clearColor                              (0.125f, 0.25f, 0.75f, 1.0f);
 
        const tcu::Vec4                                                 vertices[]                              =
        {
@@ -532,14 +591,6 @@ tcu::TestStatus renderTriangleTest (Context& context)
                &attBlendParams,                                                                                        // pAttachments
                { 0.0f, 0.0f, 0.0f, 0.0f },                                                                     // blendConstants[4]
        };
-       const VkPipelineDynamicStateCreateInfo  dynamicStateInfo                =
-       {
-               VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,   // sType
-               DE_NULL,                                                                                                // pNext
-               0u,                                                                                                             // flags
-               0u,                                                                                                             // dynamicStateCount
-               DE_NULL                                                                                                 // pDynamicStates
-       };
        const VkGraphicsPipelineCreateInfo              pipelineParams                  =
        {
                VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,                // sType
@@ -555,7 +606,7 @@ tcu::TestStatus renderTriangleTest (Context& context)
                &multisampleParams,                                                                             // pMultisampleState
                &depthStencilParams,                                                                    // pDepthStencilState
                &blendParams,                                                                                   // pColorBlendState
-               &dynamicStateInfo,                                                                              // pDynamicState
+               (const VkPipelineDynamicStateCreateInfo*)DE_NULL,               // pDynamicState
                *pipelineLayout,                                                                                // layout
                *renderPass,                                                                                    // renderPass
                0u,                                                                                                             // subpass
@@ -643,7 +694,10 @@ tcu::TestStatus renderTriangleTest (Context& context)
        }
 
        {
-               const VkClearValue                      clearValue              = makeClearValueColorF32(0.125f, 0.25f, 0.75f, 1.0f);
+               const VkClearValue                      clearValue              = makeClearValueColorF32(clearColor[0],
+                                                                                                                                                        clearColor[1],
+                                                                                                                                                        clearColor[2],
+                                                                                                                                                        clearColor[3]);
                const VkRenderPassBeginInfo     passBeginParams =
                {
                        VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,                       // sType
@@ -773,9 +827,10 @@ tcu::TestStatus renderTriangleTest (Context& context)
                VK_CHECK(vk.waitForFences(vkDevice, 1u, &fence.get(), DE_TRUE, ~0ull));
        }
 
-       // Log image
+       // Read results, render reference, compare
        {
-               const VkMappedMemoryRange       range           =
+               const tcu::TextureFormat                        tcuFormat               = vk::mapVkFormat(colorFormat);
+               const VkMappedMemoryRange                       range                   =
                {
                        VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,  // sType
                        DE_NULL,                                                                // pNext
@@ -783,10 +838,31 @@ tcu::TestStatus renderTriangleTest (Context& context)
                        0,                                                                              // offset
                        imageSizeBytes,                                                 // size
                };
-               void*                                           imagePtr        = readImageBufferMemory->getHostPtr();
+               const tcu::ConstPixelBufferAccess       resultAccess    (tcuFormat, renderSize.x(), renderSize.y(), 1, readImageBufferMemory->getHostPtr());
 
                VK_CHECK(vk.invalidateMappedMemoryRanges(vkDevice, 1u, &range));
-               context.getTestContext().getLog() << TestLog::Image("Result", "Result", tcu::ConstPixelBufferAccess(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), renderSize.x(), renderSize.y(), 1, imagePtr));
+
+               {
+                       tcu::TextureLevel       refImage                (tcuFormat, renderSize.x(), renderSize.y());
+                       const tcu::UVec4        threshold               (0u);
+                       const tcu::IVec3        posDeviation    (1,1,0);
+
+                       tcu::clear(refImage.getAccess(), clearColor);
+                       renderReferenceTriangle(refImage.getAccess(), vertices);
+
+                       if (tcu::intThresholdPositionDeviationCompare(context.getTestContext().getLog(),
+                                                                                                                 "ComparisonResult",
+                                                                                                                 "Image comparison result",
+                                                                                                                 refImage.getAccess(),
+                                                                                                                 resultAccess,
+                                                                                                                 threshold,
+                                                                                                                 posDeviation,
+                                                                                                                 false,
+                                                                                                                 tcu::COMPARE_LOG_RESULT))
+                               return tcu::TestStatus::pass("Rendering succeeded");
+                       else
+                               return tcu::TestStatus::fail("Image comparison failed");
+               }
        }
 
        return tcu::TestStatus::pass("Rendering succeeded");