Courtney Goeltzenleuchter | 3776d4b | 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> |
| 67 | #include <xglIntelExt.h> |
| 68 | #include "gtest-1.7.0/include/gtest/gtest.h" |
| 69 | |
| 70 | #include "xgldevice.h" |
| 71 | #include "xglimage.h" |
| 72 | #include "icd-bil.h" |
| 73 | |
| 74 | #include "xgltestframework.h" |
| 75 | |
| 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 | |
| 132 | static const uint32_t gen6_fs[] = { |
| 133 | 0x00600001, 0x202003fe, 0x00000000, 0x3f800000, // mov(8) m1<1>F 1F { align1 1Q }; |
| 134 | 0x00600001, 0x204003fe, 0x00000000, 0x00000000, // mov(8) m2<1>F 0F { align1 1Q }; |
| 135 | 0x00600001, 0x206003fe, 0x00000000, 0x00000000, // mov(8) m3<1>F 0F { align1 1Q }; |
| 136 | 0x00600001, 0x208003fe, 0x00000000, 0x3f800000, // mov(8) m4<1>F 1F { align1 1Q }; |
| 137 | 0x05600032, 0x20001fc8, 0x008d0020, 0x88019400, // sendc(8) null m1<8,8,1>F |
| 138 | // render RT write SIMD8 LastRT Surface = 0 mlen 4 rlen 0 { align1 1Q EOT }; |
| 139 | }; |
| 140 | |
| 141 | static const uint32_t gen6_vs[] = { |
| 142 | 0x01600110, 0x200f1ca4, 0x00600020, 0x00000000, // cmp.z.f0(8) null g1<4,4,1>.xD 0D { align16 1Q }; |
| 143 | 0x00670122, 0x000a108f, 0x000e0004, 0x000e0004, // (+f0.all4h) if(8) JIP: 10 { align16 1Q }; |
| 144 | 0x00600501, 0x204303fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.xyF -1F { align16 NoDDClr 1Q }; |
| 145 | 0x00600d01, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDClr,NoDDChk 1Q }; |
| 146 | 0x00600901, 0x204803fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.wF 1F { align16 NoDDChk 1Q }; |
| 147 | 0x00600124, 0x0014108f, 0x006e0004, 0x006e0004, // else(8) JIP: 20 { align16 1Q }; |
| 148 | 0x01600110, 0x200f1ca4, 0x00600020, 0x00000001, // cmp.z.f0(8) null g1<4,4,1>.xD 1D { align16 1Q }; |
| 149 | 0x00670122, 0x000a108f, 0x000e0004, 0x000e0004, // (+f0.all4h) if(8) JIP: 10 { align16 1Q }; |
| 150 | 0x00600501, 0x204903fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.xwF 1F { align16 NoDDClr 1Q }; |
| 151 | 0x00600d01, 0x204203fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.yF -1F { align16 NoDDClr,NoDDChk 1Q }; |
| 152 | 0x00600901, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDChk 1Q }; |
| 153 | 0x00600124, 0x0006108f, 0x006e0004, 0x006e0004, // else(8) JIP: 6 { align16 1Q }; |
| 154 | 0x00600501, 0x204503fd, 0x00000000, 0x00000000, // mov(8) g2<1>.xzF 0F { align16 NoDDClr 1Q }; |
| 155 | 0x00600901, 0x204a03fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.ywF 1F { align16 NoDDChk 1Q }; |
| 156 | 0x00600125, 0x0002108f, 0x006e0004, 0x006e0002, // endif(8) JIP: 2 { align16 1Q }; |
| 157 | 0x00600125, 0x0002108f, 0x006e0004, 0x006e0002, // endif(8) JIP: 2 { align16 1Q }; |
| 158 | 0x00600101, 0x204f0062, 0x00000000, 0x00000000, // mov(8) m2<1>UD 0x00000000UD { align16 1Q }; |
| 159 | 0x00600101, 0x206f03be, 0x006e0044, 0x00000000, // mov(8) m3<1>F g2<4,4,1>F { align16 1Q }; |
| 160 | 0x00600301, 0x202f0022, 0x006e0004, 0x00000000, // mov(8) m1<1>UD g0<4,4,1>UD { align16 WE_all 1Q }; |
| 161 | 0x06600131, 0x200f1fdc, 0x006e0024, 0x8608c400, // send(8) null m1<4,4,1>F |
| 162 | // urb 0 urb_write interleave used complete mlen 3 rlen 0 { align16 1Q EOT }; |
| 163 | }; |
| 164 | |
| 165 | static const uint32_t gen7_fs[] = { |
| 166 | 0x00600001, 0x2e2003fd, 0x00000000, 0x3f800000, // mov(8) g113<1>F 1F { align1 1Q }; |
| 167 | 0x00600001, 0x2e4003fd, 0x00000000, 0x00000000, // mov(8) g114<1>F 0F { align1 1Q }; |
| 168 | 0x00600001, 0x2e6003fd, 0x00000000, 0x00000000, // mov(8) g115<1>F 0F { align1 1Q }; |
| 169 | 0x00600001, 0x2e8003fd, 0x00000000, 0x3f800000, // mov(8) g116<1>F 1F { align1 1Q }; |
| 170 | 0x05600032, 0x20001fa8, 0x008d0e20, 0x88031400, // sendc(8) null g113<8,8,1>F |
| 171 | // render RT write SIMD8 LastRT Surface = 0 mlen 4 rlen 0 { align1 1Q EOT }; |
| 172 | }; |
| 173 | |
| 174 | static const uint32_t gen7_vs[] = { |
| 175 | 0x01608110, 0x200f1ca4, 0x00600020, 0x00000000, // cmp.z.f0(8) null g1<4,4,1>.xD 0D { align16 1Q switch }; |
| 176 | 0x00670122, 0x200f0c84, 0x000e0004, 0x001c000a, // (+f0.all4h) if(8) JIP: 10 UIP: 28 { align16 1Q }; |
| 177 | 0x00600501, 0x204303fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.xyF -1F { align16 NoDDClr 1Q }; |
| 178 | 0x00600d01, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDClr,NoDDChk 1Q }; |
| 179 | 0x00600901, 0x204803fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.wF 1F { align16 NoDDChk 1Q }; |
| 180 | 0x00600124, 0x200f0c84, 0x006e0004, 0x00000014, // else(8) JIP: 20 { align16 1Q }; |
| 181 | 0x01608110, 0x200f1ca4, 0x00600020, 0x00000001, // cmp.z.f0(8) null g1<4,4,1>.xD 1D { align16 1Q switch }; |
| 182 | 0x00670122, 0x200f0c84, 0x000e0004, 0x000e000a, // (+f0.all4h) if(8) JIP: 10 UIP: 14 { align16 1Q }; |
| 183 | 0x00600501, 0x204903fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.xwF 1F { align16 NoDDClr 1Q }; |
| 184 | 0x00600d01, 0x204203fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.yF -1F { align16 NoDDClr,NoDDChk 1Q }; |
| 185 | 0x00600901, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDChk 1Q }; |
| 186 | 0x00600124, 0x200f0c84, 0x006e0004, 0x00000006, // else(8) JIP: 6 { align16 1Q }; |
| 187 | 0x00600501, 0x204503fd, 0x00000000, 0x00000000, // mov(8) g2<1>.xzF 0F { align16 NoDDClr 1Q }; |
| 188 | 0x00600901, 0x204a03fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.ywF 1F { align16 NoDDChk 1Q }; |
| 189 | 0x00600125, 0x200f0c84, 0x006e0004, 0x00000002, // endif(8) JIP: 2 { align16 1Q }; |
| 190 | 0x00600125, 0x200f0c84, 0x006e0004, 0x00000002, // endif(8) JIP: 2 { align16 1Q }; |
| 191 | 0x00600101, 0x2e4f0061, 0x00000000, 0x00000000, // mov(8) g114<1>UD 0x00000000UD { align16 1Q }; |
| 192 | 0x00600101, 0x2e6f03bd, 0x006e0044, 0x00000000, // mov(8) g115<1>F g2<4,4,1>F { align16 1Q }; |
| 193 | 0x00600301, 0x2e2f0021, 0x006e0004, 0x00000000, // mov(8) g113<1>UD g0<4,4,1>UD { align16 WE_all 1Q }; |
| 194 | 0x00000206, 0x2e340c21, 0x00000014, 0x0000ff00, // or(1) g113.5<1>UD g0.5<0,1,0>UD 0x0000ff00UD { align1 WE_all }; |
| 195 | 0x06600131, 0x200f1fbc, 0x006e0e24, 0x8608c000, // send(8) null g113<4,4,1>F |
| 196 | // urb 0 write HWord interleave complete mlen 3 rlen 0 { align16 1Q EOT }; |
| 197 | }; |
| 198 | |
| 199 | class XglRenderTest : public XglTestFramework |
| 200 | { |
| 201 | public: |
| 202 | void CreateQueryPool(XGL_QUERY_TYPE type, XGL_UINT slots, |
| 203 | XGL_QUERY_POOL *pPool, XGL_GPU_MEMORY *pMem); |
| 204 | void DestroyQueryPool(XGL_QUERY_POOL pool, XGL_GPU_MEMORY mem); |
| 205 | |
| 206 | XGL_DEVICE device() {return m_device->device();} |
| 207 | void InitPipeline(); |
| 208 | void InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, const void* vertices ); |
| 209 | void InitConstantBuffer( int constantCount, int constantSize, const void* data ); |
| 210 | void DrawTriangleTest(); |
| 211 | void DrawRotatedTriangleTest(); |
| 212 | |
| 213 | protected: |
| 214 | XGL_APPLICATION_INFO app_info; |
| 215 | XGL_PHYSICAL_GPU objs[MAX_GPUS]; |
| 216 | XGL_UINT gpu_count; |
| 217 | XGL_GPU_MEMORY m_descriptor_set_mem; |
| 218 | XGL_GPU_MEMORY m_pipe_mem; |
| 219 | XglDevice *m_device; |
| 220 | XGL_CMD_BUFFER m_cmdBuffer; |
| 221 | XGL_UINT32 m_numVertices; |
| 222 | XGL_MEMORY_VIEW_ATTACH_INFO m_vtxBufferView; |
| 223 | XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView; |
| 224 | XGL_GPU_MEMORY m_vtxBufferMem; |
| 225 | XGL_GPU_MEMORY m_constantBufferMem; |
| 226 | XGL_UINT32 m_numMemRefs; |
| 227 | XGL_MEMORY_REF m_memRefs[5]; |
| 228 | XGL_RASTER_STATE_OBJECT m_stateRaster; |
| 229 | XGL_COLOR_BLEND_STATE_OBJECT m_colorBlend; |
| 230 | XGL_VIEWPORT_STATE_OBJECT m_stateViewport; |
| 231 | XGL_DEPTH_STENCIL_STATE_OBJECT m_stateDepthStencil; |
| 232 | XGL_MSAA_STATE_OBJECT m_stateMsaa; |
| 233 | XGL_DESCRIPTOR_SET m_rsrcDescSet; |
| 234 | |
| 235 | virtual void SetUp() { |
| 236 | XGL_RESULT err; |
| 237 | |
| 238 | this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO; |
| 239 | this->app_info.pNext = NULL; |
| 240 | this->app_info.pAppName = (const XGL_CHAR *) "base"; |
| 241 | this->app_info.appVersion = 1; |
| 242 | this->app_info.pEngineName = (const XGL_CHAR *) "unittest"; |
| 243 | this->app_info.engineVersion = 1; |
| 244 | this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0); |
| 245 | |
| 246 | memset(&m_vtxBufferView, 0, sizeof(m_vtxBufferView)); |
| 247 | m_vtxBufferView.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO; |
| 248 | |
| 249 | memset(&m_constantBufferView, 0, sizeof(m_constantBufferView)); |
| 250 | m_constantBufferView.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO; |
| 251 | |
| 252 | err = xglInitAndEnumerateGpus(&app_info, NULL, |
| 253 | MAX_GPUS, &this->gpu_count, objs); |
| 254 | ASSERT_XGL_SUCCESS(err); |
| 255 | ASSERT_GE(1, this->gpu_count) << "No GPU available"; |
| 256 | |
| 257 | m_device = new XglDevice(0, objs[0]); |
| 258 | m_device->get_device_queue(); |
| 259 | } |
| 260 | |
| 261 | virtual void TearDown() { |
| 262 | xglInitAndEnumerateGpus(&this->app_info, XGL_NULL_HANDLE, 0, &gpu_count, XGL_NULL_HANDLE); |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | |
| 267 | void XglRenderTest::CreateQueryPool(XGL_QUERY_TYPE type, XGL_UINT slots, |
| 268 | XGL_QUERY_POOL *pPool, XGL_GPU_MEMORY *pMem) |
| 269 | { |
| 270 | XGL_RESULT err; |
| 271 | |
| 272 | XGL_QUERY_POOL_CREATE_INFO poolCreateInfo = {}; |
| 273 | poolCreateInfo.sType = XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; |
| 274 | poolCreateInfo.pNext = NULL; |
| 275 | poolCreateInfo.queryType = type; |
| 276 | poolCreateInfo.slots = slots; |
| 277 | |
| 278 | err = xglCreateQueryPool(device(), &poolCreateInfo, pPool); |
| 279 | ASSERT_XGL_SUCCESS(err); |
| 280 | |
| 281 | XGL_MEMORY_REQUIREMENTS mem_req; |
| 282 | XGL_UINT data_size = sizeof(mem_req); |
| 283 | err = xglGetObjectInfo(*pPool, XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 284 | &data_size, &mem_req); |
| 285 | ASSERT_XGL_SUCCESS(err); |
| 286 | ASSERT_EQ(data_size, sizeof(mem_req)); |
| 287 | |
| 288 | if (!mem_req.size) { |
| 289 | *pMem = XGL_NULL_HANDLE; |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | XGL_MEMORY_ALLOC_INFO mem_info; |
| 294 | |
| 295 | memset(&mem_info, 0, sizeof(mem_info)); |
| 296 | mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 297 | mem_info.allocationSize = mem_req.size; |
| 298 | mem_info.alignment = mem_req.alignment; |
| 299 | mem_info.heapCount = mem_req.heapCount; |
| 300 | memcpy(mem_info.heaps, mem_req.heaps, sizeof(XGL_UINT)*XGL_MAX_MEMORY_HEAPS); |
| 301 | mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 302 | mem_info.flags = XGL_MEMORY_ALLOC_SHAREABLE_BIT; |
| 303 | err = xglAllocMemory(device(), &mem_info, pMem); |
| 304 | ASSERT_XGL_SUCCESS(err); |
| 305 | |
| 306 | err = xglBindObjectMemory(*pPool, *pMem, 0); |
| 307 | ASSERT_XGL_SUCCESS(err); |
| 308 | } |
| 309 | |
| 310 | void XglRenderTest::DestroyQueryPool(XGL_QUERY_POOL pool, XGL_GPU_MEMORY mem) |
| 311 | { |
| 312 | ASSERT_XGL_SUCCESS(xglBindObjectMemory(pool, XGL_NULL_HANDLE, 0)); |
| 313 | ASSERT_XGL_SUCCESS(xglFreeMemory(mem)); |
| 314 | ASSERT_XGL_SUCCESS(xglDestroyObject(pool)); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | // this function will create the vertex buffer and fill it with the mesh data |
| 319 | void XglRenderTest::InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, |
| 320 | const void* vertices ) |
| 321 | { |
| 322 | XGL_RESULT err = XGL_SUCCESS; |
| 323 | |
| 324 | assert( numVertices * vbStride > 0 ); |
| 325 | m_numVertices = numVertices; |
| 326 | |
| 327 | XGL_MEMORY_ALLOC_INFO alloc_info = {}; |
| 328 | XGL_UINT8 *pData; |
| 329 | |
| 330 | alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 331 | alloc_info.allocationSize = numVertices * vbStride; |
| 332 | alloc_info.alignment = 0; |
| 333 | alloc_info.heapCount = 1; |
| 334 | alloc_info.heaps[0] = 0; // TODO: Use known existing heap |
| 335 | |
| 336 | alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT; |
| 337 | alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 338 | |
| 339 | err = xglAllocMemory(device(), &alloc_info, &m_vtxBufferMem); |
| 340 | ASSERT_XGL_SUCCESS(err); |
| 341 | |
| 342 | err = xglMapMemory(m_vtxBufferMem, 0, (XGL_VOID **) &pData); |
| 343 | ASSERT_XGL_SUCCESS(err); |
| 344 | |
| 345 | memcpy(pData, vertices, alloc_info.allocationSize); |
| 346 | |
| 347 | err = xglUnmapMemory(m_vtxBufferMem); |
| 348 | ASSERT_XGL_SUCCESS(err); |
| 349 | |
| 350 | // set up the memory view for the vertex buffer |
| 351 | this->m_vtxBufferView.stride = vbStride; |
| 352 | this->m_vtxBufferView.range = numVertices * vbStride; |
| 353 | this->m_vtxBufferView.offset = 0; |
| 354 | this->m_vtxBufferView.mem = m_vtxBufferMem; |
| 355 | this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED; |
| 356 | this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED; |
| 357 | |
| 358 | // open the command buffer |
| 359 | err = xglBeginCommandBuffer( m_cmdBuffer, 0 ); |
| 360 | ASSERT_XGL_SUCCESS(err); |
| 361 | |
| 362 | XGL_MEMORY_STATE_TRANSITION transition = {}; |
| 363 | transition.mem = m_vtxBufferMem; |
| 364 | transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER; |
| 365 | transition.newState = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY; |
| 366 | transition.offset = 0; |
| 367 | transition.regionSize = numVertices * vbStride; |
| 368 | |
| 369 | // write transition to the command buffer |
| 370 | xglCmdPrepareMemoryRegions( m_cmdBuffer, 1, &transition ); |
| 371 | this->m_vtxBufferView.state = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY; |
| 372 | |
| 373 | // finish recording the command buffer |
| 374 | err = xglEndCommandBuffer( m_cmdBuffer ); |
| 375 | ASSERT_XGL_SUCCESS(err); |
| 376 | |
| 377 | // this command buffer only uses the vertex buffer memory |
| 378 | m_numMemRefs = 1; |
| 379 | m_memRefs[0].flags = 0; |
| 380 | m_memRefs[0].mem = m_vtxBufferMem; |
| 381 | |
| 382 | // submit the command buffer to the universal queue |
| 383 | err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL ); |
| 384 | ASSERT_XGL_SUCCESS(err); |
| 385 | } |
| 386 | |
| 387 | void XglRenderTest::InitConstantBuffer(int constantCount, int constantSize, const void* data) |
| 388 | { |
| 389 | XGL_RESULT err = XGL_SUCCESS; |
| 390 | |
| 391 | XGL_MEMORY_ALLOC_INFO alloc_info = {}; |
| 392 | XGL_UINT8 *pData; |
| 393 | |
| 394 | alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 395 | alloc_info.allocationSize = constantCount * constantSize; |
| 396 | alloc_info.alignment = 0; |
| 397 | alloc_info.heapCount = 1; |
| 398 | alloc_info.heaps[0] = 0; // TODO: Use known existing heap |
| 399 | |
| 400 | alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT; |
| 401 | alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 402 | |
| 403 | err = xglAllocMemory(device(), &alloc_info, &m_constantBufferMem); |
| 404 | ASSERT_XGL_SUCCESS(err); |
| 405 | |
| 406 | err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData); |
| 407 | ASSERT_XGL_SUCCESS(err); |
| 408 | |
| 409 | memcpy(pData, data, alloc_info.allocationSize); |
| 410 | |
| 411 | err = xglUnmapMemory(m_constantBufferMem); |
| 412 | ASSERT_XGL_SUCCESS(err); |
| 413 | |
| 414 | // set up the memory view for the constant buffer |
| 415 | this->m_constantBufferView.stride = 1; |
| 416 | this->m_constantBufferView.range = 16; |
| 417 | this->m_constantBufferView.offset = 0; |
| 418 | this->m_constantBufferView.mem = m_constantBufferMem; |
| 419 | this->m_constantBufferView.format.channelFormat = XGL_CH_FMT_R32G32B32A32; |
| 420 | this->m_constantBufferView.format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 421 | } |
| 422 | |
| 423 | void XglRenderTest::DrawRotatedTriangleTest() |
| 424 | { |
| 425 | // TODO : This test will pass a matrix into VS to affect triangle orientation. |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * DrawTriangleTest deliberately calls each necessary xgl call to |
| 430 | * make it clear what is necessary to render and simplify debugging. |
| 431 | */ |
| 432 | void XglRenderTest::DrawTriangleTest() |
| 433 | { |
| 434 | XGL_PIPELINE pipeline; |
| 435 | XGL_SHADER vs, ps; |
| 436 | XGL_RESULT err; |
| 437 | XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {}; |
| 438 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage; |
| 439 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage; |
| 440 | std::vector<unsigned int> bil; |
| 441 | XGL_SHADER_CREATE_INFO createInfo; |
| 442 | XGL_SHADER shader; |
| 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"; |
| 566 | static const char *vertShader2 = |
| 567 | "#version 330\n" |
| 568 | "out vec4 color;\n" |
| 569 | "out vec4 scale;\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 | " color = colors[int(mod(gl_VertexID, 3))];\n" |
| 580 | " scale = vec4(1.0, 1.0, 1.0, 1.0);\n" |
| 581 | " gl_Position = vec4(vertices[int(mod(gl_VertexID, 3))], 0.0, 1.0);\n" |
| 582 | "}\n"; |
| 583 | |
| 584 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 585 | createInfo.pNext = NULL; |
| 586 | |
| 587 | if (this->m_device->extension_exist("XGL_COMPILE_GLSL")) { |
| 588 | XGL_INTEL_COMPILE_GLSL glsl_header; |
| 589 | |
| 590 | glsl_header.stage = XGL_SHADER_STAGE_VERTEX; |
| 591 | glsl_header.pCode = vertShader2; |
| 592 | // Driver has extended CreateShader to process GLSL |
| 593 | createInfo.sType = (XGL_STRUCTURE_TYPE) XGL_INTEL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 594 | createInfo.pCode = &glsl_header; |
| 595 | createInfo.codeSize = strlen(vertShader2); |
| 596 | createInfo.flags = 0; |
| 597 | } else { |
| 598 | // Use Reference GLSL to BIL compiler |
| 599 | GLSLtoBIL(XGL_SHADER_STAGE_VERTEX, vertShader2, bil); |
| 600 | createInfo.pCode = bil.data(); |
| 601 | createInfo.codeSize = bil.size() * sizeof(unsigned int); |
| 602 | createInfo.flags = 0; |
| 603 | } |
| 604 | |
| 605 | err = xglCreateShader(device(), &createInfo, &vs); |
| 606 | ASSERT_XGL_SUCCESS(err); |
| 607 | |
| 608 | vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 609 | vs_stage.pNext = XGL_NULL_HANDLE; |
| 610 | vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX; |
| 611 | vs_stage.shader.shader = vs; |
| 612 | vs_stage.shader.descriptorSetMapping[0].descriptorCount = 0; |
| 613 | vs_stage.shader.linkConstBufferCount = 0; |
| 614 | vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE; |
| 615 | vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED; |
| 616 | vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0; |
| 617 | |
| 618 | static const char *fragShaderText = |
| 619 | "#version 130\n" |
| 620 | "uniform vec4 foo;\n" |
| 621 | "void main() {\n" |
| 622 | " gl_FragColor = foo;\n" |
| 623 | "}\n"; |
| 624 | static const char *fragShader2 = |
| 625 | "#version 430\n" |
| 626 | "in vec4 color;\n" |
| 627 | "in vec4 scale;\n" |
| 628 | "layout(location = 0) uniform vec4 foo;\n" |
| 629 | "void main() {\n" |
| 630 | " gl_FragColor = color * scale + foo;\n" |
| 631 | "}\n"; |
| 632 | |
| 633 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 634 | createInfo.pNext = NULL; |
| 635 | |
| 636 | if (this->m_device->extension_exist("XGL_COMPILE_GLSL")) { |
| 637 | XGL_INTEL_COMPILE_GLSL glsl_header; |
| 638 | |
| 639 | glsl_header.stage = XGL_SHADER_STAGE_FRAGMENT; |
| 640 | glsl_header.pCode = fragShader2; |
| 641 | // Driver has extended CreateShader to process GLSL |
| 642 | createInfo.sType = (XGL_STRUCTURE_TYPE) XGL_INTEL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 643 | createInfo.pCode = &glsl_header; |
| 644 | createInfo.codeSize = strlen(fragShader2); |
| 645 | createInfo.flags = 0; |
| 646 | } else { |
| 647 | // Use Reference GLSL to BIL compiler |
| 648 | GLSLtoBIL(XGL_SHADER_STAGE_FRAGMENT, fragShader2, bil); |
| 649 | createInfo.pCode = bil.data(); |
| 650 | createInfo.codeSize = bil.size() * sizeof(unsigned int); |
| 651 | createInfo.flags = 0; |
| 652 | } |
| 653 | |
| 654 | err = xglCreateShader(device(), &createInfo, &ps); |
| 655 | ASSERT_XGL_SUCCESS(err); |
| 656 | |
| 657 | ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 658 | ps_stage.pNext = &vs_stage; |
| 659 | ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT; |
| 660 | ps_stage.shader.shader = ps; |
| 661 | |
| 662 | const int slots = 1; |
| 663 | XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( slots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) ); |
| 664 | slotInfo[0].shaderEntityIndex = 0; |
| 665 | slotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE; |
| 666 | |
| 667 | ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo; |
| 668 | ps_stage.shader.descriptorSetMapping[0].descriptorCount = 1; |
| 669 | |
| 670 | ps_stage.shader.linkConstBufferCount = 0; |
| 671 | ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE; |
| 672 | ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED; |
| 673 | ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0; |
| 674 | |
| 675 | XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = { |
| 676 | XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType |
| 677 | &ps_stage, // pNext |
| 678 | XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY |
| 679 | XGL_FALSE, // disableVertexReuse |
| 680 | XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION |
| 681 | XGL_FALSE, // primitiveRestartEnable |
| 682 | 0 // primitiveRestartIndex |
| 683 | }; |
| 684 | |
| 685 | XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = { |
| 686 | XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO, |
| 687 | &ia_state, |
| 688 | XGL_FALSE, // depthClipEnable |
| 689 | XGL_FALSE, // rasterizerDiscardEnable |
| 690 | 1.0 // pointSize |
| 691 | }; |
| 692 | |
| 693 | XGL_PIPELINE_CB_STATE cb_state = { |
| 694 | XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO, |
| 695 | &rs_state, |
| 696 | XGL_FALSE, // alphaToCoverageEnable |
| 697 | XGL_FALSE, // dualSourceBlendEnable |
| 698 | XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP |
| 699 | { // XGL_PIPELINE_CB_ATTACHMENT_STATE |
| 700 | { |
| 701 | XGL_FALSE, // blendEnable |
| 702 | {XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM}, // XGL_FORMAT |
| 703 | 0xF // channelWriteMask |
| 704 | } |
| 705 | } |
| 706 | }; |
| 707 | |
| 708 | // TODO: Should take depth buffer format from queried formats |
| 709 | XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = { |
| 710 | XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO, |
| 711 | &cb_state, |
| 712 | {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT |
| 713 | }; |
| 714 | |
| 715 | info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 716 | info.pNext = &db_state; |
| 717 | info.flags = 0; |
| 718 | err = xglCreateGraphicsPipeline(device(), &info, &pipeline); |
| 719 | ASSERT_XGL_SUCCESS(err); |
| 720 | |
| 721 | err = m_device->AllocAndBindGpuMemory(pipeline, "Pipeline", &m_pipe_mem); |
| 722 | ASSERT_XGL_SUCCESS(err); |
| 723 | |
| 724 | /* |
| 725 | * Shaders are now part of the pipeline, don't need these anymore |
| 726 | */ |
| 727 | ASSERT_XGL_SUCCESS(xglDestroyObject(ps)); |
| 728 | ASSERT_XGL_SUCCESS(xglDestroyObject(vs)); |
| 729 | |
| 730 | XGL_QUERY_POOL query; |
| 731 | XGL_GPU_MEMORY query_mem; |
| 732 | ASSERT_NO_FATAL_FAILURE(CreateQueryPool(XGL_QUERY_PIPELINE_STATISTICS, 1, &query, &query_mem)); |
| 733 | |
| 734 | XglImage *renderTarget; |
| 735 | XGL_FORMAT fmt = { |
| 736 | XGL_CH_FMT_R8G8B8A8, |
| 737 | XGL_NUM_FMT_UNORM |
| 738 | }; |
| 739 | renderTarget = new XglImage(m_device); |
| 740 | renderTarget->init(width, height, fmt, XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | |
| 741 | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); |
| 742 | // ASSERT_NO_FATAL_FAILURE(m_device->CreateImage(width, height, fmt, |
| 743 | // XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | |
| 744 | // XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
| 745 | // &renderTarget)); |
| 746 | { |
| 747 | XGL_UINT mipCount; |
| 748 | XGL_SIZE size = sizeof(XGL_FORMAT_PROPERTIES); |
| 749 | XGL_FORMAT_PROPERTIES image_fmt; |
| 750 | |
| 751 | mipCount = 0; |
| 752 | |
| 753 | XGL_UINT _w = width; |
| 754 | XGL_UINT _h = height; |
| 755 | while( ( _w > 0 ) || ( _h > 0 ) ) |
| 756 | { |
| 757 | _w >>= 1; |
| 758 | _h >>= 1; |
| 759 | mipCount++; |
| 760 | } |
| 761 | |
| 762 | // TODO: Pick known good format rather than just expect common format |
| 763 | /* |
| 764 | * XXX: What should happen if given NULL HANDLE for the pData argument? |
| 765 | * We're not requesting XGL_INFO_TYPE_MEMORY_REQUIREMENTS so there is |
| 766 | * an expectation that pData is a valid pointer. |
| 767 | * However, why include a returned size value? That implies that the |
| 768 | * amount of data may vary and that doesn't work well for using a |
| 769 | * fixed structure. |
| 770 | */ |
| 771 | |
| 772 | err = xglGetFormatInfo(this->m_device->device(), fmt, |
| 773 | XGL_INFO_TYPE_FORMAT_PROPERTIES, |
| 774 | &size, &image_fmt); |
| 775 | ASSERT_XGL_SUCCESS(err); |
| 776 | |
| 777 | // typedef struct _XGL_IMAGE_CREATE_INFO |
| 778 | // { |
| 779 | // XGL_STRUCTURE_TYPE sType; // Must be XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO |
| 780 | // const XGL_VOID* pNext; // Pointer to next structure. |
| 781 | // XGL_IMAGE_TYPE imageType; |
| 782 | // XGL_FORMAT format; |
| 783 | // XGL_EXTENT3D extent; |
| 784 | // XGL_UINT mipLevels; |
| 785 | // XGL_UINT arraySize; |
| 786 | // XGL_UINT samples; |
| 787 | // XGL_IMAGE_TILING tiling; |
| 788 | // XGL_FLAGS usage; // XGL_IMAGE_USAGE_FLAGS |
| 789 | // XGL_FLAGS flags; // XGL_IMAGE_CREATE_FLAGS |
| 790 | // } XGL_IMAGE_CREATE_INFO; |
| 791 | |
| 792 | |
| 793 | XGL_IMAGE_CREATE_INFO imageCreateInfo = {}; |
| 794 | imageCreateInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 795 | imageCreateInfo.imageType = XGL_IMAGE_2D; |
| 796 | imageCreateInfo.format = fmt; |
| 797 | imageCreateInfo.arraySize = 1; |
| 798 | imageCreateInfo.extent.width = width; |
| 799 | imageCreateInfo.extent.height = height; |
| 800 | imageCreateInfo.extent.depth = 1; |
| 801 | imageCreateInfo.mipLevels = mipCount; |
| 802 | imageCreateInfo.samples = 1; |
| 803 | imageCreateInfo.tiling = XGL_LINEAR_TILING; |
| 804 | |
| 805 | // Image usage flags |
| 806 | // typedef enum _XGL_IMAGE_USAGE_FLAGS |
| 807 | // { |
| 808 | // XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT = 0x00000001, |
| 809 | // XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT = 0x00000002, |
| 810 | // XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT = 0x00000004, |
| 811 | // XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT = 0x00000008, |
| 812 | // } XGL_IMAGE_USAGE_FLAGS; |
| 813 | // imageCreateInfo.usage = XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 814 | imageCreateInfo.usage = XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | |
| 815 | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 816 | |
| 817 | // XGL_RESULT XGLAPI xglCreateImage( |
| 818 | // XGL_DEVICE device, |
| 819 | // const XGL_IMAGE_CREATE_INFO* pCreateInfo, |
| 820 | // XGL_IMAGE* pImage); |
| 821 | err = xglCreateImage(device(), &imageCreateInfo, &m_image); |
| 822 | ASSERT_XGL_SUCCESS(err); |
| 823 | |
| 824 | XGL_MEMORY_REQUIREMENTS mem_req; |
| 825 | XGL_UINT data_size = sizeof(XGL_MEMORY_REQUIREMENTS); |
| 826 | err = xglGetObjectInfo(m_image, XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 827 | &data_size, &mem_req); |
| 828 | ASSERT_XGL_SUCCESS(err); |
| 829 | ASSERT_EQ(data_size, sizeof(mem_req)); |
| 830 | ASSERT_NE(0, mem_req.size) << "xglGetObjectInfo (Event): Failed - expect images to require memory"; |
| 831 | |
| 832 | m_imageInfo.state = XGL_IMAGE_STATE_UNINITIALIZED_TARGET; |
| 833 | |
| 834 | // XGL_RESULT XGLAPI xglAllocMemory( |
| 835 | // XGL_DEVICE device, |
| 836 | // const XGL_MEMORY_ALLOC_INFO* pAllocInfo, |
| 837 | // XGL_GPU_MEMORY* pMem); |
| 838 | XGL_MEMORY_ALLOC_INFO mem_info; |
| 839 | |
| 840 | memset(&mem_info, 0, sizeof(mem_info)); |
| 841 | mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 842 | mem_info.allocationSize = mem_req.size; |
| 843 | mem_info.alignment = mem_req.alignment; |
| 844 | mem_info.heapCount = mem_req.heapCount; |
| 845 | memcpy(mem_info.heaps, mem_req.heaps, sizeof(XGL_UINT)*XGL_MAX_MEMORY_HEAPS); |
| 846 | mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 847 | mem_info.flags = XGL_MEMORY_ALLOC_SHAREABLE_BIT; |
| 848 | err = xglAllocMemory(device(), &mem_info, &m_memory); |
| 849 | ASSERT_XGL_SUCCESS(err); |
| 850 | |
| 851 | err = xglBindObjectMemory(m_image, m_memory, 0); |
| 852 | ASSERT_XGL_SUCCESS(err); |
| 853 | |
| 854 | XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO createView = { |
| 855 | XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO, |
| 856 | XGL_NULL_HANDLE, |
| 857 | m_image, |
| 858 | fmt, |
| 859 | 0, |
| 860 | 0, |
| 861 | 1 |
| 862 | }; |
| 863 | |
| 864 | err = xglCreateColorAttachmentView(device(), &createView, &m_targetView); |
| 865 | ASSERT_XGL_SUCCESS(err); |
| 866 | } |
| 867 | |
| 868 | // Build command buffer |
| 869 | err = xglBeginCommandBuffer(m_cmdBuffer, 0); |
| 870 | ASSERT_XGL_SUCCESS(err); |
| 871 | |
| 872 | // GenerateClearAndPrepareBufferCmds(renderTarget); |
| 873 | { |
| 874 | // whatever we want to do, we do it to the whole buffer |
| 875 | XGL_IMAGE_SUBRESOURCE_RANGE srRange = {}; |
| 876 | srRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 877 | srRange.baseMipLevel = 0; |
| 878 | srRange.mipLevels = XGL_LAST_MIP_OR_SLICE; |
| 879 | srRange.baseArraySlice = 0; |
| 880 | srRange.arraySize = XGL_LAST_MIP_OR_SLICE; |
| 881 | |
| 882 | // prepare the whole back buffer for clear |
| 883 | XGL_IMAGE_STATE_TRANSITION transitionToClear = {}; |
| 884 | transitionToClear.image = renderTarget->image(); |
| 885 | transitionToClear.oldState = renderTarget->state(); |
| 886 | transitionToClear.newState = XGL_IMAGE_STATE_CLEAR; |
| 887 | transitionToClear.subresourceRange = srRange; |
| 888 | xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear ); |
| 889 | renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState); |
| 890 | |
| 891 | // clear the back buffer to dark grey |
| 892 | XGL_UINT clearColor[4] = {64, 64, 64, 0}; |
| 893 | xglCmdClearColorImageRaw( m_cmdBuffer, renderTarget->image(), clearColor, 1, &srRange ); |
| 894 | |
| 895 | // prepare back buffer for rendering |
| 896 | XGL_IMAGE_STATE_TRANSITION transitionToRender = {}; |
| 897 | transitionToRender.image = renderTarget->image(); |
| 898 | transitionToRender.oldState = renderTarget->state(); |
| 899 | transitionToRender.newState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL; |
| 900 | transitionToRender.subresourceRange = srRange; |
| 901 | xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender ); |
| 902 | renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState); |
| 903 | } |
| 904 | |
| 905 | { |
| 906 | // bind render target |
| 907 | XGL_COLOR_ATTACHMENT_BIND_INFO colorBind = {}; |
| 908 | colorBind.view = renderTarget->targetView(); |
| 909 | colorBind.colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL; |
| 910 | xglCmdBindAttachments(m_cmdBuffer, 1, &colorBind, NULL ); |
| 911 | } |
| 912 | |
| 913 | { |
| 914 | // set all states |
| 915 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_RASTER, m_stateRaster ); |
| 916 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_VIEWPORT, m_stateViewport ); |
| 917 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_COLOR_BLEND, m_colorBlend); |
| 918 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil ); |
| 919 | xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_MSAA, m_stateMsaa ); |
| 920 | |
| 921 | // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view) |
| 922 | xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, pipeline ); |
| 923 | xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 ); |
| 924 | } |
| 925 | |
| 926 | xglCmdResetQueryPool(m_cmdBuffer, query, 0, 1); |
| 927 | xglCmdBeginQuery(m_cmdBuffer, query, 0, 0); |
| 928 | |
| 929 | // render the cube |
| 930 | xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 ); |
| 931 | |
| 932 | xglCmdEndQuery(m_cmdBuffer, query, 0); |
| 933 | |
| 934 | // prepare the back buffer for present |
| 935 | // XGL_IMAGE_STATE_TRANSITION transitionToPresent = {}; |
| 936 | // transitionToPresent.image = m_image; |
| 937 | // transitionToPresent.oldState = m_image_state; |
| 938 | // transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT; |
| 939 | // transitionToPresent.subresourceRange = srRange; |
| 940 | // xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent ); |
| 941 | // m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState; |
| 942 | |
| 943 | // finalize recording of the command buffer |
| 944 | err = xglEndCommandBuffer( m_cmdBuffer ); |
| 945 | ASSERT_XGL_SUCCESS( err ); |
| 946 | |
| 947 | // this command buffer only uses the vertex buffer memory |
| 948 | m_numMemRefs = 0; |
| 949 | // m_memRefs[0].flags = 0; |
| 950 | // m_memRefs[0].mem = m_vtxBufferMemory; |
| 951 | |
| 952 | // submit the command buffer to the universal queue |
| 953 | err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL ); |
| 954 | ASSERT_XGL_SUCCESS( err ); |
| 955 | |
| 956 | err = xglQueueWaitIdle( m_device->m_queue ); |
| 957 | ASSERT_XGL_SUCCESS( err ); |
| 958 | |
| 959 | // Wait for work to finish before cleaning up. |
| 960 | xglDeviceWaitIdle(m_device->device()); |
| 961 | |
| 962 | XGL_PIPELINE_STATISTICS_DATA stats; |
| 963 | XGL_SIZE stats_size = sizeof(stats); |
| 964 | err = xglGetQueryPoolResults(query, 0, 1, &stats_size, &stats); |
| 965 | ASSERT_XGL_SUCCESS( err ); |
| 966 | ASSERT_EQ(stats_size, sizeof(stats)); |
| 967 | |
| 968 | ASSERT_EQ(stats.vsInvocations, 3); |
| 969 | ASSERT_EQ(stats.cPrimitives, 1); |
| 970 | ASSERT_EQ(stats.cInvocations, 1); |
| 971 | |
| 972 | DestroyQueryPool(query, query_mem); |
| 973 | |
| 974 | const ::testing::TestInfo* const test_info = |
| 975 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 976 | |
| 977 | // renderTarget->WritePPM(test_info->test_case_name()); |
| 978 | // m_screen.Display(renderTarget, m_image_mem); |
| 979 | RecordImage(renderTarget); |
| 980 | |
| 981 | ASSERT_XGL_SUCCESS(xglDestroyObject(pipeline)); |
| 982 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_cmdBuffer)); |
| 983 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateRaster)); |
| 984 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateViewport)); |
| 985 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateDepthStencil)); |
| 986 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateMsaa)); |
| 987 | free(renderTarget); |
| 988 | } |
| 989 | |
| 990 | |
| 991 | TEST_F(XglRenderTest, TestDrawTriangle) { |
| 992 | DrawTriangleTest(); |
| 993 | } |
| 994 | |
| 995 | int main(int argc, char **argv) { |
| 996 | int result; |
| 997 | |
| 998 | ::testing::InitGoogleTest(&argc, argv); |
| 999 | XglTestFramework::InitArgs(&argc, argv); |
| 1000 | |
| 1001 | ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment); |
| 1002 | |
| 1003 | result = RUN_ALL_TESTS(); |
| 1004 | |
| 1005 | XglTestFramework::Finish(); |
| 1006 | return result; |
| 1007 | } |