blob: c5e05877e3c7bddb59c83da0430480e80c767fa7 [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
75#include "xgldevice.h"
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -060076#include "xglimage.h"
Chia-I Wu4115c892014-08-28 11:56:29 +080077#include "icd-bil.h"
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060078
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -060079#define GLM_FORCE_RADIANS
80#include "glm/glm.hpp"
81#include <glm/gtc/matrix_transform.hpp>
82
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060083#include "xglrenderframework.h"
Tobin Ehlis791a49c2014-11-10 12:29:12 -070084#ifdef DEBUG_CALLBACK
85XGL_VOID XGLAPI myDbgFunc(
86 XGL_DBG_MSG_TYPE msgType,
87 XGL_VALIDATION_LEVEL validationLevel,
88 XGL_BASE_OBJECT srcObject,
89 XGL_SIZE location,
90 XGL_INT msgCode,
91 const XGL_CHAR* pMsg,
92 XGL_VOID* pUserData)
93{
Tobin Ehlis3c26a542014-11-18 11:28:33 -070094 switch (msgType)
95 {
96 case XGL_DBG_MSG_WARNING:
97 printf("CALLBACK WARNING : %s\n", pMsg);
98 break;
99 case XGL_DBG_MSG_ERROR:
100 printf("CALLBACK ERROR : %s\n", pMsg);
101 break;
102 default:
103 printf("EATING Msg of type %u\n", msgType);
104 break;
105 }
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700106}
107#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700108
109
110#undef ASSERT_NO_FATAL_FAILURE
111#define ASSERT_NO_FATAL_FAILURE(x) x
112
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600113//--------------------------------------------------------------------------------------
114// Mesh and VertexFormat Data
115//--------------------------------------------------------------------------------------
116struct Vertex
117{
118 XGL_FLOAT posX, posY, posZ, posW; // Position data
119 XGL_FLOAT r, g, b, a; // Color
120};
121
122#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
123
124static const Vertex g_vbData[] =
125{
126 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
127 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
128 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
129 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
130 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
131 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
132
133 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
134 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
135 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
136 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
137 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
138 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
139
140 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
141 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
142 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
143 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
144 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
145 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
146
147 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
148 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
149 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
150 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
151 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
152 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
153
154 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
155 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
156 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
157 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
158 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
159 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
160
161 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
162 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
163 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
164 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
165 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
166 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
167};
168
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600169static const Vertex g_vb_solid_face_colors_Data[] =
170{
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 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
176 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
177
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 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
183 { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
184
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 { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
190 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
191
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 { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
197 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
198
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 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
204 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
205
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 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
211 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
212};
213
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600214class XglRenderTest : public XglRenderFramework
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600215{
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600216public:
Cody Northrop4e6b4762014-10-09 21:25:22 -0600217
Tony Barbourf43b6982014-11-25 13:18:32 -0700218 void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText);
219 void RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
220 XglConstantBufferObj constantBuffer);
221 void GenericDrawTriangleTest(XglPipelineObj pipelineobj, XglDescriptorSetObj descriptorSet, int numTris);
222 void QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs);
223
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600224 void ClearDepthStencil(XGL_FLOAT value);
225 void ClearRenderBuffer(XGL_UINT32 clear_color);
226 void InitDepthStencil();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600227 void GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600228 void XGLTriangleTest(const char *vertShaderText, const char *fragShaderText);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600229
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600230
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600231protected:
Cody Northrop350727b2014-10-06 15:42:00 -0600232 XGL_IMAGE m_texture;
233 XGL_IMAGE_VIEW m_textureView;
234 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
235 XGL_GPU_MEMORY m_textureMem;
236
237 XGL_SAMPLER m_sampler;
238
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600239 XGL_FORMAT m_depth_stencil_fmt;
240 XGL_IMAGE m_depthStencilImage;
241 XGL_GPU_MEMORY m_depthStencilMem;
242 XGL_DEPTH_STENCIL_VIEW m_depthStencilView;
Tony Barbourf43b6982014-11-25 13:18:32 -0700243 XglMemoryRefManager m_memoryRefManager;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600244
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600245
246 virtual void SetUp() {
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600247
248 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
249 this->app_info.pNext = NULL;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600250 this->app_info.pAppName = (const XGL_CHAR *) "render_tests";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600251 this->app_info.appVersion = 1;
252 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
253 this->app_info.engineVersion = 1;
254 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
255
Cody Northrop350727b2014-10-06 15:42:00 -0600256 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
257 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
258
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600259 InitFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600260 }
261
262 virtual void TearDown() {
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600263 // Clean up resources before we reset
264 ShutdownFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600265 }
266};
267
Tony Barbourf43b6982014-11-25 13:18:32 -0700268
269void XglRenderTest::GenericDrawTriangleTest(XglPipelineObj pipelineobj, XglDescriptorSetObj descriptorSet,int numTris)
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600270{
271 XGL_RESULT err = XGL_SUCCESS;
272
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600273 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600274 // Build command buffer
275 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
276 ASSERT_XGL_SUCCESS(err);
277
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600278 GenerateClearAndPrepareBufferCmds();
279 GenerateBindRenderTargetCmd();
Tobin Ehlis31446e52014-11-28 11:17:19 -0700280#ifdef DUMP_STATE_DOT
281 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
282 pDSDumpDot((char*)"triTest.dot");
283#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700284 GenerateBindStateAndPipelineCmds();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600285
Tony Barbourf43b6982014-11-25 13:18:32 -0700286 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
287 descriptorSet.BindCommandBuffer(m_cmdBuffer);
288
289 // render the triangle
290 xglCmdDraw( m_cmdBuffer, 0, 3*numTris, 0, 1 );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600291
292 // finalize recording of the command buffer
293 err = xglEndCommandBuffer( m_cmdBuffer );
294 ASSERT_XGL_SUCCESS( err );
Tony Barbourf43b6982014-11-25 13:18:32 -0700295}
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600296
Tony Barbourf43b6982014-11-25 13:18:32 -0700297void XglRenderTest::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
298{
299 XGL_RESULT err = XGL_SUCCESS;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600300
301 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700302 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, numMemRefs, memRefs, NULL );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600303 ASSERT_XGL_SUCCESS( err );
304
Chia-I Wuf34ac502014-08-27 14:58:05 +0800305 err = xglQueueWaitIdle( m_device->m_queue );
306 ASSERT_XGL_SUCCESS( err );
307
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600308 // Wait for work to finish before cleaning up.
309 xglDeviceWaitIdle(m_device->device());
310
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600311 RecordImage(m_renderTarget);
Chia-I Wuf070ec12014-08-30 23:58:36 +0800312
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600313}
314
Tony Barbourf43b6982014-11-25 13:18:32 -0700315void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600316{
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600317 XGL_RESULT err;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600318
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600319 ASSERT_NO_FATAL_FAILURE(InitState());
320 ASSERT_NO_FATAL_FAILURE(InitViewport());
321
Tony Barbourf43b6982014-11-25 13:18:32 -0700322 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
323 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600324
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600325
Tony Barbourf43b6982014-11-25 13:18:32 -0700326 XglPipelineObj pipelineobj(m_device);
327 pipelineobj.AddShader(&vs);
328 pipelineobj.AddShader(&ps);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600329
330 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
331
Tony Barbourf43b6982014-11-25 13:18:32 -0700332 // Create descriptor set
333 XglDescriptorSetObj descriptorSet(m_device);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600334
335 // Build command buffer
336 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
337 ASSERT_XGL_SUCCESS(err);
338
339 GenerateClearAndPrepareBufferCmds();
340 GenerateBindRenderTargetCmd();
Tony Barbourf43b6982014-11-25 13:18:32 -0700341 GenerateBindStateAndPipelineCmds();
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600342
Tobin Ehlis31446e52014-11-28 11:17:19 -0700343#ifdef DUMP_STATE_DOT
344 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
345 pDSDumpDot((char*)"triUniFS.dot");
346#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700347 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
348 descriptorSet.BindCommandBuffer(m_cmdBuffer);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600349
Tony Barbourf43b6982014-11-25 13:18:32 -0700350 // render the triangle
351 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600352
353 // finalize recording of the command buffer
354 err = xglEndCommandBuffer( m_cmdBuffer );
355 ASSERT_XGL_SUCCESS( err );
356
357 // this command buffer only uses the vertex buffer memory
358 m_numMemRefs = 0;
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600359
360 // submit the command buffer to the universal queue
361 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
362 ASSERT_XGL_SUCCESS( err );
363
364 err = xglQueueWaitIdle( m_device->m_queue );
365 ASSERT_XGL_SUCCESS( err );
366
367 // Wait for work to finish before cleaning up.
368 xglDeviceWaitIdle(m_device->device());
369
370 RecordImage(m_renderTarget);
371
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600372}
373
Tony Barbourf43b6982014-11-25 13:18:32 -0700374void XglRenderTest::RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
375 XglConstantBufferObj constantBuffer)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600376{
377 int i;
378 glm::mat4 MVP;
379 int matrixSize = sizeof(MVP);
380 XGL_RESULT err;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600381
382 for (i = 0; i < 8; i++) {
383 XGL_UINT8 *pData;
Tony Barbourf43b6982014-11-25 13:18:32 -0700384 err = xglMapMemory(constantBuffer.m_constantBufferMem, 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600385 ASSERT_XGL_SUCCESS(err);
386
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600387 Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
388 MVP = Projection * View * Model;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600389 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
390
Tony Barbourf43b6982014-11-25 13:18:32 -0700391 err = xglUnmapMemory(constantBuffer.m_constantBufferMem);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600392 ASSERT_XGL_SUCCESS(err);
393
394 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700395 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_memoryRefManager.GetNumRefs(), m_memoryRefManager.GetMemoryRefList(), NULL );
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600396 ASSERT_XGL_SUCCESS( err );
397
398 err = xglQueueWaitIdle( m_device->m_queue );
399 ASSERT_XGL_SUCCESS( err );
400
401 // Wait for work to finish before cleaning up.
402 xglDeviceWaitIdle(m_device->device());
403
404 RecordImage(m_renderTarget);
405 }
Cody Northrop7a1f0462014-10-10 14:49:36 -0600406}
407
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600408void dumpMatrix(const char *note, glm::mat4 MVP)
409{
410 int i,j;
411
412 printf("%s: \n", note);
413 for (i=0; i<4; i++) {
414 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
415 }
416 printf("\n");
417 fflush(stdout);
418}
419
420void dumpVec4(const char *note, glm::vec4 vector)
421{
422 printf("%s: \n", note);
423 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
424 printf("\n");
425 fflush(stdout);
426}
427
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600428void XglRenderTest::GenerateClearAndPrepareBufferCmds()
429{
430 XglRenderFramework::GenerateClearAndPrepareBufferCmds();
431
432 if (0) {
433// if (m_depthStencilImage) {
434 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
435 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
436 dsRange.baseMipLevel = 0;
437 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
438 dsRange.baseArraySlice = 0;
439 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
440
441 // prepare the depth buffer for clear
442 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
443 transitionToClear.image = m_depthStencilImage;
444 transitionToClear.oldState = m_depthStencilBinding.depthState;
445 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
446 transitionToClear.subresourceRange = dsRange;
447 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
448 m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
449
450 xglCmdClearDepthStencil(m_cmdBuffer, m_depthStencilImage, 1.0f, 0, 1, &dsRange);
451
452 // prepare depth buffer for rendering
453 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
454 transitionToRender.image = m_renderTarget->image();
455 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
456 transitionToRender.newState = m_depthStencilBinding.depthState;
457 transitionToRender.subresourceRange = dsRange;
458 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
459 m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
460 }
461}
462
Cody Northrop7a1f0462014-10-10 14:49:36 -0600463
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600464void XglRenderTest::ClearDepthStencil(XGL_FLOAT value)
465/* clear the buffer */
466{
467 XGL_RESULT err;
468 const uint16_t depth_value = (uint16_t) (value * 65535);
469 const XGL_INT tw = 128 / sizeof(uint16_t);
470 const XGL_INT th = 32;
471 XGL_INT i, j, w, h;
472 XGL_VOID *data;
473
474 w = (m_width + tw - 1) / tw;
475 h = (m_height + th - 1) / th;
476
477 err = xglMapMemory(m_depthStencilMem, 0, &data);
478 ASSERT_XGL_SUCCESS(err);
479
Tony Barbourf43b6982014-11-25 13:18:32 -0700480#ifdef DUMP_STATE_DOT
481 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
482 pDSDumpDot((char*)"triVtxFetchAndMVP.dot");
483#endif
484
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600485 for (i = 0; i < w * h; i++) {
486 uint16_t *tile = (uint16_t *) ((char *) data + 4096 * i);
487
488 for (j = 0; j < 2048; j++)
489 tile[j] = depth_value;
490 }
491
492 err = xglUnmapMemory(m_depthStencilMem);
493 ASSERT_XGL_SUCCESS(err);
494}
495
496void XglRenderTest::ClearRenderBuffer(XGL_UINT32 clear_color)
497/* clear the buffer */
498{
499 XGL_RESULT err;
500 const XGL_IMAGE_SUBRESOURCE sr = {
501 XGL_IMAGE_ASPECT_COLOR, 0, 0
502 };
503 XGL_SUBRESOURCE_LAYOUT sr_layout;
504 XGL_UINT data_size = sizeof(sr_layout);
505 XGL_VOID *ptr;
506
507 err = xglGetImageSubresourceInfo( m_renderTarget->image(),
508 &sr, XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
509 &data_size, &sr_layout);
510 ASSERT_XGL_SUCCESS( err );
511 ASSERT_EQ(data_size, sizeof(sr_layout));
512
513 err = m_renderTarget->MapMemory( &ptr );
514 ASSERT_XGL_SUCCESS( err );
515
516 ptr = (void *) ((char *) ptr + sr_layout.offset);
517
518 memset(ptr, clear_color, m_width * m_height *sizeof(XGL_UINT32));
519
520 err = m_renderTarget->UnmapMemory();
521 ASSERT_XGL_SUCCESS(err);
522}
523
524void XglRenderTest::InitDepthStencil()
525{
526 XGL_RESULT err;
527 XGL_IMAGE_CREATE_INFO image;
528 XGL_MEMORY_ALLOC_INFO mem_alloc;
529 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
530 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn6317c492014-11-21 11:33:20 -0700531 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600532
533 // Clean up default state created by framework
534 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
535
536 m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16;
537 m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS;
538
539 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
540 image.pNext = NULL;
541 image.imageType = XGL_IMAGE_2D;
542 image.format = m_depth_stencil_fmt;
543 image.extent.width = m_width;
544 image.extent.height = m_height;
545 image.extent.depth = 1;
546 image.mipLevels = 1;
547 image.arraySize = 1;
548 image.samples = 1;
549 image.tiling = XGL_OPTIMAL_TILING;
550 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
551 image.flags = 0;
552
553 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
554 mem_alloc.pNext = NULL;
555 mem_alloc.allocationSize = 0;
556 mem_alloc.alignment = 0;
557 mem_alloc.flags = 0;
558 mem_alloc.heapCount = 0;
559 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
560
561 /* create image */
562 err = xglCreateImage(device(), &image,
563 &m_depthStencilImage);
564 ASSERT_XGL_SUCCESS(err);
565
566 err = xglGetObjectInfo(m_depthStencilImage,
567 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
568 &mem_reqs_size, &mem_reqs);
569 ASSERT_XGL_SUCCESS(err);
570 ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs));
571
572 mem_alloc.allocationSize = mem_reqs.size;
573 mem_alloc.alignment = mem_reqs.alignment;
574 mem_alloc.heapCount = mem_reqs.heapCount;
575 memcpy(mem_alloc.heaps, mem_reqs.heaps,
576 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
577
578 /* allocate memory */
579 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem);
580 ASSERT_XGL_SUCCESS(err);
581
582 /* bind memory */
583 err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0);
584 ASSERT_XGL_SUCCESS(err);
585
586 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
587 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
588 depthStencil.depthTestEnable = XGL_TRUE;
589 depthStencil.depthWriteEnable = XGL_TRUE;
590 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
591 depthStencil.depthBoundsEnable = XGL_FALSE;
592 depthStencil.minDepth = 0.f;
593 depthStencil.maxDepth = 1.f;
594 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
595 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
596 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
597 depthStencil.back.stencilRef = 0x00;
598 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
599 depthStencil.front = depthStencil.back;
600
601 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
602 ASSERT_XGL_SUCCESS( err );
603
604 /* create image view */
605 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
606 view.pNext = NULL;
607 view.image = XGL_NULL_HANDLE;
608 view.mipLevel = 0;
609 view.baseArraySlice = 0;
610 view.arraySize = 1;
611 view.flags = 0;
612 view.image = m_depthStencilImage;
613 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
614 ASSERT_XGL_SUCCESS(err);
615
616 m_depthStencilBinding.view = m_depthStencilView;
617 m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
618 m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
619}
620
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600621struct xgltriangle_vs_uniform {
622 // Must start with MVP
623 XGL_FLOAT mvp[4][4];
624 XGL_FLOAT position[3][4];
625 XGL_FLOAT color[3][4];
626};
627
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600628void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600629{
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700630#ifdef DEBUG_CALLBACK
631 xglDbgRegisterMsgCallback(myDbgFunc, NULL);
632#endif
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600633 // Create identity matrix
634 int i;
635 struct xgltriangle_vs_uniform data;
636
637 glm::mat4 Projection = glm::mat4(1.0f);
638 glm::mat4 View = glm::mat4(1.0f);
639 glm::mat4 Model = glm::mat4(1.0f);
640 glm::mat4 MVP = Projection * View * Model;
641 const int matrixSize = sizeof(MVP);
642 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT);
643 memcpy(&data.mvp, &MVP[0][0], matrixSize);
644
645 static const Vertex tri_data[] =
646 {
647 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
648 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
649 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
650 };
651
652 for (i=0; i<3; i++) {
653 data.position[i][0] = tri_data[i].posX;
654 data.position[i][1] = tri_data[i].posY;
655 data.position[i][2] = tri_data[i].posZ;
656 data.position[i][3] = tri_data[i].posW;
657 data.color[i][0] = tri_data[i].r;
658 data.color[i][1] = tri_data[i].g;
659 data.color[i][2] = tri_data[i].b;
660 data.color[i][3] = tri_data[i].a;
661 }
662
Tony Barbourf43b6982014-11-25 13:18:32 -0700663 ASSERT_NO_FATAL_FAILURE(InitState());
664 ASSERT_NO_FATAL_FAILURE(InitViewport());
665
666 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data);
667
668 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
669 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
670 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
671
672 XglPipelineObj pipelineobj(m_device);
673 pipelineobj.AddShader(&vs);
674 pipelineobj.AddShader(&ps);
675
676 XglDescriptorSetObj descriptorSet(m_device);
677 descriptorSet.AttachMemoryView(&constantBuffer);
678 m_memoryRefManager.AddMemoryRef(&constantBuffer);
679
680 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
681 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
682
683 RotateTriangleVSUniform(Projection, View, Model, constantBuffer);
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700684#ifdef PRINT_OBJECTS
685 //XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)
686 OBJ_TRACK_GET_OBJECT_COUNT pObjTrackGetObjectCount = (OBJ_TRACK_GET_OBJECT_COUNT)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjectCount");
687 XGL_UINT64 numObjects = pObjTrackGetObjectCount(XGL_OBJECT_TYPE_ANY);
688 //OBJ_TRACK_GET_OBJECTS pGetObjsFunc = xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
689 printf("DEBUG : Number of Objects : %lu\n", numObjects);
690 OBJ_TRACK_GET_OBJECTS pObjTrackGetObjs = (OBJ_TRACK_GET_OBJECTS)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
691 OBJTRACK_NODE* pObjNodeArray = (OBJTRACK_NODE*)malloc(sizeof(OBJTRACK_NODE)*numObjects);
692 pObjTrackGetObjs(XGL_OBJECT_TYPE_ANY, numObjects, pObjNodeArray);
693 for (i=0; i < numObjects; i++) {
694 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);
695 }
696 free(pObjNodeArray);
697#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700698
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600699}
700
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600701TEST_F(XglRenderTest, XGLTriangle_FragColor)
702{
703 static const char *vertShaderText =
704 "#version 140\n"
705 "#extension GL_ARB_separate_shader_objects : enable\n"
706 "#extension GL_ARB_shading_language_420pack : enable\n"
707 "\n"
708 "layout(binding = 0) uniform buf {\n"
709 " mat4 MVP;\n"
710 " vec4 position[3];\n"
711 " vec4 color[3];\n"
712 "} ubuf;\n"
713 "\n"
714 "layout (location = 0) out vec4 outColor;\n"
715 "\n"
716 "void main() \n"
717 "{\n"
718 " outColor = ubuf.color[gl_VertexID];\n"
719 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
720 "}\n";
721
722 static const char *fragShaderText =
723 "#version 140\n"
724 "#extension GL_ARB_separate_shader_objects : enable\n"
725 "#extension GL_ARB_shading_language_420pack : enable\n"
726 "\n"
727 "layout (location = 0) in vec4 inColor;\n"
728 "\n"
729 "void main()\n"
730 "{\n"
731 " gl_FragColor = inColor;\n"
732 "}\n";
733
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600734 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600735 XGLTriangleTest(vertShaderText, fragShaderText);
736}
737
738TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
739{
740 static const char *vertShaderText =
741 "#version 140\n"
742 "#extension GL_ARB_separate_shader_objects : enable\n"
743 "#extension GL_ARB_shading_language_420pack : enable\n"
744 "\n"
745 "layout(binding = 0) uniform buf {\n"
746 " mat4 MVP;\n"
747 " vec4 position[3];\n"
748 " vec4 color[3];\n"
749 "} ubuf;\n"
750 "\n"
751 "layout (location = 0) out vec4 outColor;\n"
752 "\n"
753 "void main() \n"
754 "{\n"
755 " outColor = ubuf.color[gl_VertexID];\n"
756 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
757 "}\n";
758
759 static const char *fragShaderText =
760 "#version 140\n"
761 "#extension GL_ARB_separate_shader_objects : enable\n"
762 "#extension GL_ARB_shading_language_420pack : enable\n"
763 "\n"
764 "layout (location = 0) in vec4 inColor;\n"
765 "layout (location = 0) out vec4 outColor;\n"
766 "\n"
767 "void main()\n"
768 "{\n"
769 " outColor = inColor;\n"
770 "}\n";
771
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600772 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 -0600773
774 XGLTriangleTest(vertShaderText, fragShaderText);
775}
776
Tony Barbourf43b6982014-11-25 13:18:32 -0700777TEST_F(XglRenderTest, BIL_XGLTriangle)
778{
779 bool saved_use_bil = XglTestFramework::m_use_bil;
780
781 static const char *vertShaderText =
782 "#version 140\n"
783 "#extension GL_ARB_separate_shader_objects : enable\n"
784 "#extension GL_ARB_shading_language_420pack : enable\n"
785 "\n"
786 "layout(binding = 0) uniform buf {\n"
787 " mat4 MVP;\n"
788 " vec4 position[3];\n"
789 " vec4 color[3];\n"
790 "} ubuf;\n"
791 "\n"
792 "layout (location = 0) out vec4 outColor;\n"
793 "\n"
794 "void main() \n"
795 "{\n"
796 " outColor = ubuf.color[gl_VertexID];\n"
797 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
798 "}\n";
799
800 static const char *fragShaderText =
801 "#version 140\n"
802 "#extension GL_ARB_separate_shader_objects : enable\n"
803 "#extension GL_ARB_shading_language_420pack : enable\n"
804 "\n"
805 "layout (location = 0) in vec4 inColor;\n"
806 "\n"
807 "void main()\n"
808 "{\n"
809 " gl_FragColor = inColor;\n"
810 "}\n";
811
812 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver.");
813
814 XglTestFramework::m_use_bil = true;
815
816 XGLTriangleTest(vertShaderText, fragShaderText);
817
818 XglTestFramework::m_use_bil = saved_use_bil;
819}
820
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600821TEST_F(XglRenderTest, GreenTriangle)
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600822{
823 static const char *vertShaderText =
824 "#version 130\n"
825 "vec2 vertices[3];\n"
826 "void main() {\n"
827 " vertices[0] = vec2(-1.0, -1.0);\n"
828 " vertices[1] = vec2( 1.0, -1.0);\n"
829 " vertices[2] = vec2( 0.0, 1.0);\n"
830 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
831 "}\n";
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600832
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600833 static const char *fragShaderText =
834 "#version 130\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600835 "void main() {\n"
Steve K10b32512014-10-10 08:54:29 -0600836 " gl_FragColor = vec4(0,1,0,1);\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600837 "}\n";
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600838
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600839 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
840
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600841 DrawTriangleTest(vertShaderText, fragShaderText);
842}
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600843
Tony Barbourf43b6982014-11-25 13:18:32 -0700844TEST_F(XglRenderTest, BIL_GreenTriangle)
845{
846 bool saved_use_bil = XglTestFramework::m_use_bil;
847
848 static const char *vertShaderText =
849 "#version 130\n"
850 "vec2 vertices[3];\n"
851 "void main() {\n"
852 " vertices[0] = vec2(-1.0, -1.0);\n"
853 " vertices[1] = vec2( 1.0, -1.0);\n"
854 " vertices[2] = vec2( 0.0, 1.0);\n"
855 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
856 "}\n";
857
858 static const char *fragShaderText =
859 "#version 130\n"
860 "void main() {\n"
861 " gl_FragColor = vec4(0,1,0,1);\n"
862 "}\n";
863
864 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver.");
865
866 XglTestFramework::m_use_bil = true;
867 DrawTriangleTest(vertShaderText, fragShaderText);
868 XglTestFramework::m_use_bil = saved_use_bil;
869}
870
871TEST_F(XglRenderTest, YellowTriangle)
872{
873 static const char *vertShaderText =
874 "#version 130\n"
875 "void main() {\n"
876 " vec2 vertices[3];"
877 " vertices[0] = vec2(-0.5, -0.5);\n"
878 " vertices[1] = vec2( 0.5, -0.5);\n"
879 " vertices[2] = vec2( 0.5, 0.5);\n"
880 " vec4 colors[3];\n"
881 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
882 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
883 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
884 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
885 "}\n";
886
887 static const char *fragShaderText =
888 "#version 130\n"
889 "void main() {\n"
890 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
891 "}\n";
892
893 DrawTriangleTest(vertShaderText, fragShaderText);
894}
895
896TEST_F(XglRenderTest, TriangleTwoFSUniforms)
897{
898 static const char *vertShaderText =
899 "#version 130\n"
900 "out vec4 color;\n"
901 "out vec4 scale;\n"
902 "out vec2 samplePos;\n"
903 "void main() {\n"
904 " vec2 vertices[3];"
905 " vertices[0] = vec2(-0.5, -0.5);\n"
906 " vertices[1] = vec2( 0.5, -0.5);\n"
907 " vertices[2] = vec2( 0.5, 0.5);\n"
908 " vec4 colors[3];\n"
909 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
910 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
911 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
912 " color = colors[gl_VertexID % 3];\n"
913 " vec2 positions[3];"
914 " positions[0] = vec2( 0.0, 0.0);\n"
915 " positions[1] = vec2( 1.0, 0.0);\n"
916 " positions[2] = vec2( 1.0, 1.0);\n"
917 " scale = vec4(0.0, 0.0, 0.0, 0.0);\n"
918 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
919 "}\n";
920
921
922 static const char *fragShaderText =
923 "#version 430\n"
924 "in vec4 color;\n"
925 "in vec4 scale;\n"
926 "uniform vec4 foo;\n"
927 "uniform vec4 bar;\n"
928 "void main() {\n"
929 // by default, with no location or blocks
930 // the compiler will read them from buffer
931 // in reverse order of first use in shader
932 // The buffer contains red, followed by blue,
933 // so foo should be blue, bar should be red
934 " gl_FragColor = color * scale * foo * bar + foo;\n"
935 "}\n";
936
937 ASSERT_NO_FATAL_FAILURE(InitState());
938 ASSERT_NO_FATAL_FAILURE(InitViewport());
939
940 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
941 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
942
943 const int constantCount = 8;
944 const float constants[constantCount] = { 1.0, 0.0, 0.0, 1.0,
945 0.0, 0.0, 1.0, 1.0 };
946 XglConstantBufferObj constantBuffer(m_device,constantCount, sizeof(constants[0]), (const void*) constants);
947 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
948
949 XglPipelineObj pipelineobj(m_device);
950 pipelineobj.AddShader(&vs);
951 pipelineobj.AddShader(&ps);
952
953 XglDescriptorSetObj descriptorSet(m_device);
954 descriptorSet.AttachMemoryView(&constantBuffer);
955
956 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
957 QueueCommandBuffer(NULL, 0);
958
959}
960
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600961TEST_F(XglRenderTest, TriangleWithVertexFetch)
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600962{
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600963 static const char *vertShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700964 "#version 130\n"
965 //XYZ1( -1, -1, -1 )
966 "in vec4 pos;\n"
967 //XYZ1( 0.f, 0.f, 0.f )
968 "in vec4 inColor;\n"
969 "out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600970 "void main() {\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -0600971 " outColor = inColor;\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600972 " gl_Position = pos;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600973 "}\n";
974
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600975
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600976 static const char *fragShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700977 "#version 430\n"
978 "in vec4 color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600979 "void main() {\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600980 " gl_FragColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600981 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600982
Tony Barbourf43b6982014-11-25 13:18:32 -0700983
984
985 ASSERT_NO_FATAL_FAILURE(InitState());
986 ASSERT_NO_FATAL_FAILURE(InitViewport());
987
988 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
989 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
990
991 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
992 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
993
994 XglPipelineObj pipelineobj(m_device);
995 pipelineobj.AddShader(&vs);
996 pipelineobj.AddShader(&ps);
997
998 XglDescriptorSetObj descriptorSet(m_device);
999 descriptorSet.AttachMemoryView(&meshBuffer);
1000
1001 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1002 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1003 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1004 };
1005
1006 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1007 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1008 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1009 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1010 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1011 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1012 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1013 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1014 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1015
1016 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1017 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1018 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1019
1020 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1021 QueueCommandBuffer(NULL, 0);
1022
Tobin Ehlis34e0e442014-10-07 14:41:29 -06001023}
1024
GregF6bef1212014-12-02 15:41:44 -07001025TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1026{
1027 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001028
GregF6bef1212014-12-02 15:41:44 -07001029 static const char *vertShaderText =
1030 "#version 140\n"
1031 "#extension GL_ARB_separate_shader_objects : enable\n"
1032 "#extension GL_ARB_shading_language_420pack : enable\n"
1033 "layout (location = 0) in vec4 pos;\n"
1034 "layout (location = 0) out vec4 outColor;\n"
1035 "layout (location = 1) out vec4 outColor2;\n"
1036 "void main() {\n"
1037 " gl_Position = pos;\n"
1038 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1039 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1040 "}\n";
1041
1042 static const char *fragShaderText =
1043 //"#version 140\n"
1044 "#version 330\n"
1045 "#extension GL_ARB_separate_shader_objects : enable\n"
1046 "#extension GL_ARB_shading_language_420pack : enable\n"
1047 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1048 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1049 "layout (location = 0) in vec4 color;\n"
1050 "layout (location = 1) in vec4 color2;\n"
1051 "void main() {\n"
1052 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1053 " float dist_squared = dot(pos, pos);\n"
1054 " gl_FragColor = (dist_squared < 400.0)\n"
1055 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1056 " : color2;\n"
1057 "}\n";
1058
1059 ASSERT_NO_FATAL_FAILURE(InitState());
1060 ASSERT_NO_FATAL_FAILURE(InitViewport());
1061
1062 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1063 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1064
1065 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1066 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1067
1068 XglPipelineObj pipelineobj(m_device);
1069 pipelineobj.AddShader(&vs);
1070 pipelineobj.AddShader(&ps);
1071
1072 XglDescriptorSetObj descriptorSet(m_device);
1073 descriptorSet.AttachMemoryView(&meshBuffer);
1074
1075 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1076 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1077 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1078 };
1079
1080 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1081 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1082 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1083 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1084 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1085 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1086 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1087 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1088 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1089
1090 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1091 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1092 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1093
1094 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1095 QueueCommandBuffer(NULL, 0);
1096
1097}
1098
1099TEST_F(XglRenderTest, RedCirclesonBlue)
1100{
1101 // This tests that we correctly handle unread fragment inputs
1102
1103 static const char *vertShaderText =
1104 "#version 140\n"
1105 "#extension GL_ARB_separate_shader_objects : enable\n"
1106 "#extension GL_ARB_shading_language_420pack : enable\n"
1107 "layout (location = 0) in vec4 pos;\n"
1108 "layout (location = 0) out vec4 outColor;\n"
1109 "layout (location = 1) out vec4 outColor2;\n"
1110 "void main() {\n"
1111 " gl_Position = pos;\n"
1112 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1113 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1114 "}\n";
1115
1116 static const char *fragShaderText =
1117 //"#version 140\n"
1118 "#version 330\n"
1119 "#extension GL_ARB_separate_shader_objects : enable\n"
1120 "#extension GL_ARB_shading_language_420pack : enable\n"
1121 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1122 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1123 "layout (location = 0) in vec4 color;\n"
1124 "layout (location = 1) in vec4 color2;\n"
1125 "void main() {\n"
1126 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1127 " float dist_squared = dot(pos, pos);\n"
1128 " gl_FragColor = (dist_squared < 400.0)\n"
1129 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1130 " : color2;\n"
1131 "}\n";
1132
1133 ASSERT_NO_FATAL_FAILURE(InitState());
1134 ASSERT_NO_FATAL_FAILURE(InitViewport());
1135
1136 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1137 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1138
1139 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1140 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1141
1142 XglPipelineObj pipelineobj(m_device);
1143 pipelineobj.AddShader(&vs);
1144 pipelineobj.AddShader(&ps);
1145
1146 XglDescriptorSetObj descriptorSet(m_device);
1147 descriptorSet.AttachMemoryView(&meshBuffer);
1148
1149 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1150 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1151 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1152 };
1153
1154 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1155 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1156 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1157 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1158 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1159 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1160 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1161 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1162 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1163
1164 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1165 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1166 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1167
1168 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1169 QueueCommandBuffer(NULL, 0);
1170
1171}
1172
1173TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1174{
1175 // This tests reading gl_ClipDistance from FS
1176
1177 static const char *vertShaderText =
1178 "#version 330\n"
1179 "#extension GL_ARB_separate_shader_objects : enable\n"
1180 "#extension GL_ARB_shading_language_420pack : enable\n"
1181 "out gl_PerVertex {\n"
1182 " vec4 gl_Position;\n"
1183 " float gl_ClipDistance[1];\n"
1184 "};\n"
1185 "layout (location = 0) in vec4 pos;\n"
1186 "layout (location = 0) out vec4 outColor;\n"
1187 "layout (location = 1) out vec4 outColor2;\n"
1188 "void main() {\n"
1189 " gl_Position = pos;\n"
1190 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1191 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1192 " float dists[3];\n"
1193 " dists[0] = 0.0;\n"
1194 " dists[1] = 1.0;\n"
1195 " dists[2] = 1.0;\n"
1196 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1197 "}\n";
1198
1199
1200 static const char *fragShaderText =
1201 //"#version 140\n"
1202 "#version 330\n"
1203 "#extension GL_ARB_separate_shader_objects : enable\n"
1204 "#extension GL_ARB_shading_language_420pack : enable\n"
1205 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1206 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1207 "layout (location = 0) in vec4 color;\n"
1208 "layout (location = 1) in vec4 color2;\n"
1209 "void main() {\n"
1210 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1211 " float dist_squared = dot(pos, pos);\n"
1212 " gl_FragColor = (dist_squared < 400.0)\n"
1213 " ? color * gl_ClipDistance[0]\n"
1214 " : color2;\n"
1215 "}\n";
1216
1217 ASSERT_NO_FATAL_FAILURE(InitState());
1218 ASSERT_NO_FATAL_FAILURE(InitViewport());
1219
1220 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1221 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1222
1223 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1224 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1225
1226 XglPipelineObj pipelineobj(m_device);
1227 pipelineobj.AddShader(&vs);
1228 pipelineobj.AddShader(&ps);
1229
1230 XglDescriptorSetObj descriptorSet(m_device);
1231 descriptorSet.AttachMemoryView(&meshBuffer);
1232
1233 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1234 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1235 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1236 };
1237
1238 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1239 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1240 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1241 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1242 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1243 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1244 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1245 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1246 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1247
1248 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1249 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1250 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1251
1252 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1253 QueueCommandBuffer(NULL, 0);
1254
1255}
Tony Barbourf43b6982014-11-25 13:18:32 -07001256
GregF7a23c792014-12-02 17:19:34 -07001257TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1258{
1259 static const char *vertShaderText =
1260 "#version 140\n"
1261 "#extension GL_ARB_separate_shader_objects : enable\n"
1262 "#extension GL_ARB_shading_language_420pack : enable\n"
1263 "layout (location = 0) in vec4 pos;\n"
1264 "layout (location = 0) out vec4 outColor;\n"
1265 "layout (location = 1) out vec4 outColor2;\n"
1266 "void main() {\n"
1267 " gl_Position = pos;\n"
1268 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1269 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1270 "}\n";
1271
1272
1273 static const char *fragShaderText =
1274 //"#version 140\n"
1275 "#version 330\n"
1276 "#extension GL_ARB_separate_shader_objects : enable\n"
1277 "#extension GL_ARB_shading_language_420pack : enable\n"
1278 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1279 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1280 "layout (location = 0) in vec4 color;\n"
1281 "layout (location = 1) in vec4 color2;\n"
1282 "void main() {\n"
1283 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1284 " float dist_squared = dot(pos, pos);\n"
1285 " if (dist_squared < 100.0)\n"
1286 " discard;\n"
1287 " gl_FragColor = (dist_squared < 400.0)\n"
1288 " ? color\n"
1289 " : color2;\n"
1290 "}\n";
1291
1292 ASSERT_NO_FATAL_FAILURE(InitState());
1293 ASSERT_NO_FATAL_FAILURE(InitViewport());
1294
1295 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1296 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1297
1298 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1299 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1300
1301 XglPipelineObj pipelineobj(m_device);
1302 pipelineobj.AddShader(&vs);
1303 pipelineobj.AddShader(&ps);
1304
1305 XglDescriptorSetObj descriptorSet(m_device);
1306 descriptorSet.AttachMemoryView(&meshBuffer);
1307
1308 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1309 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1310 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1311 };
1312
1313 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1314 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1315 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1316 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1317 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1318 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1319 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1320 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1321 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1322
1323 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1324 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1325 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1326
1327 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1328 QueueCommandBuffer(NULL, 0);
1329
1330}
1331
1332
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001333TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001334{
1335 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001336 "#version 140\n"
1337 "#extension GL_ARB_separate_shader_objects : enable\n"
1338 "#extension GL_ARB_shading_language_420pack : enable\n"
1339 "\n"
1340 "layout(binding = 0) uniform buf {\n"
1341 " mat4 MVP;\n"
1342 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001343 "void main() {\n"
1344 " vec2 vertices[3];"
1345 " vertices[0] = vec2(-0.5, -0.5);\n"
1346 " vertices[1] = vec2( 0.5, -0.5);\n"
1347 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001348 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001349 "}\n";
1350
1351 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001352 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001353 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001354 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001355 "}\n";
1356
Tony Barbourf43b6982014-11-25 13:18:32 -07001357 ASSERT_NO_FATAL_FAILURE(InitState());
1358 ASSERT_NO_FATAL_FAILURE(InitViewport());
1359
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001360 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001361 glm::mat4 Projection = glm::mat4(1.0f);
1362 glm::mat4 View = glm::mat4(1.0f);
1363 glm::mat4 Model = glm::mat4(1.0f);
1364 glm::mat4 MVP = Projection * View * Model;
1365 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1366
Tony Barbourf43b6982014-11-25 13:18:32 -07001367 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1368 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1369 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001370
Tony Barbourf43b6982014-11-25 13:18:32 -07001371 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1372
1373 XglPipelineObj pipelineobj(m_device);
1374 pipelineobj.AddShader(&vs);
1375 pipelineobj.AddShader(&ps);
1376
1377 // Create descriptor set and attach the constant buffer to it
1378 XglDescriptorSetObj descriptorSet(m_device);
1379 descriptorSet.AttachMemoryView(&MVPBuffer);
1380
1381 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1382
1383 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1384 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1385
1386 RotateTriangleVSUniform(Projection, View, Model, MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001387}
1388
Tony Barbourf43b6982014-11-25 13:18:32 -07001389TEST_F(XglRenderTest, MixTriangle)
1390{
1391 // This tests location applied to varyings. Notice that we have switched foo
1392 // and bar in the FS. The triangle should be blended with red, green and blue
1393 // corners.
1394 static const char *vertShaderText =
1395 "#version 140\n"
1396 "#extension GL_ARB_separate_shader_objects : enable\n"
1397 "#extension GL_ARB_shading_language_420pack : enable\n"
1398 "layout (location=0) out vec4 bar;\n"
1399 "layout (location=1) out vec4 foo;\n"
1400 "layout (location=2) out float scale;\n"
1401 "vec2 vertices[3];\n"
1402 "void main() {\n"
1403 " vertices[0] = vec2(-1.0, -1.0);\n"
1404 " vertices[1] = vec2( 1.0, -1.0);\n"
1405 " vertices[2] = vec2( 0.0, 1.0);\n"
1406 "vec4 colors[3];\n"
1407 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1408 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1409 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1410 " foo = colors[gl_VertexID % 3];\n"
1411 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1412 " scale = 1.0;\n"
1413 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1414 "}\n";
1415
1416 static const char *fragShaderText =
1417 "#version 140\n"
1418 "#extension GL_ARB_separate_shader_objects : enable\n"
1419 "#extension GL_ARB_shading_language_420pack : enable\n"
1420 "layout (location = 1) in vec4 bar;\n"
1421 "layout (location = 0) in vec4 foo;\n"
1422 "layout (location = 2) in float scale;\n"
1423 "void main() {\n"
1424 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1425 "}\n";
1426
1427 ASSERT_NO_FATAL_FAILURE(InitState());
1428 ASSERT_NO_FATAL_FAILURE(InitViewport());
1429
1430 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1431 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1432
1433 XglPipelineObj pipelineobj(m_device);
1434 pipelineobj.AddShader(&vs);
1435 pipelineobj.AddShader(&ps);
1436
1437 XglDescriptorSetObj descriptorSet(m_device);
1438
1439 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1440 QueueCommandBuffer(NULL, 0);
1441}
1442
1443TEST_F(XglRenderTest, TriVertFetchAndVertID)
1444{
1445 // This tests that attributes work in the presence of gl_VertexID
1446
1447 static const char *vertShaderText =
1448 "#version 140\n"
1449 "#extension GL_ARB_separate_shader_objects : enable\n"
1450 "#extension GL_ARB_shading_language_420pack : enable\n"
1451 //XYZ1( -1, -1, -1 )
1452 "layout (location = 0) in vec4 pos;\n"
1453 //XYZ1( 0.f, 0.f, 0.f )
1454 "layout (location = 1) in vec4 inColor;\n"
1455 "layout (location = 0) out vec4 outColor;\n"
1456 "void main() {\n"
1457 " outColor = inColor;\n"
1458 " vec4 vertices[3];"
1459 " vertices[gl_VertexID % 3] = pos;\n"
1460 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1461 "}\n";
1462
1463
1464 static const char *fragShaderText =
1465 "#version 140\n"
1466 "#extension GL_ARB_separate_shader_objects : enable\n"
1467 "#extension GL_ARB_shading_language_420pack : enable\n"
1468 "layout (location = 0) in vec4 color;\n"
1469 "void main() {\n"
1470 " gl_FragColor = color;\n"
1471 "}\n";
1472
1473 ASSERT_NO_FATAL_FAILURE(InitState());
1474 ASSERT_NO_FATAL_FAILURE(InitViewport());
1475
1476 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1477 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1478
1479 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1480 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1481
1482 XglPipelineObj pipelineobj(m_device);
1483 pipelineobj.AddShader(&vs);
1484 pipelineobj.AddShader(&ps);
1485
1486 XglDescriptorSetObj descriptorSet(m_device);
1487 descriptorSet.AttachMemoryView(&meshBuffer);
1488
1489 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1490 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1491 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1492 };
1493
1494 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1495 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1496 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1497 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1498 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1499 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1500 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1501 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1502 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1503
1504 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1505 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1506 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1507
1508 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1509 QueueCommandBuffer(NULL, 0);
1510}
1511
1512TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1513{
1514 // This tests that attributes work in the presence of gl_VertexID
1515 // and a dead attribute in position 0. Draws a triangle with yellow,
1516 // red and green corners, starting at top and going clockwise.
1517
1518 static const char *vertShaderText =
1519 "#version 140\n"
1520 "#extension GL_ARB_separate_shader_objects : enable\n"
1521 "#extension GL_ARB_shading_language_420pack : enable\n"
1522 //XYZ1( -1, -1, -1 )
1523 "layout (location = 0) in vec4 pos;\n"
1524 //XYZ1( 0.f, 0.f, 0.f )
1525 "layout (location = 1) in vec4 inColor;\n"
1526 "layout (location = 0) out vec4 outColor;\n"
1527 "void main() {\n"
1528 " outColor = inColor;\n"
1529 " vec2 vertices[3];"
1530 " vertices[0] = vec2(-1.0, -1.0);\n"
1531 " vertices[1] = vec2( 1.0, -1.0);\n"
1532 " vertices[2] = vec2( 0.0, 1.0);\n"
1533 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1534 "}\n";
1535
1536
1537 static const char *fragShaderText =
1538 "#version 140\n"
1539 "#extension GL_ARB_separate_shader_objects : enable\n"
1540 "#extension GL_ARB_shading_language_420pack : enable\n"
1541 "layout (location = 0) in vec4 color;\n"
1542 "void main() {\n"
1543 " gl_FragColor = color;\n"
1544 "}\n";
1545
1546 ASSERT_NO_FATAL_FAILURE(InitState());
1547 ASSERT_NO_FATAL_FAILURE(InitViewport());
1548
1549 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1550 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1551
1552 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1553 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1554
1555 XglPipelineObj pipelineobj(m_device);
1556 pipelineobj.AddShader(&vs);
1557 pipelineobj.AddShader(&ps);
1558
1559 XglDescriptorSetObj descriptorSet(m_device);
1560 descriptorSet.AttachMemoryView(&meshBuffer);
1561
1562 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1563 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1564 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1565 };
1566
1567 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1568 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1569 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1570 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1571 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1572 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1573 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1574 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1575 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1576
1577 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1578 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1579 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1580
1581 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1582 QueueCommandBuffer(NULL, 0);
1583
1584}
1585
1586TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001587{
1588 static const char *vertShaderText =
1589 "#version 140\n"
1590 "layout (std140) uniform bufferVals {\n"
1591 " mat4 mvp;\n"
1592 "} myBufferVals;\n"
1593 "in vec4 pos;\n"
1594 "in vec4 inColor;\n"
1595 "out vec4 outColor;\n"
1596 "void main() {\n"
1597 " outColor = inColor;\n"
1598 " gl_Position = myBufferVals.mvp * pos;\n"
1599 "}\n";
1600
1601 static const char *fragShaderText =
1602 "#version 130\n"
1603 "in vec4 color;\n"
1604 "void main() {\n"
1605 " gl_FragColor = color;\n"
1606 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001607 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001608
Tony Barbourf43b6982014-11-25 13:18:32 -07001609 glm::mat4 View = glm::lookAt(
1610 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1611 glm::vec3(0,0,0), // and looks at the origin
1612 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1613 );
1614
1615 glm::mat4 Model = glm::mat4(1.0f);
1616
1617 glm::mat4 MVP = Projection * View * Model;
1618
1619 ASSERT_NO_FATAL_FAILURE(InitState());
1620 ASSERT_NO_FATAL_FAILURE(InitViewport());
1621 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1622
1623 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1624 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1625
1626 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1627
1628 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1629 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1630 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1631
1632 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1633
1634 XglPipelineObj pipelineobj(m_device);
1635 pipelineobj.AddShader(&vs);
1636 pipelineobj.AddShader(&ps);
1637
1638 XglDescriptorSetObj descriptorSet(m_device);
1639 descriptorSet.AttachMemoryView(&MVPBuffer);
1640
1641 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1642 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1643
1644 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1645 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1646 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1647 };
1648
1649 // this is the current description of g_vbData
1650 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1651 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1652 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1653 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1654 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1655 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1656 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1657 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1658 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1659
1660 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1661 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1662 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1663
1664 ClearDepthStencil(1.0f);
1665 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
1666
1667 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1668
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001669}
1670
Tony Barbourf43b6982014-11-25 13:18:32 -07001671TEST_F(XglRenderTest, VSTexture)
1672{
1673 // The expected result from this test is a green and red triangle;
1674 // one red vertex on the left, two green vertices on the right.
1675 static const char *vertShaderText =
1676 "#version 130\n"
1677 "out vec4 texColor;\n"
1678 "uniform sampler2D surface;\n"
1679 "void main() {\n"
1680 " vec2 vertices[3];"
1681 " vertices[0] = vec2(-0.5, -0.5);\n"
1682 " vertices[1] = vec2( 0.5, -0.5);\n"
1683 " vertices[2] = vec2( 0.5, 0.5);\n"
1684 " vec2 positions[3];"
1685 " positions[0] = vec2( 0.0, 0.0);\n"
1686 " positions[1] = vec2( 0.25, 0.1);\n"
1687 " positions[2] = vec2( 0.1, 0.25);\n"
1688 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1689 " texColor = textureLod(surface, samplePos, 0.0);\n"
1690 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1691 "}\n";
1692
1693 static const char *fragShaderText =
1694 "#version 130\n"
1695 "in vec4 texColor;\n"
1696 "void main() {\n"
1697 " gl_FragColor = texColor;\n"
1698 "}\n";
1699
1700 ASSERT_NO_FATAL_FAILURE(InitState());
1701 ASSERT_NO_FATAL_FAILURE(InitViewport());
1702
1703 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1704 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1705 XglSamplerObj sampler(m_device);
1706 XglTextureObj texture(m_device);
1707
1708 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1709 vs.BindShaderEntitySlotToSampler(0, &sampler);
1710
1711 XglPipelineObj pipelineobj(m_device);
1712 pipelineobj.AddShader(&vs);
1713 pipelineobj.AddShader(&ps);
1714
1715 XglDescriptorSetObj descriptorSet(m_device);
1716 descriptorSet.AttachImageView(&texture);
1717 descriptorSet.AttachSampler(&sampler);
1718
1719 m_memoryRefManager.AddMemoryRef(&texture);
1720
1721 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1722 QueueCommandBuffer(NULL, 0);
1723
1724}
1725TEST_F(XglRenderTest, TexturedTriangle)
1726{
1727 // The expected result from this test is a red and green checkered triangle
1728 static const char *vertShaderText =
1729 "#version 140\n"
1730 "#extension GL_ARB_separate_shader_objects : enable\n"
1731 "#extension GL_ARB_shading_language_420pack : enable\n"
1732 "layout (location = 0) out vec2 samplePos;\n"
1733 "void main() {\n"
1734 " vec2 vertices[3];"
1735 " vertices[0] = vec2(-0.5, -0.5);\n"
1736 " vertices[1] = vec2( 0.5, -0.5);\n"
1737 " vertices[2] = vec2( 0.5, 0.5);\n"
1738 " vec2 positions[3];"
1739 " positions[0] = vec2( 0.0, 0.0);\n"
1740 " positions[1] = vec2( 1.0, 0.0);\n"
1741 " positions[2] = vec2( 1.0, 1.0);\n"
1742 " samplePos = positions[gl_VertexID % 3];\n"
1743 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1744 "}\n";
1745
1746 static const char *fragShaderText =
1747 "#version 140\n"
1748 "#extension GL_ARB_separate_shader_objects : enable\n"
1749 "#extension GL_ARB_shading_language_420pack : enable\n"
1750 "layout (location = 0) in vec2 samplePos;\n"
1751 "layout (binding = 0) uniform sampler2D surface;\n"
1752 "layout (location=0) out vec4 outColor;\n"
1753 "void main() {\n"
1754 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1755 " outColor = texColor;\n"
1756 "}\n";
1757
1758 ASSERT_NO_FATAL_FAILURE(InitState());
1759 ASSERT_NO_FATAL_FAILURE(InitViewport());
1760
1761 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1762 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1763 XglSamplerObj sampler(m_device);
1764 XglTextureObj texture(m_device);
1765
1766 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1767 ps.BindShaderEntitySlotToSampler(0, &sampler);
1768
1769 XglPipelineObj pipelineobj(m_device);
1770 pipelineobj.AddShader(&vs);
1771 pipelineobj.AddShader(&ps);
1772
1773 XglDescriptorSetObj descriptorSet(m_device);
1774 descriptorSet.AttachImageView(&texture);
1775 descriptorSet.AttachSampler(&sampler);
1776
1777 m_memoryRefManager.AddMemoryRef(&texture);
1778
1779 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1780 QueueCommandBuffer(NULL, 0);
1781}
1782TEST_F(XglRenderTest, TexturedTriangleClip)
1783{
1784 // The expected result from this test is a red and green checkered triangle
1785 static const char *vertShaderText =
1786 "#version 330\n"
1787 "#extension GL_ARB_separate_shader_objects : enable\n"
1788 "#extension GL_ARB_shading_language_420pack : enable\n"
1789 "layout (location = 0) out vec2 samplePos;\n"
1790 "out gl_PerVertex {\n"
1791 " vec4 gl_Position;\n"
1792 " float gl_ClipDistance[1];\n"
1793 "};\n"
1794 "void main() {\n"
1795 " vec2 vertices[3];"
1796 " vertices[0] = vec2(-0.5, -0.5);\n"
1797 " vertices[1] = vec2( 0.5, -0.5);\n"
1798 " vertices[2] = vec2( 0.5, 0.5);\n"
1799 " vec2 positions[3];"
1800 " positions[0] = vec2( 0.0, 0.0);\n"
1801 " positions[1] = vec2( 1.0, 0.0);\n"
1802 " positions[2] = vec2( 1.0, 1.0);\n"
1803 " float dists[3];\n"
1804 " dists[0] = 1.0;\n"
1805 " dists[1] = 1.0;\n"
1806 " dists[2] = -1.0;\n"
1807 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1808 " samplePos = positions[gl_VertexID % 3];\n"
1809 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1810 "}\n";
1811
1812 static const char *fragShaderText =
1813 "#version 140\n"
1814 "#extension GL_ARB_separate_shader_objects : enable\n"
1815 "#extension GL_ARB_shading_language_420pack : enable\n"
1816 "layout (location = 0) in vec2 samplePos;\n"
1817 "layout (binding = 0) uniform sampler2D surface;\n"
1818 "layout (location=0) out vec4 outColor;\n"
1819 "void main() {\n"
1820 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1821 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1822 " outColor = texColor;\n"
1823 "}\n";
1824
1825
1826 ASSERT_NO_FATAL_FAILURE(InitState());
1827 ASSERT_NO_FATAL_FAILURE(InitViewport());
1828
1829 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1830 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1831 XglSamplerObj sampler(m_device);
1832 XglTextureObj texture(m_device);
1833
1834 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1835 ps.BindShaderEntitySlotToSampler(0, &sampler);
1836
1837 XglPipelineObj pipelineobj(m_device);
1838 pipelineobj.AddShader(&vs);
1839 pipelineobj.AddShader(&ps);
1840
1841 XglDescriptorSetObj descriptorSet(m_device);
1842 descriptorSet.AttachImageView(&texture);
1843 descriptorSet.AttachSampler(&sampler);
1844
1845 m_memoryRefManager.AddMemoryRef(&texture);
1846
1847 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1848 QueueCommandBuffer(NULL, 0);
1849}
1850TEST_F(XglRenderTest, FSTriangle)
1851{
1852 // The expected result from this test is a red and green checkered triangle
1853 static const char *vertShaderText =
1854 "#version 140\n"
1855 "#extension GL_ARB_separate_shader_objects : enable\n"
1856 "#extension GL_ARB_shading_language_420pack : enable\n"
1857 "layout (location = 0) out vec2 samplePos;\n"
1858 "void main() {\n"
1859 " vec2 vertices[3];"
1860 " vertices[0] = vec2(-0.5, -0.5);\n"
1861 " vertices[1] = vec2( 0.5, -0.5);\n"
1862 " vertices[2] = vec2( 0.5, 0.5);\n"
1863 " vec2 positions[3];"
1864 " positions[0] = vec2( 0.0, 0.0);\n"
1865 " positions[1] = vec2( 1.0, 0.0);\n"
1866 " positions[2] = vec2( 1.0, 1.0);\n"
1867 " samplePos = positions[gl_VertexID % 3];\n"
1868 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1869 "}\n";
1870
1871 static const char *fragShaderText =
1872 "#version 140\n"
1873 "#extension GL_ARB_separate_shader_objects : enable\n"
1874 "#extension GL_ARB_shading_language_420pack : enable\n"
1875 "layout (location = 0) in vec2 samplePos;\n"
1876 "layout (binding = 0) uniform sampler2D surface;\n"
1877 "layout (location=0) out vec4 outColor;\n"
1878 "void main() {\n"
1879 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1880 " outColor = texColor;\n"
1881 "}\n";
1882
1883 ASSERT_NO_FATAL_FAILURE(InitState());
1884 ASSERT_NO_FATAL_FAILURE(InitViewport());
1885
1886 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1887 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1888 XglSamplerObj sampler(m_device);
1889 XglTextureObj texture(m_device);
1890
1891 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1892 ps.BindShaderEntitySlotToSampler(0, &sampler);
1893
1894 XglPipelineObj pipelineobj(m_device);
1895 pipelineobj.AddShader(&vs);
1896 pipelineobj.AddShader(&ps);
1897
1898 XglDescriptorSetObj descriptorSet(m_device);
1899 descriptorSet.AttachImageView(&texture);
1900 descriptorSet.AttachSampler(&sampler);
1901
1902 m_memoryRefManager.AddMemoryRef(&texture);
1903
1904 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1905 QueueCommandBuffer(NULL, 0);
1906}
1907TEST_F(XglRenderTest, SamplerBindingsTriangle)
1908{
1909 // This test sets bindings on the samplers
1910 // For now we are asserting that sampler and texture pairs
1911 // march in lock step, and are set via GLSL binding. This can
1912 // and will probably change.
1913 // The sampler bindings should match the sampler and texture slot
1914 // number set up by the application.
1915 // This test will result in a blue triangle
1916 static const char *vertShaderText =
1917 "#version 140\n"
1918 "#extension GL_ARB_separate_shader_objects : enable\n"
1919 "#extension GL_ARB_shading_language_420pack : enable\n"
1920 "layout (location = 0) out vec4 samplePos;\n"
1921 "void main() {\n"
1922 " vec2 vertices[3];"
1923 " vertices[0] = vec2(-0.5, -0.5);\n"
1924 " vertices[1] = vec2( 0.5, -0.5);\n"
1925 " vertices[2] = vec2( 0.5, 0.5);\n"
1926 " vec2 positions[3];"
1927 " positions[0] = vec2( 0.0, 0.0);\n"
1928 " positions[1] = vec2( 1.0, 0.0);\n"
1929 " positions[2] = vec2( 1.0, 1.0);\n"
1930 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
1931 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1932 "}\n";
1933
1934 static const char *fragShaderText =
1935 "#version 140\n"
1936 "#extension GL_ARB_separate_shader_objects : enable\n"
1937 "#extension GL_ARB_shading_language_420pack : enable\n"
1938 "layout (location = 0) in vec4 samplePos;\n"
1939 "layout (binding = 0) uniform sampler2D surface0;\n"
1940 "layout (binding = 1) uniform sampler2D surface1;\n"
1941 "layout (binding = 12) uniform sampler2D surface2;\n"
1942 "void main() {\n"
1943 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
1944 "}\n";
1945
1946 ASSERT_NO_FATAL_FAILURE(InitState());
1947 ASSERT_NO_FATAL_FAILURE(InitViewport());
1948
1949 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1950 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1951
1952 XglSamplerObj sampler1(m_device);
1953 XglSamplerObj sampler2(m_device);
1954 XglSamplerObj sampler3(m_device);
1955
1956 XglTextureObj texture1(m_device); // Red
1957 texture1.ChangeColors(0xffff0000,0xffff0000);
1958 XglTextureObj texture2(m_device); // Green
1959 texture2.ChangeColors(0xff00ff00,0xff00ff00);
1960 XglTextureObj texture3(m_device); // Blue
1961 texture3.ChangeColors(0xff0000ff,0xff0000ff);
1962
1963 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture1);
1964 ps.BindShaderEntitySlotToSampler(0, &sampler1);
1965
1966 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_RESOURCE, &texture2);
1967 ps.BindShaderEntitySlotToSampler(1, &sampler2);
1968
1969 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_RESOURCE, &texture3);
1970 ps.BindShaderEntitySlotToSampler(12, &sampler3);
1971
1972 XglPipelineObj pipelineobj(m_device);
1973 pipelineobj.AddShader(&vs);
1974 pipelineobj.AddShader(&ps);
1975
1976 XglDescriptorSetObj descriptorSet(m_device);
1977 descriptorSet.AttachImageView(&texture1);
1978 descriptorSet.AttachSampler(&sampler1);
1979 descriptorSet.AttachImageView(&texture2);
1980 descriptorSet.AttachSampler(&sampler2);
1981 descriptorSet.AttachImageView(&texture3);
1982 descriptorSet.AttachSampler(&sampler3);
1983
1984 m_memoryRefManager.AddMemoryRef(&texture1);
1985 m_memoryRefManager.AddMemoryRef(&texture2);
1986 m_memoryRefManager.AddMemoryRef(&texture3);
1987
1988 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1989 QueueCommandBuffer(NULL, 0);
1990
1991}
1992
1993TEST_F(XglRenderTest, TriangleVSUniformBlock)
1994{
1995 // The expected result from this test is a blue triangle
1996
1997 static const char *vertShaderText =
1998 "#version 140\n"
1999 "#extension GL_ARB_separate_shader_objects : enable\n"
2000 "#extension GL_ARB_shading_language_420pack : enable\n"
2001 "layout (location = 0) out vec4 outColor;\n"
2002 "layout (std140, binding = 0) uniform bufferVals {\n"
2003 " vec4 red;\n"
2004 " vec4 green;\n"
2005 " vec4 blue;\n"
2006 " vec4 white;\n"
2007 "} myBufferVals;\n"
2008 "void main() {\n"
2009 " vec2 vertices[3];"
2010 " vertices[0] = vec2(-0.5, -0.5);\n"
2011 " vertices[1] = vec2( 0.5, -0.5);\n"
2012 " vertices[2] = vec2( 0.5, 0.5);\n"
2013 " outColor = myBufferVals.blue;\n"
2014 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2015 "}\n";
2016
2017 static const char *fragShaderText =
2018 "#version 140\n"
2019 "#extension GL_ARB_separate_shader_objects : enable\n"
2020 "#extension GL_ARB_shading_language_420pack : enable\n"
2021 "layout (location = 0) in vec4 inColor;\n"
2022 "void main() {\n"
2023 " gl_FragColor = inColor;\n"
2024 "}\n";
2025
2026 ASSERT_NO_FATAL_FAILURE(InitState());
2027 ASSERT_NO_FATAL_FAILURE(InitViewport());
2028
2029 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2030 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2031
2032 // Let's populate our buffer with the following:
2033 // vec4 red;
2034 // vec4 green;
2035 // vec4 blue;
2036 // vec4 white;
2037 const int valCount = 4 * 4;
2038 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2039 0.0, 1.0, 0.0, 1.0,
2040 0.0, 0.0, 1.0, 1.0,
2041 1.0, 1.0, 1.0, 1.0 };
2042
2043 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2044 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2045
2046 XglPipelineObj pipelineobj(m_device);
2047 pipelineobj.AddShader(&vs);
2048 pipelineobj.AddShader(&ps);
2049
2050 XglDescriptorSetObj descriptorSet(m_device);
2051 descriptorSet.AttachMemoryView(&colorBuffer);
2052
2053 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
2054 QueueCommandBuffer(NULL, 0);
2055
2056}
2057
2058TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2059{
2060 // This test allows the shader to select which buffer it is
2061 // pulling from using layout binding qualifier.
2062 // There are corresponding changes in the compiler stack that
2063 // will select the buffer using binding directly.
2064 // The binding number should match the slot number set up by
2065 // the application.
2066 // The expected result from this test is a purple triangle
2067
2068 static const char *vertShaderText =
2069 "#version 140\n"
2070 "#extension GL_ARB_separate_shader_objects : enable\n"
2071 "#extension GL_ARB_shading_language_420pack : enable\n"
2072 "void main() {\n"
2073 " vec2 vertices[3];"
2074 " vertices[0] = vec2(-0.5, -0.5);\n"
2075 " vertices[1] = vec2( 0.5, -0.5);\n"
2076 " vertices[2] = vec2( 0.5, 0.5);\n"
2077 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2078 "}\n";
2079
2080 static const char *fragShaderText =
2081 "#version 140\n"
2082 "#extension GL_ARB_separate_shader_objects : enable\n"
2083 "#extension GL_ARB_shading_language_420pack : enable\n"
2084 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2085 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2086 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2087 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2088 "void main() {\n"
2089 " gl_FragColor = myBlueVal.color;\n"
2090 " gl_FragColor += myRedVal.color;\n"
2091 "}\n";
2092
2093 ASSERT_NO_FATAL_FAILURE(InitState());
2094 ASSERT_NO_FATAL_FAILURE(InitViewport());
2095
2096 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2097 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2098
2099 // We're going to create a number of uniform buffers, and then allow
2100 // the shader to select which it wants to read from with a binding
2101
2102 // Let's populate the buffers with a single color each:
2103 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2104 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2105 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2106 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2107
2108 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2109 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2110 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2111 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2112
2113 const int redCount = sizeof(redVals) / sizeof(float);
2114 const int greenCount = sizeof(greenVals) / sizeof(float);
2115 const int blueCount = sizeof(blueVals) / sizeof(float);
2116 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2117
2118 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2119 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2120
2121 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2122 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2123
2124 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2125 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2126
2127 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2128 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2129
2130 XglPipelineObj pipelineobj(m_device);
2131 pipelineobj.AddShader(&vs);
2132 pipelineobj.AddShader(&ps);
2133
2134 XglDescriptorSetObj descriptorSet(m_device);
2135 descriptorSet.AttachMemoryView(&redBuffer);
2136 descriptorSet.AttachMemoryView(&greenBuffer);
2137 descriptorSet.AttachMemoryView(&blueBuffer);
2138 descriptorSet.AttachMemoryView(&whiteBuffer);
2139
2140 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
2141 QueueCommandBuffer(NULL, 0);
2142
2143}
2144
2145TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2146{
2147 // This test is the same as TriangleFSUniformBlockBinding, but
2148 // it does not provide an instance name.
2149 // The expected result from this test is a purple triangle
2150
2151 static const char *vertShaderText =
2152 "#version 140\n"
2153 "#extension GL_ARB_separate_shader_objects : enable\n"
2154 "#extension GL_ARB_shading_language_420pack : enable\n"
2155 "void main() {\n"
2156 " vec2 vertices[3];"
2157 " vertices[0] = vec2(-0.5, -0.5);\n"
2158 " vertices[1] = vec2( 0.5, -0.5);\n"
2159 " vertices[2] = vec2( 0.5, 0.5);\n"
2160 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2161 "}\n";
2162
2163 static const char *fragShaderText =
2164 "#version 430\n"
2165 "#extension GL_ARB_separate_shader_objects : enable\n"
2166 "#extension GL_ARB_shading_language_420pack : enable\n"
2167 "layout (location = 0) uniform vec4 foo;\n"
2168 "layout (location = 1) uniform vec4 bar;\n"
2169 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2170 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2171 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2172 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
2173 "void main() {\n"
2174 " gl_FragColor = blue;\n"
2175 " gl_FragColor += red;\n"
2176 "}\n";
2177 ASSERT_NO_FATAL_FAILURE(InitState());
2178 ASSERT_NO_FATAL_FAILURE(InitViewport());
2179
2180 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2181 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2182
2183 // We're going to create a number of uniform buffers, and then allow
2184 // the shader to select which it wants to read from with a binding
2185
2186 // Let's populate the buffers with a single color each:
2187 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2188 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2189 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2190 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2191
2192 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2193 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2194 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2195 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2196
2197 const int redCount = sizeof(redVals) / sizeof(float);
2198 const int greenCount = sizeof(greenVals) / sizeof(float);
2199 const int blueCount = sizeof(blueVals) / sizeof(float);
2200 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2201
2202 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2203 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2204
2205 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2206 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2207
2208 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2209 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2210
2211 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2212 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2213
2214 XglPipelineObj pipelineobj(m_device);
2215 pipelineobj.AddShader(&vs);
2216 pipelineobj.AddShader(&ps);
2217
2218 XglDescriptorSetObj descriptorSet(m_device);
2219 descriptorSet.AttachMemoryView(&redBuffer);
2220 descriptorSet.AttachMemoryView(&greenBuffer);
2221 descriptorSet.AttachMemoryView(&blueBuffer);
2222 descriptorSet.AttachMemoryView(&whiteBuffer);
2223
2224 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
2225 QueueCommandBuffer(NULL, 0);
2226
2227}
2228
2229TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2230{
2231 static const char *vertShaderText =
2232 "#version 140\n"
2233 "#extension GL_ARB_separate_shader_objects : enable\n"
2234 "#extension GL_ARB_shading_language_420pack : enable\n"
2235 "layout (std140, binding=0) uniform bufferVals {\n"
2236 " mat4 mvp;\n"
2237 "} myBufferVals;\n"
2238 "layout (location=0) in vec4 pos;\n"
2239 "layout (location=0) out vec2 UV;\n"
2240 "void main() {\n"
2241 " vec2 positions[3];"
2242 " positions[0] = vec2( 0.0, 0.0);\n"
2243 " positions[1] = vec2( 0.25, 0.1);\n"
2244 " positions[2] = vec2( 0.1, 0.25);\n"
2245 " UV = positions[gl_VertexID % 3];\n"
2246 " gl_Position = myBufferVals.mvp * pos;\n"
2247 "}\n";
2248
2249 static const char *fragShaderText =
2250 "#version 140\n"
2251 "#extension GL_ARB_separate_shader_objects : enable\n"
2252 "#extension GL_ARB_shading_language_420pack : enable\n"
2253 "layout (binding=0) uniform sampler2D surface;\n"
2254 "layout (location=0) out vec4 outColor;\n"
2255 "layout (location=0) in vec2 UV;\n"
2256 "void main() {\n"
2257 " outColor= textureLod(surface, UV, 0.0);\n"
2258 "}\n";
2259 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2260
2261 glm::mat4 View = glm::lookAt(
2262 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2263 glm::vec3(0,0,0), // and looks at the origin
2264 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2265 );
2266
2267 glm::mat4 Model = glm::mat4(1.0f);
2268
2269 glm::mat4 MVP = Projection * View * Model;
2270
2271
2272 ASSERT_NO_FATAL_FAILURE(InitState());
2273 ASSERT_NO_FATAL_FAILURE(InitViewport());
2274 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2275
2276 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2277 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
2278 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
2279
2280
2281 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2282
2283 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2284 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2285 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2286 XglSamplerObj sampler(m_device);
2287 XglTextureObj texture(m_device);
2288
2289 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2290 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
2291 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
2292 ps.BindShaderEntitySlotToSampler(0, &sampler);
2293
2294 XglPipelineObj pipelineobj(m_device);
2295 pipelineobj.AddShader(&vs);
2296 pipelineobj.AddShader(&ps);
2297
2298 XglDescriptorSetObj descriptorSet(m_device);
2299
2300 descriptorSet.AttachMemoryView(&mvpBuffer);
2301 descriptorSet.AttachImageView(&texture);
2302 descriptorSet.AttachSampler(&sampler);
2303
2304 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2305 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2306 m_memoryRefManager.AddMemoryRef(&texture);
2307
2308 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2309 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2310 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2311 };
2312
2313 // this is the current description of g_vbData
2314 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2315 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2316 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2317 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2318 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2319 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2320 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2321 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2322 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2323
2324 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2325 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2326 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2327
2328 ClearDepthStencil(1.0f);
2329 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
2330
2331 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2332
2333}
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002334int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002335 int result;
2336
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002337 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002338 XglTestFramework::InitArgs(&argc, argv);
2339
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002340 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
2341
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002342 result = RUN_ALL_TESTS();
2343
2344 XglTestFramework::Finish();
2345 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002346}