blob: 74997755c88f94d984ceb6702a157f4c9ea83987 [file] [log] [blame]
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06001// Copyright 2005, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31// XGL tests
32//
33// Copyright (C) 2014 LunarG, Inc.
34//
35// Permission is hereby granted, free of charge, to any person obtaining a
36// copy of this software and associated documentation files (the "Software"),
37// to deal in the Software without restriction, including without limitation
38// the rights to use, copy, modify, merge, publish, distribute, sublicense,
39// and/or sell copies of the Software, and to permit persons to whom the
40// Software is furnished to do so, subject to the following conditions:
41//
42// The above copyright notice and this permission notice shall be included
43// in all copies or substantial portions of the Software.
44//
45// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51// DEALINGS IN THE SOFTWARE.
52
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060053// Basic rendering tests
54
55#include <stdlib.h>
56#include <stdio.h>
57#include <stdbool.h>
58#include <string.h>
Courtney Goeltzenleuchter76a643b2014-08-21 17:34:22 -060059#include <iostream>
60#include <fstream>
61using namespace std;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060062
63#include <xgl.h>
Tobin Ehlis31446e52014-11-28 11:17:19 -070064#ifdef DUMP_STATE_DOT
65#include "../layers/draw_state.h"
66#endif
Tobin Ehlis3c26a542014-11-18 11:28:33 -070067#ifdef PRINT_OBJECTS
68#include "../layers/object_track.h"
69#endif
Tobin Ehlis791a49c2014-11-10 12:29:12 -070070#ifdef DEBUG_CALLBACK
71#include <xglDbg.h>
72#endif
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060073#include "gtest-1.7.0/include/gtest/gtest.h"
74
Cody Northropd4e020a2015-03-17 14:54:35 -060075#include "icd-spv.h"
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060076
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -060077#define GLM_FORCE_RADIANS
78#include "glm/glm.hpp"
79#include <glm/gtc/matrix_transform.hpp>
80
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060081#include "xglrenderframework.h"
Tobin Ehlis791a49c2014-11-10 12:29:12 -070082#ifdef DEBUG_CALLBACK
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060083void XGLAPI myDbgFunc(
Tobin Ehlis791a49c2014-11-10 12:29:12 -070084 XGL_DBG_MSG_TYPE msgType,
85 XGL_VALIDATION_LEVEL validationLevel,
86 XGL_BASE_OBJECT srcObject,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060087 size_t location,
88 int32_t msgCode,
89 const char* pMsg,
90 void* pUserData)
Tobin Ehlis791a49c2014-11-10 12:29:12 -070091{
Tobin Ehlis3c26a542014-11-18 11:28:33 -070092 switch (msgType)
93 {
94 case XGL_DBG_MSG_WARNING:
95 printf("CALLBACK WARNING : %s\n", pMsg);
96 break;
97 case XGL_DBG_MSG_ERROR:
98 printf("CALLBACK ERROR : %s\n", pMsg);
99 break;
100 default:
101 printf("EATING Msg of type %u\n", msgType);
102 break;
103 }
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700104}
105#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700106
107
108#undef ASSERT_NO_FATAL_FAILURE
109#define ASSERT_NO_FATAL_FAILURE(x) x
110
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600111//--------------------------------------------------------------------------------------
112// Mesh and VertexFormat Data
113//--------------------------------------------------------------------------------------
114struct Vertex
115{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600116 float posX, posY, posZ, posW; // Position data
117 float r, g, b, a; // Color
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600118};
119
120#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
121
122static const Vertex g_vbData[] =
123{
124 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
125 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
126 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
127 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
128 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
129 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
130
131 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
132 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
133 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
134 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
135 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
136 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
137
138 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
139 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
140 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
141 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
142 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
143 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
144
145 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
146 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
147 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
148 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
149 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
150 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
151
152 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
153 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
154 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
155 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
156 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
157 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
158
159 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
160 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
161 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
162 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
163 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
164 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
165};
166
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600167static const Vertex g_vb_solid_face_colors_Data[] =
168{
169 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
170 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
171 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
172 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
173 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
174 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
175
176 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
177 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
178 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
179 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
180 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
181 { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
182
183 { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
184 { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
185 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
186 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
187 { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
188 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
189
190 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
191 { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
192 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
193 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
194 { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
195 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
196
197 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
198 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
199 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
200 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
201 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
202 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
203
204 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
205 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
206 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
207 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
208 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
209 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
210};
211
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600212class XglRenderTest : public XglRenderFramework
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600213{
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600214public:
Cody Northrop4e6b4762014-10-09 21:25:22 -0600215
Tony Barbourf43b6982014-11-25 13:18:32 -0700216 void RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
Tony Barbourdd4c9642015-01-09 12:55:14 -0700217 XglConstantBufferObj *constantBuffer, XglCommandBufferObj *cmdBuffer);
Tony Barbour02472db2015-01-08 17:08:28 -0700218 void GenericDrawPreparation(XglCommandBufferObj *cmdBuffer, XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600219 void InitDepthStencil();
Tony Barbourae442072015-01-12 13:27:11 -0700220 void XGLTriangleTest(const char *vertShaderText, const char *fragShaderText, const bool rotate);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600221
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600222
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600223protected:
Cody Northrop350727b2014-10-06 15:42:00 -0600224 XGL_IMAGE m_texture;
225 XGL_IMAGE_VIEW m_textureView;
226 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
227 XGL_GPU_MEMORY m_textureMem;
228
229 XGL_SAMPLER m_sampler;
230
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600231 XGL_FORMAT m_depth_stencil_fmt;
232 XGL_IMAGE m_depthStencilImage;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600233 uint32_t m_num_mem;
Jon Ashburna9ae3832015-01-16 09:37:43 -0700234 XGL_GPU_MEMORY *m_depthStencilMem;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600235 XGL_DEPTH_STENCIL_VIEW m_depthStencilView;
Tony Barbourf43b6982014-11-25 13:18:32 -0700236 XglMemoryRefManager m_memoryRefManager;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600237
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600238
239 virtual void SetUp() {
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600240
241 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
242 this->app_info.pNext = NULL;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800243 this->app_info.pAppName = "render_tests";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600244 this->app_info.appVersion = 1;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800245 this->app_info.pEngineName = "unittest";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600246 this->app_info.engineVersion = 1;
Courtney Goeltzenleuchter211cc542015-02-23 17:40:15 -0700247 this->app_info.apiVersion = XGL_API_VERSION;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600248
Cody Northrop350727b2014-10-06 15:42:00 -0600249 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
250 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barbour97a36232014-12-04 17:14:45 -0700251 memset(&m_depthStencilImage, 0, sizeof(m_depthStencilImage));
Cody Northrop350727b2014-10-06 15:42:00 -0600252
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600253 InitFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600254 }
255
256 virtual void TearDown() {
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600257 // Clean up resources before we reset
258 ShutdownFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600259 }
260};
261
Tony Barbour02472db2015-01-08 17:08:28 -0700262void XglRenderTest::GenericDrawPreparation(XglCommandBufferObj *cmdBuffer, XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet)
263{
264 cmdBuffer->ClearAllBuffers(&m_depthStencilBinding, m_depthStencilImage);
Jeremy Hayese0c3b222015-01-14 16:17:08 -0700265 cmdBuffer->PrepareAttachments();
Tony Barbour02472db2015-01-08 17:08:28 -0700266 cmdBuffer->BindStateObject(XGL_STATE_BIND_RASTER, m_stateRaster);
267 cmdBuffer->BindStateObject(XGL_STATE_BIND_VIEWPORT, m_stateViewport);
268 cmdBuffer->BindStateObject(XGL_STATE_BIND_COLOR_BLEND, m_colorBlend);
269 cmdBuffer->BindStateObject(XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil);
Chia-I Wuf8385062015-01-04 16:27:24 +0800270 descriptorSet->CreateXGLDescriptorSet(cmdBuffer);
Tony Barbour02472db2015-01-08 17:08:28 -0700271 pipelineobj->CreateXGLPipeline(descriptorSet);
272 cmdBuffer->BindPipeline(pipelineobj->GetPipelineHandle());
Tony Barbour02472db2015-01-08 17:08:28 -0700273 cmdBuffer->BindDescriptorSet(descriptorSet->GetDescriptorSetHandle());
274}
Tony Barbourf43b6982014-11-25 13:18:32 -0700275
Tony Barbourf43b6982014-11-25 13:18:32 -0700276void XglRenderTest::RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
Tony Barbourdd4c9642015-01-09 12:55:14 -0700277 XglConstantBufferObj *constantBuffer, XglCommandBufferObj *cmdBuffer)
278{
279 int i;
280 glm::mat4 MVP;
281 int matrixSize = sizeof(MVP);
282 XGL_RESULT err;
283
284 for (i = 0; i < 8; i++) {
285 void *pData = constantBuffer->map();
286
287 Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
288 MVP = Projection * View * Model;
289 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
290
291 constantBuffer->unmap();
292
293 // submit the command buffer to the universal queue
294 cmdBuffer->QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
295
Tony Barbourdd4c9642015-01-09 12:55:14 -0700296 err = xglQueueWaitIdle( m_device->m_queue );
297 ASSERT_XGL_SUCCESS( err );
298
299 // Wait for work to finish before cleaning up.
300 xglDeviceWaitIdle(m_device->device());
301
302 assert(m_renderTargetCount == 1);
303 RecordImage(m_renderTargets[0]);
304 }
305}
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600306
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600307void dumpMatrix(const char *note, glm::mat4 MVP)
308{
Chia-I Wu7133fdc2014-12-15 23:57:34 +0800309 int i;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600310
311 printf("%s: \n", note);
312 for (i=0; i<4; i++) {
313 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
314 }
315 printf("\n");
316 fflush(stdout);
317}
318
319void dumpVec4(const char *note, glm::vec4 vector)
320{
321 printf("%s: \n", note);
322 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
323 printf("\n");
324 fflush(stdout);
325}
326
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600327void XglRenderTest::InitDepthStencil()
328{
329 XGL_RESULT err;
330 XGL_IMAGE_CREATE_INFO image;
331 XGL_MEMORY_ALLOC_INFO mem_alloc;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700332 XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600333 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
Jon Ashburna9ae3832015-01-16 09:37:43 -0700334 XGL_MEMORY_REQUIREMENTS *mem_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600335 size_t mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700336 XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600337 size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS);
338 uint32_t num_allocations = 0;
339 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600340
341 // Clean up default state created by framework
342 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
343
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700344 m_depth_stencil_fmt = XGL_FMT_D16_UNORM;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600345
346 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
347 image.pNext = NULL;
348 image.imageType = XGL_IMAGE_2D;
349 image.format = m_depth_stencil_fmt;
350 image.extent.width = m_width;
351 image.extent.height = m_height;
352 image.extent.depth = 1;
353 image.mipLevels = 1;
354 image.arraySize = 1;
355 image.samples = 1;
356 image.tiling = XGL_OPTIMAL_TILING;
357 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
358 image.flags = 0;
359
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700360 img_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO;
361 img_alloc.pNext = NULL;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600362 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700363 mem_alloc.pNext = &img_alloc;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600364 mem_alloc.allocationSize = 0;
Jon Ashburn542cd092015-01-20 13:55:32 -0700365 mem_alloc.memProps = XGL_MEMORY_PROPERTY_GPU_ONLY;
Jon Ashburn32769172015-01-20 15:06:59 -0700366 mem_alloc.memType = XGL_MEMORY_TYPE_IMAGE;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600367 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
368
369 /* create image */
370 err = xglCreateImage(device(), &image,
371 &m_depthStencilImage);
372 ASSERT_XGL_SUCCESS(err);
373
374 err = xglGetObjectInfo(m_depthStencilImage,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700375 XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
376 &num_alloc_size, &num_allocations);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600377 ASSERT_XGL_SUCCESS(err);
Jon Ashburna9ae3832015-01-16 09:37:43 -0700378 ASSERT_EQ(num_alloc_size, sizeof(num_allocations));
379 mem_reqs = (XGL_MEMORY_REQUIREMENTS *) malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS));
380 m_depthStencilMem = (XGL_GPU_MEMORY *) malloc(num_allocations * sizeof(XGL_GPU_MEMORY));
381 m_num_mem = num_allocations;
382 err = xglGetObjectInfo(m_depthStencilImage,
383 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
384 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600385 ASSERT_XGL_SUCCESS(err);
Jon Ashburna9ae3832015-01-16 09:37:43 -0700386 ASSERT_EQ(mem_reqs_size, sizeof(*mem_reqs));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700387 err = xglGetObjectInfo(m_depthStencilImage,
388 XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS,
389 &img_reqs_size, &img_reqs);
390 ASSERT_XGL_SUCCESS(err);
391 ASSERT_EQ(img_reqs_size, sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS));
392 img_alloc.usage = img_reqs.usage;
393 img_alloc.formatClass = img_reqs.formatClass;
394 img_alloc.samples = img_reqs.samples;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600395 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -0700396 mem_alloc.allocationSize = mem_reqs[i].size;
Jon Ashburna9ae3832015-01-16 09:37:43 -0700397
398 /* allocate memory */
399 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem[i]);
400 ASSERT_XGL_SUCCESS(err);
401
402 /* bind memory */
403 err = xglBindObjectMemory(m_depthStencilImage, i,
404 m_depthStencilMem[i], 0);
405 ASSERT_XGL_SUCCESS(err);
406 }
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600407
Tony Barbourfa6cac72015-01-16 14:27:35 -0700408 XGL_DYNAMIC_DS_STATE_CREATE_INFO depthStencil = {};
409 depthStencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
410
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600411 depthStencil.minDepth = 0.f;
412 depthStencil.maxDepth = 1.f;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700413 depthStencil.stencilBackRef = 0;
414 depthStencil.stencilFrontRef = 0;
415 depthStencil.stencilReadMask = 0xff;
416 depthStencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600417
Tony Barbourfa6cac72015-01-16 14:27:35 -0700418 err = xglCreateDynamicDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600419 ASSERT_XGL_SUCCESS( err );
420
421 /* create image view */
422 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
423 view.pNext = NULL;
424 view.image = XGL_NULL_HANDLE;
425 view.mipLevel = 0;
426 view.baseArraySlice = 0;
427 view.arraySize = 1;
428 view.flags = 0;
429 view.image = m_depthStencilImage;
430 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
431 ASSERT_XGL_SUCCESS(err);
432
433 m_depthStencilBinding.view = m_depthStencilView;
Mike Stroyan55658c22014-12-04 11:08:39 +0000434 m_depthStencilBinding.layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600435}
436
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600437struct xgltriangle_vs_uniform {
438 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600439 float mvp[4][4];
440 float position[3][4];
441 float color[3][4];
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600442};
443
Tony Barbourae442072015-01-12 13:27:11 -0700444void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText, const bool rotate)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600445{
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700446#ifdef DEBUG_CALLBACK
447 xglDbgRegisterMsgCallback(myDbgFunc, NULL);
448#endif
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600449 // Create identity matrix
450 int i;
451 struct xgltriangle_vs_uniform data;
452
453 glm::mat4 Projection = glm::mat4(1.0f);
454 glm::mat4 View = glm::mat4(1.0f);
455 glm::mat4 Model = glm::mat4(1.0f);
456 glm::mat4 MVP = Projection * View * Model;
457 const int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600458 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(float);
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600459 memcpy(&data.mvp, &MVP[0][0], matrixSize);
460
461 static const Vertex tri_data[] =
462 {
463 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
464 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
465 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
466 };
467
468 for (i=0; i<3; i++) {
469 data.position[i][0] = tri_data[i].posX;
470 data.position[i][1] = tri_data[i].posY;
471 data.position[i][2] = tri_data[i].posZ;
472 data.position[i][3] = tri_data[i].posW;
473 data.color[i][0] = tri_data[i].r;
474 data.color[i][1] = tri_data[i].g;
475 data.color[i][2] = tri_data[i].b;
476 data.color[i][3] = tri_data[i].a;
477 }
478
Tony Barbourf43b6982014-11-25 13:18:32 -0700479 ASSERT_NO_FATAL_FAILURE(InitState());
480 ASSERT_NO_FATAL_FAILURE(InitViewport());
481
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600482 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(float), (const void*) &data);
Tony Barbourf43b6982014-11-25 13:18:32 -0700483
484 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
485 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Tony Barbourf43b6982014-11-25 13:18:32 -0700486
487 XglPipelineObj pipelineobj(m_device);
488 pipelineobj.AddShader(&vs);
489 pipelineobj.AddShader(&ps);
490
491 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +0800492 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &constantBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -0700493 m_memoryRefManager.AddMemoryRef(&constantBuffer);
494
Tony Barbour71ba3612015-01-09 16:12:35 -0700495 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -0600496 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbour71ba3612015-01-09 16:12:35 -0700497 XglCommandBufferObj cmdBuffer(m_device);
498 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -0700499
Jeremy Hayese0c3b222015-01-14 16:17:08 -0700500 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbour71ba3612015-01-09 16:12:35 -0700501
502 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
Tony Barbour71ba3612015-01-09 16:12:35 -0700503#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600504 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbour71ba3612015-01-09 16:12:35 -0700505 pDSDumpDot((char*)"triTest2.dot");
506#endif
507 // render triangle
508 cmdBuffer.Draw(0, 3, 0, 1);
509
510 // finalize recording of the command buffer
511 cmdBuffer.EndCommandBuffer();
512 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
513
514 for (int i = 0; i < m_renderTargetCount; i++)
515 RecordImage(m_renderTargets[i]);
516
517 if (rotate)
Tobin Ehlis12ee35f2015-03-26 08:23:25 -0600518 RotateTriangleVSUniform(Projection, View, Model, &constantBuffer, &cmdBuffer);
Tony Barbour71ba3612015-01-09 16:12:35 -0700519
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700520#ifdef PRINT_OBJECTS
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600521 //uint64_t objTrackGetObjectCount(XGL_OBJECT_TYPE type)
522 OBJ_TRACK_GET_OBJECT_COUNT pObjTrackGetObjectCount = (OBJ_TRACK_GET_OBJECT_COUNT)xglGetProcAddr(gpu(), (char*)"objTrackGetObjectCount");
523 uint64_t numObjects = pObjTrackGetObjectCount(XGL_OBJECT_TYPE_ANY);
524 //OBJ_TRACK_GET_OBJECTS pGetObjsFunc = xglGetProcAddr(gpu(), (char*)"objTrackGetObjects");
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700525 printf("DEBUG : Number of Objects : %lu\n", numObjects);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600526 OBJ_TRACK_GET_OBJECTS pObjTrackGetObjs = (OBJ_TRACK_GET_OBJECTS)xglGetProcAddr(gpu(), (char*)"objTrackGetObjects");
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700527 OBJTRACK_NODE* pObjNodeArray = (OBJTRACK_NODE*)malloc(sizeof(OBJTRACK_NODE)*numObjects);
528 pObjTrackGetObjs(XGL_OBJECT_TYPE_ANY, numObjects, pObjNodeArray);
529 for (i=0; i < numObjects; i++) {
530 printf("Object %i of type %s has objID (%p) and %lu uses\n", i, string_XGL_OBJECT_TYPE(pObjNodeArray[i].objType), pObjNodeArray[i].pObj, pObjNodeArray[i].numUses);
531 }
532 free(pObjNodeArray);
533#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700534
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600535}
536
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600537TEST_F(XglRenderTest, XGLTriangle_FragColor)
538{
539 static const char *vertShaderText =
540 "#version 140\n"
541 "#extension GL_ARB_separate_shader_objects : enable\n"
542 "#extension GL_ARB_shading_language_420pack : enable\n"
543 "\n"
544 "layout(binding = 0) uniform buf {\n"
545 " mat4 MVP;\n"
546 " vec4 position[3];\n"
547 " vec4 color[3];\n"
548 "} ubuf;\n"
549 "\n"
550 "layout (location = 0) out vec4 outColor;\n"
551 "\n"
552 "void main() \n"
553 "{\n"
554 " outColor = ubuf.color[gl_VertexID];\n"
555 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
556 "}\n";
557
558 static const char *fragShaderText =
559 "#version 140\n"
560 "#extension GL_ARB_separate_shader_objects : enable\n"
561 "#extension GL_ARB_shading_language_420pack : enable\n"
562 "\n"
563 "layout (location = 0) in vec4 inColor;\n"
564 "\n"
565 "void main()\n"
566 "{\n"
567 " gl_FragColor = inColor;\n"
568 "}\n";
569
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600570 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
Tony Barbourae442072015-01-12 13:27:11 -0700571 XGLTriangleTest(vertShaderText, fragShaderText, true);
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600572}
573
574TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
575{
576 static const char *vertShaderText =
577 "#version 140\n"
578 "#extension GL_ARB_separate_shader_objects : enable\n"
579 "#extension GL_ARB_shading_language_420pack : enable\n"
580 "\n"
581 "layout(binding = 0) uniform buf {\n"
582 " mat4 MVP;\n"
583 " vec4 position[3];\n"
584 " vec4 color[3];\n"
585 "} ubuf;\n"
586 "\n"
587 "layout (location = 0) out vec4 outColor;\n"
588 "\n"
589 "void main() \n"
590 "{\n"
591 " outColor = ubuf.color[gl_VertexID];\n"
592 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
593 "}\n";
594
595 static const char *fragShaderText =
596 "#version 140\n"
597 "#extension GL_ARB_separate_shader_objects : enable\n"
598 "#extension GL_ARB_shading_language_420pack : enable\n"
599 "\n"
600 "layout (location = 0) in vec4 inColor;\n"
601 "layout (location = 0) out vec4 outColor;\n"
602 "\n"
603 "void main()\n"
604 "{\n"
605 " outColor = inColor;\n"
606 "}\n";
607
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600608 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to output location 0, which should be the same as gl_FragColor");
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600609
Tony Barbourae442072015-01-12 13:27:11 -0700610 XGLTriangleTest(vertShaderText, fragShaderText, true);
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600611}
612
Cody Northropacfb0492015-03-17 15:55:58 -0600613TEST_F(XglRenderTest, SPV_XGLTriangle)
Tony Barbourf43b6982014-11-25 13:18:32 -0700614{
Cody Northropacfb0492015-03-17 15:55:58 -0600615 bool saved_use_spv = XglTestFramework::m_use_spv;
Tony Barbourf43b6982014-11-25 13:18:32 -0700616
617 static const char *vertShaderText =
618 "#version 140\n"
619 "#extension GL_ARB_separate_shader_objects : enable\n"
620 "#extension GL_ARB_shading_language_420pack : enable\n"
621 "\n"
622 "layout(binding = 0) uniform buf {\n"
623 " mat4 MVP;\n"
624 " vec4 position[3];\n"
625 " vec4 color[3];\n"
626 "} ubuf;\n"
627 "\n"
628 "layout (location = 0) out vec4 outColor;\n"
629 "\n"
630 "void main() \n"
631 "{\n"
632 " outColor = ubuf.color[gl_VertexID];\n"
633 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
634 "}\n";
635
636 static const char *fragShaderText =
637 "#version 140\n"
638 "#extension GL_ARB_separate_shader_objects : enable\n"
639 "#extension GL_ARB_shading_language_420pack : enable\n"
640 "\n"
641 "layout (location = 0) in vec4 inColor;\n"
642 "\n"
643 "void main()\n"
644 "{\n"
645 " gl_FragColor = inColor;\n"
646 "}\n";
647
Cody Northropacfb0492015-03-17 15:55:58 -0600648 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to SPV and pass SPV to driver.");
Tony Barbourf43b6982014-11-25 13:18:32 -0700649
Cody Northropacfb0492015-03-17 15:55:58 -0600650 XglTestFramework::m_use_spv = true;
Tony Barbourf43b6982014-11-25 13:18:32 -0700651
Tony Barbourae442072015-01-12 13:27:11 -0700652 XGLTriangleTest(vertShaderText, fragShaderText, true);
Tony Barbourf43b6982014-11-25 13:18:32 -0700653
Cody Northropacfb0492015-03-17 15:55:58 -0600654 XglTestFramework::m_use_spv = saved_use_spv;
Tony Barbourf43b6982014-11-25 13:18:32 -0700655}
656
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600657TEST_F(XglRenderTest, GreenTriangle)
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600658{
659 static const char *vertShaderText =
660 "#version 130\n"
661 "vec2 vertices[3];\n"
662 "void main() {\n"
663 " vertices[0] = vec2(-1.0, -1.0);\n"
664 " vertices[1] = vec2( 1.0, -1.0);\n"
665 " vertices[2] = vec2( 0.0, 1.0);\n"
666 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
667 "}\n";
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600668
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600669 static const char *fragShaderText =
670 "#version 130\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600671 "void main() {\n"
Steve K10b32512014-10-10 08:54:29 -0600672 " gl_FragColor = vec4(0,1,0,1);\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600673 "}\n";
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600674
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600675 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
676
Tony Barbourae442072015-01-12 13:27:11 -0700677 XGLTriangleTest(vertShaderText, fragShaderText, false);
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600678}
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600679
Cody Northropacfb0492015-03-17 15:55:58 -0600680TEST_F(XglRenderTest, SPV_GreenTriangle)
Tony Barbourf43b6982014-11-25 13:18:32 -0700681{
Cody Northropacfb0492015-03-17 15:55:58 -0600682 bool saved_use_spv = XglTestFramework::m_use_spv;
Tony Barbourf43b6982014-11-25 13:18:32 -0700683
684 static const char *vertShaderText =
685 "#version 130\n"
686 "vec2 vertices[3];\n"
687 "void main() {\n"
688 " vertices[0] = vec2(-1.0, -1.0);\n"
689 " vertices[1] = vec2( 1.0, -1.0);\n"
690 " vertices[2] = vec2( 0.0, 1.0);\n"
691 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
692 "}\n";
693
694 static const char *fragShaderText =
695 "#version 130\n"
696 "void main() {\n"
697 " gl_FragColor = vec4(0,1,0,1);\n"
698 "}\n";
699
Cody Northropacfb0492015-03-17 15:55:58 -0600700 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to SPV and gives SPV to driver.");
Tony Barbourf43b6982014-11-25 13:18:32 -0700701
Cody Northropacfb0492015-03-17 15:55:58 -0600702 XglTestFramework::m_use_spv = true;
Tony Barbourae442072015-01-12 13:27:11 -0700703 XGLTriangleTest(vertShaderText, fragShaderText, false);
Cody Northropacfb0492015-03-17 15:55:58 -0600704 XglTestFramework::m_use_spv = saved_use_spv;
Tony Barbourf43b6982014-11-25 13:18:32 -0700705}
706
707TEST_F(XglRenderTest, YellowTriangle)
708{
709 static const char *vertShaderText =
710 "#version 130\n"
711 "void main() {\n"
712 " vec2 vertices[3];"
713 " vertices[0] = vec2(-0.5, -0.5);\n"
714 " vertices[1] = vec2( 0.5, -0.5);\n"
715 " vertices[2] = vec2( 0.5, 0.5);\n"
716 " vec4 colors[3];\n"
717 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
718 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
719 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
720 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
721 "}\n";
722
723 static const char *fragShaderText =
724 "#version 130\n"
725 "void main() {\n"
726 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
727 "}\n";
728
Tony Barbourae442072015-01-12 13:27:11 -0700729 XGLTriangleTest(vertShaderText, fragShaderText, false);
Tony Barbourf43b6982014-11-25 13:18:32 -0700730}
731
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600732TEST_F(XglRenderTest, TriangleWithVertexFetch)
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600733{
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600734 static const char *vertShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700735 "#version 130\n"
736 //XYZ1( -1, -1, -1 )
737 "in vec4 pos;\n"
738 //XYZ1( 0.f, 0.f, 0.f )
739 "in vec4 inColor;\n"
740 "out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600741 "void main() {\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -0600742 " outColor = inColor;\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600743 " gl_Position = pos;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600744 "}\n";
745
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600746
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600747 static const char *fragShaderText =
Cody Northrop68a10f62014-12-05 15:44:14 -0700748 "#version 140\n"
749 "#extension GL_ARB_separate_shader_objects : enable\n"
750 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -0700751 "in vec4 color;\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700752 "layout (location = 0) out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600753 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700754 " outColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600755 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600756
Tony Barbourf43b6982014-11-25 13:18:32 -0700757
758
759 ASSERT_NO_FATAL_FAILURE(InitState());
760 ASSERT_NO_FATAL_FAILURE(InitViewport());
761
762 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Mike Stroyan55658c22014-12-04 11:08:39 +0000763 meshBuffer.BufferMemoryBarrier();
Tony Barbourf43b6982014-11-25 13:18:32 -0700764
765 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
766 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
767
768 XglPipelineObj pipelineobj(m_device);
769 pipelineobj.AddShader(&vs);
770 pipelineobj.AddShader(&ps);
771
772 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +0800773 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &meshBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -0700774
Mark Lobodzinski15427102015-02-18 16:38:17 -0600775 m_memoryRefManager.AddMemoryRef(&meshBuffer);
776
Tony Barbourf43b6982014-11-25 13:18:32 -0700777 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
778 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
779 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
780 };
781
782 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
783 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700784 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -0700785 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
786 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700787 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -0700788 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
789
790 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
791 pipelineobj.AddVertexInputBindings(&vi_binding,1);
792 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
793
Tony Barboure4ed9942015-01-09 10:06:53 -0700794 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -0600795 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barboure4ed9942015-01-09 10:06:53 -0700796 XglCommandBufferObj cmdBuffer(m_device);
797 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -0700798
Jeremy Hayese0c3b222015-01-14 16:17:08 -0700799 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barboure4ed9942015-01-09 10:06:53 -0700800 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
801
Tony Barboure4ed9942015-01-09 10:06:53 -0700802 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
803
804 // render two triangles
805 cmdBuffer.Draw(0, 6, 0, 1);
806
807 // finalize recording of the command buffer
808 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -0600809 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barboure4ed9942015-01-09 10:06:53 -0700810
811 for (int i = 0; i < m_renderTargetCount; i++)
812 RecordImage(m_renderTargets[i]);
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600813}
814
Chia-I Wue09d1a72014-12-05 10:32:23 +0800815TEST_F(XglRenderTest, TriangleMRT)
816{
817 static const char *vertShaderText =
818 "#version 130\n"
819 "in vec4 pos;\n"
820 "void main() {\n"
821 " gl_Position = pos;\n"
822 "}\n";
823
824 static const char *fragShaderText =
825 "#version 130\n"
826 "void main() {\n"
827 " gl_FragData[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
828 " gl_FragData[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
829 "}\n";
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600830 const float vb_data[][2] = {
Chia-I Wue09d1a72014-12-05 10:32:23 +0800831 { -1.0f, -1.0f },
832 { 1.0f, -1.0f },
833 { -1.0f, 1.0f }
834 };
835
836 ASSERT_NO_FATAL_FAILURE(InitState());
837 ASSERT_NO_FATAL_FAILURE(InitViewport());
838
839 XglConstantBufferObj meshBuffer(m_device, sizeof(vb_data) / sizeof(vb_data[0]), sizeof(vb_data[0]), vb_data);
Mike Stroyan55658c22014-12-04 11:08:39 +0000840 meshBuffer.BufferMemoryBarrier();
Chia-I Wue09d1a72014-12-05 10:32:23 +0800841
842 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
843 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
844
845 XglPipelineObj pipelineobj(m_device);
846 pipelineobj.AddShader(&vs);
847 pipelineobj.AddShader(&ps);
848
849 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
850 sizeof(vb_data[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
851 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
852 };
853
854 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attrib;
855 vi_attrib.binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700856 vi_attrib.format = XGL_FMT_R32G32_SFLOAT; // format of source data
Chia-I Wue09d1a72014-12-05 10:32:23 +0800857 vi_attrib.offsetInBytes = 0; // Offset of first element in bytes from base of vertex
858
859 pipelineobj.AddVertexInputAttribs(&vi_attrib, 1);
860 pipelineobj.AddVertexInputBindings(&vi_binding,1);
861 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
862
863 XglDescriptorSetObj descriptorSet(m_device);
864
865 m_renderTargetCount = 2;
Tony Barboure4ed9942015-01-09 10:06:53 -0700866 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -0600867 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
868 m_memoryRefManager.AddMemoryRef(&meshBuffer);
Chia-I Wue09d1a72014-12-05 10:32:23 +0800869
870 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
871 att.blendEnable = XGL_FALSE;
872 att.format = m_render_target_fmt;
873 att.channelWriteMask = 0xf;
Tony Barbourfa6cac72015-01-16 14:27:35 -0700874 pipelineobj.AddColorAttachment(1, &att);
Chia-I Wue09d1a72014-12-05 10:32:23 +0800875
Tony Barbour5ed79702015-01-07 14:31:52 -0700876 XglCommandBufferObj cmdBuffer(m_device);
Tony Barboure4ed9942015-01-09 10:06:53 -0700877
Tony Barbour5ed79702015-01-07 14:31:52 -0700878 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
879 cmdBuffer.AddRenderTarget(m_renderTargets[1]);
Tony Barboure4ed9942015-01-09 10:06:53 -0700880
Jeremy Hayese0c3b222015-01-14 16:17:08 -0700881 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbour5ed79702015-01-07 14:31:52 -0700882
Tony Barboure4ed9942015-01-09 10:06:53 -0700883 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
884
Tony Barbour5ed79702015-01-07 14:31:52 -0700885 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
Tony Barbour5ed79702015-01-07 14:31:52 -0700886#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600887 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbour5ed79702015-01-07 14:31:52 -0700888 pDSDumpDot((char*)"triTest2.dot");
889#endif
890 // render triangle
891 cmdBuffer.Draw(0, 3, 0, 1);
892
893 // finalize recording of the command buffer
894 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -0600895 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbour5ed79702015-01-07 14:31:52 -0700896
897 for (int i = 0; i < m_renderTargetCount; i++)
898 RecordImage(m_renderTargets[i]);
899
Chia-I Wue09d1a72014-12-05 10:32:23 +0800900}
901
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700902TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
903{
904 static const char *vertShaderText =
905 "#version 140\n"
906 "#extension GL_ARB_separate_shader_objects : enable\n"
907 "#extension GL_ARB_shading_language_420pack : enable\n"
908 "layout(location = 0) in vec4 pos;\n"
909 "layout(location = 1) in vec4 inColor;\n"
910 "layout(location = 0) out vec4 outColor;\n"
911 "void main() {\n"
912 " outColor = inColor;\n"
913 " gl_Position = pos;\n"
914 "}\n";
915
916
917 static const char *fragShaderText =
918 "#version 140\n"
919 "#extension GL_ARB_separate_shader_objects : enable\n"
920 "#extension GL_ARB_shading_language_420pack : enable\n"
921 "layout(location = 0) in vec4 color;\n"
922 "void main() {\n"
923 " gl_FragColor = color;\n"
924 "}\n";
925
926 const Vertex g_vbData[] =
927 {
928 // first tri
929 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
930 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
931 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
932
933 // second tri
934 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
935 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
936 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
937 };
938
939 const uint16_t g_idxData[6] = {
940 0, 1, 2,
941 3, 4, 5,
942 };
943
944 ASSERT_NO_FATAL_FAILURE(InitState());
945 ASSERT_NO_FATAL_FAILURE(InitViewport());
946
947 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Mike Stroyan55658c22014-12-04 11:08:39 +0000948 meshBuffer.BufferMemoryBarrier();
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700949
950 XglIndexBufferObj indexBuffer(m_device);
951 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
Mark Lobodzinskid30f82a2015-02-16 14:24:23 -0600952 indexBuffer.BufferMemoryBarrier();
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700953
954 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
955 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
956
957 XglPipelineObj pipelineobj(m_device);
958 pipelineobj.AddShader(&vs);
959 pipelineobj.AddShader(&ps);
960
961 XglDescriptorSetObj descriptorSet(m_device);
962
Mark Lobodzinski15427102015-02-18 16:38:17 -0600963 m_memoryRefManager.AddMemoryRef(&meshBuffer);
964 m_memoryRefManager.AddMemoryRef(&indexBuffer);
965
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700966 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
967 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
968 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
969 };
970
971 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
972 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700973 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700974 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
975 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -0700976 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700977 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
978
979 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
980 pipelineobj.AddVertexInputBindings(&vi_binding,1);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700981
982 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -0600983 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourde9cf2e2014-12-17 11:16:05 -0700984 XglCommandBufferObj cmdBuffer(m_device);
985 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Jeremy Hayese0c3b222015-01-14 16:17:08 -0700986 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barboure4ed9942015-01-09 10:06:53 -0700987
988 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700989
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700990#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600991 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tobin Ehlis266473d2014-12-16 17:34:50 -0700992 pDSDumpDot((char*)"triTest2.dot");
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700993#endif
Tony Barbour02472db2015-01-08 17:08:28 -0700994
Tony Barbourde9cf2e2014-12-17 11:16:05 -0700995 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
996 cmdBuffer.BindIndexBuffer(&indexBuffer,0);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700997
998 // render two triangles
Tony Barbourde9cf2e2014-12-17 11:16:05 -0700999 cmdBuffer.DrawIndexed(0, 6, 0, 0, 1);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001000
1001 // finalize recording of the command buffer
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001002 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001003 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001004
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001005 for (int i = 0; i < m_renderTargetCount; i++)
1006 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001007
1008}
1009
GregF6bef1212014-12-02 15:41:44 -07001010TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1011{
1012 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001013
GregF6bef1212014-12-02 15:41:44 -07001014 static const char *vertShaderText =
1015 "#version 140\n"
1016 "#extension GL_ARB_separate_shader_objects : enable\n"
1017 "#extension GL_ARB_shading_language_420pack : enable\n"
1018 "layout (location = 0) in vec4 pos;\n"
1019 "layout (location = 0) out vec4 outColor;\n"
1020 "layout (location = 1) out vec4 outColor2;\n"
1021 "void main() {\n"
1022 " gl_Position = pos;\n"
1023 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1024 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1025 "}\n";
1026
1027 static const char *fragShaderText =
1028 //"#version 140\n"
1029 "#version 330\n"
1030 "#extension GL_ARB_separate_shader_objects : enable\n"
1031 "#extension GL_ARB_shading_language_420pack : enable\n"
1032 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1033 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1034 "layout (location = 0) in vec4 color;\n"
1035 "layout (location = 1) in vec4 color2;\n"
1036 "void main() {\n"
1037 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1038 " float dist_squared = dot(pos, pos);\n"
1039 " gl_FragColor = (dist_squared < 400.0)\n"
1040 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1041 " : color2;\n"
1042 "}\n";
1043
1044 ASSERT_NO_FATAL_FAILURE(InitState());
1045 ASSERT_NO_FATAL_FAILURE(InitViewport());
1046
1047 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Mike Stroyan55658c22014-12-04 11:08:39 +00001048 meshBuffer.BufferMemoryBarrier();
GregF6bef1212014-12-02 15:41:44 -07001049
1050 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1051 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1052
1053 XglPipelineObj pipelineobj(m_device);
1054 pipelineobj.AddShader(&vs);
1055 pipelineobj.AddShader(&ps);
1056
1057 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001058 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &meshBuffer);
GregF6bef1212014-12-02 15:41:44 -07001059
Mark Lobodzinski15427102015-02-18 16:38:17 -06001060 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1061
GregF6bef1212014-12-02 15:41:44 -07001062 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1063 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1064 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1065 };
1066
1067 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1068 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001069 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
GregF6bef1212014-12-02 15:41:44 -07001070 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1071 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001072 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
GregF6bef1212014-12-02 15:41:44 -07001073 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1074
1075 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1076 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1077 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1078
Tony Barbourdd4c9642015-01-09 12:55:14 -07001079 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001080 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001081 XglCommandBufferObj cmdBuffer(m_device);
1082 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1083
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001084 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001085
1086 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1087
1088 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1089#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001090 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001091 pDSDumpDot((char*)"triTest2.dot");
1092#endif
1093 // render triangle
1094 cmdBuffer.Draw(0, 6, 0, 1);
1095
1096 // finalize recording of the command buffer
1097 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001098 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07001099
1100 for (int i = 0; i < m_renderTargetCount; i++)
1101 RecordImage(m_renderTargets[i]);
GregF6bef1212014-12-02 15:41:44 -07001102
1103}
1104
1105TEST_F(XglRenderTest, RedCirclesonBlue)
1106{
1107 // This tests that we correctly handle unread fragment inputs
1108
1109 static const char *vertShaderText =
1110 "#version 140\n"
1111 "#extension GL_ARB_separate_shader_objects : enable\n"
1112 "#extension GL_ARB_shading_language_420pack : enable\n"
1113 "layout (location = 0) in vec4 pos;\n"
1114 "layout (location = 0) out vec4 outColor;\n"
1115 "layout (location = 1) out vec4 outColor2;\n"
1116 "void main() {\n"
1117 " gl_Position = pos;\n"
1118 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1119 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1120 "}\n";
1121
1122 static const char *fragShaderText =
1123 //"#version 140\n"
1124 "#version 330\n"
1125 "#extension GL_ARB_separate_shader_objects : enable\n"
1126 "#extension GL_ARB_shading_language_420pack : enable\n"
1127 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1128 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1129 "layout (location = 0) in vec4 color;\n"
1130 "layout (location = 1) in vec4 color2;\n"
1131 "void main() {\n"
1132 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1133 " float dist_squared = dot(pos, pos);\n"
1134 " gl_FragColor = (dist_squared < 400.0)\n"
1135 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1136 " : color2;\n"
1137 "}\n";
1138
1139 ASSERT_NO_FATAL_FAILURE(InitState());
1140 ASSERT_NO_FATAL_FAILURE(InitViewport());
1141
1142 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Mike Stroyan55658c22014-12-04 11:08:39 +00001143 meshBuffer.BufferMemoryBarrier();
GregF6bef1212014-12-02 15:41:44 -07001144
1145 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1146 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1147
1148 XglPipelineObj pipelineobj(m_device);
1149 pipelineobj.AddShader(&vs);
1150 pipelineobj.AddShader(&ps);
1151
1152 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001153 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &meshBuffer);
GregF6bef1212014-12-02 15:41:44 -07001154
Mark Lobodzinski15427102015-02-18 16:38:17 -06001155 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1156
GregF6bef1212014-12-02 15:41:44 -07001157 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1158 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1159 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1160 };
1161
1162 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1163 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001164 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
GregF6bef1212014-12-02 15:41:44 -07001165 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1166 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001167 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
GregF6bef1212014-12-02 15:41:44 -07001168 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1169
1170 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1171 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1172 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1173
Tony Barbourdd4c9642015-01-09 12:55:14 -07001174 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001175 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001176 XglCommandBufferObj cmdBuffer(m_device);
1177 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1178
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001179 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001180
1181 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1182
1183 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1184#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001185 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001186 pDSDumpDot((char*)"triTest2.dot");
1187#endif
1188 // render triangle
1189 cmdBuffer.Draw(0, 6, 0, 1);
1190
1191 // finalize recording of the command buffer
1192 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001193 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07001194
1195 for (int i = 0; i < m_renderTargetCount; i++)
1196 RecordImage(m_renderTargets[i]);
GregF6bef1212014-12-02 15:41:44 -07001197
1198}
1199
1200TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1201{
1202 // This tests reading gl_ClipDistance from FS
1203
1204 static const char *vertShaderText =
1205 "#version 330\n"
1206 "#extension GL_ARB_separate_shader_objects : enable\n"
1207 "#extension GL_ARB_shading_language_420pack : enable\n"
1208 "out gl_PerVertex {\n"
1209 " vec4 gl_Position;\n"
1210 " float gl_ClipDistance[1];\n"
1211 "};\n"
1212 "layout (location = 0) in vec4 pos;\n"
1213 "layout (location = 0) out vec4 outColor;\n"
1214 "layout (location = 1) out vec4 outColor2;\n"
1215 "void main() {\n"
1216 " gl_Position = pos;\n"
1217 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1218 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1219 " float dists[3];\n"
1220 " dists[0] = 0.0;\n"
1221 " dists[1] = 1.0;\n"
1222 " dists[2] = 1.0;\n"
1223 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1224 "}\n";
1225
1226
1227 static const char *fragShaderText =
1228 //"#version 140\n"
1229 "#version 330\n"
1230 "#extension GL_ARB_separate_shader_objects : enable\n"
1231 "#extension GL_ARB_shading_language_420pack : enable\n"
1232 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1233 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1234 "layout (location = 0) in vec4 color;\n"
1235 "layout (location = 1) in vec4 color2;\n"
1236 "void main() {\n"
1237 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1238 " float dist_squared = dot(pos, pos);\n"
1239 " gl_FragColor = (dist_squared < 400.0)\n"
1240 " ? color * gl_ClipDistance[0]\n"
1241 " : color2;\n"
1242 "}\n";
1243
1244 ASSERT_NO_FATAL_FAILURE(InitState());
1245 ASSERT_NO_FATAL_FAILURE(InitViewport());
1246
1247 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Mike Stroyan55658c22014-12-04 11:08:39 +00001248 meshBuffer.BufferMemoryBarrier();
GregF6bef1212014-12-02 15:41:44 -07001249
1250 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1251 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1252
1253 XglPipelineObj pipelineobj(m_device);
1254 pipelineobj.AddShader(&vs);
1255 pipelineobj.AddShader(&ps);
1256
1257 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001258 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &meshBuffer);
GregF6bef1212014-12-02 15:41:44 -07001259
Mark Lobodzinski15427102015-02-18 16:38:17 -06001260 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1261
GregF6bef1212014-12-02 15:41:44 -07001262 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1263 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1264 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1265 };
1266
1267 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1268 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001269 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
GregF6bef1212014-12-02 15:41:44 -07001270 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1271 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001272 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
GregF6bef1212014-12-02 15:41:44 -07001273 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1274
1275 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1276 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1277 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1278
Tony Barbourdd4c9642015-01-09 12:55:14 -07001279 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001280 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001281 XglCommandBufferObj cmdBuffer(m_device);
1282 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
GregF6bef1212014-12-02 15:41:44 -07001283
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001284 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001285
1286 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1287
1288 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1289#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001290 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001291 pDSDumpDot((char*)"triTest2.dot");
1292#endif
1293 // render triangle
1294 cmdBuffer.Draw(0, 6, 0, 1);
1295
1296 // finalize recording of the command buffer
1297 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001298 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07001299
1300 for (int i = 0; i < m_renderTargetCount; i++)
1301 RecordImage(m_renderTargets[i]);
GregF6bef1212014-12-02 15:41:44 -07001302}
Tony Barbourf43b6982014-11-25 13:18:32 -07001303
GregF7a23c792014-12-02 17:19:34 -07001304TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1305{
1306 static const char *vertShaderText =
1307 "#version 140\n"
1308 "#extension GL_ARB_separate_shader_objects : enable\n"
1309 "#extension GL_ARB_shading_language_420pack : enable\n"
1310 "layout (location = 0) in vec4 pos;\n"
1311 "layout (location = 0) out vec4 outColor;\n"
1312 "layout (location = 1) out vec4 outColor2;\n"
1313 "void main() {\n"
1314 " gl_Position = pos;\n"
1315 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1316 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1317 "}\n";
1318
1319
1320 static const char *fragShaderText =
1321 //"#version 140\n"
1322 "#version 330\n"
1323 "#extension GL_ARB_separate_shader_objects : enable\n"
1324 "#extension GL_ARB_shading_language_420pack : enable\n"
1325 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1326 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1327 "layout (location = 0) in vec4 color;\n"
1328 "layout (location = 1) in vec4 color2;\n"
1329 "void main() {\n"
1330 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1331 " float dist_squared = dot(pos, pos);\n"
1332 " if (dist_squared < 100.0)\n"
1333 " discard;\n"
1334 " gl_FragColor = (dist_squared < 400.0)\n"
1335 " ? color\n"
1336 " : color2;\n"
1337 "}\n";
1338
1339 ASSERT_NO_FATAL_FAILURE(InitState());
1340 ASSERT_NO_FATAL_FAILURE(InitViewport());
1341
1342 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Mike Stroyan55658c22014-12-04 11:08:39 +00001343 meshBuffer.BufferMemoryBarrier();
GregF7a23c792014-12-02 17:19:34 -07001344
1345 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1346 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1347
1348 XglPipelineObj pipelineobj(m_device);
1349 pipelineobj.AddShader(&vs);
1350 pipelineobj.AddShader(&ps);
1351
1352 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001353 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &meshBuffer);
GregF7a23c792014-12-02 17:19:34 -07001354
Mark Lobodzinski15427102015-02-18 16:38:17 -06001355 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1356
GregF7a23c792014-12-02 17:19:34 -07001357 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1358 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1359 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1360 };
1361
1362 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1363 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001364 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
GregF7a23c792014-12-02 17:19:34 -07001365 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1366 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001367 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
GregF7a23c792014-12-02 17:19:34 -07001368 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1369
1370 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1371 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1372 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1373
Tony Barbourdd4c9642015-01-09 12:55:14 -07001374 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001375 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001376 XglCommandBufferObj cmdBuffer(m_device);
1377 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1378
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001379 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001380
1381 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1382
1383 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1384#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001385 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001386 pDSDumpDot((char*)"triTest2.dot");
1387#endif
1388 // render triangle
1389 cmdBuffer.Draw(0, 6, 0, 1);
1390
1391 // finalize recording of the command buffer
1392 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001393 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07001394
1395 for (int i = 0; i < m_renderTargetCount; i++)
1396 RecordImage(m_renderTargets[i]);
GregF7a23c792014-12-02 17:19:34 -07001397
1398}
1399
1400
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001401TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001402{
1403 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001404 "#version 140\n"
1405 "#extension GL_ARB_separate_shader_objects : enable\n"
1406 "#extension GL_ARB_shading_language_420pack : enable\n"
1407 "\n"
1408 "layout(binding = 0) uniform buf {\n"
1409 " mat4 MVP;\n"
1410 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001411 "void main() {\n"
1412 " vec2 vertices[3];"
1413 " vertices[0] = vec2(-0.5, -0.5);\n"
1414 " vertices[1] = vec2( 0.5, -0.5);\n"
1415 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001416 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001417 "}\n";
1418
1419 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001420 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001421 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001422 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001423 "}\n";
1424
Tony Barbourf43b6982014-11-25 13:18:32 -07001425 ASSERT_NO_FATAL_FAILURE(InitState());
1426 ASSERT_NO_FATAL_FAILURE(InitViewport());
1427
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001428 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001429 glm::mat4 Projection = glm::mat4(1.0f);
1430 glm::mat4 View = glm::mat4(1.0f);
1431 glm::mat4 Model = glm::mat4(1.0f);
1432 glm::mat4 MVP = Projection * View * Model;
1433 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1434
Tony Barbourf43b6982014-11-25 13:18:32 -07001435 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1436 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1437 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001438
Tony Barbourf43b6982014-11-25 13:18:32 -07001439 XglPipelineObj pipelineobj(m_device);
1440 pipelineobj.AddShader(&vs);
1441 pipelineobj.AddShader(&ps);
1442
1443 // Create descriptor set and attach the constant buffer to it
1444 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001445 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &MVPBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07001446
1447 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1448
Tony Barbourdd4c9642015-01-09 12:55:14 -07001449 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001450 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001451 XglCommandBufferObj cmdBuffer(m_device);
1452 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001453
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001454 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001455
1456 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1457
1458 // cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1459#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001460 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001461 pDSDumpDot((char*)"triTest2.dot");
1462#endif
1463 // render triangle
1464 cmdBuffer.Draw(0, 6, 0, 1);
1465
1466 // finalize recording of the command buffer
1467 cmdBuffer.EndCommandBuffer();
1468 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1469
1470 for (int i = 0; i < m_renderTargetCount; i++)
1471 RecordImage(m_renderTargets[i]);
1472
1473 RotateTriangleVSUniform(Projection, View, Model, &MVPBuffer, &cmdBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001474}
1475
Tony Barbourf43b6982014-11-25 13:18:32 -07001476TEST_F(XglRenderTest, MixTriangle)
1477{
1478 // This tests location applied to varyings. Notice that we have switched foo
1479 // and bar in the FS. The triangle should be blended with red, green and blue
1480 // corners.
1481 static const char *vertShaderText =
1482 "#version 140\n"
1483 "#extension GL_ARB_separate_shader_objects : enable\n"
1484 "#extension GL_ARB_shading_language_420pack : enable\n"
1485 "layout (location=0) out vec4 bar;\n"
1486 "layout (location=1) out vec4 foo;\n"
1487 "layout (location=2) out float scale;\n"
1488 "vec2 vertices[3];\n"
1489 "void main() {\n"
1490 " vertices[0] = vec2(-1.0, -1.0);\n"
1491 " vertices[1] = vec2( 1.0, -1.0);\n"
1492 " vertices[2] = vec2( 0.0, 1.0);\n"
1493 "vec4 colors[3];\n"
1494 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1495 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1496 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1497 " foo = colors[gl_VertexID % 3];\n"
1498 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1499 " scale = 1.0;\n"
1500 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1501 "}\n";
1502
1503 static const char *fragShaderText =
1504 "#version 140\n"
1505 "#extension GL_ARB_separate_shader_objects : enable\n"
1506 "#extension GL_ARB_shading_language_420pack : enable\n"
1507 "layout (location = 1) in vec4 bar;\n"
1508 "layout (location = 0) in vec4 foo;\n"
1509 "layout (location = 2) in float scale;\n"
1510 "void main() {\n"
1511 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1512 "}\n";
1513
1514 ASSERT_NO_FATAL_FAILURE(InitState());
1515 ASSERT_NO_FATAL_FAILURE(InitViewport());
1516
1517 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1518 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1519
1520 XglPipelineObj pipelineobj(m_device);
1521 pipelineobj.AddShader(&vs);
1522 pipelineobj.AddShader(&ps);
1523
1524 XglDescriptorSetObj descriptorSet(m_device);
1525
Tony Barbourdd4c9642015-01-09 12:55:14 -07001526 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001527 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001528 XglCommandBufferObj cmdBuffer(m_device);
1529 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1530
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001531 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001532
1533 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1534
1535#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001536 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001537 pDSDumpDot((char*)"triTest2.dot");
1538#endif
1539 // render triangle
1540 cmdBuffer.Draw(0, 3, 0, 1);
1541
1542 // finalize recording of the command buffer
1543 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001544 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07001545
1546 for (int i = 0; i < m_renderTargetCount; i++)
1547 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001548}
1549
1550TEST_F(XglRenderTest, TriVertFetchAndVertID)
1551{
1552 // This tests that attributes work in the presence of gl_VertexID
1553
1554 static const char *vertShaderText =
1555 "#version 140\n"
1556 "#extension GL_ARB_separate_shader_objects : enable\n"
1557 "#extension GL_ARB_shading_language_420pack : enable\n"
1558 //XYZ1( -1, -1, -1 )
1559 "layout (location = 0) in vec4 pos;\n"
1560 //XYZ1( 0.f, 0.f, 0.f )
1561 "layout (location = 1) in vec4 inColor;\n"
1562 "layout (location = 0) out vec4 outColor;\n"
1563 "void main() {\n"
1564 " outColor = inColor;\n"
1565 " vec4 vertices[3];"
1566 " vertices[gl_VertexID % 3] = pos;\n"
1567 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1568 "}\n";
1569
1570
1571 static const char *fragShaderText =
1572 "#version 140\n"
1573 "#extension GL_ARB_separate_shader_objects : enable\n"
1574 "#extension GL_ARB_shading_language_420pack : enable\n"
1575 "layout (location = 0) in vec4 color;\n"
1576 "void main() {\n"
1577 " gl_FragColor = color;\n"
1578 "}\n";
1579
1580 ASSERT_NO_FATAL_FAILURE(InitState());
1581 ASSERT_NO_FATAL_FAILURE(InitViewport());
1582
1583 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Mike Stroyan55658c22014-12-04 11:08:39 +00001584 meshBuffer.BufferMemoryBarrier();
Tony Barbourf43b6982014-11-25 13:18:32 -07001585
1586 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1587 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1588
1589 XglPipelineObj pipelineobj(m_device);
1590 pipelineobj.AddShader(&vs);
1591 pipelineobj.AddShader(&ps);
1592
1593 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001594 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &meshBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07001595
Mark Lobodzinski15427102015-02-18 16:38:17 -06001596 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1597
Tony Barbourf43b6982014-11-25 13:18:32 -07001598 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1599 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1600 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1601 };
1602
1603 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1604 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001605 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -07001606 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1607 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001608 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -07001609 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1610
1611 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1612 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1613 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1614
Tony Barbourdd4c9642015-01-09 12:55:14 -07001615 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001616 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001617 XglCommandBufferObj cmdBuffer(m_device);
1618 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1619
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001620 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001621
1622 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1623
1624 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1625#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001626 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001627 pDSDumpDot((char*)"triTest2.dot");
1628#endif
1629 // render triangle
1630 cmdBuffer.Draw(0, 6, 0, 1);
1631
1632 // finalize recording of the command buffer
1633 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001634 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07001635
1636 for (int i = 0; i < m_renderTargetCount; i++)
1637 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001638}
1639
1640TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1641{
1642 // This tests that attributes work in the presence of gl_VertexID
1643 // and a dead attribute in position 0. Draws a triangle with yellow,
1644 // red and green corners, starting at top and going clockwise.
1645
1646 static const char *vertShaderText =
1647 "#version 140\n"
1648 "#extension GL_ARB_separate_shader_objects : enable\n"
1649 "#extension GL_ARB_shading_language_420pack : enable\n"
1650 //XYZ1( -1, -1, -1 )
1651 "layout (location = 0) in vec4 pos;\n"
1652 //XYZ1( 0.f, 0.f, 0.f )
1653 "layout (location = 1) in vec4 inColor;\n"
1654 "layout (location = 0) out vec4 outColor;\n"
1655 "void main() {\n"
1656 " outColor = inColor;\n"
1657 " vec2 vertices[3];"
1658 " vertices[0] = vec2(-1.0, -1.0);\n"
1659 " vertices[1] = vec2( 1.0, -1.0);\n"
1660 " vertices[2] = vec2( 0.0, 1.0);\n"
1661 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1662 "}\n";
1663
1664
1665 static const char *fragShaderText =
1666 "#version 140\n"
1667 "#extension GL_ARB_separate_shader_objects : enable\n"
1668 "#extension GL_ARB_shading_language_420pack : enable\n"
1669 "layout (location = 0) in vec4 color;\n"
1670 "void main() {\n"
1671 " gl_FragColor = color;\n"
1672 "}\n";
1673
1674 ASSERT_NO_FATAL_FAILURE(InitState());
1675 ASSERT_NO_FATAL_FAILURE(InitViewport());
1676
1677 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Mike Stroyan55658c22014-12-04 11:08:39 +00001678 meshBuffer.BufferMemoryBarrier();
Tony Barbourf43b6982014-11-25 13:18:32 -07001679
1680 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1681 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1682
1683 XglPipelineObj pipelineobj(m_device);
1684 pipelineobj.AddShader(&vs);
1685 pipelineobj.AddShader(&ps);
1686
1687 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001688 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &meshBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07001689
Mark Lobodzinski15427102015-02-18 16:38:17 -06001690 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1691
Tony Barbourf43b6982014-11-25 13:18:32 -07001692 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1693 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1694 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1695 };
1696
1697 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1698 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001699 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -07001700 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1701 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001702 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -07001703 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1704
1705 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1706 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1707 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1708
Tony Barbourdd4c9642015-01-09 12:55:14 -07001709 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001710 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001711 XglCommandBufferObj cmdBuffer(m_device);
1712 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001713
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001714 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001715
1716 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1717
1718 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1719#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001720 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001721 pDSDumpDot((char*)"triTest2.dot");
1722#endif
1723 // render triangle
1724 cmdBuffer.Draw(0, 6, 0, 1);
1725
1726 // finalize recording of the command buffer
1727 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001728 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07001729
1730 for (int i = 0; i < m_renderTargetCount; i++)
1731 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001732}
1733
1734TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001735{
1736 static const char *vertShaderText =
1737 "#version 140\n"
1738 "layout (std140) uniform bufferVals {\n"
1739 " mat4 mvp;\n"
1740 "} myBufferVals;\n"
1741 "in vec4 pos;\n"
1742 "in vec4 inColor;\n"
1743 "out vec4 outColor;\n"
1744 "void main() {\n"
1745 " outColor = inColor;\n"
1746 " gl_Position = myBufferVals.mvp * pos;\n"
1747 "}\n";
1748
1749 static const char *fragShaderText =
1750 "#version 130\n"
1751 "in vec4 color;\n"
1752 "void main() {\n"
1753 " gl_FragColor = color;\n"
1754 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001755 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001756
Tony Barbourf43b6982014-11-25 13:18:32 -07001757 glm::mat4 View = glm::lookAt(
1758 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1759 glm::vec3(0,0,0), // and looks at the origin
1760 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1761 );
1762
1763 glm::mat4 Model = glm::mat4(1.0f);
1764
1765 glm::mat4 MVP = Projection * View * Model;
1766
1767 ASSERT_NO_FATAL_FAILURE(InitState());
1768 ASSERT_NO_FATAL_FAILURE(InitViewport());
1769 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1770
1771 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1772 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1773
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001774 const int buf_size = sizeof(MVP) / sizeof(float);
Tony Barbourf43b6982014-11-25 13:18:32 -07001775
1776 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1777 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1778 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1779
Tony Barbourf43b6982014-11-25 13:18:32 -07001780 XglPipelineObj pipelineobj(m_device);
1781 pipelineobj.AddShader(&vs);
1782 pipelineobj.AddShader(&ps);
1783
Tony Barbourfa6cac72015-01-16 14:27:35 -07001784 XGL_PIPELINE_DS_STATE_CREATE_INFO ds_state;
1785 ds_state.depthTestEnable = XGL_TRUE;
1786 ds_state.depthWriteEnable = XGL_TRUE;
1787 ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
1788 ds_state.depthBoundsEnable = XGL_FALSE;
1789 ds_state.stencilTestEnable = XGL_FALSE;
1790 ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
1791 ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
1792 ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
1793 ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001794 ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001795 ds_state.front = ds_state.back;
1796 pipelineobj.SetDepthStencil(&ds_state);
1797
Tony Barbourf43b6982014-11-25 13:18:32 -07001798 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001799 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &MVPBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07001800
1801 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1802 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
Mark Lobodzinski15427102015-02-18 16:38:17 -06001803 m_memoryRefManager.AddMemoryRef(m_depthStencilMem, m_num_mem);
Tony Barbourf43b6982014-11-25 13:18:32 -07001804
1805 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1806 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1807 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1808 };
1809
1810 // this is the current description of g_vbData
1811 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1812 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001813 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -07001814 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1815 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07001816 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -07001817 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1818
1819 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1820 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1821 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1822
Tony Barboure4ed9942015-01-09 10:06:53 -07001823 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001824 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barboure4ed9942015-01-09 10:06:53 -07001825 XglCommandBufferObj cmdBuffer(m_device);
1826 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001827
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001828 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barboure4ed9942015-01-09 10:06:53 -07001829 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
Tony Barbourf43b6982014-11-25 13:18:32 -07001830
Tony Barboure4ed9942015-01-09 10:06:53 -07001831 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1832#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001833 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barboure4ed9942015-01-09 10:06:53 -07001834 pDSDumpDot((char*)"triTest2.dot");
1835#endif
1836 // render triangle
1837 cmdBuffer.Draw(0, 36, 0, 1);
1838
1839 // finalize recording of the command buffer
1840 cmdBuffer.EndCommandBuffer();
Tony Barbourdd4c9642015-01-09 12:55:14 -07001841 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barboure4ed9942015-01-09 10:06:53 -07001842
1843 for (int i = 0; i < m_renderTargetCount; i++)
1844 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001845}
1846
Tony Barbourf43b6982014-11-25 13:18:32 -07001847TEST_F(XglRenderTest, VSTexture)
1848{
1849 // The expected result from this test is a green and red triangle;
1850 // one red vertex on the left, two green vertices on the right.
1851 static const char *vertShaderText =
1852 "#version 130\n"
1853 "out vec4 texColor;\n"
1854 "uniform sampler2D surface;\n"
1855 "void main() {\n"
1856 " vec2 vertices[3];"
1857 " vertices[0] = vec2(-0.5, -0.5);\n"
1858 " vertices[1] = vec2( 0.5, -0.5);\n"
1859 " vertices[2] = vec2( 0.5, 0.5);\n"
1860 " vec2 positions[3];"
1861 " positions[0] = vec2( 0.0, 0.0);\n"
1862 " positions[1] = vec2( 0.25, 0.1);\n"
1863 " positions[2] = vec2( 0.1, 0.25);\n"
1864 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1865 " texColor = textureLod(surface, samplePos, 0.0);\n"
1866 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1867 "}\n";
1868
1869 static const char *fragShaderText =
1870 "#version 130\n"
1871 "in vec4 texColor;\n"
1872 "void main() {\n"
1873 " gl_FragColor = texColor;\n"
1874 "}\n";
1875
1876 ASSERT_NO_FATAL_FAILURE(InitState());
1877 ASSERT_NO_FATAL_FAILURE(InitViewport());
1878
1879 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1880 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1881 XglSamplerObj sampler(m_device);
1882 XglTextureObj texture(m_device);
1883
Tony Barbourf43b6982014-11-25 13:18:32 -07001884 XglPipelineObj pipelineobj(m_device);
1885 pipelineobj.AddShader(&vs);
1886 pipelineobj.AddShader(&ps);
1887
1888 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001889 descriptorSet.AppendSamplerTexture(&sampler, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001890
1891 m_memoryRefManager.AddMemoryRef(&texture);
1892
Tony Barbourdd4c9642015-01-09 12:55:14 -07001893 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001894 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001895 XglCommandBufferObj cmdBuffer(m_device);
1896 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001897
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001898 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001899
1900 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1901
1902#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001903 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001904 pDSDumpDot((char*)"triTest2.dot");
1905#endif
1906 // render triangle
1907 cmdBuffer.Draw(0, 3, 0, 1);
1908
1909 // finalize recording of the command buffer
1910 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001911 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07001912
1913 for (int i = 0; i < m_renderTargetCount; i++)
1914 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001915}
1916TEST_F(XglRenderTest, TexturedTriangle)
1917{
1918 // The expected result from this test is a red and green checkered triangle
1919 static const char *vertShaderText =
1920 "#version 140\n"
1921 "#extension GL_ARB_separate_shader_objects : enable\n"
1922 "#extension GL_ARB_shading_language_420pack : enable\n"
1923 "layout (location = 0) out vec2 samplePos;\n"
1924 "void main() {\n"
1925 " vec2 vertices[3];"
1926 " vertices[0] = vec2(-0.5, -0.5);\n"
1927 " vertices[1] = vec2( 0.5, -0.5);\n"
1928 " vertices[2] = vec2( 0.5, 0.5);\n"
1929 " vec2 positions[3];"
1930 " positions[0] = vec2( 0.0, 0.0);\n"
1931 " positions[1] = vec2( 1.0, 0.0);\n"
1932 " positions[2] = vec2( 1.0, 1.0);\n"
1933 " samplePos = positions[gl_VertexID % 3];\n"
1934 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1935 "}\n";
1936
1937 static const char *fragShaderText =
1938 "#version 140\n"
1939 "#extension GL_ARB_separate_shader_objects : enable\n"
1940 "#extension GL_ARB_shading_language_420pack : enable\n"
1941 "layout (location = 0) in vec2 samplePos;\n"
1942 "layout (binding = 0) uniform sampler2D surface;\n"
1943 "layout (location=0) out vec4 outColor;\n"
1944 "void main() {\n"
1945 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1946 " outColor = texColor;\n"
1947 "}\n";
1948
1949 ASSERT_NO_FATAL_FAILURE(InitState());
1950 ASSERT_NO_FATAL_FAILURE(InitViewport());
1951
1952 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1953 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1954 XglSamplerObj sampler(m_device);
1955 XglTextureObj texture(m_device);
1956
Tony Barbourf43b6982014-11-25 13:18:32 -07001957 XglPipelineObj pipelineobj(m_device);
1958 pipelineobj.AddShader(&vs);
1959 pipelineobj.AddShader(&ps);
1960
1961 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08001962 descriptorSet.AppendSamplerTexture(&sampler, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001963
1964 m_memoryRefManager.AddMemoryRef(&texture);
1965
Tony Barbourdd4c9642015-01-09 12:55:14 -07001966 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06001967 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07001968 XglCommandBufferObj cmdBuffer(m_device);
1969 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1970
Jeremy Hayese0c3b222015-01-14 16:17:08 -07001971 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07001972
1973 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
1974
1975#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001976 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07001977 pDSDumpDot((char*)"triTest2.dot");
1978#endif
1979 // render triangle
1980 cmdBuffer.Draw(0, 3, 0, 1);
1981
1982 // finalize recording of the command buffer
1983 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06001984 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07001985
1986 for (int i = 0; i < m_renderTargetCount; i++)
1987 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001988}
1989TEST_F(XglRenderTest, TexturedTriangleClip)
1990{
1991 // The expected result from this test is a red and green checkered triangle
1992 static const char *vertShaderText =
1993 "#version 330\n"
1994 "#extension GL_ARB_separate_shader_objects : enable\n"
1995 "#extension GL_ARB_shading_language_420pack : enable\n"
1996 "layout (location = 0) out vec2 samplePos;\n"
1997 "out gl_PerVertex {\n"
1998 " vec4 gl_Position;\n"
1999 " float gl_ClipDistance[1];\n"
2000 "};\n"
2001 "void main() {\n"
2002 " vec2 vertices[3];"
2003 " vertices[0] = vec2(-0.5, -0.5);\n"
2004 " vertices[1] = vec2( 0.5, -0.5);\n"
2005 " vertices[2] = vec2( 0.5, 0.5);\n"
2006 " vec2 positions[3];"
2007 " positions[0] = vec2( 0.0, 0.0);\n"
2008 " positions[1] = vec2( 1.0, 0.0);\n"
2009 " positions[2] = vec2( 1.0, 1.0);\n"
2010 " float dists[3];\n"
2011 " dists[0] = 1.0;\n"
2012 " dists[1] = 1.0;\n"
2013 " dists[2] = -1.0;\n"
2014 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
2015 " samplePos = positions[gl_VertexID % 3];\n"
2016 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2017 "}\n";
2018
2019 static const char *fragShaderText =
2020 "#version 140\n"
2021 "#extension GL_ARB_separate_shader_objects : enable\n"
2022 "#extension GL_ARB_shading_language_420pack : enable\n"
2023 "layout (location = 0) in vec2 samplePos;\n"
2024 "layout (binding = 0) uniform sampler2D surface;\n"
2025 "layout (location=0) out vec4 outColor;\n"
2026 "void main() {\n"
2027 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
2028 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
2029 " outColor = texColor;\n"
2030 "}\n";
2031
2032
2033 ASSERT_NO_FATAL_FAILURE(InitState());
2034 ASSERT_NO_FATAL_FAILURE(InitViewport());
2035
2036 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2037 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2038 XglSamplerObj sampler(m_device);
2039 XglTextureObj texture(m_device);
2040
Tony Barbourf43b6982014-11-25 13:18:32 -07002041 XglPipelineObj pipelineobj(m_device);
2042 pipelineobj.AddShader(&vs);
2043 pipelineobj.AddShader(&ps);
2044
2045 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08002046 descriptorSet.AppendSamplerTexture(&sampler, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002047
2048 m_memoryRefManager.AddMemoryRef(&texture);
2049
Tony Barbourdd4c9642015-01-09 12:55:14 -07002050 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06002051 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07002052 XglCommandBufferObj cmdBuffer(m_device);
2053 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
2054
Jeremy Hayese0c3b222015-01-14 16:17:08 -07002055 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07002056
2057 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
2058
2059#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002060 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07002061 pDSDumpDot((char*)"triTest2.dot");
2062#endif
2063 // render triangle
2064 cmdBuffer.Draw(0, 3, 0, 1);
2065
2066 // finalize recording of the command buffer
2067 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06002068 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07002069
2070 for (int i = 0; i < m_renderTargetCount; i++)
2071 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07002072}
2073TEST_F(XglRenderTest, FSTriangle)
2074{
2075 // The expected result from this test is a red and green checkered triangle
2076 static const char *vertShaderText =
2077 "#version 140\n"
2078 "#extension GL_ARB_separate_shader_objects : enable\n"
2079 "#extension GL_ARB_shading_language_420pack : enable\n"
2080 "layout (location = 0) out vec2 samplePos;\n"
2081 "void main() {\n"
2082 " vec2 vertices[3];"
2083 " vertices[0] = vec2(-0.5, -0.5);\n"
2084 " vertices[1] = vec2( 0.5, -0.5);\n"
2085 " vertices[2] = vec2( 0.5, 0.5);\n"
2086 " vec2 positions[3];"
2087 " positions[0] = vec2( 0.0, 0.0);\n"
2088 " positions[1] = vec2( 1.0, 0.0);\n"
2089 " positions[2] = vec2( 1.0, 1.0);\n"
2090 " samplePos = positions[gl_VertexID % 3];\n"
2091 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2092 "}\n";
2093
2094 static const char *fragShaderText =
2095 "#version 140\n"
2096 "#extension GL_ARB_separate_shader_objects : enable\n"
2097 "#extension GL_ARB_shading_language_420pack : enable\n"
2098 "layout (location = 0) in vec2 samplePos;\n"
2099 "layout (binding = 0) uniform sampler2D surface;\n"
2100 "layout (location=0) out vec4 outColor;\n"
2101 "void main() {\n"
2102 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
2103 " outColor = texColor;\n"
2104 "}\n";
2105
2106 ASSERT_NO_FATAL_FAILURE(InitState());
2107 ASSERT_NO_FATAL_FAILURE(InitViewport());
2108
2109 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2110 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2111 XglSamplerObj sampler(m_device);
2112 XglTextureObj texture(m_device);
2113
Tony Barbourf43b6982014-11-25 13:18:32 -07002114 XglPipelineObj pipelineobj(m_device);
2115 pipelineobj.AddShader(&vs);
2116 pipelineobj.AddShader(&ps);
2117
2118 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08002119 descriptorSet.AppendSamplerTexture(&sampler, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002120
2121 m_memoryRefManager.AddMemoryRef(&texture);
2122
Tony Barbourdd4c9642015-01-09 12:55:14 -07002123 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06002124 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07002125 XglCommandBufferObj cmdBuffer(m_device);
2126 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
2127
Jeremy Hayese0c3b222015-01-14 16:17:08 -07002128 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07002129
2130 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
2131
2132#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002133 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07002134 pDSDumpDot((char*)"triTest2.dot");
2135#endif
2136 // render triangle
2137 cmdBuffer.Draw(0, 3, 0, 1);
2138
2139 // finalize recording of the command buffer
2140 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06002141 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07002142
2143 for (int i = 0; i < m_renderTargetCount; i++)
2144 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07002145}
2146TEST_F(XglRenderTest, SamplerBindingsTriangle)
2147{
2148 // This test sets bindings on the samplers
2149 // For now we are asserting that sampler and texture pairs
2150 // march in lock step, and are set via GLSL binding. This can
2151 // and will probably change.
2152 // The sampler bindings should match the sampler and texture slot
2153 // number set up by the application.
2154 // This test will result in a blue triangle
2155 static const char *vertShaderText =
2156 "#version 140\n"
2157 "#extension GL_ARB_separate_shader_objects : enable\n"
2158 "#extension GL_ARB_shading_language_420pack : enable\n"
2159 "layout (location = 0) out vec4 samplePos;\n"
2160 "void main() {\n"
2161 " vec2 vertices[3];"
2162 " vertices[0] = vec2(-0.5, -0.5);\n"
2163 " vertices[1] = vec2( 0.5, -0.5);\n"
2164 " vertices[2] = vec2( 0.5, 0.5);\n"
2165 " vec2 positions[3];"
2166 " positions[0] = vec2( 0.0, 0.0);\n"
2167 " positions[1] = vec2( 1.0, 0.0);\n"
2168 " positions[2] = vec2( 1.0, 1.0);\n"
2169 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
2170 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2171 "}\n";
2172
2173 static const char *fragShaderText =
2174 "#version 140\n"
2175 "#extension GL_ARB_separate_shader_objects : enable\n"
2176 "#extension GL_ARB_shading_language_420pack : enable\n"
2177 "layout (location = 0) in vec4 samplePos;\n"
2178 "layout (binding = 0) uniform sampler2D surface0;\n"
2179 "layout (binding = 1) uniform sampler2D surface1;\n"
2180 "layout (binding = 12) uniform sampler2D surface2;\n"
2181 "void main() {\n"
2182 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
2183 "}\n";
2184
2185 ASSERT_NO_FATAL_FAILURE(InitState());
2186 ASSERT_NO_FATAL_FAILURE(InitViewport());
2187
2188 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2189 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2190
2191 XglSamplerObj sampler1(m_device);
2192 XglSamplerObj sampler2(m_device);
2193 XglSamplerObj sampler3(m_device);
2194
Tony Barbour2f421a02015-04-01 16:38:10 -06002195 uint32_t tex_colors[2] = { 0xffff0000, 0xffff0000 };
2196 XglTextureObj texture1(m_device, tex_colors); // Red
2197 tex_colors[0] = 0xff00ff00; tex_colors[1] = 0xff00ff00;
2198 XglTextureObj texture2(m_device, tex_colors); // Green
2199 tex_colors[0] = 0xff0000ff; tex_colors[1] = 0xff0000ff;
2200 XglTextureObj texture3(m_device, tex_colors); // Blue
Tony Barbourf43b6982014-11-25 13:18:32 -07002201
Tony Barbourf43b6982014-11-25 13:18:32 -07002202 XglPipelineObj pipelineobj(m_device);
2203 pipelineobj.AddShader(&vs);
2204 pipelineobj.AddShader(&ps);
2205
2206 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08002207 descriptorSet.AppendSamplerTexture(&sampler1, &texture1);
2208 descriptorSet.AppendSamplerTexture(&sampler2, &texture2);
2209 for (int i = 0; i < 10; i++)
2210 descriptorSet.AppendDummy();
2211 descriptorSet.AppendSamplerTexture(&sampler3, &texture3);
Tony Barbourf43b6982014-11-25 13:18:32 -07002212
2213 m_memoryRefManager.AddMemoryRef(&texture1);
2214 m_memoryRefManager.AddMemoryRef(&texture2);
2215 m_memoryRefManager.AddMemoryRef(&texture3);
2216
Tony Barbourdd4c9642015-01-09 12:55:14 -07002217 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06002218 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07002219 XglCommandBufferObj cmdBuffer(m_device);
2220 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
2221
Jeremy Hayese0c3b222015-01-14 16:17:08 -07002222 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07002223
2224 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
2225
2226#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002227 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07002228 pDSDumpDot((char*)"triTest2.dot");
2229#endif
2230 // render triangle
2231 cmdBuffer.Draw(0, 3, 0, 1);
2232
2233 // finalize recording of the command buffer
2234 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06002235 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07002236
2237 for (int i = 0; i < m_renderTargetCount; i++)
2238 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07002239
2240}
2241
2242TEST_F(XglRenderTest, TriangleVSUniformBlock)
2243{
2244 // The expected result from this test is a blue triangle
2245
2246 static const char *vertShaderText =
2247 "#version 140\n"
2248 "#extension GL_ARB_separate_shader_objects : enable\n"
2249 "#extension GL_ARB_shading_language_420pack : enable\n"
2250 "layout (location = 0) out vec4 outColor;\n"
2251 "layout (std140, binding = 0) uniform bufferVals {\n"
2252 " vec4 red;\n"
2253 " vec4 green;\n"
2254 " vec4 blue;\n"
2255 " vec4 white;\n"
2256 "} myBufferVals;\n"
2257 "void main() {\n"
2258 " vec2 vertices[3];"
2259 " vertices[0] = vec2(-0.5, -0.5);\n"
2260 " vertices[1] = vec2( 0.5, -0.5);\n"
2261 " vertices[2] = vec2( 0.5, 0.5);\n"
2262 " outColor = myBufferVals.blue;\n"
2263 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2264 "}\n";
2265
2266 static const char *fragShaderText =
2267 "#version 140\n"
2268 "#extension GL_ARB_separate_shader_objects : enable\n"
2269 "#extension GL_ARB_shading_language_420pack : enable\n"
2270 "layout (location = 0) in vec4 inColor;\n"
2271 "void main() {\n"
2272 " gl_FragColor = inColor;\n"
2273 "}\n";
2274
2275 ASSERT_NO_FATAL_FAILURE(InitState());
2276 ASSERT_NO_FATAL_FAILURE(InitViewport());
2277
2278 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2279 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2280
2281 // Let's populate our buffer with the following:
2282 // vec4 red;
2283 // vec4 green;
2284 // vec4 blue;
2285 // vec4 white;
2286 const int valCount = 4 * 4;
2287 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2288 0.0, 1.0, 0.0, 1.0,
2289 0.0, 0.0, 1.0, 1.0,
2290 1.0, 1.0, 1.0, 1.0 };
2291
2292 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
Tony Barbourf43b6982014-11-25 13:18:32 -07002293
2294 XglPipelineObj pipelineobj(m_device);
2295 pipelineobj.AddShader(&vs);
2296 pipelineobj.AddShader(&ps);
2297
2298 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08002299 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &colorBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07002300
Tony Barbourdd4c9642015-01-09 12:55:14 -07002301 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06002302 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07002303 XglCommandBufferObj cmdBuffer(m_device);
2304 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
2305
Jeremy Hayese0c3b222015-01-14 16:17:08 -07002306 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07002307
2308 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
2309
2310#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002311 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07002312 pDSDumpDot((char*)"triTest2.dot");
2313#endif
2314 // render triangle
2315 cmdBuffer.Draw(0, 3, 0, 1);
2316
2317 // finalize recording of the command buffer
2318 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06002319 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07002320
2321 for (int i = 0; i < m_renderTargetCount; i++)
2322 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07002323
2324}
2325
2326TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2327{
2328 // This test allows the shader to select which buffer it is
2329 // pulling from using layout binding qualifier.
2330 // There are corresponding changes in the compiler stack that
2331 // will select the buffer using binding directly.
2332 // The binding number should match the slot number set up by
2333 // the application.
2334 // The expected result from this test is a purple triangle
2335
2336 static const char *vertShaderText =
2337 "#version 140\n"
2338 "#extension GL_ARB_separate_shader_objects : enable\n"
2339 "#extension GL_ARB_shading_language_420pack : enable\n"
2340 "void main() {\n"
2341 " vec2 vertices[3];"
2342 " vertices[0] = vec2(-0.5, -0.5);\n"
2343 " vertices[1] = vec2( 0.5, -0.5);\n"
2344 " vertices[2] = vec2( 0.5, 0.5);\n"
2345 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2346 "}\n";
2347
2348 static const char *fragShaderText =
2349 "#version 140\n"
2350 "#extension GL_ARB_separate_shader_objects : enable\n"
2351 "#extension GL_ARB_shading_language_420pack : enable\n"
2352 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2353 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2354 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
Chia-I Wuf8385062015-01-04 16:27:24 +08002355 "layout (std140, binding = 3) uniform whiteVal { vec4 color; } myWhiteVal\n;"
Tony Barbourf43b6982014-11-25 13:18:32 -07002356 "void main() {\n"
2357 " gl_FragColor = myBlueVal.color;\n"
2358 " gl_FragColor += myRedVal.color;\n"
2359 "}\n";
2360
2361 ASSERT_NO_FATAL_FAILURE(InitState());
2362 ASSERT_NO_FATAL_FAILURE(InitViewport());
2363
2364 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2365 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2366
2367 // We're going to create a number of uniform buffers, and then allow
2368 // the shader to select which it wants to read from with a binding
2369
2370 // Let's populate the buffers with a single color each:
2371 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2372 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2373 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
Chia-I Wuf8385062015-01-04 16:27:24 +08002374 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
Tony Barbourf43b6982014-11-25 13:18:32 -07002375
2376 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2377 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2378 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2379 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2380
2381 const int redCount = sizeof(redVals) / sizeof(float);
2382 const int greenCount = sizeof(greenVals) / sizeof(float);
2383 const int blueCount = sizeof(blueVals) / sizeof(float);
2384 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2385
2386 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Tony Barbourf43b6982014-11-25 13:18:32 -07002387
2388 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Tony Barbourf43b6982014-11-25 13:18:32 -07002389
2390 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Tony Barbourf43b6982014-11-25 13:18:32 -07002391
2392 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Tony Barbourf43b6982014-11-25 13:18:32 -07002393
2394 XglPipelineObj pipelineobj(m_device);
2395 pipelineobj.AddShader(&vs);
2396 pipelineobj.AddShader(&ps);
2397
2398 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08002399 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &redBuffer);
2400 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &greenBuffer);
2401 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &blueBuffer);
2402 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &whiteBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07002403
Tony Barbourdd4c9642015-01-09 12:55:14 -07002404 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06002405 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07002406 XglCommandBufferObj cmdBuffer(m_device);
2407 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -07002408
Jeremy Hayese0c3b222015-01-14 16:17:08 -07002409 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07002410
2411 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
2412
2413#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002414 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07002415 pDSDumpDot((char*)"triTest2.dot");
2416#endif
2417 // render triangle
2418 cmdBuffer.Draw(0, 3, 0, 1);
2419
2420 // finalize recording of the command buffer
2421 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06002422 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07002423
2424 for (int i = 0; i < m_renderTargetCount; i++)
2425 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07002426}
2427
2428TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2429{
2430 // This test is the same as TriangleFSUniformBlockBinding, but
2431 // it does not provide an instance name.
2432 // The expected result from this test is a purple triangle
2433
2434 static const char *vertShaderText =
2435 "#version 140\n"
2436 "#extension GL_ARB_separate_shader_objects : enable\n"
2437 "#extension GL_ARB_shading_language_420pack : enable\n"
2438 "void main() {\n"
2439 " vec2 vertices[3];"
2440 " vertices[0] = vec2(-0.5, -0.5);\n"
2441 " vertices[1] = vec2( 0.5, -0.5);\n"
2442 " vertices[2] = vec2( 0.5, 0.5);\n"
2443 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2444 "}\n";
2445
2446 static const char *fragShaderText =
2447 "#version 430\n"
2448 "#extension GL_ARB_separate_shader_objects : enable\n"
2449 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002450 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2451 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2452 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
Chia-I Wuf8385062015-01-04 16:27:24 +08002453 "layout (std140, binding = 3) uniform whiteVal { vec4 white; };"
Cody Northrop68a10f62014-12-05 15:44:14 -07002454 "layout (location = 0) out vec4 outColor;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002455 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -07002456 " outColor = blue;\n"
2457 " outColor += red;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002458 "}\n";
2459 ASSERT_NO_FATAL_FAILURE(InitState());
2460 ASSERT_NO_FATAL_FAILURE(InitViewport());
2461
2462 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2463 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2464
2465 // We're going to create a number of uniform buffers, and then allow
2466 // the shader to select which it wants to read from with a binding
2467
2468 // Let's populate the buffers with a single color each:
2469 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2470 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2471 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2472 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2473
2474 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2475 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2476 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2477 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2478
2479 const int redCount = sizeof(redVals) / sizeof(float);
2480 const int greenCount = sizeof(greenVals) / sizeof(float);
2481 const int blueCount = sizeof(blueVals) / sizeof(float);
2482 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2483
2484 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Tony Barbourf43b6982014-11-25 13:18:32 -07002485
2486 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Tony Barbourf43b6982014-11-25 13:18:32 -07002487
2488 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Tony Barbourf43b6982014-11-25 13:18:32 -07002489
2490 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Tony Barbourf43b6982014-11-25 13:18:32 -07002491
2492 XglPipelineObj pipelineobj(m_device);
2493 pipelineobj.AddShader(&vs);
2494 pipelineobj.AddShader(&ps);
2495
2496 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08002497 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &redBuffer);
2498 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &greenBuffer);
2499 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &blueBuffer);
2500 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &whiteBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07002501
Tony Barbourdd4c9642015-01-09 12:55:14 -07002502 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06002503 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07002504 XglCommandBufferObj cmdBuffer(m_device);
2505 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
2506
Jeremy Hayese0c3b222015-01-14 16:17:08 -07002507 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07002508
2509 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
2510
2511#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002512 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07002513 pDSDumpDot((char*)"triTest2.dot");
2514#endif
2515 // render triangle
2516 cmdBuffer.Draw(0, 3, 0, 1);
2517
2518 // finalize recording of the command buffer
2519 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06002520 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07002521
2522 for (int i = 0; i < m_renderTargetCount; i++)
2523 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07002524
2525}
2526
2527TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2528{
2529 static const char *vertShaderText =
2530 "#version 140\n"
2531 "#extension GL_ARB_separate_shader_objects : enable\n"
2532 "#extension GL_ARB_shading_language_420pack : enable\n"
2533 "layout (std140, binding=0) uniform bufferVals {\n"
2534 " mat4 mvp;\n"
2535 "} myBufferVals;\n"
2536 "layout (location=0) in vec4 pos;\n"
2537 "layout (location=0) out vec2 UV;\n"
2538 "void main() {\n"
2539 " vec2 positions[3];"
2540 " positions[0] = vec2( 0.0, 0.0);\n"
2541 " positions[1] = vec2( 0.25, 0.1);\n"
2542 " positions[2] = vec2( 0.1, 0.25);\n"
2543 " UV = positions[gl_VertexID % 3];\n"
2544 " gl_Position = myBufferVals.mvp * pos;\n"
2545 "}\n";
2546
2547 static const char *fragShaderText =
2548 "#version 140\n"
2549 "#extension GL_ARB_separate_shader_objects : enable\n"
2550 "#extension GL_ARB_shading_language_420pack : enable\n"
Chia-I Wuf8385062015-01-04 16:27:24 +08002551 "layout (binding=1) uniform sampler2D surface;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002552 "layout (location=0) out vec4 outColor;\n"
2553 "layout (location=0) in vec2 UV;\n"
2554 "void main() {\n"
2555 " outColor= textureLod(surface, UV, 0.0);\n"
2556 "}\n";
2557 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2558
2559 glm::mat4 View = glm::lookAt(
2560 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2561 glm::vec3(0,0,0), // and looks at the origin
2562 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2563 );
2564
2565 glm::mat4 Model = glm::mat4(1.0f);
2566
2567 glm::mat4 MVP = Projection * View * Model;
2568
2569
2570 ASSERT_NO_FATAL_FAILURE(InitState());
2571 ASSERT_NO_FATAL_FAILURE(InitViewport());
2572 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2573
2574 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2575 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
Mike Stroyan55658c22014-12-04 11:08:39 +00002576 meshBuffer.BufferMemoryBarrier();
Tony Barbourf43b6982014-11-25 13:18:32 -07002577
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002578 const int buf_size = sizeof(MVP) / sizeof(float);
Tony Barbourf43b6982014-11-25 13:18:32 -07002579
2580 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2581 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2582 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2583 XglSamplerObj sampler(m_device);
2584 XglTextureObj texture(m_device);
2585
Tony Barbourf43b6982014-11-25 13:18:32 -07002586 XglPipelineObj pipelineobj(m_device);
2587 pipelineobj.AddShader(&vs);
2588 pipelineobj.AddShader(&ps);
2589
2590 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08002591 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &mvpBuffer);
2592 descriptorSet.AppendSamplerTexture(&sampler, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002593
2594 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2595 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2596 m_memoryRefManager.AddMemoryRef(&texture);
Mark Lobodzinski15427102015-02-18 16:38:17 -06002597 m_memoryRefManager.AddMemoryRef(m_depthStencilMem, m_num_mem);
Tony Barbourf43b6982014-11-25 13:18:32 -07002598
2599 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2600 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2601 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2602 };
2603
2604 // this is the current description of g_vbData
2605 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2606 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07002607 vi_attribs[0].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -07002608 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2609 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07002610 vi_attribs[1].format = XGL_FMT_R32G32B32A32_SFLOAT; // format of source data
Tony Barbourf43b6982014-11-25 13:18:32 -07002611 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2612
2613 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2614 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2615 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2616
Tony Barbourfa6cac72015-01-16 14:27:35 -07002617 XGL_PIPELINE_DS_STATE_CREATE_INFO ds_state;
2618 ds_state.depthTestEnable = XGL_TRUE;
2619 ds_state.depthWriteEnable = XGL_TRUE;
2620 ds_state.depthFunc = XGL_COMPARE_LESS_EQUAL;
2621 ds_state.depthBoundsEnable = XGL_FALSE;
2622 ds_state.stencilTestEnable = XGL_FALSE;
2623 ds_state.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
2624 ds_state.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
2625 ds_state.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
2626 ds_state.back.stencilFunc = XGL_COMPARE_ALWAYS;
Jeremy Hayes2b7e88a2015-01-23 08:51:43 -07002627 ds_state.format = XGL_FMT_D32_SFLOAT;
Tony Barbourfa6cac72015-01-16 14:27:35 -07002628 ds_state.front = ds_state.back;
2629 pipelineobj.SetDepthStencil(&ds_state);
2630
Tony Barbourdd4c9642015-01-09 12:55:14 -07002631 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06002632 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07002633 XglCommandBufferObj cmdBuffer(m_device);
2634 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -07002635
Jeremy Hayese0c3b222015-01-14 16:17:08 -07002636 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07002637 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
2638
2639 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
2640#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002641 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07002642 pDSDumpDot((char*)"triTest2.dot");
2643#endif
2644 // render triangle
2645 cmdBuffer.Draw(0, 36, 0, 1);
2646
2647 // finalize recording of the command buffer
2648 cmdBuffer.EndCommandBuffer();
2649 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2650
2651 for (int i = 0; i < m_renderTargetCount; i++)
2652 RecordImage(m_renderTargets[i]);
Tony Barbourf43b6982014-11-25 13:18:32 -07002653
2654}
Cody Northropd1ce7842014-12-09 11:17:01 -07002655
2656TEST_F(XglRenderTest, TriangleMixedSamplerUniformBlockBinding)
2657{
2658 // This test mixes binding slots of textures and buffers, ensuring
2659 // that sparse and overlapping assignments work.
Cody Northropa0410942014-12-09 13:59:39 -07002660 // The expected result from this test is a purple triangle, although
Cody Northropd1ce7842014-12-09 11:17:01 -07002661 // you can modify it to move the desired result around.
2662
2663 static const char *vertShaderText =
2664 "#version 140\n"
2665 "#extension GL_ARB_separate_shader_objects : enable\n"
2666 "#extension GL_ARB_shading_language_420pack : enable\n"
2667 "void main() {\n"
2668 " vec2 vertices[3];"
2669 " vertices[0] = vec2(-0.5, -0.5);\n"
2670 " vertices[1] = vec2( 0.5, -0.5);\n"
2671 " vertices[2] = vec2( 0.5, 0.5);\n"
2672 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2673 "}\n";
2674
2675 static const char *fragShaderText =
2676 "#version 430\n"
2677 "#extension GL_ARB_separate_shader_objects : enable\n"
2678 "#extension GL_ARB_shading_language_420pack : enable\n"
2679 "layout (binding = 0) uniform sampler2D surface0;\n"
Chia-I Wuf8385062015-01-04 16:27:24 +08002680 "layout (binding = 3) uniform sampler2D surface1;\n"
2681 "layout (binding = 1) uniform sampler2D surface2;\n"
2682 "layout (binding = 2) uniform sampler2D surface3;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002683
Cody Northropa0410942014-12-09 13:59:39 -07002684
Chia-I Wuf8385062015-01-04 16:27:24 +08002685 "layout (std140, binding = 4) uniform redVal { vec4 red; };"
2686 "layout (std140, binding = 6) uniform greenVal { vec4 green; };"
2687 "layout (std140, binding = 5) uniform blueVal { vec4 blue; };"
2688 "layout (std140, binding = 7) uniform whiteVal { vec4 white; };"
Cody Northropd1ce7842014-12-09 11:17:01 -07002689 "layout (location = 0) out vec4 outColor;\n"
2690 "void main() {\n"
Cody Northropa0410942014-12-09 13:59:39 -07002691 " outColor = red * vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002692 " outColor += white * vec4(0.00001);\n"
2693 " outColor += textureLod(surface2, vec2(0.5), 0.0)* vec4(0.00001);\n"
Cody Northropa0410942014-12-09 13:59:39 -07002694 " outColor += textureLod(surface1, vec2(0.0), 0.0);//* vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002695 "}\n";
2696 ASSERT_NO_FATAL_FAILURE(InitState());
2697 ASSERT_NO_FATAL_FAILURE(InitViewport());
2698
2699 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2700 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2701
Cody Northropd1ce7842014-12-09 11:17:01 -07002702 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2703 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2704 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2705 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2706
2707 const int redCount = sizeof(redVals) / sizeof(float);
2708 const int greenCount = sizeof(greenVals) / sizeof(float);
2709 const int blueCount = sizeof(blueVals) / sizeof(float);
2710 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2711
2712 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002713 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002714 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002715 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002716
Tony Barbour2f421a02015-04-01 16:38:10 -06002717 uint32_t tex_colors[2] = { 0xff800000, 0xff800000 };
Cody Northropd1ce7842014-12-09 11:17:01 -07002718 XglSamplerObj sampler0(m_device);
Tony Barbour2f421a02015-04-01 16:38:10 -06002719 XglTextureObj texture0(m_device, tex_colors); // Light Red
2720 tex_colors[0] = 0xff000080; tex_colors[1] = 0xff000080;
Cody Northropd1ce7842014-12-09 11:17:01 -07002721 XglSamplerObj sampler2(m_device);
Tony Barbour2f421a02015-04-01 16:38:10 -06002722 XglTextureObj texture2(m_device, tex_colors); // Light Blue
2723 tex_colors[0] = 0xff008000; tex_colors[1] = 0xff008000;
Cody Northropd1ce7842014-12-09 11:17:01 -07002724 XglSamplerObj sampler4(m_device);
Tony Barbour2f421a02015-04-01 16:38:10 -06002725 XglTextureObj texture4(m_device, tex_colors); // Light Green
Cody Northropa0410942014-12-09 13:59:39 -07002726
2727 // NOTE: Bindings 1,3,5,7,8,9,11,12,14,16 work for this sampler, but 6 does not!!!
2728 // TODO: Get back here ASAP and understand why.
Tony Barbour2f421a02015-04-01 16:38:10 -06002729 tex_colors[0] = 0xffff00ff; tex_colors[1] = 0xffff00ff;
Cody Northropa0410942014-12-09 13:59:39 -07002730 XglSamplerObj sampler7(m_device);
Tony Barbour2f421a02015-04-01 16:38:10 -06002731 XglTextureObj texture7(m_device, tex_colors); // Red and Blue
Cody Northropd1ce7842014-12-09 11:17:01 -07002732
2733 XglPipelineObj pipelineobj(m_device);
2734 pipelineobj.AddShader(&vs);
2735 pipelineobj.AddShader(&ps);
2736
2737 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08002738 descriptorSet.AppendSamplerTexture(&sampler0, &texture0);
2739 descriptorSet.AppendSamplerTexture(&sampler2, &texture2);
2740 descriptorSet.AppendSamplerTexture(&sampler4, &texture4);
2741 descriptorSet.AppendSamplerTexture(&sampler7, &texture7);
2742 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &redBuffer);
2743 // swap blue and green
2744 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &blueBuffer);
2745 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &greenBuffer);
2746 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &whiteBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002747
2748 m_memoryRefManager.AddMemoryRef(&texture0);
2749 m_memoryRefManager.AddMemoryRef(&texture2);
2750 m_memoryRefManager.AddMemoryRef(&texture4);
Cody Northropa0410942014-12-09 13:59:39 -07002751 m_memoryRefManager.AddMemoryRef(&texture7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002752
Tony Barbourdd4c9642015-01-09 12:55:14 -07002753 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06002754 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07002755 XglCommandBufferObj cmdBuffer(m_device);
2756 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Cody Northropd1ce7842014-12-09 11:17:01 -07002757
Jeremy Hayese0c3b222015-01-14 16:17:08 -07002758 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07002759
2760 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
2761
2762#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002763 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07002764 pDSDumpDot((char*)"triTest2.dot");
2765#endif
2766 // render triangle
2767 cmdBuffer.Draw(0, 3, 0, 1);
2768
2769 // finalize recording of the command buffer
2770 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06002771 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07002772
2773 for (int i = 0; i < m_renderTargetCount; i++)
2774 RecordImage(m_renderTargets[i]);
Cody Northropd1ce7842014-12-09 11:17:01 -07002775
2776}
2777
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002778TEST_F(XglRenderTest, TriangleMatchingSamplerUniformBlockBinding)
2779{
2780 // This test matches binding slots of textures and buffers, requiring
2781 // the driver to give them distinct number spaces.
2782 // The expected result from this test is a red triangle, although
2783 // you can modify it to move the desired result around.
2784
2785 static const char *vertShaderText =
2786 "#version 140\n"
2787 "#extension GL_ARB_separate_shader_objects : enable\n"
2788 "#extension GL_ARB_shading_language_420pack : enable\n"
2789 "void main() {\n"
2790 " vec2 vertices[3];"
2791 " vertices[0] = vec2(-0.5, -0.5);\n"
2792 " vertices[1] = vec2( 0.5, -0.5);\n"
2793 " vertices[2] = vec2( 0.5, 0.5);\n"
2794 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2795 "}\n";
2796
2797 static const char *fragShaderText =
2798 "#version 430\n"
2799 "#extension GL_ARB_separate_shader_objects : enable\n"
2800 "#extension GL_ARB_shading_language_420pack : enable\n"
2801 "layout (binding = 0) uniform sampler2D surface0;\n"
2802 "layout (binding = 1) uniform sampler2D surface1;\n"
2803 "layout (binding = 2) uniform sampler2D surface2;\n"
2804 "layout (binding = 3) uniform sampler2D surface3;\n"
Chia-I Wuf8385062015-01-04 16:27:24 +08002805 "layout (std140, binding = 4) uniform redVal { vec4 red; };"
2806 "layout (std140, binding = 5) uniform greenVal { vec4 green; };"
2807 "layout (std140, binding = 6) uniform blueVal { vec4 blue; };"
2808 "layout (std140, binding = 7) uniform whiteVal { vec4 white; };"
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002809 "layout (location = 0) out vec4 outColor;\n"
2810 "void main() {\n"
2811 " outColor = red;// * vec4(0.00001);\n"
2812 " outColor += white * vec4(0.00001);\n"
2813 " outColor += textureLod(surface1, vec2(0.5), 0.0)* vec4(0.00001);\n"
2814 " outColor += textureLod(surface3, vec2(0.0), 0.0)* vec4(0.00001);\n"
2815 "}\n";
2816 ASSERT_NO_FATAL_FAILURE(InitState());
2817 ASSERT_NO_FATAL_FAILURE(InitViewport());
2818
2819 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2820 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2821
2822 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2823 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2824 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2825 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2826
2827 const int redCount = sizeof(redVals) / sizeof(float);
2828 const int greenCount = sizeof(greenVals) / sizeof(float);
2829 const int blueCount = sizeof(blueVals) / sizeof(float);
2830 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2831
2832 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002833 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002834 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002835 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002836
Tony Barbour2f421a02015-04-01 16:38:10 -06002837 uint32_t tex_colors[2] = { 0xff800000, 0xff800000 };
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002838 XglSamplerObj sampler0(m_device);
Tony Barbour2f421a02015-04-01 16:38:10 -06002839 XglTextureObj texture0(m_device, tex_colors); // Light Red
2840 tex_colors[0] = 0xff000080; tex_colors[1] = 0xff000080;
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002841 XglSamplerObj sampler2(m_device);
Tony Barbour2f421a02015-04-01 16:38:10 -06002842 XglTextureObj texture2(m_device, tex_colors); // Light Blue
2843 tex_colors[0] = 0xff008000; tex_colors[1] = 0xff008000;
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002844 XglSamplerObj sampler4(m_device);
Tony Barbour2f421a02015-04-01 16:38:10 -06002845 XglTextureObj texture4(m_device, tex_colors); // Light Green
2846 tex_colors[0] = 0xffff00ff; tex_colors[1] = 0xffff00ff;
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002847 XglSamplerObj sampler7(m_device);
Tony Barbour2f421a02015-04-01 16:38:10 -06002848 XglTextureObj texture7(m_device, tex_colors); // Red and Blue
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002849
2850 XglPipelineObj pipelineobj(m_device);
2851 pipelineobj.AddShader(&vs);
2852 pipelineobj.AddShader(&ps);
2853
2854 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08002855 descriptorSet.AppendSamplerTexture(&sampler0, &texture0);
2856 descriptorSet.AppendSamplerTexture(&sampler2, &texture2);
2857 descriptorSet.AppendSamplerTexture(&sampler4, &texture4);
2858 descriptorSet.AppendSamplerTexture(&sampler7, &texture7);
2859 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &redBuffer);
2860 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &greenBuffer);
2861 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &blueBuffer);
2862 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &whiteBuffer);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002863
2864 m_memoryRefManager.AddMemoryRef(&texture0);
2865 m_memoryRefManager.AddMemoryRef(&texture2);
2866 m_memoryRefManager.AddMemoryRef(&texture4);
2867 m_memoryRefManager.AddMemoryRef(&texture7);
2868
Tony Barbourdd4c9642015-01-09 12:55:14 -07002869 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06002870 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Tony Barbourdd4c9642015-01-09 12:55:14 -07002871 XglCommandBufferObj cmdBuffer(m_device);
2872 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002873
Jeremy Hayese0c3b222015-01-14 16:17:08 -07002874 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Tony Barbourdd4c9642015-01-09 12:55:14 -07002875
2876 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
2877
2878#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002879 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Tony Barbourdd4c9642015-01-09 12:55:14 -07002880 pDSDumpDot((char*)"triTest2.dot");
2881#endif
2882 // render triangle
2883 cmdBuffer.Draw(0, 3, 0, 1);
2884
2885 // finalize recording of the command buffer
2886 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06002887 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Tony Barbourdd4c9642015-01-09 12:55:14 -07002888
2889 for (int i = 0; i < m_renderTargetCount; i++)
2890 RecordImage(m_renderTargets[i]);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002891
2892}
2893
Cody Northrop02690bd2014-12-17 15:26:33 -07002894TEST_F(XglRenderTest, TriangleUniformBufferLayout)
2895{
2896 // This test populates a buffer with a variety of different data
2897 // types, then reads them out with a shader.
2898 // The expected result from this test is a green triangle
2899
2900 static const char *vertShaderText =
2901 "#version 140\n"
2902 "#extension GL_ARB_separate_shader_objects : enable\n"
2903 "#extension GL_ARB_shading_language_420pack : enable\n"
2904 "layout (std140, binding = 0) uniform mixedBuffer {\n"
2905 " vec4 fRed;\n"
2906 " vec4 fGreen;\n"
2907 " layout(row_major) mat4 worldToProj;\n"
2908 " layout(row_major) mat4 projToWorld;\n"
2909 " layout(row_major) mat4 worldToView;\n"
2910 " layout(row_major) mat4 viewToProj;\n"
2911 " layout(row_major) mat4 worldToShadow[4];\n"
2912 " float fZero;\n"
2913 " float fOne;\n"
2914 " float fTwo;\n"
2915 " float fThree;\n"
2916 " vec3 fZeroZeroZero;\n"
2917 " float fFour;\n"
2918 " vec3 fZeroZeroOne;\n"
2919 " float fFive;\n"
2920 " vec3 fZeroOneZero;\n"
2921 " float fSix;\n"
2922 " float fSeven;\n"
2923 " float fEight;\n"
2924 " float fNine;\n"
2925 " vec2 fZeroZero;\n"
2926 " vec2 fZeroOne;\n"
2927 " vec4 fBlue;\n"
2928 " vec2 fOneZero;\n"
2929 " vec2 fOneOne;\n"
2930 " vec3 fZeroOneOne;\n"
2931 " float fTen;\n"
2932 " float fEleven;\n"
2933 " float fTwelve;\n"
2934 " vec3 fOneZeroZero;\n"
2935 " vec4 uvOffsets[4];\n"
2936 "};\n"
2937 "layout (location = 0) out vec4 color;"
2938 "void main() {\n"
2939
2940 " vec4 right = vec4(0.0, 1.0, 0.0, 1.0);\n"
2941 " vec4 wrong = vec4(1.0, 0.0, 0.0, 1.0);\n"
2942 " \n"
2943
2944 // do some exact comparisons, even though we should
2945 // really have an epsilon involved.
2946 " vec4 outColor = right;\n"
2947 " if (fRed != vec4(1.0, 0.0, 0.0, 1.0))\n"
2948 " outColor = wrong;\n"
2949 " if (fGreen != vec4(0.0, 1.0, 0.0, 1.0))\n"
2950 " outColor = wrong;\n"
2951 " if (fBlue != vec4(0.0, 0.0, 1.0, 1.0))\n"
2952 " outColor = wrong;\n"
2953
2954 " color = outColor;\n"
2955
2956 // generic position stuff
2957 " vec2 vertices;\n"
2958 " int vertexSelector = gl_VertexID;\n"
2959 " if (vertexSelector == 0)\n"
2960 " vertices = vec2(-0.5, -0.5);\n"
2961 " else if (vertexSelector == 1)\n"
2962 " vertices = vec2( 0.5, -0.5);\n"
2963 " else if (vertexSelector == 2)\n"
2964 " vertices = vec2( 0.5, 0.5);\n"
2965 " else\n"
2966 " vertices = vec2( 0.0, 0.0);\n"
2967 " gl_Position = vec4(vertices, 0.0, 1.0);\n"
2968 "}\n";
2969
2970 static const char *fragShaderText =
2971 "#version 140\n"
2972 "#extension GL_ARB_separate_shader_objects : enable\n"
2973 "#extension GL_ARB_shading_language_420pack : enable\n"
2974 "layout (std140, binding = 0) uniform mixedBuffer {\n"
2975 " vec4 fRed;\n"
2976 " vec4 fGreen;\n"
2977 " layout(row_major) mat4 worldToProj;\n"
2978 " layout(row_major) mat4 projToWorld;\n"
2979 " layout(row_major) mat4 worldToView;\n"
2980 " layout(row_major) mat4 viewToProj;\n"
2981 " layout(row_major) mat4 worldToShadow[4];\n"
2982 " float fZero;\n"
2983 " float fOne;\n"
2984 " float fTwo;\n"
2985 " float fThree;\n"
2986 " vec3 fZeroZeroZero;\n"
2987 " float fFour;\n"
2988 " vec3 fZeroZeroOne;\n"
2989 " float fFive;\n"
2990 " vec3 fZeroOneZero;\n"
2991 " float fSix;\n"
2992 " float fSeven;\n"
2993 " float fEight;\n"
2994 " float fNine;\n"
2995 " vec2 fZeroZero;\n"
2996 " vec2 fZeroOne;\n"
2997 " vec4 fBlue;\n"
2998 " vec2 fOneZero;\n"
2999 " vec2 fOneOne;\n"
3000 " vec3 fZeroOneOne;\n"
3001 " float fTen;\n"
3002 " float fEleven;\n"
3003 " float fTwelve;\n"
3004 " vec3 fOneZeroZero;\n"
3005 " vec4 uvOffsets[4];\n"
3006 "};\n"
3007 "layout (location = 0) in vec4 color;\n"
3008 "void main() {\n"
3009 " vec4 right = vec4(0.0, 1.0, 0.0, 1.0);\n"
3010 " vec4 wrong = vec4(1.0, 0.0, 0.0, 1.0);\n"
3011 " \n"
3012
3013 // start with VS value to ensure it passed
3014 " vec4 outColor = color;\n"
3015
3016 // do some exact comparisons, even though we should
3017 // really have an epsilon involved.
3018 " if (fRed != vec4(1.0, 0.0, 0.0, 1.0))\n"
3019 " outColor = wrong;\n"
3020 " if (fGreen != vec4(0.0, 1.0, 0.0, 1.0))\n"
3021 " outColor = wrong;\n"
3022 " if (projToWorld[1] != vec4(0.0, 2.0, 0.0, 0.0))\n"
3023 " outColor = wrong;\n"
3024 " if (worldToShadow[2][1] != vec4(0.0, 7.0, 0.0, 0.0))\n"
3025 " outColor = wrong;\n"
3026 " if (fTwo != 2.0)\n"
3027 " outColor = wrong;\n"
3028 " if (fOneOne != vec2(1.0, 1.0))\n"
3029 " outColor = wrong;\n"
3030 " if (fTen != 10.0)\n"
3031 " outColor = wrong;\n"
3032 " if (uvOffsets[2] != vec4(0.9, 1.0, 1.1, 1.2))\n"
3033 " outColor = wrong;\n"
3034 " \n"
3035 " gl_FragColor = outColor;\n"
3036 "}\n";
3037
3038
3039 const float mixedVals[196] = { 1.0, 0.0, 0.0, 1.0, // vec4 fRed; // align
3040 0.0, 1.0, 0.0, 1.0, // vec4 fGreen; // align
3041 1.0, 0.0, 0.0, 1.0, // layout(row_major) mat4 worldToProj;
3042 0.0, 1.0, 0.0, 1.0, // align
3043 0.0, 0.0, 1.0, 1.0, // align
3044 0.0, 0.0, 0.0, 1.0, // align
3045 2.0, 0.0, 0.0, 2.0, // layout(row_major) mat4 projToWorld;
3046 0.0, 2.0, 0.0, 2.0, // align
3047 0.0, 0.0, 2.0, 2.0, // align
3048 0.0, 0.0, 0.0, 2.0, // align
3049 3.0, 0.0, 0.0, 3.0, // layout(row_major) mat4 worldToView;
3050 0.0, 3.0, 0.0, 3.0, // align
3051 0.0, 0.0, 3.0, 3.0, // align
3052 0.0, 0.0, 0.0, 3.0, // align
3053 4.0, 0.0, 0.0, 4.0, // layout(row_major) mat4 viewToProj;
3054 0.0, 4.0, 0.0, 4.0, // align
3055 0.0, 0.0, 4.0, 4.0, // align
3056 0.0, 0.0, 0.0, 4.0, // align
3057 5.0, 0.0, 0.0, 5.0, // layout(row_major) mat4 worldToShadow[4];
3058 0.0, 5.0, 0.0, 5.0, // align
3059 0.0, 0.0, 5.0, 5.0, // align
3060 0.0, 0.0, 0.0, 5.0, // align
3061 6.0, 0.0, 0.0, 6.0, // align
3062 0.0, 6.0, 0.0, 6.0, // align
3063 0.0, 0.0, 6.0, 6.0, // align
3064 0.0, 0.0, 0.0, 6.0, // align
3065 7.0, 0.0, 0.0, 7.0, // align
3066 0.0, 7.0, 0.0, 7.0, // align
3067 0.0, 0.0, 7.0, 7.0, // align
3068 0.0, 0.0, 0.0, 7.0, // align
3069 8.0, 0.0, 0.0, 8.0, // align
3070 0.0, 8.0, 0.0, 8.0, // align
3071 0.0, 0.0, 8.0, 8.0, // align
3072 0.0, 0.0, 0.0, 8.0, // align
3073 0.0, // float fZero; // align
3074 1.0, // float fOne; // pack
3075 2.0, // float fTwo; // pack
3076 3.0, // float fThree; // pack
3077 0.0, 0.0, 0.0, // vec3 fZeroZeroZero; // align
3078 4.0, // float fFour; // pack
3079 0.0, 0.0, 1.0, // vec3 fZeroZeroOne; // align
3080 5.0, // float fFive; // pack
3081 0.0, 1.0, 0.0, // vec3 fZeroOneZero; // align
3082 6.0, // float fSix; // pack
3083 7.0, // float fSeven; // align
3084 8.0, // float fEight; // pack
3085 9.0, // float fNine; // pack
3086 0.0, // BUFFER
3087 0.0, 0.0, // vec2 fZeroZero; // align
3088 0.0, 1.0, // vec2 fZeroOne; // pack
3089 0.0, 0.0, 1.0, 1.0, // vec4 fBlue; // align
3090 1.0, 0.0, // vec2 fOneZero; // align
3091 1.0, 1.0, // vec2 fOneOne; // pack
3092 0.0, 1.0, 1.0, // vec3 fZeroOneOne; // align
3093 10.0, // float fTen; // pack
3094 11.0, // float fEleven; // align
3095 12.0, // float fTwelve; // pack
3096 0.0, 0.0, // BUFFER
3097 1.0, 0.0, 0.0, // vec3 fOneZeroZero; // align
3098 0.0, // BUFFER
3099 0.1, 0.2, 0.3, 0.4, // vec4 uvOffsets[4];
3100 0.5, 0.6, 0.7, 0.8, // align
3101 0.9, 1.0, 1.1, 1.2, // align
3102 1.3, 1.4, 1.5, 1.6, // align
3103 };
3104
3105 ASSERT_NO_FATAL_FAILURE(InitState());
3106 ASSERT_NO_FATAL_FAILURE(InitViewport());
3107
3108 const int constCount = sizeof(mixedVals) / sizeof(float);
3109
3110 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
3111 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
3112
3113 XglConstantBufferObj mixedBuffer(m_device, constCount, sizeof(mixedVals[0]), (const void*) mixedVals);
Cody Northrop02690bd2014-12-17 15:26:33 -07003114
3115 XglPipelineObj pipelineobj(m_device);
3116 pipelineobj.AddShader(&vs);
3117 pipelineobj.AddShader(&ps);
3118
3119 XglDescriptorSetObj descriptorSet(m_device);
Chia-I Wuf8385062015-01-04 16:27:24 +08003120 descriptorSet.AppendBuffer(XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, &mixedBuffer);
Cody Northrop02690bd2014-12-17 15:26:33 -07003121
3122 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Mark Lobodzinski15427102015-02-18 16:38:17 -06003123 m_memoryRefManager.AddRTMemoryRefs(m_renderTargets, m_renderTargetCount);
Cody Northrop02690bd2014-12-17 15:26:33 -07003124 XglCommandBufferObj cmdBuffer(m_device);
3125 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
3126
Jeremy Hayese0c3b222015-01-14 16:17:08 -07003127 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(renderPass()));
Cody Northrop02690bd2014-12-17 15:26:33 -07003128
3129 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
3130
3131#ifdef DUMP_STATE_DOT
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06003132 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (char*)"drawStateDumpDotFile");
Cody Northrop02690bd2014-12-17 15:26:33 -07003133 pDSDumpDot((char*)"triTest2.dot");
3134#endif
3135 // render triangle
3136 cmdBuffer.Draw(0, 3, 0, 1);
3137
3138 // finalize recording of the command buffer
3139 cmdBuffer.EndCommandBuffer();
Mark Lobodzinski15427102015-02-18 16:38:17 -06003140 cmdBuffer.QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
Cody Northrop02690bd2014-12-17 15:26:33 -07003141
3142 for (int i = 0; i < m_renderTargetCount; i++)
3143 RecordImage(m_renderTargets[i]);
3144}
3145
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06003146int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06003147 int result;
3148
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06003149 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06003150 XglTestFramework::InitArgs(&argc, argv);
3151
Chia-I Wu7133fdc2014-12-15 23:57:34 +08003152 ::testing::AddGlobalTestEnvironment(new TestEnvironment);
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06003153
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06003154 result = RUN_ALL_TESTS();
3155
3156 XglTestFramework::Finish();
3157 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06003158}