blob: c4648960fb2d4ad2ea45fb696f1ff1374ba9028a [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,
Tony Barbour09da2212014-12-03 16:13:23 -0700220 XglConstantBufferObj *constantBuffer);
Tony Barboura3953b82014-12-03 15:46:29 -0700221 void GenericDrawTriangleTest(XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet, int numTris);
Tony Barbourf43b6982014-11-25 13:18:32 -0700222 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
Tony Barboura3953b82014-12-03 15:46:29 -0700269void 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 Barboura3953b82014-12-03 15:46:29 -0700286 pipelineobj->BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
Tony Barbourbf678472014-12-03 13:58:15 -0700287 descriptorSet->BindCommandBuffer(m_cmdBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -0700288
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 Barbourbf678472014-12-03 13:58:15 -0700347 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,&descriptorSet);
Tony Barbourf43b6982014-11-25 13:18:32 -0700348 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
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600357 // submit the command buffer to the universal queue
Tony Barbour75420772014-12-04 17:17:26 -0700358 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, 0, m_memRefs, NULL );
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600359 ASSERT_XGL_SUCCESS( err );
360
361 err = xglQueueWaitIdle( m_device->m_queue );
362 ASSERT_XGL_SUCCESS( err );
363
364 // Wait for work to finish before cleaning up.
365 xglDeviceWaitIdle(m_device->device());
366
367 RecordImage(m_renderTarget);
368
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600369}
370
Tony Barbourf43b6982014-11-25 13:18:32 -0700371void XglRenderTest::RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
Tony Barbour09da2212014-12-03 16:13:23 -0700372 XglConstantBufferObj *constantBuffer)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600373{
374 int i;
375 glm::mat4 MVP;
376 int matrixSize = sizeof(MVP);
377 XGL_RESULT err;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600378
379 for (i = 0; i < 8; i++) {
380 XGL_UINT8 *pData;
Tony Barbour09da2212014-12-03 16:13:23 -0700381 err = xglMapMemory(constantBuffer->m_constantBufferMem, 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600382 ASSERT_XGL_SUCCESS(err);
383
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600384 Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
385 MVP = Projection * View * Model;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600386 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
387
Tony Barbour09da2212014-12-03 16:13:23 -0700388 err = xglUnmapMemory(constantBuffer->m_constantBufferMem);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600389 ASSERT_XGL_SUCCESS(err);
390
391 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700392 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_memoryRefManager.GetNumRefs(), m_memoryRefManager.GetMemoryRefList(), NULL );
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600393 ASSERT_XGL_SUCCESS( err );
394
395 err = xglQueueWaitIdle( m_device->m_queue );
396 ASSERT_XGL_SUCCESS( err );
397
398 // Wait for work to finish before cleaning up.
399 xglDeviceWaitIdle(m_device->device());
400
401 RecordImage(m_renderTarget);
402 }
Cody Northrop7a1f0462014-10-10 14:49:36 -0600403}
404
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600405void dumpMatrix(const char *note, glm::mat4 MVP)
406{
407 int i,j;
408
409 printf("%s: \n", note);
410 for (i=0; i<4; i++) {
411 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
412 }
413 printf("\n");
414 fflush(stdout);
415}
416
417void dumpVec4(const char *note, glm::vec4 vector)
418{
419 printf("%s: \n", note);
420 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
421 printf("\n");
422 fflush(stdout);
423}
424
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600425void XglRenderTest::GenerateClearAndPrepareBufferCmds()
426{
427 XglRenderFramework::GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600428}
429
Cody Northrop7a1f0462014-10-10 14:49:36 -0600430
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600431void XglRenderTest::ClearDepthStencil(XGL_FLOAT value)
432/* clear the buffer */
433{
434 XGL_RESULT err;
435 const uint16_t depth_value = (uint16_t) (value * 65535);
436 const XGL_INT tw = 128 / sizeof(uint16_t);
437 const XGL_INT th = 32;
438 XGL_INT i, j, w, h;
439 XGL_VOID *data;
440
441 w = (m_width + tw - 1) / tw;
442 h = (m_height + th - 1) / th;
443
444 err = xglMapMemory(m_depthStencilMem, 0, &data);
445 ASSERT_XGL_SUCCESS(err);
446
Tony Barbourf43b6982014-11-25 13:18:32 -0700447#ifdef DUMP_STATE_DOT
448 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
449 pDSDumpDot((char*)"triVtxFetchAndMVP.dot");
450#endif
451
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600452 for (i = 0; i < w * h; i++) {
453 uint16_t *tile = (uint16_t *) ((char *) data + 4096 * i);
454
455 for (j = 0; j < 2048; j++)
456 tile[j] = depth_value;
457 }
458
459 err = xglUnmapMemory(m_depthStencilMem);
460 ASSERT_XGL_SUCCESS(err);
461}
462
463void XglRenderTest::ClearRenderBuffer(XGL_UINT32 clear_color)
464/* clear the buffer */
465{
466 XGL_RESULT err;
467 const XGL_IMAGE_SUBRESOURCE sr = {
468 XGL_IMAGE_ASPECT_COLOR, 0, 0
469 };
470 XGL_SUBRESOURCE_LAYOUT sr_layout;
471 XGL_UINT data_size = sizeof(sr_layout);
472 XGL_VOID *ptr;
473
474 err = xglGetImageSubresourceInfo( m_renderTarget->image(),
475 &sr, XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
476 &data_size, &sr_layout);
477 ASSERT_XGL_SUCCESS( err );
478 ASSERT_EQ(data_size, sizeof(sr_layout));
479
480 err = m_renderTarget->MapMemory( &ptr );
481 ASSERT_XGL_SUCCESS( err );
482
483 ptr = (void *) ((char *) ptr + sr_layout.offset);
484
485 memset(ptr, clear_color, m_width * m_height *sizeof(XGL_UINT32));
486
487 err = m_renderTarget->UnmapMemory();
488 ASSERT_XGL_SUCCESS(err);
489}
490
491void XglRenderTest::InitDepthStencil()
492{
493 XGL_RESULT err;
494 XGL_IMAGE_CREATE_INFO image;
495 XGL_MEMORY_ALLOC_INFO mem_alloc;
496 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
497 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn6317c492014-11-21 11:33:20 -0700498 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600499
500 // Clean up default state created by framework
501 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
502
503 m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16;
504 m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS;
505
506 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
507 image.pNext = NULL;
508 image.imageType = XGL_IMAGE_2D;
509 image.format = m_depth_stencil_fmt;
510 image.extent.width = m_width;
511 image.extent.height = m_height;
512 image.extent.depth = 1;
513 image.mipLevels = 1;
514 image.arraySize = 1;
515 image.samples = 1;
516 image.tiling = XGL_OPTIMAL_TILING;
517 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
518 image.flags = 0;
519
520 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
521 mem_alloc.pNext = NULL;
522 mem_alloc.allocationSize = 0;
523 mem_alloc.alignment = 0;
524 mem_alloc.flags = 0;
525 mem_alloc.heapCount = 0;
526 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
527
528 /* create image */
529 err = xglCreateImage(device(), &image,
530 &m_depthStencilImage);
531 ASSERT_XGL_SUCCESS(err);
532
533 err = xglGetObjectInfo(m_depthStencilImage,
534 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
535 &mem_reqs_size, &mem_reqs);
536 ASSERT_XGL_SUCCESS(err);
537 ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs));
538
539 mem_alloc.allocationSize = mem_reqs.size;
540 mem_alloc.alignment = mem_reqs.alignment;
541 mem_alloc.heapCount = mem_reqs.heapCount;
542 memcpy(mem_alloc.heaps, mem_reqs.heaps,
543 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
544
545 /* allocate memory */
546 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem);
547 ASSERT_XGL_SUCCESS(err);
548
549 /* bind memory */
550 err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0);
551 ASSERT_XGL_SUCCESS(err);
552
553 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
554 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
555 depthStencil.depthTestEnable = XGL_TRUE;
556 depthStencil.depthWriteEnable = XGL_TRUE;
557 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
558 depthStencil.depthBoundsEnable = XGL_FALSE;
559 depthStencil.minDepth = 0.f;
560 depthStencil.maxDepth = 1.f;
561 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
562 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
563 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
564 depthStencil.back.stencilRef = 0x00;
565 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
566 depthStencil.front = depthStencil.back;
567
568 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
569 ASSERT_XGL_SUCCESS( err );
570
571 /* create image view */
572 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
573 view.pNext = NULL;
574 view.image = XGL_NULL_HANDLE;
575 view.mipLevel = 0;
576 view.baseArraySlice = 0;
577 view.arraySize = 1;
578 view.flags = 0;
579 view.image = m_depthStencilImage;
580 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
581 ASSERT_XGL_SUCCESS(err);
582
583 m_depthStencilBinding.view = m_depthStencilView;
584 m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
585 m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
586}
587
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600588struct xgltriangle_vs_uniform {
589 // Must start with MVP
590 XGL_FLOAT mvp[4][4];
591 XGL_FLOAT position[3][4];
592 XGL_FLOAT color[3][4];
593};
594
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600595void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600596{
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700597#ifdef DEBUG_CALLBACK
598 xglDbgRegisterMsgCallback(myDbgFunc, NULL);
599#endif
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600600 // Create identity matrix
601 int i;
602 struct xgltriangle_vs_uniform data;
603
604 glm::mat4 Projection = glm::mat4(1.0f);
605 glm::mat4 View = glm::mat4(1.0f);
606 glm::mat4 Model = glm::mat4(1.0f);
607 glm::mat4 MVP = Projection * View * Model;
608 const int matrixSize = sizeof(MVP);
609 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT);
610 memcpy(&data.mvp, &MVP[0][0], matrixSize);
611
612 static const Vertex tri_data[] =
613 {
614 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
615 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
616 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
617 };
618
619 for (i=0; i<3; i++) {
620 data.position[i][0] = tri_data[i].posX;
621 data.position[i][1] = tri_data[i].posY;
622 data.position[i][2] = tri_data[i].posZ;
623 data.position[i][3] = tri_data[i].posW;
624 data.color[i][0] = tri_data[i].r;
625 data.color[i][1] = tri_data[i].g;
626 data.color[i][2] = tri_data[i].b;
627 data.color[i][3] = tri_data[i].a;
628 }
629
Tony Barbourf43b6982014-11-25 13:18:32 -0700630 ASSERT_NO_FATAL_FAILURE(InitState());
631 ASSERT_NO_FATAL_FAILURE(InitViewport());
632
633 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data);
634
635 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
636 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
637 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
638
639 XglPipelineObj pipelineobj(m_device);
640 pipelineobj.AddShader(&vs);
641 pipelineobj.AddShader(&ps);
642
643 XglDescriptorSetObj descriptorSet(m_device);
644 descriptorSet.AttachMemoryView(&constantBuffer);
645 m_memoryRefManager.AddMemoryRef(&constantBuffer);
646
Tony Barboura3953b82014-12-03 15:46:29 -0700647 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -0700648 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
649
Tony Barbour09da2212014-12-03 16:13:23 -0700650 RotateTriangleVSUniform(Projection, View, Model, &constantBuffer);
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700651#ifdef PRINT_OBJECTS
652 //XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)
653 OBJ_TRACK_GET_OBJECT_COUNT pObjTrackGetObjectCount = (OBJ_TRACK_GET_OBJECT_COUNT)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjectCount");
654 XGL_UINT64 numObjects = pObjTrackGetObjectCount(XGL_OBJECT_TYPE_ANY);
655 //OBJ_TRACK_GET_OBJECTS pGetObjsFunc = xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
656 printf("DEBUG : Number of Objects : %lu\n", numObjects);
657 OBJ_TRACK_GET_OBJECTS pObjTrackGetObjs = (OBJ_TRACK_GET_OBJECTS)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
658 OBJTRACK_NODE* pObjNodeArray = (OBJTRACK_NODE*)malloc(sizeof(OBJTRACK_NODE)*numObjects);
659 pObjTrackGetObjs(XGL_OBJECT_TYPE_ANY, numObjects, pObjNodeArray);
660 for (i=0; i < numObjects; i++) {
661 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);
662 }
663 free(pObjNodeArray);
664#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700665
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600666}
667
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600668TEST_F(XglRenderTest, XGLTriangle_FragColor)
669{
670 static const char *vertShaderText =
671 "#version 140\n"
672 "#extension GL_ARB_separate_shader_objects : enable\n"
673 "#extension GL_ARB_shading_language_420pack : enable\n"
674 "\n"
675 "layout(binding = 0) uniform buf {\n"
676 " mat4 MVP;\n"
677 " vec4 position[3];\n"
678 " vec4 color[3];\n"
679 "} ubuf;\n"
680 "\n"
681 "layout (location = 0) out vec4 outColor;\n"
682 "\n"
683 "void main() \n"
684 "{\n"
685 " outColor = ubuf.color[gl_VertexID];\n"
686 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
687 "}\n";
688
689 static const char *fragShaderText =
690 "#version 140\n"
691 "#extension GL_ARB_separate_shader_objects : enable\n"
692 "#extension GL_ARB_shading_language_420pack : enable\n"
693 "\n"
694 "layout (location = 0) in vec4 inColor;\n"
695 "\n"
696 "void main()\n"
697 "{\n"
698 " gl_FragColor = inColor;\n"
699 "}\n";
700
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600701 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600702 XGLTriangleTest(vertShaderText, fragShaderText);
703}
704
705TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
706{
707 static const char *vertShaderText =
708 "#version 140\n"
709 "#extension GL_ARB_separate_shader_objects : enable\n"
710 "#extension GL_ARB_shading_language_420pack : enable\n"
711 "\n"
712 "layout(binding = 0) uniform buf {\n"
713 " mat4 MVP;\n"
714 " vec4 position[3];\n"
715 " vec4 color[3];\n"
716 "} ubuf;\n"
717 "\n"
718 "layout (location = 0) out vec4 outColor;\n"
719 "\n"
720 "void main() \n"
721 "{\n"
722 " outColor = ubuf.color[gl_VertexID];\n"
723 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
724 "}\n";
725
726 static const char *fragShaderText =
727 "#version 140\n"
728 "#extension GL_ARB_separate_shader_objects : enable\n"
729 "#extension GL_ARB_shading_language_420pack : enable\n"
730 "\n"
731 "layout (location = 0) in vec4 inColor;\n"
732 "layout (location = 0) out vec4 outColor;\n"
733 "\n"
734 "void main()\n"
735 "{\n"
736 " outColor = inColor;\n"
737 "}\n";
738
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600739 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 -0600740
741 XGLTriangleTest(vertShaderText, fragShaderText);
742}
743
Tony Barbourf43b6982014-11-25 13:18:32 -0700744TEST_F(XglRenderTest, BIL_XGLTriangle)
745{
746 bool saved_use_bil = XglTestFramework::m_use_bil;
747
748 static const char *vertShaderText =
749 "#version 140\n"
750 "#extension GL_ARB_separate_shader_objects : enable\n"
751 "#extension GL_ARB_shading_language_420pack : enable\n"
752 "\n"
753 "layout(binding = 0) uniform buf {\n"
754 " mat4 MVP;\n"
755 " vec4 position[3];\n"
756 " vec4 color[3];\n"
757 "} ubuf;\n"
758 "\n"
759 "layout (location = 0) out vec4 outColor;\n"
760 "\n"
761 "void main() \n"
762 "{\n"
763 " outColor = ubuf.color[gl_VertexID];\n"
764 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
765 "}\n";
766
767 static const char *fragShaderText =
768 "#version 140\n"
769 "#extension GL_ARB_separate_shader_objects : enable\n"
770 "#extension GL_ARB_shading_language_420pack : enable\n"
771 "\n"
772 "layout (location = 0) in vec4 inColor;\n"
773 "\n"
774 "void main()\n"
775 "{\n"
776 " gl_FragColor = inColor;\n"
777 "}\n";
778
779 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver.");
780
781 XglTestFramework::m_use_bil = true;
782
783 XGLTriangleTest(vertShaderText, fragShaderText);
784
785 XglTestFramework::m_use_bil = saved_use_bil;
786}
787
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600788TEST_F(XglRenderTest, GreenTriangle)
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600789{
790 static const char *vertShaderText =
791 "#version 130\n"
792 "vec2 vertices[3];\n"
793 "void main() {\n"
794 " vertices[0] = vec2(-1.0, -1.0);\n"
795 " vertices[1] = vec2( 1.0, -1.0);\n"
796 " vertices[2] = vec2( 0.0, 1.0);\n"
797 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
798 "}\n";
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600799
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600800 static const char *fragShaderText =
801 "#version 130\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600802 "void main() {\n"
Steve K10b32512014-10-10 08:54:29 -0600803 " gl_FragColor = vec4(0,1,0,1);\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600804 "}\n";
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600805
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600806 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
807
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600808 DrawTriangleTest(vertShaderText, fragShaderText);
809}
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600810
Tony Barbourf43b6982014-11-25 13:18:32 -0700811TEST_F(XglRenderTest, BIL_GreenTriangle)
812{
813 bool saved_use_bil = XglTestFramework::m_use_bil;
814
815 static const char *vertShaderText =
816 "#version 130\n"
817 "vec2 vertices[3];\n"
818 "void main() {\n"
819 " vertices[0] = vec2(-1.0, -1.0);\n"
820 " vertices[1] = vec2( 1.0, -1.0);\n"
821 " vertices[2] = vec2( 0.0, 1.0);\n"
822 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
823 "}\n";
824
825 static const char *fragShaderText =
826 "#version 130\n"
827 "void main() {\n"
828 " gl_FragColor = vec4(0,1,0,1);\n"
829 "}\n";
830
831 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver.");
832
833 XglTestFramework::m_use_bil = true;
834 DrawTriangleTest(vertShaderText, fragShaderText);
835 XglTestFramework::m_use_bil = saved_use_bil;
836}
837
838TEST_F(XglRenderTest, YellowTriangle)
839{
840 static const char *vertShaderText =
841 "#version 130\n"
842 "void main() {\n"
843 " vec2 vertices[3];"
844 " vertices[0] = vec2(-0.5, -0.5);\n"
845 " vertices[1] = vec2( 0.5, -0.5);\n"
846 " vertices[2] = vec2( 0.5, 0.5);\n"
847 " vec4 colors[3];\n"
848 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
849 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
850 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
851 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
852 "}\n";
853
854 static const char *fragShaderText =
855 "#version 130\n"
856 "void main() {\n"
857 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
858 "}\n";
859
860 DrawTriangleTest(vertShaderText, fragShaderText);
861}
862
863TEST_F(XglRenderTest, TriangleTwoFSUniforms)
864{
865 static const char *vertShaderText =
866 "#version 130\n"
867 "out vec4 color;\n"
868 "out vec4 scale;\n"
869 "out vec2 samplePos;\n"
870 "void main() {\n"
871 " vec2 vertices[3];"
872 " vertices[0] = vec2(-0.5, -0.5);\n"
873 " vertices[1] = vec2( 0.5, -0.5);\n"
874 " vertices[2] = vec2( 0.5, 0.5);\n"
875 " vec4 colors[3];\n"
876 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
877 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
878 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
879 " color = colors[gl_VertexID % 3];\n"
880 " vec2 positions[3];"
881 " positions[0] = vec2( 0.0, 0.0);\n"
882 " positions[1] = vec2( 1.0, 0.0);\n"
883 " positions[2] = vec2( 1.0, 1.0);\n"
884 " scale = vec4(0.0, 0.0, 0.0, 0.0);\n"
885 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
886 "}\n";
887
888
889 static const char *fragShaderText =
890 "#version 430\n"
891 "in vec4 color;\n"
892 "in vec4 scale;\n"
893 "uniform vec4 foo;\n"
894 "uniform vec4 bar;\n"
895 "void main() {\n"
896 // by default, with no location or blocks
897 // the compiler will read them from buffer
898 // in reverse order of first use in shader
899 // The buffer contains red, followed by blue,
900 // so foo should be blue, bar should be red
901 " gl_FragColor = color * scale * foo * bar + foo;\n"
902 "}\n";
903
904 ASSERT_NO_FATAL_FAILURE(InitState());
905 ASSERT_NO_FATAL_FAILURE(InitViewport());
906
907 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
908 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
909
910 const int constantCount = 8;
911 const float constants[constantCount] = { 1.0, 0.0, 0.0, 1.0,
912 0.0, 0.0, 1.0, 1.0 };
913 XglConstantBufferObj constantBuffer(m_device,constantCount, sizeof(constants[0]), (const void*) constants);
914 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
915
916 XglPipelineObj pipelineobj(m_device);
917 pipelineobj.AddShader(&vs);
918 pipelineobj.AddShader(&ps);
919
920 XglDescriptorSetObj descriptorSet(m_device);
921 descriptorSet.AttachMemoryView(&constantBuffer);
922
Tony Barboura3953b82014-12-03 15:46:29 -0700923 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -0700924 QueueCommandBuffer(NULL, 0);
925
926}
927
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600928TEST_F(XglRenderTest, TriangleWithVertexFetch)
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600929{
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600930 static const char *vertShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700931 "#version 130\n"
932 //XYZ1( -1, -1, -1 )
933 "in vec4 pos;\n"
934 //XYZ1( 0.f, 0.f, 0.f )
935 "in vec4 inColor;\n"
936 "out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600937 "void main() {\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -0600938 " outColor = inColor;\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600939 " gl_Position = pos;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600940 "}\n";
941
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600942
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600943 static const char *fragShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700944 "#version 430\n"
945 "in vec4 color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600946 "void main() {\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600947 " gl_FragColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600948 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600949
Tony Barbourf43b6982014-11-25 13:18:32 -0700950
951
952 ASSERT_NO_FATAL_FAILURE(InitState());
953 ASSERT_NO_FATAL_FAILURE(InitViewport());
954
955 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
956 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
957
958 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
959 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
960
961 XglPipelineObj pipelineobj(m_device);
962 pipelineobj.AddShader(&vs);
963 pipelineobj.AddShader(&ps);
964
965 XglDescriptorSetObj descriptorSet(m_device);
966 descriptorSet.AttachMemoryView(&meshBuffer);
967
968 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
969 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
970 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
971 };
972
973 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
974 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
975 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
976 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
977 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
978 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
979 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
980 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
981 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
982
983 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
984 pipelineobj.AddVertexInputBindings(&vi_binding,1);
985 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
986
Tony Barboura3953b82014-12-03 15:46:29 -0700987 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -0700988 QueueCommandBuffer(NULL, 0);
989
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600990}
991
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700992TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
993{
994 static const char *vertShaderText =
995 "#version 140\n"
996 "#extension GL_ARB_separate_shader_objects : enable\n"
997 "#extension GL_ARB_shading_language_420pack : enable\n"
998 "layout(location = 0) in vec4 pos;\n"
999 "layout(location = 1) in vec4 inColor;\n"
1000 "layout(location = 0) out vec4 outColor;\n"
1001 "void main() {\n"
1002 " outColor = inColor;\n"
1003 " gl_Position = pos;\n"
1004 "}\n";
1005
1006
1007 static const char *fragShaderText =
1008 "#version 140\n"
1009 "#extension GL_ARB_separate_shader_objects : enable\n"
1010 "#extension GL_ARB_shading_language_420pack : enable\n"
1011 "layout(location = 0) in vec4 color;\n"
1012 "void main() {\n"
1013 " gl_FragColor = color;\n"
1014 "}\n";
1015
1016 const Vertex g_vbData[] =
1017 {
1018 // first tri
1019 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
1020 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1021 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1022
1023 // second tri
1024 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1025 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1026 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
1027 };
1028
1029 const uint16_t g_idxData[6] = {
1030 0, 1, 2,
1031 3, 4, 5,
1032 };
1033
1034 ASSERT_NO_FATAL_FAILURE(InitState());
1035 ASSERT_NO_FATAL_FAILURE(InitViewport());
1036
1037 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1038 meshBuffer.SetMemoryState(m_cmdBuffer, XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1039
1040 XglIndexBufferObj indexBuffer(m_device);
1041 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
1042 indexBuffer.SetMemoryState(m_cmdBuffer, XGL_MEMORY_STATE_INDEX_DATA);
1043
1044 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1045 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1046
1047 XglPipelineObj pipelineobj(m_device);
1048 pipelineobj.AddShader(&vs);
1049 pipelineobj.AddShader(&ps);
1050
1051 XglDescriptorSetObj descriptorSet(m_device);
1052
1053 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1054 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1055 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1056 };
1057
1058 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1059 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1060 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1061 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1062 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1063 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1064 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1065 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1066 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1067
1068 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1069 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1070
1071 XGL_RESULT err = XGL_SUCCESS;
1072
1073 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1074 // Build command buffer
1075 // TODO: SetMemoryState above also uses this command buffer, do we need to make sure
1076 // that job is complete? Or maybe we shouldn't queue those actions?
1077 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1078 ASSERT_XGL_SUCCESS(err);
1079
1080 GenerateClearAndPrepareBufferCmds();
1081 GenerateBindRenderTargetCmd();
1082#ifdef DUMP_STATE_DOT
1083 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
1084 pDSDumpDot((char*)"triTest.dot");
1085#endif
1086 GenerateBindStateAndPipelineCmds();
1087
1088 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
1089 descriptorSet.BindCommandBuffer(m_cmdBuffer);
1090
1091 meshBuffer.Bind(m_cmdBuffer, 0, 0);
1092 indexBuffer.Bind(m_cmdBuffer, /* offset */ 0);
1093
1094 // render two triangles
1095 xglCmdDrawIndexed(m_cmdBuffer, 0, 6, 0, 0, 1);
1096
1097 // finalize recording of the command buffer
1098 err = xglEndCommandBuffer( m_cmdBuffer );
1099 ASSERT_XGL_SUCCESS( err );
1100
1101 QueueCommandBuffer(NULL, 0);
1102
1103}
1104
GregF6bef1212014-12-02 15:41:44 -07001105TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1106{
1107 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001108
GregF6bef1212014-12-02 15:41:44 -07001109 static const char *vertShaderText =
1110 "#version 140\n"
1111 "#extension GL_ARB_separate_shader_objects : enable\n"
1112 "#extension GL_ARB_shading_language_420pack : enable\n"
1113 "layout (location = 0) in vec4 pos;\n"
1114 "layout (location = 0) out vec4 outColor;\n"
1115 "layout (location = 1) out vec4 outColor2;\n"
1116 "void main() {\n"
1117 " gl_Position = pos;\n"
1118 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1119 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1120 "}\n";
1121
1122 static const char *fragShaderText =
1123 //"#version 140\n"
1124 "#version 330\n"
1125 "#extension GL_ARB_separate_shader_objects : enable\n"
1126 "#extension GL_ARB_shading_language_420pack : enable\n"
1127 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1128 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1129 "layout (location = 0) in vec4 color;\n"
1130 "layout (location = 1) in vec4 color2;\n"
1131 "void main() {\n"
1132 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1133 " float dist_squared = dot(pos, pos);\n"
1134 " gl_FragColor = (dist_squared < 400.0)\n"
1135 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1136 " : color2;\n"
1137 "}\n";
1138
1139 ASSERT_NO_FATAL_FAILURE(InitState());
1140 ASSERT_NO_FATAL_FAILURE(InitViewport());
1141
1142 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1143 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1144
1145 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1146 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1147
1148 XglPipelineObj pipelineobj(m_device);
1149 pipelineobj.AddShader(&vs);
1150 pipelineobj.AddShader(&ps);
1151
1152 XglDescriptorSetObj descriptorSet(m_device);
1153 descriptorSet.AttachMemoryView(&meshBuffer);
1154
1155 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1156 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1157 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1158 };
1159
1160 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1161 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1162 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1163 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1164 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1165 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1166 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1167 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1168 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1169
1170 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1171 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1172 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1173
Tony Barbour09da2212014-12-03 16:13:23 -07001174 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001175 QueueCommandBuffer(NULL, 0);
1176
1177}
1178
1179TEST_F(XglRenderTest, RedCirclesonBlue)
1180{
1181 // This tests that we correctly handle unread fragment inputs
1182
1183 static const char *vertShaderText =
1184 "#version 140\n"
1185 "#extension GL_ARB_separate_shader_objects : enable\n"
1186 "#extension GL_ARB_shading_language_420pack : enable\n"
1187 "layout (location = 0) in vec4 pos;\n"
1188 "layout (location = 0) out vec4 outColor;\n"
1189 "layout (location = 1) out vec4 outColor2;\n"
1190 "void main() {\n"
1191 " gl_Position = pos;\n"
1192 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1193 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1194 "}\n";
1195
1196 static const char *fragShaderText =
1197 //"#version 140\n"
1198 "#version 330\n"
1199 "#extension GL_ARB_separate_shader_objects : enable\n"
1200 "#extension GL_ARB_shading_language_420pack : enable\n"
1201 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1202 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1203 "layout (location = 0) in vec4 color;\n"
1204 "layout (location = 1) in vec4 color2;\n"
1205 "void main() {\n"
1206 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1207 " float dist_squared = dot(pos, pos);\n"
1208 " gl_FragColor = (dist_squared < 400.0)\n"
1209 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1210 " : color2;\n"
1211 "}\n";
1212
1213 ASSERT_NO_FATAL_FAILURE(InitState());
1214 ASSERT_NO_FATAL_FAILURE(InitViewport());
1215
1216 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1217 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1218
1219 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1220 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1221
1222 XglPipelineObj pipelineobj(m_device);
1223 pipelineobj.AddShader(&vs);
1224 pipelineobj.AddShader(&ps);
1225
1226 XglDescriptorSetObj descriptorSet(m_device);
1227 descriptorSet.AttachMemoryView(&meshBuffer);
1228
1229 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1230 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1231 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1232 };
1233
1234 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1235 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1236 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1237 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1238 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1239 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1240 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1241 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1242 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1243
1244 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1245 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1246 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1247
Tony Barbour09da2212014-12-03 16:13:23 -07001248 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001249 QueueCommandBuffer(NULL, 0);
1250
1251}
1252
1253TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1254{
1255 // This tests reading gl_ClipDistance from FS
1256
1257 static const char *vertShaderText =
1258 "#version 330\n"
1259 "#extension GL_ARB_separate_shader_objects : enable\n"
1260 "#extension GL_ARB_shading_language_420pack : enable\n"
1261 "out gl_PerVertex {\n"
1262 " vec4 gl_Position;\n"
1263 " float gl_ClipDistance[1];\n"
1264 "};\n"
1265 "layout (location = 0) in vec4 pos;\n"
1266 "layout (location = 0) out vec4 outColor;\n"
1267 "layout (location = 1) out vec4 outColor2;\n"
1268 "void main() {\n"
1269 " gl_Position = pos;\n"
1270 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1271 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1272 " float dists[3];\n"
1273 " dists[0] = 0.0;\n"
1274 " dists[1] = 1.0;\n"
1275 " dists[2] = 1.0;\n"
1276 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1277 "}\n";
1278
1279
1280 static const char *fragShaderText =
1281 //"#version 140\n"
1282 "#version 330\n"
1283 "#extension GL_ARB_separate_shader_objects : enable\n"
1284 "#extension GL_ARB_shading_language_420pack : enable\n"
1285 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1286 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1287 "layout (location = 0) in vec4 color;\n"
1288 "layout (location = 1) in vec4 color2;\n"
1289 "void main() {\n"
1290 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1291 " float dist_squared = dot(pos, pos);\n"
1292 " gl_FragColor = (dist_squared < 400.0)\n"
1293 " ? color * gl_ClipDistance[0]\n"
1294 " : color2;\n"
1295 "}\n";
1296
1297 ASSERT_NO_FATAL_FAILURE(InitState());
1298 ASSERT_NO_FATAL_FAILURE(InitViewport());
1299
1300 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1301 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1302
1303 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1304 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1305
1306 XglPipelineObj pipelineobj(m_device);
1307 pipelineobj.AddShader(&vs);
1308 pipelineobj.AddShader(&ps);
1309
1310 XglDescriptorSetObj descriptorSet(m_device);
1311 descriptorSet.AttachMemoryView(&meshBuffer);
1312
1313 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1314 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1315 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1316 };
1317
1318 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1319 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1320 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1321 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1322 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1323 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1324 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1325 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1326 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1327
1328 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1329 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1330 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1331
Tony Barboura3953b82014-12-03 15:46:29 -07001332 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001333 QueueCommandBuffer(NULL, 0);
1334
1335}
Tony Barbourf43b6982014-11-25 13:18:32 -07001336
GregF7a23c792014-12-02 17:19:34 -07001337TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1338{
1339 static const char *vertShaderText =
1340 "#version 140\n"
1341 "#extension GL_ARB_separate_shader_objects : enable\n"
1342 "#extension GL_ARB_shading_language_420pack : enable\n"
1343 "layout (location = 0) in vec4 pos;\n"
1344 "layout (location = 0) out vec4 outColor;\n"
1345 "layout (location = 1) out vec4 outColor2;\n"
1346 "void main() {\n"
1347 " gl_Position = pos;\n"
1348 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1349 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1350 "}\n";
1351
1352
1353 static const char *fragShaderText =
1354 //"#version 140\n"
1355 "#version 330\n"
1356 "#extension GL_ARB_separate_shader_objects : enable\n"
1357 "#extension GL_ARB_shading_language_420pack : enable\n"
1358 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1359 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1360 "layout (location = 0) in vec4 color;\n"
1361 "layout (location = 1) in vec4 color2;\n"
1362 "void main() {\n"
1363 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1364 " float dist_squared = dot(pos, pos);\n"
1365 " if (dist_squared < 100.0)\n"
1366 " discard;\n"
1367 " gl_FragColor = (dist_squared < 400.0)\n"
1368 " ? color\n"
1369 " : color2;\n"
1370 "}\n";
1371
1372 ASSERT_NO_FATAL_FAILURE(InitState());
1373 ASSERT_NO_FATAL_FAILURE(InitViewport());
1374
1375 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1376 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1377
1378 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1379 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1380
1381 XglPipelineObj pipelineobj(m_device);
1382 pipelineobj.AddShader(&vs);
1383 pipelineobj.AddShader(&ps);
1384
1385 XglDescriptorSetObj descriptorSet(m_device);
1386 descriptorSet.AttachMemoryView(&meshBuffer);
1387
1388 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1389 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1390 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1391 };
1392
1393 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1394 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1395 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1396 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1397 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1398 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1399 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1400 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1401 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1402
1403 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1404 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1405 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1406
Tony Barbour09da2212014-12-03 16:13:23 -07001407 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF7a23c792014-12-02 17:19:34 -07001408 QueueCommandBuffer(NULL, 0);
1409
1410}
1411
1412
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001413TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001414{
1415 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001416 "#version 140\n"
1417 "#extension GL_ARB_separate_shader_objects : enable\n"
1418 "#extension GL_ARB_shading_language_420pack : enable\n"
1419 "\n"
1420 "layout(binding = 0) uniform buf {\n"
1421 " mat4 MVP;\n"
1422 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001423 "void main() {\n"
1424 " vec2 vertices[3];"
1425 " vertices[0] = vec2(-0.5, -0.5);\n"
1426 " vertices[1] = vec2( 0.5, -0.5);\n"
1427 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001428 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001429 "}\n";
1430
1431 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001432 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001433 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001434 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001435 "}\n";
1436
Tony Barbourf43b6982014-11-25 13:18:32 -07001437 ASSERT_NO_FATAL_FAILURE(InitState());
1438 ASSERT_NO_FATAL_FAILURE(InitViewport());
1439
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001440 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001441 glm::mat4 Projection = glm::mat4(1.0f);
1442 glm::mat4 View = glm::mat4(1.0f);
1443 glm::mat4 Model = glm::mat4(1.0f);
1444 glm::mat4 MVP = Projection * View * Model;
1445 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1446
Tony Barbourf43b6982014-11-25 13:18:32 -07001447 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1448 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1449 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001450
Tony Barbourf43b6982014-11-25 13:18:32 -07001451 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1452
1453 XglPipelineObj pipelineobj(m_device);
1454 pipelineobj.AddShader(&vs);
1455 pipelineobj.AddShader(&ps);
1456
1457 // Create descriptor set and attach the constant buffer to it
1458 XglDescriptorSetObj descriptorSet(m_device);
1459 descriptorSet.AttachMemoryView(&MVPBuffer);
1460
1461 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1462
Tony Barboura3953b82014-12-03 15:46:29 -07001463 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001464 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1465
Tony Barbour09da2212014-12-03 16:13:23 -07001466 RotateTriangleVSUniform(Projection, View, Model, &MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001467}
1468
Tony Barbourf43b6982014-11-25 13:18:32 -07001469TEST_F(XglRenderTest, MixTriangle)
1470{
1471 // This tests location applied to varyings. Notice that we have switched foo
1472 // and bar in the FS. The triangle should be blended with red, green and blue
1473 // corners.
1474 static const char *vertShaderText =
1475 "#version 140\n"
1476 "#extension GL_ARB_separate_shader_objects : enable\n"
1477 "#extension GL_ARB_shading_language_420pack : enable\n"
1478 "layout (location=0) out vec4 bar;\n"
1479 "layout (location=1) out vec4 foo;\n"
1480 "layout (location=2) out float scale;\n"
1481 "vec2 vertices[3];\n"
1482 "void main() {\n"
1483 " vertices[0] = vec2(-1.0, -1.0);\n"
1484 " vertices[1] = vec2( 1.0, -1.0);\n"
1485 " vertices[2] = vec2( 0.0, 1.0);\n"
1486 "vec4 colors[3];\n"
1487 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1488 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1489 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1490 " foo = colors[gl_VertexID % 3];\n"
1491 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1492 " scale = 1.0;\n"
1493 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1494 "}\n";
1495
1496 static const char *fragShaderText =
1497 "#version 140\n"
1498 "#extension GL_ARB_separate_shader_objects : enable\n"
1499 "#extension GL_ARB_shading_language_420pack : enable\n"
1500 "layout (location = 1) in vec4 bar;\n"
1501 "layout (location = 0) in vec4 foo;\n"
1502 "layout (location = 2) in float scale;\n"
1503 "void main() {\n"
1504 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1505 "}\n";
1506
1507 ASSERT_NO_FATAL_FAILURE(InitState());
1508 ASSERT_NO_FATAL_FAILURE(InitViewport());
1509
1510 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1511 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1512
1513 XglPipelineObj pipelineobj(m_device);
1514 pipelineobj.AddShader(&vs);
1515 pipelineobj.AddShader(&ps);
1516
1517 XglDescriptorSetObj descriptorSet(m_device);
1518
Tony Barboura3953b82014-12-03 15:46:29 -07001519 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001520 QueueCommandBuffer(NULL, 0);
1521}
1522
1523TEST_F(XglRenderTest, TriVertFetchAndVertID)
1524{
1525 // This tests that attributes work in the presence of gl_VertexID
1526
1527 static const char *vertShaderText =
1528 "#version 140\n"
1529 "#extension GL_ARB_separate_shader_objects : enable\n"
1530 "#extension GL_ARB_shading_language_420pack : enable\n"
1531 //XYZ1( -1, -1, -1 )
1532 "layout (location = 0) in vec4 pos;\n"
1533 //XYZ1( 0.f, 0.f, 0.f )
1534 "layout (location = 1) in vec4 inColor;\n"
1535 "layout (location = 0) out vec4 outColor;\n"
1536 "void main() {\n"
1537 " outColor = inColor;\n"
1538 " vec4 vertices[3];"
1539 " vertices[gl_VertexID % 3] = pos;\n"
1540 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1541 "}\n";
1542
1543
1544 static const char *fragShaderText =
1545 "#version 140\n"
1546 "#extension GL_ARB_separate_shader_objects : enable\n"
1547 "#extension GL_ARB_shading_language_420pack : enable\n"
1548 "layout (location = 0) in vec4 color;\n"
1549 "void main() {\n"
1550 " gl_FragColor = color;\n"
1551 "}\n";
1552
1553 ASSERT_NO_FATAL_FAILURE(InitState());
1554 ASSERT_NO_FATAL_FAILURE(InitViewport());
1555
1556 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1557 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1558
1559 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1560 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1561
1562 XglPipelineObj pipelineobj(m_device);
1563 pipelineobj.AddShader(&vs);
1564 pipelineobj.AddShader(&ps);
1565
1566 XglDescriptorSetObj descriptorSet(m_device);
1567 descriptorSet.AttachMemoryView(&meshBuffer);
1568
1569 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1570 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1571 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1572 };
1573
1574 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1575 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1576 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1577 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1578 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1579 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1580 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1581 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1582 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1583
1584 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1585 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1586 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1587
Tony Barboura3953b82014-12-03 15:46:29 -07001588 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001589 QueueCommandBuffer(NULL, 0);
1590}
1591
1592TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1593{
1594 // This tests that attributes work in the presence of gl_VertexID
1595 // and a dead attribute in position 0. Draws a triangle with yellow,
1596 // red and green corners, starting at top and going clockwise.
1597
1598 static const char *vertShaderText =
1599 "#version 140\n"
1600 "#extension GL_ARB_separate_shader_objects : enable\n"
1601 "#extension GL_ARB_shading_language_420pack : enable\n"
1602 //XYZ1( -1, -1, -1 )
1603 "layout (location = 0) in vec4 pos;\n"
1604 //XYZ1( 0.f, 0.f, 0.f )
1605 "layout (location = 1) in vec4 inColor;\n"
1606 "layout (location = 0) out vec4 outColor;\n"
1607 "void main() {\n"
1608 " outColor = inColor;\n"
1609 " vec2 vertices[3];"
1610 " vertices[0] = vec2(-1.0, -1.0);\n"
1611 " vertices[1] = vec2( 1.0, -1.0);\n"
1612 " vertices[2] = vec2( 0.0, 1.0);\n"
1613 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1614 "}\n";
1615
1616
1617 static const char *fragShaderText =
1618 "#version 140\n"
1619 "#extension GL_ARB_separate_shader_objects : enable\n"
1620 "#extension GL_ARB_shading_language_420pack : enable\n"
1621 "layout (location = 0) in vec4 color;\n"
1622 "void main() {\n"
1623 " gl_FragColor = color;\n"
1624 "}\n";
1625
1626 ASSERT_NO_FATAL_FAILURE(InitState());
1627 ASSERT_NO_FATAL_FAILURE(InitViewport());
1628
1629 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1630 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1631
1632 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1633 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1634
1635 XglPipelineObj pipelineobj(m_device);
1636 pipelineobj.AddShader(&vs);
1637 pipelineobj.AddShader(&ps);
1638
1639 XglDescriptorSetObj descriptorSet(m_device);
1640 descriptorSet.AttachMemoryView(&meshBuffer);
1641
1642 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1643 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1644 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1645 };
1646
1647 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1648 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1649 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1650 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1651 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1652 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1653 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1654 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1655 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1656
1657 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1658 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1659 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1660
Tony Barbour09da2212014-12-03 16:13:23 -07001661 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001662 QueueCommandBuffer(NULL, 0);
1663
1664}
1665
1666TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001667{
1668 static const char *vertShaderText =
1669 "#version 140\n"
1670 "layout (std140) uniform bufferVals {\n"
1671 " mat4 mvp;\n"
1672 "} myBufferVals;\n"
1673 "in vec4 pos;\n"
1674 "in vec4 inColor;\n"
1675 "out vec4 outColor;\n"
1676 "void main() {\n"
1677 " outColor = inColor;\n"
1678 " gl_Position = myBufferVals.mvp * pos;\n"
1679 "}\n";
1680
1681 static const char *fragShaderText =
1682 "#version 130\n"
1683 "in vec4 color;\n"
1684 "void main() {\n"
1685 " gl_FragColor = color;\n"
1686 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001687 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001688
Tony Barbourf43b6982014-11-25 13:18:32 -07001689 glm::mat4 View = glm::lookAt(
1690 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1691 glm::vec3(0,0,0), // and looks at the origin
1692 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1693 );
1694
1695 glm::mat4 Model = glm::mat4(1.0f);
1696
1697 glm::mat4 MVP = Projection * View * Model;
1698
1699 ASSERT_NO_FATAL_FAILURE(InitState());
1700 ASSERT_NO_FATAL_FAILURE(InitViewport());
1701 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1702
1703 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1704 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1705
1706 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1707
1708 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1709 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1710 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1711
1712 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1713
1714 XglPipelineObj pipelineobj(m_device);
1715 pipelineobj.AddShader(&vs);
1716 pipelineobj.AddShader(&ps);
1717
1718 XglDescriptorSetObj descriptorSet(m_device);
1719 descriptorSet.AttachMemoryView(&MVPBuffer);
1720
1721 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1722 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1723
1724 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1725 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1726 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1727 };
1728
1729 // this is the current description of g_vbData
1730 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1731 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1732 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1733 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1734 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1735 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1736 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1737 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1738 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1739
1740 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1741 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1742 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1743
1744 ClearDepthStencil(1.0f);
Tony Barboura3953b82014-12-03 15:46:29 -07001745 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07001746
1747 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1748
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001749}
1750
Tony Barbourf43b6982014-11-25 13:18:32 -07001751TEST_F(XglRenderTest, VSTexture)
1752{
1753 // The expected result from this test is a green and red triangle;
1754 // one red vertex on the left, two green vertices on the right.
1755 static const char *vertShaderText =
1756 "#version 130\n"
1757 "out vec4 texColor;\n"
1758 "uniform sampler2D surface;\n"
1759 "void main() {\n"
1760 " vec2 vertices[3];"
1761 " vertices[0] = vec2(-0.5, -0.5);\n"
1762 " vertices[1] = vec2( 0.5, -0.5);\n"
1763 " vertices[2] = vec2( 0.5, 0.5);\n"
1764 " vec2 positions[3];"
1765 " positions[0] = vec2( 0.0, 0.0);\n"
1766 " positions[1] = vec2( 0.25, 0.1);\n"
1767 " positions[2] = vec2( 0.1, 0.25);\n"
1768 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1769 " texColor = textureLod(surface, samplePos, 0.0);\n"
1770 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1771 "}\n";
1772
1773 static const char *fragShaderText =
1774 "#version 130\n"
1775 "in vec4 texColor;\n"
1776 "void main() {\n"
1777 " gl_FragColor = texColor;\n"
1778 "}\n";
1779
1780 ASSERT_NO_FATAL_FAILURE(InitState());
1781 ASSERT_NO_FATAL_FAILURE(InitViewport());
1782
1783 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1784 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1785 XglSamplerObj sampler(m_device);
1786 XglTextureObj texture(m_device);
1787
1788 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1789 vs.BindShaderEntitySlotToSampler(0, &sampler);
1790
1791 XglPipelineObj pipelineobj(m_device);
1792 pipelineobj.AddShader(&vs);
1793 pipelineobj.AddShader(&ps);
1794
1795 XglDescriptorSetObj descriptorSet(m_device);
1796 descriptorSet.AttachImageView(&texture);
1797 descriptorSet.AttachSampler(&sampler);
1798
1799 m_memoryRefManager.AddMemoryRef(&texture);
1800
Tony Barboura3953b82014-12-03 15:46:29 -07001801 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001802 QueueCommandBuffer(NULL, 0);
1803
1804}
1805TEST_F(XglRenderTest, TexturedTriangle)
1806{
1807 // The expected result from this test is a red and green checkered triangle
1808 static const char *vertShaderText =
1809 "#version 140\n"
1810 "#extension GL_ARB_separate_shader_objects : enable\n"
1811 "#extension GL_ARB_shading_language_420pack : enable\n"
1812 "layout (location = 0) out vec2 samplePos;\n"
1813 "void main() {\n"
1814 " vec2 vertices[3];"
1815 " vertices[0] = vec2(-0.5, -0.5);\n"
1816 " vertices[1] = vec2( 0.5, -0.5);\n"
1817 " vertices[2] = vec2( 0.5, 0.5);\n"
1818 " vec2 positions[3];"
1819 " positions[0] = vec2( 0.0, 0.0);\n"
1820 " positions[1] = vec2( 1.0, 0.0);\n"
1821 " positions[2] = vec2( 1.0, 1.0);\n"
1822 " samplePos = positions[gl_VertexID % 3];\n"
1823 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1824 "}\n";
1825
1826 static const char *fragShaderText =
1827 "#version 140\n"
1828 "#extension GL_ARB_separate_shader_objects : enable\n"
1829 "#extension GL_ARB_shading_language_420pack : enable\n"
1830 "layout (location = 0) in vec2 samplePos;\n"
1831 "layout (binding = 0) uniform sampler2D surface;\n"
1832 "layout (location=0) out vec4 outColor;\n"
1833 "void main() {\n"
1834 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1835 " outColor = texColor;\n"
1836 "}\n";
1837
1838 ASSERT_NO_FATAL_FAILURE(InitState());
1839 ASSERT_NO_FATAL_FAILURE(InitViewport());
1840
1841 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1842 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1843 XglSamplerObj sampler(m_device);
1844 XglTextureObj texture(m_device);
1845
1846 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1847 ps.BindShaderEntitySlotToSampler(0, &sampler);
1848
1849 XglPipelineObj pipelineobj(m_device);
1850 pipelineobj.AddShader(&vs);
1851 pipelineobj.AddShader(&ps);
1852
1853 XglDescriptorSetObj descriptorSet(m_device);
1854 descriptorSet.AttachImageView(&texture);
1855 descriptorSet.AttachSampler(&sampler);
1856
1857 m_memoryRefManager.AddMemoryRef(&texture);
1858
Tony Barboura3953b82014-12-03 15:46:29 -07001859 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001860 QueueCommandBuffer(NULL, 0);
1861}
1862TEST_F(XglRenderTest, TexturedTriangleClip)
1863{
1864 // The expected result from this test is a red and green checkered triangle
1865 static const char *vertShaderText =
1866 "#version 330\n"
1867 "#extension GL_ARB_separate_shader_objects : enable\n"
1868 "#extension GL_ARB_shading_language_420pack : enable\n"
1869 "layout (location = 0) out vec2 samplePos;\n"
1870 "out gl_PerVertex {\n"
1871 " vec4 gl_Position;\n"
1872 " float gl_ClipDistance[1];\n"
1873 "};\n"
1874 "void main() {\n"
1875 " vec2 vertices[3];"
1876 " vertices[0] = vec2(-0.5, -0.5);\n"
1877 " vertices[1] = vec2( 0.5, -0.5);\n"
1878 " vertices[2] = vec2( 0.5, 0.5);\n"
1879 " vec2 positions[3];"
1880 " positions[0] = vec2( 0.0, 0.0);\n"
1881 " positions[1] = vec2( 1.0, 0.0);\n"
1882 " positions[2] = vec2( 1.0, 1.0);\n"
1883 " float dists[3];\n"
1884 " dists[0] = 1.0;\n"
1885 " dists[1] = 1.0;\n"
1886 " dists[2] = -1.0;\n"
1887 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1888 " samplePos = positions[gl_VertexID % 3];\n"
1889 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1890 "}\n";
1891
1892 static const char *fragShaderText =
1893 "#version 140\n"
1894 "#extension GL_ARB_separate_shader_objects : enable\n"
1895 "#extension GL_ARB_shading_language_420pack : enable\n"
1896 "layout (location = 0) in vec2 samplePos;\n"
1897 "layout (binding = 0) uniform sampler2D surface;\n"
1898 "layout (location=0) out vec4 outColor;\n"
1899 "void main() {\n"
1900 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1901 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1902 " outColor = texColor;\n"
1903 "}\n";
1904
1905
1906 ASSERT_NO_FATAL_FAILURE(InitState());
1907 ASSERT_NO_FATAL_FAILURE(InitViewport());
1908
1909 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1910 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1911 XglSamplerObj sampler(m_device);
1912 XglTextureObj texture(m_device);
1913
1914 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1915 ps.BindShaderEntitySlotToSampler(0, &sampler);
1916
1917 XglPipelineObj pipelineobj(m_device);
1918 pipelineobj.AddShader(&vs);
1919 pipelineobj.AddShader(&ps);
1920
1921 XglDescriptorSetObj descriptorSet(m_device);
1922 descriptorSet.AttachImageView(&texture);
1923 descriptorSet.AttachSampler(&sampler);
1924
1925 m_memoryRefManager.AddMemoryRef(&texture);
1926
Tony Barboura3953b82014-12-03 15:46:29 -07001927 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001928 QueueCommandBuffer(NULL, 0);
1929}
1930TEST_F(XglRenderTest, FSTriangle)
1931{
1932 // The expected result from this test is a red and green checkered triangle
1933 static const char *vertShaderText =
1934 "#version 140\n"
1935 "#extension GL_ARB_separate_shader_objects : enable\n"
1936 "#extension GL_ARB_shading_language_420pack : enable\n"
1937 "layout (location = 0) out vec2 samplePos;\n"
1938 "void main() {\n"
1939 " vec2 vertices[3];"
1940 " vertices[0] = vec2(-0.5, -0.5);\n"
1941 " vertices[1] = vec2( 0.5, -0.5);\n"
1942 " vertices[2] = vec2( 0.5, 0.5);\n"
1943 " vec2 positions[3];"
1944 " positions[0] = vec2( 0.0, 0.0);\n"
1945 " positions[1] = vec2( 1.0, 0.0);\n"
1946 " positions[2] = vec2( 1.0, 1.0);\n"
1947 " samplePos = positions[gl_VertexID % 3];\n"
1948 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1949 "}\n";
1950
1951 static const char *fragShaderText =
1952 "#version 140\n"
1953 "#extension GL_ARB_separate_shader_objects : enable\n"
1954 "#extension GL_ARB_shading_language_420pack : enable\n"
1955 "layout (location = 0) in vec2 samplePos;\n"
1956 "layout (binding = 0) uniform sampler2D surface;\n"
1957 "layout (location=0) out vec4 outColor;\n"
1958 "void main() {\n"
1959 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1960 " outColor = texColor;\n"
1961 "}\n";
1962
1963 ASSERT_NO_FATAL_FAILURE(InitState());
1964 ASSERT_NO_FATAL_FAILURE(InitViewport());
1965
1966 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1967 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1968 XglSamplerObj sampler(m_device);
1969 XglTextureObj texture(m_device);
1970
1971 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1972 ps.BindShaderEntitySlotToSampler(0, &sampler);
1973
1974 XglPipelineObj pipelineobj(m_device);
1975 pipelineobj.AddShader(&vs);
1976 pipelineobj.AddShader(&ps);
1977
1978 XglDescriptorSetObj descriptorSet(m_device);
1979 descriptorSet.AttachImageView(&texture);
1980 descriptorSet.AttachSampler(&sampler);
1981
1982 m_memoryRefManager.AddMemoryRef(&texture);
1983
Tony Barboura3953b82014-12-03 15:46:29 -07001984 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001985 QueueCommandBuffer(NULL, 0);
1986}
1987TEST_F(XglRenderTest, SamplerBindingsTriangle)
1988{
1989 // This test sets bindings on the samplers
1990 // For now we are asserting that sampler and texture pairs
1991 // march in lock step, and are set via GLSL binding. This can
1992 // and will probably change.
1993 // The sampler bindings should match the sampler and texture slot
1994 // number set up by the application.
1995 // This test will result in a blue triangle
1996 static const char *vertShaderText =
1997 "#version 140\n"
1998 "#extension GL_ARB_separate_shader_objects : enable\n"
1999 "#extension GL_ARB_shading_language_420pack : enable\n"
2000 "layout (location = 0) out vec4 samplePos;\n"
2001 "void main() {\n"
2002 " vec2 vertices[3];"
2003 " vertices[0] = vec2(-0.5, -0.5);\n"
2004 " vertices[1] = vec2( 0.5, -0.5);\n"
2005 " vertices[2] = vec2( 0.5, 0.5);\n"
2006 " vec2 positions[3];"
2007 " positions[0] = vec2( 0.0, 0.0);\n"
2008 " positions[1] = vec2( 1.0, 0.0);\n"
2009 " positions[2] = vec2( 1.0, 1.0);\n"
2010 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
2011 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2012 "}\n";
2013
2014 static const char *fragShaderText =
2015 "#version 140\n"
2016 "#extension GL_ARB_separate_shader_objects : enable\n"
2017 "#extension GL_ARB_shading_language_420pack : enable\n"
2018 "layout (location = 0) in vec4 samplePos;\n"
2019 "layout (binding = 0) uniform sampler2D surface0;\n"
2020 "layout (binding = 1) uniform sampler2D surface1;\n"
2021 "layout (binding = 12) uniform sampler2D surface2;\n"
2022 "void main() {\n"
2023 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\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 XglSamplerObj sampler1(m_device);
2033 XglSamplerObj sampler2(m_device);
2034 XglSamplerObj sampler3(m_device);
2035
2036 XglTextureObj texture1(m_device); // Red
2037 texture1.ChangeColors(0xffff0000,0xffff0000);
2038 XglTextureObj texture2(m_device); // Green
2039 texture2.ChangeColors(0xff00ff00,0xff00ff00);
2040 XglTextureObj texture3(m_device); // Blue
2041 texture3.ChangeColors(0xff0000ff,0xff0000ff);
2042
2043 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture1);
2044 ps.BindShaderEntitySlotToSampler(0, &sampler1);
2045
2046 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_RESOURCE, &texture2);
2047 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2048
2049 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_RESOURCE, &texture3);
2050 ps.BindShaderEntitySlotToSampler(12, &sampler3);
2051
2052 XglPipelineObj pipelineobj(m_device);
2053 pipelineobj.AddShader(&vs);
2054 pipelineobj.AddShader(&ps);
2055
2056 XglDescriptorSetObj descriptorSet(m_device);
2057 descriptorSet.AttachImageView(&texture1);
2058 descriptorSet.AttachSampler(&sampler1);
2059 descriptorSet.AttachImageView(&texture2);
2060 descriptorSet.AttachSampler(&sampler2);
2061 descriptorSet.AttachImageView(&texture3);
2062 descriptorSet.AttachSampler(&sampler3);
2063
2064 m_memoryRefManager.AddMemoryRef(&texture1);
2065 m_memoryRefManager.AddMemoryRef(&texture2);
2066 m_memoryRefManager.AddMemoryRef(&texture3);
2067
Tony Barboura3953b82014-12-03 15:46:29 -07002068 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002069 QueueCommandBuffer(NULL, 0);
2070
2071}
2072
2073TEST_F(XglRenderTest, TriangleVSUniformBlock)
2074{
2075 // The expected result from this test is a blue triangle
2076
2077 static const char *vertShaderText =
2078 "#version 140\n"
2079 "#extension GL_ARB_separate_shader_objects : enable\n"
2080 "#extension GL_ARB_shading_language_420pack : enable\n"
2081 "layout (location = 0) out vec4 outColor;\n"
2082 "layout (std140, binding = 0) uniform bufferVals {\n"
2083 " vec4 red;\n"
2084 " vec4 green;\n"
2085 " vec4 blue;\n"
2086 " vec4 white;\n"
2087 "} myBufferVals;\n"
2088 "void main() {\n"
2089 " vec2 vertices[3];"
2090 " vertices[0] = vec2(-0.5, -0.5);\n"
2091 " vertices[1] = vec2( 0.5, -0.5);\n"
2092 " vertices[2] = vec2( 0.5, 0.5);\n"
2093 " outColor = myBufferVals.blue;\n"
2094 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2095 "}\n";
2096
2097 static const char *fragShaderText =
2098 "#version 140\n"
2099 "#extension GL_ARB_separate_shader_objects : enable\n"
2100 "#extension GL_ARB_shading_language_420pack : enable\n"
2101 "layout (location = 0) in vec4 inColor;\n"
2102 "void main() {\n"
2103 " gl_FragColor = inColor;\n"
2104 "}\n";
2105
2106 ASSERT_NO_FATAL_FAILURE(InitState());
2107 ASSERT_NO_FATAL_FAILURE(InitViewport());
2108
2109 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2110 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2111
2112 // Let's populate our buffer with the following:
2113 // vec4 red;
2114 // vec4 green;
2115 // vec4 blue;
2116 // vec4 white;
2117 const int valCount = 4 * 4;
2118 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2119 0.0, 1.0, 0.0, 1.0,
2120 0.0, 0.0, 1.0, 1.0,
2121 1.0, 1.0, 1.0, 1.0 };
2122
2123 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2124 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2125
2126 XglPipelineObj pipelineobj(m_device);
2127 pipelineobj.AddShader(&vs);
2128 pipelineobj.AddShader(&ps);
2129
2130 XglDescriptorSetObj descriptorSet(m_device);
2131 descriptorSet.AttachMemoryView(&colorBuffer);
2132
Tony Barboura3953b82014-12-03 15:46:29 -07002133 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002134 QueueCommandBuffer(NULL, 0);
2135
2136}
2137
2138TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2139{
2140 // This test allows the shader to select which buffer it is
2141 // pulling from using layout binding qualifier.
2142 // There are corresponding changes in the compiler stack that
2143 // will select the buffer using binding directly.
2144 // The binding number should match the slot number set up by
2145 // the application.
2146 // The expected result from this test is a purple triangle
2147
2148 static const char *vertShaderText =
2149 "#version 140\n"
2150 "#extension GL_ARB_separate_shader_objects : enable\n"
2151 "#extension GL_ARB_shading_language_420pack : enable\n"
2152 "void main() {\n"
2153 " vec2 vertices[3];"
2154 " vertices[0] = vec2(-0.5, -0.5);\n"
2155 " vertices[1] = vec2( 0.5, -0.5);\n"
2156 " vertices[2] = vec2( 0.5, 0.5);\n"
2157 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2158 "}\n";
2159
2160 static const char *fragShaderText =
2161 "#version 140\n"
2162 "#extension GL_ARB_separate_shader_objects : enable\n"
2163 "#extension GL_ARB_shading_language_420pack : enable\n"
2164 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2165 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2166 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2167 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2168 "void main() {\n"
2169 " gl_FragColor = myBlueVal.color;\n"
2170 " gl_FragColor += myRedVal.color;\n"
2171 "}\n";
2172
2173 ASSERT_NO_FATAL_FAILURE(InitState());
2174 ASSERT_NO_FATAL_FAILURE(InitViewport());
2175
2176 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2177 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2178
2179 // We're going to create a number of uniform buffers, and then allow
2180 // the shader to select which it wants to read from with a binding
2181
2182 // Let's populate the buffers with a single color each:
2183 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2184 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2185 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2186 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2187
2188 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2189 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2190 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2191 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2192
2193 const int redCount = sizeof(redVals) / sizeof(float);
2194 const int greenCount = sizeof(greenVals) / sizeof(float);
2195 const int blueCount = sizeof(blueVals) / sizeof(float);
2196 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2197
2198 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2199 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2200
2201 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2202 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2203
2204 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2205 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2206
2207 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2208 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2209
2210 XglPipelineObj pipelineobj(m_device);
2211 pipelineobj.AddShader(&vs);
2212 pipelineobj.AddShader(&ps);
2213
2214 XglDescriptorSetObj descriptorSet(m_device);
2215 descriptorSet.AttachMemoryView(&redBuffer);
2216 descriptorSet.AttachMemoryView(&greenBuffer);
2217 descriptorSet.AttachMemoryView(&blueBuffer);
2218 descriptorSet.AttachMemoryView(&whiteBuffer);
2219
Tony Barboura3953b82014-12-03 15:46:29 -07002220 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002221 QueueCommandBuffer(NULL, 0);
2222
2223}
2224
2225TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2226{
2227 // This test is the same as TriangleFSUniformBlockBinding, but
2228 // it does not provide an instance name.
2229 // The expected result from this test is a purple triangle
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 "void main() {\n"
2236 " vec2 vertices[3];"
2237 " vertices[0] = vec2(-0.5, -0.5);\n"
2238 " vertices[1] = vec2( 0.5, -0.5);\n"
2239 " vertices[2] = vec2( 0.5, 0.5);\n"
2240 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2241 "}\n";
2242
2243 static const char *fragShaderText =
2244 "#version 430\n"
2245 "#extension GL_ARB_separate_shader_objects : enable\n"
2246 "#extension GL_ARB_shading_language_420pack : enable\n"
2247 "layout (location = 0) uniform vec4 foo;\n"
2248 "layout (location = 1) uniform vec4 bar;\n"
2249 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2250 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2251 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2252 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
2253 "void main() {\n"
2254 " gl_FragColor = blue;\n"
2255 " gl_FragColor += red;\n"
2256 "}\n";
2257 ASSERT_NO_FATAL_FAILURE(InitState());
2258 ASSERT_NO_FATAL_FAILURE(InitViewport());
2259
2260 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2261 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2262
2263 // We're going to create a number of uniform buffers, and then allow
2264 // the shader to select which it wants to read from with a binding
2265
2266 // Let's populate the buffers with a single color each:
2267 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2268 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2269 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2270 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2271
2272 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2273 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2274 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2275 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2276
2277 const int redCount = sizeof(redVals) / sizeof(float);
2278 const int greenCount = sizeof(greenVals) / sizeof(float);
2279 const int blueCount = sizeof(blueVals) / sizeof(float);
2280 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2281
2282 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2283 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2284
2285 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2286 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2287
2288 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2289 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2290
2291 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2292 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2293
2294 XglPipelineObj pipelineobj(m_device);
2295 pipelineobj.AddShader(&vs);
2296 pipelineobj.AddShader(&ps);
2297
2298 XglDescriptorSetObj descriptorSet(m_device);
2299 descriptorSet.AttachMemoryView(&redBuffer);
2300 descriptorSet.AttachMemoryView(&greenBuffer);
2301 descriptorSet.AttachMemoryView(&blueBuffer);
2302 descriptorSet.AttachMemoryView(&whiteBuffer);
2303
Tony Barboura3953b82014-12-03 15:46:29 -07002304 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002305 QueueCommandBuffer(NULL, 0);
2306
2307}
2308
2309TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2310{
2311 static const char *vertShaderText =
2312 "#version 140\n"
2313 "#extension GL_ARB_separate_shader_objects : enable\n"
2314 "#extension GL_ARB_shading_language_420pack : enable\n"
2315 "layout (std140, binding=0) uniform bufferVals {\n"
2316 " mat4 mvp;\n"
2317 "} myBufferVals;\n"
2318 "layout (location=0) in vec4 pos;\n"
2319 "layout (location=0) out vec2 UV;\n"
2320 "void main() {\n"
2321 " vec2 positions[3];"
2322 " positions[0] = vec2( 0.0, 0.0);\n"
2323 " positions[1] = vec2( 0.25, 0.1);\n"
2324 " positions[2] = vec2( 0.1, 0.25);\n"
2325 " UV = positions[gl_VertexID % 3];\n"
2326 " gl_Position = myBufferVals.mvp * pos;\n"
2327 "}\n";
2328
2329 static const char *fragShaderText =
2330 "#version 140\n"
2331 "#extension GL_ARB_separate_shader_objects : enable\n"
2332 "#extension GL_ARB_shading_language_420pack : enable\n"
2333 "layout (binding=0) uniform sampler2D surface;\n"
2334 "layout (location=0) out vec4 outColor;\n"
2335 "layout (location=0) in vec2 UV;\n"
2336 "void main() {\n"
2337 " outColor= textureLod(surface, UV, 0.0);\n"
2338 "}\n";
2339 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2340
2341 glm::mat4 View = glm::lookAt(
2342 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2343 glm::vec3(0,0,0), // and looks at the origin
2344 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2345 );
2346
2347 glm::mat4 Model = glm::mat4(1.0f);
2348
2349 glm::mat4 MVP = Projection * View * Model;
2350
2351
2352 ASSERT_NO_FATAL_FAILURE(InitState());
2353 ASSERT_NO_FATAL_FAILURE(InitViewport());
2354 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2355
2356 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2357 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
2358 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
2359
2360
2361 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2362
2363 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2364 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2365 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2366 XglSamplerObj sampler(m_device);
2367 XglTextureObj texture(m_device);
2368
2369 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2370 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
2371 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
2372 ps.BindShaderEntitySlotToSampler(0, &sampler);
2373
2374 XglPipelineObj pipelineobj(m_device);
2375 pipelineobj.AddShader(&vs);
2376 pipelineobj.AddShader(&ps);
2377
2378 XglDescriptorSetObj descriptorSet(m_device);
2379
2380 descriptorSet.AttachMemoryView(&mvpBuffer);
2381 descriptorSet.AttachImageView(&texture);
2382 descriptorSet.AttachSampler(&sampler);
2383
2384 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2385 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2386 m_memoryRefManager.AddMemoryRef(&texture);
2387
2388 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2389 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2390 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2391 };
2392
2393 // this is the current description of g_vbData
2394 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2395 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2396 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2397 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2398 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2399 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2400 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2401 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2402 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2403
2404 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2405 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2406 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2407
2408 ClearDepthStencil(1.0f);
Tony Barboura3953b82014-12-03 15:46:29 -07002409 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07002410
2411 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2412
2413}
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002414int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002415 int result;
2416
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002417 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002418 XglTestFramework::InitArgs(&argc, argv);
2419
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002420 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
2421
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002422 result = RUN_ALL_TESTS();
2423
2424 XglTestFramework::Finish();
2425 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002426}