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