blob: cb83653ebf7f0535cfbbfd7eefc796f11ee8716c [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 Northropac6179b2014-10-29 12:09:45 -0600135// These values are used in a few places
136static const int g_UniformBufferCount = 16;
137static const int g_SamplerCount = 3;
138static const int g_TextureCount = 3;
Cody Northropec1d3c32014-10-28 15:41:42 -0600139
Cody Northrop722ff402014-10-20 09:22:42 -0600140class XglRenderTest : public XglRenderFramework
141{
142public:
143 void InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride, const void* vertices );
Cody Northropac6179b2014-10-29 12:09:45 -0600144 void InitTexture(int textureSlot = 0, int* color = 0);
145 void InitMultipleTextures(int textureCount, int* colors);
146 void InitSampler(int samplerSlot = 0);
147 void InitMultipleSamplers(int samplerCount);
Cody Northropec1d3c32014-10-28 15:41:42 -0600148 void InitUniformBuffer(int constantCount, int constantSize, int constantIndex, const void* data);
Cody Northrop722ff402014-10-20 09:22:42 -0600149 void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText);
Cody Northrop722ff402014-10-20 09:22:42 -0600150 void DrawTriangleWithVertexFetch(const char *vertShaderText, const char *fragShaderText);
Cody Northropec1d3c32014-10-28 15:41:42 -0600151 void DrawTriangleFSUniformBlockBinding(const char *vertShaderText, const char *fragShaderText);
Cody Northrop0fdf64e2014-10-20 11:51:06 -0600152 void DrawTriangleVSUniformBlock(const char *vertShaderText, const char *fragShaderText);
Cody Northropc6953d02014-10-20 11:23:32 -0600153 void DrawTexturedTriangle(const char *vertShaderText, const char *fragShaderText);
Cody Northrop9394e0c2014-10-23 10:21:47 -0600154 void DrawVSTexture(const char *vertShaderText, const char *fragShaderText);
Cody Northropac6179b2014-10-29 12:09:45 -0600155 void DrawSamplerBindingsTriangle(const char *vertShaderText, const char *fragShaderText, int textureCount, int samplerCount);
Cody Northrop722ff402014-10-20 09:22:42 -0600156
Cody Northrop0fdf64e2014-10-20 11:51:06 -0600157
Cody Northrop722ff402014-10-20 09:22:42 -0600158 void CreatePipelineWithVertexFetch(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps);
Cody Northropec1d3c32014-10-28 15:41:42 -0600159 void CreatePipelineFSUniformBlockBinding(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps, int bufferCount);
Cody Northrop722ff402014-10-20 09:22:42 -0600160 void CreatePipelineVSUniform(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps);
Cody Northropc6953d02014-10-20 11:23:32 -0600161 void CreatePipelineSingleTextureAndSampler(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps);
Cody Northropac6179b2014-10-29 12:09:45 -0600162 void CreatePipelineMultipleTexturesAndSamplers(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps, int textureCount, int samplerCount);
Cody Northrop722ff402014-10-20 09:22:42 -0600163
164protected:
Cody Northropac6179b2014-10-29 12:09:45 -0600165 XGL_IMAGE m_texture[g_TextureCount];
166 XGL_IMAGE_VIEW m_textureView[g_TextureCount];
167 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo[g_TextureCount];
168 XGL_GPU_MEMORY m_textureMem[g_TextureCount];
Cody Northrop722ff402014-10-20 09:22:42 -0600169
Cody Northrop722ff402014-10-20 09:22:42 -0600170
Cody Northropac6179b2014-10-29 12:09:45 -0600171 XGL_SAMPLER m_sampler[g_SamplerCount];
172
173 XGL_GPU_MEMORY m_uniformBufferMem[g_UniformBufferCount];
174 XGL_MEMORY_VIEW_ATTACH_INFO m_uniformBufferView[g_UniformBufferCount];
Cody Northropec1d3c32014-10-28 15:41:42 -0600175
176
177
Cody Northrop722ff402014-10-20 09:22:42 -0600178// XGL_APPLICATION_INFO app_info;
179// XGL_PHYSICAL_GPU objs[MAX_GPUS];
180// XGL_UINT gpu_count;
181// XGL_GPU_MEMORY m_descriptor_set_mem;
182// XGL_GPU_MEMORY m_pipe_mem;
183// XglDevice *m_device;
184// XGL_CMD_BUFFER m_cmdBuffer;
185// XGL_UINT32 m_numVertices;
186// XGL_MEMORY_VIEW_ATTACH_INFO m_vtxBufferView;
187// XGL_MEMORY_VIEW_ATTACH_INFO m_constantBufferView;
188// XGL_GPU_MEMORY m_vtxBufferMem;
189// XGL_GPU_MEMORY m_constantBufferMem;
190// XGL_UINT32 m_numMemRefs;
191// XGL_MEMORY_REF m_memRefs[5];
192// XGL_RASTER_STATE_OBJECT m_stateRaster;
193// XGL_COLOR_BLEND_STATE_OBJECT m_colorBlend;
194// XGL_VIEWPORT_STATE_OBJECT m_stateViewport;
195// XGL_DEPTH_STENCIL_STATE_OBJECT m_stateDepthStencil;
196// XGL_MSAA_STATE_OBJECT m_stateMsaa;
197// XGL_DESCRIPTOR_SET m_rsrcDescSet;
198
199 virtual void SetUp() {
200
201 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
202 this->app_info.pNext = NULL;
Cody Northropaa853252014-10-20 09:25:17 -0600203 this->app_info.pAppName = (const XGL_CHAR *) "compiler render_tests";
Cody Northrop722ff402014-10-20 09:22:42 -0600204 this->app_info.appVersion = 1;
205 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
206 this->app_info.engineVersion = 1;
207 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
208
Cody Northropac6179b2014-10-29 12:09:45 -0600209 for (int i = 0; i < g_TextureCount; ++i) {
210 m_textureViewInfo[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
211 }
Cody Northrop722ff402014-10-20 09:22:42 -0600212
213 InitFramework();
214 }
215
216 virtual void TearDown() {
217 // Clean up resources before we reset
218 ShutdownFramework();
219 }
220};
221
222// this function will create the vertex buffer and fill it with the mesh data
223void XglRenderTest::InitMesh( XGL_UINT32 numVertices, XGL_GPU_SIZE vbStride,
224 const void* vertices )
225{
226 XGL_RESULT err = XGL_SUCCESS;
227
228 assert( numVertices * vbStride > 0 );
229 m_numVertices = numVertices;
230
231 XGL_MEMORY_ALLOC_INFO alloc_info = {};
232 XGL_UINT8 *pData;
233
234 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
235 alloc_info.allocationSize = numVertices * vbStride;
236 alloc_info.alignment = 0;
237 alloc_info.heapCount = 1;
238 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
239
240 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
241 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
242
243 err = xglAllocMemory(device(), &alloc_info, &m_vtxBufferMem);
244 ASSERT_XGL_SUCCESS(err);
245
246 err = xglMapMemory(m_vtxBufferMem, 0, (XGL_VOID **) &pData);
247 ASSERT_XGL_SUCCESS(err);
248
249 memcpy(pData, vertices, alloc_info.allocationSize);
250
251 err = xglUnmapMemory(m_vtxBufferMem);
252 ASSERT_XGL_SUCCESS(err);
253
254 // set up the memory view for the vertex buffer
255 this->m_vtxBufferView.stride = vbStride;
256 this->m_vtxBufferView.range = numVertices * vbStride;
257 this->m_vtxBufferView.offset = 0;
258 this->m_vtxBufferView.mem = m_vtxBufferMem;
259 this->m_vtxBufferView.format.channelFormat = XGL_CH_FMT_UNDEFINED;
260 this->m_vtxBufferView.format.numericFormat = XGL_NUM_FMT_UNDEFINED;
Cody Northrop722ff402014-10-20 09:22:42 -0600261}
262
Cody Northropac6179b2014-10-29 12:09:45 -0600263void XglRenderTest::InitTexture(int textureSlot, int* color)
Cody Northrop722ff402014-10-20 09:22:42 -0600264{
265#define DEMO_TEXTURE_COUNT 1
266
Cody Northropac6179b2014-10-29 12:09:45 -0600267 const XGL_FORMAT tex_format = { XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM };
Cody Northrop722ff402014-10-20 09:22:42 -0600268 const XGL_INT tex_width = 16;
269 const XGL_INT tex_height = 16;
Cody Northropac6179b2014-10-29 12:09:45 -0600270 uint32_t tex_colors[DEMO_TEXTURE_COUNT][2];
271
272 // assign the texture color with parameter
273 assert(1 == DEMO_TEXTURE_COUNT);
274 if (color != NULL) {
275 tex_colors[0][0] = *color;
276 tex_colors[0][1] = *color;
277 } else {
Cody Northrop6217b822014-10-30 11:20:22 -0600278 tex_colors[0][0] = 0xff0000ff;
Cody Northropac6179b2014-10-29 12:09:45 -0600279 tex_colors[0][1] = 0xff00ff00;
280 }
281
Cody Northrop722ff402014-10-20 09:22:42 -0600282 XGL_RESULT err;
283 XGL_UINT i;
284
285 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Cody Northrop722ff402014-10-20 09:22:42 -0600286 const XGL_IMAGE_CREATE_INFO image = {
287 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
288 .pNext = NULL,
289 .imageType = XGL_IMAGE_2D,
290 .format = tex_format,
291 .extent = { tex_width, tex_height, 1 },
292 .mipLevels = 1,
293 .arraySize = 1,
294 .samples = 1,
295 .tiling = XGL_LINEAR_TILING,
296 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
297 .flags = 0,
298 };
299 XGL_MEMORY_ALLOC_INFO mem_alloc;
300 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
301 mem_alloc.pNext = NULL;
302 mem_alloc.allocationSize = 0;
303 mem_alloc.alignment = 0;
304 mem_alloc.flags = 0;
305 mem_alloc.heapCount = 0;
306 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
307 XGL_IMAGE_VIEW_CREATE_INFO view;
308 view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
309 view.pNext = NULL;
310 view.image = XGL_NULL_HANDLE;
311 view.viewType = XGL_IMAGE_VIEW_2D;
312 view.format = image.format;
313 view.channels.r = XGL_CHANNEL_SWIZZLE_R;
314 view.channels.g = XGL_CHANNEL_SWIZZLE_G;
315 view.channels.b = XGL_CHANNEL_SWIZZLE_B;
316 view.channels.a = XGL_CHANNEL_SWIZZLE_A;
317 view.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR;
318 view.subresourceRange.baseMipLevel = 0;
319 view.subresourceRange.mipLevels = 1;
320 view.subresourceRange.baseArraySlice = 0;
321 view.subresourceRange.arraySize = 1;
322 view.minLod = 0.0f;
323
324 XGL_MEMORY_REQUIREMENTS mem_reqs;
325 XGL_SIZE mem_reqs_size;
326
Cody Northrop722ff402014-10-20 09:22:42 -0600327 /* create image */
Cody Northropac6179b2014-10-29 12:09:45 -0600328 err = xglCreateImage(device(), &image, &m_texture[textureSlot]);
Cody Northrop722ff402014-10-20 09:22:42 -0600329 assert(!err);
330
Cody Northropac6179b2014-10-29 12:09:45 -0600331 err = xglGetObjectInfo(m_texture[textureSlot],
Cody Northrop722ff402014-10-20 09:22:42 -0600332 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
333 &mem_reqs_size, &mem_reqs);
334 assert(!err && mem_reqs_size == sizeof(mem_reqs));
335
336 mem_alloc.allocationSize = mem_reqs.size;
337 mem_alloc.alignment = mem_reqs.alignment;
338 mem_alloc.heapCount = mem_reqs.heapCount;
339 memcpy(mem_alloc.heaps, mem_reqs.heaps,
340 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
341
342 /* allocate memory */
Cody Northropac6179b2014-10-29 12:09:45 -0600343 err = xglAllocMemory(device(), &mem_alloc, &m_textureMem[textureSlot]);
Cody Northrop722ff402014-10-20 09:22:42 -0600344 assert(!err);
345
346 /* bind memory */
Cody Northropac6179b2014-10-29 12:09:45 -0600347 err = xglBindObjectMemory(m_texture[textureSlot], m_textureMem[textureSlot], 0);
Cody Northrop722ff402014-10-20 09:22:42 -0600348 assert(!err);
349
350 /* create image view */
Cody Northropac6179b2014-10-29 12:09:45 -0600351 view.image = m_texture[textureSlot];
352 err = xglCreateImageView(device(), &view, &m_textureView[textureSlot]);
Cody Northrop722ff402014-10-20 09:22:42 -0600353 assert(!err);
354 }
355
356 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
357 const XGL_IMAGE_SUBRESOURCE subres = {
358 .aspect = XGL_IMAGE_ASPECT_COLOR,
359 .mipLevel = 0,
360 .arraySlice = 0,
361 };
362 XGL_SUBRESOURCE_LAYOUT layout;
363 XGL_SIZE layout_size;
364 XGL_VOID *data;
365 XGL_INT x, y;
366
Cody Northropac6179b2014-10-29 12:09:45 -0600367 err = xglGetImageSubresourceInfo(m_texture[textureSlot], &subres,
Cody Northrop722ff402014-10-20 09:22:42 -0600368 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
369 assert(!err && layout_size == sizeof(layout));
370
Cody Northropac6179b2014-10-29 12:09:45 -0600371 err = xglMapMemory(m_textureMem[textureSlot], 0, &data);
Cody Northrop722ff402014-10-20 09:22:42 -0600372 assert(!err);
373
374 for (y = 0; y < tex_height; y++) {
375 uint32_t *row = (uint32_t *) ((char *) data + layout.rowPitch * y);
376 for (x = 0; x < tex_width; x++)
377 row[x] = tex_colors[i][(x & 1) ^ (y & 1)];
378 }
379
Cody Northropac6179b2014-10-29 12:09:45 -0600380 err = xglUnmapMemory(m_textureMem[textureSlot]);
Cody Northrop722ff402014-10-20 09:22:42 -0600381 assert(!err);
382 }
383
Cody Northropac6179b2014-10-29 12:09:45 -0600384 m_textureViewInfo[textureSlot].view = m_textureView[textureSlot];
Cody Northrop722ff402014-10-20 09:22:42 -0600385}
386
Cody Northropac6179b2014-10-29 12:09:45 -0600387
388void XglRenderTest::InitMultipleTextures(int textureCount, int* colors)
389{
390
391 for (int i = 0; i < textureCount; ++i)
392 InitTexture(i, &colors[i]);
393}
394
395
396void XglRenderTest::InitMultipleSamplers(const int samplerCount)
397{
398 XGL_RESULT err;
399
400 for (int i = 0; i < samplerCount; ++i) {
401
402 XGL_SAMPLER_CREATE_INFO samplerCreateInfo = {};
403 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
404 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
405 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
406 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
407 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
408 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
409 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
410 samplerCreateInfo.mipLodBias = 0.0;
411 samplerCreateInfo.maxAnisotropy = 0.0;
412 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
413 samplerCreateInfo.minLod = 0.0;
414 samplerCreateInfo.maxLod = 0.0;
415 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
416
417 err = xglCreateSampler(device(),&samplerCreateInfo, &m_sampler[i]);
418 ASSERT_XGL_SUCCESS(err);
419 }
420}
421
422void XglRenderTest::InitSampler(int samplerSlot)
Cody Northrop722ff402014-10-20 09:22:42 -0600423{
424 XGL_RESULT err;
425
426 XGL_SAMPLER_CREATE_INFO samplerCreateInfo = {};
427 samplerCreateInfo.sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
428 samplerCreateInfo.magFilter = XGL_TEX_FILTER_NEAREST;
429 samplerCreateInfo.minFilter = XGL_TEX_FILTER_NEAREST;
430 samplerCreateInfo.mipMode = XGL_TEX_MIPMAP_BASE;
431 samplerCreateInfo.addressU = XGL_TEX_ADDRESS_WRAP;
432 samplerCreateInfo.addressV = XGL_TEX_ADDRESS_WRAP;
433 samplerCreateInfo.addressW = XGL_TEX_ADDRESS_WRAP;
434 samplerCreateInfo.mipLodBias = 0.0;
435 samplerCreateInfo.maxAnisotropy = 0.0;
436 samplerCreateInfo.compareFunc = XGL_COMPARE_NEVER;
437 samplerCreateInfo.minLod = 0.0;
438 samplerCreateInfo.maxLod = 0.0;
439 samplerCreateInfo.borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE;
440
Cody Northropac6179b2014-10-29 12:09:45 -0600441 err = xglCreateSampler(device(),&samplerCreateInfo, &m_sampler[samplerSlot]);
Cody Northrop722ff402014-10-20 09:22:42 -0600442 ASSERT_XGL_SUCCESS(err);
443}
444
Cody Northropac6179b2014-10-29 12:09:45 -0600445
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
Cody Northropc6953d02014-10-20 11:23:32 -0600578void XglRenderTest::DrawTexturedTriangle(const char *vertShaderText, const char *fragShaderText)
579{
580
581 // based on DrawTriangleTwoUniformsFS
582
583 XGL_PIPELINE pipeline;
584 XGL_SHADER vs, ps;
585 XGL_RESULT err;
586
587 ASSERT_NO_FATAL_FAILURE(InitState());
588 ASSERT_NO_FATAL_FAILURE(InitViewport());
589
590 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
591 vertShaderText, &vs));
592
593 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
594 fragShaderText, &ps));
595
596 ASSERT_NO_FATAL_FAILURE(CreatePipelineSingleTextureAndSampler(&pipeline, vs, ps));
597
598 /*
599 * Shaders are now part of the pipeline, don't need these anymore
600 */
601 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
602 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
603
604 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
605
606
607 // Enable our single sampler
608 ASSERT_NO_FATAL_FAILURE(InitSampler());
609
610 // Enable our single texture
611 ASSERT_NO_FATAL_FAILURE(InitTexture());
612
613 // Create descriptor set for a texture and sampler resources
Cody Northrop5fa1d332014-10-20 11:51:32 -0600614 const int slotCount = 2;
Cody Northropc6953d02014-10-20 11:23:32 -0600615 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
616 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
617 descriptorInfo.slots = slotCount;
618
619 // create a descriptor set with a single slot
620 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
621 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
622
623 // bind memory to the descriptor set
624 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
625
626 // write the sampler and image views to the descriptor set
627 // ensure this matches order set in CreatePipelineSingleTextureAndSampler
628 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
Cody Northropac6179b2014-10-29 12:09:45 -0600629 xglAttachImageViewDescriptors( m_rsrcDescSet, 0, 1, &m_textureViewInfo[0] );
630 xglAttachSamplerDescriptors(m_rsrcDescSet, 1, 1, &m_sampler[0]);
Cody Northropc6953d02014-10-20 11:23:32 -0600631 xglEndDescriptorSetUpdate( m_rsrcDescSet );
632
633 // Build command buffer
634 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
635 ASSERT_XGL_SUCCESS(err);
636
637 GenerateClearAndPrepareBufferCmds();
638 GenerateBindRenderTargetCmd();
639 GenerateBindStateAndPipelineCmds(&pipeline);
640
641// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
642// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
643
644 // render the cube
645 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
646
647 // prepare the back buffer for present
648// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
649// transitionToPresent.image = m_image;
650// transitionToPresent.oldState = m_image_state;
651// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
652// transitionToPresent.subresourceRange = srRange;
653// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
654// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
655
656 // finalize recording of the command buffer
657 err = xglEndCommandBuffer( m_cmdBuffer );
658 ASSERT_XGL_SUCCESS( err );
659
660 // this command buffer only uses the vertex buffer memory
661 m_numMemRefs = 0;
662// m_memRefs[0].flags = 0;
663// m_memRefs[0].mem = m_vtxBufferMemory;
664
665 // submit the command buffer to the universal queue
666 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
667 ASSERT_XGL_SUCCESS( err );
668
669 err = xglQueueWaitIdle( m_device->m_queue );
670 ASSERT_XGL_SUCCESS( err );
671
672 // Wait for work to finish before cleaning up.
673 xglDeviceWaitIdle(m_device->device());
674
675 RecordImage(m_renderTarget);
676
677}
Cody Northrop722ff402014-10-20 09:22:42 -0600678
Cody Northrop0fdf64e2014-10-20 11:51:06 -0600679void XglRenderTest::DrawTriangleVSUniformBlock(const char *vertShaderText, const char *fragShaderText)
680{
681 // sourced from DrawTriangleVSUniform
682 XGL_PIPELINE pipeline;
683 XGL_SHADER vs, ps;
684 XGL_RESULT err;
685
686 ASSERT_NO_FATAL_FAILURE(InitState());
687 ASSERT_NO_FATAL_FAILURE(InitViewport());
688
689 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
690 vertShaderText, &vs));
691
692 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
693 fragShaderText, &ps));
694
695 ASSERT_NO_FATAL_FAILURE(CreatePipelineVSUniform(&pipeline, vs, ps));
696
697 /*
698 * Shaders are now part of the pipeline, don't need these anymore
699 */
700 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
701 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
702
703 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
704
705 // Let's populate our buffer with the following:
706 // vec4 red;
707 // vec4 green;
708 // vec4 blue;
709 // vec4 white;
710 const int valCount = 4 * 4;
711 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
712 0.0, 1.0, 0.0, 1.0,
713 0.0, 0.0, 1.0, 1.0,
714 1.0, 1.0, 1.0, 1.0 };
715
716 InitConstantBuffer(valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
717
718 // Create descriptor set for a uniform resource
719 const int slotCount = 1;
720 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
721 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
722 descriptorInfo.slots = slotCount;
723
724 // create a descriptor set with a single slot
725 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
726 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
727
728 // bind memory to the descriptor set
729 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
730
731 // write the constant buffer view to the descriptor set
732 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
733 xglAttachMemoryViewDescriptors( m_rsrcDescSet, 0, 1, &m_constantBufferView );
734 xglEndDescriptorSetUpdate( m_rsrcDescSet );
735
736 // Build command buffer
737 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
738 ASSERT_XGL_SUCCESS(err);
739
740 GenerateClearAndPrepareBufferCmds();
741 GenerateBindRenderTargetCmd();
742 GenerateBindStateAndPipelineCmds(&pipeline);
743
744// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
745// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
746
747 // render the cube
748 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
749
750 // prepare the back buffer for present
751// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
752// transitionToPresent.image = m_image;
753// transitionToPresent.oldState = m_image_state;
754// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
755// transitionToPresent.subresourceRange = srRange;
756// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
757// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
758
759 // finalize recording of the command buffer
760 err = xglEndCommandBuffer( m_cmdBuffer );
761 ASSERT_XGL_SUCCESS( err );
762
763 // this command buffer only uses the vertex buffer memory
764 m_numMemRefs = 0;
765// m_memRefs[0].flags = 0;
766// m_memRefs[0].mem = m_vtxBufferMemory;
767
768 // submit the command buffer to the universal queue
769 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
770 ASSERT_XGL_SUCCESS( err );
771
772 err = xglQueueWaitIdle( m_device->m_queue );
773 ASSERT_XGL_SUCCESS( err );
774
775 // Wait for work to finish before cleaning up.
776 xglDeviceWaitIdle(m_device->device());
777
778 RecordImage(m_renderTarget);
779}
780
Cody Northrop722ff402014-10-20 09:22:42 -0600781void XglRenderTest::CreatePipelineWithVertexFetch(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps)
782{
783 XGL_RESULT err;
784 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
785 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
786 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
787
788
789 // Create descriptor set for our one resource
790 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
791 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
792 descriptorInfo.slots = 1; // Vertex buffer only
793
794 // create a descriptor set with a single slot
795 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
796 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
797
798 // bind memory to the descriptor set
799 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
800
Cody Northrop722ff402014-10-20 09:22:42 -0600801 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
802 vs_stage.pNext = XGL_NULL_HANDLE;
803 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
804 vs_stage.shader.shader = vs;
Chia-I Wu3b04af52014-11-08 10:48:20 +0800805 vs_stage.shader.descriptorSetMapping[0].descriptorCount = 0;
Cody Northrop722ff402014-10-20 09:22:42 -0600806 vs_stage.shader.linkConstBufferCount = 0;
807 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
808 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
809 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
810
811 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
812 ps_stage.pNext = &vs_stage;
813 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
814 ps_stage.shader.shader = ps;
815 ps_stage.shader.descriptorSetMapping[0].descriptorCount = 0;
816 ps_stage.shader.linkConstBufferCount = 0;
817 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
818 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
819 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
820
821 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
822 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
823 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
824 };
825
826 // this is the current description of g_vbData
827 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
828 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
829 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
830 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
831 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
832 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
833 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
834 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
835 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
836
837 XGL_PIPELINE_VERTEX_INPUT_CREATE_INFO vi_state = {
838 XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO, // sType;
839 &ps_stage, // pNext;
840 1, // bindingCount
841 &vi_binding, // pVertexBindingDescriptions;
842 2, // attributeCount; // number of attributes
843 vi_attribs // pVertexAttributeDescriptions;
844 };
845
846 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
847 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
848 &vi_state, // pNext
849 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
850 XGL_FALSE, // disableVertexReuse
851 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
852 XGL_FALSE, // primitiveRestartEnable
853 0 // primitiveRestartIndex
854 };
855
856 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
857 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
858 &ia_state,
859 XGL_FALSE, // depthClipEnable
860 XGL_FALSE, // rasterizerDiscardEnable
861 1.0 // pointSize
862 };
863
864 XGL_PIPELINE_CB_STATE cb_state = {
865 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
866 &rs_state,
867 XGL_FALSE, // alphaToCoverageEnable
868 XGL_FALSE, // dualSourceBlendEnable
869 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
870 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
871 {
872 XGL_FALSE, // blendEnable
873 m_render_target_fmt, // XGL_FORMAT
874 0xF // channelWriteMask
875 }
876 }
877 };
878
879 // TODO: Should take depth buffer format from queried formats
880 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
881 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
882 &cb_state,
883 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
884 };
885
886 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
887 info.pNext = &db_state;
888 info.flags = 0;
889 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
890 ASSERT_XGL_SUCCESS(err);
891
892 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
893 ASSERT_XGL_SUCCESS(err);
894}
895
896void XglRenderTest::CreatePipelineVSUniform(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps)
897{
898 XGL_RESULT err;
899 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
900 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
901 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
902
903
904 const int vsSlots = 1; // Uniform buffer only
905
906 // Create descriptor set for our one resource
907 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
908 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
909 descriptorInfo.slots = vsSlots;
910
911 // create a descriptor set with a single slot
912 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
913 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
914
915 // bind memory to the descriptor set
916 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
917
918
919 XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( vsSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
920 slotInfo[0].shaderEntityIndex = 0;
921 slotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
922
923 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
924 vs_stage.pNext = XGL_NULL_HANDLE;
925 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
926 vs_stage.shader.shader = vs;
927 vs_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
928 vs_stage.shader.descriptorSetMapping[0].descriptorCount = vsSlots;
929 vs_stage.shader.linkConstBufferCount = 0;
930 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
931 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
932 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
933
934 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
935 ps_stage.pNext = &vs_stage;
936 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
937 ps_stage.shader.shader = ps;
938 ps_stage.shader.descriptorSetMapping[0].descriptorCount = 0;
939 ps_stage.shader.linkConstBufferCount = 0;
940 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
941 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
942 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
943
944 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
945 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
946 &ps_stage, // pNext
947 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
948 XGL_FALSE, // disableVertexReuse
949 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
950 XGL_FALSE, // primitiveRestartEnable
951 0 // primitiveRestartIndex
952 };
953
954 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
955 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
956 &ia_state,
957 XGL_FALSE, // depthClipEnable
958 XGL_FALSE, // rasterizerDiscardEnable
959 1.0 // pointSize
960 };
961
962 XGL_PIPELINE_CB_STATE cb_state = {
963 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
964 &rs_state,
965 XGL_FALSE, // alphaToCoverageEnable
966 XGL_FALSE, // dualSourceBlendEnable
967 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
968 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
969 {
970 XGL_FALSE, // blendEnable
971 m_render_target_fmt, // XGL_FORMAT
972 0xF // channelWriteMask
973 }
974 }
975 };
976
977 // TODO: Should take depth buffer format from queried formats
978 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
979 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
980 &cb_state,
981 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
982 };
983
984 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
985 info.pNext = &db_state;
986 info.flags = 0;
987 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
988 ASSERT_XGL_SUCCESS(err);
989
990 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
991 ASSERT_XGL_SUCCESS(err);
992}
993
Cody Northropec1d3c32014-10-28 15:41:42 -0600994void XglRenderTest::CreatePipelineFSUniformBlockBinding(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps, const int bufferCount)
995{
996 // based on CreateDefaultPipeline
997 // only difference is number of constant buffers
998
999 XGL_RESULT err;
1000 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1001 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
1002 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
1003
1004 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1005 vs_stage.pNext = XGL_NULL_HANDLE;
1006 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
1007 vs_stage.shader.shader = vs;
1008 vs_stage.shader.descriptorSetMapping[0].descriptorCount = 0;
1009 vs_stage.shader.linkConstBufferCount = 0;
1010 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1011 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1012 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1013
1014 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1015 ps_stage.pNext = &vs_stage;
1016 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1017 ps_stage.shader.shader = ps;
1018
1019// const int slots = 4;
1020// assert (slots == bufferCount); // update as needed
1021
1022 XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( bufferCount * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
Cody Northrop929838b2014-11-05 17:08:33 -07001023 for (int i = 0; i < bufferCount - 1; ++i) {
Cody Northropec1d3c32014-10-28 15:41:42 -06001024 // Note: These are all constant buffers
1025 slotInfo[i].shaderEntityIndex = i;
1026 slotInfo[i].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1027 }
1028
Cody Northrop929838b2014-11-05 17:08:33 -07001029 slotInfo[bufferCount - 1].shaderEntityIndex = 18;
1030 slotInfo[bufferCount - 1].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1031
Cody Northropec1d3c32014-10-28 15:41:42 -06001032 ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
1033 ps_stage.shader.descriptorSetMapping[0].descriptorCount = bufferCount;
1034
1035 ps_stage.shader.linkConstBufferCount = 0;
1036 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1037 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1038 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1039
1040 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
1041 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
1042 &ps_stage, // pNext
1043 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
1044 XGL_FALSE, // disableVertexReuse
1045 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
1046 XGL_FALSE, // primitiveRestartEnable
1047 0 // primitiveRestartIndex
1048 };
1049
1050 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
1051 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
1052 &ia_state,
1053 XGL_FALSE, // depthClipEnable
1054 XGL_FALSE, // rasterizerDiscardEnable
1055 1.0 // pointSize
1056 };
1057
1058 XGL_PIPELINE_CB_STATE cb_state = {
1059 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
1060 &rs_state,
1061 XGL_FALSE, // alphaToCoverageEnable
1062 XGL_FALSE, // dualSourceBlendEnable
1063 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
1064 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
1065 {
1066 XGL_FALSE, // blendEnable
1067 m_render_target_fmt, // XGL_FORMAT
1068 0xF // channelWriteMask
1069 }
1070 }
1071 };
1072
1073 // TODO: Should take depth buffer format from queried formats
1074 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
1075 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
1076 &cb_state,
1077 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
1078 };
1079
1080 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1081 info.pNext = &db_state;
1082 info.flags = 0;
1083 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
1084 ASSERT_XGL_SUCCESS(err);
1085
1086 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
1087 ASSERT_XGL_SUCCESS(err);
1088}
1089
Cody Northropc6953d02014-10-20 11:23:32 -06001090void XglRenderTest::CreatePipelineSingleTextureAndSampler(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps)
1091{
1092 // based on CreatePipelineVSUniform
1093
1094 XGL_RESULT err;
1095 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1096 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
1097 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
1098
Cody Northrop9394e0c2014-10-23 10:21:47 -06001099 const int vsSlots = 2; // One texture, one sampler
Cody Northropc6953d02014-10-20 11:23:32 -06001100
1101 // Create descriptor set for single texture and sampler
Cody Northrop9394e0c2014-10-23 10:21:47 -06001102 XGL_DESCRIPTOR_SET_CREATE_INFO vsDescriptorInfo = {};
1103 vsDescriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1104 vsDescriptorInfo.slots = vsSlots;
1105 err = xglCreateDescriptorSet( device(), &vsDescriptorInfo, &m_rsrcDescSet );
Cody Northropc6953d02014-10-20 11:23:32 -06001106 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1107 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1108
Cody Northropc6953d02014-10-20 11:23:32 -06001109 // Assign the slots, note that only t0 and s0 will work as of writing this test
Cody Northrop9394e0c2014-10-23 10:21:47 -06001110 XGL_DESCRIPTOR_SLOT_INFO *vsSlotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( vsSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
1111 vsSlotInfo[0].shaderEntityIndex = 0;
1112 vsSlotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1113 vsSlotInfo[1].shaderEntityIndex = 0;
1114 vsSlotInfo[1].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
Cody Northropc6953d02014-10-20 11:23:32 -06001115
1116 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1117 vs_stage.pNext = XGL_NULL_HANDLE;
1118 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
1119 vs_stage.shader.shader = vs;
Cody Northrop9394e0c2014-10-23 10:21:47 -06001120 vs_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) vsSlotInfo;
1121 vs_stage.shader.descriptorSetMapping[0].descriptorCount = vsSlots;
Cody Northropc6953d02014-10-20 11:23:32 -06001122 vs_stage.shader.linkConstBufferCount = 0;
1123 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1124 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1125 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1126
Cody Northrop9394e0c2014-10-23 10:21:47 -06001127 const int psSlots = 2; // One texture, one sampler
1128
1129 // Create descriptor set for single texture and sampler
1130 XGL_DESCRIPTOR_SET_CREATE_INFO psDescriptorInfo = {};
1131 psDescriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1132 psDescriptorInfo.slots = psSlots;
1133 err = xglCreateDescriptorSet( device(), &psDescriptorInfo, &m_rsrcDescSet );
1134 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1135 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1136
1137 // Assign the slots, note that only t0 and s0 will work as of writing this test
1138 XGL_DESCRIPTOR_SLOT_INFO *psSlotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( psSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
1139 psSlotInfo[0].shaderEntityIndex = 0;
1140 psSlotInfo[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1141 psSlotInfo[1].shaderEntityIndex = 0;
1142 psSlotInfo[1].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
1143
Cody Northropc6953d02014-10-20 11:23:32 -06001144 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1145 ps_stage.pNext = &vs_stage;
1146 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1147 ps_stage.shader.shader = ps;
Cody Northrop9394e0c2014-10-23 10:21:47 -06001148 ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) psSlotInfo;
Cody Northropc6953d02014-10-20 11:23:32 -06001149 ps_stage.shader.descriptorSetMapping[0].descriptorCount = psSlots;
1150 ps_stage.shader.linkConstBufferCount = 0;
1151 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1152 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1153 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1154
1155 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
1156 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
1157 &ps_stage, // pNext
1158 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
1159 XGL_FALSE, // disableVertexReuse
1160 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
1161 XGL_FALSE, // primitiveRestartEnable
1162 0 // primitiveRestartIndex
1163 };
1164
1165 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
1166 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
1167 &ia_state,
1168 XGL_FALSE, // depthClipEnable
1169 XGL_FALSE, // rasterizerDiscardEnable
1170 1.0 // pointSize
1171 };
1172
1173 XGL_PIPELINE_CB_STATE cb_state = {
1174 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
1175 &rs_state,
1176 XGL_FALSE, // alphaToCoverageEnable
1177 XGL_FALSE, // dualSourceBlendEnable
1178 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
1179 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
1180 {
1181 XGL_FALSE, // blendEnable
1182 m_render_target_fmt, // XGL_FORMAT
1183 0xF // channelWriteMask
1184 }
1185 }
1186 };
1187
1188 // TODO: Should take depth buffer format from queried formats
1189 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
1190 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
1191 &cb_state,
1192 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
1193 };
1194
1195 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1196 info.pNext = &db_state;
1197 info.flags = 0;
1198 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
1199 ASSERT_XGL_SUCCESS(err);
1200
1201 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
1202 ASSERT_XGL_SUCCESS(err);
1203}
1204
Cody Northropac6179b2014-10-29 12:09:45 -06001205void XglRenderTest::CreatePipelineMultipleTexturesAndSamplers(XGL_PIPELINE* pipeline, XGL_SHADER vs, XGL_SHADER ps,
1206 int textureCount, int samplerCount)
1207{
1208 // based on CreatePipelineSingleTextureAndSampler
1209
1210 XGL_RESULT err;
1211 XGL_GRAPHICS_PIPELINE_CREATE_INFO info = {};
1212 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs_stage;
1213 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO ps_stage;
1214
1215 const int psSlots = textureCount + samplerCount;
1216
1217 // Create descriptor set for single texture and sampler
1218 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
1219 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1220 descriptorInfo.slots = psSlots;
1221 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1222 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1223 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1224
1225 // Assign the slots, note that only t0 and s0 will work as of writing this test
1226 XGL_DESCRIPTOR_SLOT_INFO *slotInfo = (XGL_DESCRIPTOR_SLOT_INFO*) malloc( psSlots * sizeof(XGL_DESCRIPTOR_SLOT_INFO) );
1227 int slotIndex = 0;
Cody Northrop929838b2014-11-05 17:08:33 -07001228 for (int i = 0; i < textureCount - 1; ++i) {
Cody Northropac6179b2014-10-29 12:09:45 -06001229 slotInfo[slotIndex].shaderEntityIndex = i;
1230 slotInfo[slotIndex++].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1231 }
Cody Northrop929838b2014-11-05 17:08:33 -07001232 slotInfo[slotIndex].shaderEntityIndex = 12;
1233 slotInfo[slotIndex++].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1234 for (int i = 0; i < samplerCount - 1; ++i) {
Cody Northropac6179b2014-10-29 12:09:45 -06001235 slotInfo[slotIndex].shaderEntityIndex = i;
1236 slotInfo[slotIndex++].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
1237 }
1238
Cody Northrop929838b2014-11-05 17:08:33 -07001239 slotInfo[slotIndex].shaderEntityIndex = 12;
1240 slotInfo[slotIndex++].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
1241
Cody Northropac6179b2014-10-29 12:09:45 -06001242 vs_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1243 vs_stage.pNext = XGL_NULL_HANDLE;
1244 vs_stage.shader.stage = XGL_SHADER_STAGE_VERTEX;
1245 vs_stage.shader.shader = vs;
1246 vs_stage.shader.descriptorSetMapping[0].descriptorCount = 0;
1247 vs_stage.shader.linkConstBufferCount = 0;
1248 vs_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1249 vs_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1250 vs_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1251
1252 ps_stage.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1253 ps_stage.pNext = &vs_stage;
1254 ps_stage.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1255 ps_stage.shader.shader = ps;
1256 ps_stage.shader.descriptorSetMapping[0].pDescriptorInfo = (const XGL_DESCRIPTOR_SLOT_INFO*) slotInfo;
1257 ps_stage.shader.descriptorSetMapping[0].descriptorCount = psSlots;
1258 ps_stage.shader.linkConstBufferCount = 0;
1259 ps_stage.shader.pLinkConstBufferInfo = XGL_NULL_HANDLE;
1260 ps_stage.shader.dynamicMemoryViewMapping.slotObjectType = XGL_SLOT_UNUSED;
1261 ps_stage.shader.dynamicMemoryViewMapping.shaderEntityIndex = 0;
1262
1263 XGL_PIPELINE_IA_STATE_CREATE_INFO ia_state = {
1264 XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO, // sType
1265 &ps_stage, // pNext
1266 XGL_TOPOLOGY_TRIANGLE_LIST, // XGL_PRIMITIVE_TOPOLOGY
1267 XGL_FALSE, // disableVertexReuse
1268 XGL_PROVOKING_VERTEX_LAST, // XGL_PROVOKING_VERTEX_CONVENTION
1269 XGL_FALSE, // primitiveRestartEnable
1270 0 // primitiveRestartIndex
1271 };
1272
1273 XGL_PIPELINE_RS_STATE_CREATE_INFO rs_state = {
1274 XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO,
1275 &ia_state,
1276 XGL_FALSE, // depthClipEnable
1277 XGL_FALSE, // rasterizerDiscardEnable
1278 1.0 // pointSize
1279 };
1280
1281 XGL_PIPELINE_CB_STATE cb_state = {
1282 XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO,
1283 &rs_state,
1284 XGL_FALSE, // alphaToCoverageEnable
1285 XGL_FALSE, // dualSourceBlendEnable
1286 XGL_LOGIC_OP_COPY, // XGL_LOGIC_OP
1287 { // XGL_PIPELINE_CB_ATTACHMENT_STATE
1288 {
1289 XGL_FALSE, // blendEnable
1290 m_render_target_fmt, // XGL_FORMAT
1291 0xF // channelWriteMask
1292 }
1293 }
1294 };
1295
1296 // TODO: Should take depth buffer format from queried formats
1297 XGL_PIPELINE_DB_STATE_CREATE_INFO db_state = {
1298 XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO,
1299 &cb_state,
1300 {XGL_CH_FMT_R32, XGL_NUM_FMT_DS} // XGL_FORMAT
1301 };
1302
1303 info.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1304 info.pNext = &db_state;
1305 info.flags = 0;
1306 err = xglCreateGraphicsPipeline(device(), &info, pipeline);
1307 ASSERT_XGL_SUCCESS(err);
1308
1309 err = m_device->AllocAndBindGpuMemory(*pipeline, "Pipeline", &m_pipe_mem);
1310 ASSERT_XGL_SUCCESS(err);
1311}
1312
Cody Northrop722ff402014-10-20 09:22:42 -06001313void XglRenderTest::DrawTriangleWithVertexFetch(const char *vertShaderText, const char *fragShaderText)
1314{
1315 XGL_PIPELINE pipeline;
1316 XGL_SHADER vs, ps;
1317 XGL_RESULT err;
1318
1319 ASSERT_NO_FATAL_FAILURE(InitState());
1320 ASSERT_NO_FATAL_FAILURE(InitViewport());
1321 ASSERT_NO_FATAL_FAILURE(InitMesh(sizeof(g_vbData)/sizeof(g_vbData[0]), sizeof(g_vbData[0]), g_vbData));
1322
1323 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
1324 vertShaderText, &vs));
1325
1326 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
1327 fragShaderText, &ps));
1328
1329 ASSERT_NO_FATAL_FAILURE(CreatePipelineWithVertexFetch(&pipeline, vs, ps));
1330
1331 /*
1332 * Shaders are now part of the pipeline, don't need these anymore
1333 */
1334 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
1335 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
1336
1337 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1338
1339 // Build command buffer
1340 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1341 ASSERT_XGL_SUCCESS(err);
1342
1343 GenerateClearAndPrepareBufferCmds();
1344 GenerateBindRenderTargetCmd();
1345 GenerateBindStateAndPipelineCmds(&pipeline);
1346
1347// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
1348// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
1349
1350 // render the cube
1351 xglCmdDraw( m_cmdBuffer, 0, 6, 0, 1 );
1352
1353 // prepare the back buffer for present
1354// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
1355// transitionToPresent.image = m_image;
1356// transitionToPresent.oldState = m_image_state;
1357// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
1358// transitionToPresent.subresourceRange = srRange;
1359// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
1360// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
1361
1362 // finalize recording of the command buffer
1363 err = xglEndCommandBuffer( m_cmdBuffer );
1364 ASSERT_XGL_SUCCESS( err );
1365
1366 // this command buffer only uses the vertex buffer memory
1367 m_numMemRefs = 0;
1368// m_memRefs[0].flags = 0;
1369// m_memRefs[0].mem = m_vtxBufferMemory;
1370
1371 // submit the command buffer to the universal queue
1372 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
1373 ASSERT_XGL_SUCCESS( err );
1374
1375 err = xglQueueWaitIdle( m_device->m_queue );
1376 ASSERT_XGL_SUCCESS( err );
1377
1378 // Wait for work to finish before cleaning up.
1379 xglDeviceWaitIdle(m_device->device());
1380
1381 RecordImage(m_renderTarget);
1382
1383}
1384
Cody Northropec1d3c32014-10-28 15:41:42 -06001385void XglRenderTest::DrawTriangleFSUniformBlockBinding(const char *vertShaderText, const char *fragShaderText)
1386{
1387 // sourced from DrawTriangleFSUniformBlock
1388
1389 XGL_PIPELINE pipeline;
1390 XGL_SHADER vs, ps;
1391 XGL_RESULT err;
1392
1393 ASSERT_NO_FATAL_FAILURE(InitState());
1394 ASSERT_NO_FATAL_FAILURE(InitViewport());
1395
1396 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
1397 vertShaderText, &vs));
1398
1399 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
1400 fragShaderText, &ps));
1401
1402 const int bufferCount = 4;
1403 ASSERT_NO_FATAL_FAILURE(CreatePipelineFSUniformBlockBinding(&pipeline, vs, ps, bufferCount));
1404
1405 /*
1406 * Shaders are now part of the pipeline, don't need these anymore
1407 */
1408 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
1409 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
1410
1411 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1412
1413
1414 // We're going to create a number of uniform buffers, and then allow
1415 // the shader to select which it wants to read from with a binding
1416
1417 // Let's populate the buffers with a single color each:
1418 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
1419 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
1420 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
1421 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
1422
1423 assert(4 == bufferCount); // update the following code if you want more than 4
1424
1425 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
1426 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
1427 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
1428 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
1429
1430 const int redCount = sizeof(redVals) / sizeof(float);
1431 const int greenCount = sizeof(greenVals) / sizeof(float);
1432 const int blueCount = sizeof(blueVals) / sizeof(float);
1433 const int whiteCount = sizeof(whiteVals) / sizeof(float);
1434
1435 int index = 0;
1436 InitUniformBuffer(redCount, sizeof(redVals[0]), index++, (const void *) redVals);
1437 InitUniformBuffer(greenCount, sizeof(greenVals[0]), index++, (const void *) greenVals);
1438 InitUniformBuffer(blueCount, sizeof(blueVals[0]), index++, (const void *) blueVals);
1439 InitUniformBuffer(whiteCount, sizeof(whiteVals[0]), index++, (const void *) whiteVals);
1440
1441
1442 // Create descriptor set for a uniform resource
1443 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
1444 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1445 descriptorInfo.slots = bufferCount;
1446
1447 // create a descriptor set with a single slot
1448 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1449 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1450
1451 // bind memory to the descriptor set
1452 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1453
1454 // write the constant buffer view to the descriptor set
1455 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
1456
1457 for(int i = 0; i < bufferCount; ++i)
1458 xglAttachMemoryViewDescriptors( m_rsrcDescSet, i, 1, &m_uniformBufferView[i] );
1459
1460 xglEndDescriptorSetUpdate( m_rsrcDescSet );
1461
1462 // Build command buffer
1463 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1464 ASSERT_XGL_SUCCESS(err);
1465
1466 GenerateClearAndPrepareBufferCmds();
1467 GenerateBindRenderTargetCmd();
1468 GenerateBindStateAndPipelineCmds(&pipeline);
1469
1470// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
1471// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
1472
1473 // render the cube
1474 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
1475
1476 // prepare the back buffer for present
1477// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
1478// transitionToPresent.image = m_image;
1479// transitionToPresent.oldState = m_image_state;
1480// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
1481// transitionToPresent.subresourceRange = srRange;
1482// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
1483// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
1484
1485 // finalize recording of the command buffer
1486 err = xglEndCommandBuffer( m_cmdBuffer );
1487 ASSERT_XGL_SUCCESS( err );
1488
1489 // this command buffer only uses the vertex buffer memory
1490 m_numMemRefs = 0;
1491// m_memRefs[0].flags = 0;
1492// m_memRefs[0].mem = m_vtxBufferMemory;
1493
1494 // submit the command buffer to the universal queue
1495 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
1496 ASSERT_XGL_SUCCESS( err );
1497
1498 err = xglQueueWaitIdle( m_device->m_queue );
1499 ASSERT_XGL_SUCCESS( err );
1500
1501 // Wait for work to finish before cleaning up.
1502 xglDeviceWaitIdle(m_device->device());
1503
1504 RecordImage(m_renderTarget);
1505
1506}
1507
Cody Northropac6179b2014-10-29 12:09:45 -06001508void XglRenderTest::DrawSamplerBindingsTriangle(const char *vertShaderText, const char *fragShaderText,
1509 int textureCount, int samplerCount)
1510{
1511 // based on DrawTexturedTriangle
1512
1513 XGL_PIPELINE pipeline;
1514 XGL_SHADER vs, ps;
1515 XGL_RESULT err;
1516
1517 ASSERT_NO_FATAL_FAILURE(InitState());
1518 ASSERT_NO_FATAL_FAILURE(InitViewport());
1519
1520 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_VERTEX,
1521 vertShaderText, &vs));
1522
1523 ASSERT_NO_FATAL_FAILURE(CreateShader(XGL_SHADER_STAGE_FRAGMENT,
1524 fragShaderText, &ps));
1525
1526 ASSERT_NO_FATAL_FAILURE(CreatePipelineMultipleTexturesAndSamplers(&pipeline, vs, ps, textureCount, samplerCount));
1527
1528 /*
1529 * Shaders are now part of the pipeline, don't need these anymore
1530 */
1531 ASSERT_XGL_SUCCESS(xglDestroyObject(ps));
1532 ASSERT_XGL_SUCCESS(xglDestroyObject(vs));
1533
1534 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1535
1536
1537 // Create a few texture/sampler pairs
1538 int textureColors[16];
1539 assert(textureCount < 16);
1540 textureColors[0] = 0xFF0000FF; //red
1541 textureColors[1] = 0xFF00FF00; //green
1542 textureColors[2] = 0xFFFF0000; //blue
1543
1544 ASSERT_NO_FATAL_FAILURE(InitMultipleSamplers(samplerCount));
1545 ASSERT_NO_FATAL_FAILURE(InitMultipleTextures(textureCount, textureColors));
1546
1547 // Create descriptor set for a texture and sampler resources
1548 const int slotCount = textureCount + samplerCount;
1549 XGL_DESCRIPTOR_SET_CREATE_INFO descriptorInfo = {};
1550 descriptorInfo.sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO;
1551 descriptorInfo.slots = slotCount;
1552
1553 // create a descriptor set with a single slot
1554 err = xglCreateDescriptorSet( device(), &descriptorInfo, &m_rsrcDescSet );
1555 ASSERT_XGL_SUCCESS(err) << "xglCreateDescriptorSet failed";
1556
1557 // bind memory to the descriptor set
1558 err = m_device->AllocAndBindGpuMemory(m_rsrcDescSet, "DescriptorSet", &m_descriptor_set_mem);
1559
1560 // write the sampler and image views to the descriptor set
1561 // ensure this matches order set in CreatePipelineSingleTextureAndSampler
1562 xglBeginDescriptorSetUpdate( m_rsrcDescSet );
1563 int descSlot = 0;
1564 for (int i = 0; i < textureCount; ++i)
1565 xglAttachImageViewDescriptors( m_rsrcDescSet, descSlot++, 1, &m_textureViewInfo[i]);
1566 for (int i = 0; i < samplerCount; ++i)
1567 xglAttachSamplerDescriptors(m_rsrcDescSet, descSlot++, 1, &m_sampler[i]);
1568 xglEndDescriptorSetUpdate( m_rsrcDescSet );
1569
1570 // Build command buffer
1571 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1572 ASSERT_XGL_SUCCESS(err);
1573
1574 GenerateClearAndPrepareBufferCmds();
1575 GenerateBindRenderTargetCmd();
1576 GenerateBindStateAndPipelineCmds(&pipeline);
1577
1578// xglCmdBindDescriptorSet(m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, 0, m_rsrcDescSet, 0 );
1579// xglCmdBindDynamicMemoryView( m_cmdBuffer, XGL_PIPELINE_BIND_POINT_GRAPHICS, &m_constantBufferView );
1580
1581 // render the cube
1582 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
1583
1584 // prepare the back buffer for present
1585// XGL_IMAGE_STATE_TRANSITION transitionToPresent = {};
1586// transitionToPresent.image = m_image;
1587// transitionToPresent.oldState = m_image_state;
1588// transitionToPresent.newState = m_display.fullscreen ? XGL_WSI_WIN_PRESENT_SOURCE_FLIP : XGL_WSI_WIN_PRESENT_SOURCE_BLT;
1589// transitionToPresent.subresourceRange = srRange;
1590// xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToPresent );
1591// m_image_state = ( XGL_IMAGE_STATE ) transitionToPresent.newState;
1592
1593 // finalize recording of the command buffer
1594 err = xglEndCommandBuffer( m_cmdBuffer );
1595 ASSERT_XGL_SUCCESS( err );
1596
1597 // this command buffer only uses the vertex buffer memory
1598 m_numMemRefs = 0;
1599// m_memRefs[0].flags = 0;
1600// m_memRefs[0].mem = m_vtxBufferMemory;
1601
1602 // submit the command buffer to the universal queue
1603 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
1604 ASSERT_XGL_SUCCESS( err );
1605
1606 err = xglQueueWaitIdle( m_device->m_queue );
1607 ASSERT_XGL_SUCCESS( err );
1608
1609 // Wait for work to finish before cleaning up.
1610 xglDeviceWaitIdle(m_device->device());
1611
1612 RecordImage(m_renderTarget);
1613
1614}
1615
Cody Northrop722ff402014-10-20 09:22:42 -06001616TEST_F(XglRenderTest, GreenTriangle)
1617{
1618 static const char *vertShaderText =
1619 "#version 130\n"
1620 "vec2 vertices[3];\n"
1621 "void main() {\n"
1622 " vertices[0] = vec2(-1.0, -1.0);\n"
1623 " vertices[1] = vec2( 1.0, -1.0);\n"
1624 " vertices[2] = vec2( 0.0, 1.0);\n"
1625 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1626 "}\n";
1627
1628 static const char *fragShaderText =
Cody Northrop6217b822014-10-30 11:20:22 -06001629 "#version 130\n"
1630 "void main() {\n"
1631 " gl_FragColor = vec4(0,1,0,1);\n"
1632 "}\n";
Cody Northrop722ff402014-10-20 09:22:42 -06001633
Cody Northrop722ff402014-10-20 09:22:42 -06001634 XglTestFramework::m_use_bil = true;
1635 DrawTriangleTest(vertShaderText, fragShaderText);
Cody Northrop722ff402014-10-20 09:22:42 -06001636}
1637
GregFef5d9cf2014-10-22 14:40:26 -06001638TEST_F(XglRenderTest, MixTriangle)
1639{
1640 // This tests location applied to varyings. Notice that we have switched foo
1641 // and bar in the FS. The triangle should be blended with red, green and blue
1642 // corners.
1643 static const char *vertShaderText =
1644 "#version 140\n"
1645 "#extension GL_ARB_separate_shader_objects : enable\n"
1646 "#extension GL_ARB_shading_language_420pack : enable\n"
1647 "layout (location=0) out vec4 bar;\n"
1648 "layout (location=1) out vec4 foo;\n"
Courtney Goeltzenleuchter3d88b532014-11-03 15:40:13 -07001649 "layout (location=2) out float scale;\n"
GregFef5d9cf2014-10-22 14:40:26 -06001650 "vec2 vertices[3];\n"
1651 "void main() {\n"
1652 " vertices[0] = vec2(-1.0, -1.0);\n"
1653 " vertices[1] = vec2( 1.0, -1.0);\n"
1654 " vertices[2] = vec2( 0.0, 1.0);\n"
1655 "vec4 colors[3];\n"
1656 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1657 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1658 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1659 " foo = colors[gl_VertexID % 3];\n"
1660 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
Courtney Goeltzenleuchter3d88b532014-11-03 15:40:13 -07001661 " scale = 1.0;\n"
GregFef5d9cf2014-10-22 14:40:26 -06001662 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1663 "}\n";
1664
1665 static const char *fragShaderText =
Cody Northrop6217b822014-10-30 11:20:22 -06001666 "#version 140\n"
1667 "#extension GL_ARB_separate_shader_objects : enable\n"
1668 "#extension GL_ARB_shading_language_420pack : enable\n"
1669 "layout (location = 1) in vec4 bar;\n"
1670 "layout (location = 0) in vec4 foo;\n"
Courtney Goeltzenleuchter3d88b532014-11-03 15:40:13 -07001671 "layout (location = 2) in float scale;\n"
Cody Northrop6217b822014-10-30 11:20:22 -06001672 "void main() {\n"
Courtney Goeltzenleuchter3d88b532014-11-03 15:40:13 -07001673 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
Cody Northrop6217b822014-10-30 11:20:22 -06001674 "}\n";
GregFef5d9cf2014-10-22 14:40:26 -06001675
GregF91b2d302014-10-23 10:44:44 -06001676 XglTestFramework::m_use_bil = true;
1677 DrawTriangleTest(vertShaderText, fragShaderText);
Cody Northrop722ff402014-10-20 09:22:42 -06001678}
1679
1680TEST_F(XglRenderTest, TriangleWithVertexFetch)
1681{
1682 static const char *vertShaderText =
Cody Northrop6217b822014-10-30 11:20:22 -06001683 "#version 140\n"
1684 "#extension GL_ARB_separate_shader_objects : enable\n"
1685 "#extension GL_ARB_shading_language_420pack : enable\n"
Cody Northrop722ff402014-10-20 09:22:42 -06001686 //XYZ1( 0.f, 0.f, 0.f )
Cody Northrop6217b822014-10-30 11:20:22 -06001687 "layout (location = 1) in vec4 inColor;\n"
GregF781f8562014-11-04 13:42:39 -07001688 //XYZ1( -1, -1, -1 )
1689 "layout (location = 0) in vec4 pos;\n"
Cody Northrop6217b822014-10-30 11:20:22 -06001690 "layout (location = 0) out vec4 outColor;\n"
Cody Northrop722ff402014-10-20 09:22:42 -06001691 "void main() {\n"
1692 " outColor = inColor;\n"
1693 " gl_Position = pos;\n"
1694 "}\n";
1695
1696
1697 static const char *fragShaderText =
Cody Northrop6217b822014-10-30 11:20:22 -06001698 "#version 140\n"
1699 "#extension GL_ARB_separate_shader_objects : enable\n"
1700 "#extension GL_ARB_shading_language_420pack : enable\n"
1701 "layout (location = 0) in vec4 color;\n"
Cody Northrop722ff402014-10-20 09:22:42 -06001702 "void main() {\n"
1703 " gl_FragColor = color;\n"
1704 "}\n";
1705
Cody Northrop6217b822014-10-30 11:20:22 -06001706 XglTestFramework::m_use_bil = true;
Cody Northrop722ff402014-10-20 09:22:42 -06001707 DrawTriangleWithVertexFetch(vertShaderText, fragShaderText);
1708}
1709
GregF1ce3a832014-10-30 16:46:12 -06001710TEST_F(XglRenderTest, TriVertFetchAndVertID)
1711{
1712 // This tests that attributes work in the presence of gl_VertexID
1713
1714 static const char *vertShaderText =
1715 "#version 140\n"
1716 "#extension GL_ARB_separate_shader_objects : enable\n"
1717 "#extension GL_ARB_shading_language_420pack : enable\n"
1718 //XYZ1( -1, -1, -1 )
1719 "layout (location = 0) in vec4 pos;\n"
1720 //XYZ1( 0.f, 0.f, 0.f )
1721 "layout (location = 1) in vec4 inColor;\n"
1722 "layout (location = 0) out vec4 outColor;\n"
1723 "void main() {\n"
1724 " outColor = inColor;\n"
1725 " vec4 vertices[3];"
1726 " vertices[gl_VertexID % 3] = pos;\n"
1727 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1728 "}\n";
1729
1730
1731 static const char *fragShaderText =
1732 "#version 140\n"
1733 "#extension GL_ARB_separate_shader_objects : enable\n"
1734 "#extension GL_ARB_shading_language_420pack : enable\n"
1735 "layout (location = 0) in vec4 color;\n"
1736 "void main() {\n"
1737 " gl_FragColor = color;\n"
1738 "}\n";
1739
1740 XglTestFramework::m_use_bil = true;
1741 DrawTriangleWithVertexFetch(vertShaderText, fragShaderText);
1742}
1743
GregF31deb632014-10-31 17:36:18 -06001744TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1745{
1746 // This tests that attributes work in the presence of gl_VertexID
GregF8e5eb2b2014-11-04 14:20:50 -07001747 // and a dead attribute in position 0. Draws a triangle with yellow,
1748 // red and green corners, starting at top and going clockwise.
GregF31deb632014-10-31 17:36:18 -06001749
1750 static const char *vertShaderText =
1751 "#version 140\n"
1752 "#extension GL_ARB_separate_shader_objects : enable\n"
1753 "#extension GL_ARB_shading_language_420pack : enable\n"
1754 //XYZ1( -1, -1, -1 )
1755 "layout (location = 0) in vec4 pos;\n"
1756 //XYZ1( 0.f, 0.f, 0.f )
1757 "layout (location = 1) in vec4 inColor;\n"
1758 "layout (location = 0) out vec4 outColor;\n"
1759 "void main() {\n"
GregF8e5eb2b2014-11-04 14:20:50 -07001760 " outColor = inColor;\n"
1761 " vec2 vertices[3];"
1762 " vertices[0] = vec2(-1.0, -1.0);\n"
1763 " vertices[1] = vec2( 1.0, -1.0);\n"
1764 " vertices[2] = vec2( 0.0, 1.0);\n"
1765 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
GregF31deb632014-10-31 17:36:18 -06001766 "}\n";
1767
1768
1769 static const char *fragShaderText =
1770 "#version 140\n"
1771 "#extension GL_ARB_separate_shader_objects : enable\n"
1772 "#extension GL_ARB_shading_language_420pack : enable\n"
1773 "layout (location = 0) in vec4 color;\n"
1774 "void main() {\n"
1775 " gl_FragColor = color;\n"
1776 "}\n";
1777
1778 XglTestFramework::m_use_bil = true;
1779 DrawTriangleWithVertexFetch(vertShaderText, fragShaderText);
1780}
1781
Cody Northropc6953d02014-10-20 11:23:32 -06001782TEST_F(XglRenderTest, TexturedTriangle)
1783{
Cody Northrop5fa1d332014-10-20 11:51:32 -06001784 // The expected result from this test is a red and green checkered triangle
Cody Northropc6953d02014-10-20 11:23:32 -06001785 static const char *vertShaderText =
Cody Northrop6217b822014-10-30 11:20:22 -06001786 "#version 140\n"
1787 "#extension GL_ARB_separate_shader_objects : enable\n"
1788 "#extension GL_ARB_shading_language_420pack : enable\n"
1789 "layout (location = 0) out vec2 samplePos;\n"
Cody Northropc6953d02014-10-20 11:23:32 -06001790 "void main() {\n"
1791 " vec2 vertices[3];"
1792 " vertices[0] = vec2(-0.5, -0.5);\n"
1793 " vertices[1] = vec2( 0.5, -0.5);\n"
1794 " vertices[2] = vec2( 0.5, 0.5);\n"
1795 " vec2 positions[3];"
1796 " positions[0] = vec2( 0.0, 0.0);\n"
1797 " positions[1] = vec2( 1.0, 0.0);\n"
1798 " positions[2] = vec2( 1.0, 1.0);\n"
1799 " samplePos = positions[gl_VertexID % 3];\n"
1800 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1801 "}\n";
1802
1803 static const char *fragShaderText =
Cody Northrop6217b822014-10-30 11:20:22 -06001804 "#version 140\n"
1805 "#extension GL_ARB_separate_shader_objects : enable\n"
1806 "#extension GL_ARB_shading_language_420pack : enable\n"
1807 "layout (location = 0) in vec2 samplePos;\n"
1808 "layout (binding = 0) uniform sampler2D surface;\n"
Courtney Goeltzenleuchter75a4d102014-11-03 15:44:32 -07001809 "layout (location=0) out vec4 outColor;\n"
Courtney Goeltzenleuchter75a4d102014-11-03 15:44:32 -07001810 "void main() {\n"
1811 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1812 " outColor = texColor;\n"
1813 "}\n";
GregFea15e542014-11-03 16:52:56 -07001814
1815 XglTestFramework::m_use_bil = true;
Courtney Goeltzenleuchter75a4d102014-11-03 15:44:32 -07001816 DrawTexturedTriangle(vertShaderText, fragShaderText);
1817}
1818
GregFe636c1a2014-11-07 13:29:23 -07001819TEST_F(XglRenderTest, TexturedTriangleClip)
GregFef017512014-11-06 14:53:03 -07001820{
1821 // The expected result from this test is a red and green checkered triangle
1822 static const char *vertShaderText =
GregFe636c1a2014-11-07 13:29:23 -07001823 "#version 330\n"
GregFef017512014-11-06 14:53:03 -07001824 "#extension GL_ARB_separate_shader_objects : enable\n"
1825 "#extension GL_ARB_shading_language_420pack : enable\n"
1826 "layout (location = 0) out vec2 samplePos;\n"
GregFe636c1a2014-11-07 13:29:23 -07001827 "out gl_PerVertex {\n"
1828 " vec4 gl_Position;\n"
1829 " float gl_ClipDistance[1];\n"
1830 "};\n"
GregFef017512014-11-06 14:53:03 -07001831 "void main() {\n"
1832 " vec2 vertices[3];"
1833 " vertices[0] = vec2(-0.5, -0.5);\n"
1834 " vertices[1] = vec2( 0.5, -0.5);\n"
1835 " vertices[2] = vec2( 0.5, 0.5);\n"
1836 " vec2 positions[3];"
1837 " positions[0] = vec2( 0.0, 0.0);\n"
1838 " positions[1] = vec2( 1.0, 0.0);\n"
1839 " positions[2] = vec2( 1.0, 1.0);\n"
GregFe636c1a2014-11-07 13:29:23 -07001840 " float dists[3];\n"
1841 " dists[0] = 1.0;\n"
1842 " dists[1] = 1.0;\n"
1843 " dists[2] = -1.0;\n"
1844 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
GregFef017512014-11-06 14:53:03 -07001845 " samplePos = positions[gl_VertexID % 3];\n"
1846 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1847 "}\n";
1848
1849 static const char *fragShaderText =
1850 "#version 140\n"
1851 "#extension GL_ARB_separate_shader_objects : enable\n"
1852 "#extension GL_ARB_shading_language_420pack : enable\n"
1853 "layout (location = 0) in vec2 samplePos;\n"
1854 "layout (binding = 0) uniform sampler2D surface;\n"
1855 "layout (location=0) out vec4 outColor;\n"
1856 "void main() {\n"
1857 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1858 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1859 " outColor = texColor;\n"
1860 "}\n";
1861
GregFe636c1a2014-11-07 13:29:23 -07001862 XglTestFramework::m_use_bil = false;
GregFef017512014-11-06 14:53:03 -07001863 DrawTexturedTriangle(vertShaderText, fragShaderText);
1864}
1865
Cody Northrop9394e0c2014-10-23 10:21:47 -06001866TEST_F(XglRenderTest, VSTexture)
1867{
1868 // The expected result from this test is a green and red triangle;
1869 // one red vertex on the left, two green vertices on the right.
1870 static const char *vertShaderText =
Cody Northrop6217b822014-10-30 11:20:22 -06001871 "#version 140\n"
1872 "#extension GL_ARB_separate_shader_objects : enable\n"
1873 "#extension GL_ARB_shading_language_420pack : enable\n"
1874 "layout (location = 0) out vec4 texColor;\n"
1875 "layout (binding = 0) uniform sampler2D surface;\n"
Cody Northrop9394e0c2014-10-23 10:21:47 -06001876 "void main() {\n"
1877 " vec2 vertices[3];"
1878 " vertices[0] = vec2(-0.5, -0.5);\n"
1879 " vertices[1] = vec2( 0.5, -0.5);\n"
1880 " vertices[2] = vec2( 0.5, 0.5);\n"
1881 " vec2 positions[3];"
1882 " positions[0] = vec2( 0.0, 0.0);\n"
1883 " positions[1] = vec2( 0.25, 0.1);\n"
1884 " positions[2] = vec2( 0.1, 0.25);\n"
1885 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1886 " texColor = textureLod(surface, samplePos, 0.0);\n"
1887 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1888 "}\n";
1889
1890 static const char *fragShaderText =
Cody Northrop6217b822014-10-30 11:20:22 -06001891 "#version 140\n"
1892 "#extension GL_ARB_separate_shader_objects : enable\n"
1893 "#extension GL_ARB_shading_language_420pack : enable\n"
1894 "layout (location = 0) in vec4 texColor;\n"
Cody Northrop9394e0c2014-10-23 10:21:47 -06001895 "void main() {\n"
1896 " gl_FragColor = texColor;\n"
1897 "}\n";
1898
Cody Northrop6217b822014-10-30 11:20:22 -06001899 XglTestFramework::m_use_bil = true;
Cody Northrop9394e0c2014-10-23 10:21:47 -06001900 DrawTexturedTriangle(vertShaderText, fragShaderText);
1901}
1902
Cody Northrop6217b822014-10-30 11:20:22 -06001903TEST_F(XglRenderTest, SamplerBindingsTriangle)
1904{
1905 // This test sets bindings on the samplers
1906 // For now we are asserting that sampler and texture pairs
1907 // march in lock step, and are set via GLSL binding. This can
1908 // and will probably change.
1909 // The sampler bindings should match the sampler and texture slot
1910 // number set up by the application.
1911 // This test will result in a blue triangle
1912 static const char *vertShaderText =
1913 "#version 140\n"
1914 "#extension GL_ARB_separate_shader_objects : enable\n"
1915 "#extension GL_ARB_shading_language_420pack : enable\n"
1916 "layout (location = 0) out vec4 samplePos;\n"
1917 "void main() {\n"
1918 " vec2 vertices[3];"
1919 " vertices[0] = vec2(-0.5, -0.5);\n"
1920 " vertices[1] = vec2( 0.5, -0.5);\n"
1921 " vertices[2] = vec2( 0.5, 0.5);\n"
1922 " vec2 positions[3];"
1923 " positions[0] = vec2( 0.0, 0.0);\n"
1924 " positions[1] = vec2( 1.0, 0.0);\n"
1925 " positions[2] = vec2( 1.0, 1.0);\n"
1926 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
1927 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1928 "}\n";
1929
1930 static const char *fragShaderText =
1931 "#version 140\n"
1932 "#extension GL_ARB_separate_shader_objects : enable\n"
1933 "#extension GL_ARB_shading_language_420pack : enable\n"
1934 "layout (location = 0) in vec4 samplePos;\n"
1935 "layout (binding = 0) uniform sampler2D surface0;\n"
1936 "layout (binding = 1) uniform sampler2D surface1;\n"
Cody Northrop929838b2014-11-05 17:08:33 -07001937 "layout (binding = 12) uniform sampler2D surface2;\n"
Cody Northrop6217b822014-10-30 11:20:22 -06001938 "void main() {\n"
1939 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
1940 "}\n";
1941
1942 XglTestFramework::m_use_bil = true;
1943 int textureCount = g_TextureCount;
1944 int samplerCount = g_SamplerCount;
1945 DrawSamplerBindingsTriangle(vertShaderText, fragShaderText, textureCount, samplerCount);
1946}
1947
1948TEST_F(XglRenderTest, TriangleVSUniformBlock)
1949{
1950 // The expected result from this test is a blue triangle
1951
1952 static const char *vertShaderText =
1953 "#version 140\n"
1954 "#extension GL_ARB_separate_shader_objects : enable\n"
1955 "#extension GL_ARB_shading_language_420pack : enable\n"
1956 "layout (location = 0) out vec4 outColor;\n"
1957 "layout (std140, binding = 0) uniform bufferVals {\n"
1958 " vec4 red;\n"
1959 " vec4 green;\n"
1960 " vec4 blue;\n"
1961 " vec4 white;\n"
1962 "} myBufferVals;\n"
1963 "void main() {\n"
1964 " vec2 vertices[3];"
1965 " vertices[0] = vec2(-0.5, -0.5);\n"
1966 " vertices[1] = vec2( 0.5, -0.5);\n"
1967 " vertices[2] = vec2( 0.5, 0.5);\n"
1968 " outColor = myBufferVals.blue;\n"
1969 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1970 "}\n";
1971
1972 static const char *fragShaderText =
1973 "#version 140\n"
1974 "#extension GL_ARB_separate_shader_objects : enable\n"
1975 "#extension GL_ARB_shading_language_420pack : enable\n"
1976 "layout (location = 0) in vec4 inColor;\n"
1977 "void main() {\n"
1978 " gl_FragColor = inColor;\n"
1979 "}\n";
1980
1981 XglTestFramework::m_use_bil = true;
1982 DrawTriangleVSUniformBlock(vertShaderText, fragShaderText);
1983}
1984
Cody Northropec1d3c32014-10-28 15:41:42 -06001985TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
1986{
1987 // This test allows the shader to select which buffer it is
1988 // pulling from using layout binding qualifier.
1989 // There are corresponding changes in the compiler stack that
1990 // will select the buffer using binding directly.
Cody Northrop6217b822014-10-30 11:20:22 -06001991 // The binding number should match the slot number set up by
1992 // the application.
Cody Northropec1d3c32014-10-28 15:41:42 -06001993 // The expected result from this test is a purple triangle
1994
1995 static const char *vertShaderText =
Cody Northrop6217b822014-10-30 11:20:22 -06001996 "#version 140\n"
1997 "#extension GL_ARB_separate_shader_objects : enable\n"
1998 "#extension GL_ARB_shading_language_420pack : enable\n"
Cody Northropec1d3c32014-10-28 15:41:42 -06001999 "void main() {\n"
2000 " vec2 vertices[3];"
2001 " vertices[0] = vec2(-0.5, -0.5);\n"
2002 " vertices[1] = vec2( 0.5, -0.5);\n"
2003 " vertices[2] = vec2( 0.5, 0.5);\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 (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2012 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2013 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
Cody Northrop929838b2014-11-05 17:08:33 -07002014 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
Cody Northropec1d3c32014-10-28 15:41:42 -06002015 "void main() {\n"
2016 " gl_FragColor = myBlueVal.color;\n"
2017 " gl_FragColor += myRedVal.color;\n"
2018 "}\n";
2019
Cody Northrop6217b822014-10-30 11:20:22 -06002020 XglTestFramework::m_use_bil = true;
Cody Northropec1d3c32014-10-28 15:41:42 -06002021 DrawTriangleFSUniformBlockBinding(vertShaderText, fragShaderText);
2022}
2023
Cody Northrop929838b2014-11-05 17:08:33 -07002024TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2025{
2026 // This test is the same as TriangleFSUniformBlockBinding, but
2027 // it does not provide an instance name.
2028 // The expected result from this test is a purple triangle
2029
2030 static const char *vertShaderText =
2031 "#version 140\n"
2032 "#extension GL_ARB_separate_shader_objects : enable\n"
2033 "#extension GL_ARB_shading_language_420pack : enable\n"
2034 "void main() {\n"
2035 " vec2 vertices[3];"
2036 " vertices[0] = vec2(-0.5, -0.5);\n"
2037 " vertices[1] = vec2( 0.5, -0.5);\n"
2038 " vertices[2] = vec2( 0.5, 0.5);\n"
2039 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2040 "}\n";
2041
2042 static const char *fragShaderText =
2043 "#version 430\n"
2044 "#extension GL_ARB_separate_shader_objects : enable\n"
2045 "#extension GL_ARB_shading_language_420pack : enable\n"
2046 "layout (location = 0) uniform vec4 foo;\n"
2047 "layout (location = 1) uniform vec4 bar;\n"
2048 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2049 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2050 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2051 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
2052 "void main() {\n"
2053 " gl_FragColor = blue;\n"
2054 " gl_FragColor += red;\n"
2055 "}\n";
2056
2057 XglTestFramework::m_use_bil = true;
2058 DrawTriangleFSUniformBlockBinding(vertShaderText, fragShaderText);
2059}
2060
Cody Northrop722ff402014-10-20 09:22:42 -06002061int main(int argc, char **argv) {
2062 int result;
2063
2064 ::testing::InitGoogleTest(&argc, argv);
2065 XglTestFramework::InitArgs(&argc, argv);
2066
2067 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
2068
2069 result = RUN_ALL_TESTS();
2070
2071 XglTestFramework::Finish();
2072 return result;
2073}