1 #ifndef _VKTPIPELINEREFERENCERENDERER_HPP
2 #define _VKTPIPELINEREFERENCERENDERER_HPP
3 /*------------------------------------------------------------------------
4 * Vulkan Conformance Tests
5 * ------------------------
7 * Copyright (c) 2015 The Khronos Group Inc.
8 * Copyright (c) 2015 Imagination Technologies Ltd.
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:
18 * The above copyright notice(s) and this permission notice shall be included
19 * in all copies or substantial portions of the Materials.
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.
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.
35 * \brief Reference renderer.
36 *//*--------------------------------------------------------------------*/
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"
54 tcu::Vec4 swizzle (const tcu::Vec4& color, const tcu::UVec4& swizzle);
56 class ColorVertexShader : public rr::VertexShader
59 ColorVertexShader (void) : rr::VertexShader(2, 2)
61 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT;
62 m_inputs[1].type = rr::GENERICVECTYPE_FLOAT;
64 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT;
65 m_outputs[1].type = rr::GENERICVECTYPE_FLOAT;
68 virtual ~ColorVertexShader (void) {}
70 virtual void shadeVertices (const rr::VertexAttrib* inputs,
71 rr::VertexPacket* const* packets,
72 const int numPackets) const
77 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
79 rr::VertexPacket* const packet = packets[packetNdx];
81 readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx);
82 readVertexAttrib(color, inputs[1], packet->instanceNdx, packet->vertexNdx);
84 packet->outputs[0] = position;
85 packet->outputs[1] = color;
86 packet->position = position;
91 class TexCoordVertexShader : public rr::VertexShader
94 TexCoordVertexShader (void) : rr::VertexShader(2, 2)
96 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT;
97 m_inputs[1].type = rr::GENERICVECTYPE_FLOAT;
99 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT;
100 m_outputs[1].type = rr::GENERICVECTYPE_FLOAT;
103 virtual ~TexCoordVertexShader (void) {}
105 virtual void shadeVertices (const rr::VertexAttrib* inputs,
106 rr::VertexPacket* const* packets,
107 const int numPackets) const
112 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
114 rr::VertexPacket* const packet = packets[packetNdx];
116 readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx);
117 readVertexAttrib(texCoord, inputs[1], packet->instanceNdx, packet->vertexNdx);
119 packet->outputs[0] = position;
120 packet->outputs[1] = texCoord;
121 packet->position = position;
126 class ColorFragmentShader : public rr::FragmentShader
129 const tcu::TextureFormat m_colorFormat;
130 const tcu::TextureFormat m_depthStencilFormat;
133 ColorFragmentShader (const tcu::TextureFormat& colorFormat,
134 const tcu::TextureFormat& depthStencilFormat)
135 : rr::FragmentShader (2, 1)
136 , m_colorFormat (colorFormat)
137 , m_depthStencilFormat (depthStencilFormat)
139 const tcu::TextureChannelClass channelClass = tcu::getTextureChannelClass(m_colorFormat.type);
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;
148 virtual ~ColorFragmentShader (void) {}
150 virtual void shadeFragments (rr::FragmentPacket* packets,
151 const int numPackets,
152 const rr::FragmentShadingContext& context) const
154 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
156 const rr::FragmentPacket& packet = packets[packetNdx];
158 if (m_depthStencilFormat.order == tcu::TextureFormat::D || m_depthStencilFormat.order == tcu::TextureFormat::DS)
160 for (int fragNdx = 0; fragNdx < 4; fragNdx++)
162 const tcu::Vec4 vtxPosition = rr::readVarying<float>(packet, context, 0, fragNdx);
163 rr::writeFragmentDepth(context, packetNdx, fragNdx, 0, vtxPosition.z());
167 for (int fragNdx = 0; fragNdx < 4; fragNdx++)
169 const tcu::Vec4 vtxColor = rr::readVarying<float>(packet, context, 1, fragNdx);
170 rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, vtxColor);
176 template<typename TextureType>
177 class SamplerFragmentShader : public rr::FragmentShader
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;
186 const tcu::UVec4 m_swizzle;
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)
197 , m_swizzle (swizzle)
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;
207 virtual ~SamplerFragmentShader (void)
211 static tcu::Vec4 sampleTexture (const tcu::Texture1D& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
213 return texture.sample(sampler, texCoord.x(), lod);
216 static tcu::Vec4 sampleTexture (const tcu::Texture1DArray& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
218 return texture.sample(sampler, texCoord.x(), texCoord.y(), lod);
221 static tcu::Vec4 sampleTexture (const tcu::Texture2D& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
223 return texture.sample(sampler, texCoord.x(), texCoord.y(), lod);
226 static tcu::Vec4 sampleTexture (const tcu::Texture2DArray& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
228 return texture.sample(sampler, texCoord.x(), texCoord.y(), texCoord.z(), lod);
231 static tcu::Vec4 sampleTexture (const tcu::Texture3D& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
233 return texture.sample(sampler, texCoord.x(), texCoord.y(), texCoord.z(), lod);
236 static tcu::Vec4 sampleTexture (const tcu::TextureCube& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
238 return texture.sample(sampler, texCoord.x(), texCoord.y(), texCoord.z(), lod);
241 static tcu::Vec4 sampleTexture (const tcu::TextureCubeArray& texture, const tcu::Sampler& sampler, const tcu::Vec4& texCoord, float lod)
243 return texture.sample(sampler, texCoord.x(), texCoord.y(), texCoord.z(), texCoord.w(), lod);
246 virtual void shadeFragments (rr::FragmentPacket* packets,
247 const int numPackets,
248 const rr::FragmentShadingContext& context) const
250 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
252 const rr::FragmentPacket& packet = packets[packetNdx];
254 for (int fragNdx = 0; fragNdx < 4; fragNdx++)
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);
270 virtual ~Program (void) { }
272 virtual rr::Program getReferenceProgram (void) const = 0;
275 template<typename TextureType>
276 class SamplerProgram: public Program
279 TexCoordVertexShader m_vertexShader;
280 SamplerFragmentShader<TextureType> m_fragmentShader;
282 SamplerProgram (const tcu::TextureFormat& colorFormat, const TextureType& texture, const tcu::Sampler& sampler, float lod, const tcu::UVec4& swizzle)
284 , m_fragmentShader (colorFormat, texture, sampler, lod, swizzle)
288 virtual ~SamplerProgram (void) { }
290 virtual rr::Program getReferenceProgram (void) const
292 return rr::Program(&m_vertexShader, &m_fragmentShader);
296 class ReferenceRenderer
299 ReferenceRenderer (int surfaceWidth,
302 const tcu::TextureFormat& colorFormat,
303 const tcu::TextureFormat& depthStencilFormat,
304 const rr::Program* const program);
306 virtual ~ReferenceRenderer (void);
308 void colorClear (const tcu::Vec4& color);
310 void draw (const rr::RenderState& renderState,
311 const rr::PrimitiveType primitive,
312 const std::vector<Vertex4RGBA>& vertexBuffer);
314 void draw (const rr::RenderState& renderState,
315 const rr::PrimitiveType primitive,
316 const std::vector<Vertex4Tex4>& vertexBuffer);
318 tcu::PixelBufferAccess getAccess (void);
319 const rr::ViewportState getViewportState (void) const;
322 rr::Renderer m_renderer;
324 const int m_surfaceWidth;
325 const int m_surfaceHeight;
326 const int m_numSamples;
328 const tcu::TextureFormat m_colorFormat;
329 const tcu::TextureFormat m_depthStencilFormat;
331 tcu::TextureLevel m_colorBuffer;
332 tcu::TextureLevel m_resolveColorBuffer;
333 tcu::TextureLevel m_depthStencilBuffer;
335 rr::RenderTarget* m_renderTarget;
336 const rr::Program* m_program;
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);
349 #endif // _VKTPIPELINEREFERENCERENDERER_HPP