blob: 1bf2df600f163f50a2fb65a379eee5fcaf516bd3 [file] [log] [blame]
Cody Northrop722ff402014-10-20 09:22:42 -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// Basic rendering tests
54
55#include <stdlib.h>
56#include <stdio.h>
57#include <stdbool.h>
58#include <string.h>
59#include <iostream>
60#include <fstream>
61using namespace std;
62
63#include <xgl.h>
64#include "gtest-1.7.0/include/gtest/gtest.h"
65
66#include "xgldevice.h"
67#include "xglimage.h"
68#include "icd-bil.h"
69
70#define GLM_FORCE_RADIANS
71#include "glm/glm.hpp"
72#include <glm/gtc/matrix_transform.hpp>
73
74#include "xglrenderframework.h"
75
76#undef ASSERT_NO_FATAL_FAILURE
77#define ASSERT_NO_FATAL_FAILURE(x) x
78
79//--------------------------------------------------------------------------------------
80// Mesh and VertexFormat Data
81//--------------------------------------------------------------------------------------
82struct Vertex
83{
84 XGL_FLOAT posX, posY, posZ, posW; // Position data
85 XGL_FLOAT r, g, b, a; // Color
86};
87
88#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
89
90static const Vertex g_vbData[] =
91{
92 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
93 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
94 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
95 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
96 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
97 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
98
99 { XYZ1( -1, -1, 1 ), XYZ1( 0.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, 0.f, 1.f ) },
102 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
103 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
104 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
105
106 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.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, 1.f ) },
109 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
110 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
111 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
112
113 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
114 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
115 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
116 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
117 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
118 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
119
120 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
121 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
122 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
123 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
124 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
125 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
126
127 { XYZ1( 1, -1, 1 ), XYZ1( 1.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, 1.f ) },
130 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
131 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
132 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
133};
134
Cody Northropec1d3c32014-10-28 15:41:42 -0600135static const int uniformBufferCount = 16;
136
Cody Northrop722ff402014-10-20 09:22:42 -0600137class XglRenderTest : public XglRenderFramework
138{
139public:
140 void InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, const void* vertices );
141 void InitTexture();
142 void InitSampler();
Cody Northropec1d3c32014-10-28 15:41:42 -0600143 void InitUniformBuffer(int constantCount, int constantSize, int constantIndex, const void* data);
Cody Northrop722ff402014-10-20 09:22:42 -0600144 void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText);
145 void DrawTriangleTwoUniformsFS(const char *vertShaderText, const char *fragShaderText);
146 void DrawTriangleWithVertexFetch(const char *vertShaderText, const char *fragShaderText);
147 void DrawTriangleVSUniform(const char *vertShaderText, const char *fragShaderText);
Cody Northrop0fdf64e2014-10-20 11:51:06 -0600148 void DrawTriangleFSUniformBlock(const char *vertShaderText, const char *fragShaderText);
Cody Northropec1d3c32014-10-28 15:41:42 -0600149 void DrawTriangleFSUniformBlockBinding(const char *vertShaderText, const char *fragShaderText);
Cody Northrop0fdf64e2014-10-20 11:51:06 -0600150 void DrawTriangleVSUniformBlock(const char *vertShaderText, const char *fragShaderText);
151 void DrawTriangleVSFSUniformBlock(const char *vertShaderText, const char *fragShaderText);
Cody Northropc6953d02014-10-20 11:23:32 -0600152 void DrawTexturedTriangle(const char *vertShaderText, const char *fragShaderText);
Cody Northrop9394e0c2014-10-23 10:21:47 -0600153 void DrawVSTexture(const char *vertShaderText, const char *fragShaderText);
Cody Northrop722ff402014-10-20 09:22:42 -0600154
Cody Northrop0fdf64e2014-10-20 11:51:06 -0600155
Cody Northrop722ff402014-10-20 09:22:42 -0600156 void CreatePipelineWithVertexFetch(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps);
Cody Northropec1d3c32014-10-28 15:41:42 -0600157 void CreatePipelineFSUniformBlockBinding(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps, int bufferCount);
Cody Northrop722ff402014-10-20 09:22:42 -0600158 void CreatePipelineVSUniform(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps);
Cody Northrop0fdf64e2014-10-20 11:51:06 -0600159 void CreatePipelineVSFSUniformBlock(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps);
Cody Northropc6953d02014-10-20 11:23:32 -0600160 void CreatePipelineSingleTextureAndSampler(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps);
Cody Northrop722ff402014-10-20 09:22:42 -0600161
162
163protected:
164 XGL_IMAGE m_texture;
165 XGL_IMAGE_VIEW m_textureView;
166 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
167 XGL_GPU_MEMORY m_textureMem;
168
169 XGL_SAMPLER m_sampler;
170
Cody Northropec1d3c32014-10-28 15:41:42 -0600171 XGL_GPU_MEMORY m_uniformBufferMem[uniformBufferCount];
172 XGL_MEMORY_VIEW_ATTACH_INFO m_uniformBufferView[uniformBufferCount];
173
174
175
Cody Northrop722ff402014-10-20 09:22:42 -0600176// XGL_APPLICATION_INFO app_info;
177// XGL_PHYSICAL_GPU objs[MAX_GPUS];
178// XGL_UINT gpu_count;
179// XGL_GPU_MEMORY m_descriptor_set_mem;
180// XGL_GPU_MEMORY m_pipe_mem;
181// XglDevice *m_device;
182// XGL_CMD_BUFFER m_cmdBuffer;
183// XGL_UINT32 m_numVertices;
184// XGL_MEMORY_VIEW_ATTACH_INFO m_vtxBufferView;
185// XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView;
186// XGL_GPU_MEMORY m_vtxBufferMem;
187// XGL_GPU_MEMORY m_constantBufferMem;
188// XGL_UINT32 m_numMemRefs;
189// XGL_MEMORY_REF m_memRefs[5];
190// XGL_RASTER_STATE_OBJECT m_stateRaster;
191// XGL_COLOR_BLEND_STATE_OBJECT m_colorBlend;
192// XGL_VIEWPORT_STATE_OBJECT m_stateViewport;
193// XGL_DEPTH_STENCIL_STATE_OBJECT m_stateDepthStencil;
194// XGL_MSAA_STATE_OBJECT m_stateMsaa;
195// XGL_DESCRIPTOR_SET m_rsrcDescSet;
196
197 virtual void SetUp() {
198
199 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
200 this->app_info.pNext = NULL;
Cody Northropaa853252014-10-20 09:25:17 -0600201 this->app_info.pAppName = (const XGL_CHAR *) "compiler render_tests";
Cody Northrop722ff402014-10-20 09:22:42 -0600202 this->app_info.appVersion = 1;
203 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
204 this->app_info.engineVersion = 1;
205 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
206
207 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
208 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
209
210 InitFramework();
211 }
212
213 virtual void TearDown() {
214 // Clean up resources before we reset
215 ShutdownFramework();
216 }
217};
218
219// this function will create the vertex buffer and fill it with the mesh data
220void XglRenderTest::InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride,
221 const void* vertices )
222{
223 XGL_RESULT err = XGL_SUCCESS;
224
225 assert( numVertices * vbStride > 0 );
226 m_numVertices = numVertices;
227
228 XGL_MEMORY_ALLOC_INFO alloc_info = {};
229 XGL_UINT8 *pData;
230
231 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
232 alloc_info.allocationSize = numVertices * vbStride;
233 alloc_info.alignment = 0;
234 alloc_info.heapCount = 1;
235 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
236
237 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
238 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
239
240 err = xglAllocMemory(device(), &alloc_info, &m_vtxBufferMem);
241 ASSERT_XGL_SUCCESS(err);
242
243 err = xglMapMemory(m_vtxBufferMem, 0, (XGL_VOID **) &pData);
244 ASSERT_XGL_SUCCESS(err);
245
246 memcpy(pData, vertices, alloc_info.allocationSize);
247
248 err = xglUnmapMemory(m_vtxBufferMem);
249 ASSERT_XGL_SUCCESS(err);
250
251 // set up the memory view for the vertex buffer
252 this->m_vtxBufferView.stride = vbStride;
253 this->m_vtxBufferView.range = numVertices * vbStride;
254 this->m_vtxBufferView.offset = 0;
255 this->m_vtxBufferView.mem = m_vtxBufferMem;
256 this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED;
257 this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED;
258
259 // open the command buffer
260 err = xglBeginCommandBuffer( m_cmdBuffer, 0 );
261 ASSERT_XGL_SUCCESS(err);
262
263 XGL_MEMORY_STATE_TRANSITION transition = {};
264 transition.mem = m_vtxBufferMem;
265 transition.oldState = XGL_MEMORY_STATE_DATA_TRANSFER;
266 transition.newState = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
267 transition.offset = 0;
268 transition.regionSize = numVertices * vbStride;
269
270 // write transition to the command buffer
271 xglCmdPrepareMemoryRegions( m_cmdBuffer, 1, &transition );
272 this->m_vtxBufferView.state = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
273
274 // finish recording the command buffer
275 err = xglEndCommandBuffer( m_cmdBuffer );
276 ASSERT_XGL_SUCCESS(err);
277
278 // this command buffer only uses the vertex buffer memory
279 m_numMemRefs = 1;
280 m_memRefs[0].flags = 0;
281 m_memRefs[0].mem = m_vtxBufferMem;
282
283 // submit the command buffer to the universal queue
284 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
285 ASSERT_XGL_SUCCESS(err);
286}
287
288void XglRenderTest::InitTexture()
289{
290#define DEMO_TEXTURE_COUNT 1
291
292 const XGL_FORMAT tex_format = { XGL_CH_FMT_B8G8R8A8, XGL_NUM_FMT_UNORM };
293 const XGL_INT tex_width = 16;
294 const XGL_INT tex_height = 16;
295 const uint32_t tex_colors[DEMO_TEXTURE_COUNT][2] = {
296 { 0xffff0000, 0xff00ff00 },
297 };
298 XGL_RESULT err;
299 XGL_UINT i;
300
301 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
302 const XGL_SAMPLER_CREATE_INFO sampler = {
303 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
304 .pNext = NULL,
305 .magFilter = XGL_TEX_FILTER_NEAREST,
306 .minFilter = XGL_TEX_FILTER_NEAREST,
307 .mipMode = XGL_TEX_MIPMAP_BASE,
308 .addressU = XGL_TEX_ADDRESS_WRAP,
309 .addressV = XGL_TEX_ADDRESS_WRAP,
310 .addressW = XGL_TEX_ADDRESS_WRAP,
311 .mipLodBias = 0.0f,
312 .maxAnisotropy = 0,
313 .compareFunc = XGL_COMPARE_NEVER,
314 .minLod = 0.0f,
315 .maxLod = 0.0f,
316 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
317 };
318 const XGL_IMAGE_CREATE_INFO image = {
319 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
320 .pNext = NULL,
321 .imageType = XGL_IMAGE_2D,
322 .format = tex_format,
323 .extent = { tex_width, tex_height, 1 },
324 .mipLevels = 1,
325 .arraySize = 1,
326 .samples = 1,
327 .tiling = XGL_LINEAR_TILING,
328 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
329 .flags = 0,
330 };
331 XGL_MEMORY_ALLOC_INFO mem_alloc;
332 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
333 mem_alloc.pNext = NULL;
334 mem_alloc.allocationSize = 0;
335 mem_alloc.alignment = 0;
336 mem_alloc.flags = 0;
337 mem_alloc.heapCount = 0;
338 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
339 XGL_IMAGE_VIEW_CREATE_INFO view;
340 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
341 view.pNext = NULL;
342 view.image = XGL_NULL_HANDLE;
343 view.viewType = XGL_IMAGE_VIEW_2D;
344 view.format = image.format;
345 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
346 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
347 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
348 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
349 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
350 view.subresourceRange.baseMipLevel = 0;
351 view.subresourceRange.mipLevels = 1;
352 view.subresourceRange.baseArraySlice = 0;
353 view.subresourceRange.arraySize = 1;
354 view.minLod = 0.0f;
355
356 XGL_MEMORY_REQUIREMENTS mem_reqs;
357 XGL_SIZE mem_reqs_size;
358
359 /* create sampler */
360 err = xglCreateSampler(device(), &sampler, &m_sampler);
361 assert(!err);
362
363 /* create image */
364 err = xglCreateImage(device(), &image, &m_texture);
365 assert(!err);
366
367 err = xglGetObjectInfo(m_texture,
368 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
369 &mem_reqs_size, &mem_reqs);
370 assert(!err && mem_reqs_size == sizeof(mem_reqs));
371
372 mem_alloc.allocationSize = mem_reqs.size;
373 mem_alloc.alignment = mem_reqs.alignment;
374 mem_alloc.heapCount = mem_reqs.heapCount;
375 memcpy(mem_alloc.heaps, mem_reqs.heaps,
376 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
377
378 /* allocate memory */
379 err = xglAllocMemory(device(), &mem_alloc, &m_textureMem);
380 assert(!err);
381
382 /* bind memory */
383 err = xglBindObjectMemory(m_texture, m_textureMem, 0);
384 assert(!err);
385
386 /* create image view */
387 view.image = m_texture;
388 err = xglCreateImageView(device(), &view, &m_textureView);
389 assert(!err);
390 }
391
392 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
393 const XGL_IMAGE_SUBRESOURCE subres = {
394 .aspect = XGL_IMAGE_ASPECT_COLOR,
395 .mipLevel = 0,
396 .arraySlice = 0,
397 };
398 XGL_SUBRESOURCE_LAYOUT layout;
399 XGL_SIZE layout_size;
400 XGL_VOID *data;
401 XGL_INT x, y;
402
403 err = xglGetImageSubresourceInfo(m_texture, &subres,
404 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
405 assert(!err && layout_size == sizeof(layout));
406
407 err = xglMapMemory(m_textureMem, 0, &data);
408 assert(!err);
409
410 for (y = 0; y < tex_height; y++) {
411 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
412 for (x = 0; x < tex_width; x++)
413 row[x] = tex_colors[i][(x & 1) ^ (y & 1)];
414 }
415
416 err = xglUnmapMemory(m_textureMem);
417 assert(!err);
418 }
419
420 m_textureViewInfo.view = m_textureView;
421}
422
423void XglRenderTest::InitSampler()
424{
425 XGL_RESULT err;
426
427 XGL_SAMPLER_CREATE_INFO samplerCreateInfo = {};
428 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
429 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
430 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
431 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
432 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
433 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
434 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
435 samplerCreateInfo.mipLodBias = 0.0;
436 samplerCreateInfo.maxAnisotropy = 0.0;
437 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
438 samplerCreateInfo.minLod = 0.0;
439 samplerCreateInfo.maxLod = 0.0;
440 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
441
442 err = xglCreateSampler(device(),&samplerCreateInfo, &m_sampler);
443 ASSERT_XGL_SUCCESS(err);
444}
445
Cody Northropec1d3c32014-10-28 15:41:42 -0600446void XglRenderTest::InitUniformBuffer(int constantCount, int constantSize,
447 int constantIndex, const void* data)
Cody Northrop722ff402014-10-20 09:22:42 -0600448{
Cody Northropec1d3c32014-10-28 15:41:42 -0600449 // based on XglRenderFramework::InitConstantBuffer
450 // mainly add an index when selecting which buffer you are creating
451
452 XGL_RESULT err = XGL_SUCCESS;
453
454 XGL_MEMORY_ALLOC_INFO alloc_info = {};
455 XGL_UINT8 *pData;
456
457 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
458 alloc_info.allocationSize = constantCount * constantSize;
459 alloc_info.alignment = 0;
460 alloc_info.heapCount = 1;
461 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
462
463 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
464 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
465
466 err = xglAllocMemory(device(), &alloc_info, &m_uniformBufferMem[constantIndex]);
467 ASSERT_XGL_SUCCESS(err);
468
469 err = xglMapMemory(m_uniformBufferMem[constantIndex], 0, (XGL_VOID **) &pData);
470 ASSERT_XGL_SUCCESS(err);
471
472 memcpy(pData, data, alloc_info.allocationSize);
473
474 err = xglUnmapMemory(m_uniformBufferMem[constantIndex]);
475 ASSERT_XGL_SUCCESS(err);
476
477 // set up the memory view for the constant buffer
478 this->m_uniformBufferView[constantIndex].stride = 16;
479 this->m_uniformBufferView[constantIndex].range = alloc_info.allocationSize;
480 this->m_uniformBufferView[constantIndex].offset = 0;
481 this->m_uniformBufferView[constantIndex].mem = m_uniformBufferMem[constantIndex];
482 this->m_uniformBufferView[constantIndex].format.channelFormat = XGL_CH_FMT_R32G32B32A32;
483 this->m_uniformBufferView[constantIndex].format.numericFormat = XGL_NUM_FMT_FLOAT;
Cody Northrop722ff402014-10-20 09:22:42 -0600484}
485
486void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText)
487{
488 XGL_PIPELINE pipeline;
489 XGL_SHADER vs, ps;
490 XGL_RESULT err;
491
492 ASSERT_NO_FATAL_FAILURE(InitState());
493 ASSERT_NO_FATAL_FAILURE(InitViewport());
494
495 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
496 vertShaderText, &vs));
497
498 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
499 fragShaderText, &ps));
500
501 ASSERT_NO_FATAL_FAILURE(CreateDefaultPipeline(&pipeline, vs, ps));
502
503 /*
504 * Shaders are now part of the pipeline, don't need these anymore
505 */
506 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
507 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
508
509 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
510
511 const int constantCount = 4;
512 const float constants[constantCount] = { 0.5, 0.5, 0.5, 1.0 };
513 InitConstantBuffer(constantCount, sizeof(constants[0]), (const void*) constants);
514
515 // Create descriptor set for a uniform resource
516 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
517 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
518 descriptorInfo.slots = 1;
519
520 // create a descriptor set with a single slot
521 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
522 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
523
524 // bind memory to the descriptor set
525 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
526
527 // write the constant buffer view to the descriptor set
528 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
529 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
530 xglEndDescriptorSetUpdate( m_rsrcDescSet );
531
532 // Build command buffer
533 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
534 ASSERT_XGL_SUCCESS(err);
535
536 GenerateClearAndPrepareBufferCmds();
537 GenerateBindRenderTargetCmd();
538 GenerateBindStateAndPipelineCmds(&pipeline);
539
540// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
541// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
542
543 // render the cube
544 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
545
546 // prepare the back buffer for present
547// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
548// transitionToPresent.image = m_image;
549// transitionToPresent.oldState = m_image_state;
550// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
551// transitionToPresent.subresourceRange = srRange;
552// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
553// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
554
555 // finalize recording of the command buffer
556 err = xglEndCommandBuffer( m_cmdBuffer );
557 ASSERT_XGL_SUCCESS( err );
558
559 // this command buffer only uses the vertex buffer memory
560 m_numMemRefs = 0;
561// m_memRefs[0].flags = 0;
562// m_memRefs[0].mem = m_vtxBufferMemory;
563
564 // submit the command buffer to the universal queue
565 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
566 ASSERT_XGL_SUCCESS( err );
567
568 err = xglQueueWaitIdle( m_device->m_queue );
569 ASSERT_XGL_SUCCESS( err );
570
571 // Wait for work to finish before cleaning up.
572 xglDeviceWaitIdle(m_device->device());
573
574 RecordImage(m_renderTarget);
575
576}
577
578void XglRenderTest::DrawTriangleTwoUniformsFS(const char *vertShaderText, const char *fragShaderText)
579{
580 XGL_PIPELINE pipeline;
581 XGL_SHADER vs, ps;
582 XGL_RESULT err;
583
584 ASSERT_NO_FATAL_FAILURE(InitState());
585 ASSERT_NO_FATAL_FAILURE(InitViewport());
586
587 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
588 vertShaderText, &vs));
589
590 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
591 fragShaderText, &ps));
592
593 ASSERT_NO_FATAL_FAILURE(CreateDefaultPipeline(&pipeline, vs, ps));
594
595 /*
596 * Shaders are now part of the pipeline, don't need these anymore
597 */
598 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
599 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
600
601 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
602
603 const int constantCount = 8;
604 const float constants[constantCount] = { 1.0, 0.0, 0.0, 1.0,
605 0.0, 0.0, 1.0, 1.0 };
606
607 InitConstantBuffer(constantCount, sizeof(constants[0]), (const void*) constants);
608
609 // Create descriptor set for a uniform resource
610 const int slotCount = 1;
611 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
612 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
613 descriptorInfo.slots = slotCount;
614
615 // create a descriptor set with a single slot
616 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
617 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
618
619 // bind memory to the descriptor set
620 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
621
622 // write the constant buffer view to the descriptor set
623 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
624 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
625 xglEndDescriptorSetUpdate( m_rsrcDescSet );
626
627 // Build command buffer
628 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
629 ASSERT_XGL_SUCCESS(err);
630
631 GenerateClearAndPrepareBufferCmds();
632 GenerateBindRenderTargetCmd();
633 GenerateBindStateAndPipelineCmds(&pipeline);
634
635// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
636// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
637
638 // render the cube
639 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
640
641 // prepare the back buffer for present
642// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
643// transitionToPresent.image = m_image;
644// transitionToPresent.oldState = m_image_state;
645// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
646// transitionToPresent.subresourceRange = srRange;
647// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
648// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
649
650 // finalize recording of the command buffer
651 err = xglEndCommandBuffer( m_cmdBuffer );
652 ASSERT_XGL_SUCCESS( err );
653
654 // this command buffer only uses the vertex buffer memory
655 m_numMemRefs = 0;
656// m_memRefs[0].flags = 0;
657// m_memRefs[0].mem = m_vtxBufferMemory;
658
659 // submit the command buffer to the universal queue
660 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
661 ASSERT_XGL_SUCCESS( err );
662
663 err = xglQueueWaitIdle( m_device->m_queue );
664 ASSERT_XGL_SUCCESS( err );
665
666 // Wait for work to finish before cleaning up.
667 xglDeviceWaitIdle(m_device->device());
668
669 RecordImage(m_renderTarget);
670
671}
672
Cody Northropc6953d02014-10-20 11:23:32 -0600673void XglRenderTest::DrawTexturedTriangle(const char *vertShaderText, const char *fragShaderText)
674{
675
676 // based on DrawTriangleTwoUniformsFS
677
678 XGL_PIPELINE pipeline;
679 XGL_SHADER vs, ps;
680 XGL_RESULT err;
681
682 ASSERT_NO_FATAL_FAILURE(InitState());
683 ASSERT_NO_FATAL_FAILURE(InitViewport());
684
685 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
686 vertShaderText, &vs));
687
688 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
689 fragShaderText, &ps));
690
691 ASSERT_NO_FATAL_FAILURE(CreatePipelineSingleTextureAndSampler(&pipeline, vs, ps));
692
693 /*
694 * Shaders are now part of the pipeline, don't need these anymore
695 */
696 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
697 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
698
699 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
700
701
702 // Enable our single sampler
703 ASSERT_NO_FATAL_FAILURE(InitSampler());
704
705 // Enable our single texture
706 ASSERT_NO_FATAL_FAILURE(InitTexture());
707
708 // Create descriptor set for a texture and sampler resources
Cody Northrop5fa1d332014-10-20 11:51:32 -0600709 const int slotCount = 2;
Cody Northropc6953d02014-10-20 11:23:32 -0600710 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
711 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
712 descriptorInfo.slots = slotCount;
713
714 // create a descriptor set with a single slot
715 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
716 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
717
718 // bind memory to the descriptor set
719 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
720
721 // write the sampler and image views to the descriptor set
722 // ensure this matches order set in CreatePipelineSingleTextureAndSampler
723 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
724 xglAttachImageViewDescriptors( m_rsrcDescSet, 0, 1, &m_textureViewInfo );
725 xglAttachSamplerDescriptors(m_rsrcDescSet, 1, 1, &m_sampler);
726 xglEndDescriptorSetUpdate( m_rsrcDescSet );
727
728 // Build command buffer
729 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
730 ASSERT_XGL_SUCCESS(err);
731
732 GenerateClearAndPrepareBufferCmds();
733 GenerateBindRenderTargetCmd();
734 GenerateBindStateAndPipelineCmds(&pipeline);
735
736// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
737// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
738
739 // render the cube
740 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
741
742 // prepare the back buffer for present
743// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
744// transitionToPresent.image = m_image;
745// transitionToPresent.oldState = m_image_state;
746// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
747// transitionToPresent.subresourceRange = srRange;
748// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
749// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
750
751 // finalize recording of the command buffer
752 err = xglEndCommandBuffer( m_cmdBuffer );
753 ASSERT_XGL_SUCCESS( err );
754
755 // this command buffer only uses the vertex buffer memory
756 m_numMemRefs = 0;
757// m_memRefs[0].flags = 0;
758// m_memRefs[0].mem = m_vtxBufferMemory;
759
760 // submit the command buffer to the universal queue
761 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
762 ASSERT_XGL_SUCCESS( err );
763
764 err = xglQueueWaitIdle( m_device->m_queue );
765 ASSERT_XGL_SUCCESS( err );
766
767 // Wait for work to finish before cleaning up.
768 xglDeviceWaitIdle(m_device->device());
769
770 RecordImage(m_renderTarget);
771
772}
Cody Northrop722ff402014-10-20 09:22:42 -0600773
774void XglRenderTest::DrawTriangleVSUniform(const char *vertShaderText, const char *fragShaderText)
775{
776 XGL_PIPELINE pipeline;
777 XGL_SHADER vs, ps;
778 XGL_RESULT err;
779 glm::mat4 MVP;
780 int i;
781
782 // Create identity matrix
783 glm::mat4 Model = glm::mat4(1.0f);
784
785 ASSERT_NO_FATAL_FAILURE(InitState());
786 ASSERT_NO_FATAL_FAILURE(InitViewport());
787
788 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
789 vertShaderText, &vs));
790
791 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
792 fragShaderText, &ps));
793
794 ASSERT_NO_FATAL_FAILURE(CreatePipelineVSUniform(&pipeline, vs, ps));
795
796 /*
797 * Shaders are now part of the pipeline, don't need these anymore
798 */
799 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
800 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
801
802 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
803
804 const int matrixSize = 16;
805 MVP = Model;
806
807 InitConstantBuffer(matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
808
809 // Create descriptor set for a uniform resource
810 const int slotCount = 1;
811 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
812 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
813 descriptorInfo.slots = slotCount;
814
815 // create a descriptor set with a single slot
816 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
817 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
818
819 // bind memory to the descriptor set
820 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
821
822 // write the constant buffer view to the descriptor set
823 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
824 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
825 xglEndDescriptorSetUpdate( m_rsrcDescSet );
826
827 // Build command buffer
828 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
829 ASSERT_XGL_SUCCESS(err);
830
831 GenerateClearAndPrepareBufferCmds();
832 GenerateBindRenderTargetCmd();
833 GenerateBindStateAndPipelineCmds(&pipeline);
834
835// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
836// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
837
838 // render the cube
839 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
840
841 // prepare the back buffer for present
842// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
843// transitionToPresent.image = m_image;
844// transitionToPresent.oldState = m_image_state;
845// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
846// transitionToPresent.subresourceRange = srRange;
847// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
848// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
849
850 // finalize recording of the command buffer
851 err = xglEndCommandBuffer( m_cmdBuffer );
852 ASSERT_XGL_SUCCESS( err );
853
854 // this command buffer only uses the vertex buffer memory
855 m_numMemRefs = 0;
856// m_memRefs[0].flags = 0;
857// m_memRefs[0].mem = m_vtxBufferMemory;
858
859 // submit the command buffer to the universal queue
860 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
861 ASSERT_XGL_SUCCESS( err );
862
863 err = xglQueueWaitIdle( m_device->m_queue );
864 ASSERT_XGL_SUCCESS( err );
865
866 // Wait for work to finish before cleaning up.
867 xglDeviceWaitIdle(m_device->device());
868
869 RecordImage(m_renderTarget);
870
871 for (i = 0; i < 8; i++) {
872 XGL_UINT8 *pData;
873 err = xglMapMemory(m_constantBufferMem, 0, (XGL_VOID **) &pData);
874 ASSERT_XGL_SUCCESS(err);
875
876 MVP = glm::rotate(MVP, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
877 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
878
879 err = xglUnmapMemory(m_constantBufferMem);
880 ASSERT_XGL_SUCCESS(err);
881
882 // submit the command buffer to the universal queue
883 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
884 ASSERT_XGL_SUCCESS( err );
885
886 err = xglQueueWaitIdle( m_device->m_queue );
887 ASSERT_XGL_SUCCESS( err );
888
889 // Wait for work to finish before cleaning up.
890 xglDeviceWaitIdle(m_device->device());
891
892 RecordImage(m_renderTarget);
893 }
894}
895
Cody Northrop0fdf64e2014-10-20 11:51:06 -0600896void XglRenderTest::DrawTriangleVSUniformBlock(const char *vertShaderText, const char *fragShaderText)
897{
898 // sourced from DrawTriangleVSUniform
899 XGL_PIPELINE pipeline;
900 XGL_SHADER vs, ps;
901 XGL_RESULT err;
902
903 ASSERT_NO_FATAL_FAILURE(InitState());
904 ASSERT_NO_FATAL_FAILURE(InitViewport());
905
906 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
907 vertShaderText, &vs));
908
909 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
910 fragShaderText, &ps));
911
912 ASSERT_NO_FATAL_FAILURE(CreatePipelineVSUniform(&pipeline, vs, ps));
913
914 /*
915 * Shaders are now part of the pipeline, don't need these anymore
916 */
917 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
918 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
919
920 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
921
922 // Let's populate our buffer with the following:
923 // vec4 red;
924 // vec4 green;
925 // vec4 blue;
926 // vec4 white;
927 const int valCount = 4 * 4;
928 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
929 0.0, 1.0, 0.0, 1.0,
930 0.0, 0.0, 1.0, 1.0,
931 1.0, 1.0, 1.0, 1.0 };
932
933 InitConstantBuffer(valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
934
935 // Create descriptor set for a uniform resource
936 const int slotCount = 1;
937 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
938 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
939 descriptorInfo.slots = slotCount;
940
941 // create a descriptor set with a single slot
942 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
943 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
944
945 // bind memory to the descriptor set
946 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
947
948 // write the constant buffer view to the descriptor set
949 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
950 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
951 xglEndDescriptorSetUpdate( m_rsrcDescSet );
952
953 // Build command buffer
954 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
955 ASSERT_XGL_SUCCESS(err);
956
957 GenerateClearAndPrepareBufferCmds();
958 GenerateBindRenderTargetCmd();
959 GenerateBindStateAndPipelineCmds(&pipeline);
960
961// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
962// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
963
964 // render the cube
965 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
966
967 // prepare the back buffer for present
968// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
969// transitionToPresent.image = m_image;
970// transitionToPresent.oldState = m_image_state;
971// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
972// transitionToPresent.subresourceRange = srRange;
973// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
974// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
975
976 // finalize recording of the command buffer
977 err = xglEndCommandBuffer( m_cmdBuffer );
978 ASSERT_XGL_SUCCESS( err );
979
980 // this command buffer only uses the vertex buffer memory
981 m_numMemRefs = 0;
982// m_memRefs[0].flags = 0;
983// m_memRefs[0].mem = m_vtxBufferMemory;
984
985 // submit the command buffer to the universal queue
986 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
987 ASSERT_XGL_SUCCESS( err );
988
989 err = xglQueueWaitIdle( m_device->m_queue );
990 ASSERT_XGL_SUCCESS( err );
991
992 // Wait for work to finish before cleaning up.
993 xglDeviceWaitIdle(m_device->device());
994
995 RecordImage(m_renderTarget);
996}
997
Cody Northrop722ff402014-10-20 09:22:42 -0600998void XglRenderTest::CreatePipelineWithVertexFetch(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps)
999{
1000 XGL_RESULT err;
1001 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1002 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
1003 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
1004
1005
1006 // Create descriptor set for our one resource
1007 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
1008 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1009 descriptorInfo.slots = 1; // Vertex buffer only
1010
1011 // create a descriptor set with a single slot
1012 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1013 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1014
1015 // bind memory to the descriptor set
1016 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1017
1018 // write the vertex buffer view to the descriptor set
1019 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
1020 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_vtxBufferView );
1021 xglEndDescriptorSetUpdate( m_rsrcDescSet );
1022
1023 const int slots = 1;
1024 XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( slots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
1025 slotInfo[0].shaderEntityIndex = 0;
1026 slotInfo[0].slotObjectType = XGL_SLOT_VERTEX_INPUT;
1027
1028 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1029 vs_stage.pNext = XGL_NULL_HANDLE;
1030 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
1031 vs_stage.shader.shader = vs;
1032 vs_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
1033 vs_stage.shader.descriptorSetMapping[0].descriptorCount = slots;
1034 vs_stage.shader.linkConstBufferCount = 0;
1035 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1036 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1037 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1038
1039 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1040 ps_stage.pNext = &vs_stage;
1041 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1042 ps_stage.shader.shader = ps;
1043 ps_stage.shader.descriptorSetMapping[0].descriptorCount = 0;
1044 ps_stage.shader.linkConstBufferCount = 0;
1045 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1046 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1047 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1048
1049 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1050 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1051 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1052 };
1053
1054 // this is the current description of g_vbData
1055 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1056 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1057 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1058 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1059 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1060 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1061 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1062 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1063 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1064
1065 XGL_PIPELINE_VERTEX_INPUT_CREATE_INFO vi_state = {
1066 XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO, // sType;
1067 &ps_stage, // pNext;
1068 1, // bindingCount
1069 &vi_binding, // pVertexBindingDescriptions;
1070 2, // attributeCount; // number of attributes
1071 vi_attribs // pVertexAttributeDescriptions;
1072 };
1073
1074 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
1075 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
1076 &vi_state, // pNext
1077 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
1078 XGL_FALSE, // disableVertexReuse
1079 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
1080 XGL_FALSE, // primitiveRestartEnable
1081 0 // primitiveRestartIndex
1082 };
1083
1084 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
1085 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
1086 &ia_state,
1087 XGL_FALSE, // depthClipEnable
1088 XGL_FALSE, // rasterizerDiscardEnable
1089 1.0 // pointSize
1090 };
1091
1092 XGL_PIPELINE_CB_STATE cb_state = {
1093 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
1094 &rs_state,
1095 XGL_FALSE, // alphaToCoverageEnable
1096 XGL_FALSE, // dualSourceBlendEnable
1097 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
1098 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
1099 {
1100 XGL_FALSE, // blendEnable
1101 m_render_target_fmt, // XGL_FORMAT
1102 0xF // channelWriteMask
1103 }
1104 }
1105 };
1106
1107 // TODO: Should take depth buffer format from queried formats
1108 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
1109 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
1110 &cb_state,
1111 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
1112 };
1113
1114 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1115 info.pNext = &db_state;
1116 info.flags = 0;
1117 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
1118 ASSERT_XGL_SUCCESS(err);
1119
1120 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
1121 ASSERT_XGL_SUCCESS(err);
1122}
1123
1124void XglRenderTest::CreatePipelineVSUniform(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps)
1125{
1126 XGL_RESULT err;
1127 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1128 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
1129 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
1130
1131
1132 const int vsSlots = 1; // Uniform buffer only
1133
1134 // Create descriptor set for our one resource
1135 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
1136 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1137 descriptorInfo.slots = vsSlots;
1138
1139 // create a descriptor set with a single slot
1140 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1141 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1142
1143 // bind memory to the descriptor set
1144 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1145
1146
1147 XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( vsSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
1148 slotInfo[0].shaderEntityIndex = 0;
1149 slotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1150
1151 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1152 vs_stage.pNext = XGL_NULL_HANDLE;
1153 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
1154 vs_stage.shader.shader = vs;
1155 vs_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
1156 vs_stage.shader.descriptorSetMapping[0].descriptorCount = vsSlots;
1157 vs_stage.shader.linkConstBufferCount = 0;
1158 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1159 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1160 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1161
1162 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1163 ps_stage.pNext = &vs_stage;
1164 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1165 ps_stage.shader.shader = ps;
1166 ps_stage.shader.descriptorSetMapping[0].descriptorCount = 0;
1167 ps_stage.shader.linkConstBufferCount = 0;
1168 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1169 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1170 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1171
1172 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
1173 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
1174 &ps_stage, // pNext
1175 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
1176 XGL_FALSE, // disableVertexReuse
1177 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
1178 XGL_FALSE, // primitiveRestartEnable
1179 0 // primitiveRestartIndex
1180 };
1181
1182 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
1183 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
1184 &ia_state,
1185 XGL_FALSE, // depthClipEnable
1186 XGL_FALSE, // rasterizerDiscardEnable
1187 1.0 // pointSize
1188 };
1189
1190 XGL_PIPELINE_CB_STATE cb_state = {
1191 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
1192 &rs_state,
1193 XGL_FALSE, // alphaToCoverageEnable
1194 XGL_FALSE, // dualSourceBlendEnable
1195 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
1196 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
1197 {
1198 XGL_FALSE, // blendEnable
1199 m_render_target_fmt, // XGL_FORMAT
1200 0xF // channelWriteMask
1201 }
1202 }
1203 };
1204
1205 // TODO: Should take depth buffer format from queried formats
1206 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
1207 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
1208 &cb_state,
1209 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
1210 };
1211
1212 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1213 info.pNext = &db_state;
1214 info.flags = 0;
1215 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
1216 ASSERT_XGL_SUCCESS(err);
1217
1218 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
1219 ASSERT_XGL_SUCCESS(err);
1220}
1221
Cody Northropec1d3c32014-10-28 15:41:42 -06001222void XglRenderTest::CreatePipelineFSUniformBlockBinding(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps, const int bufferCount)
1223{
1224 // based on CreateDefaultPipeline
1225 // only difference is number of constant buffers
1226
1227 XGL_RESULT err;
1228 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1229 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
1230 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
1231
1232 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1233 vs_stage.pNext = XGL_NULL_HANDLE;
1234 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
1235 vs_stage.shader.shader = vs;
1236 vs_stage.shader.descriptorSetMapping[0].descriptorCount = 0;
1237 vs_stage.shader.linkConstBufferCount = 0;
1238 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1239 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1240 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1241
1242 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1243 ps_stage.pNext = &vs_stage;
1244 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1245 ps_stage.shader.shader = ps;
1246
1247// const int slots = 4;
1248// assert (slots == bufferCount); // update as needed
1249
1250 XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( bufferCount * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
1251 for (int i = 0; i < bufferCount; ++i) {
1252 // Note: These are all constant buffers
1253 slotInfo[i].shaderEntityIndex = i;
1254 slotInfo[i].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1255 }
1256
1257 ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
1258 ps_stage.shader.descriptorSetMapping[0].descriptorCount = bufferCount;
1259
1260 ps_stage.shader.linkConstBufferCount = 0;
1261 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1262 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1263 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1264
1265 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
1266 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
1267 &ps_stage, // pNext
1268 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
1269 XGL_FALSE, // disableVertexReuse
1270 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
1271 XGL_FALSE, // primitiveRestartEnable
1272 0 // primitiveRestartIndex
1273 };
1274
1275 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
1276 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
1277 &ia_state,
1278 XGL_FALSE, // depthClipEnable
1279 XGL_FALSE, // rasterizerDiscardEnable
1280 1.0 // pointSize
1281 };
1282
1283 XGL_PIPELINE_CB_STATE cb_state = {
1284 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
1285 &rs_state,
1286 XGL_FALSE, // alphaToCoverageEnable
1287 XGL_FALSE, // dualSourceBlendEnable
1288 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
1289 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
1290 {
1291 XGL_FALSE, // blendEnable
1292 m_render_target_fmt, // XGL_FORMAT
1293 0xF // channelWriteMask
1294 }
1295 }
1296 };
1297
1298 // TODO: Should take depth buffer format from queried formats
1299 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
1300 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
1301 &cb_state,
1302 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
1303 };
1304
1305 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1306 info.pNext = &db_state;
1307 info.flags = 0;
1308 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
1309 ASSERT_XGL_SUCCESS(err);
1310
1311 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
1312 ASSERT_XGL_SUCCESS(err);
1313}
1314
Cody Northrop0fdf64e2014-10-20 11:51:06 -06001315void XglRenderTest::CreatePipelineVSFSUniformBlock(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps)
1316{
1317 // this is based on CreatePipelineVSUniform
1318
1319 XGL_RESULT err;
1320 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1321 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
1322 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
1323
1324
1325 const int slots = 1; // Uniform buffer only
1326
1327 // Create descriptor set for our one resource
1328 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
1329 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1330 descriptorInfo.slots = slots;
1331
1332 // create a descriptor set with a single slot
1333 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1334 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1335
1336 // bind memory to the descriptor set
1337 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1338
1339
1340 XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( slots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
1341 slotInfo[0].shaderEntityIndex = 0;
1342 slotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1343
1344 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1345 vs_stage.pNext = XGL_NULL_HANDLE;
1346 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
1347 vs_stage.shader.shader = vs;
1348 vs_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
1349 vs_stage.shader.descriptorSetMapping[0].descriptorCount = slots;
1350 vs_stage.shader.linkConstBufferCount = 0;
1351 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1352 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1353 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1354
1355 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1356 ps_stage.pNext = &vs_stage;
1357 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1358 ps_stage.shader.shader = ps;
1359 ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
1360 ps_stage.shader.descriptorSetMapping[0].descriptorCount = slots;
1361 ps_stage.shader.linkConstBufferCount = 0;
1362 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1363 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1364 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1365
1366 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
1367 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
1368 &ps_stage, // pNext
1369 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
1370 XGL_FALSE, // disableVertexReuse
1371 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
1372 XGL_FALSE, // primitiveRestartEnable
1373 0 // primitiveRestartIndex
1374 };
1375
1376 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
1377 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
1378 &ia_state,
1379 XGL_FALSE, // depthClipEnable
1380 XGL_FALSE, // rasterizerDiscardEnable
1381 1.0 // pointSize
1382 };
1383
1384 XGL_PIPELINE_CB_STATE cb_state = {
1385 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
1386 &rs_state,
1387 XGL_FALSE, // alphaToCoverageEnable
1388 XGL_FALSE, // dualSourceBlendEnable
1389 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
1390 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
1391 {
1392 XGL_FALSE, // blendEnable
1393 m_render_target_fmt, // XGL_FORMAT
1394 0xF // channelWriteMask
1395 }
1396 }
1397 };
1398
1399 // TODO: Should take depth buffer format from queried formats
1400 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
1401 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
1402 &cb_state,
1403 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
1404 };
1405
1406 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1407 info.pNext = &db_state;
1408 info.flags = 0;
1409 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
1410 ASSERT_XGL_SUCCESS(err);
1411
1412 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
1413 ASSERT_XGL_SUCCESS(err);
1414}
1415
Cody Northropc6953d02014-10-20 11:23:32 -06001416void XglRenderTest::CreatePipelineSingleTextureAndSampler(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps)
1417{
1418 // based on CreatePipelineVSUniform
1419
1420 XGL_RESULT err;
1421 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1422 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
1423 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
1424
Cody Northrop9394e0c2014-10-23 10:21:47 -06001425 const int vsSlots = 2; // One texture, one sampler
Cody Northropc6953d02014-10-20 11:23:32 -06001426
1427 // Create descriptor set for single texture and sampler
Cody Northrop9394e0c2014-10-23 10:21:47 -06001428 XGL_DESCRIPTOR_SET_CREATE_INFO vsDescriptorInfo = {};
1429 vsDescriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1430 vsDescriptorInfo.slots = vsSlots;
1431 err = xglCreateDescriptorSet( device(), &vsDescriptorInfo, &m_rsrcDescSet );
Cody Northropc6953d02014-10-20 11:23:32 -06001432 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1433 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1434
Cody Northropc6953d02014-10-20 11:23:32 -06001435 // Assign the slots, note that only t0 and s0 will work as of writing this test
Cody Northrop9394e0c2014-10-23 10:21:47 -06001436 XGL_DESCRIPTOR_SLOT_INFO *vsSlotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( vsSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
1437 vsSlotInfo[0].shaderEntityIndex = 0;
1438 vsSlotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1439 vsSlotInfo[1].shaderEntityIndex = 0;
1440 vsSlotInfo[1].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
Cody Northropc6953d02014-10-20 11:23:32 -06001441
1442 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1443 vs_stage.pNext = XGL_NULL_HANDLE;
1444 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
1445 vs_stage.shader.shader = vs;
Cody Northrop9394e0c2014-10-23 10:21:47 -06001446 vs_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) vsSlotInfo;
1447 vs_stage.shader.descriptorSetMapping[0].descriptorCount = vsSlots;
Cody Northropc6953d02014-10-20 11:23:32 -06001448 vs_stage.shader.linkConstBufferCount = 0;
1449 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1450 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1451 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1452
Cody Northrop9394e0c2014-10-23 10:21:47 -06001453 const int psSlots = 2; // One texture, one sampler
1454
1455 // Create descriptor set for single texture and sampler
1456 XGL_DESCRIPTOR_SET_CREATE_INFO psDescriptorInfo = {};
1457 psDescriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1458 psDescriptorInfo.slots = psSlots;
1459 err = xglCreateDescriptorSet( device(), &psDescriptorInfo, &m_rsrcDescSet );
1460 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1461 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1462
1463 // Assign the slots, note that only t0 and s0 will work as of writing this test
1464 XGL_DESCRIPTOR_SLOT_INFO *psSlotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( psSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
1465 psSlotInfo[0].shaderEntityIndex = 0;
1466 psSlotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1467 psSlotInfo[1].shaderEntityIndex = 0;
1468 psSlotInfo[1].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
1469
Cody Northropc6953d02014-10-20 11:23:32 -06001470 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1471 ps_stage.pNext = &vs_stage;
1472 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1473 ps_stage.shader.shader = ps;
Cody Northrop9394e0c2014-10-23 10:21:47 -06001474 ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) psSlotInfo;
Cody Northropc6953d02014-10-20 11:23:32 -06001475 ps_stage.shader.descriptorSetMapping[0].descriptorCount = psSlots;
1476 ps_stage.shader.linkConstBufferCount = 0;
1477 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1478 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1479 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1480
1481 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
1482 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
1483 &ps_stage, // pNext
1484 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
1485 XGL_FALSE, // disableVertexReuse
1486 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
1487 XGL_FALSE, // primitiveRestartEnable
1488 0 // primitiveRestartIndex
1489 };
1490
1491 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
1492 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
1493 &ia_state,
1494 XGL_FALSE, // depthClipEnable
1495 XGL_FALSE, // rasterizerDiscardEnable
1496 1.0 // pointSize
1497 };
1498
1499 XGL_PIPELINE_CB_STATE cb_state = {
1500 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
1501 &rs_state,
1502 XGL_FALSE, // alphaToCoverageEnable
1503 XGL_FALSE, // dualSourceBlendEnable
1504 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
1505 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
1506 {
1507 XGL_FALSE, // blendEnable
1508 m_render_target_fmt, // XGL_FORMAT
1509 0xF // channelWriteMask
1510 }
1511 }
1512 };
1513
1514 // TODO: Should take depth buffer format from queried formats
1515 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
1516 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
1517 &cb_state,
1518 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
1519 };
1520
1521 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1522 info.pNext = &db_state;
1523 info.flags = 0;
1524 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
1525 ASSERT_XGL_SUCCESS(err);
1526
1527 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
1528 ASSERT_XGL_SUCCESS(err);
1529}
1530
Cody Northrop722ff402014-10-20 09:22:42 -06001531void XglRenderTest::DrawTriangleWithVertexFetch(const char *vertShaderText, const char *fragShaderText)
1532{
1533 XGL_PIPELINE pipeline;
1534 XGL_SHADER vs, ps;
1535 XGL_RESULT err;
1536
1537 ASSERT_NO_FATAL_FAILURE(InitState());
1538 ASSERT_NO_FATAL_FAILURE(InitViewport());
1539 ASSERT_NO_FATAL_FAILURE(InitMesh(sizeof(g_vbData)/sizeof(g_vbData[0]), sizeof(g_vbData[0]), g_vbData));
1540
1541 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
1542 vertShaderText, &vs));
1543
1544 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
1545 fragShaderText, &ps));
1546
1547 ASSERT_NO_FATAL_FAILURE(CreatePipelineWithVertexFetch(&pipeline, vs, ps));
1548
1549 /*
1550 * Shaders are now part of the pipeline, don't need these anymore
1551 */
1552 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
1553 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
1554
1555 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1556
1557 // Build command buffer
1558 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1559 ASSERT_XGL_SUCCESS(err);
1560
1561 GenerateClearAndPrepareBufferCmds();
1562 GenerateBindRenderTargetCmd();
1563 GenerateBindStateAndPipelineCmds(&pipeline);
1564
1565// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
1566// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
1567
1568 // render the cube
1569 xglCmdDraw( m_cmdBuffer, 0, 6, 0, 1 );
1570
1571 // prepare the back buffer for present
1572// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
1573// transitionToPresent.image = m_image;
1574// transitionToPresent.oldState = m_image_state;
1575// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
1576// transitionToPresent.subresourceRange = srRange;
1577// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
1578// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
1579
1580 // finalize recording of the command buffer
1581 err = xglEndCommandBuffer( m_cmdBuffer );
1582 ASSERT_XGL_SUCCESS( err );
1583
1584 // this command buffer only uses the vertex buffer memory
1585 m_numMemRefs = 0;
1586// m_memRefs[0].flags = 0;
1587// m_memRefs[0].mem = m_vtxBufferMemory;
1588
1589 // submit the command buffer to the universal queue
1590 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
1591 ASSERT_XGL_SUCCESS( err );
1592
1593 err = xglQueueWaitIdle( m_device->m_queue );
1594 ASSERT_XGL_SUCCESS( err );
1595
1596 // Wait for work to finish before cleaning up.
1597 xglDeviceWaitIdle(m_device->device());
1598
1599 RecordImage(m_renderTarget);
1600
1601}
1602
Cody Northrop0fdf64e2014-10-20 11:51:06 -06001603void XglRenderTest::DrawTriangleFSUniformBlock(const char *vertShaderText, const char *fragShaderText)
1604{
1605 // probably sourced from DrawTriangleTwoUniformsFS
1606
1607 XGL_PIPELINE pipeline;
1608 XGL_SHADER vs, ps;
1609 XGL_RESULT err;
1610
1611 ASSERT_NO_FATAL_FAILURE(InitState());
1612 ASSERT_NO_FATAL_FAILURE(InitViewport());
1613
1614 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
1615 vertShaderText, &vs));
1616
1617 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
1618 fragShaderText, &ps));
1619
1620 ASSERT_NO_FATAL_FAILURE(CreateDefaultPipeline(&pipeline, vs, ps));
1621
1622 /*
1623 * Shaders are now part of the pipeline, don't need these anymore
1624 */
1625 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
1626 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
1627
1628 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1629
1630
1631 // Let's populate our buffer with the following:
1632 // vec4 red;
1633 // vec4 green;
1634 // vec4 blue;
1635 // vec4 white;
1636 const int valCount = 4 * 4;
1637 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
1638 0.0, 1.0, 0.0, 1.0,
1639 0.0, 0.0, 1.0, 1.0,
1640 1.0, 1.0, 1.0, 1.0 };
1641
1642 InitConstantBuffer(valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
1643
1644 // Create descriptor set for a uniform resource
1645 const int slotCount = 1;
1646 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
1647 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1648 descriptorInfo.slots = slotCount;
1649
1650 // create a descriptor set with a single slot
1651 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1652 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1653
1654 // bind memory to the descriptor set
1655 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1656
1657 // write the constant buffer view to the descriptor set
1658 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
1659 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
1660 xglEndDescriptorSetUpdate( m_rsrcDescSet );
1661
1662 // Build command buffer
1663 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1664 ASSERT_XGL_SUCCESS(err);
1665
1666 GenerateClearAndPrepareBufferCmds();
1667 GenerateBindRenderTargetCmd();
1668 GenerateBindStateAndPipelineCmds(&pipeline);
1669
1670// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
1671// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
1672
1673 // render the cube
1674 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
1675
1676 // prepare the back buffer for present
1677// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
1678// transitionToPresent.image = m_image;
1679// transitionToPresent.oldState = m_image_state;
1680// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
1681// transitionToPresent.subresourceRange = srRange;
1682// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
1683// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
1684
1685 // finalize recording of the command buffer
1686 err = xglEndCommandBuffer( m_cmdBuffer );
1687 ASSERT_XGL_SUCCESS( err );
1688
1689 // this command buffer only uses the vertex buffer memory
1690 m_numMemRefs = 0;
1691// m_memRefs[0].flags = 0;
1692// m_memRefs[0].mem = m_vtxBufferMemory;
1693
1694 // submit the command buffer to the universal queue
1695 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
1696 ASSERT_XGL_SUCCESS( err );
1697
1698 err = xglQueueWaitIdle( m_device->m_queue );
1699 ASSERT_XGL_SUCCESS( err );
1700
1701 // Wait for work to finish before cleaning up.
1702 xglDeviceWaitIdle(m_device->device());
1703
1704 RecordImage(m_renderTarget);
1705
1706}
1707
Cody Northropec1d3c32014-10-28 15:41:42 -06001708void XglRenderTest::DrawTriangleFSUniformBlockBinding(const char *vertShaderText, const char *fragShaderText)
1709{
1710 // sourced from DrawTriangleFSUniformBlock
1711
1712 XGL_PIPELINE pipeline;
1713 XGL_SHADER vs, ps;
1714 XGL_RESULT err;
1715
1716 ASSERT_NO_FATAL_FAILURE(InitState());
1717 ASSERT_NO_FATAL_FAILURE(InitViewport());
1718
1719 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
1720 vertShaderText, &vs));
1721
1722 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
1723 fragShaderText, &ps));
1724
1725 const int bufferCount = 4;
1726 ASSERT_NO_FATAL_FAILURE(CreatePipelineFSUniformBlockBinding(&pipeline, vs, ps, bufferCount));
1727
1728 /*
1729 * Shaders are now part of the pipeline, don't need these anymore
1730 */
1731 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
1732 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
1733
1734 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1735
1736
1737 // We're going to create a number of uniform buffers, and then allow
1738 // the shader to select which it wants to read from with a binding
1739
1740 // Let's populate the buffers with a single color each:
1741 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
1742 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
1743 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
1744 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
1745
1746 assert(4 == bufferCount); // update the following code if you want more than 4
1747
1748 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
1749 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
1750 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
1751 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
1752
1753 const int redCount = sizeof(redVals) / sizeof(float);
1754 const int greenCount = sizeof(greenVals) / sizeof(float);
1755 const int blueCount = sizeof(blueVals) / sizeof(float);
1756 const int whiteCount = sizeof(whiteVals) / sizeof(float);
1757
1758 int index = 0;
1759 InitUniformBuffer(redCount, sizeof(redVals[0]), index++, (const void *) redVals);
1760 InitUniformBuffer(greenCount, sizeof(greenVals[0]), index++, (const void *) greenVals);
1761 InitUniformBuffer(blueCount, sizeof(blueVals[0]), index++, (const void *) blueVals);
1762 InitUniformBuffer(whiteCount, sizeof(whiteVals[0]), index++, (const void *) whiteVals);
1763
1764
1765 // Create descriptor set for a uniform resource
1766 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
1767 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1768 descriptorInfo.slots = bufferCount;
1769
1770 // create a descriptor set with a single slot
1771 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1772 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1773
1774 // bind memory to the descriptor set
1775 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1776
1777 // write the constant buffer view to the descriptor set
1778 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
1779
1780 for(int i = 0; i < bufferCount; ++i)
1781 xglAttachMemoryViewDescriptors( m_rsrcDescSet, i, 1, &m_uniformBufferView[i] );
1782
1783 xglEndDescriptorSetUpdate( m_rsrcDescSet );
1784
1785 // Build command buffer
1786 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1787 ASSERT_XGL_SUCCESS(err);
1788
1789 GenerateClearAndPrepareBufferCmds();
1790 GenerateBindRenderTargetCmd();
1791 GenerateBindStateAndPipelineCmds(&pipeline);
1792
1793// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
1794// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
1795
1796 // render the cube
1797 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
1798
1799 // prepare the back buffer for present
1800// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
1801// transitionToPresent.image = m_image;
1802// transitionToPresent.oldState = m_image_state;
1803// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
1804// transitionToPresent.subresourceRange = srRange;
1805// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
1806// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
1807
1808 // finalize recording of the command buffer
1809 err = xglEndCommandBuffer( m_cmdBuffer );
1810 ASSERT_XGL_SUCCESS( err );
1811
1812 // this command buffer only uses the vertex buffer memory
1813 m_numMemRefs = 0;
1814// m_memRefs[0].flags = 0;
1815// m_memRefs[0].mem = m_vtxBufferMemory;
1816
1817 // submit the command buffer to the universal queue
1818 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
1819 ASSERT_XGL_SUCCESS( err );
1820
1821 err = xglQueueWaitIdle( m_device->m_queue );
1822 ASSERT_XGL_SUCCESS( err );
1823
1824 // Wait for work to finish before cleaning up.
1825 xglDeviceWaitIdle(m_device->device());
1826
1827 RecordImage(m_renderTarget);
1828
1829}
1830
Cody Northrop0fdf64e2014-10-20 11:51:06 -06001831void XglRenderTest::DrawTriangleVSFSUniformBlock(const char *vertShaderText, const char *fragShaderText)
1832{
1833 // this is sourced from DrawTriangleFSUniformBlock
1834
1835 XGL_PIPELINE pipeline;
1836 XGL_SHADER vs, ps;
1837 XGL_RESULT err;
1838
1839 ASSERT_NO_FATAL_FAILURE(InitState());
1840 ASSERT_NO_FATAL_FAILURE(InitViewport());
1841
1842 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
1843 vertShaderText, &vs));
1844
1845 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
1846 fragShaderText, &ps));
1847
1848 ASSERT_NO_FATAL_FAILURE(CreatePipelineVSFSUniformBlock(&pipeline, vs, ps));
1849
1850 /*
1851 * Shaders are now part of the pipeline, don't need these anymore
1852 */
1853 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
1854 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
1855
1856 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1857
1858
1859 // Let's populate our buffer with the following:
1860 // vec4 red;
1861 // vec4 green;
1862 // vec4 blue;
1863 // vec4 white;
1864 const int valCount = 4 * 4;
1865 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
1866 0.0, 1.0, 0.0, 1.0,
1867 0.0, 0.0, 1.0, 1.0,
1868 1.0, 1.0, 1.0, 1.0 };
1869
1870 InitConstantBuffer(valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
1871
1872 // Create descriptor set for a uniform resource
1873 const int slotCount = 1;
1874 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
1875 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1876 descriptorInfo.slots = slotCount;
1877
1878 // create a descriptor set with a single slot
1879 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1880 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1881
1882 // bind memory to the descriptor set
1883 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1884
1885 // write the constant buffer view to the descriptor set
1886 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
1887 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
1888 xglEndDescriptorSetUpdate( m_rsrcDescSet );
1889
1890 // Build command buffer
1891 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1892 ASSERT_XGL_SUCCESS(err);
1893
1894 GenerateClearAndPrepareBufferCmds();
1895 GenerateBindRenderTargetCmd();
1896 GenerateBindStateAndPipelineCmds(&pipeline);
1897
1898// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
1899// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
1900
1901 // render the cube
1902 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
1903
1904 // prepare the back buffer for present
1905// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
1906// transitionToPresent.image = m_image;
1907// transitionToPresent.oldState = m_image_state;
1908// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
1909// transitionToPresent.subresourceRange = srRange;
1910// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
1911// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
1912
1913 // finalize recording of the command buffer
1914 err = xglEndCommandBuffer( m_cmdBuffer );
1915 ASSERT_XGL_SUCCESS( err );
1916
1917 // this command buffer only uses the vertex buffer memory
1918 m_numMemRefs = 0;
1919// m_memRefs[0].flags = 0;
1920// m_memRefs[0].mem = m_vtxBufferMemory;
1921
1922 // submit the command buffer to the universal queue
1923 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
1924 ASSERT_XGL_SUCCESS( err );
1925
1926 err = xglQueueWaitIdle( m_device->m_queue );
1927 ASSERT_XGL_SUCCESS( err );
1928
1929 // Wait for work to finish before cleaning up.
1930 xglDeviceWaitIdle(m_device->device());
1931
1932 RecordImage(m_renderTarget);
1933
1934}
1935
Cody Northrop722ff402014-10-20 09:22:42 -06001936TEST_F(XglRenderTest, GreenTriangle)
1937{
1938 static const char *vertShaderText =
1939 "#version 130\n"
1940 "vec2 vertices[3];\n"
1941 "void main() {\n"
1942 " vertices[0] = vec2(-1.0, -1.0);\n"
1943 " vertices[1] = vec2( 1.0, -1.0);\n"
1944 " vertices[2] = vec2( 0.0, 1.0);\n"
1945 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1946 "}\n";
1947
1948 static const char *fragShaderText =
1949 "#version 130\n"
1950 "void main() {\n"
1951 " gl_FragColor = vec4(0,1,0,1);\n"
1952 "}\n";
1953 DrawTriangleTest(vertShaderText, fragShaderText);
1954}
1955
1956TEST_F(XglRenderTest, BIL_GreenTriangle)
1957{
1958 bool saved_use_bil = XglTestFramework::m_use_bil;
1959
1960 static const char *vertShaderText =
1961 "#version 130\n"
1962 "vec2 vertices[3];\n"
1963 "void main() {\n"
1964 " vertices[0] = vec2(-1.0, -1.0);\n"
1965 " vertices[1] = vec2( 1.0, -1.0);\n"
1966 " vertices[2] = vec2( 0.0, 1.0);\n"
1967 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1968 "}\n";
1969
1970 static const char *fragShaderText =
1971 "#version 130\n"
1972 "void main() {\n"
1973 " gl_FragColor = vec4(0,1,0,1);\n"
1974 "}\n";
1975 XglTestFramework::m_use_bil = true;
1976 DrawTriangleTest(vertShaderText, fragShaderText);
1977 XglTestFramework::m_use_bil = saved_use_bil;
1978}
1979
GregFef5d9cf2014-10-22 14:40:26 -06001980TEST_F(XglRenderTest, MixTriangle)
1981{
1982 // This tests location applied to varyings. Notice that we have switched foo
1983 // and bar in the FS. The triangle should be blended with red, green and blue
1984 // corners.
1985 static const char *vertShaderText =
1986 "#version 140\n"
1987 "#extension GL_ARB_separate_shader_objects : enable\n"
1988 "#extension GL_ARB_shading_language_420pack : enable\n"
1989 "layout (location=0) out vec4 bar;\n"
1990 "layout (location=1) out vec4 foo;\n"
1991 "layout (location=2) out vec4 scale;\n"
1992 "vec2 vertices[3];\n"
1993 "void main() {\n"
1994 " vertices[0] = vec2(-1.0, -1.0);\n"
1995 " vertices[1] = vec2( 1.0, -1.0);\n"
1996 " vertices[2] = vec2( 0.0, 1.0);\n"
1997 "vec4 colors[3];\n"
1998 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1999 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
2000 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
2001 " foo = colors[gl_VertexID % 3];\n"
2002 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
2003 " scale.x = 0.0;\n"
2004 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2005 "}\n";
2006
2007 static const char *fragShaderText =
2008 "#version 140\n"
2009 "#extension GL_ARB_separate_shader_objects : enable\n"
2010 "#extension GL_ARB_shading_language_420pack : enable\n"
2011 "layout (location=1) in vec4 bar;\n"
2012 "layout (location=0) in vec4 foo;\n"
2013 "layout (location=2) in vec4 scale;\n"
2014 "void main() {\n"
2015 " gl_FragColor = bar + foo * scale.x;\n"
2016 "}\n";
2017 DrawTriangleTest(vertShaderText, fragShaderText);
2018}
2019
GregF91b2d302014-10-23 10:44:44 -06002020TEST_F(XglRenderTest, BIL_MixTriangle)
2021{
2022 bool saved_use_bil = XglTestFramework::m_use_bil;
2023
2024 // This tests location applied to varyings. Notice that we have switched foo
2025 // and bar in the FS. The triangle should be blended with red, green and blue
2026 // corners.
2027 static const char *vertShaderText =
2028 "#version 140\n"
2029 "#extension GL_ARB_separate_shader_objects : enable\n"
2030 "#extension GL_ARB_shading_language_420pack : enable\n"
2031 "layout (location=0) out vec4 bar;\n"
2032 "layout (location=1) out vec4 foo;\n"
2033 "layout (location=2) out vec4 scale;\n"
2034 "vec2 vertices[3];\n"
2035 "void main() {\n"
2036 " vertices[0] = vec2(-1.0, -1.0);\n"
2037 " vertices[1] = vec2( 1.0, -1.0);\n"
2038 " vertices[2] = vec2( 0.0, 1.0);\n"
2039 "vec4 colors[3];\n"
2040 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
2041 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
2042 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
2043 " foo = colors[gl_VertexID % 3];\n"
2044 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
2045 " scale.x = 0.0;\n"
2046 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2047 "}\n";
2048
2049 static const char *fragShaderText =
2050 "#version 140\n"
2051 "#extension GL_ARB_separate_shader_objects : enable\n"
2052 "#extension GL_ARB_shading_language_420pack : enable\n"
2053 "layout (location=1) in vec4 bar;\n"
2054 "layout (location=0) in vec4 foo;\n"
2055 "layout (location=2) in vec4 scale;\n"
2056 "void main() {\n"
2057 " gl_FragColor = bar + foo * scale.x;\n"
2058 "}\n";
2059 XglTestFramework::m_use_bil = true;
2060 DrawTriangleTest(vertShaderText, fragShaderText);
2061 XglTestFramework::m_use_bil = saved_use_bil;
2062}
2063
Cody Northrop722ff402014-10-20 09:22:42 -06002064TEST_F(XglRenderTest, TriangleFragUniform)
2065{
2066
2067 static const char *vertShaderText =
2068 "#version 130\n"
2069 "out vec4 color;\n"
2070 "out vec4 scale;\n"
2071 "vec2 vertices[3];\n"
2072 "void main() {\n"
2073 "vec2 vertices[3];\n"
2074 " vertices[0] = vec2(-0.5, -0.5);\n"
2075 " vertices[1] = vec2( 0.5, -0.5);\n"
2076 " vertices[2] = vec2( 0.5, 0.5);\n"
2077 "vec4 colors[3];\n"
2078 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
2079 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
2080 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
2081 " color = colors[gl_VertexID % 3];\n"
2082 " scale = vec4(1.0, 1.0, 1.0, 1.0);\n"
2083 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2084 "}\n";
2085
2086 static const char *fragShaderText =
2087 "#version 130\n"
2088 "in vec4 color;\n"
2089 "in vec4 scale;\n"
2090 "uniform vec4 foo;\n"
2091 "void main() {\n"
2092 " gl_FragColor = color * scale + foo;\n"
2093 "}\n";
2094
2095 DrawTriangleTest(vertShaderText, fragShaderText);
2096}
2097
2098TEST_F(XglRenderTest, YellowTriangle)
2099{
2100 static const char *vertShaderText =
2101 "#version 130\n"
2102 "void main() {\n"
2103 " vec2 vertices[3];"
2104 " vertices[0] = vec2(-0.5, -0.5);\n"
2105 " vertices[1] = vec2( 0.5, -0.5);\n"
2106 " vertices[2] = vec2( 0.5, 0.5);\n"
2107 " vec4 colors[3];\n"
2108 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
2109 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
2110 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
2111 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2112 "}\n";
2113
2114 static const char *fragShaderText =
2115 "#version 130\n"
2116 "void main() {\n"
2117 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
2118 "}\n";
2119
2120 DrawTriangleTest(vertShaderText, fragShaderText);
2121}
2122
Cody Northrop722ff402014-10-20 09:22:42 -06002123TEST_F(XglRenderTest, TriangleTwoFSUniforms)
2124{
2125 static const char *vertShaderText =
2126 "#version 130\n"
2127 "out vec4 color;\n"
2128 "out vec4 scale;\n"
2129 "out vec2 samplePos;\n"
2130 "void main() {\n"
2131 " vec2 vertices[3];"
2132 " vertices[0] = vec2(-0.5, -0.5);\n"
2133 " vertices[1] = vec2( 0.5, -0.5);\n"
2134 " vertices[2] = vec2( 0.5, 0.5);\n"
2135 " vec4 colors[3];\n"
2136 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
2137 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
2138 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
2139 " color = colors[gl_VertexID % 3];\n"
2140 " vec2 positions[3];"
2141 " positions[0] = vec2( 0.0, 0.0);\n"
2142 " positions[1] = vec2( 1.0, 0.0);\n"
2143 " positions[2] = vec2( 1.0, 1.0);\n"
2144 " scale = vec4(0.0, 0.0, 0.0, 0.0);\n"
2145 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2146 "}\n";
2147
2148
2149 static const char *fragShaderText =
2150 "#version 430\n"
2151 "in vec4 color;\n"
2152 "in vec4 scale;\n"
2153 "uniform vec4 foo;\n"
2154 "uniform vec4 bar;\n"
2155 "void main() {\n"
2156 // by default, with no location or blocks
2157 // the compiler will read them from buffer
2158 // in reverse order of first use in shader
2159 // The buffer contains red, followed by blue,
2160 // so foo should be blue, bar should be red
2161 " gl_FragColor = color * scale * foo * bar + foo;\n"
2162 "}\n";
2163
2164 DrawTriangleTwoUniformsFS(vertShaderText, fragShaderText);
2165}
2166
2167TEST_F(XglRenderTest, TriangleWithVertexFetch)
2168{
2169 static const char *vertShaderText =
2170 "#version 130\n"
2171 //XYZ1( -1, -1, -1 )
2172 "in vec4 pos;\n"
2173 //XYZ1( 0.f, 0.f, 0.f )
2174 "in vec4 inColor;\n"
2175 "out vec4 outColor;\n"
2176 "void main() {\n"
2177 " outColor = inColor;\n"
2178 " gl_Position = pos;\n"
2179 "}\n";
2180
2181
2182 static const char *fragShaderText =
2183 "#version 430\n"
2184 "in vec4 color;\n"
2185 "void main() {\n"
2186 " gl_FragColor = color;\n"
2187 "}\n";
2188
2189 DrawTriangleWithVertexFetch(vertShaderText, fragShaderText);
2190}
2191
2192TEST_F(XglRenderTest, TriangleVSUniform)
2193{
2194 static const char *vertShaderText =
2195 "#version 130\n"
2196 "uniform mat4 mvp;\n"
2197 "void main() {\n"
2198 " vec2 vertices[3];"
2199 " vertices[0] = vec2(-0.5, -0.5);\n"
2200 " vertices[1] = vec2( 0.5, -0.5);\n"
2201 " vertices[2] = vec2( 0.5, 0.5);\n"
2202 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0) * mvp;\n"
2203 "}\n";
2204
2205 static const char *fragShaderText =
2206 "#version 430\n"
2207 "void main() {\n"
2208 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
2209 "}\n";
2210
2211 // Create identity matrix
2212 glm::mat4 Model = glm::mat4(1.0f);
2213 DrawTriangleVSUniform(vertShaderText, fragShaderText);
2214
2215// Model = glm::rotate(Model, glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f));
2216// DrawTriangleVSUniform(vertShaderText, fragShaderText, Model);
2217}
2218
Cody Northrop0fdf64e2014-10-20 11:51:06 -06002219TEST_F(XglRenderTest, TriangleFSUniformBlock)
2220{
2221 // The expected result from this test is a blue triangle
2222
2223 static const char *vertShaderText =
2224 "#version 130\n"
2225 "void main() {\n"
2226 " vec2 vertices[3];"
2227 " vertices[0] = vec2(-0.5, -0.5);\n"
2228 " vertices[1] = vec2( 0.5, -0.5);\n"
2229 " vertices[2] = vec2( 0.5, 0.5);\n"
2230 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2231 "}\n";
2232
2233 static const char *fragShaderText =
2234 "#version 430\n"
2235 "layout (std140) uniform bufferVals {\n"
2236 " vec4 red;\n"
2237 " vec4 green;\n"
2238 " vec4 blue;\n"
2239 " vec4 white;\n"
2240 "} myBufferVals;\n"
2241 "void main() {\n"
2242 " gl_FragColor = myBufferVals.blue;\n"
2243 "}\n";
2244
2245 DrawTriangleFSUniformBlock(vertShaderText, fragShaderText);
2246}
2247
2248TEST_F(XglRenderTest, TriangleVSUniformBlock)
2249{
2250 // The expected result from this test is a blue triangle
2251
2252 static const char *vertShaderText =
2253 "#version 140\n"
2254 "out vec4 outColor;\n"
2255 "layout (std140) uniform bufferVals {\n"
2256 " vec4 red;\n"
2257 " vec4 green;\n"
2258 " vec4 blue;\n"
2259 " vec4 white;\n"
2260 "} myBufferVals;\n"
2261 "void main() {\n"
2262 " vec2 vertices[3];"
2263 " vertices[0] = vec2(-0.5, -0.5);\n"
2264 " vertices[1] = vec2( 0.5, -0.5);\n"
2265 " vertices[2] = vec2( 0.5, 0.5);\n"
2266 " outColor = myBufferVals.blue;\n"
2267 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2268 "}\n";
2269
2270 static const char *fragShaderText =
2271 "#version 430\n"
2272 "in vec4 inColor;\n"
2273 "void main() {\n"
2274 " gl_FragColor = inColor;\n"
2275 "}\n";
2276
2277 DrawTriangleVSUniformBlock(vertShaderText, fragShaderText);
2278}
2279
2280TEST_F(XglRenderTest, TriangleVSFSUniformBlock)
2281{
2282 // The expected result from this test is a green triangle
2283 // Note the buffer is shared between stages, idenitical layout
2284
2285 static const char *vertShaderText =
2286 "#version 140\n"
2287 "out vec4 outRed;\n"
2288 "out vec4 outGreen;\n"
2289 "out vec4 outBlue;\n"
2290 "out vec4 outWhite;\n"
2291 "layout (std140) uniform bufferVals {\n"
2292 " vec4 red;\n"
2293 " vec4 green;\n"
2294 " vec4 blue;\n"
2295 " vec4 white;\n"
2296 "} myBufferVals;\n"
2297 "void main() {\n"
2298 " vec2 vertices[3];"
2299 " vertices[0] = vec2(-0.5, -0.5);\n"
2300 " vertices[1] = vec2( 0.5, -0.5);\n"
2301 " vertices[2] = vec2( 0.5, 0.5);\n"
2302 " outRed = myBufferVals.red;\n"
2303 " outGreen = myBufferVals.green;\n"
2304 " outBlue = myBufferVals.blue;\n"
2305 " outWhite = myBufferVals.white;\n"
2306 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2307 "}\n";
2308
2309 static const char *fragShaderText =
2310 "#version 430\n"
2311 "in vec4 inRed;\n"
2312 "in vec4 inGreen;\n"
2313 "in vec4 inBlue;\n"
2314 "in vec4 inWhite;\n"
2315 "layout (std140) uniform bufferVals {\n"
2316 " vec4 red;\n"
2317 " vec4 green;\n"
2318 " vec4 blue;\n"
2319 " vec4 white;\n"
2320 "} myBufferVals;\n"
2321 "void main() {\n"
2322 " if (inRed == myBufferVals.red && \n"
2323 " inGreen == myBufferVals.green && \n"
2324 " inBlue == myBufferVals.blue && \n"
2325 " inWhite == myBufferVals.white) \n"
2326 " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
2327 " else\n"
2328 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
2329 "}\n";
2330
2331 DrawTriangleVSFSUniformBlock(vertShaderText, fragShaderText);
2332 }
2333
2334
Cody Northropc6953d02014-10-20 11:23:32 -06002335TEST_F(XglRenderTest, TexturedTriangle)
2336{
Cody Northrop5fa1d332014-10-20 11:51:32 -06002337 // The expected result from this test is a red and green checkered triangle
Cody Northropc6953d02014-10-20 11:23:32 -06002338 static const char *vertShaderText =
2339 "#version 130\n"
2340 "out vec2 samplePos;\n"
2341 "void main() {\n"
2342 " vec2 vertices[3];"
2343 " vertices[0] = vec2(-0.5, -0.5);\n"
2344 " vertices[1] = vec2( 0.5, -0.5);\n"
2345 " vertices[2] = vec2( 0.5, 0.5);\n"
2346 " vec2 positions[3];"
2347 " positions[0] = vec2( 0.0, 0.0);\n"
2348 " positions[1] = vec2( 1.0, 0.0);\n"
2349 " positions[2] = vec2( 1.0, 1.0);\n"
2350 " samplePos = positions[gl_VertexID % 3];\n"
2351 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2352 "}\n";
2353
2354 static const char *fragShaderText =
2355 "#version 130\n"
2356 "in vec2 samplePos;\n"
2357 "uniform sampler2D surface;\n"
2358 "void main() {\n"
2359 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
2360 " gl_FragColor = texColor;\n"
2361 "}\n";
2362 DrawTexturedTriangle(vertShaderText, fragShaderText);
2363}
2364
Cody Northrop9394e0c2014-10-23 10:21:47 -06002365TEST_F(XglRenderTest, VSTexture)
2366{
2367 // The expected result from this test is a green and red triangle;
2368 // one red vertex on the left, two green vertices on the right.
2369 static const char *vertShaderText =
2370 "#version 130\n"
2371 "out vec4 texColor;\n"
2372 "uniform sampler2D surface;\n"
2373 "void main() {\n"
2374 " vec2 vertices[3];"
2375 " vertices[0] = vec2(-0.5, -0.5);\n"
2376 " vertices[1] = vec2( 0.5, -0.5);\n"
2377 " vertices[2] = vec2( 0.5, 0.5);\n"
2378 " vec2 positions[3];"
2379 " positions[0] = vec2( 0.0, 0.0);\n"
2380 " positions[1] = vec2( 0.25, 0.1);\n"
2381 " positions[2] = vec2( 0.1, 0.25);\n"
2382 " vec2 samplePos = positions[gl_VertexID % 3];\n"
2383 " texColor = textureLod(surface, samplePos, 0.0);\n"
2384 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2385 "}\n";
2386
2387 static const char *fragShaderText =
2388 "#version 130\n"
2389 "in vec4 texColor;\n"
2390 "void main() {\n"
2391 " gl_FragColor = texColor;\n"
2392 "}\n";
2393
2394 DrawTexturedTriangle(vertShaderText, fragShaderText);
2395}
2396
Cody Northropec1d3c32014-10-28 15:41:42 -06002397TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2398{
2399 // This test allows the shader to select which buffer it is
2400 // pulling from using layout binding qualifier.
2401 // There are corresponding changes in the compiler stack that
2402 // will select the buffer using binding directly.
2403 // The expected result from this test is a purple triangle
2404
2405 static const char *vertShaderText =
2406 "#version 130\n"
2407 "void main() {\n"
2408 " vec2 vertices[3];"
2409 " vertices[0] = vec2(-0.5, -0.5);\n"
2410 " vertices[1] = vec2( 0.5, -0.5);\n"
2411 " vertices[2] = vec2( 0.5, 0.5);\n"
2412 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2413 "}\n";
2414
2415 static const char *fragShaderText =
2416 "#version 140\n"
2417 "#extension GL_ARB_separate_shader_objects : enable\n"
2418 "#extension GL_ARB_shading_language_420pack : enable\n"
2419 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2420 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2421 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2422 "layout (std140, binding = 3) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2423 "void main() {\n"
2424 " gl_FragColor = myBlueVal.color;\n"
2425 " gl_FragColor += myRedVal.color;\n"
2426 "}\n";
2427
2428 DrawTriangleFSUniformBlockBinding(vertShaderText, fragShaderText);
2429}
2430
Cody Northrop722ff402014-10-20 09:22:42 -06002431int main(int argc, char **argv) {
2432 int result;
2433
2434 ::testing::InitGoogleTest(&argc, argv);
2435 XglTestFramework::InitArgs(&argc, argv);
2436
2437 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
2438
2439 result = RUN_ALL_TESTS();
2440
2441 XglTestFramework::Finish();
2442 return result;
2443}