Tony Barbour | 32f368c | 2014-10-30 14:29:04 -0600 | [diff] [blame^] | 1 | // Copyright 2005, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | |
| 31 | // XGL tests |
| 32 | // |
| 33 | // Copyright (C) 2014 LunarG, Inc. |
| 34 | // |
| 35 | // Permission is hereby granted, free of charge, to any person obtaining a |
| 36 | // copy of this software and associated documentation files (the "Software"), |
| 37 | // to deal in the Software without restriction, including without limitation |
| 38 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 39 | // and/or sell copies of the Software, and to permit persons to whom the |
| 40 | // Software is furnished to do so, subject to the following conditions: |
| 41 | // |
| 42 | // The above copyright notice and this permission notice shall be included |
| 43 | // in all copies or substantial portions of the Software. |
| 44 | // |
| 45 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 46 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 47 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 48 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 49 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 50 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 51 | // DEALINGS IN THE SOFTWARE. |
| 52 | |
| 53 | // Basic rendering tests |
| 54 | |
| 55 | #include <stdlib.h> |
| 56 | #include <stdio.h> |
| 57 | #include <stdbool.h> |
| 58 | #include <string.h> |
| 59 | #include <iostream> |
| 60 | #include <fstream> |
| 61 | using namespace std; |
| 62 | |
| 63 | #include <xgl.h> |
| 64 | #include "gtest-1.7.0/include/gtest/gtest.h" |
| 65 | |
| 66 | #include "xgldevice.h" |
| 67 | #include "xglimage.h" |
| 68 | #include "icd-bil.h" |
| 69 | |
| 70 | #define GLM_FORCE_RADIANS |
| 71 | #include "glm/glm.hpp" |
| 72 | #include <glm/gtc/matrix_transform.hpp> |
| 73 | |
| 74 | #include "xglrenderframework.h" |
| 75 | #include "tony_objs.c" |
| 76 | |
| 77 | #undef ASSERT_NO_FATAL_FAILURE |
| 78 | #define ASSERT_NO_FATAL_FAILURE(x) x |
| 79 | |
| 80 | //-------------------------------------------------------------------------------------- |
| 81 | // Mesh and VertexFormat Data |
| 82 | //-------------------------------------------------------------------------------------- |
| 83 | struct Vertex |
| 84 | { |
| 85 | XGL_FLOAT posX, posY, posZ, posW; // Position data |
| 86 | XGL_FLOAT r, g, b, a; // Color |
| 87 | }; |
| 88 | |
| 89 | #define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f |
| 90 | |
| 91 | static const Vertex g_vbData[] = |
| 92 | { |
| 93 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, |
| 94 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 95 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 96 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 97 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 98 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 99 | |
| 100 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 101 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 102 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 103 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 104 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 105 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) }, |
| 106 | |
| 107 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) }, |
| 108 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 109 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 110 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 111 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 112 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 113 | |
| 114 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 115 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 116 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 117 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 118 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 119 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, |
| 120 | |
| 121 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) }, |
| 122 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 123 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 124 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 125 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 126 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 127 | |
| 128 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 129 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 130 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 131 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 132 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 133 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, |
| 134 | }; |
| 135 | |
| 136 | static const Vertex g_vb_solid_face_colors_Data[] = |
| 137 | { |
| 138 | { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 139 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 140 | { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 141 | { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 142 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 143 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 144 | |
| 145 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 146 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 147 | { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 148 | { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 149 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 150 | { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 151 | |
| 152 | { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 153 | { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 154 | { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 155 | { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 156 | { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 157 | { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 158 | |
| 159 | { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 160 | { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 161 | { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 162 | { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 163 | { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 164 | { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 165 | |
| 166 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 167 | { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 168 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 169 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 170 | { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 171 | { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 172 | |
| 173 | { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 174 | { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 175 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 176 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 177 | { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 178 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 179 | }; |
| 180 | |
| 181 | class XglRenderTest : public XglRenderFramework |
| 182 | { |
| 183 | public: |
| 184 | void UploadMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, const void* vertices ); |
| 185 | void InitTexture(); |
| 186 | void InitSampler(); |
| 187 | void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText); |
| 188 | void RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model, |
| 189 | XglConstantBufferObj constantBuffer); |
| 190 | void GenericDrawTriangleTest(XglPipelineObj pipelineobj, XglDescriptorSetObj descriptorSet, int numTris); |
| 191 | void QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs); |
| 192 | |
| 193 | void ClearDepthStencil(XGL_FLOAT value); |
| 194 | void ClearRenderBuffer(XGL_UINT32 clear_color); |
| 195 | void InitDepthStencil(); |
| 196 | void DrawRotatedTriangleTest(); |
| 197 | void GenerateClearAndPrepareBufferCmds(); |
| 198 | void XGLTriangleTest(const char *vertShaderText, const char *fragShaderText); |
| 199 | |
| 200 | |
| 201 | protected: |
| 202 | XGL_IMAGE m_texture; |
| 203 | XGL_IMAGE_VIEW m_textureView; |
| 204 | XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo; |
| 205 | XGL_GPU_MEMORY m_textureMem; |
| 206 | |
| 207 | XGL_SAMPLER m_sampler; |
| 208 | |
| 209 | XGL_FORMAT m_depth_stencil_fmt; |
| 210 | XGL_IMAGE m_depthStencilImage; |
| 211 | XGL_GPU_MEMORY m_depthStencilMem; |
| 212 | XGL_DEPTH_STENCIL_VIEW m_depthStencilView; |
| 213 | XglMemoryRefManager m_memoryRefManager; |
| 214 | |
| 215 | // XGL_APPLICATION_INFO app_info; |
| 216 | // XGL_PHYSICAL_GPU objs[MAX_GPUS]; |
| 217 | // XGL_UINT gpu_count; |
| 218 | // XGL_GPU_MEMORY m_descriptor_set_mem; |
| 219 | // XGL_GPU_MEMORY m_pipe_mem; |
| 220 | // XglDevice *m_device; |
| 221 | // XGL_CMD_BUFFER m_cmdBuffer; |
| 222 | // XGL_UINT32 m_numVertices; |
| 223 | // XGL_MEMORY_VIEW_ATTACH_INFO m_vtxBufferView; |
| 224 | // XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView; |
| 225 | // XGL_GPU_MEMORY m_vtxBufferMem; |
| 226 | // XGL_GPU_MEMORY m_constantBufferMem; |
| 227 | // XGL_UINT32 m_numMemRefs; |
| 228 | // XGL_MEMORY_REF m_memRefs[5]; |
| 229 | // XGL_RASTER_STATE_OBJECT m_stateRaster; |
| 230 | // XGL_COLOR_BLEND_STATE_OBJECT m_colorBlend; |
| 231 | // XGL_VIEWPORT_STATE_OBJECT m_stateViewport; |
| 232 | // XGL_DEPTH_STENCIL_STATE_OBJECT m_stateDepthStencil; |
| 233 | // XGL_MSAA_STATE_OBJECT m_stateMsaa; |
| 234 | // XGL_DESCRIPTOR_SET m_rsrcDescSet; |
| 235 | |
| 236 | virtual void SetUp() { |
| 237 | |
| 238 | this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO; |
| 239 | this->app_info.pNext = NULL; |
| 240 | this->app_info.pAppName = (const XGL_CHAR *) "render_tests"; |
| 241 | this->app_info.appVersion = 1; |
| 242 | this->app_info.pEngineName = (const XGL_CHAR *) "unittest"; |
| 243 | this->app_info.engineVersion = 1; |
| 244 | this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0); |
| 245 | |
| 246 | memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo)); |
| 247 | m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO; |
| 248 | |
| 249 | InitFramework(); |
| 250 | } |
| 251 | |
| 252 | virtual void TearDown() { |
| 253 | // Clean up resources before we reset |
| 254 | ShutdownFramework(); |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | // this function will create the vertex buffer and fill it with the mesh data |
| 259 | void XglRenderTest::UploadMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, |
| 260 | const void* vertices ) |
| 261 | { |
| 262 | XGL_UINT8 *pData; |
| 263 | XGL_RESULT err = XGL_SUCCESS; |
| 264 | |
| 265 | assert( numVertices * vbStride > 0 ); |
| 266 | m_numVertices = numVertices; |
| 267 | |
| 268 | err = xglMapMemory(m_vtxBufferMem, 0, (XGL_VOID **) &pData); |
| 269 | ASSERT_XGL_SUCCESS(err); |
| 270 | |
| 271 | memcpy(pData, vertices, numVertices * vbStride); |
| 272 | |
| 273 | err = xglUnmapMemory(m_vtxBufferMem); |
| 274 | ASSERT_XGL_SUCCESS(err); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | |
| 279 | void XglRenderTest::DrawRotatedTriangleTest() |
| 280 | { |
| 281 | // TODO : This test will pass a matrix into VS to affect triangle orientation. |
| 282 | } |
| 283 | void XglRenderTest::GenericDrawTriangleTest(XglPipelineObj pipelineobj, XglDescriptorSetObj descriptorSet,int numTris) |
| 284 | { |
| 285 | XGL_PIPELINE pipeline; // Not really used but it's useful to keep original tests working to compare |
| 286 | XGL_RESULT err = XGL_SUCCESS; |
| 287 | |
| 288 | ASSERT_NO_FATAL_FAILURE(InitRenderTarget()); |
| 289 | // Build command buffer |
| 290 | err = xglBeginCommandBuffer(m_cmdBuffer, 0); |
| 291 | ASSERT_XGL_SUCCESS(err); |
| 292 | |
| 293 | GenerateClearAndPrepareBufferCmds(); |
| 294 | GenerateBindRenderTargetCmd(); |
| 295 | GenerateBindStateAndPipelineCmds(&pipeline); |
| 296 | |
| 297 | pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet); |
| 298 | descriptorSet.BindCommandBuffer(m_cmdBuffer); |
| 299 | |
| 300 | // render the triangle |
| 301 | xglCmdDraw( m_cmdBuffer, 0, 3*numTris, 0, 1 ); |
| 302 | |
| 303 | // finalize recording of the command buffer |
| 304 | err = xglEndCommandBuffer( m_cmdBuffer ); |
| 305 | ASSERT_XGL_SUCCESS( err ); |
| 306 | } |
| 307 | |
| 308 | void XglRenderTest::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs) |
| 309 | { |
| 310 | XGL_RESULT err = XGL_SUCCESS; |
| 311 | |
| 312 | // submit the command buffer to the universal queue |
| 313 | err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, numMemRefs, memRefs, NULL ); |
| 314 | ASSERT_XGL_SUCCESS( err ); |
| 315 | |
| 316 | err = xglQueueWaitIdle( m_device->m_queue ); |
| 317 | ASSERT_XGL_SUCCESS( err ); |
| 318 | |
| 319 | // Wait for work to finish before cleaning up. |
| 320 | xglDeviceWaitIdle(m_device->device()); |
| 321 | |
| 322 | RecordImage(m_renderTarget); |
| 323 | |
| 324 | } |
| 325 | |
| 326 | void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText) |
| 327 | { |
| 328 | XGL_PIPELINE pipeline; |
| 329 | XGL_RESULT err; |
| 330 | |
| 331 | ASSERT_NO_FATAL_FAILURE(InitState()); |
| 332 | ASSERT_NO_FATAL_FAILURE(InitViewport()); |
| 333 | |
| 334 | XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX ); |
| 335 | XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT); |
| 336 | |
| 337 | |
| 338 | XglPipelineObj pipelineobj(m_device); |
| 339 | pipelineobj.AddShader(&vs); |
| 340 | pipelineobj.AddShader(&ps); |
| 341 | |
| 342 | ASSERT_NO_FATAL_FAILURE(InitRenderTarget()); |
| 343 | |
| 344 | // Create descriptor set |
| 345 | XglDescriptorSetObj descriptorSet(m_device); |
| 346 | |
| 347 | // Build command buffer |
| 348 | err = xglBeginCommandBuffer(m_cmdBuffer, 0); |
| 349 | ASSERT_XGL_SUCCESS(err); |
| 350 | |
| 351 | GenerateClearAndPrepareBufferCmds(); |
| 352 | GenerateBindRenderTargetCmd(); |
| 353 | GenerateBindStateAndPipelineCmds(&pipeline); |
| 354 | |
| 355 | pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet); |
| 356 | descriptorSet.BindCommandBuffer(m_cmdBuffer); |
| 357 | |
| 358 | |
| 359 | // render the triangle |
| 360 | xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 ); |
| 361 | |
| 362 | // finalize recording of the command buffer |
| 363 | err = xglEndCommandBuffer( m_cmdBuffer ); |
| 364 | ASSERT_XGL_SUCCESS( err ); |
| 365 | |
| 366 | // this command buffer only uses the vertex buffer memory |
| 367 | m_numMemRefs = 0; |
| 368 | |
| 369 | // submit the command buffer to the universal queue |
| 370 | err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL ); |
| 371 | ASSERT_XGL_SUCCESS( err ); |
| 372 | |
| 373 | err = xglQueueWaitIdle( m_device->m_queue ); |
| 374 | ASSERT_XGL_SUCCESS( err ); |
| 375 | |
| 376 | // Wait for work to finish before cleaning up. |
| 377 | xglDeviceWaitIdle(m_device->device()); |
| 378 | |
| 379 | RecordImage(m_renderTarget); |
| 380 | |
| 381 | } |
| 382 | |
| 383 | void XglRenderTest::RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model, |
| 384 | XglConstantBufferObj constantBuffer) |
| 385 | { |
| 386 | int i; |
| 387 | glm::mat4 MVP; |
| 388 | int matrixSize = sizeof(MVP); |
| 389 | XGL_RESULT err; |
| 390 | |
| 391 | for (i = 0; i < 8; i++) { |
| 392 | XGL_UINT8 *pData; |
| 393 | err = xglMapMemory(constantBuffer.m_constantBufferMem, 0, (XGL_VOID **) &pData); |
| 394 | ASSERT_XGL_SUCCESS(err); |
| 395 | |
| 396 | Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f)); |
| 397 | MVP = Projection * View * Model; |
| 398 | memcpy(pData, (const void*) &MVP[0][0], matrixSize); |
| 399 | |
| 400 | err = xglUnmapMemory(constantBuffer.m_constantBufferMem); |
| 401 | ASSERT_XGL_SUCCESS(err); |
| 402 | |
| 403 | // submit the command buffer to the universal queue |
| 404 | err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_memoryRefManager.GetNumRefs(), m_memoryRefManager.GetMemoryRefList(), NULL ); |
| 405 | ASSERT_XGL_SUCCESS( err ); |
| 406 | |
| 407 | err = xglQueueWaitIdle( m_device->m_queue ); |
| 408 | ASSERT_XGL_SUCCESS( err ); |
| 409 | |
| 410 | // Wait for work to finish before cleaning up. |
| 411 | xglDeviceWaitIdle(m_device->device()); |
| 412 | |
| 413 | RecordImage(m_renderTarget); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void dumpMatrix(const char *note, glm::mat4 MVP) |
| 418 | { |
| 419 | int i,j; |
| 420 | |
| 421 | printf("%s: \n", note); |
| 422 | for (i=0; i<4; i++) { |
| 423 | printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]); |
| 424 | } |
| 425 | printf("\n"); |
| 426 | fflush(stdout); |
| 427 | } |
| 428 | |
| 429 | void dumpVec4(const char *note, glm::vec4 vector) |
| 430 | { |
| 431 | printf("%s: \n", note); |
| 432 | printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]); |
| 433 | printf("\n"); |
| 434 | fflush(stdout); |
| 435 | } |
| 436 | |
| 437 | void XglRenderTest::GenerateClearAndPrepareBufferCmds() |
| 438 | { |
| 439 | XglRenderFramework::GenerateClearAndPrepareBufferCmds(); |
| 440 | |
| 441 | if (0) { |
| 442 | // if (m_depthStencilImage) { |
| 443 | XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {}; |
| 444 | dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH; |
| 445 | dsRange.baseMipLevel = 0; |
| 446 | dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE; |
| 447 | dsRange.baseArraySlice = 0; |
| 448 | dsRange.arraySize = XGL_LAST_MIP_OR_SLICE; |
| 449 | |
| 450 | // prepare the depth buffer for clear |
| 451 | XGL_IMAGE_STATE_TRANSITION transitionToClear = {}; |
| 452 | transitionToClear.image = m_depthStencilImage; |
| 453 | transitionToClear.oldState = m_depthStencilBinding.depthState; |
| 454 | transitionToClear.newState = XGL_IMAGE_STATE_CLEAR; |
| 455 | transitionToClear.subresourceRange = dsRange; |
| 456 | xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear ); |
| 457 | m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState); |
| 458 | |
| 459 | xglCmdClearDepthStencil(m_cmdBuffer, m_depthStencilImage, 1.0f, 0, 1, &dsRange); |
| 460 | |
| 461 | // prepare depth buffer for rendering |
| 462 | XGL_IMAGE_STATE_TRANSITION transitionToRender = {}; |
| 463 | transitionToRender.image = m_renderTarget->image(); |
| 464 | transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR; |
| 465 | transitionToRender.newState = m_depthStencilBinding.depthState; |
| 466 | transitionToRender.subresourceRange = dsRange; |
| 467 | xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender ); |
| 468 | m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | |
| 473 | void XglRenderTest::ClearDepthStencil(XGL_FLOAT value) |
| 474 | /* clear the buffer */ |
| 475 | { |
| 476 | XGL_RESULT err; |
| 477 | const uint16_t depth_value = (uint16_t) (value * 65535); |
| 478 | const XGL_INT tw = 128 / sizeof(uint16_t); |
| 479 | const XGL_INT th = 32; |
| 480 | XGL_INT i, j, w, h; |
| 481 | XGL_VOID *data; |
| 482 | |
| 483 | w = (m_width + tw - 1) / tw; |
| 484 | h = (m_height + th - 1) / th; |
| 485 | |
| 486 | err = xglMapMemory(m_depthStencilMem, 0, &data); |
| 487 | ASSERT_XGL_SUCCESS(err); |
| 488 | |
| 489 | for (i = 0; i < w * h; i++) { |
| 490 | uint16_t *tile = (uint16_t *) ((char *) data + 4096 * i); |
| 491 | |
| 492 | for (j = 0; j < 2048; j++) |
| 493 | tile[j] = depth_value; |
| 494 | } |
| 495 | |
| 496 | err = xglUnmapMemory(m_depthStencilMem); |
| 497 | ASSERT_XGL_SUCCESS(err); |
| 498 | } |
| 499 | |
| 500 | void XglRenderTest::ClearRenderBuffer(XGL_UINT32 clear_color) |
| 501 | /* clear the buffer */ |
| 502 | { |
| 503 | XGL_RESULT err; |
| 504 | const XGL_IMAGE_SUBRESOURCE sr = { |
| 505 | XGL_IMAGE_ASPECT_COLOR, 0, 0 |
| 506 | }; |
| 507 | XGL_SUBRESOURCE_LAYOUT sr_layout; |
| 508 | XGL_UINT data_size = sizeof(sr_layout); |
| 509 | XGL_VOID *ptr; |
| 510 | |
| 511 | err = xglGetImageSubresourceInfo( m_renderTarget->image(), |
| 512 | &sr, XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, |
| 513 | &data_size, &sr_layout); |
| 514 | ASSERT_XGL_SUCCESS( err ); |
| 515 | ASSERT_EQ(data_size, sizeof(sr_layout)); |
| 516 | |
| 517 | err = m_renderTarget->MapMemory( &ptr ); |
| 518 | ASSERT_XGL_SUCCESS( err ); |
| 519 | |
| 520 | ptr = (void *) ((char *) ptr + sr_layout.offset); |
| 521 | |
| 522 | memset(ptr, clear_color, m_width * m_height *sizeof(XGL_UINT32)); |
| 523 | |
| 524 | err = m_renderTarget->UnmapMemory(); |
| 525 | ASSERT_XGL_SUCCESS(err); |
| 526 | } |
| 527 | |
| 528 | void XglRenderTest::InitDepthStencil() |
| 529 | { |
| 530 | XGL_RESULT err; |
| 531 | XGL_IMAGE_CREATE_INFO image; |
| 532 | XGL_MEMORY_ALLOC_INFO mem_alloc; |
| 533 | XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view; |
| 534 | XGL_MEMORY_REQUIREMENTS mem_reqs; |
| 535 | XGL_SIZE mem_reqs_size; |
| 536 | |
| 537 | // Clean up default state created by framework |
| 538 | if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil); |
| 539 | |
| 540 | m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16; |
| 541 | m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS; |
| 542 | |
| 543 | image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 544 | image.pNext = NULL; |
| 545 | image.imageType = XGL_IMAGE_2D; |
| 546 | image.format = m_depth_stencil_fmt; |
| 547 | image.extent.width = m_width; |
| 548 | image.extent.height = m_height; |
| 549 | image.extent.depth = 1; |
| 550 | image.mipLevels = 1; |
| 551 | image.arraySize = 1; |
| 552 | image.samples = 1; |
| 553 | image.tiling = XGL_OPTIMAL_TILING; |
| 554 | image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT; |
| 555 | image.flags = 0; |
| 556 | |
| 557 | mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 558 | mem_alloc.pNext = NULL; |
| 559 | mem_alloc.allocationSize = 0; |
| 560 | mem_alloc.alignment = 0; |
| 561 | mem_alloc.flags = 0; |
| 562 | mem_alloc.heapCount = 0; |
| 563 | mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 564 | |
| 565 | /* create image */ |
| 566 | err = xglCreateImage(device(), &image, |
| 567 | &m_depthStencilImage); |
| 568 | ASSERT_XGL_SUCCESS(err); |
| 569 | |
| 570 | err = xglGetObjectInfo(m_depthStencilImage, |
| 571 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 572 | &mem_reqs_size, &mem_reqs); |
| 573 | ASSERT_XGL_SUCCESS(err); |
| 574 | ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs)); |
| 575 | |
| 576 | mem_alloc.allocationSize = mem_reqs.size; |
| 577 | mem_alloc.alignment = mem_reqs.alignment; |
| 578 | mem_alloc.heapCount = mem_reqs.heapCount; |
| 579 | memcpy(mem_alloc.heaps, mem_reqs.heaps, |
| 580 | sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount); |
| 581 | |
| 582 | /* allocate memory */ |
| 583 | err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem); |
| 584 | ASSERT_XGL_SUCCESS(err); |
| 585 | |
| 586 | /* bind memory */ |
| 587 | err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0); |
| 588 | ASSERT_XGL_SUCCESS(err); |
| 589 | |
| 590 | XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {}; |
| 591 | depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO; |
| 592 | depthStencil.depthTestEnable = XGL_TRUE; |
| 593 | depthStencil.depthWriteEnable = XGL_TRUE; |
| 594 | depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL; |
| 595 | depthStencil.depthBoundsEnable = XGL_FALSE; |
| 596 | depthStencil.minDepth = 0.f; |
| 597 | depthStencil.maxDepth = 1.f; |
| 598 | depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP; |
| 599 | depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP; |
| 600 | depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP; |
| 601 | depthStencil.back.stencilRef = 0x00; |
| 602 | depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS; |
| 603 | depthStencil.front = depthStencil.back; |
| 604 | |
| 605 | err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil ); |
| 606 | ASSERT_XGL_SUCCESS( err ); |
| 607 | |
| 608 | /* create image view */ |
| 609 | view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO; |
| 610 | view.pNext = NULL; |
| 611 | view.image = XGL_NULL_HANDLE; |
| 612 | view.mipLevel = 0; |
| 613 | view.baseArraySlice = 0; |
| 614 | view.arraySize = 1; |
| 615 | view.flags = 0; |
| 616 | view.image = m_depthStencilImage; |
| 617 | err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView); |
| 618 | ASSERT_XGL_SUCCESS(err); |
| 619 | |
| 620 | m_depthStencilBinding.view = m_depthStencilView; |
| 621 | m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL; |
| 622 | m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL; |
| 623 | } |
| 624 | |
| 625 | struct xgltriangle_vs_uniform { |
| 626 | // Must start with MVP |
| 627 | XGL_FLOAT mvp[4][4]; |
| 628 | XGL_FLOAT position[3][4]; |
| 629 | XGL_FLOAT color[3][4]; |
| 630 | }; |
| 631 | |
| 632 | void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText) |
| 633 | { |
| 634 | // Create identity matrix |
| 635 | int i; |
| 636 | struct xgltriangle_vs_uniform data; |
| 637 | |
| 638 | glm::mat4 Projection = glm::mat4(1.0f); |
| 639 | glm::mat4 View = glm::mat4(1.0f); |
| 640 | glm::mat4 Model = glm::mat4(1.0f); |
| 641 | glm::mat4 MVP = Projection * View * Model; |
| 642 | const int matrixSize = sizeof(MVP); |
| 643 | const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT); |
| 644 | memcpy(&data.mvp, &MVP[0][0], matrixSize); |
| 645 | |
| 646 | static const Vertex tri_data[] = |
| 647 | { |
| 648 | { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 649 | { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 650 | { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 651 | }; |
| 652 | |
| 653 | for (i=0; i<3; i++) { |
| 654 | data.position[i][0] = tri_data[i].posX; |
| 655 | data.position[i][1] = tri_data[i].posY; |
| 656 | data.position[i][2] = tri_data[i].posZ; |
| 657 | data.position[i][3] = tri_data[i].posW; |
| 658 | data.color[i][0] = tri_data[i].r; |
| 659 | data.color[i][1] = tri_data[i].g; |
| 660 | data.color[i][2] = tri_data[i].b; |
| 661 | data.color[i][3] = tri_data[i].a; |
| 662 | } |
| 663 | |
| 664 | ASSERT_NO_FATAL_FAILURE(InitState()); |
| 665 | ASSERT_NO_FATAL_FAILURE(InitViewport()); |
| 666 | |
| 667 | XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data); |
| 668 | |
| 669 | XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX ); |
| 670 | XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT); |
| 671 | vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &constantBuffer.m_constantBufferView); |
| 672 | |
| 673 | XglPipelineObj pipelineobj(m_device); |
| 674 | pipelineobj.AddShader(&vs); |
| 675 | pipelineobj.AddShader(&ps); |
| 676 | |
| 677 | XglDescriptorSetObj descriptorSet(m_device); |
| 678 | descriptorSet.AttachMemoryView(&constantBuffer.m_constantBufferView); |
| 679 | m_memoryRefManager.AddMemoryRef(&constantBuffer.m_constantBufferMem); |
| 680 | |
| 681 | GenericDrawTriangleTest(pipelineobj, descriptorSet, 1); |
| 682 | QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs()); |
| 683 | |
| 684 | RotateTriangleVSUniform(Projection, View, Model, constantBuffer); |
| 685 | |
| 686 | } |
| 687 | |
| 688 | TEST_F(XglRenderTest, XGLTriangle_FragColor) |
| 689 | { |
| 690 | static const char *vertShaderText = |
| 691 | "#version 140\n" |
| 692 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 693 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 694 | "\n" |
| 695 | "layout(binding = 0) uniform buf {\n" |
| 696 | " mat4 MVP;\n" |
| 697 | " vec4 position[3];\n" |
| 698 | " vec4 color[3];\n" |
| 699 | "} ubuf;\n" |
| 700 | "\n" |
| 701 | "layout (location = 0) out vec4 outColor;\n" |
| 702 | "\n" |
| 703 | "void main() \n" |
| 704 | "{\n" |
| 705 | " outColor = ubuf.color[gl_VertexID];\n" |
| 706 | " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n" |
| 707 | "}\n"; |
| 708 | |
| 709 | static const char *fragShaderText = |
| 710 | "#version 140\n" |
| 711 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 712 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 713 | "\n" |
| 714 | "layout (location = 0) in vec4 inColor;\n" |
| 715 | "\n" |
| 716 | "void main()\n" |
| 717 | "{\n" |
| 718 | " gl_FragColor = inColor;\n" |
| 719 | "}\n"; |
| 720 | |
| 721 | TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor"); |
| 722 | XGLTriangleTest(vertShaderText, fragShaderText); |
| 723 | } |
| 724 | |
| 725 | TEST_F(XglRenderTest, XGLTriangle_OutputLocation) |
| 726 | { |
| 727 | static const char *vertShaderText = |
| 728 | "#version 140\n" |
| 729 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 730 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 731 | "\n" |
| 732 | "layout(binding = 0) uniform buf {\n" |
| 733 | " mat4 MVP;\n" |
| 734 | " vec4 position[3];\n" |
| 735 | " vec4 color[3];\n" |
| 736 | "} ubuf;\n" |
| 737 | "\n" |
| 738 | "layout (location = 0) out vec4 outColor;\n" |
| 739 | "\n" |
| 740 | "void main() \n" |
| 741 | "{\n" |
| 742 | " outColor = ubuf.color[gl_VertexID];\n" |
| 743 | " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n" |
| 744 | "}\n"; |
| 745 | |
| 746 | static const char *fragShaderText = |
| 747 | "#version 140\n" |
| 748 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 749 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 750 | "\n" |
| 751 | "layout (location = 0) in vec4 inColor;\n" |
| 752 | "layout (location = 0) out vec4 outColor;\n" |
| 753 | "\n" |
| 754 | "void main()\n" |
| 755 | "{\n" |
| 756 | " outColor = inColor;\n" |
| 757 | "}\n"; |
| 758 | |
| 759 | TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to output location 0, which should be the same as gl_FragColor"); |
| 760 | |
| 761 | XGLTriangleTest(vertShaderText, fragShaderText); |
| 762 | } |
| 763 | |
| 764 | TEST_F(XglRenderTest, BIL_XGLTriangle) |
| 765 | { |
| 766 | bool saved_use_bil = XglTestFramework::m_use_bil; |
| 767 | |
| 768 | static const char *vertShaderText = |
| 769 | "#version 140\n" |
| 770 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 771 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 772 | "\n" |
| 773 | "layout(binding = 0) uniform buf {\n" |
| 774 | " mat4 MVP;\n" |
| 775 | " vec4 position[3];\n" |
| 776 | " vec4 color[3];\n" |
| 777 | "} ubuf;\n" |
| 778 | "\n" |
| 779 | "layout (location = 0) out vec4 outColor;\n" |
| 780 | "\n" |
| 781 | "void main() \n" |
| 782 | "{\n" |
| 783 | " outColor = ubuf.color[gl_VertexID];\n" |
| 784 | " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n" |
| 785 | "}\n"; |
| 786 | |
| 787 | static const char *fragShaderText = |
| 788 | "#version 140\n" |
| 789 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 790 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 791 | "\n" |
| 792 | "layout (location = 0) in vec4 inColor;\n" |
| 793 | "\n" |
| 794 | "void main()\n" |
| 795 | "{\n" |
| 796 | " gl_FragColor = inColor;\n" |
| 797 | "}\n"; |
| 798 | |
| 799 | TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver."); |
| 800 | |
| 801 | XglTestFramework::m_use_bil = true; |
| 802 | |
| 803 | XGLTriangleTest(vertShaderText, fragShaderText); |
| 804 | |
| 805 | XglTestFramework::m_use_bil = saved_use_bil; |
| 806 | } |
| 807 | |
| 808 | TEST_F(XglRenderTest, GreenTriangle) |
| 809 | { |
| 810 | static const char *vertShaderText = |
| 811 | "#version 130\n" |
| 812 | "vec2 vertices[3];\n" |
| 813 | "void main() {\n" |
| 814 | " vertices[0] = vec2(-1.0, -1.0);\n" |
| 815 | " vertices[1] = vec2( 1.0, -1.0);\n" |
| 816 | " vertices[2] = vec2( 0.0, 1.0);\n" |
| 817 | " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 818 | "}\n"; |
| 819 | |
| 820 | static const char *fragShaderText = |
| 821 | "#version 130\n" |
| 822 | "void main() {\n" |
| 823 | " gl_FragColor = vec4(0,1,0,1);\n" |
| 824 | "}\n"; |
| 825 | |
| 826 | TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader."); |
| 827 | |
| 828 | DrawTriangleTest(vertShaderText, fragShaderText); |
| 829 | } |
| 830 | |
| 831 | TEST_F(XglRenderTest, BIL_GreenTriangle) |
| 832 | { |
| 833 | bool saved_use_bil = XglTestFramework::m_use_bil; |
| 834 | |
| 835 | static const char *vertShaderText = |
| 836 | "#version 130\n" |
| 837 | "vec2 vertices[3];\n" |
| 838 | "void main() {\n" |
| 839 | " vertices[0] = vec2(-1.0, -1.0);\n" |
| 840 | " vertices[1] = vec2( 1.0, -1.0);\n" |
| 841 | " vertices[2] = vec2( 0.0, 1.0);\n" |
| 842 | " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 843 | "}\n"; |
| 844 | |
| 845 | static const char *fragShaderText = |
| 846 | "#version 130\n" |
| 847 | "void main() {\n" |
| 848 | " gl_FragColor = vec4(0,1,0,1);\n" |
| 849 | "}\n"; |
| 850 | |
| 851 | TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver."); |
| 852 | |
| 853 | XglTestFramework::m_use_bil = true; |
| 854 | DrawTriangleTest(vertShaderText, fragShaderText); |
| 855 | XglTestFramework::m_use_bil = saved_use_bil; |
| 856 | } |
| 857 | |
| 858 | TEST_F(XglRenderTest, TriangleFragUniform) |
| 859 | { |
| 860 | |
| 861 | static const char *vertShaderText = |
| 862 | "#version 130\n" |
| 863 | "out vec4 color;\n" |
| 864 | "out vec4 scale;\n" |
| 865 | "vec2 vertices[3];\n" |
| 866 | "void main() {\n" |
| 867 | "vec2 vertices[3];\n" |
| 868 | " vertices[0] = vec2(-0.5, -0.5);\n" |
| 869 | " vertices[1] = vec2( 0.5, -0.5);\n" |
| 870 | " vertices[2] = vec2( 0.5, 0.5);\n" |
| 871 | "vec4 colors[3];\n" |
| 872 | " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n" |
| 873 | " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n" |
| 874 | " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n" |
| 875 | " color = colors[gl_VertexID % 3];\n" |
| 876 | " scale = vec4(1.0, 1.0, 1.0, 1.0);\n" |
| 877 | " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 878 | "}\n"; |
| 879 | |
| 880 | static const char *fragShaderText = |
| 881 | "#version 130\n" |
| 882 | "in vec4 color;\n" |
| 883 | "in vec4 scale;\n" |
| 884 | "uniform vec4 foo;\n" |
| 885 | "void main() {\n" |
| 886 | " gl_FragColor = color * scale + foo;\n" |
| 887 | "}\n"; |
| 888 | |
| 889 | DrawTriangleTest(vertShaderText, fragShaderText); |
| 890 | } |
| 891 | |
| 892 | TEST_F(XglRenderTest, YellowTriangle) |
| 893 | { |
| 894 | static const char *vertShaderText = |
| 895 | "#version 130\n" |
| 896 | "void main() {\n" |
| 897 | " vec2 vertices[3];" |
| 898 | " vertices[0] = vec2(-0.5, -0.5);\n" |
| 899 | " vertices[1] = vec2( 0.5, -0.5);\n" |
| 900 | " vertices[2] = vec2( 0.5, 0.5);\n" |
| 901 | " vec4 colors[3];\n" |
| 902 | " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n" |
| 903 | " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n" |
| 904 | " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n" |
| 905 | " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 906 | "}\n"; |
| 907 | |
| 908 | static const char *fragShaderText = |
| 909 | "#version 130\n" |
| 910 | "void main() {\n" |
| 911 | " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n" |
| 912 | "}\n"; |
| 913 | |
| 914 | DrawTriangleTest(vertShaderText, fragShaderText); |
| 915 | } |
| 916 | |
| 917 | TEST_F(XglRenderTest, RotatedTriangle) { |
| 918 | DrawRotatedTriangleTest(); |
| 919 | } |
| 920 | |
| 921 | TEST_F(XglRenderTest, TriangleTwoFSUniforms) |
| 922 | { |
| 923 | static const char *vertShaderText = |
| 924 | "#version 130\n" |
| 925 | "out vec4 color;\n" |
| 926 | "out vec4 scale;\n" |
| 927 | "out vec2 samplePos;\n" |
| 928 | "void main() {\n" |
| 929 | " vec2 vertices[3];" |
| 930 | " vertices[0] = vec2(-0.5, -0.5);\n" |
| 931 | " vertices[1] = vec2( 0.5, -0.5);\n" |
| 932 | " vertices[2] = vec2( 0.5, 0.5);\n" |
| 933 | " vec4 colors[3];\n" |
| 934 | " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n" |
| 935 | " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n" |
| 936 | " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n" |
| 937 | " color = colors[gl_VertexID % 3];\n" |
| 938 | " vec2 positions[3];" |
| 939 | " positions[0] = vec2( 0.0, 0.0);\n" |
| 940 | " positions[1] = vec2( 1.0, 0.0);\n" |
| 941 | " positions[2] = vec2( 1.0, 1.0);\n" |
| 942 | " scale = vec4(0.0, 0.0, 0.0, 0.0);\n" |
| 943 | " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 944 | "}\n"; |
| 945 | |
| 946 | |
| 947 | static const char *fragShaderText = |
| 948 | "#version 430\n" |
| 949 | "in vec4 color;\n" |
| 950 | "in vec4 scale;\n" |
| 951 | "uniform vec4 foo;\n" |
| 952 | "uniform vec4 bar;\n" |
| 953 | "void main() {\n" |
| 954 | // by default, with no location or blocks |
| 955 | // the compiler will read them from buffer |
| 956 | // in reverse order of first use in shader |
| 957 | // The buffer contains red, followed by blue, |
| 958 | // so foo should be blue, bar should be red |
| 959 | " gl_FragColor = color * scale * foo * bar + foo;\n" |
| 960 | "}\n"; |
| 961 | |
| 962 | ASSERT_NO_FATAL_FAILURE(InitState()); |
| 963 | ASSERT_NO_FATAL_FAILURE(InitViewport()); |
| 964 | |
| 965 | XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX ); |
| 966 | XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT); |
| 967 | |
| 968 | const int constantCount = 8; |
| 969 | const float constants[constantCount] = { 1.0, 0.0, 0.0, 1.0, |
| 970 | 0.0, 0.0, 1.0, 1.0 }; |
| 971 | XglConstantBufferObj constantBuffer(m_device,constantCount, sizeof(constants[0]), (const void*) constants); |
| 972 | ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &constantBuffer.m_constantBufferView); |
| 973 | |
| 974 | XglPipelineObj pipelineobj(m_device); |
| 975 | pipelineobj.AddShader(&vs); |
| 976 | pipelineobj.AddShader(&ps); |
| 977 | |
| 978 | XglDescriptorSetObj descriptorSet(m_device); |
| 979 | descriptorSet.AttachMemoryView(&constantBuffer.m_constantBufferView); |
| 980 | |
| 981 | GenericDrawTriangleTest(pipelineobj, descriptorSet, 1); |
| 982 | QueueCommandBuffer(NULL, 0); |
| 983 | |
| 984 | } |
| 985 | |
| 986 | TEST_F(XglRenderTest, TriangleWithVertexFetch) |
| 987 | { |
| 988 | static const char *vertShaderText = |
| 989 | "#version 130\n" |
| 990 | //XYZ1( -1, -1, -1 ) |
| 991 | "in vec4 pos;\n" |
| 992 | //XYZ1( 0.f, 0.f, 0.f ) |
| 993 | "in vec4 inColor;\n" |
| 994 | "out vec4 outColor;\n" |
| 995 | "void main() {\n" |
| 996 | " outColor = inColor;\n" |
| 997 | " gl_Position = pos;\n" |
| 998 | "}\n"; |
| 999 | |
| 1000 | |
| 1001 | static const char *fragShaderText = |
| 1002 | "#version 430\n" |
| 1003 | "in vec4 color;\n" |
| 1004 | "void main() {\n" |
| 1005 | " gl_FragColor = color;\n" |
| 1006 | "}\n"; |
| 1007 | |
| 1008 | |
| 1009 | ASSERT_NO_FATAL_FAILURE(InitState()); |
| 1010 | ASSERT_NO_FATAL_FAILURE(InitViewport()); |
| 1011 | |
| 1012 | XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData); |
| 1013 | // meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY); |
| 1014 | |
| 1015 | XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX ); |
| 1016 | XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT); |
| 1017 | // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &constantBuffer.m_constantBufferView); |
| 1018 | |
| 1019 | XglPipelineObj pipelineobj(m_device); |
| 1020 | pipelineobj.AddShader(&vs); |
| 1021 | pipelineobj.AddShader(&ps); |
| 1022 | |
| 1023 | XglDescriptorSetObj descriptorSet(m_device); |
| 1024 | descriptorSet.AttachMemoryView(&meshBuffer.m_constantBufferView); |
| 1025 | |
| 1026 | XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = { |
| 1027 | sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement) |
| 1028 | XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented |
| 1029 | }; |
| 1030 | |
| 1031 | XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2]; |
| 1032 | vi_attribs[0].binding = 0; // index into vertexBindingDescriptions |
| 1033 | vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data |
| 1034 | vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 1035 | vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex |
| 1036 | vi_attribs[1].binding = 0; // index into vertexBindingDescriptions |
| 1037 | vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data |
| 1038 | vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 1039 | vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex |
| 1040 | |
| 1041 | pipelineobj.AddVertexInputAttribs(vi_attribs,2); |
| 1042 | pipelineobj.AddVertexInputBindings(&vi_binding,1); |
| 1043 | pipelineobj.AddVertexDataBuffer(&meshBuffer); |
| 1044 | |
| 1045 | GenericDrawTriangleTest(pipelineobj, descriptorSet, 12); |
| 1046 | QueueCommandBuffer(NULL, 0); |
| 1047 | } |
| 1048 | |
| 1049 | TEST_F(XglRenderTest, TriangleVSUniform) |
| 1050 | { |
| 1051 | static const char *vertShaderText = |
| 1052 | "#version 140\n" |
| 1053 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1054 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1055 | "\n" |
| 1056 | "layout(binding = 0) uniform buf {\n" |
| 1057 | " mat4 MVP;\n" |
| 1058 | "} ubuf;\n" |
| 1059 | "void main() {\n" |
| 1060 | " vec2 vertices[3];" |
| 1061 | " vertices[0] = vec2(-0.5, -0.5);\n" |
| 1062 | " vertices[1] = vec2( 0.5, -0.5);\n" |
| 1063 | " vertices[2] = vec2( 0.5, 0.5);\n" |
| 1064 | " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 1065 | "}\n"; |
| 1066 | |
| 1067 | static const char *fragShaderText = |
| 1068 | "#version 130\n" |
| 1069 | "void main() {\n" |
| 1070 | " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" |
| 1071 | "}\n"; |
| 1072 | |
| 1073 | ASSERT_NO_FATAL_FAILURE(InitState()); |
| 1074 | ASSERT_NO_FATAL_FAILURE(InitViewport()); |
| 1075 | |
| 1076 | // Create identity matrix |
| 1077 | glm::mat4 Projection = glm::mat4(1.0f); |
| 1078 | glm::mat4 View = glm::mat4(1.0f); |
| 1079 | glm::mat4 Model = glm::mat4(1.0f); |
| 1080 | glm::mat4 MVP = Projection * View * Model; |
| 1081 | const int matrixSize = sizeof(MVP) / sizeof(MVP[0]); |
| 1082 | |
| 1083 | XglConstantBufferObj constantBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]); |
| 1084 | XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX ); |
| 1085 | XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT); |
| 1086 | |
| 1087 | vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &constantBuffer.m_constantBufferView); |
| 1088 | |
| 1089 | XglPipelineObj pipelineobj(m_device); |
| 1090 | pipelineobj.AddShader(&vs); |
| 1091 | pipelineobj.AddShader(&ps); |
| 1092 | |
| 1093 | // Create descriptor set and attach the constant buffer to it |
| 1094 | XglDescriptorSetObj descriptorSet(m_device); XGL_RESULT err = XGL_SUCCESS; |
| 1095 | descriptorSet.AttachMemoryView(&constantBuffer.m_constantBufferView); |
| 1096 | |
| 1097 | m_memoryRefManager.AddMemoryRef(&constantBuffer.m_constantBufferMem); |
| 1098 | |
| 1099 | GenericDrawTriangleTest(pipelineobj, descriptorSet, 1); |
| 1100 | QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs()); |
| 1101 | |
| 1102 | RotateTriangleVSUniform(Projection, View, Model, constantBuffer); |
| 1103 | } |
| 1104 | |
| 1105 | TEST_F(XglRenderTest, TriangleWithVertexFetchAndMVP) |
| 1106 | { |
| 1107 | static const char *vertShaderText = |
| 1108 | "#version 140\n" |
| 1109 | "layout (std140) uniform bufferVals {\n" |
| 1110 | " mat4 mvp;\n" |
| 1111 | "} myBufferVals;\n" |
| 1112 | "in vec4 pos;\n" |
| 1113 | "in vec4 inColor;\n" |
| 1114 | "out vec4 outColor;\n" |
| 1115 | "void main() {\n" |
| 1116 | " outColor = inColor;\n" |
| 1117 | " gl_Position = myBufferVals.mvp * pos;\n" |
| 1118 | "}\n"; |
| 1119 | |
| 1120 | static const char *fragShaderText = |
| 1121 | "#version 130\n" |
| 1122 | "in vec4 color;\n" |
| 1123 | "void main() {\n" |
| 1124 | " gl_FragColor = color;\n" |
| 1125 | "}\n"; |
| 1126 | glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f); |
| 1127 | |
| 1128 | glm::mat4 View = glm::lookAt( |
| 1129 | glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space |
| 1130 | glm::vec3(0,0,0), // and looks at the origin |
| 1131 | glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down) |
| 1132 | ); |
| 1133 | |
| 1134 | glm::mat4 Model = glm::mat4(1.0f); |
| 1135 | |
| 1136 | glm::mat4 MVP = Projection * View * Model; |
| 1137 | |
| 1138 | ASSERT_NO_FATAL_FAILURE(InitState()); |
| 1139 | ASSERT_NO_FATAL_FAILURE(InitViewport()); |
| 1140 | ASSERT_NO_FATAL_FAILURE(InitDepthStencil()); |
| 1141 | |
| 1142 | XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]), |
| 1143 | sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data); |
| 1144 | |
| 1145 | // What part of thisv do I still need to do? See 144b1763e9b300f62b45077d21dd20ca3a2f838b |
| 1146 | // meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY); |
| 1147 | |
| 1148 | |
| 1149 | const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT); |
| 1150 | |
| 1151 | XglConstantBufferObj constantBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]); |
| 1152 | XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX ); |
| 1153 | XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT); |
| 1154 | |
| 1155 | // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView); |
| 1156 | vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &constantBuffer.m_constantBufferView); |
| 1157 | |
| 1158 | XglPipelineObj pipelineobj(m_device); |
| 1159 | pipelineobj.AddShader(&vs); |
| 1160 | pipelineobj.AddShader(&ps); |
| 1161 | |
| 1162 | XglDescriptorSetObj descriptorSet(m_device); |
| 1163 | descriptorSet.AttachMemoryView(&constantBuffer.m_constantBufferView); |
| 1164 | descriptorSet.AttachMemoryView(&meshBuffer.m_constantBufferView); |
| 1165 | |
| 1166 | m_memoryRefManager.AddMemoryRef(&meshBuffer.m_constantBufferMem); |
| 1167 | m_memoryRefManager.AddMemoryRef(&constantBuffer.m_constantBufferMem); |
| 1168 | |
| 1169 | XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = { |
| 1170 | sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement) |
| 1171 | XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented |
| 1172 | }; |
| 1173 | |
| 1174 | // this is the current description of g_vbData |
| 1175 | XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2]; |
| 1176 | vi_attribs[0].binding = 0; // index into vertexBindingDescriptions |
| 1177 | vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data |
| 1178 | vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 1179 | vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex |
| 1180 | vi_attribs[1].binding = 0; // index into vertexBindingDescriptions |
| 1181 | vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data |
| 1182 | vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 1183 | vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex |
| 1184 | |
| 1185 | pipelineobj.AddVertexInputBindings(&vi_binding,1); |
| 1186 | pipelineobj.AddVertexInputAttribs(vi_attribs,2); |
| 1187 | |
| 1188 | ClearDepthStencil(1.0f); |
| 1189 | GenericDrawTriangleTest(pipelineobj, descriptorSet, 12); |
| 1190 | |
| 1191 | QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs()); |
| 1192 | |
| 1193 | } |
| 1194 | |
| 1195 | TEST_F(XglRenderTest, VSTexture) |
| 1196 | { |
| 1197 | // The expected result from this test is a green and red triangle; |
| 1198 | // one red vertex on the left, two green vertices on the right. |
| 1199 | static const char *vertShaderText = |
| 1200 | "#version 130\n" |
| 1201 | "out vec4 texColor;\n" |
| 1202 | "uniform sampler2D surface;\n" |
| 1203 | "void main() {\n" |
| 1204 | " vec2 vertices[3];" |
| 1205 | " vertices[0] = vec2(-0.5, -0.5);\n" |
| 1206 | " vertices[1] = vec2( 0.5, -0.5);\n" |
| 1207 | " vertices[2] = vec2( 0.5, 0.5);\n" |
| 1208 | " vec2 positions[3];" |
| 1209 | " positions[0] = vec2( 0.0, 0.0);\n" |
| 1210 | " positions[1] = vec2( 0.25, 0.1);\n" |
| 1211 | " positions[2] = vec2( 0.1, 0.25);\n" |
| 1212 | " vec2 samplePos = positions[gl_VertexID % 3];\n" |
| 1213 | " texColor = textureLod(surface, samplePos, 0.0);\n" |
| 1214 | " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 1215 | "}\n"; |
| 1216 | |
| 1217 | static const char *fragShaderText = |
| 1218 | "#version 130\n" |
| 1219 | "in vec4 texColor;\n" |
| 1220 | "void main() {\n" |
| 1221 | " gl_FragColor = texColor;\n" |
| 1222 | "}\n"; |
| 1223 | |
| 1224 | ASSERT_NO_FATAL_FAILURE(InitState()); |
| 1225 | ASSERT_NO_FATAL_FAILURE(InitViewport()); |
| 1226 | |
| 1227 | XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX ); |
| 1228 | XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT); |
| 1229 | XglSamplerObj sampler(m_device); |
| 1230 | XglTextureObj texture(m_device); |
| 1231 | |
| 1232 | vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &texture.m_textureViewInfo); |
| 1233 | vs.BindShaderEntitySlotToSampler(0, (XGL_OBJECT) &sampler.m_sampler); |
| 1234 | |
| 1235 | XglPipelineObj pipelineobj(m_device); |
| 1236 | pipelineobj.AddShader(&vs); |
| 1237 | pipelineobj.AddShader(&ps); |
| 1238 | |
| 1239 | XglDescriptorSetObj descriptorSet(m_device); |
| 1240 | descriptorSet.AttachImageView(&texture.m_textureViewInfo); |
| 1241 | descriptorSet.AttachSampler(&sampler.m_sampler); |
| 1242 | |
| 1243 | m_memoryRefManager.AddMemoryRef(&texture.m_textureMem); |
| 1244 | |
| 1245 | GenericDrawTriangleTest(pipelineobj, descriptorSet, 1); |
| 1246 | QueueCommandBuffer(NULL, 0); |
| 1247 | |
| 1248 | } |
| 1249 | |
| 1250 | TEST_F(XglRenderTest, TriangleWithVertexFetchAndMVPAndTexture) |
| 1251 | { |
| 1252 | static const char *vertShaderText = |
| 1253 | "#version 140\n" |
| 1254 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1255 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1256 | "layout (std140, binding=0) uniform bufferVals {\n" |
| 1257 | " mat4 mvp;\n" |
| 1258 | "} myBufferVals;\n" |
| 1259 | "layout (location=0) in vec4 pos;\n" |
| 1260 | "layout (location=1) in vec4 inColor;\n" |
| 1261 | "layout (location=0) out vec2 UVout;\n" |
| 1262 | "void main() {\n" |
| 1263 | " vec2 positions[3];" |
| 1264 | " positions[0] = vec2( 0.0, 0.0);\n" |
| 1265 | " positions[1] = vec2( 0.25, 0.1);\n" |
| 1266 | " positions[2] = vec2( 0.1, 0.25);\n" |
| 1267 | " UVout = positions[gl_VertexID % 3];\n" |
| 1268 | " gl_Position = myBufferVals.mvp * pos;\n" |
| 1269 | "}\n"; |
| 1270 | |
| 1271 | static const char *fragShaderText = |
| 1272 | "#version 140\n" |
| 1273 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1274 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1275 | "layout (binding=0) uniform sampler2D surface;\n" |
| 1276 | "layout (location=0) out vec4 outColor;\n" |
| 1277 | "layout (location=0) in vec2 UVin;\n" |
| 1278 | "void main() {\n" |
| 1279 | " outColor= textureLod(surface, UVin, 0.0);\n" |
| 1280 | " outColor= vec4(1.0,0.0,0.0,1.0);\n" |
| 1281 | "}\n"; |
| 1282 | glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f); |
| 1283 | |
| 1284 | glm::mat4 View = glm::lookAt( |
| 1285 | glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space |
| 1286 | glm::vec3(0,0,0), // and looks at the origin |
| 1287 | glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down) |
| 1288 | ); |
| 1289 | |
| 1290 | glm::mat4 Model = glm::mat4(1.0f); |
| 1291 | |
| 1292 | glm::mat4 MVP = Projection * View * Model; |
| 1293 | |
| 1294 | |
| 1295 | ASSERT_NO_FATAL_FAILURE(InitState()); |
| 1296 | ASSERT_NO_FATAL_FAILURE(InitViewport()); |
| 1297 | ASSERT_NO_FATAL_FAILURE(InitDepthStencil()); |
| 1298 | |
| 1299 | XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]), |
| 1300 | sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data); |
| 1301 | meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY); |
| 1302 | |
| 1303 | |
| 1304 | const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT); |
| 1305 | |
| 1306 | XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]); |
| 1307 | XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX ); |
| 1308 | XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT); |
| 1309 | XglSamplerObj sampler(m_device); |
| 1310 | XglTextureObj texture(m_device); |
| 1311 | |
| 1312 | // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView); |
| 1313 | vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &mvpBuffer.m_constantBufferView); |
| 1314 | ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &texture.m_textureViewInfo); |
| 1315 | ps.BindShaderEntitySlotToSampler(0, (XGL_OBJECT) &sampler.m_sampler); |
| 1316 | |
| 1317 | XglPipelineObj pipelineobj(m_device); |
| 1318 | pipelineobj.AddShader(&vs); |
| 1319 | pipelineobj.AddShader(&ps); |
| 1320 | |
| 1321 | XglDescriptorSetObj descriptorSet(m_device); |
| 1322 | descriptorSet.AttachMemoryView(&mvpBuffer.m_constantBufferView); |
| 1323 | descriptorSet.AttachMemoryView(&meshBuffer.m_constantBufferView); |
| 1324 | descriptorSet.AttachImageView(&texture.m_textureViewInfo); |
| 1325 | descriptorSet.AttachSampler(&sampler.m_sampler); |
| 1326 | |
| 1327 | m_memoryRefManager.AddMemoryRef(&meshBuffer.m_constantBufferMem); |
| 1328 | m_memoryRefManager.AddMemoryRef(&mvpBuffer.m_constantBufferMem); |
| 1329 | m_memoryRefManager.AddMemoryRef(&texture.m_textureMem); |
| 1330 | |
| 1331 | XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = { |
| 1332 | sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement) |
| 1333 | XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented |
| 1334 | }; |
| 1335 | |
| 1336 | // this is the current description of g_vbData |
| 1337 | XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2]; |
| 1338 | vi_attribs[0].binding = 0; // index into vertexBindingDescriptions |
| 1339 | vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data |
| 1340 | vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 1341 | vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex |
| 1342 | vi_attribs[1].binding = 0; // index into vertexBindingDescriptions |
| 1343 | vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data |
| 1344 | vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 1345 | vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex |
| 1346 | |
| 1347 | pipelineobj.AddVertexInputBindings(&vi_binding,1); |
| 1348 | pipelineobj.AddVertexInputAttribs(vi_attribs,2); |
| 1349 | |
| 1350 | ClearDepthStencil(1.0f); |
| 1351 | GenericDrawTriangleTest(pipelineobj, descriptorSet, 12); |
| 1352 | |
| 1353 | QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs()); |
| 1354 | |
| 1355 | } |
| 1356 | int main(int argc, char **argv) { |
| 1357 | int result; |
| 1358 | |
| 1359 | ::testing::InitGoogleTest(&argc, argv); |
| 1360 | XglTestFramework::InitArgs(&argc, argv); |
| 1361 | |
| 1362 | ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment); |
| 1363 | |
| 1364 | result = RUN_ALL_TESTS(); |
| 1365 | |
| 1366 | XglTestFramework::Finish(); |
| 1367 | return result; |
| 1368 | } |