blob: 5bbcc1eae0f38b9a02e17a1313af2afe2099d0fd [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 =
Cody Northrop68a10f62014-12-05 15:44:14 -0700887 "#version 130\n"
Tony Barbourf43b6982014-11-25 13:18:32 -0700888 "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 =
Cody Northrop68a10f62014-12-05 15:44:14 -0700941 "#version 140\n"
942 "#extension GL_ARB_separate_shader_objects : enable\n"
943 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -0700944 "in vec4 color;\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700945 "layout (location = 0) out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600946 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700947 " outColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600948 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600949
Tony Barbourf43b6982014-11-25 13:18:32 -0700950
951
952 ASSERT_NO_FATAL_FAILURE(InitState());
953 ASSERT_NO_FATAL_FAILURE(InitViewport());
954
955 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
956 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
957
958 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
959 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
960
961 XglPipelineObj pipelineobj(m_device);
962 pipelineobj.AddShader(&vs);
963 pipelineobj.AddShader(&ps);
964
965 XglDescriptorSetObj descriptorSet(m_device);
966 descriptorSet.AttachMemoryView(&meshBuffer);
967
968 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
969 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
970 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
971 };
972
973 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
974 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
975 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
976 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
977 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
978 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
979 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
980 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
981 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
982
983 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
984 pipelineobj.AddVertexInputBindings(&vi_binding,1);
985 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
986
Tony Barboura3953b82014-12-03 15:46:29 -0700987 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -0700988 QueueCommandBuffer(NULL, 0);
989
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600990}
991
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700992TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
993{
994 static const char *vertShaderText =
995 "#version 140\n"
996 "#extension GL_ARB_separate_shader_objects : enable\n"
997 "#extension GL_ARB_shading_language_420pack : enable\n"
998 "layout(location = 0) in vec4 pos;\n"
999 "layout(location = 1) in vec4 inColor;\n"
1000 "layout(location = 0) out vec4 outColor;\n"
1001 "void main() {\n"
1002 " outColor = inColor;\n"
1003 " gl_Position = pos;\n"
1004 "}\n";
1005
1006
1007 static const char *fragShaderText =
1008 "#version 140\n"
1009 "#extension GL_ARB_separate_shader_objects : enable\n"
1010 "#extension GL_ARB_shading_language_420pack : enable\n"
1011 "layout(location = 0) in vec4 color;\n"
1012 "void main() {\n"
1013 " gl_FragColor = color;\n"
1014 "}\n";
1015
1016 const Vertex g_vbData[] =
1017 {
1018 // first tri
1019 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
1020 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1021 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1022
1023 // second tri
1024 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1025 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1026 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
1027 };
1028
1029 const uint16_t g_idxData[6] = {
1030 0, 1, 2,
1031 3, 4, 5,
1032 };
1033
1034 ASSERT_NO_FATAL_FAILURE(InitState());
1035 ASSERT_NO_FATAL_FAILURE(InitViewport());
1036
1037 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1038 meshBuffer.SetMemoryState(m_cmdBuffer, XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1039
1040 XglIndexBufferObj indexBuffer(m_device);
1041 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
1042 indexBuffer.SetMemoryState(m_cmdBuffer, XGL_MEMORY_STATE_INDEX_DATA);
1043
1044 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1045 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1046
1047 XglPipelineObj pipelineobj(m_device);
1048 pipelineobj.AddShader(&vs);
1049 pipelineobj.AddShader(&ps);
1050
1051 XglDescriptorSetObj descriptorSet(m_device);
1052
1053 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1054 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1055 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1056 };
1057
1058 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1059 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1060 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1061 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1062 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1063 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1064 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1065 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1066 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1067
1068 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1069 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1070
1071 XGL_RESULT err = XGL_SUCCESS;
1072
1073 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1074 // Build command buffer
1075 // TODO: SetMemoryState above also uses this command buffer, do we need to make sure
1076 // that job is complete? Or maybe we shouldn't queue those actions?
1077 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1078 ASSERT_XGL_SUCCESS(err);
1079
1080 GenerateClearAndPrepareBufferCmds();
1081 GenerateBindRenderTargetCmd();
1082#ifdef DUMP_STATE_DOT
1083 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
1084 pDSDumpDot((char*)"triTest.dot");
1085#endif
1086 GenerateBindStateAndPipelineCmds();
1087
Tony Barbour9d3c3172014-12-04 17:27:08 -07001088 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,&descriptorSet);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001089 descriptorSet.BindCommandBuffer(m_cmdBuffer);
1090
1091 meshBuffer.Bind(m_cmdBuffer, 0, 0);
1092 indexBuffer.Bind(m_cmdBuffer, /* offset */ 0);
1093
1094 // render two triangles
1095 xglCmdDrawIndexed(m_cmdBuffer, 0, 6, 0, 0, 1);
1096
1097 // finalize recording of the command buffer
1098 err = xglEndCommandBuffer( m_cmdBuffer );
1099 ASSERT_XGL_SUCCESS( err );
1100
1101 QueueCommandBuffer(NULL, 0);
1102
1103}
1104
GregF6bef1212014-12-02 15:41:44 -07001105TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1106{
1107 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001108
GregF6bef1212014-12-02 15:41:44 -07001109 static const char *vertShaderText =
1110 "#version 140\n"
1111 "#extension GL_ARB_separate_shader_objects : enable\n"
1112 "#extension GL_ARB_shading_language_420pack : enable\n"
1113 "layout (location = 0) in vec4 pos;\n"
1114 "layout (location = 0) out vec4 outColor;\n"
1115 "layout (location = 1) out vec4 outColor2;\n"
1116 "void main() {\n"
1117 " gl_Position = pos;\n"
1118 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1119 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1120 "}\n";
1121
1122 static const char *fragShaderText =
1123 //"#version 140\n"
1124 "#version 330\n"
1125 "#extension GL_ARB_separate_shader_objects : enable\n"
1126 "#extension GL_ARB_shading_language_420pack : enable\n"
1127 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1128 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1129 "layout (location = 0) in vec4 color;\n"
1130 "layout (location = 1) in vec4 color2;\n"
1131 "void main() {\n"
1132 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1133 " float dist_squared = dot(pos, pos);\n"
1134 " gl_FragColor = (dist_squared < 400.0)\n"
1135 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1136 " : color2;\n"
1137 "}\n";
1138
1139 ASSERT_NO_FATAL_FAILURE(InitState());
1140 ASSERT_NO_FATAL_FAILURE(InitViewport());
1141
1142 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1143 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1144
1145 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1146 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1147
1148 XglPipelineObj pipelineobj(m_device);
1149 pipelineobj.AddShader(&vs);
1150 pipelineobj.AddShader(&ps);
1151
1152 XglDescriptorSetObj descriptorSet(m_device);
1153 descriptorSet.AttachMemoryView(&meshBuffer);
1154
1155 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1156 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1157 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1158 };
1159
1160 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1161 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1162 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1163 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1164 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1165 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1166 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1167 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1168 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1169
1170 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1171 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1172 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1173
Tony Barbour09da2212014-12-03 16:13:23 -07001174 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001175 QueueCommandBuffer(NULL, 0);
1176
1177}
1178
1179TEST_F(XglRenderTest, RedCirclesonBlue)
1180{
1181 // This tests that we correctly handle unread fragment inputs
1182
1183 static const char *vertShaderText =
1184 "#version 140\n"
1185 "#extension GL_ARB_separate_shader_objects : enable\n"
1186 "#extension GL_ARB_shading_language_420pack : enable\n"
1187 "layout (location = 0) in vec4 pos;\n"
1188 "layout (location = 0) out vec4 outColor;\n"
1189 "layout (location = 1) out vec4 outColor2;\n"
1190 "void main() {\n"
1191 " gl_Position = pos;\n"
1192 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1193 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1194 "}\n";
1195
1196 static const char *fragShaderText =
1197 //"#version 140\n"
1198 "#version 330\n"
1199 "#extension GL_ARB_separate_shader_objects : enable\n"
1200 "#extension GL_ARB_shading_language_420pack : enable\n"
1201 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1202 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1203 "layout (location = 0) in vec4 color;\n"
1204 "layout (location = 1) in vec4 color2;\n"
1205 "void main() {\n"
1206 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1207 " float dist_squared = dot(pos, pos);\n"
1208 " gl_FragColor = (dist_squared < 400.0)\n"
1209 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1210 " : color2;\n"
1211 "}\n";
1212
1213 ASSERT_NO_FATAL_FAILURE(InitState());
1214 ASSERT_NO_FATAL_FAILURE(InitViewport());
1215
1216 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1217 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1218
1219 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1220 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1221
1222 XglPipelineObj pipelineobj(m_device);
1223 pipelineobj.AddShader(&vs);
1224 pipelineobj.AddShader(&ps);
1225
1226 XglDescriptorSetObj descriptorSet(m_device);
1227 descriptorSet.AttachMemoryView(&meshBuffer);
1228
1229 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1230 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1231 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1232 };
1233
1234 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1235 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1236 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1237 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1238 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1239 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1240 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1241 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1242 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1243
1244 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1245 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1246 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1247
Tony Barbour09da2212014-12-03 16:13:23 -07001248 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001249 QueueCommandBuffer(NULL, 0);
1250
1251}
1252
1253TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1254{
1255 // This tests reading gl_ClipDistance from FS
1256
1257 static const char *vertShaderText =
1258 "#version 330\n"
1259 "#extension GL_ARB_separate_shader_objects : enable\n"
1260 "#extension GL_ARB_shading_language_420pack : enable\n"
1261 "out gl_PerVertex {\n"
1262 " vec4 gl_Position;\n"
1263 " float gl_ClipDistance[1];\n"
1264 "};\n"
1265 "layout (location = 0) in vec4 pos;\n"
1266 "layout (location = 0) out vec4 outColor;\n"
1267 "layout (location = 1) out vec4 outColor2;\n"
1268 "void main() {\n"
1269 " gl_Position = pos;\n"
1270 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1271 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1272 " float dists[3];\n"
1273 " dists[0] = 0.0;\n"
1274 " dists[1] = 1.0;\n"
1275 " dists[2] = 1.0;\n"
1276 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1277 "}\n";
1278
1279
1280 static const char *fragShaderText =
1281 //"#version 140\n"
1282 "#version 330\n"
1283 "#extension GL_ARB_separate_shader_objects : enable\n"
1284 "#extension GL_ARB_shading_language_420pack : enable\n"
1285 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1286 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1287 "layout (location = 0) in vec4 color;\n"
1288 "layout (location = 1) in vec4 color2;\n"
1289 "void main() {\n"
1290 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1291 " float dist_squared = dot(pos, pos);\n"
1292 " gl_FragColor = (dist_squared < 400.0)\n"
1293 " ? color * gl_ClipDistance[0]\n"
1294 " : color2;\n"
1295 "}\n";
1296
1297 ASSERT_NO_FATAL_FAILURE(InitState());
1298 ASSERT_NO_FATAL_FAILURE(InitViewport());
1299
1300 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1301 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1302
1303 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1304 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1305
1306 XglPipelineObj pipelineobj(m_device);
1307 pipelineobj.AddShader(&vs);
1308 pipelineobj.AddShader(&ps);
1309
1310 XglDescriptorSetObj descriptorSet(m_device);
1311 descriptorSet.AttachMemoryView(&meshBuffer);
1312
1313 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1314 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1315 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1316 };
1317
1318 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1319 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1320 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1321 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1322 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1323 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1324 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1325 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1326 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1327
1328 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1329 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1330 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1331
Tony Barboura3953b82014-12-03 15:46:29 -07001332 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001333 QueueCommandBuffer(NULL, 0);
1334
1335}
Tony Barbourf43b6982014-11-25 13:18:32 -07001336
GregF7a23c792014-12-02 17:19:34 -07001337TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1338{
1339 static const char *vertShaderText =
1340 "#version 140\n"
1341 "#extension GL_ARB_separate_shader_objects : enable\n"
1342 "#extension GL_ARB_shading_language_420pack : enable\n"
1343 "layout (location = 0) in vec4 pos;\n"
1344 "layout (location = 0) out vec4 outColor;\n"
1345 "layout (location = 1) out vec4 outColor2;\n"
1346 "void main() {\n"
1347 " gl_Position = pos;\n"
1348 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1349 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1350 "}\n";
1351
1352
1353 static const char *fragShaderText =
1354 //"#version 140\n"
1355 "#version 330\n"
1356 "#extension GL_ARB_separate_shader_objects : enable\n"
1357 "#extension GL_ARB_shading_language_420pack : enable\n"
1358 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1359 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1360 "layout (location = 0) in vec4 color;\n"
1361 "layout (location = 1) in vec4 color2;\n"
1362 "void main() {\n"
1363 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1364 " float dist_squared = dot(pos, pos);\n"
1365 " if (dist_squared < 100.0)\n"
1366 " discard;\n"
1367 " gl_FragColor = (dist_squared < 400.0)\n"
1368 " ? color\n"
1369 " : color2;\n"
1370 "}\n";
1371
1372 ASSERT_NO_FATAL_FAILURE(InitState());
1373 ASSERT_NO_FATAL_FAILURE(InitViewport());
1374
1375 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1376 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1377
1378 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1379 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1380
1381 XglPipelineObj pipelineobj(m_device);
1382 pipelineobj.AddShader(&vs);
1383 pipelineobj.AddShader(&ps);
1384
1385 XglDescriptorSetObj descriptorSet(m_device);
1386 descriptorSet.AttachMemoryView(&meshBuffer);
1387
1388 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1389 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1390 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1391 };
1392
1393 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1394 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1395 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1396 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1397 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1398 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1399 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1400 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1401 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1402
1403 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1404 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1405 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1406
Tony Barbour09da2212014-12-03 16:13:23 -07001407 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF7a23c792014-12-02 17:19:34 -07001408 QueueCommandBuffer(NULL, 0);
1409
1410}
1411
1412
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001413TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001414{
1415 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001416 "#version 140\n"
1417 "#extension GL_ARB_separate_shader_objects : enable\n"
1418 "#extension GL_ARB_shading_language_420pack : enable\n"
1419 "\n"
1420 "layout(binding = 0) uniform buf {\n"
1421 " mat4 MVP;\n"
1422 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001423 "void main() {\n"
1424 " vec2 vertices[3];"
1425 " vertices[0] = vec2(-0.5, -0.5);\n"
1426 " vertices[1] = vec2( 0.5, -0.5);\n"
1427 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001428 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001429 "}\n";
1430
1431 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001432 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001433 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001434 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001435 "}\n";
1436
Tony Barbourf43b6982014-11-25 13:18:32 -07001437 ASSERT_NO_FATAL_FAILURE(InitState());
1438 ASSERT_NO_FATAL_FAILURE(InitViewport());
1439
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001440 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001441 glm::mat4 Projection = glm::mat4(1.0f);
1442 glm::mat4 View = glm::mat4(1.0f);
1443 glm::mat4 Model = glm::mat4(1.0f);
1444 glm::mat4 MVP = Projection * View * Model;
1445 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1446
Tony Barbourf43b6982014-11-25 13:18:32 -07001447 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1448 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1449 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001450
Tony Barbourf43b6982014-11-25 13:18:32 -07001451 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1452
1453 XglPipelineObj pipelineobj(m_device);
1454 pipelineobj.AddShader(&vs);
1455 pipelineobj.AddShader(&ps);
1456
1457 // Create descriptor set and attach the constant buffer to it
1458 XglDescriptorSetObj descriptorSet(m_device);
1459 descriptorSet.AttachMemoryView(&MVPBuffer);
1460
1461 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1462
Tony Barboura3953b82014-12-03 15:46:29 -07001463 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001464 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1465
Tony Barbour09da2212014-12-03 16:13:23 -07001466 RotateTriangleVSUniform(Projection, View, Model, &MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001467}
1468
Tony Barbourf43b6982014-11-25 13:18:32 -07001469TEST_F(XglRenderTest, MixTriangle)
1470{
1471 // This tests location applied to varyings. Notice that we have switched foo
1472 // and bar in the FS. The triangle should be blended with red, green and blue
1473 // corners.
1474 static const char *vertShaderText =
1475 "#version 140\n"
1476 "#extension GL_ARB_separate_shader_objects : enable\n"
1477 "#extension GL_ARB_shading_language_420pack : enable\n"
1478 "layout (location=0) out vec4 bar;\n"
1479 "layout (location=1) out vec4 foo;\n"
1480 "layout (location=2) out float scale;\n"
1481 "vec2 vertices[3];\n"
1482 "void main() {\n"
1483 " vertices[0] = vec2(-1.0, -1.0);\n"
1484 " vertices[1] = vec2( 1.0, -1.0);\n"
1485 " vertices[2] = vec2( 0.0, 1.0);\n"
1486 "vec4 colors[3];\n"
1487 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1488 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1489 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1490 " foo = colors[gl_VertexID % 3];\n"
1491 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1492 " scale = 1.0;\n"
1493 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1494 "}\n";
1495
1496 static const char *fragShaderText =
1497 "#version 140\n"
1498 "#extension GL_ARB_separate_shader_objects : enable\n"
1499 "#extension GL_ARB_shading_language_420pack : enable\n"
1500 "layout (location = 1) in vec4 bar;\n"
1501 "layout (location = 0) in vec4 foo;\n"
1502 "layout (location = 2) in float scale;\n"
1503 "void main() {\n"
1504 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1505 "}\n";
1506
1507 ASSERT_NO_FATAL_FAILURE(InitState());
1508 ASSERT_NO_FATAL_FAILURE(InitViewport());
1509
1510 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1511 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1512
1513 XglPipelineObj pipelineobj(m_device);
1514 pipelineobj.AddShader(&vs);
1515 pipelineobj.AddShader(&ps);
1516
1517 XglDescriptorSetObj descriptorSet(m_device);
1518
Tony Barboura3953b82014-12-03 15:46:29 -07001519 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001520 QueueCommandBuffer(NULL, 0);
1521}
1522
1523TEST_F(XglRenderTest, TriVertFetchAndVertID)
1524{
1525 // This tests that attributes work in the presence of gl_VertexID
1526
1527 static const char *vertShaderText =
1528 "#version 140\n"
1529 "#extension GL_ARB_separate_shader_objects : enable\n"
1530 "#extension GL_ARB_shading_language_420pack : enable\n"
1531 //XYZ1( -1, -1, -1 )
1532 "layout (location = 0) in vec4 pos;\n"
1533 //XYZ1( 0.f, 0.f, 0.f )
1534 "layout (location = 1) in vec4 inColor;\n"
1535 "layout (location = 0) out vec4 outColor;\n"
1536 "void main() {\n"
1537 " outColor = inColor;\n"
1538 " vec4 vertices[3];"
1539 " vertices[gl_VertexID % 3] = pos;\n"
1540 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1541 "}\n";
1542
1543
1544 static const char *fragShaderText =
1545 "#version 140\n"
1546 "#extension GL_ARB_separate_shader_objects : enable\n"
1547 "#extension GL_ARB_shading_language_420pack : enable\n"
1548 "layout (location = 0) in vec4 color;\n"
1549 "void main() {\n"
1550 " gl_FragColor = color;\n"
1551 "}\n";
1552
1553 ASSERT_NO_FATAL_FAILURE(InitState());
1554 ASSERT_NO_FATAL_FAILURE(InitViewport());
1555
1556 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1557 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1558
1559 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1560 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1561
1562 XglPipelineObj pipelineobj(m_device);
1563 pipelineobj.AddShader(&vs);
1564 pipelineobj.AddShader(&ps);
1565
1566 XglDescriptorSetObj descriptorSet(m_device);
1567 descriptorSet.AttachMemoryView(&meshBuffer);
1568
1569 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1570 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1571 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1572 };
1573
1574 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1575 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1576 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1577 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1578 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1579 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1580 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1581 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1582 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1583
1584 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1585 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1586 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1587
Tony Barboura3953b82014-12-03 15:46:29 -07001588 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001589 QueueCommandBuffer(NULL, 0);
1590}
1591
1592TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1593{
1594 // This tests that attributes work in the presence of gl_VertexID
1595 // and a dead attribute in position 0. Draws a triangle with yellow,
1596 // red and green corners, starting at top and going clockwise.
1597
1598 static const char *vertShaderText =
1599 "#version 140\n"
1600 "#extension GL_ARB_separate_shader_objects : enable\n"
1601 "#extension GL_ARB_shading_language_420pack : enable\n"
1602 //XYZ1( -1, -1, -1 )
1603 "layout (location = 0) in vec4 pos;\n"
1604 //XYZ1( 0.f, 0.f, 0.f )
1605 "layout (location = 1) in vec4 inColor;\n"
1606 "layout (location = 0) out vec4 outColor;\n"
1607 "void main() {\n"
1608 " outColor = inColor;\n"
1609 " vec2 vertices[3];"
1610 " vertices[0] = vec2(-1.0, -1.0);\n"
1611 " vertices[1] = vec2( 1.0, -1.0);\n"
1612 " vertices[2] = vec2( 0.0, 1.0);\n"
1613 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1614 "}\n";
1615
1616
1617 static const char *fragShaderText =
1618 "#version 140\n"
1619 "#extension GL_ARB_separate_shader_objects : enable\n"
1620 "#extension GL_ARB_shading_language_420pack : enable\n"
1621 "layout (location = 0) in vec4 color;\n"
1622 "void main() {\n"
1623 " gl_FragColor = color;\n"
1624 "}\n";
1625
1626 ASSERT_NO_FATAL_FAILURE(InitState());
1627 ASSERT_NO_FATAL_FAILURE(InitViewport());
1628
1629 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1630 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1631
1632 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1633 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1634
1635 XglPipelineObj pipelineobj(m_device);
1636 pipelineobj.AddShader(&vs);
1637 pipelineobj.AddShader(&ps);
1638
1639 XglDescriptorSetObj descriptorSet(m_device);
1640 descriptorSet.AttachMemoryView(&meshBuffer);
1641
1642 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1643 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1644 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1645 };
1646
1647 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1648 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1649 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1650 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1651 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1652 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1653 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1654 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1655 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1656
1657 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1658 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1659 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1660
Tony Barbour09da2212014-12-03 16:13:23 -07001661 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001662 QueueCommandBuffer(NULL, 0);
1663
1664}
1665
1666TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001667{
1668 static const char *vertShaderText =
1669 "#version 140\n"
1670 "layout (std140) uniform bufferVals {\n"
1671 " mat4 mvp;\n"
1672 "} myBufferVals;\n"
1673 "in vec4 pos;\n"
1674 "in vec4 inColor;\n"
1675 "out vec4 outColor;\n"
1676 "void main() {\n"
1677 " outColor = inColor;\n"
1678 " gl_Position = myBufferVals.mvp * pos;\n"
1679 "}\n";
1680
1681 static const char *fragShaderText =
1682 "#version 130\n"
1683 "in vec4 color;\n"
1684 "void main() {\n"
1685 " gl_FragColor = color;\n"
1686 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001687 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001688
Tony Barbourf43b6982014-11-25 13:18:32 -07001689 glm::mat4 View = glm::lookAt(
1690 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1691 glm::vec3(0,0,0), // and looks at the origin
1692 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1693 );
1694
1695 glm::mat4 Model = glm::mat4(1.0f);
1696
1697 glm::mat4 MVP = Projection * View * Model;
1698
1699 ASSERT_NO_FATAL_FAILURE(InitState());
1700 ASSERT_NO_FATAL_FAILURE(InitViewport());
1701 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1702
1703 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1704 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1705
1706 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1707
1708 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1709 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1710 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1711
1712 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1713
1714 XglPipelineObj pipelineobj(m_device);
1715 pipelineobj.AddShader(&vs);
1716 pipelineobj.AddShader(&ps);
1717
1718 XglDescriptorSetObj descriptorSet(m_device);
1719 descriptorSet.AttachMemoryView(&MVPBuffer);
1720
1721 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1722 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1723
1724 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1725 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1726 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1727 };
1728
1729 // this is the current description of g_vbData
1730 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1731 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1732 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1733 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1734 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1735 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1736 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1737 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1738 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1739
1740 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1741 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1742 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1743
Tony Barboura3953b82014-12-03 15:46:29 -07001744 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07001745
1746 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1747
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001748}
1749
Tony Barbourf43b6982014-11-25 13:18:32 -07001750TEST_F(XglRenderTest, VSTexture)
1751{
1752 // The expected result from this test is a green and red triangle;
1753 // one red vertex on the left, two green vertices on the right.
1754 static const char *vertShaderText =
1755 "#version 130\n"
1756 "out vec4 texColor;\n"
1757 "uniform sampler2D surface;\n"
1758 "void main() {\n"
1759 " vec2 vertices[3];"
1760 " vertices[0] = vec2(-0.5, -0.5);\n"
1761 " vertices[1] = vec2( 0.5, -0.5);\n"
1762 " vertices[2] = vec2( 0.5, 0.5);\n"
1763 " vec2 positions[3];"
1764 " positions[0] = vec2( 0.0, 0.0);\n"
1765 " positions[1] = vec2( 0.25, 0.1);\n"
1766 " positions[2] = vec2( 0.1, 0.25);\n"
1767 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1768 " texColor = textureLod(surface, samplePos, 0.0);\n"
1769 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1770 "}\n";
1771
1772 static const char *fragShaderText =
1773 "#version 130\n"
1774 "in vec4 texColor;\n"
1775 "void main() {\n"
1776 " gl_FragColor = texColor;\n"
1777 "}\n";
1778
1779 ASSERT_NO_FATAL_FAILURE(InitState());
1780 ASSERT_NO_FATAL_FAILURE(InitViewport());
1781
1782 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1783 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1784 XglSamplerObj sampler(m_device);
1785 XglTextureObj texture(m_device);
1786
1787 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1788 vs.BindShaderEntitySlotToSampler(0, &sampler);
1789
1790 XglPipelineObj pipelineobj(m_device);
1791 pipelineobj.AddShader(&vs);
1792 pipelineobj.AddShader(&ps);
1793
1794 XglDescriptorSetObj descriptorSet(m_device);
1795 descriptorSet.AttachImageView(&texture);
1796 descriptorSet.AttachSampler(&sampler);
1797
1798 m_memoryRefManager.AddMemoryRef(&texture);
1799
Tony Barboura3953b82014-12-03 15:46:29 -07001800 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001801 QueueCommandBuffer(NULL, 0);
1802
1803}
1804TEST_F(XglRenderTest, TexturedTriangle)
1805{
1806 // The expected result from this test is a red and green checkered triangle
1807 static const char *vertShaderText =
1808 "#version 140\n"
1809 "#extension GL_ARB_separate_shader_objects : enable\n"
1810 "#extension GL_ARB_shading_language_420pack : enable\n"
1811 "layout (location = 0) out vec2 samplePos;\n"
1812 "void main() {\n"
1813 " vec2 vertices[3];"
1814 " vertices[0] = vec2(-0.5, -0.5);\n"
1815 " vertices[1] = vec2( 0.5, -0.5);\n"
1816 " vertices[2] = vec2( 0.5, 0.5);\n"
1817 " vec2 positions[3];"
1818 " positions[0] = vec2( 0.0, 0.0);\n"
1819 " positions[1] = vec2( 1.0, 0.0);\n"
1820 " positions[2] = vec2( 1.0, 1.0);\n"
1821 " samplePos = positions[gl_VertexID % 3];\n"
1822 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1823 "}\n";
1824
1825 static const char *fragShaderText =
1826 "#version 140\n"
1827 "#extension GL_ARB_separate_shader_objects : enable\n"
1828 "#extension GL_ARB_shading_language_420pack : enable\n"
1829 "layout (location = 0) in vec2 samplePos;\n"
1830 "layout (binding = 0) uniform sampler2D surface;\n"
1831 "layout (location=0) out vec4 outColor;\n"
1832 "void main() {\n"
1833 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1834 " outColor = texColor;\n"
1835 "}\n";
1836
1837 ASSERT_NO_FATAL_FAILURE(InitState());
1838 ASSERT_NO_FATAL_FAILURE(InitViewport());
1839
1840 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1841 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1842 XglSamplerObj sampler(m_device);
1843 XglTextureObj texture(m_device);
1844
1845 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1846 ps.BindShaderEntitySlotToSampler(0, &sampler);
1847
1848 XglPipelineObj pipelineobj(m_device);
1849 pipelineobj.AddShader(&vs);
1850 pipelineobj.AddShader(&ps);
1851
1852 XglDescriptorSetObj descriptorSet(m_device);
1853 descriptorSet.AttachImageView(&texture);
1854 descriptorSet.AttachSampler(&sampler);
1855
1856 m_memoryRefManager.AddMemoryRef(&texture);
1857
Tony Barboura3953b82014-12-03 15:46:29 -07001858 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001859 QueueCommandBuffer(NULL, 0);
1860}
1861TEST_F(XglRenderTest, TexturedTriangleClip)
1862{
1863 // The expected result from this test is a red and green checkered triangle
1864 static const char *vertShaderText =
1865 "#version 330\n"
1866 "#extension GL_ARB_separate_shader_objects : enable\n"
1867 "#extension GL_ARB_shading_language_420pack : enable\n"
1868 "layout (location = 0) out vec2 samplePos;\n"
1869 "out gl_PerVertex {\n"
1870 " vec4 gl_Position;\n"
1871 " float gl_ClipDistance[1];\n"
1872 "};\n"
1873 "void main() {\n"
1874 " vec2 vertices[3];"
1875 " vertices[0] = vec2(-0.5, -0.5);\n"
1876 " vertices[1] = vec2( 0.5, -0.5);\n"
1877 " vertices[2] = vec2( 0.5, 0.5);\n"
1878 " vec2 positions[3];"
1879 " positions[0] = vec2( 0.0, 0.0);\n"
1880 " positions[1] = vec2( 1.0, 0.0);\n"
1881 " positions[2] = vec2( 1.0, 1.0);\n"
1882 " float dists[3];\n"
1883 " dists[0] = 1.0;\n"
1884 " dists[1] = 1.0;\n"
1885 " dists[2] = -1.0;\n"
1886 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1887 " samplePos = positions[gl_VertexID % 3];\n"
1888 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1889 "}\n";
1890
1891 static const char *fragShaderText =
1892 "#version 140\n"
1893 "#extension GL_ARB_separate_shader_objects : enable\n"
1894 "#extension GL_ARB_shading_language_420pack : enable\n"
1895 "layout (location = 0) in vec2 samplePos;\n"
1896 "layout (binding = 0) uniform sampler2D surface;\n"
1897 "layout (location=0) out vec4 outColor;\n"
1898 "void main() {\n"
1899 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1900 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1901 " outColor = texColor;\n"
1902 "}\n";
1903
1904
1905 ASSERT_NO_FATAL_FAILURE(InitState());
1906 ASSERT_NO_FATAL_FAILURE(InitViewport());
1907
1908 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1909 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1910 XglSamplerObj sampler(m_device);
1911 XglTextureObj texture(m_device);
1912
1913 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1914 ps.BindShaderEntitySlotToSampler(0, &sampler);
1915
1916 XglPipelineObj pipelineobj(m_device);
1917 pipelineobj.AddShader(&vs);
1918 pipelineobj.AddShader(&ps);
1919
1920 XglDescriptorSetObj descriptorSet(m_device);
1921 descriptorSet.AttachImageView(&texture);
1922 descriptorSet.AttachSampler(&sampler);
1923
1924 m_memoryRefManager.AddMemoryRef(&texture);
1925
Tony Barboura3953b82014-12-03 15:46:29 -07001926 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001927 QueueCommandBuffer(NULL, 0);
1928}
1929TEST_F(XglRenderTest, FSTriangle)
1930{
1931 // The expected result from this test is a red and green checkered triangle
1932 static const char *vertShaderText =
1933 "#version 140\n"
1934 "#extension GL_ARB_separate_shader_objects : enable\n"
1935 "#extension GL_ARB_shading_language_420pack : enable\n"
1936 "layout (location = 0) out vec2 samplePos;\n"
1937 "void main() {\n"
1938 " vec2 vertices[3];"
1939 " vertices[0] = vec2(-0.5, -0.5);\n"
1940 " vertices[1] = vec2( 0.5, -0.5);\n"
1941 " vertices[2] = vec2( 0.5, 0.5);\n"
1942 " vec2 positions[3];"
1943 " positions[0] = vec2( 0.0, 0.0);\n"
1944 " positions[1] = vec2( 1.0, 0.0);\n"
1945 " positions[2] = vec2( 1.0, 1.0);\n"
1946 " samplePos = positions[gl_VertexID % 3];\n"
1947 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1948 "}\n";
1949
1950 static const char *fragShaderText =
1951 "#version 140\n"
1952 "#extension GL_ARB_separate_shader_objects : enable\n"
1953 "#extension GL_ARB_shading_language_420pack : enable\n"
1954 "layout (location = 0) in vec2 samplePos;\n"
1955 "layout (binding = 0) uniform sampler2D surface;\n"
1956 "layout (location=0) out vec4 outColor;\n"
1957 "void main() {\n"
1958 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1959 " outColor = texColor;\n"
1960 "}\n";
1961
1962 ASSERT_NO_FATAL_FAILURE(InitState());
1963 ASSERT_NO_FATAL_FAILURE(InitViewport());
1964
1965 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1966 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1967 XglSamplerObj sampler(m_device);
1968 XglTextureObj texture(m_device);
1969
1970 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1971 ps.BindShaderEntitySlotToSampler(0, &sampler);
1972
1973 XglPipelineObj pipelineobj(m_device);
1974 pipelineobj.AddShader(&vs);
1975 pipelineobj.AddShader(&ps);
1976
1977 XglDescriptorSetObj descriptorSet(m_device);
1978 descriptorSet.AttachImageView(&texture);
1979 descriptorSet.AttachSampler(&sampler);
1980
1981 m_memoryRefManager.AddMemoryRef(&texture);
1982
Tony Barboura3953b82014-12-03 15:46:29 -07001983 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001984 QueueCommandBuffer(NULL, 0);
1985}
1986TEST_F(XglRenderTest, SamplerBindingsTriangle)
1987{
1988 // This test sets bindings on the samplers
1989 // For now we are asserting that sampler and texture pairs
1990 // march in lock step, and are set via GLSL binding. This can
1991 // and will probably change.
1992 // The sampler bindings should match the sampler and texture slot
1993 // number set up by the application.
1994 // This test will result in a blue triangle
1995 static const char *vertShaderText =
1996 "#version 140\n"
1997 "#extension GL_ARB_separate_shader_objects : enable\n"
1998 "#extension GL_ARB_shading_language_420pack : enable\n"
1999 "layout (location = 0) out vec4 samplePos;\n"
2000 "void main() {\n"
2001 " vec2 vertices[3];"
2002 " vertices[0] = vec2(-0.5, -0.5);\n"
2003 " vertices[1] = vec2( 0.5, -0.5);\n"
2004 " vertices[2] = vec2( 0.5, 0.5);\n"
2005 " vec2 positions[3];"
2006 " positions[0] = vec2( 0.0, 0.0);\n"
2007 " positions[1] = vec2( 1.0, 0.0);\n"
2008 " positions[2] = vec2( 1.0, 1.0);\n"
2009 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
2010 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2011 "}\n";
2012
2013 static const char *fragShaderText =
2014 "#version 140\n"
2015 "#extension GL_ARB_separate_shader_objects : enable\n"
2016 "#extension GL_ARB_shading_language_420pack : enable\n"
2017 "layout (location = 0) in vec4 samplePos;\n"
2018 "layout (binding = 0) uniform sampler2D surface0;\n"
2019 "layout (binding = 1) uniform sampler2D surface1;\n"
2020 "layout (binding = 12) uniform sampler2D surface2;\n"
2021 "void main() {\n"
2022 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
2023 "}\n";
2024
2025 ASSERT_NO_FATAL_FAILURE(InitState());
2026 ASSERT_NO_FATAL_FAILURE(InitViewport());
2027
2028 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2029 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2030
2031 XglSamplerObj sampler1(m_device);
2032 XglSamplerObj sampler2(m_device);
2033 XglSamplerObj sampler3(m_device);
2034
2035 XglTextureObj texture1(m_device); // Red
2036 texture1.ChangeColors(0xffff0000,0xffff0000);
2037 XglTextureObj texture2(m_device); // Green
2038 texture2.ChangeColors(0xff00ff00,0xff00ff00);
2039 XglTextureObj texture3(m_device); // Blue
2040 texture3.ChangeColors(0xff0000ff,0xff0000ff);
2041
2042 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture1);
2043 ps.BindShaderEntitySlotToSampler(0, &sampler1);
2044
2045 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_RESOURCE, &texture2);
2046 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2047
2048 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_RESOURCE, &texture3);
2049 ps.BindShaderEntitySlotToSampler(12, &sampler3);
2050
2051 XglPipelineObj pipelineobj(m_device);
2052 pipelineobj.AddShader(&vs);
2053 pipelineobj.AddShader(&ps);
2054
2055 XglDescriptorSetObj descriptorSet(m_device);
2056 descriptorSet.AttachImageView(&texture1);
2057 descriptorSet.AttachSampler(&sampler1);
2058 descriptorSet.AttachImageView(&texture2);
2059 descriptorSet.AttachSampler(&sampler2);
2060 descriptorSet.AttachImageView(&texture3);
2061 descriptorSet.AttachSampler(&sampler3);
2062
2063 m_memoryRefManager.AddMemoryRef(&texture1);
2064 m_memoryRefManager.AddMemoryRef(&texture2);
2065 m_memoryRefManager.AddMemoryRef(&texture3);
2066
Tony Barboura3953b82014-12-03 15:46:29 -07002067 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002068 QueueCommandBuffer(NULL, 0);
2069
2070}
2071
2072TEST_F(XglRenderTest, TriangleVSUniformBlock)
2073{
2074 // The expected result from this test is a blue triangle
2075
2076 static const char *vertShaderText =
2077 "#version 140\n"
2078 "#extension GL_ARB_separate_shader_objects : enable\n"
2079 "#extension GL_ARB_shading_language_420pack : enable\n"
2080 "layout (location = 0) out vec4 outColor;\n"
2081 "layout (std140, binding = 0) uniform bufferVals {\n"
2082 " vec4 red;\n"
2083 " vec4 green;\n"
2084 " vec4 blue;\n"
2085 " vec4 white;\n"
2086 "} myBufferVals;\n"
2087 "void main() {\n"
2088 " vec2 vertices[3];"
2089 " vertices[0] = vec2(-0.5, -0.5);\n"
2090 " vertices[1] = vec2( 0.5, -0.5);\n"
2091 " vertices[2] = vec2( 0.5, 0.5);\n"
2092 " outColor = myBufferVals.blue;\n"
2093 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2094 "}\n";
2095
2096 static const char *fragShaderText =
2097 "#version 140\n"
2098 "#extension GL_ARB_separate_shader_objects : enable\n"
2099 "#extension GL_ARB_shading_language_420pack : enable\n"
2100 "layout (location = 0) in vec4 inColor;\n"
2101 "void main() {\n"
2102 " gl_FragColor = inColor;\n"
2103 "}\n";
2104
2105 ASSERT_NO_FATAL_FAILURE(InitState());
2106 ASSERT_NO_FATAL_FAILURE(InitViewport());
2107
2108 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2109 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2110
2111 // Let's populate our buffer with the following:
2112 // vec4 red;
2113 // vec4 green;
2114 // vec4 blue;
2115 // vec4 white;
2116 const int valCount = 4 * 4;
2117 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2118 0.0, 1.0, 0.0, 1.0,
2119 0.0, 0.0, 1.0, 1.0,
2120 1.0, 1.0, 1.0, 1.0 };
2121
2122 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2123 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2124
2125 XglPipelineObj pipelineobj(m_device);
2126 pipelineobj.AddShader(&vs);
2127 pipelineobj.AddShader(&ps);
2128
2129 XglDescriptorSetObj descriptorSet(m_device);
2130 descriptorSet.AttachMemoryView(&colorBuffer);
2131
Tony Barboura3953b82014-12-03 15:46:29 -07002132 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002133 QueueCommandBuffer(NULL, 0);
2134
2135}
2136
2137TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2138{
2139 // This test allows the shader to select which buffer it is
2140 // pulling from using layout binding qualifier.
2141 // There are corresponding changes in the compiler stack that
2142 // will select the buffer using binding directly.
2143 // The binding number should match the slot number set up by
2144 // the application.
2145 // The expected result from this test is a purple triangle
2146
2147 static const char *vertShaderText =
2148 "#version 140\n"
2149 "#extension GL_ARB_separate_shader_objects : enable\n"
2150 "#extension GL_ARB_shading_language_420pack : enable\n"
2151 "void main() {\n"
2152 " vec2 vertices[3];"
2153 " vertices[0] = vec2(-0.5, -0.5);\n"
2154 " vertices[1] = vec2( 0.5, -0.5);\n"
2155 " vertices[2] = vec2( 0.5, 0.5);\n"
2156 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2157 "}\n";
2158
2159 static const char *fragShaderText =
2160 "#version 140\n"
2161 "#extension GL_ARB_separate_shader_objects : enable\n"
2162 "#extension GL_ARB_shading_language_420pack : enable\n"
2163 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2164 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2165 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2166 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2167 "void main() {\n"
2168 " gl_FragColor = myBlueVal.color;\n"
2169 " gl_FragColor += myRedVal.color;\n"
2170 "}\n";
2171
2172 ASSERT_NO_FATAL_FAILURE(InitState());
2173 ASSERT_NO_FATAL_FAILURE(InitViewport());
2174
2175 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2176 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2177
2178 // We're going to create a number of uniform buffers, and then allow
2179 // the shader to select which it wants to read from with a binding
2180
2181 // Let's populate the buffers with a single color each:
2182 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2183 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2184 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2185 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2186
2187 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2188 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2189 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2190 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2191
2192 const int redCount = sizeof(redVals) / sizeof(float);
2193 const int greenCount = sizeof(greenVals) / sizeof(float);
2194 const int blueCount = sizeof(blueVals) / sizeof(float);
2195 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2196
2197 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2198 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2199
2200 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2201 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2202
2203 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2204 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2205
2206 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2207 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2208
2209 XglPipelineObj pipelineobj(m_device);
2210 pipelineobj.AddShader(&vs);
2211 pipelineobj.AddShader(&ps);
2212
2213 XglDescriptorSetObj descriptorSet(m_device);
2214 descriptorSet.AttachMemoryView(&redBuffer);
2215 descriptorSet.AttachMemoryView(&greenBuffer);
2216 descriptorSet.AttachMemoryView(&blueBuffer);
2217 descriptorSet.AttachMemoryView(&whiteBuffer);
2218
Tony Barboura3953b82014-12-03 15:46:29 -07002219 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002220 QueueCommandBuffer(NULL, 0);
2221
2222}
2223
2224TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2225{
2226 // This test is the same as TriangleFSUniformBlockBinding, but
2227 // it does not provide an instance name.
2228 // The expected result from this test is a purple triangle
2229
2230 static const char *vertShaderText =
2231 "#version 140\n"
2232 "#extension GL_ARB_separate_shader_objects : enable\n"
2233 "#extension GL_ARB_shading_language_420pack : enable\n"
2234 "void main() {\n"
2235 " vec2 vertices[3];"
2236 " vertices[0] = vec2(-0.5, -0.5);\n"
2237 " vertices[1] = vec2( 0.5, -0.5);\n"
2238 " vertices[2] = vec2( 0.5, 0.5);\n"
2239 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2240 "}\n";
2241
2242 static const char *fragShaderText =
2243 "#version 430\n"
2244 "#extension GL_ARB_separate_shader_objects : enable\n"
2245 "#extension GL_ARB_shading_language_420pack : enable\n"
2246 "layout (location = 0) uniform vec4 foo;\n"
2247 "layout (location = 1) uniform vec4 bar;\n"
2248 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2249 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2250 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2251 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
Cody Northrop68a10f62014-12-05 15:44:14 -07002252 "layout (location = 0) out vec4 outColor;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002253 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -07002254 " outColor = blue;\n"
2255 " outColor += red;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002256 "}\n";
2257 ASSERT_NO_FATAL_FAILURE(InitState());
2258 ASSERT_NO_FATAL_FAILURE(InitViewport());
2259
2260 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2261 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2262
2263 // We're going to create a number of uniform buffers, and then allow
2264 // the shader to select which it wants to read from with a binding
2265
2266 // Let's populate the buffers with a single color each:
2267 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2268 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2269 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2270 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2271
2272 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2273 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2274 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2275 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2276
2277 const int redCount = sizeof(redVals) / sizeof(float);
2278 const int greenCount = sizeof(greenVals) / sizeof(float);
2279 const int blueCount = sizeof(blueVals) / sizeof(float);
2280 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2281
2282 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2283 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2284
2285 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2286 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2287
2288 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2289 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2290
2291 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2292 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2293
2294 XglPipelineObj pipelineobj(m_device);
2295 pipelineobj.AddShader(&vs);
2296 pipelineobj.AddShader(&ps);
2297
2298 XglDescriptorSetObj descriptorSet(m_device);
2299 descriptorSet.AttachMemoryView(&redBuffer);
2300 descriptorSet.AttachMemoryView(&greenBuffer);
2301 descriptorSet.AttachMemoryView(&blueBuffer);
2302 descriptorSet.AttachMemoryView(&whiteBuffer);
2303
Tony Barboura3953b82014-12-03 15:46:29 -07002304 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002305 QueueCommandBuffer(NULL, 0);
2306
2307}
2308
2309TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2310{
2311 static const char *vertShaderText =
2312 "#version 140\n"
2313 "#extension GL_ARB_separate_shader_objects : enable\n"
2314 "#extension GL_ARB_shading_language_420pack : enable\n"
2315 "layout (std140, binding=0) uniform bufferVals {\n"
2316 " mat4 mvp;\n"
2317 "} myBufferVals;\n"
2318 "layout (location=0) in vec4 pos;\n"
2319 "layout (location=0) out vec2 UV;\n"
2320 "void main() {\n"
2321 " vec2 positions[3];"
2322 " positions[0] = vec2( 0.0, 0.0);\n"
2323 " positions[1] = vec2( 0.25, 0.1);\n"
2324 " positions[2] = vec2( 0.1, 0.25);\n"
2325 " UV = positions[gl_VertexID % 3];\n"
2326 " gl_Position = myBufferVals.mvp * pos;\n"
2327 "}\n";
2328
2329 static const char *fragShaderText =
2330 "#version 140\n"
2331 "#extension GL_ARB_separate_shader_objects : enable\n"
2332 "#extension GL_ARB_shading_language_420pack : enable\n"
2333 "layout (binding=0) uniform sampler2D surface;\n"
2334 "layout (location=0) out vec4 outColor;\n"
2335 "layout (location=0) in vec2 UV;\n"
2336 "void main() {\n"
2337 " outColor= textureLod(surface, UV, 0.0);\n"
2338 "}\n";
2339 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2340
2341 glm::mat4 View = glm::lookAt(
2342 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2343 glm::vec3(0,0,0), // and looks at the origin
2344 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2345 );
2346
2347 glm::mat4 Model = glm::mat4(1.0f);
2348
2349 glm::mat4 MVP = Projection * View * Model;
2350
2351
2352 ASSERT_NO_FATAL_FAILURE(InitState());
2353 ASSERT_NO_FATAL_FAILURE(InitViewport());
2354 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2355
2356 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2357 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
2358 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
2359
2360
2361 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2362
2363 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2364 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2365 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2366 XglSamplerObj sampler(m_device);
2367 XglTextureObj texture(m_device);
2368
2369 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2370 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
2371 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
2372 ps.BindShaderEntitySlotToSampler(0, &sampler);
2373
2374 XglPipelineObj pipelineobj(m_device);
2375 pipelineobj.AddShader(&vs);
2376 pipelineobj.AddShader(&ps);
2377
2378 XglDescriptorSetObj descriptorSet(m_device);
2379
2380 descriptorSet.AttachMemoryView(&mvpBuffer);
2381 descriptorSet.AttachImageView(&texture);
2382 descriptorSet.AttachSampler(&sampler);
2383
2384 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2385 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2386 m_memoryRefManager.AddMemoryRef(&texture);
2387
2388 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2389 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2390 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2391 };
2392
2393 // this is the current description of g_vbData
2394 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2395 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2396 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2397 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2398 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2399 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2400 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2401 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2402 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2403
2404 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2405 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2406 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2407
Tony Barboura3953b82014-12-03 15:46:29 -07002408 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07002409
2410 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2411
2412}
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002413int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002414 int result;
2415
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002416 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002417 XglTestFramework::InitArgs(&argc, argv);
2418
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002419 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
2420
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002421 result = RUN_ALL_TESTS();
2422
2423 XglTestFramework::Finish();
2424 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002425}