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