blob: 530d78b656b3da31caeb1e87632a45f3324c6fbf [file] [log] [blame]
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06001// 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// Basic rendering tests
55
56#include <stdlib.h>
57#include <stdio.h>
58#include <stdbool.h>
59#include <string.h>
Courtney Goeltzenleuchter76a643b2014-08-21 17:34:22 -060060#include <iostream>
61#include <fstream>
62using namespace std;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060063
64#include <xgl.h>
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -060065#include <xglIntelExt.h>
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060066#include "gtest-1.7.0/include/gtest/gtest.h"
67
68#include "xgldevice.h"
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -060069#include "xglimage.h"
Chia-I Wu4115c892014-08-28 11:56:29 +080070#include "icd-bil.h"
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060071
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -060072#include "xgltestframework.h"
Courtney Goeltzenleuchter2268d1e2014-09-01 13:57:15 -060073
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -060074//--------------------------------------------------------------------------------------
75// Mesh and VertexFormat Data
76//--------------------------------------------------------------------------------------
77struct Vertex
78{
79 XGL_FLOAT posX, posY, posZ, posW; // Position data
80 XGL_FLOAT r, g, b, a; // Color
81};
82
83#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
84
85static const Vertex g_vbData[] =
86{
87 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
88 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
89 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
90 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
91 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
92 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
93
94 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
95 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
96 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
97 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
98 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
99 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
100
101 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
102 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
103 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
104 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
105 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
106 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
107
108 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
109 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
110 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
111 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
112 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
113 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
114
115 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
116 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
117 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
118 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
119 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
120 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
121
122 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
123 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
124 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
125 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
126 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
127 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
128};
129
Chia-I Wu6e9789c2014-08-30 14:03:32 +0800130static const uint32_t gen6_fs[] = {
131 0x00600001, 0x202003fe, 0x00000000, 0x3f800000, // mov(8) m1<1>F 1F { align1 1Q };
132 0x00600001, 0x204003fe, 0x00000000, 0x00000000, // mov(8) m2<1>F 0F { align1 1Q };
133 0x00600001, 0x206003fe, 0x00000000, 0x00000000, // mov(8) m3<1>F 0F { align1 1Q };
134 0x00600001, 0x208003fe, 0x00000000, 0x3f800000, // mov(8) m4<1>F 1F { align1 1Q };
135 0x05600032, 0x20001fc8, 0x008d0020, 0x88019400, // sendc(8) null m1<8,8,1>F
136 // render RT write SIMD8 LastRT Surface = 0 mlen 4 rlen 0 { align1 1Q EOT };
137};
138
139static const uint32_t gen6_vs[] = {
140 0x01600110, 0x200f1ca4, 0x00600020, 0x00000000, // cmp.z.f0(8) null g1<4,4,1>.xD 0D { align16 1Q };
141 0x00670122, 0x000a108f, 0x000e0004, 0x000e0004, // (+f0.all4h) if(8) JIP: 10 { align16 1Q };
142 0x00600501, 0x204303fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.xyF -1F { align16 NoDDClr 1Q };
143 0x00600d01, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDClr,NoDDChk 1Q };
144 0x00600901, 0x204803fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.wF 1F { align16 NoDDChk 1Q };
145 0x00600124, 0x0014108f, 0x006e0004, 0x006e0004, // else(8) JIP: 20 { align16 1Q };
146 0x01600110, 0x200f1ca4, 0x00600020, 0x00000001, // cmp.z.f0(8) null g1<4,4,1>.xD 1D { align16 1Q };
147 0x00670122, 0x000a108f, 0x000e0004, 0x000e0004, // (+f0.all4h) if(8) JIP: 10 { align16 1Q };
148 0x00600501, 0x204903fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.xwF 1F { align16 NoDDClr 1Q };
149 0x00600d01, 0x204203fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.yF -1F { align16 NoDDClr,NoDDChk 1Q };
150 0x00600901, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDChk 1Q };
151 0x00600124, 0x0006108f, 0x006e0004, 0x006e0004, // else(8) JIP: 6 { align16 1Q };
152 0x00600501, 0x204503fd, 0x00000000, 0x00000000, // mov(8) g2<1>.xzF 0F { align16 NoDDClr 1Q };
153 0x00600901, 0x204a03fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.ywF 1F { align16 NoDDChk 1Q };
154 0x00600125, 0x0002108f, 0x006e0004, 0x006e0002, // endif(8) JIP: 2 { align16 1Q };
155 0x00600125, 0x0002108f, 0x006e0004, 0x006e0002, // endif(8) JIP: 2 { align16 1Q };
156 0x00600101, 0x204f0062, 0x00000000, 0x00000000, // mov(8) m2<1>UD 0x00000000UD { align16 1Q };
157 0x00600101, 0x206f03be, 0x006e0044, 0x00000000, // mov(8) m3<1>F g2<4,4,1>F { align16 1Q };
158 0x00600301, 0x202f0022, 0x006e0004, 0x00000000, // mov(8) m1<1>UD g0<4,4,1>UD { align16 WE_all 1Q };
159 0x06600131, 0x200f1fdc, 0x006e0024, 0x8608c400, // send(8) null m1<4,4,1>F
160 // urb 0 urb_write interleave used complete mlen 3 rlen 0 { align16 1Q EOT };
161};
162
163static const uint32_t gen7_fs[] = {
164 0x00600001, 0x2e2003fd, 0x00000000, 0x3f800000, // mov(8) g113<1>F 1F { align1 1Q };
165 0x00600001, 0x2e4003fd, 0x00000000, 0x00000000, // mov(8) g114<1>F 0F { align1 1Q };
166 0x00600001, 0x2e6003fd, 0x00000000, 0x00000000, // mov(8) g115<1>F 0F { align1 1Q };
167 0x00600001, 0x2e8003fd, 0x00000000, 0x3f800000, // mov(8) g116<1>F 1F { align1 1Q };
168 0x05600032, 0x20001fa8, 0x008d0e20, 0x88031400, // sendc(8) null g113<8,8,1>F
169 // render RT write SIMD8 LastRT Surface = 0 mlen 4 rlen 0 { align1 1Q EOT };
170};
171
172static const uint32_t gen7_vs[] = {
173 0x01608110, 0x200f1ca4, 0x00600020, 0x00000000, // cmp.z.f0(8) null g1<4,4,1>.xD 0D { align16 1Q switch };
174 0x00670122, 0x200f0c84, 0x000e0004, 0x001c000a, // (+f0.all4h) if(8) JIP: 10 UIP: 28 { align16 1Q };
175 0x00600501, 0x204303fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.xyF -1F { align16 NoDDClr 1Q };
176 0x00600d01, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDClr,NoDDChk 1Q };
177 0x00600901, 0x204803fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.wF 1F { align16 NoDDChk 1Q };
178 0x00600124, 0x200f0c84, 0x006e0004, 0x00000014, // else(8) JIP: 20 { align16 1Q };
179 0x01608110, 0x200f1ca4, 0x00600020, 0x00000001, // cmp.z.f0(8) null g1<4,4,1>.xD 1D { align16 1Q switch };
180 0x00670122, 0x200f0c84, 0x000e0004, 0x000e000a, // (+f0.all4h) if(8) JIP: 10 UIP: 14 { align16 1Q };
181 0x00600501, 0x204903fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.xwF 1F { align16 NoDDClr 1Q };
182 0x00600d01, 0x204203fd, 0x00000000, 0xbf800000, // mov(8) g2<1>.yF -1F { align16 NoDDClr,NoDDChk 1Q };
183 0x00600901, 0x204403fd, 0x00000000, 0x00000000, // mov(8) g2<1>.zF 0F { align16 NoDDChk 1Q };
184 0x00600124, 0x200f0c84, 0x006e0004, 0x00000006, // else(8) JIP: 6 { align16 1Q };
185 0x00600501, 0x204503fd, 0x00000000, 0x00000000, // mov(8) g2<1>.xzF 0F { align16 NoDDClr 1Q };
186 0x00600901, 0x204a03fd, 0x00000000, 0x3f800000, // mov(8) g2<1>.ywF 1F { align16 NoDDChk 1Q };
187 0x00600125, 0x200f0c84, 0x006e0004, 0x00000002, // endif(8) JIP: 2 { align16 1Q };
188 0x00600125, 0x200f0c84, 0x006e0004, 0x00000002, // endif(8) JIP: 2 { align16 1Q };
189 0x00600101, 0x2e4f0061, 0x00000000, 0x00000000, // mov(8) g114<1>UD 0x00000000UD { align16 1Q };
190 0x00600101, 0x2e6f03bd, 0x006e0044, 0x00000000, // mov(8) g115<1>F g2<4,4,1>F { align16 1Q };
191 0x00600301, 0x2e2f0021, 0x006e0004, 0x00000000, // mov(8) g113<1>UD g0<4,4,1>UD { align16 WE_all 1Q };
192 0x00000206, 0x2e340c21, 0x00000014, 0x0000ff00, // or(1) g113.5<1>UD g0.5<0,1,0>UD 0x0000ff00UD { align1 WE_all };
193 0x06600131, 0x200f1fbc, 0x006e0e24, 0x8608c000, // send(8) null g113<4,4,1>F
194 // urb 0 write HWord interleave complete mlen 3 rlen 0 { align16 1Q EOT };
195};
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600196
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600197class XglRenderTest : public XglTestFramework
198{
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600199public:
Chia-I Wua3b9d6a2014-08-30 23:53:01 +0800200 void CreateQueryPool(XGL_QUERY_TYPE type, XGL_UINT slots,
201 XGL_QUERY_POOL *pPool, XGL_GPU_MEMORY *pMem);
202 void DestroyQueryPool(XGL_QUERY_POOL pool, XGL_GPU_MEMORY mem);
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600203 void CreateDefaultPipeline(XGL_PIPELINE* pipeline, XGL_SHADER* vs, XGL_SHADER* ps, int width, int height);
204 void GenerateClearAndPrepareBufferCmds(XglImage *renderTarget);
205 void GenerateBindRenderTargetCmd(XglImage *renderTarget);
206 void GenerateBindStateAndPipelineCmds(XGL_PIPELINE* pipeline);
Chia-I Wua3b9d6a2014-08-30 23:53:01 +0800207
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600208 XGL_DEVICE device() {return m_device->device();}
Courtney Goeltzenleuchter94192032014-10-03 09:53:32 -0600209 void CreateShader(XGL_PIPELINE_SHADER_STAGE stage, const char *shader_code, XGL_SHADER *pshader);
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600210 void InitPipeline();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600211 void InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, const void* vertices );
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600212 void InitConstantBuffer( int constantCount, int constantSize, const void* data );
Cody Northrop350727b2014-10-06 15:42:00 -0600213 void InitTexture();
214 void InitSampler();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600215 void DrawTriangleTest();
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600216 void DrawRotatedTriangleTest();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600217
218protected:
219 XGL_APPLICATION_INFO app_info;
220 XGL_PHYSICAL_GPU objs[MAX_GPUS];
221 XGL_UINT gpu_count;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600222 XGL_GPU_MEMORY m_descriptor_set_mem;
223 XGL_GPU_MEMORY m_pipe_mem;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600224 XglDevice *m_device;
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600225 XGL_CMD_BUFFER m_cmdBuffer;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600226 XGL_UINT32 m_numVertices;
227 XGL_MEMORY_VIEW_ATTACH_INFO m_vtxBufferView;
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600228 XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView;
Cody Northrop350727b2014-10-06 15:42:00 -0600229
230 XGL_IMAGE m_texture;
231 XGL_IMAGE_VIEW m_textureView;
232 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
233 XGL_GPU_MEMORY m_textureMem;
234
235 XGL_SAMPLER m_sampler;
236
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600237 XGL_GPU_MEMORY m_vtxBufferMem;
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600238 XGL_GPU_MEMORY m_constantBufferMem;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600239 XGL_UINT32 m_numMemRefs;
240 XGL_MEMORY_REF m_memRefs[5];
241 XGL_RASTER_STATE_OBJECT m_stateRaster;
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600242 XGL_COLOR_BLEND_STATE_OBJECT m_colorBlend;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600243 XGL_VIEWPORT_STATE_OBJECT m_stateViewport;
244 XGL_DEPTH_STENCIL_STATE_OBJECT m_stateDepthStencil;
245 XGL_MSAA_STATE_OBJECT m_stateMsaa;
246 XGL_DESCRIPTOR_SET m_rsrcDescSet;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600247
248 virtual void SetUp() {
249 XGL_RESULT err;
250
251 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
252 this->app_info.pNext = NULL;
253 this->app_info.pAppName = (const XGL_CHAR *) "base";
254 this->app_info.appVersion = 1;
255 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
256 this->app_info.engineVersion = 1;
257 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
258
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600259 memset(&m_vtxBufferView, 0, sizeof(m_vtxBufferView));
260 m_vtxBufferView.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO;
261
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600262 memset(&m_constantBufferView, 0, sizeof(m_constantBufferView));
263 m_constantBufferView.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO;
264
Cody Northrop350727b2014-10-06 15:42:00 -0600265 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
266 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
267
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600268 err = xglInitAndEnumerateGpus(&app_info, NULL,
269 MAX_GPUS, &this->gpu_count, objs);
270 ASSERT_XGL_SUCCESS(err);
271 ASSERT_GE(1, this->gpu_count) << "No GPU available";
272
273 m_device = new XglDevice(0, objs[0]);
274 m_device->get_device_queue();
275 }
276
277 virtual void TearDown() {
Tobin Ehlisa9b13542014-10-23 13:45:13 -0600278 m_device->destroy_device();
Tobin Ehlisdcd421a2014-10-23 15:18:50 -0600279 xglInitAndEnumerateGpus(&this->app_info, XGL_NULL_HANDLE, 0, &gpu_count, XGL_NULL_HANDLE);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600280 }
281};
282
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600283
Chia-I Wua3b9d6a2014-08-30 23:53:01 +0800284void XglRenderTest::CreateQueryPool(XGL_QUERY_TYPE type, XGL_UINT slots,
285 XGL_QUERY_POOL *pPool, XGL_GPU_MEMORY *pMem)
286{
287 XGL_RESULT err;
288
289 XGL_QUERY_POOL_CREATE_INFO poolCreateInfo = {};
290 poolCreateInfo.sType = XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
291 poolCreateInfo.pNext = NULL;
292 poolCreateInfo.queryType = type;
293 poolCreateInfo.slots = slots;
294
295 err = xglCreateQueryPool(device(), &poolCreateInfo, pPool);
296 ASSERT_XGL_SUCCESS(err);
297
298 XGL_MEMORY_REQUIREMENTS mem_req;
Jon Ashburnb8e43892014-09-25 14:36:58 -0600299 XGL_UINT data_size = sizeof(mem_req);
Chia-I Wua3b9d6a2014-08-30 23:53:01 +0800300 err = xglGetObjectInfo(*pPool, XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
301 &data_size, &mem_req);
302 ASSERT_XGL_SUCCESS(err);
303 ASSERT_EQ(data_size, sizeof(mem_req));
304
305 if (!mem_req.size) {
306 *pMem = XGL_NULL_HANDLE;
307 return;
308 }
309
310 XGL_MEMORY_ALLOC_INFO mem_info;
311
312 memset(&mem_info, 0, sizeof(mem_info));
313 mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
314 mem_info.allocationSize = mem_req.size;
315 mem_info.alignment = mem_req.alignment;
316 mem_info.heapCount = mem_req.heapCount;
317 memcpy(mem_info.heaps, mem_req.heaps, sizeof(XGL_UINT)*XGL_MAX_MEMORY_HEAPS);
318 mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
319 mem_info.flags = XGL_MEMORY_ALLOC_SHAREABLE_BIT;
320 err = xglAllocMemory(device(), &mem_info, pMem);
321 ASSERT_XGL_SUCCESS(err);
322
323 err = xglBindObjectMemory(*pPool, *pMem, 0);
324 ASSERT_XGL_SUCCESS(err);
325}
326
327void XglRenderTest::DestroyQueryPool(XGL_QUERY_POOL pool, XGL_GPU_MEMORY mem)
328{
329 ASSERT_XGL_SUCCESS(xglBindObjectMemory(pool, XGL_NULL_HANDLE, 0));
330 ASSERT_XGL_SUCCESS(xglFreeMemory(mem));
331 ASSERT_XGL_SUCCESS(xglDestroyObject(pool));
332}
333
Courtney Goeltzenleuchter94192032014-10-03 09:53:32 -0600334void XglRenderTest::CreateShader(XGL_PIPELINE_SHADER_STAGE stage,
335 const char *shader_code,
336 XGL_SHADER *pshader)
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600337{
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600338 XGL_RESULT err;
Courtney Goeltzenleuchter94192032014-10-03 09:53:32 -0600339 std::vector<unsigned int> bil;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600340 XGL_SHADER_CREATE_INFO createInfo;
341 XGL_SHADER shader;
Courtney Goeltzenleuchter76a643b2014-08-21 17:34:22 -0600342
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600343 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
344 createInfo.pNext = NULL;
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600345
346 if (this->m_device->extension_exist("XGL_COMPILE_GLSL")) {
Courtney Goeltzenleuchterd6f75892014-10-06 12:45:37 -0600347 XGL_INTEL_COMPILE_GLSL glsl_header;
348
349 glsl_header.stage = stage;
350 glsl_header.pCode = shader_code;
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600351 // Driver has extended CreateShader to process GLSL
352 createInfo.sType = (XGL_STRUCTURE_TYPE) XGL_INTEL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchterd6f75892014-10-06 12:45:37 -0600353 createInfo.pCode = &glsl_header;
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -0600354 createInfo.codeSize = strlen(shader_code);
355 createInfo.flags = 0;
356 } else {
357 // Use Reference GLSL to BIL compiler
358 GLSLtoBIL(stage, shader_code, bil);
359 createInfo.pCode = bil.data();
360 createInfo.codeSize = bil.size() * sizeof(unsigned int);
361 createInfo.flags = 0;
362 }
363
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600364 err = xglCreateShader(device(), &createInfo, &shader);
365 ASSERT_XGL_SUCCESS(err);
Courtney Goeltzenleuchter76a643b2014-08-21 17:34:22 -0600366
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600367 *pshader = shader;
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600368}
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600369
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600370// this function will create the vertex buffer and fill it with the mesh data
371void XglRenderTest::InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride,
372 const void* vertices )
373{
374 XGL_RESULT err = XGL_SUCCESS;
375
376 assert( numVertices * vbStride > 0 );
377 m_numVertices = numVertices;
378
379 XGL_MEMORY_ALLOC_INFO alloc_info = {};
380 XGL_UINT8 *pData;
381
382 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
383 alloc_info.allocationSize = numVertices * vbStride;
384 alloc_info.alignment = 0;
385 alloc_info.heapCount = 1;
386 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
387
388 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
389 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
390
391 err = xglAllocMemory(device(), &alloc_info, &m_vtxBufferMem);
392 ASSERT_XGL_SUCCESS(err);
393
394 err = xglMapMemory(m_vtxBufferMem, 0, (XGL_VOID **) &pData);
395 ASSERT_XGL_SUCCESS(err);
396
397 memcpy(pData, vertices, alloc_info.allocationSize);
398
399 err = xglUnmapMemory(m_vtxBufferMem);
400 ASSERT_XGL_SUCCESS(err);
401
402 // set up the memory view for the vertex buffer
403 this->m_vtxBufferView.stride = vbStride;
404 this->m_vtxBufferView.range = numVertices * vbStride;
405 this->m_vtxBufferView.offset = 0;
406 this->m_vtxBufferView.mem = m_vtxBufferMem;
407 this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED;
408 this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED;
409
410 // open the command buffer
411 err = xglBeginCommandBuffer( m_cmdBuffer, 0 );
412 ASSERT_XGL_SUCCESS(err);
413
414 XGL_MEMORY_STATE_TRANSITION transition = {};
415 transition.mem = m_vtxBufferMem;
416 transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER;
417 transition.newState = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
418 transition.offset = 0;
419 transition.regionSize = numVertices * vbStride;
420
421 // write transition to the command buffer
422 xglCmdPrepareMemoryRegions( m_cmdBuffer, 1, &transition );
423 this->m_vtxBufferView.state = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
424
425 // finish recording the command buffer
426 err = xglEndCommandBuffer( m_cmdBuffer );
427 ASSERT_XGL_SUCCESS(err);
428
429 // this command buffer only uses the vertex buffer memory
430 m_numMemRefs = 1;
431 m_memRefs[0].flags = 0;
432 m_memRefs[0].mem = m_vtxBufferMem;
433
434 // submit the command buffer to the universal queue
435 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
436 ASSERT_XGL_SUCCESS(err);
437}
438
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600439void XglRenderTest::InitConstantBuffer(int constantCount, int constantSize, const void* data)
440{
441 XGL_RESULT err = XGL_SUCCESS;
442
443 XGL_MEMORY_ALLOC_INFO alloc_info = {};
444 XGL_UINT8 *pData;
445
446 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
447 alloc_info.allocationSize = constantCount * constantSize;
448 alloc_info.alignment = 0;
449 alloc_info.heapCount = 1;
450 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
451
452 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
453 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
454
455 err = xglAllocMemory(device(), &alloc_info, &m_constantBufferMem);
456 ASSERT_XGL_SUCCESS(err);
457
458 err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData);
459 ASSERT_XGL_SUCCESS(err);
460
461 memcpy(pData, data, alloc_info.allocationSize);
462
463 err = xglUnmapMemory(m_constantBufferMem);
464 ASSERT_XGL_SUCCESS(err);
465
466 // set up the memory view for the constant buffer
467 this->m_constantBufferView.stride = 1;
468 this->m_constantBufferView.range = 16;
469 this->m_constantBufferView.offset = 0;
470 this->m_constantBufferView.mem = m_constantBufferMem;
471 this->m_constantBufferView.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
472 this->m_constantBufferView.format.numericFormat = XGL_NUM_FMT_FLOAT;
473}
474
Cody Northrop350727b2014-10-06 15:42:00 -0600475void XglRenderTest::InitTexture()
476{
Cody Northrop904742c2014-10-07 16:25:00 -0600477#define DEMO_TEXTURE_COUNT 1
478
479 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
480 const XGL_INT tex_width = 16;
481 const XGL_INT tex_height = 16;
482 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
483 { 0xffff0000, 0xff00ff00 },
484 };
Cody Northrop350727b2014-10-06 15:42:00 -0600485 XGL_RESULT err;
Cody Northrop904742c2014-10-07 16:25:00 -0600486 XGL_UINT i;
Cody Northrop350727b2014-10-06 15:42:00 -0600487
Cody Northrop904742c2014-10-07 16:25:00 -0600488 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
489 const XGL_SAMPLER_CREATE_INFO sampler = {
490 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
491 .pNext = NULL,
492 .magFilter = XGL_TEX_FILTER_NEAREST,
493 .minFilter = XGL_TEX_FILTER_NEAREST,
494 .mipMode = XGL_TEX_MIPMAP_BASE,
495 .addressU = XGL_TEX_ADDRESS_WRAP,
496 .addressV = XGL_TEX_ADDRESS_WRAP,
497 .addressW = XGL_TEX_ADDRESS_WRAP,
498 .mipLodBias = 0.0f,
499 .maxAnisotropy = 0,
500 .compareFunc = XGL_COMPARE_NEVER,
501 .minLod = 0.0f,
502 .maxLod = 0.0f,
503 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
504 };
505 const XGL_IMAGE_CREATE_INFO image = {
506 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
507 .pNext = NULL,
508 .imageType = XGL_IMAGE_2D,
509 .format = tex_format,
510 .extent = { tex_width, tex_height, 1 },
511 .mipLevels = 1,
512 .arraySize = 1,
513 .samples = 1,
514 .tiling = XGL_LINEAR_TILING,
515 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
516 .flags = 0,
517 };
518 XGL_MEMORY_ALLOC_INFO mem_alloc;
519 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
520 mem_alloc.pNext = NULL;
521 mem_alloc.allocationSize = 0;
522 mem_alloc.alignment = 0;
523 mem_alloc.flags = 0;
524 mem_alloc.heapCount = 0;
525 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
526 XGL_IMAGE_VIEW_CREATE_INFO view;
527 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
528 view.pNext = NULL;
529 view.image = XGL_NULL_HANDLE;
530 view.viewType = XGL_IMAGE_VIEW_2D;
531 view.format = image.format;
532 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
533 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
534 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
535 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
536 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
537 view.subresourceRange.baseMipLevel = 0;
538 view.subresourceRange.mipLevels = 1;
539 view.subresourceRange.baseArraySlice = 0;
540 view.subresourceRange.arraySize = 1;
541 view.minLod = 0.0f;
Cody Northrop350727b2014-10-06 15:42:00 -0600542
Cody Northrop904742c2014-10-07 16:25:00 -0600543 XGL_MEMORY_REQUIREMENTS mem_reqs;
544 XGL_SIZE mem_reqs_size;
Cody Northrop350727b2014-10-06 15:42:00 -0600545
Cody Northrop904742c2014-10-07 16:25:00 -0600546 /* create sampler */
547 err = xglCreateSampler(device(), &sampler, &m_sampler);
548 assert(!err);
549
550 /* create image */
551 err = xglCreateImage(device(), &image, &m_texture);
552 assert(!err);
553
554 err = xglGetObjectInfo(m_texture,
555 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
556 &mem_reqs_size, &mem_reqs);
557 assert(!err && mem_reqs_size == sizeof(mem_reqs));
558
559 mem_alloc.allocationSize = mem_reqs.size;
560 mem_alloc.alignment = mem_reqs.alignment;
561 mem_alloc.heapCount = mem_reqs.heapCount;
562 memcpy(mem_alloc.heaps, mem_reqs.heaps,
563 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
564
565 /* allocate memory */
566 err = xglAllocMemory(device(), &mem_alloc, &m_textureMem);
567 assert(!err);
568
569 /* bind memory */
570 err = xglBindObjectMemory(m_texture, m_textureMem, 0);
571 assert(!err);
572
573 /* create image view */
574 view.image = m_texture;
575 err = xglCreateImageView(device(), &view, &m_textureView);
576 assert(!err);
Cody Northrop350727b2014-10-06 15:42:00 -0600577 }
578
Cody Northrop904742c2014-10-07 16:25:00 -0600579 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
580 const XGL_IMAGE_SUBRESOURCE subres = {
581 .aspect = XGL_IMAGE_ASPECT_COLOR,
582 .mipLevel = 0,
583 .arraySlice = 0,
584 };
585 XGL_SUBRESOURCE_LAYOUT layout;
586 XGL_SIZE layout_size;
587 XGL_VOID *data;
588 XGL_INT x, y;
Cody Northrop350727b2014-10-06 15:42:00 -0600589
Cody Northrop904742c2014-10-07 16:25:00 -0600590 err = xglGetImageSubresourceInfo(m_texture, &subres,
591 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
592 assert(!err && layout_size == sizeof(layout));
Cody Northrop350727b2014-10-06 15:42:00 -0600593
Cody Northrop904742c2014-10-07 16:25:00 -0600594 err = xglMapMemory(m_textureMem, 0, &data);
595 assert(!err);
Cody Northrop350727b2014-10-06 15:42:00 -0600596
Cody Northrop904742c2014-10-07 16:25:00 -0600597 for (y = 0; y < tex_height; y++) {
598 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
599 for (x = 0; x < tex_width; x++)
600 row[x] = tex_colors[i][(x & 1) ^ (y & 1)];
Cody Northrop350727b2014-10-06 15:42:00 -0600601 }
Cody Northrop904742c2014-10-07 16:25:00 -0600602
603 err = xglUnmapMemory(m_textureMem);
604 assert(!err);
Cody Northrop350727b2014-10-06 15:42:00 -0600605 }
606
Cody Northrop350727b2014-10-06 15:42:00 -0600607 m_textureViewInfo.view = m_textureView;
608}
609
610void XglRenderTest::InitSampler()
611{
612 XGL_RESULT err;
613
614 XGL_SAMPLER_CREATE_INFO samplerCreateInfo = {};
615 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
616 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
617 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
618 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
619 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
620 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
621 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
622 samplerCreateInfo.mipLodBias = 0.0;
623 samplerCreateInfo.maxAnisotropy = 0.0;
624 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
625 samplerCreateInfo.minLod = 0.0;
626 samplerCreateInfo.maxLod = 0.0;
627 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
628
629 err = xglCreateSampler(device(),&samplerCreateInfo, &m_sampler);
630 ASSERT_XGL_SUCCESS(err);
631}
632
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600633void XglRenderTest::CreateDefaultPipeline(XGL_PIPELINE* pipeline, XGL_SHADER* vs, XGL_SHADER* ps, int width, int height)
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600634{
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600635 XGL_RESULT err;
636 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600637 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
638 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600639
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600640 // create a raster state (solid, back-face culling)
641 XGL_RASTER_STATE_CREATE_INFO raster = {};
642 raster.sType = XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO;
643 raster.fillMode = XGL_FILL_SOLID;
Chia-I Wu0059c372014-08-31 00:52:42 +0800644 raster.cullMode = XGL_CULL_NONE;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600645 raster.frontFace = XGL_FRONT_FACE_CCW;
646 err = xglCreateRasterState( device(), &raster, &m_stateRaster );
647 ASSERT_XGL_SUCCESS(err);
648
649 XGL_VIEWPORT_STATE_CREATE_INFO viewport = {};
650 viewport.viewportCount = 1;
651 viewport.scissorEnable = XGL_FALSE;
652 viewport.viewports[0].originX = 0;
653 viewport.viewports[0].originY = 0;
654 viewport.viewports[0].width = 1.f * width;
655 viewport.viewports[0].height = 1.f * height;
656 viewport.viewports[0].minDepth = 0.f;
657 viewport.viewports[0].maxDepth = 1.f;
658
659 err = xglCreateViewportState( device(), &viewport, &m_stateViewport );
660 ASSERT_XGL_SUCCESS( err );
661
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600662 XGL_COLOR_BLEND_STATE_CREATE_INFO blend = {};
663 blend.sType = XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO;
664 err = xglCreateColorBlendState(device(), &blend, &m_colorBlend);
665 ASSERT_XGL_SUCCESS( err );
666
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600667 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
668 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
669 depthStencil.depthTestEnable = XGL_FALSE;
670 depthStencil.depthWriteEnable = XGL_FALSE;
671 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
672 depthStencil.depthBoundsEnable = XGL_FALSE;
673 depthStencil.minDepth = 0.f;
674 depthStencil.maxDepth = 1.f;
675 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
676 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
677 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
678 depthStencil.back.stencilRef = 0x00;
679 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
680 depthStencil.front = depthStencil.back;
681
682 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
683 ASSERT_XGL_SUCCESS( err );
684
685 XGL_MSAA_STATE_CREATE_INFO msaa = {};
686 msaa.sType = XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO;
687 msaa.sampleMask = 1;
688 msaa.samples = 1;
689
690 err = xglCreateMsaaState( device(), &msaa, &m_stateMsaa );
691 ASSERT_XGL_SUCCESS( err );
692
693 XGL_CMD_BUFFER_CREATE_INFO cmdInfo = {};
694
695 cmdInfo.sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO;
696 cmdInfo.queueType = XGL_QUEUE_TYPE_GRAPHICS;
697 err = xglCreateCommandBuffer(device(), &cmdInfo, &m_cmdBuffer);
698 ASSERT_XGL_SUCCESS(err) << "xglCreateCommandBuffer failed";
699
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600700#if 0
701 // Create descriptor set for our one resource
702 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
703 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
704 descriptorInfo.slots = 1; // Vertex buffer only
705
706 // create a descriptor set with a single slot
707 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
708 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
709
710 // bind memory to the descriptor set
711 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
712
713 // set up the memory view for the vertex buffer
714 this->m_vtxBufferView.stride = vbStride;
715 this->m_vtxBufferView.range = numVertices * vbStride;
716 this->m_vtxBufferView.offset = 0;
717 this->m_vtxBufferView.mem = m_vtxBufferMem;
718 this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED;
719 this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED;
720 // write the vertex buffer view to the descriptor set
721 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
722 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_vtxBufferView );
723 xglEndDescriptorSetUpdate( m_rsrcDescSet );
724#endif
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600725
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600726 const int constantCount = 4;
Cody Northrop8d177d42014-10-06 17:07:40 -0600727 const float constants[constantCount] = { 0.0, 0.0, 0.0, 0.0 };
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600728 InitConstantBuffer(constantCount, sizeof(constants[0]), (const void*) constants);
729
Cody Northrop8d177d42014-10-06 17:07:40 -0600730 // Create a texture too
731 InitTexture();
732 InitSampler();
733
734 // Create descriptor set for a uniform resource, texture, and sampler
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600735 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
736 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
Cody Northrop8d177d42014-10-06 17:07:40 -0600737 descriptorInfo.slots = 3;
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600738
Cody Northrop8d177d42014-10-06 17:07:40 -0600739 // create a descriptor set with multiple slots
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600740 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
741 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
742
743 // bind memory to the descriptor set
744 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
745
746 // write the constant buffer view to the descriptor set
Cody Northrop8d177d42014-10-06 17:07:40 -0600747 // the order here matters... i.e. swapping the texture with uniform breaks the test...
748 // we need to understand how to support any order of declaration, probably via intel_shader
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600749 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
Cody Northrop8d177d42014-10-06 17:07:40 -0600750 xglAttachImageViewDescriptors( m_rsrcDescSet, 0, 1, &m_textureViewInfo );
751 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 1, 1, &m_constantBufferView );
752 xglAttachSamplerDescriptors(m_rsrcDescSet, 2, 1, &m_sampler);
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600753 xglEndDescriptorSetUpdate( m_rsrcDescSet );
754
Courtney Goeltzenleuchter94192032014-10-03 09:53:32 -0600755 static const char *vertShaderText =
756 "#version 130\n"
757 "vec2 vertices[3];\n"
758 "void main() {\n"
759 " vertices[0] = vec2(-1.0, -1.0);\n"
760 " vertices[1] = vec2( 1.0, -1.0);\n"
761 " vertices[2] = vec2( 0.0, 1.0);\n"
762 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
763 "}\n";
Courtney Goeltzenleuchterd6f75892014-10-06 12:45:37 -0600764 static const char *vertShader2 =
Cody Northrop7e16d3d2014-10-07 16:38:45 -0600765 "#version 130\n"
Courtney Goeltzenleuchterd6f75892014-10-06 12:45:37 -0600766 "out vec4 color;\n"
767 "out vec4 scale;\n"
Cody Northrop8d177d42014-10-06 17:07:40 -0600768 "out vec2 samplePos;\n"
Courtney Goeltzenleuchterd6f75892014-10-06 12:45:37 -0600769 "void main() {\n"
770 " vec2 vertices[3];"
771 " vertices[0] = vec2(-0.5, -0.5);\n"
772 " vertices[1] = vec2( 0.5, -0.5);\n"
773 " vertices[2] = vec2( 0.5, 0.5);\n"
774 " vec4 colors[3];\n"
775 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
776 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
777 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
Cody Northrop7e16d3d2014-10-07 16:38:45 -0600778 " color = colors[gl_VertexID % 3];\n"
Cody Northrop8d177d42014-10-06 17:07:40 -0600779 " vec2 positions[3];"
780 " positions[0] = vec2( 0.0, 0.0);\n"
781 " positions[1] = vec2( 1.0, 0.0);\n"
782 " positions[2] = vec2( 1.0, 1.0);\n"
783 " scale = vec4(1.0, 1.0, 1.0, 1.0);\n"
Cody Northrop7e16d3d2014-10-07 16:38:45 -0600784 " samplePos = positions[gl_VertexID % 3];\n"
785 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Courtney Goeltzenleuchterd6f75892014-10-06 12:45:37 -0600786 "}\n";
787
Courtney Goeltzenleuchter94192032014-10-03 09:53:32 -0600788 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchterd6f75892014-10-06 12:45:37 -0600789 vertShader2, vs));
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600790
791 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
792 vs_stage.pNext = XGL_NULL_HANDLE;
793 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600794 vs_stage.shader.shader = *vs;
Jon Ashburnf93c1bd2014-10-20 13:37:31 -0600795 for (unsigned int i = 0; i < XGL_MAX_DESCRIPTOR_SETS; i++)
796 vs_stage.shader.descriptorSetMapping[i].descriptorCount = 0;
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600797 vs_stage.shader.linkConstBufferCount = 0;
798 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Chia-I Wu3c89e4e2014-08-23 17:47:45 +0800799 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600800 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
801
Courtney Goeltzenleuchter94192032014-10-03 09:53:32 -0600802 static const char *fragShaderText =
803 "#version 130\n"
Steve K147013a2014-10-07 10:57:33 -0600804 "in vec4 color;\n"
805 "in vec4 scale;\n"
Courtney Goeltzenleuchter94192032014-10-03 09:53:32 -0600806 "void main() {\n"
Steve K147013a2014-10-07 10:57:33 -0600807 " gl_FragColor = color * scale;\n"
Courtney Goeltzenleuchter94192032014-10-03 09:53:32 -0600808 "}\n";
Courtney Goeltzenleuchterd6f75892014-10-06 12:45:37 -0600809 static const char *fragShader2 =
Cody Northrop8d177d42014-10-06 17:07:40 -0600810 "#version 430\n"
811 "in vec4 color;\n"
812 "in vec4 scale;\n"
813 "in vec2 samplePos;\n"
814 "uniform sampler2D surface;\n"
815 "layout(location = 0) uniform vec4 foo;\n"
816 "void main() {\n"
817 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
818 " gl_FragColor = color * scale * foo + texColor;\n"
819 "}\n";
Courtney Goeltzenleuchter94192032014-10-03 09:53:32 -0600820
821 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
Steve K147013a2014-10-07 10:57:33 -0600822 fragShaderText, ps));
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600823
824 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
825 ps_stage.pNext = &vs_stage;
826 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600827 ps_stage.shader.shader = *ps;
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600828 // TODO: Do we need a descriptor set mapping for fragment?
Jon Ashburnf93c1bd2014-10-20 13:37:31 -0600829 for (unsigned int i = 0; i < XGL_MAX_DESCRIPTOR_SETS; i++)
830 ps_stage.shader.descriptorSetMapping[i].descriptorCount = 0;
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600831
Cody Northrop8d177d42014-10-06 17:07:40 -0600832 const int slots = 3;
833 assert(slots == descriptorInfo.slots);
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600834 XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( slots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
835 slotInfo[0].shaderEntityIndex = 0;
836 slotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
Cody Northrop8d177d42014-10-06 17:07:40 -0600837 slotInfo[1].shaderEntityIndex = 1;
838 slotInfo[1].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
839 slotInfo[2].shaderEntityIndex = 0;
840 slotInfo[2].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600841
842 ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
Cody Northrop8d177d42014-10-06 17:07:40 -0600843 ps_stage.shader.descriptorSetMapping[0].descriptorCount = descriptorInfo.slots;
Cody Northrop2c4c6d92014-10-01 14:03:25 -0600844
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600845 ps_stage.shader.linkConstBufferCount = 0;
846 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
Chia-I Wu3c89e4e2014-08-23 17:47:45 +0800847 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600848 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
849
850 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
851 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
852 &ps_stage, // pNext
853 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
854 XGL_FALSE, // disableVertexReuse
855 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
856 XGL_FALSE, // primitiveRestartEnable
857 0 // primitiveRestartIndex
858 };
859
860 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
861 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
862 &ia_state,
863 XGL_FALSE, // depthClipEnable
864 XGL_FALSE, // rasterizerDiscardEnable
865 1.0 // pointSize
866 };
867
868 XGL_PIPELINE_CB_STATE cb_state = {
869 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
870 &rs_state,
871 XGL_FALSE, // alphaToCoverageEnable
872 XGL_FALSE, // dualSourceBlendEnable
873 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
874 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
875 {
876 XGL_FALSE, // blendEnable
Chia-I Wuf5d5c1a2014-08-27 15:02:10 +0800877 {XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM}, // XGL_FORMAT
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600878 0xF // channelWriteMask
879 }
880 }
881 };
882
883 // TODO: Should take depth buffer format from queried formats
884 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
885 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
886 &cb_state,
887 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
888 };
889
890 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
891 info.pNext = &db_state;
892 info.flags = 0;
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600893 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600894 ASSERT_XGL_SUCCESS(err);
Cody Northrop342912c2014-10-01 14:03:25 -0600895
896 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
897 ASSERT_XGL_SUCCESS(err);
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600898}
Courtney Goeltzenleuchterac8bd172014-08-20 15:28:00 -0600899
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600900void XglRenderTest::GenerateClearAndPrepareBufferCmds(XglImage *renderTarget)
901{
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600902 // whatever we want to do, we do it to the whole buffer
903 XGL_IMAGE_SUBRESOURCE_RANGE srRange = {};
904 srRange.aspect = XGL_IMAGE_ASPECT_COLOR;
905 srRange.baseMipLevel = 0;
906 srRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
907 srRange.baseArraySlice = 0;
908 srRange.arraySize = XGL_LAST_MIP_OR_SLICE;
909
910 // prepare the whole back buffer for clear
911 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -0600912 transitionToClear.image = renderTarget->image();
913 transitionToClear.oldState = renderTarget->state();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600914 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
915 transitionToClear.subresourceRange = srRange;
916 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -0600917 renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600918
919 // clear the back buffer to dark grey
920 XGL_UINT clearColor[4] = {64, 64, 64, 0};
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -0600921 xglCmdClearColorImageRaw( m_cmdBuffer, renderTarget->image(), clearColor, 1, &srRange );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600922
923 // prepare back buffer for rendering
924 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -0600925 transitionToRender.image = renderTarget->image();
926 transitionToRender.oldState = renderTarget->state();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600927 transitionToRender.newState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
928 transitionToRender.subresourceRange = srRange;
929 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -0600930 renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600931}
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600932
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600933void XglRenderTest::GenerateBindRenderTargetCmd(XglImage *renderTarget)
934{
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600935 // bind render target
936 XGL_COLOR_ATTACHMENT_BIND_INFO colorBind = {};
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -0600937 colorBind.view = renderTarget->targetView();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600938 colorBind.colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
939 xglCmdBindAttachments(m_cmdBuffer, 1, &colorBind, NULL );
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600940}
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600941
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600942void XglRenderTest::GenerateBindStateAndPipelineCmds(XGL_PIPELINE* pipeline)
943{
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600944 // set all states
945 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_RASTER, m_stateRaster );
946 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_VIEWPORT, m_stateViewport );
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600947 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_COLOR_BLEND, m_colorBlend);
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600948 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil );
949 xglCmdBindStateObject( m_cmdBuffer, XGL_STATE_BIND_MSAA, m_stateMsaa );
950
951 // bind pipeline, vertex buffer (descriptor set) and WVP (dynamic memory view)
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600952 xglCmdBindPipeline( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, *pipeline );
953}
954
955void XglRenderTest::DrawRotatedTriangleTest()
956{
957 // TODO : This test will pass a matrix into VS to affect triangle orientation.
958}
959
960void XglRenderTest::DrawTriangleTest()
961{
962 XGL_PIPELINE pipeline;
963 XGL_SHADER vs, ps;
964 XGL_RESULT err;
965 int width = 256, height = 256;
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600966
Cody Northrop342912c2014-10-01 14:03:25 -0600967 ASSERT_NO_FATAL_FAILURE(CreateDefaultPipeline(&pipeline, &vs, &ps, width, height));
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600968
969 /*
970 * Shaders are now part of the pipeline, don't need these anymore
971 */
972 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
973 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
974
975 XGL_QUERY_POOL query;
976 XGL_GPU_MEMORY query_mem;
977 ASSERT_NO_FATAL_FAILURE(CreateQueryPool(XGL_QUERY_PIPELINE_STATISTICS, 1, &query, &query_mem));
978
979 XglImage *renderTarget;
980 XGL_FORMAT fmt = {
981 XGL_CH_FMT_R8G8B8A8,
982 XGL_NUM_FMT_UNORM
983 };
984 ASSERT_NO_FATAL_FAILURE(m_device->CreateImage(width, height, fmt,
985 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
986 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
987 &renderTarget));
988
Cody Northrop342912c2014-10-01 14:03:25 -0600989 const int constantCount = 4;
990 const float constants[constantCount] = { 0.5, 0.5, 0.5, 1.0 };
991 InitConstantBuffer(constantCount, sizeof(constants[0]), (const void*) constants);
992
993 // Create descriptor set for a uniform resource
994 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
995 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
996 descriptorInfo.slots = 1;
997
998 // create a descriptor set with a single slot
999 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1000 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1001
1002 // bind memory to the descriptor set
1003 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1004
1005 // write the constant buffer view to the descriptor set
1006 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
1007 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
1008 xglEndDescriptorSetUpdate( m_rsrcDescSet );
1009
Tobin Ehlis34e0e442014-10-07 14:41:29 -06001010 // Build command buffer
1011 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1012 ASSERT_XGL_SUCCESS(err);
1013
1014 GenerateClearAndPrepareBufferCmds(renderTarget);
1015 GenerateBindRenderTargetCmd(renderTarget);
1016 GenerateBindStateAndPipelineCmds(&pipeline);
1017
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -06001018// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
Cody Northrop2c4c6d92014-10-01 14:03:25 -06001019 xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
Tobin Ehliscf9489b2014-10-07 14:41:29 -06001020}
1021
1022void XglRenderTest::DrawRotatedTriangleTest()
1023{
1024 // TODO : This test will pass a matrix into VS to affect triangle orientation.
1025}
1026
1027void XglRenderTest::DrawTriangleTest()
1028{
1029 XGL_PIPELINE pipeline;
1030 XGL_SHADER vs, ps;
1031 XGL_RESULT err;
1032 int width = 256, height = 256;
1033 CreateDefaultPipeline(&pipeline, &vs, &ps, width, height);
1034 //ASSERT_XGL_SUCCESS(err);
1035
1036 err = m_device->AllocAndBindGpuMemory(pipeline, "Pipeline", &m_pipe_mem);
1037 ASSERT_XGL_SUCCESS(err);
1038
1039 /*
1040 * Shaders are now part of the pipeline, don't need these anymore
1041 */
1042 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
1043 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
1044
1045 XGL_QUERY_POOL query;
1046 XGL_GPU_MEMORY query_mem;
1047 ASSERT_NO_FATAL_FAILURE(CreateQueryPool(XGL_QUERY_PIPELINE_STATISTICS, 1, &query, &query_mem));
1048
1049 XglImage *renderTarget;
1050 XGL_FORMAT fmt = {
1051 XGL_CH_FMT_R8G8B8A8,
1052 XGL_NUM_FMT_UNORM
1053 };
1054 ASSERT_NO_FATAL_FAILURE(m_device->CreateImage(width, height, fmt,
1055 XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT |
1056 XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
1057 &renderTarget));
1058
1059 // Build command buffer
1060 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1061 ASSERT_XGL_SUCCESS(err);
1062
1063 GenerateClearAndPrepareBufferCmds(renderTarget);
1064 GenerateBindRenderTargetCmd(renderTarget);
1065 GenerateBindStateAndPipelineCmds(&pipeline);
1066
1067// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -06001068// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
1069
Chia-I Wuf070ec12014-08-30 23:58:36 +08001070 xglCmdResetQueryPool(m_cmdBuffer, query, 0, 1);
1071 xglCmdBeginQuery(m_cmdBuffer, query, 0, 0);
1072
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -06001073 // render the cube
1074 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
1075
Chia-I Wuf070ec12014-08-30 23:58:36 +08001076 xglCmdEndQuery(m_cmdBuffer, query, 0);
1077
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -06001078 // prepare the back buffer for present
1079// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
1080// transitionToPresent.image = m_image;
1081// transitionToPresent.oldState = m_image_state;
1082// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
1083// transitionToPresent.subresourceRange = srRange;
1084// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
1085// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
1086
1087 // finalize recording of the command buffer
1088 err = xglEndCommandBuffer( m_cmdBuffer );
1089 ASSERT_XGL_SUCCESS( err );
1090
1091 // this command buffer only uses the vertex buffer memory
1092 m_numMemRefs = 0;
1093// m_memRefs[0].flags = 0;
1094// m_memRefs[0].mem = m_vtxBufferMemory;
1095
1096 // submit the command buffer to the universal queue
1097 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
1098 ASSERT_XGL_SUCCESS( err );
1099
Chia-I Wuf34ac502014-08-27 14:58:05 +08001100 err = xglQueueWaitIdle( m_device->m_queue );
1101 ASSERT_XGL_SUCCESS( err );
1102
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -06001103 // Wait for work to finish before cleaning up.
1104 xglDeviceWaitIdle(m_device->device());
1105
Chia-I Wuf070ec12014-08-30 23:58:36 +08001106 XGL_PIPELINE_STATISTICS_DATA stats;
Jon Ashburnb8e43892014-09-25 14:36:58 -06001107 XGL_SIZE stats_size = sizeof(stats);
Chia-I Wuf070ec12014-08-30 23:58:36 +08001108 err = xglGetQueryPoolResults(query, 0, 1, &stats_size, &stats);
1109 ASSERT_XGL_SUCCESS( err );
1110 ASSERT_EQ(stats_size, sizeof(stats));
1111
1112 ASSERT_EQ(stats.vsInvocations, 3);
1113 ASSERT_EQ(stats.cPrimitives, 1);
1114 ASSERT_EQ(stats.cInvocations, 1);
1115
1116 DestroyQueryPool(query, query_mem);
1117
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -06001118 const ::testing::TestInfo* const test_info =
1119 ::testing::UnitTest::GetInstance()->current_test_info();
1120
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06001121// renderTarget->WritePPM(test_info->test_case_name());
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -06001122// m_screen.Display(renderTarget, m_image_mem);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06001123 RecordImage(renderTarget);
Chia-I Wuf34ac502014-08-27 14:58:05 +08001124
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -06001125 ASSERT_XGL_SUCCESS(xglDestroyObject(pipeline));
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -06001126 ASSERT_XGL_SUCCESS(xglDestroyObject(m_cmdBuffer));
1127 ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateRaster));
1128 ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateViewport));
1129 ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateDepthStencil));
1130 ASSERT_XGL_SUCCESS(xglDestroyObject(m_stateMsaa));
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -06001131 free(renderTarget);
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -06001132}
1133
Chia-I Wuf34ac502014-08-27 14:58:05 +08001134
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -06001135TEST_F(XglRenderTest, TestDrawTriangle) {
1136 DrawTriangleTest();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06001137}
1138
Tobin Ehlis34e0e442014-10-07 14:41:29 -06001139TEST_F(XglRenderTest, TestDrawRotatedTriangle) {
1140 DrawRotatedTriangleTest();
1141}
1142
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06001143int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06001144 int result;
1145
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06001146 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06001147 XglTestFramework::InitArgs(&argc, argv);
1148
1149 result = RUN_ALL_TESTS();
1150
1151 XglTestFramework::Finish();
1152 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06001153}