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