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