Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -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 | |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 53 | // Basic rendering tests |
| 54 | |
| 55 | #include <stdlib.h> |
| 56 | #include <stdio.h> |
| 57 | #include <stdbool.h> |
| 58 | #include <string.h> |
Courtney Goeltzenleuchter | da91b95 | 2014-08-21 17:34:22 -0600 | [diff] [blame] | 59 | #include <iostream> |
| 60 | #include <fstream> |
| 61 | using namespace std; |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 62 | |
| 63 | #include <xgl.h> |
Courtney Goeltzenleuchter | 42c144a | 2014-10-03 18:05:10 -0600 | [diff] [blame] | 64 | #include <xglIntelExt.h> |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 65 | #include "gtest-1.7.0/include/gtest/gtest.h" |
| 66 | |
| 67 | #include "xgldevice.h" |
Courtney Goeltzenleuchter | efd4e0a | 2014-09-01 16:37:18 -0600 | [diff] [blame] | 68 | #include "xglimage.h" |
Chia-I Wu | fe7c1de | 2014-08-28 11:56:29 +0800 | [diff] [blame] | 69 | #include "icd-bil.h" |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 70 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 71 | #include "xglrenderframework.h" |
Courtney Goeltzenleuchter | f21d32d | 2014-09-01 13:57:15 -0600 | [diff] [blame] | 72 | |
Courtney Goeltzenleuchter | f1b5b08 | 2014-10-09 11:05:19 -0600 | [diff] [blame] | 73 | #undef ASSERT_NO_FATAL_FAILURE |
| 74 | #define ASSERT_NO_FATAL_FAILURE(x) x |
| 75 | |
Courtney Goeltzenleuchter | 0246188 | 2014-08-22 16:27:58 -0600 | [diff] [blame] | 76 | //-------------------------------------------------------------------------------------- |
| 77 | // Mesh and VertexFormat Data |
| 78 | //-------------------------------------------------------------------------------------- |
| 79 | struct Vertex |
| 80 | { |
| 81 | XGL_FLOAT posX, posY, posZ, posW; // Position data |
| 82 | XGL_FLOAT r, g, b, a; // Color |
| 83 | }; |
| 84 | |
| 85 | #define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f |
| 86 | |
| 87 | static const Vertex g_vbData[] = |
| 88 | { |
| 89 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, |
| 90 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 91 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 92 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 93 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 94 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 95 | |
| 96 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 97 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 98 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 99 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 100 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 101 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) }, |
| 102 | |
| 103 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) }, |
| 104 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 105 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 106 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 107 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 108 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 109 | |
| 110 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 111 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 112 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 113 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 114 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 115 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, |
| 116 | |
| 117 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) }, |
| 118 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 119 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 120 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 121 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 122 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 123 | |
| 124 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 125 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 126 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 127 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 128 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 129 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, |
| 130 | }; |
| 131 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 132 | class XglRenderTest : public XglRenderFramework |
Courtney Goeltzenleuchter | 54119a3 | 2014-09-04 16:26:02 -0600 | [diff] [blame] | 133 | { |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 134 | public: |
Courtney Goeltzenleuchter | 0246188 | 2014-08-22 16:27:58 -0600 | [diff] [blame] | 135 | void InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, const void* vertices ); |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 136 | void InitTexture(); |
| 137 | void InitSampler(); |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 138 | void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText); |
Tobin Ehlis | d806c8e | 2014-10-07 14:41:29 -0600 | [diff] [blame] | 139 | void DrawRotatedTriangleTest(); |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 140 | void NewGenerateClearAndPrepareBufferCmds(XglImage *renderTarget); |
| 141 | void NewGenerateBindStateAndPipelineCmds(XGL_PIPELINE* pipeline); |
| 142 | |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 143 | |
| 144 | protected: |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 145 | XGL_IMAGE m_texture; |
| 146 | XGL_IMAGE_VIEW m_textureView; |
| 147 | XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo; |
| 148 | XGL_GPU_MEMORY m_textureMem; |
| 149 | |
| 150 | XGL_SAMPLER m_sampler; |
| 151 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 152 | // XGL_APPLICATION_INFO app_info; |
| 153 | // XGL_PHYSICAL_GPU objs[MAX_GPUS]; |
| 154 | // XGL_UINT gpu_count; |
| 155 | // XGL_GPU_MEMORY m_descriptor_set_mem; |
| 156 | // XGL_GPU_MEMORY m_pipe_mem; |
| 157 | // XglDevice *m_device; |
| 158 | // XGL_CMD_BUFFER m_cmdBuffer; |
| 159 | // XGL_UINT32 m_numVertices; |
| 160 | // XGL_MEMORY_VIEW_ATTACH_INFO m_vtxBufferView; |
| 161 | // XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView; |
| 162 | // XGL_GPU_MEMORY m_vtxBufferMem; |
| 163 | // XGL_GPU_MEMORY m_constantBufferMem; |
| 164 | // XGL_UINT32 m_numMemRefs; |
| 165 | // XGL_MEMORY_REF m_memRefs[5]; |
| 166 | // XGL_RASTER_STATE_OBJECT m_stateRaster; |
| 167 | // XGL_COLOR_BLEND_STATE_OBJECT m_colorBlend; |
| 168 | // XGL_VIEWPORT_STATE_OBJECT m_stateViewport; |
| 169 | // XGL_DEPTH_STENCIL_STATE_OBJECT m_stateDepthStencil; |
| 170 | // XGL_MSAA_STATE_OBJECT m_stateMsaa; |
| 171 | // XGL_DESCRIPTOR_SET m_rsrcDescSet; |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 172 | |
| 173 | virtual void SetUp() { |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 174 | |
| 175 | this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO; |
| 176 | this->app_info.pNext = NULL; |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 177 | this->app_info.pAppName = (const XGL_CHAR *) "render_tests"; |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 178 | this->app_info.appVersion = 1; |
| 179 | this->app_info.pEngineName = (const XGL_CHAR *) "unittest"; |
| 180 | this->app_info.engineVersion = 1; |
| 181 | this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0); |
| 182 | |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 183 | memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo)); |
| 184 | m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO; |
| 185 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 186 | InitFramework(); |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | virtual void TearDown() { |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 190 | // Clean up resources before we reset |
| 191 | ShutdownFramework(); |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 192 | } |
| 193 | }; |
| 194 | |
Courtney Goeltzenleuchter | 0246188 | 2014-08-22 16:27:58 -0600 | [diff] [blame] | 195 | // this function will create the vertex buffer and fill it with the mesh data |
| 196 | void XglRenderTest::InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, |
| 197 | const void* vertices ) |
| 198 | { |
| 199 | XGL_RESULT err = XGL_SUCCESS; |
| 200 | |
| 201 | assert( numVertices * vbStride > 0 ); |
| 202 | m_numVertices = numVertices; |
| 203 | |
| 204 | XGL_MEMORY_ALLOC_INFO alloc_info = {}; |
| 205 | XGL_UINT8 *pData; |
| 206 | |
| 207 | alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 208 | alloc_info.allocationSize = numVertices * vbStride; |
| 209 | alloc_info.alignment = 0; |
| 210 | alloc_info.heapCount = 1; |
| 211 | alloc_info.heaps[0] = 0; // TODO: Use known existing heap |
| 212 | |
| 213 | alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT; |
| 214 | alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 215 | |
| 216 | err = xglAllocMemory(device(), &alloc_info, &m_vtxBufferMem); |
| 217 | ASSERT_XGL_SUCCESS(err); |
| 218 | |
| 219 | err = xglMapMemory(m_vtxBufferMem, 0, (XGL_VOID **) &pData); |
| 220 | ASSERT_XGL_SUCCESS(err); |
| 221 | |
| 222 | memcpy(pData, vertices, alloc_info.allocationSize); |
| 223 | |
| 224 | err = xglUnmapMemory(m_vtxBufferMem); |
| 225 | ASSERT_XGL_SUCCESS(err); |
| 226 | |
| 227 | // set up the memory view for the vertex buffer |
| 228 | this->m_vtxBufferView.stride = vbStride; |
| 229 | this->m_vtxBufferView.range = numVertices * vbStride; |
| 230 | this->m_vtxBufferView.offset = 0; |
| 231 | this->m_vtxBufferView.mem = m_vtxBufferMem; |
| 232 | this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED; |
| 233 | this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED; |
| 234 | |
| 235 | // open the command buffer |
| 236 | err = xglBeginCommandBuffer( m_cmdBuffer, 0 ); |
| 237 | ASSERT_XGL_SUCCESS(err); |
| 238 | |
| 239 | XGL_MEMORY_STATE_TRANSITION transition = {}; |
| 240 | transition.mem = m_vtxBufferMem; |
| 241 | transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER; |
| 242 | transition.newState = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY; |
| 243 | transition.offset = 0; |
| 244 | transition.regionSize = numVertices * vbStride; |
| 245 | |
| 246 | // write transition to the command buffer |
| 247 | xglCmdPrepareMemoryRegions( m_cmdBuffer, 1, &transition ); |
| 248 | this->m_vtxBufferView.state = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY; |
| 249 | |
| 250 | // finish recording the command buffer |
| 251 | err = xglEndCommandBuffer( m_cmdBuffer ); |
| 252 | ASSERT_XGL_SUCCESS(err); |
| 253 | |
| 254 | // this command buffer only uses the vertex buffer memory |
| 255 | m_numMemRefs = 1; |
| 256 | m_memRefs[0].flags = 0; |
| 257 | m_memRefs[0].mem = m_vtxBufferMem; |
| 258 | |
| 259 | // submit the command buffer to the universal queue |
| 260 | err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL ); |
| 261 | ASSERT_XGL_SUCCESS(err); |
| 262 | } |
| 263 | |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 264 | void XglRenderTest::InitTexture() |
| 265 | { |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 266 | #define DEMO_TEXTURE_COUNT 1 |
| 267 | |
| 268 | const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM }; |
| 269 | const XGL_INT tex_width = 16; |
| 270 | const XGL_INT tex_height = 16; |
| 271 | const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = { |
| 272 | { 0xffff0000, 0xff00ff00 }, |
| 273 | }; |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 274 | XGL_RESULT err; |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 275 | XGL_UINT i; |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 276 | |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 277 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 278 | const XGL_SAMPLER_CREATE_INFO sampler = { |
| 279 | .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
| 280 | .pNext = NULL, |
| 281 | .magFilter = XGL_TEX_FILTER_NEAREST, |
| 282 | .minFilter = XGL_TEX_FILTER_NEAREST, |
| 283 | .mipMode = XGL_TEX_MIPMAP_BASE, |
| 284 | .addressU = XGL_TEX_ADDRESS_WRAP, |
| 285 | .addressV = XGL_TEX_ADDRESS_WRAP, |
| 286 | .addressW = XGL_TEX_ADDRESS_WRAP, |
| 287 | .mipLodBias = 0.0f, |
| 288 | .maxAnisotropy = 0, |
| 289 | .compareFunc = XGL_COMPARE_NEVER, |
| 290 | .minLod = 0.0f, |
| 291 | .maxLod = 0.0f, |
| 292 | .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE, |
| 293 | }; |
| 294 | const XGL_IMAGE_CREATE_INFO image = { |
| 295 | .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 296 | .pNext = NULL, |
| 297 | .imageType = XGL_IMAGE_2D, |
| 298 | .format = tex_format, |
| 299 | .extent = { tex_width, tex_height, 1 }, |
| 300 | .mipLevels = 1, |
| 301 | .arraySize = 1, |
| 302 | .samples = 1, |
| 303 | .tiling = XGL_LINEAR_TILING, |
| 304 | .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, |
| 305 | .flags = 0, |
| 306 | }; |
| 307 | XGL_MEMORY_ALLOC_INFO mem_alloc; |
| 308 | mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 309 | mem_alloc.pNext = NULL; |
| 310 | mem_alloc.allocationSize = 0; |
| 311 | mem_alloc.alignment = 0; |
| 312 | mem_alloc.flags = 0; |
| 313 | mem_alloc.heapCount = 0; |
| 314 | mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 315 | XGL_IMAGE_VIEW_CREATE_INFO view; |
| 316 | view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 317 | view.pNext = NULL; |
| 318 | view.image = XGL_NULL_HANDLE; |
| 319 | view.viewType = XGL_IMAGE_VIEW_2D; |
| 320 | view.format = image.format; |
| 321 | view.channels.r = XGL_CHANNEL_SWIZZLE_R; |
| 322 | view.channels.g = XGL_CHANNEL_SWIZZLE_G; |
| 323 | view.channels.b = XGL_CHANNEL_SWIZZLE_B; |
| 324 | view.channels.a = XGL_CHANNEL_SWIZZLE_A; |
| 325 | view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 326 | view.subresourceRange.baseMipLevel = 0; |
| 327 | view.subresourceRange.mipLevels = 1; |
| 328 | view.subresourceRange.baseArraySlice = 0; |
| 329 | view.subresourceRange.arraySize = 1; |
| 330 | view.minLod = 0.0f; |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 331 | |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 332 | XGL_MEMORY_REQUIREMENTS mem_reqs; |
| 333 | XGL_SIZE mem_reqs_size; |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 334 | |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 335 | /* create sampler */ |
| 336 | err = xglCreateSampler(device(), &sampler, &m_sampler); |
| 337 | assert(!err); |
| 338 | |
| 339 | /* create image */ |
| 340 | err = xglCreateImage(device(), &image, &m_texture); |
| 341 | assert(!err); |
| 342 | |
| 343 | err = xglGetObjectInfo(m_texture, |
| 344 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 345 | &mem_reqs_size, &mem_reqs); |
| 346 | assert(!err && mem_reqs_size == sizeof(mem_reqs)); |
| 347 | |
| 348 | mem_alloc.allocationSize = mem_reqs.size; |
| 349 | mem_alloc.alignment = mem_reqs.alignment; |
| 350 | mem_alloc.heapCount = mem_reqs.heapCount; |
| 351 | memcpy(mem_alloc.heaps, mem_reqs.heaps, |
| 352 | sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount); |
| 353 | |
| 354 | /* allocate memory */ |
| 355 | err = xglAllocMemory(device(), &mem_alloc, &m_textureMem); |
| 356 | assert(!err); |
| 357 | |
| 358 | /* bind memory */ |
| 359 | err = xglBindObjectMemory(m_texture, m_textureMem, 0); |
| 360 | assert(!err); |
| 361 | |
| 362 | /* create image view */ |
| 363 | view.image = m_texture; |
| 364 | err = xglCreateImageView(device(), &view, &m_textureView); |
| 365 | assert(!err); |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 366 | } |
| 367 | |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 368 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 369 | const XGL_IMAGE_SUBRESOURCE subres = { |
| 370 | .aspect = XGL_IMAGE_ASPECT_COLOR, |
| 371 | .mipLevel = 0, |
| 372 | .arraySlice = 0, |
| 373 | }; |
| 374 | XGL_SUBRESOURCE_LAYOUT layout; |
| 375 | XGL_SIZE layout_size; |
| 376 | XGL_VOID *data; |
| 377 | XGL_INT x, y; |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 378 | |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 379 | err = xglGetImageSubresourceInfo(m_texture, &subres, |
| 380 | XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout); |
| 381 | assert(!err && layout_size == sizeof(layout)); |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 382 | |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 383 | err = xglMapMemory(m_textureMem, 0, &data); |
| 384 | assert(!err); |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 385 | |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 386 | for (y = 0; y < tex_height; y++) { |
| 387 | uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y); |
| 388 | for (x = 0; x < tex_width; x++) |
| 389 | row[x] = tex_colors[i][(x & 1) ^ (y & 1)]; |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 390 | } |
Cody Northrop | 2f1646d | 2014-10-07 16:25:00 -0600 | [diff] [blame] | 391 | |
| 392 | err = xglUnmapMemory(m_textureMem); |
| 393 | assert(!err); |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 394 | } |
| 395 | |
Cody Northrop | 7d2035d | 2014-10-06 15:42:00 -0600 | [diff] [blame] | 396 | m_textureViewInfo.view = m_textureView; |
| 397 | } |
| 398 | |
| 399 | void XglRenderTest::InitSampler() |
| 400 | { |
| 401 | XGL_RESULT err; |
| 402 | |
| 403 | XGL_SAMPLER_CREATE_INFO samplerCreateInfo = {}; |
| 404 | samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; |
| 405 | samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST; |
| 406 | samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST; |
| 407 | samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE; |
| 408 | samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP; |
| 409 | samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP; |
| 410 | samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP; |
| 411 | samplerCreateInfo.mipLodBias = 0.0; |
| 412 | samplerCreateInfo.maxAnisotropy = 0.0; |
| 413 | samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER; |
| 414 | samplerCreateInfo.minLod = 0.0; |
| 415 | samplerCreateInfo.maxLod = 0.0; |
| 416 | samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE; |
| 417 | |
| 418 | err = xglCreateSampler(device(),&samplerCreateInfo, &m_sampler); |
| 419 | ASSERT_XGL_SUCCESS(err); |
| 420 | } |
| 421 | |
Tobin Ehlis | d806c8e | 2014-10-07 14:41:29 -0600 | [diff] [blame] | 422 | void XglRenderTest::DrawRotatedTriangleTest() |
| 423 | { |
| 424 | // TODO : This test will pass a matrix into VS to affect triangle orientation. |
| 425 | } |
| 426 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 427 | void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText) |
Tobin Ehlis | d806c8e | 2014-10-07 14:41:29 -0600 | [diff] [blame] | 428 | { |
| 429 | XGL_PIPELINE pipeline; |
| 430 | XGL_SHADER vs, ps; |
| 431 | XGL_RESULT err; |
Tobin Ehlis | d806c8e | 2014-10-07 14:41:29 -0600 | [diff] [blame] | 432 | |
Courtney Goeltzenleuchter | 53d8d89 | 2014-10-08 12:20:26 -0600 | [diff] [blame] | 433 | ASSERT_NO_FATAL_FAILURE(InitState()); |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 434 | ASSERT_NO_FATAL_FAILURE(InitViewport()); |
| 435 | |
| 436 | ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX, |
| 437 | vertShaderText, &vs)); |
| 438 | |
| 439 | ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT, |
| 440 | fragShaderText, &ps)); |
| 441 | |
| 442 | ASSERT_NO_FATAL_FAILURE(CreateDefaultPipeline(&pipeline, vs, ps)); |
Tobin Ehlis | d806c8e | 2014-10-07 14:41:29 -0600 | [diff] [blame] | 443 | |
| 444 | /* |
| 445 | * Shaders are now part of the pipeline, don't need these anymore |
| 446 | */ |
| 447 | ASSERT_XGL_SUCCESS(xglDestroyObject(ps)); |
| 448 | ASSERT_XGL_SUCCESS(xglDestroyObject(vs)); |
| 449 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 450 | ASSERT_NO_FATAL_FAILURE(InitRenderTarget()); |
Tobin Ehlis | d806c8e | 2014-10-07 14:41:29 -0600 | [diff] [blame] | 451 | |
Cody Northrop | 275d40b | 2014-10-01 14:03:25 -0600 | [diff] [blame] | 452 | const int constantCount = 4; |
| 453 | const float constants[constantCount] = { 0.5, 0.5, 0.5, 1.0 }; |
| 454 | InitConstantBuffer(constantCount, sizeof(constants[0]), (const void*) constants); |
| 455 | |
| 456 | // Create descriptor set for a uniform resource |
| 457 | XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {}; |
| 458 | descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO; |
| 459 | descriptorInfo.slots = 1; |
| 460 | |
| 461 | // create a descriptor set with a single slot |
| 462 | err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet ); |
| 463 | ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed"; |
| 464 | |
| 465 | // bind memory to the descriptor set |
| 466 | err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem); |
| 467 | |
| 468 | // write the constant buffer view to the descriptor set |
| 469 | xglBeginDescriptorSetUpdate( m_rsrcDescSet ); |
| 470 | xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView ); |
| 471 | xglEndDescriptorSetUpdate( m_rsrcDescSet ); |
| 472 | |
Tobin Ehlis | d806c8e | 2014-10-07 14:41:29 -0600 | [diff] [blame] | 473 | // Build command buffer |
| 474 | err = xglBeginCommandBuffer(m_cmdBuffer, 0); |
| 475 | ASSERT_XGL_SUCCESS(err); |
| 476 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 477 | GenerateClearAndPrepareBufferCmds(); |
| 478 | GenerateBindRenderTargetCmd(); |
Tobin Ehlis | d806c8e | 2014-10-07 14:41:29 -0600 | [diff] [blame] | 479 | GenerateBindStateAndPipelineCmds(&pipeline); |
| 480 | |
Courtney Goeltzenleuchter | 0246188 | 2014-08-22 16:27:58 -0600 | [diff] [blame] | 481 | // xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 ); |
| 482 | // xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView ); |
| 483 | |
| 484 | // render the cube |
| 485 | xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 ); |
| 486 | |
| 487 | // prepare the back buffer for present |
| 488 | // XGL_IMAGE_STATE_TRANSITION transitionToPresent = {}; |
| 489 | // transitionToPresent.image = m_image; |
| 490 | // transitionToPresent.oldState = m_image_state; |
| 491 | // transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT; |
| 492 | // transitionToPresent.subresourceRange = srRange; |
| 493 | // xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent ); |
| 494 | // m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState; |
| 495 | |
| 496 | // finalize recording of the command buffer |
| 497 | err = xglEndCommandBuffer( m_cmdBuffer ); |
| 498 | ASSERT_XGL_SUCCESS( err ); |
| 499 | |
| 500 | // this command buffer only uses the vertex buffer memory |
| 501 | m_numMemRefs = 0; |
| 502 | // m_memRefs[0].flags = 0; |
| 503 | // m_memRefs[0].mem = m_vtxBufferMemory; |
| 504 | |
| 505 | // submit the command buffer to the universal queue |
| 506 | err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL ); |
| 507 | ASSERT_XGL_SUCCESS( err ); |
| 508 | |
Chia-I Wu | 113e9d9 | 2014-08-27 14:58:05 +0800 | [diff] [blame] | 509 | err = xglQueueWaitIdle( m_device->m_queue ); |
| 510 | ASSERT_XGL_SUCCESS( err ); |
| 511 | |
Courtney Goeltzenleuchter | 464746c | 2014-08-26 18:16:41 -0600 | [diff] [blame] | 512 | // Wait for work to finish before cleaning up. |
| 513 | xglDeviceWaitIdle(m_device->device()); |
| 514 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 515 | RecordImage(m_renderTarget); |
Chia-I Wu | 57b3fa8 | 2014-08-30 23:58:36 +0800 | [diff] [blame] | 516 | |
Courtney Goeltzenleuchter | 0246188 | 2014-08-22 16:27:58 -0600 | [diff] [blame] | 517 | } |
| 518 | |
Chia-I Wu | 113e9d9 | 2014-08-27 14:58:05 +0800 | [diff] [blame] | 519 | |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 520 | TEST_F(XglRenderTest, TestDrawTriangle1) { |
| 521 | static const char *vertShaderText = |
| 522 | "#version 130\n" |
| 523 | "vec2 vertices[3];\n" |
| 524 | "void main() {\n" |
| 525 | " vertices[0] = vec2(-1.0, -1.0);\n" |
| 526 | " vertices[1] = vec2( 1.0, -1.0);\n" |
| 527 | " vertices[2] = vec2( 0.0, 1.0);\n" |
| 528 | " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 529 | "}\n"; |
| 530 | |
| 531 | static const char *fragShaderText = |
| 532 | "#version 130\n" |
| 533 | "uniform vec4 foo;\n" |
| 534 | "void main() {\n" |
| 535 | " gl_FragColor = foo;\n" |
| 536 | "}\n"; |
| 537 | DrawTriangleTest(vertShaderText, fragShaderText); |
| 538 | } |
| 539 | |
| 540 | TEST_F(XglRenderTest, TestDrawTriangle2) { |
Courtney Goeltzenleuchter | f1b5b08 | 2014-10-09 11:05:19 -0600 | [diff] [blame] | 541 | // static const char *vertShaderText = |
| 542 | // "#version 130\n" |
| 543 | // "void main() {\n" |
| 544 | // " vec2 vertices[3];" |
| 545 | // " vertices[0] = vec2(-0.5, -0.5);\n" |
| 546 | // " vertices[1] = vec2( 0.5, -0.5);\n" |
| 547 | // " vertices[2] = vec2( 0.5, 0.5);\n" |
| 548 | // " vec4 colors[3];\n" |
| 549 | // " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n" |
| 550 | // " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n" |
| 551 | // " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n" |
| 552 | // " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 553 | // "}\n"; |
| 554 | |
| 555 | // static const char *fragShaderText = |
| 556 | // "#version 130\n" |
| 557 | // "void main() {\n" |
| 558 | // " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n" |
| 559 | // "}\n"; |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 560 | static const char *vertShaderText = |
Courtney Goeltzenleuchter | f1b5b08 | 2014-10-09 11:05:19 -0600 | [diff] [blame] | 561 | "#version 130\n" |
| 562 | "vec2 vertices[3];\n" |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 563 | "void main() {\n" |
Courtney Goeltzenleuchter | f1b5b08 | 2014-10-09 11:05:19 -0600 | [diff] [blame] | 564 | " vertices[0] = vec2(-1.0, -1.0);\n" |
| 565 | " vertices[1] = vec2( 1.0, -1.0);\n" |
| 566 | " vertices[2] = vec2( 0.0, 1.0);\n" |
| 567 | " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
Courtney Goeltzenleuchter | 02d33c1 | 2014-10-08 14:26:40 -0600 | [diff] [blame] | 568 | "}\n"; |
| 569 | |
| 570 | static const char *fragShaderText = |
Courtney Goeltzenleuchter | f1b5b08 | 2014-10-09 11:05:19 -0600 | [diff] [blame] | 571 | "#version 130\n" |
| 572 | "uniform vec4 foo;\n" |
| 573 | "void main() {\n" |
| 574 | " gl_FragColor = foo;\n" |
| 575 | "}\n"; |
| 576 | // DrawTriangleTest(vertShaderText, fragShaderText); |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 577 | } |
| 578 | |
Tobin Ehlis | d806c8e | 2014-10-07 14:41:29 -0600 | [diff] [blame] | 579 | TEST_F(XglRenderTest, TestDrawRotatedTriangle) { |
| 580 | DrawRotatedTriangleTest(); |
| 581 | } |
| 582 | |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 583 | int main(int argc, char **argv) { |
Courtney Goeltzenleuchter | 54119a3 | 2014-09-04 16:26:02 -0600 | [diff] [blame] | 584 | int result; |
| 585 | |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 586 | ::testing::InitGoogleTest(&argc, argv); |
Courtney Goeltzenleuchter | 54119a3 | 2014-09-04 16:26:02 -0600 | [diff] [blame] | 587 | XglTestFramework::InitArgs(&argc, argv); |
| 588 | |
Courtney Goeltzenleuchter | a0f74c5 | 2014-10-08 08:46:51 -0600 | [diff] [blame] | 589 | ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment); |
| 590 | |
Courtney Goeltzenleuchter | 54119a3 | 2014-09-04 16:26:02 -0600 | [diff] [blame] | 591 | result = RUN_ALL_TESTS(); |
| 592 | |
| 593 | XglTestFramework::Finish(); |
| 594 | return result; |
Courtney Goeltzenleuchter | cc5eb3a | 2014-08-19 18:35:50 -0600 | [diff] [blame] | 595 | } |