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 * Licensed under the Apache License, Version 2.0 (the "License"); 11 * you may not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 * See the License for the specific language governing permissions and 20 * limitations under the License. 21 * 22 *//*! 23 * \file 24 * \brief Reference renderer. 25 *//*--------------------------------------------------------------------*/ 26 27 #include "vkDefs.hpp" 28 #include "vktPipelineVertexUtil.hpp" 29 #include "tcuVector.hpp" 30 #include "tcuVectorType.hpp" 31 #include "tcuTexture.hpp" 32 #include "tcuTextureUtil.hpp" 33 #include "rrRenderState.hpp" 34 #include "rrRenderer.hpp" 35 #include <cstring> 36 37 namespace vkt 38 { 39 40 namespace pipeline 41 { 42 43 tcu::Vec4 swizzle (const tcu::Vec4& color, const tcu::UVec4& swizzle); 44 45 class ColorVertexShader : public rr::VertexShader 46 { 47 public: ColorVertexShader(void)48 ColorVertexShader (void) : rr::VertexShader(2, 2) 49 { 50 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT; 51 m_inputs[1].type = rr::GENERICVECTYPE_FLOAT; 52 53 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT; 54 m_outputs[1].type = rr::GENERICVECTYPE_FLOAT; 55 } 56 ~ColorVertexShader(void)57 virtual ~ColorVertexShader (void) {} 58 shadeVertices(const rr::VertexAttrib * inputs,rr::VertexPacket * const * packets,const int numPackets) const59 virtual void shadeVertices (const rr::VertexAttrib* inputs, 60 rr::VertexPacket* const* packets, 61 const int numPackets) const 62 { 63 tcu::Vec4 position; 64 tcu::Vec4 color; 65 66 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++) 67 { 68 rr::VertexPacket* const packet = packets[packetNdx]; 69 70 readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx); 71 readVertexAttrib(color, inputs[1], packet->instanceNdx, packet->vertexNdx); 72 73 packet->outputs[0] = position; 74 packet->outputs[1] = color; 75 packet->position = position; 76 } 77 } 78 }; 79 80 class ColorVertexShaderDualSource : public rr::VertexShader 81 { 82 public: ColorVertexShaderDualSource(void)83 ColorVertexShaderDualSource (void) : rr::VertexShader(3, 3) 84 { 85 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT; 86 m_inputs[1].type = rr::GENERICVECTYPE_FLOAT; 87 m_inputs[2].type = rr::GENERICVECTYPE_FLOAT; 88 89 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT; 90 m_outputs[1].type = rr::GENERICVECTYPE_FLOAT; 91 m_outputs[2].type = rr::GENERICVECTYPE_FLOAT; 92 } 93 ~ColorVertexShaderDualSource(void)94 virtual ~ColorVertexShaderDualSource (void) {} 95 shadeVertices(const rr::VertexAttrib * inputs,rr::VertexPacket * const * packets,const int numPackets) const96 virtual void shadeVertices (const rr::VertexAttrib* inputs, 97 rr::VertexPacket* const* packets, 98 const int numPackets) const 99 { 100 tcu::Vec4 position; 101 tcu::Vec4 color0; 102 tcu::Vec4 color1; 103 104 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++) 105 { 106 rr::VertexPacket* const packet = packets[packetNdx]; 107 108 readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx); 109 readVertexAttrib(color0, inputs[1], packet->instanceNdx, packet->vertexNdx); 110 readVertexAttrib(color1, inputs[2], packet->instanceNdx, packet->vertexNdx); 111 112 packet->outputs[0] = position; 113 packet->outputs[1] = color0; 114 packet->outputs[2] = color1; 115 packet->position = position; 116 } 117 } 118 }; 119 120 class TexCoordVertexShader : public rr::VertexShader 121 { 122 public: TexCoordVertexShader(void)123 TexCoordVertexShader (void) : rr::VertexShader(2, 2) 124 { 125 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT; 126 m_inputs[1].type = rr::GENERICVECTYPE_FLOAT; 127 128 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT; 129 m_outputs[1].type = rr::GENERICVECTYPE_FLOAT; 130 } 131 ~TexCoordVertexShader(void)132 virtual ~TexCoordVertexShader (void) {} 133 shadeVertices(const rr::VertexAttrib * inputs,rr::VertexPacket * const * packets,const int numPackets) const134 virtual void shadeVertices (const rr::VertexAttrib* inputs, 135 rr::VertexPacket* const* packets, 136 const int numPackets) const 137 { 138 tcu::Vec4 position; 139 tcu::Vec4 texCoord; 140 141 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++) 142 { 143 rr::VertexPacket* const packet = packets[packetNdx]; 144 145 readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx); 146 readVertexAttrib(texCoord, inputs[1], packet->instanceNdx, packet->vertexNdx); 147 148 packet->outputs[0] = position; 149 packet->outputs[1] = texCoord; 150 packet->position = position; 151 } 152 } 153 }; 154 155 class ColorFragmentShader : public rr::FragmentShader 156 { 157 private: 158 const tcu::TextureFormat m_colorFormat; 159 const tcu::TextureFormat m_depthStencilFormat; 160 161 public: ColorFragmentShader(const tcu::TextureFormat & colorFormat,const tcu::TextureFormat & depthStencilFormat)162 ColorFragmentShader (const tcu::TextureFormat& colorFormat, 163 const tcu::TextureFormat& depthStencilFormat) 164 : rr::FragmentShader (2, 1) 165 , m_colorFormat (colorFormat) 166 , m_depthStencilFormat (depthStencilFormat) 167 { 168 const tcu::TextureChannelClass channelClass = tcu::getTextureChannelClass(m_colorFormat.type); 169 170 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT; 171 m_inputs[1].type = rr::GENERICVECTYPE_FLOAT; 172 m_outputs[0].type = (channelClass == tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER)? rr::GENERICVECTYPE_INT32 : 173 (channelClass == tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER)? rr::GENERICVECTYPE_UINT32 174 : rr::GENERICVECTYPE_FLOAT; 175 } 176 ~ColorFragmentShader(void)177 virtual ~ColorFragmentShader (void) {} 178 shadeFragments(rr::FragmentPacket * packets,const int numPackets,const rr::FragmentShadingContext & context) const179 virtual void shadeFragments (rr::FragmentPacket* packets, 180 const int numPackets, 181 const rr::FragmentShadingContext& context) const 182 { 183 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++) 184 { 185 const rr::FragmentPacket& packet = packets[packetNdx]; 186 187 if (m_depthStencilFormat.order == tcu::TextureFormat::D || m_depthStencilFormat.order == tcu::TextureFormat::DS) 188 { 189 for (int fragNdx = 0; fragNdx < 4; fragNdx++) 190 { 191 const tcu::Vec4 vtxPosition = rr::readVarying<float>(packet, context, 0, fragNdx); 192 rr::writeFragmentDepth(context, packetNdx, fragNdx, 0, vtxPosition.z()); 193 } 194 } 195 196 for (int fragNdx = 0; fragNdx < 4; fragNdx++) 197 { 198 const tcu::Vec4 vtxColor = rr::readVarying<float>(packet, context, 1, fragNdx); 199 rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, vtxColor); 200 } 201 } 202 } 203 }; 204 205 class ColorFragmentShaderDualSource : public rr::FragmentShader 206 { 207 private: 208 const tcu::TextureFormat m_colorFormat; 209 const tcu::TextureFormat m_depthStencilFormat; 210 211 public: ColorFragmentShaderDualSource(const tcu::TextureFormat & colorFormat,const tcu::TextureFormat & depthStencilFormat)212 ColorFragmentShaderDualSource (const tcu::TextureFormat& colorFormat, 213 const tcu::TextureFormat& depthStencilFormat) 214 : rr::FragmentShader (3, 1) 215 , m_colorFormat (colorFormat) 216 , m_depthStencilFormat (depthStencilFormat) 217 { 218 const tcu::TextureChannelClass channelClass = tcu::getTextureChannelClass(m_colorFormat.type); 219 220 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT; 221 m_inputs[1].type = rr::GENERICVECTYPE_FLOAT; 222 m_inputs[2].type = rr::GENERICVECTYPE_FLOAT; 223 224 m_outputs[0].type = (channelClass == tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER)? rr::GENERICVECTYPE_INT32 : 225 (channelClass == tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER)? rr::GENERICVECTYPE_UINT32 226 : rr::GENERICVECTYPE_FLOAT; 227 } 228 ~ColorFragmentShaderDualSource(void)229 virtual ~ColorFragmentShaderDualSource (void) {} 230 shadeFragments(rr::FragmentPacket * packets,const int numPackets,const rr::FragmentShadingContext & context) const231 virtual void shadeFragments (rr::FragmentPacket* packets, 232 const int numPackets, 233 const rr::FragmentShadingContext& context) const 234 { 235 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++) 236 { 237 const rr::FragmentPacket& packet = packets[packetNdx]; 238 239 if (m_depthStencilFormat.order == tcu::TextureFormat::D || m_depthStencilFormat.order == tcu::TextureFormat::DS) 240 { 241 for (int fragNdx = 0; fragNdx < 4; fragNdx++) 242 { 243 const tcu::Vec4 vtxPosition = rr::readVarying<float>(packet, context, 0, fragNdx); 244 rr::writeFragmentDepth(context, packetNdx, fragNdx, 0, vtxPosition.z()); 245 } 246 } 247 248 for (int fragNdx = 0; fragNdx < 4; fragNdx++) 249 { 250 const tcu::Vec4 vtxColor0 = rr::readVarying<float>(packet, context, 1, fragNdx); 251 const tcu::Vec4 vtxColor1 = rr::readVarying<float>(packet, context, 2, fragNdx); 252 rr::writeFragmentOutputDualSource(context, packetNdx, fragNdx, 0, vtxColor0, vtxColor1); 253 } 254 } 255 } 256 }; 257 258 class CoordinateCaptureFragmentShader : public rr::FragmentShader 259 { 260 public: CoordinateCaptureFragmentShader(void)261 CoordinateCaptureFragmentShader (void) 262 : rr::FragmentShader(2, 1) 263 { 264 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT; 265 m_inputs[1].type = rr::GENERICVECTYPE_FLOAT; 266 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT; 267 } 268 ~CoordinateCaptureFragmentShader(void)269 virtual ~CoordinateCaptureFragmentShader (void) 270 { 271 } 272 shadeFragments(rr::FragmentPacket * packets,const int numPackets,const rr::FragmentShadingContext & context) const273 virtual void shadeFragments (rr::FragmentPacket* packets, 274 const int numPackets, 275 const rr::FragmentShadingContext& context) const 276 { 277 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++) 278 { 279 const rr::FragmentPacket& packet = packets[packetNdx]; 280 281 for (int fragNdx = 0; fragNdx < 4; fragNdx++) 282 { 283 const tcu::Vec4 vtxTexCoord = rr::readVarying<float>(packet, context, 1, fragNdx); 284 rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, vtxTexCoord); 285 } 286 } 287 } 288 }; 289 290 class Program 291 { 292 public: ~Program(void)293 virtual ~Program (void) { } 294 295 virtual rr::Program getReferenceProgram (void) const = 0; 296 }; 297 298 class CoordinateCaptureProgram : public Program 299 { 300 private: 301 TexCoordVertexShader m_vertexShader; 302 CoordinateCaptureFragmentShader m_fragmentShader; 303 public: CoordinateCaptureProgram(void)304 CoordinateCaptureProgram (void) 305 { 306 } 307 ~CoordinateCaptureProgram(void)308 virtual ~CoordinateCaptureProgram (void) { } 309 getReferenceProgram(void) const310 virtual rr::Program getReferenceProgram (void) const 311 { 312 return rr::Program(&m_vertexShader, &m_fragmentShader); 313 } 314 }; 315 316 class ReferenceRenderer 317 { 318 public: 319 ReferenceRenderer (int surfaceWidth, 320 int surfaceHeight, 321 int numSamples, 322 const tcu::TextureFormat& colorFormat, 323 const tcu::TextureFormat& depthStencilFormat, 324 const rr::Program* const program); 325 326 virtual ~ReferenceRenderer (void); 327 328 void colorClear (const tcu::Vec4& color); 329 330 void draw (const rr::RenderState& renderState, 331 const rr::PrimitiveType primitive, 332 const std::vector<Vertex4RGBA>& vertexBuffer); 333 334 void draw (const rr::RenderState& renderState, 335 const rr::PrimitiveType primitive, 336 const std::vector<Vertex4RGBARGBA>& vertexBuffer); 337 338 void draw (const rr::RenderState& renderState, 339 const rr::PrimitiveType primitive, 340 const std::vector<Vertex4Tex4>& vertexBuffer); 341 342 tcu::PixelBufferAccess getAccess (void); 343 tcu::PixelBufferAccess getDepthStencilAccess (void); 344 const rr::ViewportState getViewportState (void) const; 345 346 private: 347 rr::Renderer m_renderer; 348 349 const int m_surfaceWidth; 350 const int m_surfaceHeight; 351 const int m_numSamples; 352 353 const tcu::TextureFormat m_colorFormat; 354 const tcu::TextureFormat m_depthStencilFormat; 355 356 tcu::TextureLevel m_colorBuffer; 357 tcu::TextureLevel m_resolveColorBuffer; 358 tcu::TextureLevel m_depthStencilBuffer; 359 tcu::TextureLevel m_resolveDepthStencilBuffer; 360 361 rr::RenderTarget* m_renderTarget; 362 const rr::Program* m_program; 363 }; 364 365 rr::TestFunc mapVkCompareOp (vk::VkCompareOp compareFunc); 366 rr::PrimitiveType mapVkPrimitiveTopology (vk::VkPrimitiveTopology primitiveTopology); 367 rr::BlendFunc mapVkBlendFactor (vk::VkBlendFactor blendFactor); 368 rr::BlendEquation mapVkBlendOp (vk::VkBlendOp blendOp); 369 tcu::BVec4 mapVkColorComponentFlags (vk::VkColorComponentFlags flags); 370 rr::StencilOp mapVkStencilOp (vk::VkStencilOp stencilOp); 371 372 } // pipeline 373 } // vkt 374 375 #endif // _VKTPIPELINEREFERENCERENDERER_HPP 376