blob: 6ef2bc8c8e0a959373a5fce6eebd95c5f20014de [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
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060053// Basic rendering tests
54
55#include <stdlib.h>
56#include <stdio.h>
57#include <stdbool.h>
58#include <string.h>
Courtney Goeltzenleuchter76a643b2014-08-21 17:34:22 -060059#include <iostream>
60#include <fstream>
61using namespace std;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060062
63#include <xgl.h>
Courtney Goeltzenleuchterff87c822014-10-03 18:05:10 -060064#include <xglIntelExt.h>
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060065#include "gtest-1.7.0/include/gtest/gtest.h"
66
67#include "xgldevice.h"
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -060068#include "xglimage.h"
Chia-I Wu4115c892014-08-28 11:56:29 +080069#include "icd-bil.h"
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060070
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060071#include "xglrenderframework.h"
Courtney Goeltzenleuchter2268d1e2014-09-01 13:57:15 -060072
Courtney Goeltzenleuchterbb7014d2014-10-09 11:05:19 -060073#undef ASSERT_NO_FATAL_FAILURE
74#define ASSERT_NO_FATAL_FAILURE(x) x
75
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -060076//--------------------------------------------------------------------------------------
77// Mesh and VertexFormat Data
78//--------------------------------------------------------------------------------------
79struct 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
87static 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
Chia-I Wu6e9789c2014-08-30 14:03:32 +0800132static 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
141static 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
165static 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
174static 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};
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600198
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600199class XglRenderTest : public XglRenderFramework
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600200{
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600201public:
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600202 void InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, const void* vertices );
Cody Northrop350727b2014-10-06 15:42:00 -0600203 void InitTexture();
204 void InitSampler();
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600205 void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText);
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600206 void DrawRotatedTriangleTest();
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600207 void NewGenerateClearAndPrepareBufferCmds(XglImage *renderTarget);
208 void NewGenerateBindStateAndPipelineCmds(XGL_PIPELINE* pipeline);
209
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600210
211protected:
Cody Northrop350727b2014-10-06 15:42:00 -0600212 XGL_IMAGE m_texture;
213 XGL_IMAGE_VIEW m_textureView;
214 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
215 XGL_GPU_MEMORY m_textureMem;
216
217 XGL_SAMPLER m_sampler;
218
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600219// XGL_APPLICATION_INFO app_info;
220// XGL_PHYSICAL_GPU objs[MAX_GPUS];
221// XGL_UINT gpu_count;
222// XGL_GPU_MEMORY m_descriptor_set_mem;
223// XGL_GPU_MEMORY m_pipe_mem;
224// XglDevice *m_device;
225// XGL_CMD_BUFFER m_cmdBuffer;
226// XGL_UINT32 m_numVertices;
227// XGL_MEMORY_VIEW_ATTACH_INFO m_vtxBufferView;
228// XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView;
229// XGL_GPU_MEMORY m_vtxBufferMem;
230// XGL_GPU_MEMORY m_constantBufferMem;
231// XGL_UINT32 m_numMemRefs;
232// XGL_MEMORY_REF m_memRefs[5];
233// XGL_RASTER_STATE_OBJECT m_stateRaster;
234// XGL_COLOR_BLEND_STATE_OBJECT m_colorBlend;
235// XGL_VIEWPORT_STATE_OBJECT m_stateViewport;
236// XGL_DEPTH_STENCIL_STATE_OBJECT m_stateDepthStencil;
237// XGL_MSAA_STATE_OBJECT m_stateMsaa;
238// XGL_DESCRIPTOR_SET m_rsrcDescSet;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600239
240 virtual void SetUp() {
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600241
242 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
243 this->app_info.pNext = NULL;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600244 this->app_info.pAppName = (const XGL_CHAR *) "render_tests";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600245 this->app_info.appVersion = 1;
246 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
247 this->app_info.engineVersion = 1;
248 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
249
Cody Northrop350727b2014-10-06 15:42:00 -0600250 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
251 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
252
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600253 InitFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600254 }
255
256 virtual void TearDown() {
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600257 // Clean up resources before we reset
258 ShutdownFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600259 }
260};
261
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600262// this function will create the vertex buffer and fill it with the mesh data
263void XglRenderTest::InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride,
264 const void* vertices )
265{
266 XGL_RESULT err = XGL_SUCCESS;
267
268 assert( numVertices * vbStride > 0 );
269 m_numVertices = numVertices;
270
271 XGL_MEMORY_ALLOC_INFO alloc_info = {};
272 XGL_UINT8 *pData;
273
274 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
275 alloc_info.allocationSize = numVertices * vbStride;
276 alloc_info.alignment = 0;
277 alloc_info.heapCount = 1;
278 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
279
280 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
281 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
282
283 err = xglAllocMemory(device(), &alloc_info, &m_vtxBufferMem);
284 ASSERT_XGL_SUCCESS(err);
285
286 err = xglMapMemory(m_vtxBufferMem, 0, (XGL_VOID **) &pData);
287 ASSERT_XGL_SUCCESS(err);
288
289 memcpy(pData, vertices, alloc_info.allocationSize);
290
291 err = xglUnmapMemory(m_vtxBufferMem);
292 ASSERT_XGL_SUCCESS(err);
293
294 // set up the memory view for the vertex buffer
295 this->m_vtxBufferView.stride = vbStride;
296 this->m_vtxBufferView.range = numVertices * vbStride;
297 this->m_vtxBufferView.offset = 0;
298 this->m_vtxBufferView.mem = m_vtxBufferMem;
299 this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED;
300 this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED;
301
302 // open the command buffer
303 err = xglBeginCommandBuffer( m_cmdBuffer, 0 );
304 ASSERT_XGL_SUCCESS(err);
305
306 XGL_MEMORY_STATE_TRANSITION transition = {};
307 transition.mem = m_vtxBufferMem;
308 transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER;
309 transition.newState = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
310 transition.offset = 0;
311 transition.regionSize = numVertices * vbStride;
312
313 // write transition to the command buffer
314 xglCmdPrepareMemoryRegions( m_cmdBuffer, 1, &transition );
315 this->m_vtxBufferView.state = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
316
317 // finish recording the command buffer
318 err = xglEndCommandBuffer( m_cmdBuffer );
319 ASSERT_XGL_SUCCESS(err);
320
321 // this command buffer only uses the vertex buffer memory
322 m_numMemRefs = 1;
323 m_memRefs[0].flags = 0;
324 m_memRefs[0].mem = m_vtxBufferMem;
325
326 // submit the command buffer to the universal queue
327 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
328 ASSERT_XGL_SUCCESS(err);
329}
330
Cody Northrop350727b2014-10-06 15:42:00 -0600331void XglRenderTest::InitTexture()
332{
Cody Northrop904742c2014-10-07 16:25:00 -0600333#define DEMO_TEXTURE_COUNT 1
334
335 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
336 const XGL_INT tex_width = 16;
337 const XGL_INT tex_height = 16;
338 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
339 { 0xffff0000, 0xff00ff00 },
340 };
Cody Northrop350727b2014-10-06 15:42:00 -0600341 XGL_RESULT err;
Cody Northrop904742c2014-10-07 16:25:00 -0600342 XGL_UINT i;
Cody Northrop350727b2014-10-06 15:42:00 -0600343
Cody Northrop904742c2014-10-07 16:25:00 -0600344 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
345 const XGL_SAMPLER_CREATE_INFO sampler = {
346 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
347 .pNext = NULL,
348 .magFilter = XGL_TEX_FILTER_NEAREST,
349 .minFilter = XGL_TEX_FILTER_NEAREST,
350 .mipMode = XGL_TEX_MIPMAP_BASE,
351 .addressU = XGL_TEX_ADDRESS_WRAP,
352 .addressV = XGL_TEX_ADDRESS_WRAP,
353 .addressW = XGL_TEX_ADDRESS_WRAP,
354 .mipLodBias = 0.0f,
355 .maxAnisotropy = 0,
356 .compareFunc = XGL_COMPARE_NEVER,
357 .minLod = 0.0f,
358 .maxLod = 0.0f,
359 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
360 };
361 const XGL_IMAGE_CREATE_INFO image = {
362 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
363 .pNext = NULL,
364 .imageType = XGL_IMAGE_2D,
365 .format = tex_format,
366 .extent = { tex_width, tex_height, 1 },
367 .mipLevels = 1,
368 .arraySize = 1,
369 .samples = 1,
370 .tiling = XGL_LINEAR_TILING,
371 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
372 .flags = 0,
373 };
374 XGL_MEMORY_ALLOC_INFO mem_alloc;
375 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
376 mem_alloc.pNext = NULL;
377 mem_alloc.allocationSize = 0;
378 mem_alloc.alignment = 0;
379 mem_alloc.flags = 0;
380 mem_alloc.heapCount = 0;
381 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
382 XGL_IMAGE_VIEW_CREATE_INFO view;
383 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
384 view.pNext = NULL;
385 view.image = XGL_NULL_HANDLE;
386 view.viewType = XGL_IMAGE_VIEW_2D;
387 view.format = image.format;
388 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
389 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
390 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
391 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
392 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
393 view.subresourceRange.baseMipLevel = 0;
394 view.subresourceRange.mipLevels = 1;
395 view.subresourceRange.baseArraySlice = 0;
396 view.subresourceRange.arraySize = 1;
397 view.minLod = 0.0f;
Cody Northrop350727b2014-10-06 15:42:00 -0600398
Cody Northrop904742c2014-10-07 16:25:00 -0600399 XGL_MEMORY_REQUIREMENTS mem_reqs;
400 XGL_SIZE mem_reqs_size;
Cody Northrop350727b2014-10-06 15:42:00 -0600401
Cody Northrop904742c2014-10-07 16:25:00 -0600402 /* create sampler */
403 err = xglCreateSampler(device(), &sampler, &m_sampler);
404 assert(!err);
405
406 /* create image */
407 err = xglCreateImage(device(), &image, &m_texture);
408 assert(!err);
409
410 err = xglGetObjectInfo(m_texture,
411 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
412 &mem_reqs_size, &mem_reqs);
413 assert(!err && mem_reqs_size == sizeof(mem_reqs));
414
415 mem_alloc.allocationSize = mem_reqs.size;
416 mem_alloc.alignment = mem_reqs.alignment;
417 mem_alloc.heapCount = mem_reqs.heapCount;
418 memcpy(mem_alloc.heaps, mem_reqs.heaps,
419 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
420
421 /* allocate memory */
422 err = xglAllocMemory(device(), &mem_alloc, &m_textureMem);
423 assert(!err);
424
425 /* bind memory */
426 err = xglBindObjectMemory(m_texture, m_textureMem, 0);
427 assert(!err);
428
429 /* create image view */
430 view.image = m_texture;
431 err = xglCreateImageView(device(), &view, &m_textureView);
432 assert(!err);
Cody Northrop350727b2014-10-06 15:42:00 -0600433 }
434
Cody Northrop904742c2014-10-07 16:25:00 -0600435 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
436 const XGL_IMAGE_SUBRESOURCE subres = {
437 .aspect = XGL_IMAGE_ASPECT_COLOR,
438 .mipLevel = 0,
439 .arraySlice = 0,
440 };
441 XGL_SUBRESOURCE_LAYOUT layout;
442 XGL_SIZE layout_size;
443 XGL_VOID *data;
444 XGL_INT x, y;
Cody Northrop350727b2014-10-06 15:42:00 -0600445
Cody Northrop904742c2014-10-07 16:25:00 -0600446 err = xglGetImageSubresourceInfo(m_texture, &subres,
447 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
448 assert(!err && layout_size == sizeof(layout));
Cody Northrop350727b2014-10-06 15:42:00 -0600449
Cody Northrop904742c2014-10-07 16:25:00 -0600450 err = xglMapMemory(m_textureMem, 0, &data);
451 assert(!err);
Cody Northrop350727b2014-10-06 15:42:00 -0600452
Cody Northrop904742c2014-10-07 16:25:00 -0600453 for (y = 0; y < tex_height; y++) {
454 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
455 for (x = 0; x < tex_width; x++)
456 row[x] = tex_colors[i][(x & 1) ^ (y & 1)];
Cody Northrop350727b2014-10-06 15:42:00 -0600457 }
Cody Northrop904742c2014-10-07 16:25:00 -0600458
459 err = xglUnmapMemory(m_textureMem);
460 assert(!err);
Cody Northrop350727b2014-10-06 15:42:00 -0600461 }
462
Cody Northrop350727b2014-10-06 15:42:00 -0600463 m_textureViewInfo.view = m_textureView;
464}
465
466void XglRenderTest::InitSampler()
467{
468 XGL_RESULT err;
469
470 XGL_SAMPLER_CREATE_INFO samplerCreateInfo = {};
471 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
472 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
473 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
474 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
475 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
476 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
477 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
478 samplerCreateInfo.mipLodBias = 0.0;
479 samplerCreateInfo.maxAnisotropy = 0.0;
480 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
481 samplerCreateInfo.minLod = 0.0;
482 samplerCreateInfo.maxLod = 0.0;
483 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
484
485 err = xglCreateSampler(device(),&samplerCreateInfo, &m_sampler);
486 ASSERT_XGL_SUCCESS(err);
487}
488
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600489void XglRenderTest::DrawRotatedTriangleTest()
490{
491 // TODO : This test will pass a matrix into VS to affect triangle orientation.
492}
493
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600494void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText)
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600495{
496 XGL_PIPELINE pipeline;
497 XGL_SHADER vs, ps;
498 XGL_RESULT err;
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600499
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600500 ASSERT_NO_FATAL_FAILURE(InitState());
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600501 ASSERT_NO_FATAL_FAILURE(InitViewport());
502
503 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
504 vertShaderText, &vs));
505
506 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
507 fragShaderText, &ps));
508
509 ASSERT_NO_FATAL_FAILURE(CreateDefaultPipeline(&pipeline, vs, ps));
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600510
511 /*
512 * Shaders are now part of the pipeline, don't need these anymore
513 */
514 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
515 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
516
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600517 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600518
Cody Northrop342912c2014-10-01 14:03:25 -0600519 const int constantCount = 4;
520 const float constants[constantCount] = { 0.5, 0.5, 0.5, 1.0 };
521 InitConstantBuffer(constantCount, sizeof(constants[0]), (const void*) constants);
522
523 // Create descriptor set for a uniform resource
524 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
525 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
526 descriptorInfo.slots = 1;
527
528 // create a descriptor set with a single slot
529 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
530 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
531
532 // bind memory to the descriptor set
533 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
534
535 // write the constant buffer view to the descriptor set
536 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
537 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
538 xglEndDescriptorSetUpdate( m_rsrcDescSet );
539
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600540 // Build command buffer
541 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
542 ASSERT_XGL_SUCCESS(err);
543
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600544 GenerateClearAndPrepareBufferCmds();
545 GenerateBindRenderTargetCmd();
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600546 GenerateBindStateAndPipelineCmds(&pipeline);
547
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600548// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
549// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
550
551 // render the cube
552 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
553
554 // prepare the back buffer for present
555// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
556// transitionToPresent.image = m_image;
557// transitionToPresent.oldState = m_image_state;
558// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
559// transitionToPresent.subresourceRange = srRange;
560// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
561// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
562
563 // finalize recording of the command buffer
564 err = xglEndCommandBuffer( m_cmdBuffer );
565 ASSERT_XGL_SUCCESS( err );
566
567 // this command buffer only uses the vertex buffer memory
568 m_numMemRefs = 0;
569// m_memRefs[0].flags = 0;
570// m_memRefs[0].mem = m_vtxBufferMemory;
571
572 // submit the command buffer to the universal queue
573 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
574 ASSERT_XGL_SUCCESS( err );
575
Chia-I Wuf34ac502014-08-27 14:58:05 +0800576 err = xglQueueWaitIdle( m_device->m_queue );
577 ASSERT_XGL_SUCCESS( err );
578
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600579 // Wait for work to finish before cleaning up.
580 xglDeviceWaitIdle(m_device->device());
581
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600582 RecordImage(m_renderTarget);
Chia-I Wuf070ec12014-08-30 23:58:36 +0800583
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600584}
585
Chia-I Wuf34ac502014-08-27 14:58:05 +0800586
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600587TEST_F(XglRenderTest, TestDrawTriangle1) {
588 static const char *vertShaderText =
589 "#version 130\n"
590 "vec2 vertices[3];\n"
591 "void main() {\n"
592 " vertices[0] = vec2(-1.0, -1.0);\n"
593 " vertices[1] = vec2( 1.0, -1.0);\n"
594 " vertices[2] = vec2( 0.0, 1.0);\n"
595 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
596 "}\n";
597
598 static const char *fragShaderText =
599 "#version 130\n"
600 "uniform vec4 foo;\n"
601 "void main() {\n"
602 " gl_FragColor = foo;\n"
603 "}\n";
604 DrawTriangleTest(vertShaderText, fragShaderText);
605}
606
607TEST_F(XglRenderTest, TestDrawTriangle2) {
Courtney Goeltzenleuchterbb7014d2014-10-09 11:05:19 -0600608// static const char *vertShaderText =
609// "#version 130\n"
610// "void main() {\n"
611// " vec2 vertices[3];"
612// " vertices[0] = vec2(-0.5, -0.5);\n"
613// " vertices[1] = vec2( 0.5, -0.5);\n"
614// " vertices[2] = vec2( 0.5, 0.5);\n"
615// " vec4 colors[3];\n"
616// " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
617// " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
618// " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
619// " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
620// "}\n";
621
622// static const char *fragShaderText =
623// "#version 130\n"
624// "void main() {\n"
625// " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
626// "}\n";
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600627 static const char *vertShaderText =
Courtney Goeltzenleuchterbb7014d2014-10-09 11:05:19 -0600628 "#version 130\n"
629 "vec2 vertices[3];\n"
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600630 "void main() {\n"
Courtney Goeltzenleuchterbb7014d2014-10-09 11:05:19 -0600631 " vertices[0] = vec2(-1.0, -1.0);\n"
632 " vertices[1] = vec2( 1.0, -1.0);\n"
633 " vertices[2] = vec2( 0.0, 1.0);\n"
634 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600635 "}\n";
636
637 static const char *fragShaderText =
Courtney Goeltzenleuchterbb7014d2014-10-09 11:05:19 -0600638 "#version 130\n"
639 "uniform vec4 foo;\n"
640 "void main() {\n"
641 " gl_FragColor = foo;\n"
642 "}\n";
643// DrawTriangleTest(vertShaderText, fragShaderText);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600644}
645
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600646TEST_F(XglRenderTest, TestDrawRotatedTriangle) {
647 DrawRotatedTriangleTest();
648}
649
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600650int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600651 int result;
652
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600653 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600654 XglTestFramework::InitArgs(&argc, argv);
655
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -0600656 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
657
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600658 result = RUN_ALL_TESTS();
659
660 XglTestFramework::Finish();
661 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600662}