blob: e3763736fcda3aeffe918a405bc2f4d56e536f28 [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 Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060072#include "xglrenderframework.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 Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600197class XglRenderTest : public XglRenderFramework
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600198{
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600199public:
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600200 void InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, const void* vertices );
Cody Northrop350727b2014-10-06 15:42:00 -0600201 void InitTexture();
202 void InitSampler();
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600203 void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText);
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600204 void DrawRotatedTriangleTest();
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600205 void NewGenerateClearAndPrepareBufferCmds(XglImage *renderTarget);
206 void NewGenerateBindStateAndPipelineCmds(XGL_PIPELINE* pipeline);
207
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600208
209protected:
Cody Northrop350727b2014-10-06 15:42:00 -0600210 XGL_IMAGE m_texture;
211 XGL_IMAGE_VIEW m_textureView;
212 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
213 XGL_GPU_MEMORY m_textureMem;
214
215 XGL_SAMPLER m_sampler;
216
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600217// XGL_APPLICATION_INFO app_info;
218// XGL_PHYSICAL_GPU objs[MAX_GPUS];
219// XGL_UINT gpu_count;
220// XGL_GPU_MEMORY m_descriptor_set_mem;
221// XGL_GPU_MEMORY m_pipe_mem;
222// XglDevice *m_device;
223// XGL_CMD_BUFFER m_cmdBuffer;
224// XGL_UINT32 m_numVertices;
225// XGL_MEMORY_VIEW_ATTACH_INFO m_vtxBufferView;
226// XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView;
227// XGL_GPU_MEMORY m_vtxBufferMem;
228// XGL_GPU_MEMORY m_constantBufferMem;
229// XGL_UINT32 m_numMemRefs;
230// XGL_MEMORY_REF m_memRefs[5];
231// XGL_RASTER_STATE_OBJECT m_stateRaster;
232// XGL_COLOR_BLEND_STATE_OBJECT m_colorBlend;
233// XGL_VIEWPORT_STATE_OBJECT m_stateViewport;
234// XGL_DEPTH_STENCIL_STATE_OBJECT m_stateDepthStencil;
235// XGL_MSAA_STATE_OBJECT m_stateMsaa;
236// XGL_DESCRIPTOR_SET m_rsrcDescSet;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600237
238 virtual void SetUp() {
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600239
240 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
241 this->app_info.pNext = NULL;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600242 this->app_info.pAppName = (const XGL_CHAR *) "render_tests";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600243 this->app_info.appVersion = 1;
244 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
245 this->app_info.engineVersion = 1;
246 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
247
Cody Northrop350727b2014-10-06 15:42:00 -0600248 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
249 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
250
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600251 InitFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600252 }
253
254 virtual void TearDown() {
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600255 // Clean up resources before we reset
256 ShutdownFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600257 }
258};
259
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600260// this function will create the vertex buffer and fill it with the mesh data
261void XglRenderTest::InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride,
262 const void* vertices )
263{
264 XGL_RESULT err = XGL_SUCCESS;
265
266 assert( numVertices * vbStride > 0 );
267 m_numVertices = numVertices;
268
269 XGL_MEMORY_ALLOC_INFO alloc_info = {};
270 XGL_UINT8 *pData;
271
272 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
273 alloc_info.allocationSize = numVertices * vbStride;
274 alloc_info.alignment = 0;
275 alloc_info.heapCount = 1;
276 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
277
278 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
279 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
280
281 err = xglAllocMemory(device(), &alloc_info, &m_vtxBufferMem);
282 ASSERT_XGL_SUCCESS(err);
283
284 err = xglMapMemory(m_vtxBufferMem, 0, (XGL_VOID **) &pData);
285 ASSERT_XGL_SUCCESS(err);
286
287 memcpy(pData, vertices, alloc_info.allocationSize);
288
289 err = xglUnmapMemory(m_vtxBufferMem);
290 ASSERT_XGL_SUCCESS(err);
291
292 // set up the memory view for the vertex buffer
293 this->m_vtxBufferView.stride = vbStride;
294 this->m_vtxBufferView.range = numVertices * vbStride;
295 this->m_vtxBufferView.offset = 0;
296 this->m_vtxBufferView.mem = m_vtxBufferMem;
297 this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED;
298 this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED;
299
300 // open the command buffer
301 err = xglBeginCommandBuffer( m_cmdBuffer, 0 );
302 ASSERT_XGL_SUCCESS(err);
303
304 XGL_MEMORY_STATE_TRANSITION transition = {};
305 transition.mem = m_vtxBufferMem;
306 transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER;
307 transition.newState = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
308 transition.offset = 0;
309 transition.regionSize = numVertices * vbStride;
310
311 // write transition to the command buffer
312 xglCmdPrepareMemoryRegions( m_cmdBuffer, 1, &transition );
313 this->m_vtxBufferView.state = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
314
315 // finish recording the command buffer
316 err = xglEndCommandBuffer( m_cmdBuffer );
317 ASSERT_XGL_SUCCESS(err);
318
319 // this command buffer only uses the vertex buffer memory
320 m_numMemRefs = 1;
321 m_memRefs[0].flags = 0;
322 m_memRefs[0].mem = m_vtxBufferMem;
323
324 // submit the command buffer to the universal queue
325 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
326 ASSERT_XGL_SUCCESS(err);
327}
328
Cody Northrop350727b2014-10-06 15:42:00 -0600329void XglRenderTest::InitTexture()
330{
Cody Northrop904742c2014-10-07 16:25:00 -0600331#define DEMO_TEXTURE_COUNT 1
332
333 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
334 const XGL_INT tex_width = 16;
335 const XGL_INT tex_height = 16;
336 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
337 { 0xffff0000, 0xff00ff00 },
338 };
Cody Northrop350727b2014-10-06 15:42:00 -0600339 XGL_RESULT err;
Cody Northrop904742c2014-10-07 16:25:00 -0600340 XGL_UINT i;
Cody Northrop350727b2014-10-06 15:42:00 -0600341
Cody Northrop904742c2014-10-07 16:25:00 -0600342 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
343 const XGL_SAMPLER_CREATE_INFO sampler = {
344 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
345 .pNext = NULL,
346 .magFilter = XGL_TEX_FILTER_NEAREST,
347 .minFilter = XGL_TEX_FILTER_NEAREST,
348 .mipMode = XGL_TEX_MIPMAP_BASE,
349 .addressU = XGL_TEX_ADDRESS_WRAP,
350 .addressV = XGL_TEX_ADDRESS_WRAP,
351 .addressW = XGL_TEX_ADDRESS_WRAP,
352 .mipLodBias = 0.0f,
353 .maxAnisotropy = 0,
354 .compareFunc = XGL_COMPARE_NEVER,
355 .minLod = 0.0f,
356 .maxLod = 0.0f,
357 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
358 };
359 const XGL_IMAGE_CREATE_INFO image = {
360 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
361 .pNext = NULL,
362 .imageType = XGL_IMAGE_2D,
363 .format = tex_format,
364 .extent = { tex_width, tex_height, 1 },
365 .mipLevels = 1,
366 .arraySize = 1,
367 .samples = 1,
368 .tiling = XGL_LINEAR_TILING,
369 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
370 .flags = 0,
371 };
372 XGL_MEMORY_ALLOC_INFO mem_alloc;
373 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
374 mem_alloc.pNext = NULL;
375 mem_alloc.allocationSize = 0;
376 mem_alloc.alignment = 0;
377 mem_alloc.flags = 0;
378 mem_alloc.heapCount = 0;
379 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
380 XGL_IMAGE_VIEW_CREATE_INFO view;
381 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
382 view.pNext = NULL;
383 view.image = XGL_NULL_HANDLE;
384 view.viewType = XGL_IMAGE_VIEW_2D;
385 view.format = image.format;
386 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
387 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
388 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
389 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
390 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
391 view.subresourceRange.baseMipLevel = 0;
392 view.subresourceRange.mipLevels = 1;
393 view.subresourceRange.baseArraySlice = 0;
394 view.subresourceRange.arraySize = 1;
395 view.minLod = 0.0f;
Cody Northrop350727b2014-10-06 15:42:00 -0600396
Cody Northrop904742c2014-10-07 16:25:00 -0600397 XGL_MEMORY_REQUIREMENTS mem_reqs;
398 XGL_SIZE mem_reqs_size;
Cody Northrop350727b2014-10-06 15:42:00 -0600399
Cody Northrop904742c2014-10-07 16:25:00 -0600400 /* create sampler */
401 err = xglCreateSampler(device(), &sampler, &m_sampler);
402 assert(!err);
403
404 /* create image */
405 err = xglCreateImage(device(), &image, &m_texture);
406 assert(!err);
407
408 err = xglGetObjectInfo(m_texture,
409 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
410 &mem_reqs_size, &mem_reqs);
411 assert(!err && mem_reqs_size == sizeof(mem_reqs));
412
413 mem_alloc.allocationSize = mem_reqs.size;
414 mem_alloc.alignment = mem_reqs.alignment;
415 mem_alloc.heapCount = mem_reqs.heapCount;
416 memcpy(mem_alloc.heaps, mem_reqs.heaps,
417 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
418
419 /* allocate memory */
420 err = xglAllocMemory(device(), &mem_alloc, &m_textureMem);
421 assert(!err);
422
423 /* bind memory */
424 err = xglBindObjectMemory(m_texture, m_textureMem, 0);
425 assert(!err);
426
427 /* create image view */
428 view.image = m_texture;
429 err = xglCreateImageView(device(), &view, &m_textureView);
430 assert(!err);
Cody Northrop350727b2014-10-06 15:42:00 -0600431 }
432
Cody Northrop904742c2014-10-07 16:25:00 -0600433 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
434 const XGL_IMAGE_SUBRESOURCE subres = {
435 .aspect = XGL_IMAGE_ASPECT_COLOR,
436 .mipLevel = 0,
437 .arraySlice = 0,
438 };
439 XGL_SUBRESOURCE_LAYOUT layout;
440 XGL_SIZE layout_size;
441 XGL_VOID *data;
442 XGL_INT x, y;
Cody Northrop350727b2014-10-06 15:42:00 -0600443
Cody Northrop904742c2014-10-07 16:25:00 -0600444 err = xglGetImageSubresourceInfo(m_texture, &subres,
445 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
446 assert(!err && layout_size == sizeof(layout));
Cody Northrop350727b2014-10-06 15:42:00 -0600447
Cody Northrop904742c2014-10-07 16:25:00 -0600448 err = xglMapMemory(m_textureMem, 0, &data);
449 assert(!err);
Cody Northrop350727b2014-10-06 15:42:00 -0600450
Cody Northrop904742c2014-10-07 16:25:00 -0600451 for (y = 0; y < tex_height; y++) {
452 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
453 for (x = 0; x < tex_width; x++)
454 row[x] = tex_colors[i][(x & 1) ^ (y & 1)];
Cody Northrop350727b2014-10-06 15:42:00 -0600455 }
Cody Northrop904742c2014-10-07 16:25:00 -0600456
457 err = xglUnmapMemory(m_textureMem);
458 assert(!err);
Cody Northrop350727b2014-10-06 15:42:00 -0600459 }
460
Cody Northrop350727b2014-10-06 15:42:00 -0600461 m_textureViewInfo.view = m_textureView;
462}
463
464void XglRenderTest::InitSampler()
465{
466 XGL_RESULT err;
467
468 XGL_SAMPLER_CREATE_INFO samplerCreateInfo = {};
469 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
470 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
471 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
472 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
473 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
474 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
475 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
476 samplerCreateInfo.mipLodBias = 0.0;
477 samplerCreateInfo.maxAnisotropy = 0.0;
478 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
479 samplerCreateInfo.minLod = 0.0;
480 samplerCreateInfo.maxLod = 0.0;
481 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
482
483 err = xglCreateSampler(device(),&samplerCreateInfo, &m_sampler);
484 ASSERT_XGL_SUCCESS(err);
485}
486
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600487void XglRenderTest::DrawRotatedTriangleTest()
488{
489 // TODO : This test will pass a matrix into VS to affect triangle orientation.
490}
491
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600492void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText)
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600493{
494 XGL_PIPELINE pipeline;
495 XGL_SHADER vs, ps;
496 XGL_RESULT err;
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600497
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600498 ASSERT_NO_FATAL_FAILURE(InitState());
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600499 ASSERT_NO_FATAL_FAILURE(InitViewport());
500
501 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
502 vertShaderText, &vs));
503
504 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
505 fragShaderText, &ps));
506
507 ASSERT_NO_FATAL_FAILURE(CreateDefaultPipeline(&pipeline, vs, ps));
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600508
509 /*
510 * Shaders are now part of the pipeline, don't need these anymore
511 */
512 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
513 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
514
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600515 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600516
Cody Northrop342912c2014-10-01 14:03:25 -0600517 const int constantCount = 4;
518 const float constants[constantCount] = { 0.5, 0.5, 0.5, 1.0 };
519 InitConstantBuffer(constantCount, sizeof(constants[0]), (const void*) constants);
520
521 // Create descriptor set for a uniform resource
522 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
523 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
524 descriptorInfo.slots = 1;
525
526 // create a descriptor set with a single slot
527 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
528 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
529
530 // bind memory to the descriptor set
531 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
532
533 // write the constant buffer view to the descriptor set
534 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
535 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
536 xglEndDescriptorSetUpdate( m_rsrcDescSet );
537
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600538 // Build command buffer
539 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
540 ASSERT_XGL_SUCCESS(err);
541
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600542 GenerateClearAndPrepareBufferCmds();
543 GenerateBindRenderTargetCmd();
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600544 GenerateBindStateAndPipelineCmds(&pipeline);
545
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600546// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
547// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
548
549 // render the cube
550 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
551
552 // prepare the back buffer for present
553// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
554// transitionToPresent.image = m_image;
555// transitionToPresent.oldState = m_image_state;
556// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
557// transitionToPresent.subresourceRange = srRange;
558// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
559// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
560
561 // finalize recording of the command buffer
562 err = xglEndCommandBuffer( m_cmdBuffer );
563 ASSERT_XGL_SUCCESS( err );
564
565 // this command buffer only uses the vertex buffer memory
566 m_numMemRefs = 0;
567// m_memRefs[0].flags = 0;
568// m_memRefs[0].mem = m_vtxBufferMemory;
569
570 // submit the command buffer to the universal queue
571 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
572 ASSERT_XGL_SUCCESS( err );
573
Chia-I Wuf34ac502014-08-27 14:58:05 +0800574 err = xglQueueWaitIdle( m_device->m_queue );
575 ASSERT_XGL_SUCCESS( err );
576
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600577 // Wait for work to finish before cleaning up.
578 xglDeviceWaitIdle(m_device->device());
579
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600580 RecordImage(m_renderTarget);
Chia-I Wuf070ec12014-08-30 23:58:36 +0800581
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600582}
583
Chia-I Wuf34ac502014-08-27 14:58:05 +0800584
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600585TEST_F(XglRenderTest, TestDrawTriangle1) {
586 static const char *vertShaderText =
587 "#version 130\n"
588 "vec2 vertices[3];\n"
589 "void main() {\n"
590 " vertices[0] = vec2(-1.0, -1.0);\n"
591 " vertices[1] = vec2( 1.0, -1.0);\n"
592 " vertices[2] = vec2( 0.0, 1.0);\n"
593 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
594 "}\n";
595
596 static const char *fragShaderText =
597 "#version 130\n"
598 "uniform vec4 foo;\n"
599 "void main() {\n"
600 " gl_FragColor = foo;\n"
601 "}\n";
602 DrawTriangleTest(vertShaderText, fragShaderText);
603}
604
605TEST_F(XglRenderTest, TestDrawTriangle2) {
606 static const char *vertShaderText =
607 "#version 330\n"
608 "out vec4 color;\n"
609 "out vec4 scale;\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 " color = colors[int(mod(gl_VertexID, 3))];\n"
620 " scale = vec4(1.0, 1.0, 1.0, 1.0);\n"
621 " gl_Position = vec4(vertices[int(mod(gl_VertexID, 3))], 0.0, 1.0);\n"
622 "}\n";
623
624 static const char *fragShaderText =
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 DrawTriangleTest(vertShaderText, fragShaderText);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600633}
634
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600635TEST_F(XglRenderTest, TestDrawRotatedTriangle) {
636 DrawRotatedTriangleTest();
637}
638
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600639int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600640 int result;
641
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600642 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600643 XglTestFramework::InitArgs(&argc, argv);
644
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -0600645 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
646
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600647 result = RUN_ALL_TESTS();
648
649 XglTestFramework::Finish();
650 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600651}