dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / pipeline / vktPipelineReferenceRenderer.hpp
1 #ifndef _VKTPIPELINEREFERENCERENDERER_HPP
2 #define _VKTPIPELINEREFERENCERENDERER_HPP
3 /*------------------------------------------------------------------------
4  * Vulkan Conformance Tests
5  * ------------------------
6  *
7  * Copyright (c) 2015 The Khronos Group Inc.
8  * Copyright (c) 2015 Imagination Technologies Ltd.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and/or associated documentation files (the
12  * "Materials"), to deal in the Materials without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Materials, and to
15  * permit persons to whom the Materials are furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice(s) and this permission notice shall be included
19  * in all copies or substantial portions of the Materials.
20  *
21  * The Materials are Confidential Information as defined by the
22  * Khronos Membership Agreement until designated non-confidential by Khronos,
23  * at which point this condition clause shall be removed.
24  *
25  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
29  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
30  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
32  *
33  *//*!
34  * \file
35  * \brief Reference renderer.
36  *//*--------------------------------------------------------------------*/
37
38 #include "vkDefs.hpp"
39 #include "vktPipelineVertexUtil.hpp"
40 #include "tcuVector.hpp"
41 #include "tcuVectorType.hpp"
42 #include "tcuTexture.hpp"
43 #include "tcuTextureUtil.hpp"
44 #include "rrRenderState.hpp"
45 #include "rrRenderer.hpp"
46 #include <cstring>
47
48 namespace vkt
49 {
50
51 namespace pipeline
52 {
53
54 tcu::Vec4       swizzle         (const tcu::Vec4& color, const tcu::UVec4& swizzle);
55
56 class ColorVertexShader : public rr::VertexShader
57 {
58 public:
59         ColorVertexShader (void) : rr::VertexShader(2, 2)
60         {
61                 m_inputs[0].type        = rr::GENERICVECTYPE_FLOAT;
62                 m_inputs[1].type        = rr::GENERICVECTYPE_FLOAT;
63
64                 m_outputs[0].type       = rr::GENERICVECTYPE_FLOAT;
65                 m_outputs[1].type       = rr::GENERICVECTYPE_FLOAT;
66         }
67
68         virtual ~ColorVertexShader (void) {}
69
70         virtual void shadeVertices (const rr::VertexAttrib*             inputs,
71                                                                 rr::VertexPacket* const*        packets,
72                                                                 const int                                       numPackets) const
73         {
74                 tcu::Vec4 position;
75                 tcu::Vec4 color;
76
77                 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
78                 {
79                         rr::VertexPacket* const packet  = packets[packetNdx];
80
81                         readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx);
82                         readVertexAttrib(color, inputs[1], packet->instanceNdx, packet->vertexNdx);
83
84                         packet->outputs[0]      = position;
85                         packet->outputs[1]      = color;
86                         packet->position        = position;
87                 }
88         }
89 };
90
91 class TexCoordVertexShader : public rr::VertexShader
92 {
93 public:
94         TexCoordVertexShader (void) : rr::VertexShader(2, 2)
95         {
96                 m_inputs[0].type        = rr::GENERICVECTYPE_FLOAT;
97                 m_inputs[1].type        = rr::GENERICVECTYPE_FLOAT;
98
99                 m_outputs[0].type       = rr::GENERICVECTYPE_FLOAT;
100                 m_outputs[1].type       = rr::GENERICVECTYPE_FLOAT;
101         }
102
103         virtual ~TexCoordVertexShader (void) {}
104
105         virtual void shadeVertices (const rr::VertexAttrib*             inputs,
106                                                                 rr::VertexPacket* const*        packets,
107                                                                 const int                                       numPackets) const
108         {
109                 tcu::Vec4 position;
110                 tcu::Vec4 texCoord;
111
112                 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
113                 {
114                         rr::VertexPacket* const packet  = packets[packetNdx];
115
116                         readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx);
117                         readVertexAttrib(texCoord, inputs[1], packet->instanceNdx, packet->vertexNdx);
118
119                         packet->outputs[0]      = position;
120                         packet->outputs[1]      = texCoord;
121                         packet->position        = position;
122                 }
123         }
124 };
125
126 class ColorFragmentShader : public rr::FragmentShader
127 {
128 private:
129         const tcu::TextureFormat                m_colorFormat;
130         const tcu::TextureFormat                m_depthStencilFormat;
131
132 public:
133         ColorFragmentShader (const tcu::TextureFormat& colorFormat,
134                                                  const tcu::TextureFormat& depthStencilFormat)
135                 : rr::FragmentShader    (2, 1)
136                 , m_colorFormat                 (colorFormat)
137                 , m_depthStencilFormat  (depthStencilFormat)
138         {
139                 const tcu::TextureChannelClass channelClass = tcu::getTextureChannelClass(m_colorFormat.type);
140
141                 m_inputs[0].type        = rr::GENERICVECTYPE_FLOAT;
142                 m_inputs[1].type        = rr::GENERICVECTYPE_FLOAT;
143                 m_outputs[0].type       = (channelClass == tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER)? rr::GENERICVECTYPE_INT32 :
144                                                           (channelClass == tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER)? rr::GENERICVECTYPE_UINT32
145                                                           : rr::GENERICVECTYPE_FLOAT;
146         }
147
148         virtual ~ColorFragmentShader (void) {}
149
150         virtual void shadeFragments (rr::FragmentPacket*                                packets,
151                                                                  const int                                                      numPackets,
152                                                                  const rr::FragmentShadingContext&      context) const
153         {
154                 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
155                 {
156                         const rr::FragmentPacket& packet = packets[packetNdx];
157
158                         if (m_depthStencilFormat.order == tcu::TextureFormat::D || m_depthStencilFormat.order == tcu::TextureFormat::DS)
159                         {
160                                 for (int fragNdx = 0; fragNdx < 4; fragNdx++)
161                                 {
162                                         const tcu::Vec4 vtxPosition = rr::readVarying<float>(packet, context, 0, fragNdx);
163                                         rr::writeFragmentDepth(context, packetNdx, fragNdx, 0, vtxPosition.z());
164                                 }
165                         }
166
167                         for (int fragNdx = 0; fragNdx < 4; fragNdx++)
168                         {
169                                 const tcu::Vec4 vtxColor = rr::readVarying<float>(packet, context, 1, fragNdx);
170                                 rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, vtxColor);
171                         }
172                 }
173         }
174 };
175
176 template<typename TextureType>
177 class SamplerFragmentShader : public rr::FragmentShader
178 {
179 private:
180         const tcu::TextureFormat                m_colorFormat;
181         const tcu::TextureFormatInfo    m_colorFormatInfo;
182         const TextureType                               m_texture;
183         const tcu::TextureFormatInfo    m_textureFormatInfo;
184         const tcu::Sampler                              m_sampler;
185         const float                                             m_lod;
186         const tcu::UVec4                                m_swizzle;
187
188 public:
189         SamplerFragmentShader (const tcu::TextureFormat& colorFormat, const TextureType& texture, const tcu::Sampler& sampler, float lod, const tcu::UVec4& swizzle)
190                 : rr::FragmentShader    (2, 1)
191                 , m_colorFormat                 (colorFormat)
192                 , m_colorFormatInfo             (tcu::getTextureFormatInfo(m_colorFormat))
193                 , m_texture                             (texture)
194                 , m_textureFormatInfo   (tcu::getTextureFormatInfo(m_texture.getFormat()))
195                 , m_sampler                             (sampler)
196                 , m_lod                                 (lod)
197                 , m_swizzle                             (swizzle)
198         {
199                 const tcu::TextureChannelClass channelClass = tcu::getTextureChannelClass(m_colorFormat.type);
200                 m_inputs[0].type        = rr::GENERICVECTYPE_FLOAT;
201                 m_inputs[1].type        = rr::GENERICVECTYPE_FLOAT;
202                 m_outputs[0].type       = (channelClass == tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER)? rr::GENERICVECTYPE_INT32 :
203                                                           (channelClass == tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER)? rr::GENERICVECTYPE_UINT32
204                                                           : rr::GENERICVECTYPE_FLOAT;
205         }
206
207         virtual ~SamplerFragmentShader (void)
208         {
209         }
210
211         static tcu::Vec4 sampleTexture (const tcu::Texture1D& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
212         {
213                 return texture.sample(sampler, texCoord.x(), lod);
214         }
215
216         static tcu::Vec4 sampleTexture (const tcu::Texture1DArray& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
217         {
218                 return texture.sample(sampler, texCoord.x(), texCoord.y(), lod);
219         }
220
221         static tcu::Vec4 sampleTexture (const tcu::Texture2D& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
222         {
223                 return texture.sample(sampler, texCoord.x(), texCoord.y(), lod);
224         }
225
226         static tcu::Vec4 sampleTexture (const tcu::Texture2DArray& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
227         {
228                 return texture.sample(sampler, texCoord.x(), texCoord.y(), texCoord.z(), lod);
229         }
230
231         static tcu::Vec4 sampleTexture (const tcu::Texture3D& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
232         {
233                 return texture.sample(sampler, texCoord.x(), texCoord.y(), texCoord.z(), lod);
234         }
235
236         static tcu::Vec4 sampleTexture (const tcu::TextureCube& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
237         {
238                 return texture.sample(sampler, texCoord.x(), texCoord.y(), texCoord.z(), lod);
239         }
240
241         static tcu::Vec4 sampleTexture (const tcu::TextureCubeArray& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
242         {
243                 return texture.sample(sampler, texCoord.x(), texCoord.y(), texCoord.z(), texCoord.w(), lod);
244         }
245
246         virtual void shadeFragments (rr::FragmentPacket*                                packets,
247                                                                  const int                                                      numPackets,
248                                                                  const rr::FragmentShadingContext&      context) const
249         {
250                 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
251                 {
252                         const rr::FragmentPacket& packet = packets[packetNdx];
253
254                         for (int fragNdx = 0; fragNdx < 4; fragNdx++)
255                         {
256                                 const tcu::Vec4 vtxTexCoord     = rr::readVarying<float>(packet, context, 1, fragNdx);
257                                 const tcu::Vec4 texColor        = sampleTexture(m_texture, m_sampler, vtxTexCoord, m_lod);
258                                 const tcu::Vec4 swizColor       = swizzle(texColor, m_swizzle);
259                                 const tcu::Vec4 normColor       = swizColor * swizzle(m_textureFormatInfo.lookupScale, m_swizzle) + swizzle(m_textureFormatInfo.lookupBias, m_swizzle);
260                                 const tcu::Vec4 color           = (normColor - m_colorFormatInfo.lookupBias) / m_colorFormatInfo.lookupScale;
261                                 rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, color);
262                         }
263                 }
264         }
265 };
266
267 class Program
268 {
269 public:
270         virtual ~Program (void) { }
271
272         virtual rr::Program getReferenceProgram (void) const = 0;
273 };
274
275 template<typename TextureType>
276 class SamplerProgram: public Program
277 {
278 private:
279         TexCoordVertexShader                            m_vertexShader;
280         SamplerFragmentShader<TextureType>      m_fragmentShader;
281 public:
282         SamplerProgram (const tcu::TextureFormat& colorFormat, const TextureType& texture, const tcu::Sampler& sampler, float lod, const tcu::UVec4& swizzle)
283                 : m_vertexShader        ()
284                 , m_fragmentShader      (colorFormat, texture, sampler, lod, swizzle)
285         {
286         }
287
288         virtual ~SamplerProgram (void) { }
289
290         virtual rr::Program getReferenceProgram (void) const
291         {
292                 return rr::Program(&m_vertexShader, &m_fragmentShader);
293         }
294 };
295
296 class ReferenceRenderer
297 {
298 public:
299                                                                 ReferenceRenderer               (int                                                    surfaceWidth,
300                                                                                                                  int                                                    surfaceHeight,
301                                                                                                                  int                                                    numSamples,
302                                                                                                                  const tcu::TextureFormat&              colorFormat,
303                                                                                                                  const tcu::TextureFormat&              depthStencilFormat,
304                                                                                                                  const rr::Program* const               program);
305
306         virtual                                         ~ReferenceRenderer              (void);
307
308         void                                            colorClear                              (const tcu::Vec4& color);
309
310         void                                            draw                                    (const rr::RenderState&                         renderState,
311                                                                                                                  const rr::PrimitiveType                        primitive,
312                                                                                                                  const std::vector<Vertex4RGBA>&        vertexBuffer);
313
314         void                                            draw                                    (const rr::RenderState&                         renderState,
315                                                                                                                  const rr::PrimitiveType                        primitive,
316                                                                                                                  const std::vector<Vertex4Tex4>&        vertexBuffer);
317
318         tcu::PixelBufferAccess          getAccess                               (void);
319         const rr::ViewportState         getViewportState                (void) const;
320
321 private:
322         rr::Renderer                            m_renderer;
323
324         const int                                       m_surfaceWidth;
325         const int                                       m_surfaceHeight;
326         const int                                       m_numSamples;
327
328         const tcu::TextureFormat        m_colorFormat;
329         const tcu::TextureFormat        m_depthStencilFormat;
330
331         tcu::TextureLevel                       m_colorBuffer;
332         tcu::TextureLevel                       m_resolveColorBuffer;
333         tcu::TextureLevel                       m_depthStencilBuffer;
334
335         rr::RenderTarget*                       m_renderTarget;
336         const rr::Program*                      m_program;
337 };
338
339 rr::TestFunc                                    mapVkCompareOp                          (vk::VkCompareOp compareFunc);
340 rr::PrimitiveType                               mapVkPrimitiveTopology          (vk::VkPrimitiveTopology primitiveTopology);
341 rr::BlendFunc                                   mapVkBlendFactor                        (vk::VkBlendFactor blendFactor);
342 rr::BlendEquation                               mapVkBlendOp                            (vk::VkBlendOp blendOp);
343 tcu::BVec4                                              mapVkColorComponentFlags        (vk::VkColorComponentFlags flags);
344 rr::StencilOp                                   mapVkStencilOp                          (vk::VkStencilOp stencilOp);
345
346 } // pipeline
347 } // vkt
348
349 #endif // _VKTPIPELINEREFERENCERENDERER_HPP