Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -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 | |
| 54 | // Base rendering test |
| 55 | // The intend of this test is to have all the necessary steps to render |
| 56 | // a textured triangle in straight-line code to simplify debugging |
| 57 | |
| 58 | #include <stdlib.h> |
| 59 | #include <stdio.h> |
| 60 | #include <stdbool.h> |
| 61 | #include <string.h> |
| 62 | #include <iostream> |
| 63 | #include <fstream> |
| 64 | using namespace std; |
| 65 | |
| 66 | #include <xgl.h> |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 67 | #include "gtest-1.7.0/include/gtest/gtest.h" |
| 68 | |
| 69 | #include "xgldevice.h" |
| 70 | #include "xglimage.h" |
| 71 | #include "icd-bil.h" |
| 72 | |
| 73 | #include "xgltestframework.h" |
| 74 | |
| 75 | //-------------------------------------------------------------------------------------- |
| 76 | // Mesh and VertexFormat Data |
| 77 | //-------------------------------------------------------------------------------------- |
| 78 | struct Vertex |
| 79 | { |
| 80 | XGL_FLOAT posX, posY, posZ, posW; // Position data |
| 81 | XGL_FLOAT r, g, b, a; // Color |
| 82 | }; |
| 83 | |
| 84 | #define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f |
| 85 | |
| 86 | static const Vertex g_vbData[] = |
| 87 | { |
| 88 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, |
| 89 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 90 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 91 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 92 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 93 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 94 | |
| 95 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 96 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 97 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 98 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 99 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 100 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) }, |
| 101 | |
| 102 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) }, |
| 103 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 104 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 105 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 106 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 107 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 108 | |
| 109 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 110 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 111 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 112 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 113 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 114 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, |
| 115 | |
| 116 | { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) }, |
| 117 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 118 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 119 | { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, |
| 120 | { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) }, |
| 121 | { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, |
| 122 | |
| 123 | { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) }, |
| 124 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 125 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 126 | { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) }, |
| 127 | { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, |
| 128 | { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, |
| 129 | }; |
| 130 | |
| 131 | static const uint32_t gen6_fs[] = { |
| 132 | 0x00600001, 0x202003fe, 0x00000000, 0x3f800000, // mov(8) m1<1>F 1F { align1 1Q }; |
| 133 | 0x00600001, 0x204003fe, 0x00000000, 0x00000000, // mov(8) m2<1>F 0F { align1 1Q }; |
| 134 | 0x00600001, 0x206003fe, 0x00000000, 0x00000000, // mov(8) m3<1>F 0F { align1 1Q }; |
| 135 | 0x00600001, 0x208003fe, 0x00000000, 0x3f800000, // mov(8) m4<1>F 1F { align1 1Q }; |
| 136 | 0x05600032, 0x20001fc8, 0x008d0020, 0x88019400, // sendc(8) null m1<8,8,1>F |
| 137 | // render RT write SIMD8 LastRT Surface = 0 mlen 4 rlen 0 { align1 1Q EOT }; |
| 138 | }; |
| 139 | |
| 140 | static const uint32_t gen6_vs[] = { |
| 141 | 0x01600110, 0x200f1ca4, 0x00600020, 0x00000000, // cmp.z.f0(8) null g1<4,4,1>.xD 0D { align16 1Q }; |
| 142 | 0x00670122, 0x000a108f, 0x000e0004, 0x000e0004, // (+f0.all4h) if(8) JIP: 10 { align16 1Q }; |
| 143 | 0x00600501, 0x204303fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.xyF -1F { align16 NoDDClr 1Q }; |
| 144 | 0x00600d01, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDClr,NoDDChk 1Q }; |
| 145 | 0x00600901, 0x204803fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.wF 1F { align16 NoDDChk 1Q }; |
| 146 | 0x00600124, 0x0014108f, 0x006e0004, 0x006e0004, // else(8) JIP: 20 { align16 1Q }; |
| 147 | 0x01600110, 0x200f1ca4, 0x00600020, 0x00000001, // cmp.z.f0(8) null g1<4,4,1>.xD 1D { align16 1Q }; |
| 148 | 0x00670122, 0x000a108f, 0x000e0004, 0x000e0004, // (+f0.all4h) if(8) JIP: 10 { align16 1Q }; |
| 149 | 0x00600501, 0x204903fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.xwF 1F { align16 NoDDClr 1Q }; |
| 150 | 0x00600d01, 0x204203fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.yF -1F { align16 NoDDClr,NoDDChk 1Q }; |
| 151 | 0x00600901, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDChk 1Q }; |
| 152 | 0x00600124, 0x0006108f, 0x006e0004, 0x006e0004, // else(8) JIP: 6 { align16 1Q }; |
| 153 | 0x00600501, 0x204503fd, 0x00000000, 0x00000000, // mov(8) g2<1>.xzF 0F { align16 NoDDClr 1Q }; |
| 154 | 0x00600901, 0x204a03fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.ywF 1F { align16 NoDDChk 1Q }; |
| 155 | 0x00600125, 0x0002108f, 0x006e0004, 0x006e0002, // endif(8) JIP: 2 { align16 1Q }; |
| 156 | 0x00600125, 0x0002108f, 0x006e0004, 0x006e0002, // endif(8) JIP: 2 { align16 1Q }; |
| 157 | 0x00600101, 0x204f0062, 0x00000000, 0x00000000, // mov(8) m2<1>UD 0x00000000UD { align16 1Q }; |
| 158 | 0x00600101, 0x206f03be, 0x006e0044, 0x00000000, // mov(8) m3<1>F g2<4,4,1>F { align16 1Q }; |
| 159 | 0x00600301, 0x202f0022, 0x006e0004, 0x00000000, // mov(8) m1<1>UD g0<4,4,1>UD { align16 WE_all 1Q }; |
| 160 | 0x06600131, 0x200f1fdc, 0x006e0024, 0x8608c400, // send(8) null m1<4,4,1>F |
| 161 | // urb 0 urb_write interleave used complete mlen 3 rlen 0 { align16 1Q EOT }; |
| 162 | }; |
| 163 | |
| 164 | static const uint32_t gen7_fs[] = { |
| 165 | 0x00600001, 0x2e2003fd, 0x00000000, 0x3f800000, // mov(8) g113<1>F 1F { align1 1Q }; |
| 166 | 0x00600001, 0x2e4003fd, 0x00000000, 0x00000000, // mov(8) g114<1>F 0F { align1 1Q }; |
| 167 | 0x00600001, 0x2e6003fd, 0x00000000, 0x00000000, // mov(8) g115<1>F 0F { align1 1Q }; |
| 168 | 0x00600001, 0x2e8003fd, 0x00000000, 0x3f800000, // mov(8) g116<1>F 1F { align1 1Q }; |
| 169 | 0x05600032, 0x20001fa8, 0x008d0e20, 0x88031400, // sendc(8) null g113<8,8,1>F |
| 170 | // render RT write SIMD8 LastRT Surface = 0 mlen 4 rlen 0 { align1 1Q EOT }; |
| 171 | }; |
| 172 | |
| 173 | static const uint32_t gen7_vs[] = { |
| 174 | 0x01608110, 0x200f1ca4, 0x00600020, 0x00000000, // cmp.z.f0(8) null g1<4,4,1>.xD 0D { align16 1Q switch }; |
| 175 | 0x00670122, 0x200f0c84, 0x000e0004, 0x001c000a, // (+f0.all4h) if(8) JIP: 10 UIP: 28 { align16 1Q }; |
| 176 | 0x00600501, 0x204303fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.xyF -1F { align16 NoDDClr 1Q }; |
| 177 | 0x00600d01, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDClr,NoDDChk 1Q }; |
| 178 | 0x00600901, 0x204803fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.wF 1F { align16 NoDDChk 1Q }; |
| 179 | 0x00600124, 0x200f0c84, 0x006e0004, 0x00000014, // else(8) JIP: 20 { align16 1Q }; |
| 180 | 0x01608110, 0x200f1ca4, 0x00600020, 0x00000001, // cmp.z.f0(8) null g1<4,4,1>.xD 1D { align16 1Q switch }; |
| 181 | 0x00670122, 0x200f0c84, 0x000e0004, 0x000e000a, // (+f0.all4h) if(8) JIP: 10 UIP: 14 { align16 1Q }; |
| 182 | 0x00600501, 0x204903fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.xwF 1F { align16 NoDDClr 1Q }; |
| 183 | 0x00600d01, 0x204203fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.yF -1F { align16 NoDDClr,NoDDChk 1Q }; |
| 184 | 0x00600901, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDChk 1Q }; |
| 185 | 0x00600124, 0x200f0c84, 0x006e0004, 0x00000006, // else(8) JIP: 6 { align16 1Q }; |
| 186 | 0x00600501, 0x204503fd, 0x00000000, 0x00000000, // mov(8) g2<1>.xzF 0F { align16 NoDDClr 1Q }; |
| 187 | 0x00600901, 0x204a03fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.ywF 1F { align16 NoDDChk 1Q }; |
| 188 | 0x00600125, 0x200f0c84, 0x006e0004, 0x00000002, // endif(8) JIP: 2 { align16 1Q }; |
| 189 | 0x00600125, 0x200f0c84, 0x006e0004, 0x00000002, // endif(8) JIP: 2 { align16 1Q }; |
| 190 | 0x00600101, 0x2e4f0061, 0x00000000, 0x00000000, // mov(8) g114<1>UD 0x00000000UD { align16 1Q }; |
| 191 | 0x00600101, 0x2e6f03bd, 0x006e0044, 0x00000000, // mov(8) g115<1>F g2<4,4,1>F { align16 1Q }; |
| 192 | 0x00600301, 0x2e2f0021, 0x006e0004, 0x00000000, // mov(8) g113<1>UD g0<4,4,1>UD { align16 WE_all 1Q }; |
| 193 | 0x00000206, 0x2e340c21, 0x00000014, 0x0000ff00, // or(1) g113.5<1>UD g0.5<0,1,0>UD 0x0000ff00UD { align1 WE_all }; |
| 194 | 0x06600131, 0x200f1fbc, 0x006e0e24, 0x8608c000, // send(8) null g113<4,4,1>F |
| 195 | // urb 0 write HWord interleave complete mlen 3 rlen 0 { align16 1Q EOT }; |
| 196 | }; |
| 197 | |
| 198 | class XglRenderTest : public XglTestFramework |
| 199 | { |
| 200 | public: |
| 201 | void CreateQueryPool(XGL_QUERY_TYPE type, XGL_UINT slots, |
| 202 | XGL_QUERY_POOL *pPool, XGL_GPU_MEMORY *pMem); |
| 203 | void DestroyQueryPool(XGL_QUERY_POOL pool, XGL_GPU_MEMORY mem); |
| 204 | |
| 205 | XGL_DEVICE device() {return m_device->device();} |
| 206 | void InitPipeline(); |
| 207 | void InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, const void* vertices ); |
| 208 | void InitConstantBuffer( int constantCount, int constantSize, const void* data ); |
| 209 | void DrawTriangleTest(); |
| 210 | void DrawRotatedTriangleTest(); |
| 211 | |
| 212 | protected: |
| 213 | XGL_APPLICATION_INFO app_info; |
| 214 | XGL_PHYSICAL_GPU objs[MAX_GPUS]; |
| 215 | XGL_UINT gpu_count; |
| 216 | XGL_GPU_MEMORY m_descriptor_set_mem; |
| 217 | XGL_GPU_MEMORY m_pipe_mem; |
| 218 | XglDevice *m_device; |
| 219 | XGL_CMD_BUFFER m_cmdBuffer; |
| 220 | XGL_UINT32 m_numVertices; |
| 221 | XGL_MEMORY_VIEW_ATTACH_INFO m_vtxBufferView; |
| 222 | XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView; |
| 223 | XGL_GPU_MEMORY m_vtxBufferMem; |
| 224 | XGL_GPU_MEMORY m_constantBufferMem; |
| 225 | XGL_UINT32 m_numMemRefs; |
| 226 | XGL_MEMORY_REF m_memRefs[5]; |
| 227 | XGL_RASTER_STATE_OBJECT m_stateRaster; |
| 228 | XGL_COLOR_BLEND_STATE_OBJECT m_colorBlend; |
| 229 | XGL_VIEWPORT_STATE_OBJECT m_stateViewport; |
| 230 | XGL_DEPTH_STENCIL_STATE_OBJECT m_stateDepthStencil; |
| 231 | XGL_MSAA_STATE_OBJECT m_stateMsaa; |
| 232 | XGL_DESCRIPTOR_SET m_rsrcDescSet; |
| 233 | |
| 234 | virtual void SetUp() { |
| 235 | XGL_RESULT err; |
| 236 | |
| 237 | this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO; |
| 238 | this->app_info.pNext = NULL; |
| 239 | this->app_info.pAppName = (const XGL_CHAR *) "base"; |
| 240 | this->app_info.appVersion = 1; |
| 241 | this->app_info.pEngineName = (const XGL_CHAR *) "unittest"; |
| 242 | this->app_info.engineVersion = 1; |
| 243 | this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0); |
| 244 | |
| 245 | memset(&m_vtxBufferView, 0, sizeof(m_vtxBufferView)); |
| 246 | m_vtxBufferView.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO; |
| 247 | |
| 248 | memset(&m_constantBufferView, 0, sizeof(m_constantBufferView)); |
| 249 | m_constantBufferView.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO; |
| 250 | |
| 251 | err = xglInitAndEnumerateGpus(&app_info, NULL, |
| 252 | MAX_GPUS, &this->gpu_count, objs); |
| 253 | ASSERT_XGL_SUCCESS(err); |
| 254 | ASSERT_GE(1, this->gpu_count) << "No GPU available"; |
| 255 | |
| 256 | m_device = new XglDevice(0, objs[0]); |
| 257 | m_device->get_device_queue(); |
| 258 | } |
| 259 | |
| 260 | virtual void TearDown() { |
| 261 | xglInitAndEnumerateGpus(&this->app_info, XGL_NULL_HANDLE, 0, &gpu_count, XGL_NULL_HANDLE); |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | |
| 266 | void XglRenderTest::CreateQueryPool(XGL_QUERY_TYPE type, XGL_UINT slots, |
| 267 | XGL_QUERY_POOL *pPool, XGL_GPU_MEMORY *pMem) |
| 268 | { |
| 269 | XGL_RESULT err; |
| 270 | |
| 271 | XGL_QUERY_POOL_CREATE_INFO poolCreateInfo = {}; |
| 272 | poolCreateInfo.sType = XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; |
| 273 | poolCreateInfo.pNext = NULL; |
| 274 | poolCreateInfo.queryType = type; |
| 275 | poolCreateInfo.slots = slots; |
| 276 | |
| 277 | err = xglCreateQueryPool(device(), &poolCreateInfo, pPool); |
| 278 | ASSERT_XGL_SUCCESS(err); |
| 279 | |
| 280 | XGL_MEMORY_REQUIREMENTS mem_req; |
| 281 | XGL_UINT data_size = sizeof(mem_req); |
| 282 | err = xglGetObjectInfo(*pPool, XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 283 | &data_size, &mem_req); |
| 284 | ASSERT_XGL_SUCCESS(err); |
| 285 | ASSERT_EQ(data_size, sizeof(mem_req)); |
| 286 | |
| 287 | if (!mem_req.size) { |
| 288 | *pMem = XGL_NULL_HANDLE; |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | XGL_MEMORY_ALLOC_INFO mem_info; |
| 293 | |
| 294 | memset(&mem_info, 0, sizeof(mem_info)); |
| 295 | mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 296 | mem_info.allocationSize = mem_req.size; |
| 297 | mem_info.alignment = mem_req.alignment; |
| 298 | mem_info.heapCount = mem_req.heapCount; |
| 299 | memcpy(mem_info.heaps, mem_req.heaps, sizeof(XGL_UINT)*XGL_MAX_MEMORY_HEAPS); |
| 300 | mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 301 | mem_info.flags = XGL_MEMORY_ALLOC_SHAREABLE_BIT; |
| 302 | err = xglAllocMemory(device(), &mem_info, pMem); |
| 303 | ASSERT_XGL_SUCCESS(err); |
| 304 | |
| 305 | err = xglBindObjectMemory(*pPool, *pMem, 0); |
| 306 | ASSERT_XGL_SUCCESS(err); |
| 307 | } |
| 308 | |
| 309 | void XglRenderTest::DestroyQueryPool(XGL_QUERY_POOL pool, XGL_GPU_MEMORY mem) |
| 310 | { |
| 311 | ASSERT_XGL_SUCCESS(xglBindObjectMemory(pool, XGL_NULL_HANDLE, 0)); |
| 312 | ASSERT_XGL_SUCCESS(xglFreeMemory(mem)); |
| 313 | ASSERT_XGL_SUCCESS(xglDestroyObject(pool)); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | // this function will create the vertex buffer and fill it with the mesh data |
| 318 | void XglRenderTest::InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, |
| 319 | const void* vertices ) |
| 320 | { |
| 321 | XGL_RESULT err = XGL_SUCCESS; |
| 322 | |
| 323 | assert( numVertices * vbStride > 0 ); |
| 324 | m_numVertices = numVertices; |
| 325 | |
| 326 | XGL_MEMORY_ALLOC_INFO alloc_info = {}; |
| 327 | XGL_UINT8 *pData; |
| 328 | |
| 329 | alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 330 | alloc_info.allocationSize = numVertices * vbStride; |
| 331 | alloc_info.alignment = 0; |
| 332 | alloc_info.heapCount = 1; |
| 333 | alloc_info.heaps[0] = 0; // TODO: Use known existing heap |
| 334 | |
| 335 | alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT; |
| 336 | alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 337 | |
| 338 | err = xglAllocMemory(device(), &alloc_info, &m_vtxBufferMem); |
| 339 | ASSERT_XGL_SUCCESS(err); |
| 340 | |
| 341 | err = xglMapMemory(m_vtxBufferMem, 0, (XGL_VOID **) &pData); |
| 342 | ASSERT_XGL_SUCCESS(err); |
| 343 | |
| 344 | memcpy(pData, vertices, alloc_info.allocationSize); |
| 345 | |
| 346 | err = xglUnmapMemory(m_vtxBufferMem); |
| 347 | ASSERT_XGL_SUCCESS(err); |
| 348 | |
| 349 | // set up the memory view for the vertex buffer |
| 350 | this->m_vtxBufferView.stride = vbStride; |
| 351 | this->m_vtxBufferView.range = numVertices * vbStride; |
| 352 | this->m_vtxBufferView.offset = 0; |
| 353 | this->m_vtxBufferView.mem = m_vtxBufferMem; |
| 354 | this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED; |
| 355 | this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED; |
| 356 | |
| 357 | // open the command buffer |
| 358 | err = xglBeginCommandBuffer( m_cmdBuffer, 0 ); |
| 359 | ASSERT_XGL_SUCCESS(err); |
| 360 | |
| 361 | XGL_MEMORY_STATE_TRANSITION transition = {}; |
| 362 | transition.mem = m_vtxBufferMem; |
| 363 | transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER; |
| 364 | transition.newState = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY; |
| 365 | transition.offset = 0; |
| 366 | transition.regionSize = numVertices * vbStride; |
| 367 | |
| 368 | // write transition to the command buffer |
| 369 | xglCmdPrepareMemoryRegions( m_cmdBuffer, 1, &transition ); |
| 370 | this->m_vtxBufferView.state = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY; |
| 371 | |
| 372 | // finish recording the command buffer |
| 373 | err = xglEndCommandBuffer( m_cmdBuffer ); |
| 374 | ASSERT_XGL_SUCCESS(err); |
| 375 | |
| 376 | // this command buffer only uses the vertex buffer memory |
| 377 | m_numMemRefs = 1; |
| 378 | m_memRefs[0].flags = 0; |
| 379 | m_memRefs[0].mem = m_vtxBufferMem; |
| 380 | |
| 381 | // submit the command buffer to the universal queue |
| 382 | err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL ); |
| 383 | ASSERT_XGL_SUCCESS(err); |
| 384 | } |
| 385 | |
| 386 | void XglRenderTest::InitConstantBuffer(int constantCount, int constantSize, const void* data) |
| 387 | { |
| 388 | XGL_RESULT err = XGL_SUCCESS; |
| 389 | |
| 390 | XGL_MEMORY_ALLOC_INFO alloc_info = {}; |
| 391 | XGL_UINT8 *pData; |
| 392 | |
| 393 | alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 394 | alloc_info.allocationSize = constantCount * constantSize; |
| 395 | alloc_info.alignment = 0; |
| 396 | alloc_info.heapCount = 1; |
| 397 | alloc_info.heaps[0] = 0; // TODO: Use known existing heap |
| 398 | |
| 399 | alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT; |
| 400 | alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 401 | |
| 402 | err = xglAllocMemory(device(), &alloc_info, &m_constantBufferMem); |
| 403 | ASSERT_XGL_SUCCESS(err); |
| 404 | |
| 405 | err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData); |
| 406 | ASSERT_XGL_SUCCESS(err); |
| 407 | |
| 408 | memcpy(pData, data, alloc_info.allocationSize); |
| 409 | |
| 410 | err = xglUnmapMemory(m_constantBufferMem); |
| 411 | ASSERT_XGL_SUCCESS(err); |
| 412 | |
| 413 | // set up the memory view for the constant buffer |
| 414 | this->m_constantBufferView.stride = 1; |
| 415 | this->m_constantBufferView.range = 16; |
| 416 | this->m_constantBufferView.offset = 0; |
| 417 | this->m_constantBufferView.mem = m_constantBufferMem; |
| 418 | this->m_constantBufferView.format.channelFormat = XGL_CH_FMT_R32G32B32A32; |
| 419 | this->m_constantBufferView.format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 420 | } |
| 421 | |
| 422 | void XglRenderTest::DrawRotatedTriangleTest() |
| 423 | { |
| 424 | // TODO : This test will pass a matrix into VS to affect triangle orientation. |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * DrawTriangleTest deliberately calls each necessary xgl call to |
| 429 | * make it clear what is necessary to render and simplify debugging. |
| 430 | */ |
| 431 | void XglRenderTest::DrawTriangleTest() |
| 432 | { |
| 433 | XGL_PIPELINE pipeline; |
| 434 | XGL_SHADER vs, ps; |
| 435 | XGL_RESULT err; |
| 436 | XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {}; |
| 437 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage; |
| 438 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage; |
| 439 | std::vector<unsigned int> bil; |
| 440 | XGL_SHADER_CREATE_INFO createInfo; |
| 441 | XGL_SHADER shader; |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame^] | 442 | size_t shader_len; |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 443 | XGL_IMAGE m_image; |
| 444 | XGL_COLOR_ATTACHMENT_VIEW m_targetView; |
| 445 | XGL_IMAGE_VIEW_ATTACH_INFO m_imageInfo; |
| 446 | XGL_GPU_MEMORY m_memory; |
| 447 | |
| 448 | int width = 256, height = 256; |
| 449 | |
| 450 | // create a raster state (solid, back-face culling) |
| 451 | XGL_RASTER_STATE_CREATE_INFO raster = {}; |
| 452 | raster.sType = XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO; |
| 453 | raster.fillMode = XGL_FILL_SOLID; |
| 454 | raster.cullMode = XGL_CULL_NONE; |
| 455 | raster.frontFace = XGL_FRONT_FACE_CCW; |
| 456 | err = xglCreateRasterState( device(), &raster, &m_stateRaster ); |
| 457 | ASSERT_XGL_SUCCESS(err); |
| 458 | |
| 459 | XGL_VIEWPORT_STATE_CREATE_INFO viewport = {}; |
| 460 | viewport.viewportCount = 1; |
| 461 | viewport.scissorEnable = XGL_FALSE; |
| 462 | viewport.viewports[0].originX = 0; |
| 463 | viewport.viewports[0].originY = 0; |
| 464 | viewport.viewports[0].width = 1.f * width; |
| 465 | viewport.viewports[0].height = 1.f * height; |
| 466 | viewport.viewports[0].minDepth = 0.f; |
| 467 | viewport.viewports[0].maxDepth = 1.f; |
| 468 | |
| 469 | err = xglCreateViewportState( device(), &viewport, &m_stateViewport ); |
| 470 | ASSERT_XGL_SUCCESS( err ); |
| 471 | |
| 472 | XGL_COLOR_BLEND_STATE_CREATE_INFO blend = {}; |
| 473 | blend.sType = XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO; |
| 474 | err = xglCreateColorBlendState(device(), &blend, &m_colorBlend); |
| 475 | ASSERT_XGL_SUCCESS( err ); |
| 476 | |
| 477 | XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {}; |
| 478 | depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO; |
| 479 | depthStencil.depthTestEnable = XGL_FALSE; |
| 480 | depthStencil.depthWriteEnable = XGL_FALSE; |
| 481 | depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL; |
| 482 | depthStencil.depthBoundsEnable = XGL_FALSE; |
| 483 | depthStencil.minDepth = 0.f; |
| 484 | depthStencil.maxDepth = 1.f; |
| 485 | depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP; |
| 486 | depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP; |
| 487 | depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP; |
| 488 | depthStencil.back.stencilRef = 0x00; |
| 489 | depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS; |
| 490 | depthStencil.front = depthStencil.back; |
| 491 | |
| 492 | err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil ); |
| 493 | ASSERT_XGL_SUCCESS( err ); |
| 494 | |
| 495 | XGL_MSAA_STATE_CREATE_INFO msaa = {}; |
| 496 | msaa.sType = XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO; |
| 497 | msaa.sampleMask = 1; |
| 498 | msaa.samples = 1; |
| 499 | |
| 500 | err = xglCreateMsaaState( device(), &msaa, &m_stateMsaa ); |
| 501 | ASSERT_XGL_SUCCESS( err ); |
| 502 | |
| 503 | XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {}; |
| 504 | |
| 505 | cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO; |
| 506 | cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS; |
| 507 | err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer); |
| 508 | ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed"; |
| 509 | |
| 510 | #if 0 |
| 511 | // Create descriptor set for our one resource |
| 512 | XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {}; |
| 513 | descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO; |
| 514 | descriptorInfo.slots = 1; // Vertex buffer only |
| 515 | |
| 516 | // create a descriptor set with a single slot |
| 517 | err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet ); |
| 518 | ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed"; |
| 519 | |
| 520 | // bind memory to the descriptor set |
| 521 | err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem); |
| 522 | |
| 523 | // set up the memory view for the vertex buffer |
| 524 | this->m_vtxBufferView.stride = vbStride; |
| 525 | this->m_vtxBufferView.range = numVertices * vbStride; |
| 526 | this->m_vtxBufferView.offset = 0; |
| 527 | this->m_vtxBufferView.mem = m_vtxBufferMem; |
| 528 | this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED; |
| 529 | this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED; |
| 530 | // write the vertex buffer view to the descriptor set |
| 531 | xglBeginDescriptorSetUpdate( m_rsrcDescSet ); |
| 532 | xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_vtxBufferView ); |
| 533 | xglEndDescriptorSetUpdate( m_rsrcDescSet ); |
| 534 | #endif |
| 535 | |
| 536 | const int constantCount = 4; |
| 537 | const float constants[constantCount] = { 0.5, 0.5, 0.5, 1.0 }; |
| 538 | InitConstantBuffer(constantCount, sizeof(constants[0]), (const void*) constants); |
| 539 | |
| 540 | // Create descriptor set for a uniform resource |
| 541 | XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {}; |
| 542 | descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO; |
| 543 | descriptorInfo.slots = 1; |
| 544 | |
| 545 | // create a descriptor set with a single slot |
| 546 | err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet ); |
| 547 | ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed"; |
| 548 | |
| 549 | // bind memory to the descriptor set |
| 550 | err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem); |
| 551 | |
| 552 | // write the constant buffer view to the descriptor set |
| 553 | xglBeginDescriptorSetUpdate( m_rsrcDescSet ); |
| 554 | xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView ); |
| 555 | xglEndDescriptorSetUpdate( m_rsrcDescSet ); |
| 556 | |
| 557 | static const char *vertShaderText = |
| 558 | "#version 130\n" |
| 559 | "vec2 vertices[3];\n" |
| 560 | "void main() {\n" |
| 561 | " vertices[0] = vec2(-1.0, -1.0);\n" |
| 562 | " vertices[1] = vec2( 1.0, -1.0);\n" |
| 563 | " vertices[2] = vec2( 0.0, 1.0);\n" |
| 564 | " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n" |
| 565 | "}\n"; |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 566 | |
| 567 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 568 | createInfo.pNext = NULL; |
| 569 | |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame^] | 570 | shader_len = strlen(vertShaderText); |
| 571 | createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1; |
| 572 | createInfo.pCode = malloc(createInfo.codeSize); |
| 573 | createInfo.flags = 0; |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 574 | |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame^] | 575 | /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */ |
| 576 | ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC; |
| 577 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 578 | ((uint32_t *) createInfo.pCode)[2] = XGL_SHADER_STAGE_VERTEX; |
| 579 | memcpy(((uint32_t *) createInfo.pCode + 3), vertShaderText, shader_len + 1); |
| 580 | |
| 581 | err = xglCreateShader(device(), &createInfo, &shader); |
| 582 | if (err) { |
| 583 | free((void *) createInfo.pCode); |
| 584 | |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 585 | // Use Reference GLSL to BIL compiler |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame^] | 586 | GLSLtoBIL(XGL_SHADER_STAGE_VERTEX, vertShaderText, bil); |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 587 | createInfo.pCode = bil.data(); |
| 588 | createInfo.codeSize = bil.size() * sizeof(unsigned int); |
| 589 | createInfo.flags = 0; |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame^] | 590 | err = xglCreateShader(device(), &createInfo, &shader); |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 591 | } |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 592 | ASSERT_XGL_SUCCESS(err); |
| 593 | |
| 594 | vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 595 | vs_stage.pNext = XGL_NULL_HANDLE; |
| 596 | vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX; |
| 597 | vs_stage.shader.shader = vs; |
| 598 | vs_stage.shader.descriptorSetMapping[0].descriptorCount = 0; |
| 599 | vs_stage.shader.linkConstBufferCount = 0; |
| 600 | vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE; |
| 601 | vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED; |
| 602 | vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0; |
| 603 | |
| 604 | static const char *fragShaderText = |
| 605 | "#version 130\n" |
| 606 | "uniform vec4 foo;\n" |
| 607 | "void main() {\n" |
| 608 | " gl_FragColor = foo;\n" |
| 609 | "}\n"; |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 610 | |
| 611 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 612 | createInfo.pNext = NULL; |
| 613 | |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame^] | 614 | shader_len = strlen(fragShaderText); |
| 615 | createInfo.codeSize = 3 * sizeof(uint32_t) + shader_len + 1; |
| 616 | createInfo.pCode = malloc(createInfo.codeSize); |
| 617 | createInfo.flags = 0; |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 618 | |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame^] | 619 | /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */ |
| 620 | ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC; |
| 621 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 622 | ((uint32_t *) createInfo.pCode)[2] = XGL_SHADER_STAGE_VERTEX; |
| 623 | memcpy(((uint32_t *) createInfo.pCode + 3), fragShaderText, shader_len + 1); |
| 624 | |
| 625 | err = xglCreateShader(device(), &createInfo, &ps); |
| 626 | if (err) { |
| 627 | free((void *) createInfo.pCode); |
| 628 | |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 629 | // Use Reference GLSL to BIL compiler |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame^] | 630 | GLSLtoBIL(XGL_SHADER_STAGE_VERTEX, fragShaderText, bil); |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 631 | createInfo.pCode = bil.data(); |
| 632 | createInfo.codeSize = bil.size() * sizeof(unsigned int); |
| 633 | createInfo.flags = 0; |
Courtney Goeltzenleuchter | bfde09b | 2014-10-10 16:29:46 -0600 | [diff] [blame^] | 634 | err = xglCreateShader(device(), &createInfo, &ps); |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 635 | } |
Courtney Goeltzenleuchter | 336139d | 2014-10-08 08:59:45 -0600 | [diff] [blame] | 636 | ASSERT_XGL_SUCCESS(err); |
| 637 | |
| 638 | ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 639 | ps_stage.pNext = &vs_stage; |
| 640 | ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT; |
| 641 | ps_stage.shader.shader = ps; |
| 642 | |
| 643 | const int slots = 1; |
| 644 | XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( slots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) ); |
| 645 | slotInfo[0].shaderEntityIndex = 0; |
| 646 | slotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE; |
| 647 | |
| 648 | ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo; |
| 649 | ps_stage.shader.descriptorSetMapping[0].descriptorCount = 1; |
| 650 | |
| 651 | ps_stage.shader.linkConstBufferCount = 0; |
| 652 | ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE; |
| 653 | ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED; |
| 654 | ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0; |
| 655 | |
| 656 | XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = { |
| 657 | XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType |
| 658 | &ps_stage, // pNext |
| 659 | XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY |
| 660 | XGL_FALSE, // disableVertexReuse |
| 661 | XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION |
| 662 | XGL_FALSE, // primitiveRestartEnable |
| 663 | 0 // primitiveRestartIndex |
| 664 | }; |
| 665 | |
| 666 | XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = { |
| 667 | XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO, |
| 668 | &ia_state, |
| 669 | XGL_FALSE, // depthClipEnable |
| 670 | XGL_FALSE, // rasterizerDiscardEnable |
| 671 | 1.0 // pointSize |
| 672 | }; |
| 673 | |
| 674 | XGL_PIPELINE_CB_STATE cb_state = { |
| 675 | XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO, |
| 676 | &rs_state, |
| 677 | XGL_FALSE, // alphaToCoverageEnable |
| 678 | XGL_FALSE, // dualSourceBlendEnable |
| 679 | XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP |
| 680 | { // XGL_PIPELINE_CB_ATTACHMENT_STATE |
| 681 | { |
| 682 | XGL_FALSE, // blendEnable |
| 683 | {XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM}, // XGL_FORMAT |
| 684 | 0xF // channelWriteMask |
| 685 | } |
| 686 | } |
| 687 | }; |
| 688 | |
| 689 | // TODO: Should take depth buffer format from queried formats |
| 690 | XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = { |
| 691 | XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO, |
| 692 | &cb_state, |
| 693 | {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT |
| 694 | }; |
| 695 | |
| 696 | info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 697 | info.pNext = &db_state; |
| 698 | info.flags = 0; |
| 699 | err = xglCreateGraphicsPipeline(device(), &info, &pipeline); |
| 700 | ASSERT_XGL_SUCCESS(err); |
| 701 | |
| 702 | err = m_device->AllocAndBindGpuMemory(pipeline, "Pipeline", &m_pipe_mem); |
| 703 | ASSERT_XGL_SUCCESS(err); |
| 704 | |
| 705 | /* |
| 706 | * Shaders are now part of the pipeline, don't need these anymore |
| 707 | */ |
| 708 | ASSERT_XGL_SUCCESS(xglDestroyObject(ps)); |
| 709 | ASSERT_XGL_SUCCESS(xglDestroyObject(vs)); |
| 710 | |
| 711 | XGL_QUERY_POOL query; |
| 712 | XGL_GPU_MEMORY query_mem; |
| 713 | ASSERT_NO_FATAL_FAILURE(CreateQueryPool(XGL_QUERY_PIPELINE_STATISTICS, 1, &query, &query_mem)); |
| 714 | |
| 715 | XglImage *renderTarget; |
| 716 | XGL_FORMAT fmt = { |
| 717 | XGL_CH_FMT_R8G8B8A8, |
| 718 | XGL_NUM_FMT_UNORM |
| 719 | }; |
| 720 | renderTarget = new XglImage(m_device); |
| 721 | renderTarget->init(width, height, fmt, XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | |
| 722 | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 723 | // ASSERT_NO_FATAL_FAILURE(m_device->CreateImage(width, height, fmt, |
| 724 | // XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | |
| 725 | // XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
| 726 | // &renderTarget)); |
| 727 | { |
| 728 | XGL_UINT mipCount; |
| 729 | XGL_SIZE size = sizeof(XGL_FORMAT_PROPERTIES); |
| 730 | XGL_FORMAT_PROPERTIES image_fmt; |
| 731 | |
| 732 | mipCount = 0; |
| 733 | |
| 734 | XGL_UINT _w = width; |
| 735 | XGL_UINT _h = height; |
| 736 | while( ( _w > 0 ) || ( _h > 0 ) ) |
| 737 | { |
| 738 | _w >>= 1; |
| 739 | _h >>= 1; |
| 740 | mipCount++; |
| 741 | } |
| 742 | |
| 743 | // TODO: Pick known good format rather than just expect common format |
| 744 | /* |
| 745 | * XXX: What should happen if given NULL HANDLE for the pData argument? |
| 746 | * We're not requesting XGL_INFO_TYPE_MEMORY_REQUIREMENTS so there is |
| 747 | * an expectation that pData is a valid pointer. |
| 748 | * However, why include a returned size value? That implies that the |
| 749 | * amount of data may vary and that doesn't work well for using a |
| 750 | * fixed structure. |
| 751 | */ |
| 752 | |
| 753 | err = xglGetFormatInfo(this->m_device->device(), fmt, |
| 754 | XGL_INFO_TYPE_FORMAT_PROPERTIES, |
| 755 | &size, &image_fmt); |
| 756 | ASSERT_XGL_SUCCESS(err); |
| 757 | |
| 758 | // typedef struct _XGL_IMAGE_CREATE_INFO |
| 759 | // { |
| 760 | // XGL_STRUCTURE_TYPE sType; // Must be XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO |
| 761 | // const XGL_VOID* pNext; // Pointer to next structure. |
| 762 | // XGL_IMAGE_TYPE imageType; |
| 763 | // XGL_FORMAT format; |
| 764 | // XGL_EXTENT3D extent; |
| 765 | // XGL_UINT mipLevels; |
| 766 | // XGL_UINT arraySize; |
| 767 | // XGL_UINT samples; |
| 768 | // XGL_IMAGE_TILING tiling; |
| 769 | // XGL_FLAGS usage; // XGL_IMAGE_USAGE_FLAGS |
| 770 | // XGL_FLAGS flags; // XGL_IMAGE_CREATE_FLAGS |
| 771 | // } XGL_IMAGE_CREATE_INFO; |
| 772 | |
| 773 | |
| 774 | XGL_IMAGE_CREATE_INFO imageCreateInfo = {}; |
| 775 | imageCreateInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 776 | imageCreateInfo.imageType = XGL_IMAGE_2D; |
| 777 | imageCreateInfo.format = fmt; |
| 778 | imageCreateInfo.arraySize = 1; |
| 779 | imageCreateInfo.extent.width = width; |
| 780 | imageCreateInfo.extent.height = height; |
| 781 | imageCreateInfo.extent.depth = 1; |
| 782 | imageCreateInfo.mipLevels = mipCount; |
| 783 | imageCreateInfo.samples = 1; |
| 784 | imageCreateInfo.tiling = XGL_LINEAR_TILING; |
| 785 | |
| 786 | // Image usage flags |
| 787 | // typedef enum _XGL_IMAGE_USAGE_FLAGS |
| 788 | // { |
| 789 | // XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT = 0x00000001, |
| 790 | // XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT = 0x00000002, |
| 791 | // XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT = 0x00000004, |
| 792 | // XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT = 0x00000008, |
| 793 | // } XGL_IMAGE_USAGE_FLAGS; |
| 794 | // imageCreateInfo.usage = XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 795 | imageCreateInfo.usage = XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | |
| 796 | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 797 | |
| 798 | // XGL_RESULT XGLAPI xglCreateImage( |
| 799 | // XGL_DEVICE device, |
| 800 | // const XGL_IMAGE_CREATE_INFO* pCreateInfo, |
| 801 | // XGL_IMAGE* pImage); |
| 802 | err = xglCreateImage(device(), &imageCreateInfo, &m_image); |
| 803 | ASSERT_XGL_SUCCESS(err); |
| 804 | |
| 805 | XGL_MEMORY_REQUIREMENTS mem_req; |
| 806 | XGL_UINT data_size = sizeof(XGL_MEMORY_REQUIREMENTS); |
| 807 | err = xglGetObjectInfo(m_image, XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 808 | &data_size, &mem_req); |
| 809 | ASSERT_XGL_SUCCESS(err); |
| 810 | ASSERT_EQ(data_size, sizeof(mem_req)); |
| 811 | ASSERT_NE(0, mem_req.size) << "xglGetObjectInfo (Event): Failed - expect images to require memory"; |
| 812 | |
| 813 | m_imageInfo.state = XGL_IMAGE_STATE_UNINITIALIZED_TARGET; |
| 814 | |
| 815 | // XGL_RESULT XGLAPI xglAllocMemory( |
| 816 | // XGL_DEVICE device, |
| 817 | // const XGL_MEMORY_ALLOC_INFO* pAllocInfo, |
| 818 | // XGL_GPU_MEMORY* pMem); |
| 819 | XGL_MEMORY_ALLOC_INFO mem_info; |
| 820 | |
| 821 | memset(&mem_info, 0, sizeof(mem_info)); |
| 822 | mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 823 | mem_info.allocationSize = mem_req.size; |
| 824 | mem_info.alignment = mem_req.alignment; |
| 825 | mem_info.heapCount = mem_req.heapCount; |
| 826 | memcpy(mem_info.heaps, mem_req.heaps, sizeof(XGL_UINT)*XGL_MAX_MEMORY_HEAPS); |
| 827 | mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 828 | mem_info.flags = XGL_MEMORY_ALLOC_SHAREABLE_BIT; |
| 829 | err = xglAllocMemory(device(), &mem_info, &m_memory); |
| 830 | ASSERT_XGL_SUCCESS(err); |
| 831 | |
| 832 | err = xglBindObjectMemory(m_image, m_memory, 0); |
| 833 | ASSERT_XGL_SUCCESS(err); |
| 834 | |
| 835 | XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = { |
| 836 | XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO, |
| 837 | XGL_NULL_HANDLE, |
| 838 | m_image, |
| 839 | fmt, |
| 840 | 0, |
| 841 | 0, |
| 842 | 1 |
| 843 | }; |
| 844 | |
| 845 | err = xglCreateColorAttachmentView(device(), &createView, &m_targetView); |
| 846 | ASSERT_XGL_SUCCESS(err); |
| 847 | } |
| 848 | |
| 849 | // Build command buffer |
| 850 | err = xglBeginCommandBuffer(m_cmdBuffer, 0); |
| 851 | ASSERT_XGL_SUCCESS(err); |
| 852 | |
| 853 | // GenerateClearAndPrepareBufferCmds(renderTarget); |
| 854 | { |
| 855 | // whatever we want to do, we do it to the whole buffer |
| 856 | XGL_IMAGE_SUBRESOURCE_RANGE srRange = {}; |
| 857 | srRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 858 | srRange.baseMipLevel = 0; |
| 859 | srRange.mipLevels = XGL_LAST_MIP_OR_SLICE; |
| 860 | srRange.baseArraySlice = 0; |
| 861 | srRange.arraySize = XGL_LAST_MIP_OR_SLICE; |
| 862 | |
| 863 | // prepare the whole back buffer for clear |
| 864 | XGL_IMAGE_STATE_TRANSITION transitionToClear = {}; |
| 865 | transitionToClear.image = renderTarget->image(); |
| 866 | transitionToClear.oldState = renderTarget->state(); |
| 867 | transitionToClear.newState = XGL_IMAGE_STATE_CLEAR; |
| 868 | transitionToClear.subresourceRange = srRange; |
| 869 | xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear ); |
| 870 | renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState); |
| 871 | |
| 872 | // clear the back buffer to dark grey |
| 873 | XGL_UINT clearColor[4] = {64, 64, 64, 0}; |
| 874 | xglCmdClearColorImageRaw( m_cmdBuffer, renderTarget->image(), clearColor, 1, &srRange ); |
| 875 | |
| 876 | // prepare back buffer for rendering |
| 877 | XGL_IMAGE_STATE_TRANSITION transitionToRender = {}; |
| 878 | transitionToRender.image = renderTarget->image(); |
| 879 | transitionToRender.oldState = renderTarget->state(); |
| 880 | transitionToRender.newState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL; |
| 881 | transitionToRender.subresourceRange = srRange; |
| 882 | xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender ); |
| 883 | renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState); |
| 884 | } |
| 885 | |
| 886 | { |
| 887 | // bind render target |
| 888 | XGL_COLOR_ATTACHMENT_BIND_INFO colorBind = {}; |
| 889 | colorBind.view = renderTarget->targetView(); |
| 890 | colorBind.colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL; |
| 891 | xglCmdBindAttachments(m_cmdBuffer, 1, &colorBind, NULL ); |
| 892 | } |
| 893 | |
| 894 | { |
| 895 | // set all states |
| 896 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_RASTER, m_stateRaster ); |
| 897 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_VIEWPORT, m_stateViewport ); |
| 898 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_COLOR_BLEND, m_colorBlend); |
| 899 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil ); |
| 900 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_MSAA, m_stateMsaa ); |
| 901 | |
| 902 | // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view) |
| 903 | xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline ); |
| 904 | xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 ); |
| 905 | } |
| 906 | |
| 907 | xglCmdResetQueryPool(m_cmdBuffer, query, 0, 1); |
| 908 | xglCmdBeginQuery(m_cmdBuffer, query, 0, 0); |
| 909 | |
| 910 | // render the cube |
| 911 | xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 ); |
| 912 | |
| 913 | xglCmdEndQuery(m_cmdBuffer, query, 0); |
| 914 | |
| 915 | // prepare the back buffer for present |
| 916 | // XGL_IMAGE_STATE_TRANSITION transitionToPresent = {}; |
| 917 | // transitionToPresent.image = m_image; |
| 918 | // transitionToPresent.oldState = m_image_state; |
| 919 | // transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT; |
| 920 | // transitionToPresent.subresourceRange = srRange; |
| 921 | // xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent ); |
| 922 | // m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState; |
| 923 | |
| 924 | // finalize recording of the command buffer |
| 925 | err = xglEndCommandBuffer( m_cmdBuffer ); |
| 926 | ASSERT_XGL_SUCCESS( err ); |
| 927 | |
| 928 | // this command buffer only uses the vertex buffer memory |
| 929 | m_numMemRefs = 0; |
| 930 | // m_memRefs[0].flags = 0; |
| 931 | // m_memRefs[0].mem = m_vtxBufferMemory; |
| 932 | |
| 933 | // submit the command buffer to the universal queue |
| 934 | err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL ); |
| 935 | ASSERT_XGL_SUCCESS( err ); |
| 936 | |
| 937 | err = xglQueueWaitIdle( m_device->m_queue ); |
| 938 | ASSERT_XGL_SUCCESS( err ); |
| 939 | |
| 940 | // Wait for work to finish before cleaning up. |
| 941 | xglDeviceWaitIdle(m_device->device()); |
| 942 | |
| 943 | XGL_PIPELINE_STATISTICS_DATA stats; |
| 944 | XGL_SIZE stats_size = sizeof(stats); |
| 945 | err = xglGetQueryPoolResults(query, 0, 1, &stats_size, &stats); |
| 946 | ASSERT_XGL_SUCCESS( err ); |
| 947 | ASSERT_EQ(stats_size, sizeof(stats)); |
| 948 | |
| 949 | ASSERT_EQ(stats.vsInvocations, 3); |
| 950 | ASSERT_EQ(stats.cPrimitives, 1); |
| 951 | ASSERT_EQ(stats.cInvocations, 1); |
| 952 | |
| 953 | DestroyQueryPool(query, query_mem); |
| 954 | |
| 955 | const ::testing::TestInfo* const test_info = |
| 956 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 957 | |
| 958 | // renderTarget->WritePPM(test_info->test_case_name()); |
| 959 | // m_screen.Display(renderTarget, m_image_mem); |
| 960 | RecordImage(renderTarget); |
| 961 | |
| 962 | ASSERT_XGL_SUCCESS(xglDestroyObject(pipeline)); |
| 963 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_cmdBuffer)); |
| 964 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateRaster)); |
| 965 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateViewport)); |
| 966 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateDepthStencil)); |
| 967 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateMsaa)); |
| 968 | free(renderTarget); |
| 969 | } |
| 970 | |
| 971 | |
| 972 | TEST_F(XglRenderTest, TestDrawTriangle) { |
| 973 | DrawTriangleTest(); |
| 974 | } |
| 975 | |
| 976 | int main(int argc, char **argv) { |
| 977 | int result; |
| 978 | |
| 979 | ::testing::InitGoogleTest(&argc, argv); |
| 980 | XglTestFramework::InitArgs(&argc, argv); |
| 981 | |
| 982 | ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment); |
| 983 | |
| 984 | result = RUN_ALL_TESTS(); |
| 985 | |
| 986 | XglTestFramework::Finish(); |
| 987 | return result; |
| 988 | } |