blob: 94b96afec9bdf241f02f245e4dd525ad47f0906e [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
Tony Barbourf43b6982014-11-25 13:18:32 -07001025
1026
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001027TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001028{
1029 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001030 "#version 140\n"
1031 "#extension GL_ARB_separate_shader_objects : enable\n"
1032 "#extension GL_ARB_shading_language_420pack : enable\n"
1033 "\n"
1034 "layout(binding = 0) uniform buf {\n"
1035 " mat4 MVP;\n"
1036 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001037 "void main() {\n"
1038 " vec2 vertices[3];"
1039 " vertices[0] = vec2(-0.5, -0.5);\n"
1040 " vertices[1] = vec2( 0.5, -0.5);\n"
1041 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001042 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001043 "}\n";
1044
1045 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001046 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001047 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001048 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001049 "}\n";
1050
Tony Barbourf43b6982014-11-25 13:18:32 -07001051 ASSERT_NO_FATAL_FAILURE(InitState());
1052 ASSERT_NO_FATAL_FAILURE(InitViewport());
1053
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001054 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001055 glm::mat4 Projection = glm::mat4(1.0f);
1056 glm::mat4 View = glm::mat4(1.0f);
1057 glm::mat4 Model = glm::mat4(1.0f);
1058 glm::mat4 MVP = Projection * View * Model;
1059 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1060
Tony Barbourf43b6982014-11-25 13:18:32 -07001061 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1062 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1063 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001064
Tony Barbourf43b6982014-11-25 13:18:32 -07001065 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1066
1067 XglPipelineObj pipelineobj(m_device);
1068 pipelineobj.AddShader(&vs);
1069 pipelineobj.AddShader(&ps);
1070
1071 // Create descriptor set and attach the constant buffer to it
1072 XglDescriptorSetObj descriptorSet(m_device);
1073 descriptorSet.AttachMemoryView(&MVPBuffer);
1074
1075 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1076
1077 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1078 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1079
1080 RotateTriangleVSUniform(Projection, View, Model, MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001081}
1082
Tony Barbourf43b6982014-11-25 13:18:32 -07001083TEST_F(XglRenderTest, MixTriangle)
1084{
1085 // This tests location applied to varyings. Notice that we have switched foo
1086 // and bar in the FS. The triangle should be blended with red, green and blue
1087 // corners.
1088 static const char *vertShaderText =
1089 "#version 140\n"
1090 "#extension GL_ARB_separate_shader_objects : enable\n"
1091 "#extension GL_ARB_shading_language_420pack : enable\n"
1092 "layout (location=0) out vec4 bar;\n"
1093 "layout (location=1) out vec4 foo;\n"
1094 "layout (location=2) out float scale;\n"
1095 "vec2 vertices[3];\n"
1096 "void main() {\n"
1097 " vertices[0] = vec2(-1.0, -1.0);\n"
1098 " vertices[1] = vec2( 1.0, -1.0);\n"
1099 " vertices[2] = vec2( 0.0, 1.0);\n"
1100 "vec4 colors[3];\n"
1101 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1102 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1103 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1104 " foo = colors[gl_VertexID % 3];\n"
1105 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1106 " scale = 1.0;\n"
1107 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1108 "}\n";
1109
1110 static const char *fragShaderText =
1111 "#version 140\n"
1112 "#extension GL_ARB_separate_shader_objects : enable\n"
1113 "#extension GL_ARB_shading_language_420pack : enable\n"
1114 "layout (location = 1) in vec4 bar;\n"
1115 "layout (location = 0) in vec4 foo;\n"
1116 "layout (location = 2) in float scale;\n"
1117 "void main() {\n"
1118 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1119 "}\n";
1120
1121 ASSERT_NO_FATAL_FAILURE(InitState());
1122 ASSERT_NO_FATAL_FAILURE(InitViewport());
1123
1124 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1125 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1126
1127 XglPipelineObj pipelineobj(m_device);
1128 pipelineobj.AddShader(&vs);
1129 pipelineobj.AddShader(&ps);
1130
1131 XglDescriptorSetObj descriptorSet(m_device);
1132
1133 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1134 QueueCommandBuffer(NULL, 0);
1135}
1136
1137TEST_F(XglRenderTest, TriVertFetchAndVertID)
1138{
1139 // This tests that attributes work in the presence of gl_VertexID
1140
1141 static const char *vertShaderText =
1142 "#version 140\n"
1143 "#extension GL_ARB_separate_shader_objects : enable\n"
1144 "#extension GL_ARB_shading_language_420pack : enable\n"
1145 //XYZ1( -1, -1, -1 )
1146 "layout (location = 0) in vec4 pos;\n"
1147 //XYZ1( 0.f, 0.f, 0.f )
1148 "layout (location = 1) in vec4 inColor;\n"
1149 "layout (location = 0) out vec4 outColor;\n"
1150 "void main() {\n"
1151 " outColor = inColor;\n"
1152 " vec4 vertices[3];"
1153 " vertices[gl_VertexID % 3] = pos;\n"
1154 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1155 "}\n";
1156
1157
1158 static const char *fragShaderText =
1159 "#version 140\n"
1160 "#extension GL_ARB_separate_shader_objects : enable\n"
1161 "#extension GL_ARB_shading_language_420pack : enable\n"
1162 "layout (location = 0) in vec4 color;\n"
1163 "void main() {\n"
1164 " gl_FragColor = color;\n"
1165 "}\n";
1166
1167 ASSERT_NO_FATAL_FAILURE(InitState());
1168 ASSERT_NO_FATAL_FAILURE(InitViewport());
1169
1170 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1171 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1172
1173 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1174 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1175
1176 XglPipelineObj pipelineobj(m_device);
1177 pipelineobj.AddShader(&vs);
1178 pipelineobj.AddShader(&ps);
1179
1180 XglDescriptorSetObj descriptorSet(m_device);
1181 descriptorSet.AttachMemoryView(&meshBuffer);
1182
1183 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1184 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1185 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1186 };
1187
1188 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1189 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1190 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1191 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1192 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1193 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1194 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1195 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1196 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1197
1198 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1199 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1200 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1201
1202 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1203 QueueCommandBuffer(NULL, 0);
1204}
1205
1206TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1207{
1208 // This tests that attributes work in the presence of gl_VertexID
1209 // and a dead attribute in position 0. Draws a triangle with yellow,
1210 // red and green corners, starting at top and going clockwise.
1211
1212 static const char *vertShaderText =
1213 "#version 140\n"
1214 "#extension GL_ARB_separate_shader_objects : enable\n"
1215 "#extension GL_ARB_shading_language_420pack : enable\n"
1216 //XYZ1( -1, -1, -1 )
1217 "layout (location = 0) in vec4 pos;\n"
1218 //XYZ1( 0.f, 0.f, 0.f )
1219 "layout (location = 1) in vec4 inColor;\n"
1220 "layout (location = 0) out vec4 outColor;\n"
1221 "void main() {\n"
1222 " outColor = inColor;\n"
1223 " vec2 vertices[3];"
1224 " vertices[0] = vec2(-1.0, -1.0);\n"
1225 " vertices[1] = vec2( 1.0, -1.0);\n"
1226 " vertices[2] = vec2( 0.0, 1.0);\n"
1227 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1228 "}\n";
1229
1230
1231 static const char *fragShaderText =
1232 "#version 140\n"
1233 "#extension GL_ARB_separate_shader_objects : enable\n"
1234 "#extension GL_ARB_shading_language_420pack : enable\n"
1235 "layout (location = 0) in vec4 color;\n"
1236 "void main() {\n"
1237 " gl_FragColor = color;\n"
1238 "}\n";
1239
1240 ASSERT_NO_FATAL_FAILURE(InitState());
1241 ASSERT_NO_FATAL_FAILURE(InitViewport());
1242
1243 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1244 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1245
1246 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1247 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1248
1249 XglPipelineObj pipelineobj(m_device);
1250 pipelineobj.AddShader(&vs);
1251 pipelineobj.AddShader(&ps);
1252
1253 XglDescriptorSetObj descriptorSet(m_device);
1254 descriptorSet.AttachMemoryView(&meshBuffer);
1255
1256 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1257 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1258 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1259 };
1260
1261 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1262 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1263 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1264 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1265 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1266 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1267 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1268 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1269 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1270
1271 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1272 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1273 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1274
1275 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1276 QueueCommandBuffer(NULL, 0);
1277
1278}
1279
1280TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001281{
1282 static const char *vertShaderText =
1283 "#version 140\n"
1284 "layout (std140) uniform bufferVals {\n"
1285 " mat4 mvp;\n"
1286 "} myBufferVals;\n"
1287 "in vec4 pos;\n"
1288 "in vec4 inColor;\n"
1289 "out vec4 outColor;\n"
1290 "void main() {\n"
1291 " outColor = inColor;\n"
1292 " gl_Position = myBufferVals.mvp * pos;\n"
1293 "}\n";
1294
1295 static const char *fragShaderText =
1296 "#version 130\n"
1297 "in vec4 color;\n"
1298 "void main() {\n"
1299 " gl_FragColor = color;\n"
1300 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001301 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001302
Tony Barbourf43b6982014-11-25 13:18:32 -07001303 glm::mat4 View = glm::lookAt(
1304 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1305 glm::vec3(0,0,0), // and looks at the origin
1306 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1307 );
1308
1309 glm::mat4 Model = glm::mat4(1.0f);
1310
1311 glm::mat4 MVP = Projection * View * Model;
1312
1313 ASSERT_NO_FATAL_FAILURE(InitState());
1314 ASSERT_NO_FATAL_FAILURE(InitViewport());
1315 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1316
1317 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1318 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1319
1320 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1321
1322 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1323 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1324 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1325
1326 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1327
1328 XglPipelineObj pipelineobj(m_device);
1329 pipelineobj.AddShader(&vs);
1330 pipelineobj.AddShader(&ps);
1331
1332 XglDescriptorSetObj descriptorSet(m_device);
1333 descriptorSet.AttachMemoryView(&MVPBuffer);
1334
1335 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1336 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1337
1338 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1339 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1340 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1341 };
1342
1343 // this is the current description of g_vbData
1344 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1345 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1346 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1347 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1348 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1349 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1350 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1351 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1352 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1353
1354 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1355 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1356 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1357
1358 ClearDepthStencil(1.0f);
1359 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
1360
1361 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1362
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001363}
1364
Tony Barbourf43b6982014-11-25 13:18:32 -07001365TEST_F(XglRenderTest, VSTexture)
1366{
1367 // The expected result from this test is a green and red triangle;
1368 // one red vertex on the left, two green vertices on the right.
1369 static const char *vertShaderText =
1370 "#version 130\n"
1371 "out vec4 texColor;\n"
1372 "uniform sampler2D surface;\n"
1373 "void main() {\n"
1374 " vec2 vertices[3];"
1375 " vertices[0] = vec2(-0.5, -0.5);\n"
1376 " vertices[1] = vec2( 0.5, -0.5);\n"
1377 " vertices[2] = vec2( 0.5, 0.5);\n"
1378 " vec2 positions[3];"
1379 " positions[0] = vec2( 0.0, 0.0);\n"
1380 " positions[1] = vec2( 0.25, 0.1);\n"
1381 " positions[2] = vec2( 0.1, 0.25);\n"
1382 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1383 " texColor = textureLod(surface, samplePos, 0.0);\n"
1384 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1385 "}\n";
1386
1387 static const char *fragShaderText =
1388 "#version 130\n"
1389 "in vec4 texColor;\n"
1390 "void main() {\n"
1391 " gl_FragColor = texColor;\n"
1392 "}\n";
1393
1394 ASSERT_NO_FATAL_FAILURE(InitState());
1395 ASSERT_NO_FATAL_FAILURE(InitViewport());
1396
1397 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1398 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1399 XglSamplerObj sampler(m_device);
1400 XglTextureObj texture(m_device);
1401
1402 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1403 vs.BindShaderEntitySlotToSampler(0, &sampler);
1404
1405 XglPipelineObj pipelineobj(m_device);
1406 pipelineobj.AddShader(&vs);
1407 pipelineobj.AddShader(&ps);
1408
1409 XglDescriptorSetObj descriptorSet(m_device);
1410 descriptorSet.AttachImageView(&texture);
1411 descriptorSet.AttachSampler(&sampler);
1412
1413 m_memoryRefManager.AddMemoryRef(&texture);
1414
1415 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1416 QueueCommandBuffer(NULL, 0);
1417
1418}
1419TEST_F(XglRenderTest, TexturedTriangle)
1420{
1421 // The expected result from this test is a red and green checkered triangle
1422 static const char *vertShaderText =
1423 "#version 140\n"
1424 "#extension GL_ARB_separate_shader_objects : enable\n"
1425 "#extension GL_ARB_shading_language_420pack : enable\n"
1426 "layout (location = 0) out vec2 samplePos;\n"
1427 "void main() {\n"
1428 " vec2 vertices[3];"
1429 " vertices[0] = vec2(-0.5, -0.5);\n"
1430 " vertices[1] = vec2( 0.5, -0.5);\n"
1431 " vertices[2] = vec2( 0.5, 0.5);\n"
1432 " vec2 positions[3];"
1433 " positions[0] = vec2( 0.0, 0.0);\n"
1434 " positions[1] = vec2( 1.0, 0.0);\n"
1435 " positions[2] = vec2( 1.0, 1.0);\n"
1436 " samplePos = positions[gl_VertexID % 3];\n"
1437 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1438 "}\n";
1439
1440 static const char *fragShaderText =
1441 "#version 140\n"
1442 "#extension GL_ARB_separate_shader_objects : enable\n"
1443 "#extension GL_ARB_shading_language_420pack : enable\n"
1444 "layout (location = 0) in vec2 samplePos;\n"
1445 "layout (binding = 0) uniform sampler2D surface;\n"
1446 "layout (location=0) out vec4 outColor;\n"
1447 "void main() {\n"
1448 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1449 " outColor = texColor;\n"
1450 "}\n";
1451
1452 ASSERT_NO_FATAL_FAILURE(InitState());
1453 ASSERT_NO_FATAL_FAILURE(InitViewport());
1454
1455 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1456 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1457 XglSamplerObj sampler(m_device);
1458 XglTextureObj texture(m_device);
1459
1460 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1461 ps.BindShaderEntitySlotToSampler(0, &sampler);
1462
1463 XglPipelineObj pipelineobj(m_device);
1464 pipelineobj.AddShader(&vs);
1465 pipelineobj.AddShader(&ps);
1466
1467 XglDescriptorSetObj descriptorSet(m_device);
1468 descriptorSet.AttachImageView(&texture);
1469 descriptorSet.AttachSampler(&sampler);
1470
1471 m_memoryRefManager.AddMemoryRef(&texture);
1472
1473 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1474 QueueCommandBuffer(NULL, 0);
1475}
1476TEST_F(XglRenderTest, TexturedTriangleClip)
1477{
1478 // The expected result from this test is a red and green checkered triangle
1479 static const char *vertShaderText =
1480 "#version 330\n"
1481 "#extension GL_ARB_separate_shader_objects : enable\n"
1482 "#extension GL_ARB_shading_language_420pack : enable\n"
1483 "layout (location = 0) out vec2 samplePos;\n"
1484 "out gl_PerVertex {\n"
1485 " vec4 gl_Position;\n"
1486 " float gl_ClipDistance[1];\n"
1487 "};\n"
1488 "void main() {\n"
1489 " vec2 vertices[3];"
1490 " vertices[0] = vec2(-0.5, -0.5);\n"
1491 " vertices[1] = vec2( 0.5, -0.5);\n"
1492 " vertices[2] = vec2( 0.5, 0.5);\n"
1493 " vec2 positions[3];"
1494 " positions[0] = vec2( 0.0, 0.0);\n"
1495 " positions[1] = vec2( 1.0, 0.0);\n"
1496 " positions[2] = vec2( 1.0, 1.0);\n"
1497 " float dists[3];\n"
1498 " dists[0] = 1.0;\n"
1499 " dists[1] = 1.0;\n"
1500 " dists[2] = -1.0;\n"
1501 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1502 " samplePos = positions[gl_VertexID % 3];\n"
1503 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1504 "}\n";
1505
1506 static const char *fragShaderText =
1507 "#version 140\n"
1508 "#extension GL_ARB_separate_shader_objects : enable\n"
1509 "#extension GL_ARB_shading_language_420pack : enable\n"
1510 "layout (location = 0) in vec2 samplePos;\n"
1511 "layout (binding = 0) uniform sampler2D surface;\n"
1512 "layout (location=0) out vec4 outColor;\n"
1513 "void main() {\n"
1514 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1515 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1516 " outColor = texColor;\n"
1517 "}\n";
1518
1519
1520 ASSERT_NO_FATAL_FAILURE(InitState());
1521 ASSERT_NO_FATAL_FAILURE(InitViewport());
1522
1523 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1524 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1525 XglSamplerObj sampler(m_device);
1526 XglTextureObj texture(m_device);
1527
1528 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1529 ps.BindShaderEntitySlotToSampler(0, &sampler);
1530
1531 XglPipelineObj pipelineobj(m_device);
1532 pipelineobj.AddShader(&vs);
1533 pipelineobj.AddShader(&ps);
1534
1535 XglDescriptorSetObj descriptorSet(m_device);
1536 descriptorSet.AttachImageView(&texture);
1537 descriptorSet.AttachSampler(&sampler);
1538
1539 m_memoryRefManager.AddMemoryRef(&texture);
1540
1541 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1542 QueueCommandBuffer(NULL, 0);
1543}
1544TEST_F(XglRenderTest, FSTriangle)
1545{
1546 // The expected result from this test is a red and green checkered triangle
1547 static const char *vertShaderText =
1548 "#version 140\n"
1549 "#extension GL_ARB_separate_shader_objects : enable\n"
1550 "#extension GL_ARB_shading_language_420pack : enable\n"
1551 "layout (location = 0) out vec2 samplePos;\n"
1552 "void main() {\n"
1553 " vec2 vertices[3];"
1554 " vertices[0] = vec2(-0.5, -0.5);\n"
1555 " vertices[1] = vec2( 0.5, -0.5);\n"
1556 " vertices[2] = vec2( 0.5, 0.5);\n"
1557 " vec2 positions[3];"
1558 " positions[0] = vec2( 0.0, 0.0);\n"
1559 " positions[1] = vec2( 1.0, 0.0);\n"
1560 " positions[2] = vec2( 1.0, 1.0);\n"
1561 " samplePos = positions[gl_VertexID % 3];\n"
1562 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1563 "}\n";
1564
1565 static const char *fragShaderText =
1566 "#version 140\n"
1567 "#extension GL_ARB_separate_shader_objects : enable\n"
1568 "#extension GL_ARB_shading_language_420pack : enable\n"
1569 "layout (location = 0) in vec2 samplePos;\n"
1570 "layout (binding = 0) uniform sampler2D surface;\n"
1571 "layout (location=0) out vec4 outColor;\n"
1572 "void main() {\n"
1573 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1574 " outColor = texColor;\n"
1575 "}\n";
1576
1577 ASSERT_NO_FATAL_FAILURE(InitState());
1578 ASSERT_NO_FATAL_FAILURE(InitViewport());
1579
1580 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1581 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1582 XglSamplerObj sampler(m_device);
1583 XglTextureObj texture(m_device);
1584
1585 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1586 ps.BindShaderEntitySlotToSampler(0, &sampler);
1587
1588 XglPipelineObj pipelineobj(m_device);
1589 pipelineobj.AddShader(&vs);
1590 pipelineobj.AddShader(&ps);
1591
1592 XglDescriptorSetObj descriptorSet(m_device);
1593 descriptorSet.AttachImageView(&texture);
1594 descriptorSet.AttachSampler(&sampler);
1595
1596 m_memoryRefManager.AddMemoryRef(&texture);
1597
1598 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1599 QueueCommandBuffer(NULL, 0);
1600}
1601TEST_F(XglRenderTest, SamplerBindingsTriangle)
1602{
1603 // This test sets bindings on the samplers
1604 // For now we are asserting that sampler and texture pairs
1605 // march in lock step, and are set via GLSL binding. This can
1606 // and will probably change.
1607 // The sampler bindings should match the sampler and texture slot
1608 // number set up by the application.
1609 // This test will result in a blue triangle
1610 static const char *vertShaderText =
1611 "#version 140\n"
1612 "#extension GL_ARB_separate_shader_objects : enable\n"
1613 "#extension GL_ARB_shading_language_420pack : enable\n"
1614 "layout (location = 0) out vec4 samplePos;\n"
1615 "void main() {\n"
1616 " vec2 vertices[3];"
1617 " vertices[0] = vec2(-0.5, -0.5);\n"
1618 " vertices[1] = vec2( 0.5, -0.5);\n"
1619 " vertices[2] = vec2( 0.5, 0.5);\n"
1620 " vec2 positions[3];"
1621 " positions[0] = vec2( 0.0, 0.0);\n"
1622 " positions[1] = vec2( 1.0, 0.0);\n"
1623 " positions[2] = vec2( 1.0, 1.0);\n"
1624 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
1625 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1626 "}\n";
1627
1628 static const char *fragShaderText =
1629 "#version 140\n"
1630 "#extension GL_ARB_separate_shader_objects : enable\n"
1631 "#extension GL_ARB_shading_language_420pack : enable\n"
1632 "layout (location = 0) in vec4 samplePos;\n"
1633 "layout (binding = 0) uniform sampler2D surface0;\n"
1634 "layout (binding = 1) uniform sampler2D surface1;\n"
1635 "layout (binding = 12) uniform sampler2D surface2;\n"
1636 "void main() {\n"
1637 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
1638 "}\n";
1639
1640 ASSERT_NO_FATAL_FAILURE(InitState());
1641 ASSERT_NO_FATAL_FAILURE(InitViewport());
1642
1643 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1644 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1645
1646 XglSamplerObj sampler1(m_device);
1647 XglSamplerObj sampler2(m_device);
1648 XglSamplerObj sampler3(m_device);
1649
1650 XglTextureObj texture1(m_device); // Red
1651 texture1.ChangeColors(0xffff0000,0xffff0000);
1652 XglTextureObj texture2(m_device); // Green
1653 texture2.ChangeColors(0xff00ff00,0xff00ff00);
1654 XglTextureObj texture3(m_device); // Blue
1655 texture3.ChangeColors(0xff0000ff,0xff0000ff);
1656
1657 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture1);
1658 ps.BindShaderEntitySlotToSampler(0, &sampler1);
1659
1660 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_RESOURCE, &texture2);
1661 ps.BindShaderEntitySlotToSampler(1, &sampler2);
1662
1663 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_RESOURCE, &texture3);
1664 ps.BindShaderEntitySlotToSampler(12, &sampler3);
1665
1666 XglPipelineObj pipelineobj(m_device);
1667 pipelineobj.AddShader(&vs);
1668 pipelineobj.AddShader(&ps);
1669
1670 XglDescriptorSetObj descriptorSet(m_device);
1671 descriptorSet.AttachImageView(&texture1);
1672 descriptorSet.AttachSampler(&sampler1);
1673 descriptorSet.AttachImageView(&texture2);
1674 descriptorSet.AttachSampler(&sampler2);
1675 descriptorSet.AttachImageView(&texture3);
1676 descriptorSet.AttachSampler(&sampler3);
1677
1678 m_memoryRefManager.AddMemoryRef(&texture1);
1679 m_memoryRefManager.AddMemoryRef(&texture2);
1680 m_memoryRefManager.AddMemoryRef(&texture3);
1681
1682 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1683 QueueCommandBuffer(NULL, 0);
1684
1685}
1686
1687TEST_F(XglRenderTest, TriangleVSUniformBlock)
1688{
1689 // The expected result from this test is a blue triangle
1690
1691 static const char *vertShaderText =
1692 "#version 140\n"
1693 "#extension GL_ARB_separate_shader_objects : enable\n"
1694 "#extension GL_ARB_shading_language_420pack : enable\n"
1695 "layout (location = 0) out vec4 outColor;\n"
1696 "layout (std140, binding = 0) uniform bufferVals {\n"
1697 " vec4 red;\n"
1698 " vec4 green;\n"
1699 " vec4 blue;\n"
1700 " vec4 white;\n"
1701 "} myBufferVals;\n"
1702 "void main() {\n"
1703 " vec2 vertices[3];"
1704 " vertices[0] = vec2(-0.5, -0.5);\n"
1705 " vertices[1] = vec2( 0.5, -0.5);\n"
1706 " vertices[2] = vec2( 0.5, 0.5);\n"
1707 " outColor = myBufferVals.blue;\n"
1708 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1709 "}\n";
1710
1711 static const char *fragShaderText =
1712 "#version 140\n"
1713 "#extension GL_ARB_separate_shader_objects : enable\n"
1714 "#extension GL_ARB_shading_language_420pack : enable\n"
1715 "layout (location = 0) in vec4 inColor;\n"
1716 "void main() {\n"
1717 " gl_FragColor = inColor;\n"
1718 "}\n";
1719
1720 ASSERT_NO_FATAL_FAILURE(InitState());
1721 ASSERT_NO_FATAL_FAILURE(InitViewport());
1722
1723 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1724 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1725
1726 // Let's populate our buffer with the following:
1727 // vec4 red;
1728 // vec4 green;
1729 // vec4 blue;
1730 // vec4 white;
1731 const int valCount = 4 * 4;
1732 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
1733 0.0, 1.0, 0.0, 1.0,
1734 0.0, 0.0, 1.0, 1.0,
1735 1.0, 1.0, 1.0, 1.0 };
1736
1737 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
1738 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
1739
1740 XglPipelineObj pipelineobj(m_device);
1741 pipelineobj.AddShader(&vs);
1742 pipelineobj.AddShader(&ps);
1743
1744 XglDescriptorSetObj descriptorSet(m_device);
1745 descriptorSet.AttachMemoryView(&colorBuffer);
1746
1747 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1748 QueueCommandBuffer(NULL, 0);
1749
1750}
1751
1752TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
1753{
1754 // This test allows the shader to select which buffer it is
1755 // pulling from using layout binding qualifier.
1756 // There are corresponding changes in the compiler stack that
1757 // will select the buffer using binding directly.
1758 // The binding number should match the slot number set up by
1759 // the application.
1760 // The expected result from this test is a purple triangle
1761
1762 static const char *vertShaderText =
1763 "#version 140\n"
1764 "#extension GL_ARB_separate_shader_objects : enable\n"
1765 "#extension GL_ARB_shading_language_420pack : enable\n"
1766 "void main() {\n"
1767 " vec2 vertices[3];"
1768 " vertices[0] = vec2(-0.5, -0.5);\n"
1769 " vertices[1] = vec2( 0.5, -0.5);\n"
1770 " vertices[2] = vec2( 0.5, 0.5);\n"
1771 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1772 "}\n";
1773
1774 static const char *fragShaderText =
1775 "#version 140\n"
1776 "#extension GL_ARB_separate_shader_objects : enable\n"
1777 "#extension GL_ARB_shading_language_420pack : enable\n"
1778 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
1779 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
1780 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
1781 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
1782 "void main() {\n"
1783 " gl_FragColor = myBlueVal.color;\n"
1784 " gl_FragColor += myRedVal.color;\n"
1785 "}\n";
1786
1787 ASSERT_NO_FATAL_FAILURE(InitState());
1788 ASSERT_NO_FATAL_FAILURE(InitViewport());
1789
1790 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1791 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1792
1793 // We're going to create a number of uniform buffers, and then allow
1794 // the shader to select which it wants to read from with a binding
1795
1796 // Let's populate the buffers with a single color each:
1797 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
1798 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
1799 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
1800 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
1801
1802 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
1803 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
1804 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
1805 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
1806
1807 const int redCount = sizeof(redVals) / sizeof(float);
1808 const int greenCount = sizeof(greenVals) / sizeof(float);
1809 const int blueCount = sizeof(blueVals) / sizeof(float);
1810 const int whiteCount = sizeof(whiteVals) / sizeof(float);
1811
1812 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
1813 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
1814
1815 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
1816 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
1817
1818 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
1819 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
1820
1821 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
1822 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
1823
1824 XglPipelineObj pipelineobj(m_device);
1825 pipelineobj.AddShader(&vs);
1826 pipelineobj.AddShader(&ps);
1827
1828 XglDescriptorSetObj descriptorSet(m_device);
1829 descriptorSet.AttachMemoryView(&redBuffer);
1830 descriptorSet.AttachMemoryView(&greenBuffer);
1831 descriptorSet.AttachMemoryView(&blueBuffer);
1832 descriptorSet.AttachMemoryView(&whiteBuffer);
1833
1834 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1835 QueueCommandBuffer(NULL, 0);
1836
1837}
1838
1839TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
1840{
1841 // This test is the same as TriangleFSUniformBlockBinding, but
1842 // it does not provide an instance name.
1843 // The expected result from this test is a purple triangle
1844
1845 static const char *vertShaderText =
1846 "#version 140\n"
1847 "#extension GL_ARB_separate_shader_objects : enable\n"
1848 "#extension GL_ARB_shading_language_420pack : enable\n"
1849 "void main() {\n"
1850 " vec2 vertices[3];"
1851 " vertices[0] = vec2(-0.5, -0.5);\n"
1852 " vertices[1] = vec2( 0.5, -0.5);\n"
1853 " vertices[2] = vec2( 0.5, 0.5);\n"
1854 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1855 "}\n";
1856
1857 static const char *fragShaderText =
1858 "#version 430\n"
1859 "#extension GL_ARB_separate_shader_objects : enable\n"
1860 "#extension GL_ARB_shading_language_420pack : enable\n"
1861 "layout (location = 0) uniform vec4 foo;\n"
1862 "layout (location = 1) uniform vec4 bar;\n"
1863 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
1864 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
1865 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
1866 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
1867 "void main() {\n"
1868 " gl_FragColor = blue;\n"
1869 " gl_FragColor += red;\n"
1870 "}\n";
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
1877 // We're going to create a number of uniform buffers, and then allow
1878 // the shader to select which it wants to read from with a binding
1879
1880 // Let's populate the buffers with a single color each:
1881 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
1882 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
1883 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
1884 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
1885
1886 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
1887 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
1888 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
1889 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
1890
1891 const int redCount = sizeof(redVals) / sizeof(float);
1892 const int greenCount = sizeof(greenVals) / sizeof(float);
1893 const int blueCount = sizeof(blueVals) / sizeof(float);
1894 const int whiteCount = sizeof(whiteVals) / sizeof(float);
1895
1896 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
1897 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
1898
1899 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
1900 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
1901
1902 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
1903 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
1904
1905 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
1906 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
1907
1908 XglPipelineObj pipelineobj(m_device);
1909 pipelineobj.AddShader(&vs);
1910 pipelineobj.AddShader(&ps);
1911
1912 XglDescriptorSetObj descriptorSet(m_device);
1913 descriptorSet.AttachMemoryView(&redBuffer);
1914 descriptorSet.AttachMemoryView(&greenBuffer);
1915 descriptorSet.AttachMemoryView(&blueBuffer);
1916 descriptorSet.AttachMemoryView(&whiteBuffer);
1917
1918 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1919 QueueCommandBuffer(NULL, 0);
1920
1921}
1922
1923TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
1924{
1925 static const char *vertShaderText =
1926 "#version 140\n"
1927 "#extension GL_ARB_separate_shader_objects : enable\n"
1928 "#extension GL_ARB_shading_language_420pack : enable\n"
1929 "layout (std140, binding=0) uniform bufferVals {\n"
1930 " mat4 mvp;\n"
1931 "} myBufferVals;\n"
1932 "layout (location=0) in vec4 pos;\n"
1933 "layout (location=0) out vec2 UV;\n"
1934 "void main() {\n"
1935 " vec2 positions[3];"
1936 " positions[0] = vec2( 0.0, 0.0);\n"
1937 " positions[1] = vec2( 0.25, 0.1);\n"
1938 " positions[2] = vec2( 0.1, 0.25);\n"
1939 " UV = positions[gl_VertexID % 3];\n"
1940 " gl_Position = myBufferVals.mvp * pos;\n"
1941 "}\n";
1942
1943 static const char *fragShaderText =
1944 "#version 140\n"
1945 "#extension GL_ARB_separate_shader_objects : enable\n"
1946 "#extension GL_ARB_shading_language_420pack : enable\n"
1947 "layout (binding=0) uniform sampler2D surface;\n"
1948 "layout (location=0) out vec4 outColor;\n"
1949 "layout (location=0) in vec2 UV;\n"
1950 "void main() {\n"
1951 " outColor= textureLod(surface, UV, 0.0);\n"
1952 "}\n";
1953 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
1954
1955 glm::mat4 View = glm::lookAt(
1956 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1957 glm::vec3(0,0,0), // and looks at the origin
1958 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1959 );
1960
1961 glm::mat4 Model = glm::mat4(1.0f);
1962
1963 glm::mat4 MVP = Projection * View * Model;
1964
1965
1966 ASSERT_NO_FATAL_FAILURE(InitState());
1967 ASSERT_NO_FATAL_FAILURE(InitViewport());
1968 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1969
1970 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1971 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1972 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1973
1974
1975 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1976
1977 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1978 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1979 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1980 XglSamplerObj sampler(m_device);
1981 XglTextureObj texture(m_device);
1982
1983 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
1984 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
1985 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1986 ps.BindShaderEntitySlotToSampler(0, &sampler);
1987
1988 XglPipelineObj pipelineobj(m_device);
1989 pipelineobj.AddShader(&vs);
1990 pipelineobj.AddShader(&ps);
1991
1992 XglDescriptorSetObj descriptorSet(m_device);
1993
1994 descriptorSet.AttachMemoryView(&mvpBuffer);
1995 descriptorSet.AttachImageView(&texture);
1996 descriptorSet.AttachSampler(&sampler);
1997
1998 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1999 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2000 m_memoryRefManager.AddMemoryRef(&texture);
2001
2002 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2003 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2004 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2005 };
2006
2007 // this is the current description of g_vbData
2008 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2009 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2010 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2011 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2012 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2013 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2014 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2015 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2016 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2017
2018 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2019 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2020 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2021
2022 ClearDepthStencil(1.0f);
2023 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
2024
2025 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2026
2027}
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002028int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002029 int result;
2030
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002031 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002032 XglTestFramework::InitArgs(&argc, argv);
2033
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002034 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
2035
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002036 result = RUN_ALL_TESTS();
2037
2038 XglTestFramework::Finish();
2039 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002040}