blob: 57be0d603d81596d18fdbd9cff9d429a3a65bb36 [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 InitDepthStencil();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600225 void GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600226 void XGLTriangleTest(const char *vertShaderText, const char *fragShaderText);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600227
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600228
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600229protected:
Cody Northrop350727b2014-10-06 15:42:00 -0600230 XGL_IMAGE m_texture;
231 XGL_IMAGE_VIEW m_textureView;
232 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
233 XGL_GPU_MEMORY m_textureMem;
234
235 XGL_SAMPLER m_sampler;
236
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600237 XGL_FORMAT m_depth_stencil_fmt;
238 XGL_IMAGE m_depthStencilImage;
239 XGL_GPU_MEMORY m_depthStencilMem;
240 XGL_DEPTH_STENCIL_VIEW m_depthStencilView;
Tony Barbourf43b6982014-11-25 13:18:32 -0700241 XglMemoryRefManager m_memoryRefManager;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600242
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600243
244 virtual void SetUp() {
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600245
246 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
247 this->app_info.pNext = NULL;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600248 this->app_info.pAppName = (const XGL_CHAR *) "render_tests";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600249 this->app_info.appVersion = 1;
250 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
251 this->app_info.engineVersion = 1;
252 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
253
Cody Northrop350727b2014-10-06 15:42:00 -0600254 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
255 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barbour97a36232014-12-04 17:14:45 -0700256 memset(&m_depthStencilImage, 0, sizeof(m_depthStencilImage));
Cody Northrop350727b2014-10-06 15:42:00 -0600257
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600258 InitFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600259 }
260
261 virtual void TearDown() {
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600262 // Clean up resources before we reset
263 ShutdownFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600264 }
265};
266
Tony Barbourf43b6982014-11-25 13:18:32 -0700267
Tony Barboura3953b82014-12-03 15:46:29 -0700268void XglRenderTest::GenericDrawTriangleTest(XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet,int numTris)
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600269{
270 XGL_RESULT err = XGL_SUCCESS;
271
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600272 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600273 // Build command buffer
274 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
275 ASSERT_XGL_SUCCESS(err);
276
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600277 GenerateClearAndPrepareBufferCmds();
278 GenerateBindRenderTargetCmd();
Tobin Ehlis31446e52014-11-28 11:17:19 -0700279#ifdef DUMP_STATE_DOT
280 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
281 pDSDumpDot((char*)"triTest.dot");
282#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700283 GenerateBindStateAndPipelineCmds();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600284
Tony Barboura3953b82014-12-03 15:46:29 -0700285 pipelineobj->BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
Tony Barbourbf678472014-12-03 13:58:15 -0700286 descriptorSet->BindCommandBuffer(m_cmdBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -0700287
288 // render the triangle
289 xglCmdDraw( m_cmdBuffer, 0, 3*numTris, 0, 1 );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600290
291 // finalize recording of the command buffer
292 err = xglEndCommandBuffer( m_cmdBuffer );
293 ASSERT_XGL_SUCCESS( err );
Tony Barbourf43b6982014-11-25 13:18:32 -0700294}
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600295
Tony Barbourf43b6982014-11-25 13:18:32 -0700296void XglRenderTest::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
297{
298 XGL_RESULT err = XGL_SUCCESS;
Chia-I Wue7748802014-12-05 10:45:15 +0800299 XGL_UINT i;
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
Chia-I Wue7748802014-12-05 10:45:15 +0800311 for (i = 0; i < m_renderTargetCount; i++)
312 RecordImage(m_renderTargets[i]);
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
Chia-I Wue7748802014-12-05 10:45:15 +0800367 assert(m_renderTargetCount == 1);
368 RecordImage(m_renderTargets[0]);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600369
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600370}
371
Tony Barbourf43b6982014-11-25 13:18:32 -0700372void XglRenderTest::RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
Tony Barbour09da2212014-12-03 16:13:23 -0700373 XglConstantBufferObj *constantBuffer)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600374{
375 int i;
376 glm::mat4 MVP;
377 int matrixSize = sizeof(MVP);
378 XGL_RESULT err;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600379
380 for (i = 0; i < 8; i++) {
381 XGL_UINT8 *pData;
Tony Barbour09da2212014-12-03 16:13:23 -0700382 err = xglMapMemory(constantBuffer->m_constantBufferMem, 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600383 ASSERT_XGL_SUCCESS(err);
384
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600385 Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
386 MVP = Projection * View * Model;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600387 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
388
Tony Barbour09da2212014-12-03 16:13:23 -0700389 err = xglUnmapMemory(constantBuffer->m_constantBufferMem);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600390 ASSERT_XGL_SUCCESS(err);
391
392 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700393 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_memoryRefManager.GetNumRefs(), m_memoryRefManager.GetMemoryRefList(), NULL );
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600394 ASSERT_XGL_SUCCESS( err );
395
396 err = xglQueueWaitIdle( m_device->m_queue );
397 ASSERT_XGL_SUCCESS( err );
398
399 // Wait for work to finish before cleaning up.
400 xglDeviceWaitIdle(m_device->device());
401
Chia-I Wue7748802014-12-05 10:45:15 +0800402 assert(m_renderTargetCount == 1);
403 RecordImage(m_renderTargets[0]);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600404 }
Cody Northrop7a1f0462014-10-10 14:49:36 -0600405}
406
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600407void dumpMatrix(const char *note, glm::mat4 MVP)
408{
409 int i,j;
410
411 printf("%s: \n", note);
412 for (i=0; i<4; i++) {
413 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
414 }
415 printf("\n");
416 fflush(stdout);
417}
418
419void dumpVec4(const char *note, glm::vec4 vector)
420{
421 printf("%s: \n", note);
422 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
423 printf("\n");
424 fflush(stdout);
425}
426
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600427void XglRenderTest::GenerateClearAndPrepareBufferCmds()
428{
429 XglRenderFramework::GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600430
Tony Barbour97a36232014-12-04 17:14:45 -0700431 if (m_depthStencilImage) {
432 printf("Clearing Depth Stencil\n");fflush(stdout);
433 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
434 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
435 dsRange.baseMipLevel = 0;
436 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
437 dsRange.baseArraySlice = 0;
438 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
Cody Northrop7a1f0462014-10-10 14:49:36 -0600439
Tony Barbour97a36232014-12-04 17:14:45 -0700440 // prepare the depth buffer for clear
441 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
442 transitionToClear.image = m_depthStencilImage;
443 transitionToClear.oldState = m_depthStencilBinding.depthState;
444 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
445 transitionToClear.subresourceRange = dsRange;
446 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
447 m_depthStencilBinding.depthState = transitionToClear.newState;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600448
Tony Barbour97a36232014-12-04 17:14:45 -0700449 xglCmdClearDepthStencil(m_cmdBuffer, m_depthStencilImage, 1.0f, 0, 1, &dsRange);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600450
Tony Barbour97a36232014-12-04 17:14:45 -0700451 // prepare depth buffer for rendering
452 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
Chia-I Wu18749b02014-12-05 10:48:20 +0800453 transitionToRender.image = m_depthStencilImage;
Tony Barbour97a36232014-12-04 17:14:45 -0700454 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
455 transitionToRender.newState = m_depthStencilBinding.depthState;
456 transitionToRender.subresourceRange = dsRange;
457 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
458 m_depthStencilBinding.depthState = transitionToClear.newState;
459 }
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600460}
461
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600462void XglRenderTest::InitDepthStencil()
463{
464 XGL_RESULT err;
465 XGL_IMAGE_CREATE_INFO image;
466 XGL_MEMORY_ALLOC_INFO mem_alloc;
467 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
468 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn6317c492014-11-21 11:33:20 -0700469 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600470
471 // Clean up default state created by framework
472 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
473
474 m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16;
475 m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS;
476
477 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
478 image.pNext = NULL;
479 image.imageType = XGL_IMAGE_2D;
480 image.format = m_depth_stencil_fmt;
481 image.extent.width = m_width;
482 image.extent.height = m_height;
483 image.extent.depth = 1;
484 image.mipLevels = 1;
485 image.arraySize = 1;
486 image.samples = 1;
487 image.tiling = XGL_OPTIMAL_TILING;
488 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
489 image.flags = 0;
490
491 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
492 mem_alloc.pNext = NULL;
493 mem_alloc.allocationSize = 0;
494 mem_alloc.alignment = 0;
495 mem_alloc.flags = 0;
496 mem_alloc.heapCount = 0;
497 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
498
499 /* create image */
500 err = xglCreateImage(device(), &image,
501 &m_depthStencilImage);
502 ASSERT_XGL_SUCCESS(err);
503
504 err = xglGetObjectInfo(m_depthStencilImage,
505 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
506 &mem_reqs_size, &mem_reqs);
507 ASSERT_XGL_SUCCESS(err);
508 ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs));
509
510 mem_alloc.allocationSize = mem_reqs.size;
511 mem_alloc.alignment = mem_reqs.alignment;
512 mem_alloc.heapCount = mem_reqs.heapCount;
513 memcpy(mem_alloc.heaps, mem_reqs.heaps,
514 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
515
516 /* allocate memory */
517 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem);
518 ASSERT_XGL_SUCCESS(err);
519
520 /* bind memory */
521 err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0);
522 ASSERT_XGL_SUCCESS(err);
523
524 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
525 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
526 depthStencil.depthTestEnable = XGL_TRUE;
527 depthStencil.depthWriteEnable = XGL_TRUE;
528 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
529 depthStencil.depthBoundsEnable = XGL_FALSE;
530 depthStencil.minDepth = 0.f;
531 depthStencil.maxDepth = 1.f;
532 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
533 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
534 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
535 depthStencil.back.stencilRef = 0x00;
536 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
537 depthStencil.front = depthStencil.back;
538
539 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
540 ASSERT_XGL_SUCCESS( err );
541
542 /* create image view */
543 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
544 view.pNext = NULL;
545 view.image = XGL_NULL_HANDLE;
546 view.mipLevel = 0;
547 view.baseArraySlice = 0;
548 view.arraySize = 1;
549 view.flags = 0;
550 view.image = m_depthStencilImage;
551 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
552 ASSERT_XGL_SUCCESS(err);
553
554 m_depthStencilBinding.view = m_depthStencilView;
555 m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
556 m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
557}
558
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600559struct xgltriangle_vs_uniform {
560 // Must start with MVP
561 XGL_FLOAT mvp[4][4];
562 XGL_FLOAT position[3][4];
563 XGL_FLOAT color[3][4];
564};
565
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600566void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600567{
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700568#ifdef DEBUG_CALLBACK
569 xglDbgRegisterMsgCallback(myDbgFunc, NULL);
570#endif
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600571 // Create identity matrix
572 int i;
573 struct xgltriangle_vs_uniform data;
574
575 glm::mat4 Projection = glm::mat4(1.0f);
576 glm::mat4 View = glm::mat4(1.0f);
577 glm::mat4 Model = glm::mat4(1.0f);
578 glm::mat4 MVP = Projection * View * Model;
579 const int matrixSize = sizeof(MVP);
580 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT);
581 memcpy(&data.mvp, &MVP[0][0], matrixSize);
582
583 static const Vertex tri_data[] =
584 {
585 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
586 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
587 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
588 };
589
590 for (i=0; i<3; i++) {
591 data.position[i][0] = tri_data[i].posX;
592 data.position[i][1] = tri_data[i].posY;
593 data.position[i][2] = tri_data[i].posZ;
594 data.position[i][3] = tri_data[i].posW;
595 data.color[i][0] = tri_data[i].r;
596 data.color[i][1] = tri_data[i].g;
597 data.color[i][2] = tri_data[i].b;
598 data.color[i][3] = tri_data[i].a;
599 }
600
Tony Barbourf43b6982014-11-25 13:18:32 -0700601 ASSERT_NO_FATAL_FAILURE(InitState());
602 ASSERT_NO_FATAL_FAILURE(InitViewport());
603
604 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data);
605
606 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
607 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
608 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
609
610 XglPipelineObj pipelineobj(m_device);
611 pipelineobj.AddShader(&vs);
612 pipelineobj.AddShader(&ps);
613
614 XglDescriptorSetObj descriptorSet(m_device);
615 descriptorSet.AttachMemoryView(&constantBuffer);
616 m_memoryRefManager.AddMemoryRef(&constantBuffer);
617
Tony Barboura3953b82014-12-03 15:46:29 -0700618 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -0700619 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
620
Tony Barbour09da2212014-12-03 16:13:23 -0700621 RotateTriangleVSUniform(Projection, View, Model, &constantBuffer);
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700622#ifdef PRINT_OBJECTS
623 //XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)
624 OBJ_TRACK_GET_OBJECT_COUNT pObjTrackGetObjectCount = (OBJ_TRACK_GET_OBJECT_COUNT)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjectCount");
625 XGL_UINT64 numObjects = pObjTrackGetObjectCount(XGL_OBJECT_TYPE_ANY);
626 //OBJ_TRACK_GET_OBJECTS pGetObjsFunc = xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
627 printf("DEBUG : Number of Objects : %lu\n", numObjects);
628 OBJ_TRACK_GET_OBJECTS pObjTrackGetObjs = (OBJ_TRACK_GET_OBJECTS)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
629 OBJTRACK_NODE* pObjNodeArray = (OBJTRACK_NODE*)malloc(sizeof(OBJTRACK_NODE)*numObjects);
630 pObjTrackGetObjs(XGL_OBJECT_TYPE_ANY, numObjects, pObjNodeArray);
631 for (i=0; i < numObjects; i++) {
632 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);
633 }
634 free(pObjNodeArray);
635#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700636
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600637}
638
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600639TEST_F(XglRenderTest, XGLTriangle_FragColor)
640{
641 static const char *vertShaderText =
642 "#version 140\n"
643 "#extension GL_ARB_separate_shader_objects : enable\n"
644 "#extension GL_ARB_shading_language_420pack : enable\n"
645 "\n"
646 "layout(binding = 0) uniform buf {\n"
647 " mat4 MVP;\n"
648 " vec4 position[3];\n"
649 " vec4 color[3];\n"
650 "} ubuf;\n"
651 "\n"
652 "layout (location = 0) out vec4 outColor;\n"
653 "\n"
654 "void main() \n"
655 "{\n"
656 " outColor = ubuf.color[gl_VertexID];\n"
657 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
658 "}\n";
659
660 static const char *fragShaderText =
661 "#version 140\n"
662 "#extension GL_ARB_separate_shader_objects : enable\n"
663 "#extension GL_ARB_shading_language_420pack : enable\n"
664 "\n"
665 "layout (location = 0) in vec4 inColor;\n"
666 "\n"
667 "void main()\n"
668 "{\n"
669 " gl_FragColor = inColor;\n"
670 "}\n";
671
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600672 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600673 XGLTriangleTest(vertShaderText, fragShaderText);
674}
675
676TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
677{
678 static const char *vertShaderText =
679 "#version 140\n"
680 "#extension GL_ARB_separate_shader_objects : enable\n"
681 "#extension GL_ARB_shading_language_420pack : enable\n"
682 "\n"
683 "layout(binding = 0) uniform buf {\n"
684 " mat4 MVP;\n"
685 " vec4 position[3];\n"
686 " vec4 color[3];\n"
687 "} ubuf;\n"
688 "\n"
689 "layout (location = 0) out vec4 outColor;\n"
690 "\n"
691 "void main() \n"
692 "{\n"
693 " outColor = ubuf.color[gl_VertexID];\n"
694 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
695 "}\n";
696
697 static const char *fragShaderText =
698 "#version 140\n"
699 "#extension GL_ARB_separate_shader_objects : enable\n"
700 "#extension GL_ARB_shading_language_420pack : enable\n"
701 "\n"
702 "layout (location = 0) in vec4 inColor;\n"
703 "layout (location = 0) out vec4 outColor;\n"
704 "\n"
705 "void main()\n"
706 "{\n"
707 " outColor = inColor;\n"
708 "}\n";
709
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600710 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 -0600711
712 XGLTriangleTest(vertShaderText, fragShaderText);
713}
714
Tony Barbourf43b6982014-11-25 13:18:32 -0700715TEST_F(XglRenderTest, BIL_XGLTriangle)
716{
717 bool saved_use_bil = XglTestFramework::m_use_bil;
718
719 static const char *vertShaderText =
720 "#version 140\n"
721 "#extension GL_ARB_separate_shader_objects : enable\n"
722 "#extension GL_ARB_shading_language_420pack : enable\n"
723 "\n"
724 "layout(binding = 0) uniform buf {\n"
725 " mat4 MVP;\n"
726 " vec4 position[3];\n"
727 " vec4 color[3];\n"
728 "} ubuf;\n"
729 "\n"
730 "layout (location = 0) out vec4 outColor;\n"
731 "\n"
732 "void main() \n"
733 "{\n"
734 " outColor = ubuf.color[gl_VertexID];\n"
735 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
736 "}\n";
737
738 static const char *fragShaderText =
739 "#version 140\n"
740 "#extension GL_ARB_separate_shader_objects : enable\n"
741 "#extension GL_ARB_shading_language_420pack : enable\n"
742 "\n"
743 "layout (location = 0) in vec4 inColor;\n"
744 "\n"
745 "void main()\n"
746 "{\n"
747 " gl_FragColor = inColor;\n"
748 "}\n";
749
750 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver.");
751
752 XglTestFramework::m_use_bil = true;
753
754 XGLTriangleTest(vertShaderText, fragShaderText);
755
756 XglTestFramework::m_use_bil = saved_use_bil;
757}
758
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600759TEST_F(XglRenderTest, GreenTriangle)
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600760{
761 static const char *vertShaderText =
762 "#version 130\n"
763 "vec2 vertices[3];\n"
764 "void main() {\n"
765 " vertices[0] = vec2(-1.0, -1.0);\n"
766 " vertices[1] = vec2( 1.0, -1.0);\n"
767 " vertices[2] = vec2( 0.0, 1.0);\n"
768 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
769 "}\n";
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600770
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600771 static const char *fragShaderText =
772 "#version 130\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600773 "void main() {\n"
Steve K10b32512014-10-10 08:54:29 -0600774 " gl_FragColor = vec4(0,1,0,1);\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600775 "}\n";
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600776
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600777 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
778
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600779 DrawTriangleTest(vertShaderText, fragShaderText);
780}
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600781
Tony Barbourf43b6982014-11-25 13:18:32 -0700782TEST_F(XglRenderTest, BIL_GreenTriangle)
783{
784 bool saved_use_bil = XglTestFramework::m_use_bil;
785
786 static const char *vertShaderText =
787 "#version 130\n"
788 "vec2 vertices[3];\n"
789 "void main() {\n"
790 " vertices[0] = vec2(-1.0, -1.0);\n"
791 " vertices[1] = vec2( 1.0, -1.0);\n"
792 " vertices[2] = vec2( 0.0, 1.0);\n"
793 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
794 "}\n";
795
796 static const char *fragShaderText =
797 "#version 130\n"
798 "void main() {\n"
799 " gl_FragColor = vec4(0,1,0,1);\n"
800 "}\n";
801
802 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver.");
803
804 XglTestFramework::m_use_bil = true;
805 DrawTriangleTest(vertShaderText, fragShaderText);
806 XglTestFramework::m_use_bil = saved_use_bil;
807}
808
809TEST_F(XglRenderTest, YellowTriangle)
810{
811 static const char *vertShaderText =
812 "#version 130\n"
813 "void main() {\n"
814 " vec2 vertices[3];"
815 " vertices[0] = vec2(-0.5, -0.5);\n"
816 " vertices[1] = vec2( 0.5, -0.5);\n"
817 " vertices[2] = vec2( 0.5, 0.5);\n"
818 " vec4 colors[3];\n"
819 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
820 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
821 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
822 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
823 "}\n";
824
825 static const char *fragShaderText =
826 "#version 130\n"
827 "void main() {\n"
828 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
829 "}\n";
830
831 DrawTriangleTest(vertShaderText, fragShaderText);
832}
833
834TEST_F(XglRenderTest, TriangleTwoFSUniforms)
835{
836 static const char *vertShaderText =
837 "#version 130\n"
838 "out vec4 color;\n"
839 "out vec4 scale;\n"
840 "out vec2 samplePos;\n"
841 "void main() {\n"
842 " vec2 vertices[3];"
843 " vertices[0] = vec2(-0.5, -0.5);\n"
844 " vertices[1] = vec2( 0.5, -0.5);\n"
845 " vertices[2] = vec2( 0.5, 0.5);\n"
846 " vec4 colors[3];\n"
847 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
848 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
849 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
850 " color = colors[gl_VertexID % 3];\n"
851 " vec2 positions[3];"
852 " positions[0] = vec2( 0.0, 0.0);\n"
853 " positions[1] = vec2( 1.0, 0.0);\n"
854 " positions[2] = vec2( 1.0, 1.0);\n"
855 " scale = vec4(0.0, 0.0, 0.0, 0.0);\n"
856 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
857 "}\n";
858
859
860 static const char *fragShaderText =
Cody Northrop68a10f62014-12-05 15:44:14 -0700861 "#version 130\n"
Tony Barbourf43b6982014-11-25 13:18:32 -0700862 "in vec4 color;\n"
863 "in vec4 scale;\n"
864 "uniform vec4 foo;\n"
865 "uniform vec4 bar;\n"
866 "void main() {\n"
867 // by default, with no location or blocks
868 // the compiler will read them from buffer
869 // in reverse order of first use in shader
870 // The buffer contains red, followed by blue,
871 // so foo should be blue, bar should be red
872 " gl_FragColor = color * scale * foo * bar + foo;\n"
873 "}\n";
874
875 ASSERT_NO_FATAL_FAILURE(InitState());
876 ASSERT_NO_FATAL_FAILURE(InitViewport());
877
878 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
879 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
880
881 const int constantCount = 8;
882 const float constants[constantCount] = { 1.0, 0.0, 0.0, 1.0,
883 0.0, 0.0, 1.0, 1.0 };
884 XglConstantBufferObj constantBuffer(m_device,constantCount, sizeof(constants[0]), (const void*) constants);
885 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
886
887 XglPipelineObj pipelineobj(m_device);
888 pipelineobj.AddShader(&vs);
889 pipelineobj.AddShader(&ps);
890
891 XglDescriptorSetObj descriptorSet(m_device);
892 descriptorSet.AttachMemoryView(&constantBuffer);
893
Tony Barboura3953b82014-12-03 15:46:29 -0700894 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -0700895 QueueCommandBuffer(NULL, 0);
896
897}
898
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600899TEST_F(XglRenderTest, TriangleWithVertexFetch)
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600900{
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600901 static const char *vertShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700902 "#version 130\n"
903 //XYZ1( -1, -1, -1 )
904 "in vec4 pos;\n"
905 //XYZ1( 0.f, 0.f, 0.f )
906 "in vec4 inColor;\n"
907 "out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600908 "void main() {\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -0600909 " outColor = inColor;\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600910 " gl_Position = pos;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600911 "}\n";
912
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600913
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600914 static const char *fragShaderText =
Cody Northrop68a10f62014-12-05 15:44:14 -0700915 "#version 140\n"
916 "#extension GL_ARB_separate_shader_objects : enable\n"
917 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -0700918 "in vec4 color;\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700919 "layout (location = 0) out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600920 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700921 " outColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600922 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600923
Tony Barbourf43b6982014-11-25 13:18:32 -0700924
925
926 ASSERT_NO_FATAL_FAILURE(InitState());
927 ASSERT_NO_FATAL_FAILURE(InitViewport());
928
929 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
930 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
931
932 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
933 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
934
935 XglPipelineObj pipelineobj(m_device);
936 pipelineobj.AddShader(&vs);
937 pipelineobj.AddShader(&ps);
938
939 XglDescriptorSetObj descriptorSet(m_device);
940 descriptorSet.AttachMemoryView(&meshBuffer);
941
942 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
943 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
944 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
945 };
946
947 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
948 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
949 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
950 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
951 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
952 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
953 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
954 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
955 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
956
957 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
958 pipelineobj.AddVertexInputBindings(&vi_binding,1);
959 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
960
Tony Barboura3953b82014-12-03 15:46:29 -0700961 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -0700962 QueueCommandBuffer(NULL, 0);
963
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600964}
965
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700966TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
967{
968 static const char *vertShaderText =
969 "#version 140\n"
970 "#extension GL_ARB_separate_shader_objects : enable\n"
971 "#extension GL_ARB_shading_language_420pack : enable\n"
972 "layout(location = 0) in vec4 pos;\n"
973 "layout(location = 1) in vec4 inColor;\n"
974 "layout(location = 0) out vec4 outColor;\n"
975 "void main() {\n"
976 " outColor = inColor;\n"
977 " gl_Position = pos;\n"
978 "}\n";
979
980
981 static const char *fragShaderText =
982 "#version 140\n"
983 "#extension GL_ARB_separate_shader_objects : enable\n"
984 "#extension GL_ARB_shading_language_420pack : enable\n"
985 "layout(location = 0) in vec4 color;\n"
986 "void main() {\n"
987 " gl_FragColor = color;\n"
988 "}\n";
989
990 const Vertex g_vbData[] =
991 {
992 // first tri
993 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
994 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
995 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
996
997 // second tri
998 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
999 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1000 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
1001 };
1002
1003 const uint16_t g_idxData[6] = {
1004 0, 1, 2,
1005 3, 4, 5,
1006 };
1007
1008 ASSERT_NO_FATAL_FAILURE(InitState());
1009 ASSERT_NO_FATAL_FAILURE(InitViewport());
1010
1011 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1012 meshBuffer.SetMemoryState(m_cmdBuffer, XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1013
1014 XglIndexBufferObj indexBuffer(m_device);
1015 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
1016 indexBuffer.SetMemoryState(m_cmdBuffer, XGL_MEMORY_STATE_INDEX_DATA);
1017
1018 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1019 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1020
1021 XglPipelineObj pipelineobj(m_device);
1022 pipelineobj.AddShader(&vs);
1023 pipelineobj.AddShader(&ps);
1024
1025 XglDescriptorSetObj descriptorSet(m_device);
1026
1027 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1028 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1029 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1030 };
1031
1032 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1033 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1034 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1035 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1036 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1037 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1038 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1039 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1040 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1041
1042 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1043 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1044
1045 XGL_RESULT err = XGL_SUCCESS;
1046
1047 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1048 // Build command buffer
1049 // TODO: SetMemoryState above also uses this command buffer, do we need to make sure
1050 // that job is complete? Or maybe we shouldn't queue those actions?
1051 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1052 ASSERT_XGL_SUCCESS(err);
1053
1054 GenerateClearAndPrepareBufferCmds();
1055 GenerateBindRenderTargetCmd();
1056#ifdef DUMP_STATE_DOT
1057 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
1058 pDSDumpDot((char*)"triTest.dot");
1059#endif
1060 GenerateBindStateAndPipelineCmds();
1061
Tony Barbour9d3c3172014-12-04 17:27:08 -07001062 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,&descriptorSet);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001063 descriptorSet.BindCommandBuffer(m_cmdBuffer);
1064
1065 meshBuffer.Bind(m_cmdBuffer, 0, 0);
1066 indexBuffer.Bind(m_cmdBuffer, /* offset */ 0);
1067
1068 // render two triangles
1069 xglCmdDrawIndexed(m_cmdBuffer, 0, 6, 0, 0, 1);
1070
1071 // finalize recording of the command buffer
1072 err = xglEndCommandBuffer( m_cmdBuffer );
1073 ASSERT_XGL_SUCCESS( err );
1074
1075 QueueCommandBuffer(NULL, 0);
1076
1077}
1078
GregF6bef1212014-12-02 15:41:44 -07001079TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1080{
1081 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001082
GregF6bef1212014-12-02 15:41:44 -07001083 static const char *vertShaderText =
1084 "#version 140\n"
1085 "#extension GL_ARB_separate_shader_objects : enable\n"
1086 "#extension GL_ARB_shading_language_420pack : enable\n"
1087 "layout (location = 0) in vec4 pos;\n"
1088 "layout (location = 0) out vec4 outColor;\n"
1089 "layout (location = 1) out vec4 outColor2;\n"
1090 "void main() {\n"
1091 " gl_Position = pos;\n"
1092 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1093 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1094 "}\n";
1095
1096 static const char *fragShaderText =
1097 //"#version 140\n"
1098 "#version 330\n"
1099 "#extension GL_ARB_separate_shader_objects : enable\n"
1100 "#extension GL_ARB_shading_language_420pack : enable\n"
1101 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1102 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1103 "layout (location = 0) in vec4 color;\n"
1104 "layout (location = 1) in vec4 color2;\n"
1105 "void main() {\n"
1106 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1107 " float dist_squared = dot(pos, pos);\n"
1108 " gl_FragColor = (dist_squared < 400.0)\n"
1109 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1110 " : color2;\n"
1111 "}\n";
1112
1113 ASSERT_NO_FATAL_FAILURE(InitState());
1114 ASSERT_NO_FATAL_FAILURE(InitViewport());
1115
1116 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1117 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1118
1119 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1120 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1121
1122 XglPipelineObj pipelineobj(m_device);
1123 pipelineobj.AddShader(&vs);
1124 pipelineobj.AddShader(&ps);
1125
1126 XglDescriptorSetObj descriptorSet(m_device);
1127 descriptorSet.AttachMemoryView(&meshBuffer);
1128
1129 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1130 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1131 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1132 };
1133
1134 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1135 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1136 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1137 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1138 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1139 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1140 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1141 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1142 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1143
1144 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1145 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1146 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1147
Tony Barbour09da2212014-12-03 16:13:23 -07001148 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001149 QueueCommandBuffer(NULL, 0);
1150
1151}
1152
1153TEST_F(XglRenderTest, RedCirclesonBlue)
1154{
1155 // This tests that we correctly handle unread fragment inputs
1156
1157 static const char *vertShaderText =
1158 "#version 140\n"
1159 "#extension GL_ARB_separate_shader_objects : enable\n"
1160 "#extension GL_ARB_shading_language_420pack : enable\n"
1161 "layout (location = 0) in vec4 pos;\n"
1162 "layout (location = 0) out vec4 outColor;\n"
1163 "layout (location = 1) out vec4 outColor2;\n"
1164 "void main() {\n"
1165 " gl_Position = pos;\n"
1166 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1167 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1168 "}\n";
1169
1170 static const char *fragShaderText =
1171 //"#version 140\n"
1172 "#version 330\n"
1173 "#extension GL_ARB_separate_shader_objects : enable\n"
1174 "#extension GL_ARB_shading_language_420pack : enable\n"
1175 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1176 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1177 "layout (location = 0) in vec4 color;\n"
1178 "layout (location = 1) in vec4 color2;\n"
1179 "void main() {\n"
1180 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1181 " float dist_squared = dot(pos, pos);\n"
1182 " gl_FragColor = (dist_squared < 400.0)\n"
1183 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1184 " : color2;\n"
1185 "}\n";
1186
1187 ASSERT_NO_FATAL_FAILURE(InitState());
1188 ASSERT_NO_FATAL_FAILURE(InitViewport());
1189
1190 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1191 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1192
1193 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1194 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1195
1196 XglPipelineObj pipelineobj(m_device);
1197 pipelineobj.AddShader(&vs);
1198 pipelineobj.AddShader(&ps);
1199
1200 XglDescriptorSetObj descriptorSet(m_device);
1201 descriptorSet.AttachMemoryView(&meshBuffer);
1202
1203 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1204 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1205 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1206 };
1207
1208 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1209 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1210 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1211 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1212 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1213 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1214 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1215 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1216 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1217
1218 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1219 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1220 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1221
Tony Barbour09da2212014-12-03 16:13:23 -07001222 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001223 QueueCommandBuffer(NULL, 0);
1224
1225}
1226
1227TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1228{
1229 // This tests reading gl_ClipDistance from FS
1230
1231 static const char *vertShaderText =
1232 "#version 330\n"
1233 "#extension GL_ARB_separate_shader_objects : enable\n"
1234 "#extension GL_ARB_shading_language_420pack : enable\n"
1235 "out gl_PerVertex {\n"
1236 " vec4 gl_Position;\n"
1237 " float gl_ClipDistance[1];\n"
1238 "};\n"
1239 "layout (location = 0) in vec4 pos;\n"
1240 "layout (location = 0) out vec4 outColor;\n"
1241 "layout (location = 1) out vec4 outColor2;\n"
1242 "void main() {\n"
1243 " gl_Position = pos;\n"
1244 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1245 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1246 " float dists[3];\n"
1247 " dists[0] = 0.0;\n"
1248 " dists[1] = 1.0;\n"
1249 " dists[2] = 1.0;\n"
1250 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1251 "}\n";
1252
1253
1254 static const char *fragShaderText =
1255 //"#version 140\n"
1256 "#version 330\n"
1257 "#extension GL_ARB_separate_shader_objects : enable\n"
1258 "#extension GL_ARB_shading_language_420pack : enable\n"
1259 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1260 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1261 "layout (location = 0) in vec4 color;\n"
1262 "layout (location = 1) in vec4 color2;\n"
1263 "void main() {\n"
1264 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1265 " float dist_squared = dot(pos, pos);\n"
1266 " gl_FragColor = (dist_squared < 400.0)\n"
1267 " ? color * gl_ClipDistance[0]\n"
1268 " : color2;\n"
1269 "}\n";
1270
1271 ASSERT_NO_FATAL_FAILURE(InitState());
1272 ASSERT_NO_FATAL_FAILURE(InitViewport());
1273
1274 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1275 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1276
1277 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1278 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1279
1280 XglPipelineObj pipelineobj(m_device);
1281 pipelineobj.AddShader(&vs);
1282 pipelineobj.AddShader(&ps);
1283
1284 XglDescriptorSetObj descriptorSet(m_device);
1285 descriptorSet.AttachMemoryView(&meshBuffer);
1286
1287 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1288 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1289 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1290 };
1291
1292 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1293 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1294 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1295 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1296 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1297 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1298 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1299 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1300 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1301
1302 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1303 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1304 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1305
Tony Barboura3953b82014-12-03 15:46:29 -07001306 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001307 QueueCommandBuffer(NULL, 0);
1308
1309}
Tony Barbourf43b6982014-11-25 13:18:32 -07001310
GregF7a23c792014-12-02 17:19:34 -07001311TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1312{
1313 static const char *vertShaderText =
1314 "#version 140\n"
1315 "#extension GL_ARB_separate_shader_objects : enable\n"
1316 "#extension GL_ARB_shading_language_420pack : enable\n"
1317 "layout (location = 0) in vec4 pos;\n"
1318 "layout (location = 0) out vec4 outColor;\n"
1319 "layout (location = 1) out vec4 outColor2;\n"
1320 "void main() {\n"
1321 " gl_Position = pos;\n"
1322 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1323 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1324 "}\n";
1325
1326
1327 static const char *fragShaderText =
1328 //"#version 140\n"
1329 "#version 330\n"
1330 "#extension GL_ARB_separate_shader_objects : enable\n"
1331 "#extension GL_ARB_shading_language_420pack : enable\n"
1332 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1333 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1334 "layout (location = 0) in vec4 color;\n"
1335 "layout (location = 1) in vec4 color2;\n"
1336 "void main() {\n"
1337 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1338 " float dist_squared = dot(pos, pos);\n"
1339 " if (dist_squared < 100.0)\n"
1340 " discard;\n"
1341 " gl_FragColor = (dist_squared < 400.0)\n"
1342 " ? color\n"
1343 " : color2;\n"
1344 "}\n";
1345
1346 ASSERT_NO_FATAL_FAILURE(InitState());
1347 ASSERT_NO_FATAL_FAILURE(InitViewport());
1348
1349 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1350 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1351
1352 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1353 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1354
1355 XglPipelineObj pipelineobj(m_device);
1356 pipelineobj.AddShader(&vs);
1357 pipelineobj.AddShader(&ps);
1358
1359 XglDescriptorSetObj descriptorSet(m_device);
1360 descriptorSet.AttachMemoryView(&meshBuffer);
1361
1362 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1363 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1364 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1365 };
1366
1367 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1368 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1369 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1370 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1371 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1372 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1373 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1374 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1375 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1376
1377 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1378 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1379 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1380
Tony Barbour09da2212014-12-03 16:13:23 -07001381 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF7a23c792014-12-02 17:19:34 -07001382 QueueCommandBuffer(NULL, 0);
1383
1384}
1385
1386
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001387TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001388{
1389 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001390 "#version 140\n"
1391 "#extension GL_ARB_separate_shader_objects : enable\n"
1392 "#extension GL_ARB_shading_language_420pack : enable\n"
1393 "\n"
1394 "layout(binding = 0) uniform buf {\n"
1395 " mat4 MVP;\n"
1396 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001397 "void main() {\n"
1398 " vec2 vertices[3];"
1399 " vertices[0] = vec2(-0.5, -0.5);\n"
1400 " vertices[1] = vec2( 0.5, -0.5);\n"
1401 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001402 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001403 "}\n";
1404
1405 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001406 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001407 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001408 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001409 "}\n";
1410
Tony Barbourf43b6982014-11-25 13:18:32 -07001411 ASSERT_NO_FATAL_FAILURE(InitState());
1412 ASSERT_NO_FATAL_FAILURE(InitViewport());
1413
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001414 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001415 glm::mat4 Projection = glm::mat4(1.0f);
1416 glm::mat4 View = glm::mat4(1.0f);
1417 glm::mat4 Model = glm::mat4(1.0f);
1418 glm::mat4 MVP = Projection * View * Model;
1419 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1420
Tony Barbourf43b6982014-11-25 13:18:32 -07001421 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1422 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1423 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001424
Tony Barbourf43b6982014-11-25 13:18:32 -07001425 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1426
1427 XglPipelineObj pipelineobj(m_device);
1428 pipelineobj.AddShader(&vs);
1429 pipelineobj.AddShader(&ps);
1430
1431 // Create descriptor set and attach the constant buffer to it
1432 XglDescriptorSetObj descriptorSet(m_device);
1433 descriptorSet.AttachMemoryView(&MVPBuffer);
1434
1435 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1436
Tony Barboura3953b82014-12-03 15:46:29 -07001437 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001438 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1439
Tony Barbour09da2212014-12-03 16:13:23 -07001440 RotateTriangleVSUniform(Projection, View, Model, &MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001441}
1442
Tony Barbourf43b6982014-11-25 13:18:32 -07001443TEST_F(XglRenderTest, MixTriangle)
1444{
1445 // This tests location applied to varyings. Notice that we have switched foo
1446 // and bar in the FS. The triangle should be blended with red, green and blue
1447 // corners.
1448 static const char *vertShaderText =
1449 "#version 140\n"
1450 "#extension GL_ARB_separate_shader_objects : enable\n"
1451 "#extension GL_ARB_shading_language_420pack : enable\n"
1452 "layout (location=0) out vec4 bar;\n"
1453 "layout (location=1) out vec4 foo;\n"
1454 "layout (location=2) out float scale;\n"
1455 "vec2 vertices[3];\n"
1456 "void main() {\n"
1457 " vertices[0] = vec2(-1.0, -1.0);\n"
1458 " vertices[1] = vec2( 1.0, -1.0);\n"
1459 " vertices[2] = vec2( 0.0, 1.0);\n"
1460 "vec4 colors[3];\n"
1461 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1462 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1463 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1464 " foo = colors[gl_VertexID % 3];\n"
1465 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1466 " scale = 1.0;\n"
1467 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1468 "}\n";
1469
1470 static const char *fragShaderText =
1471 "#version 140\n"
1472 "#extension GL_ARB_separate_shader_objects : enable\n"
1473 "#extension GL_ARB_shading_language_420pack : enable\n"
1474 "layout (location = 1) in vec4 bar;\n"
1475 "layout (location = 0) in vec4 foo;\n"
1476 "layout (location = 2) in float scale;\n"
1477 "void main() {\n"
1478 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1479 "}\n";
1480
1481 ASSERT_NO_FATAL_FAILURE(InitState());
1482 ASSERT_NO_FATAL_FAILURE(InitViewport());
1483
1484 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1485 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1486
1487 XglPipelineObj pipelineobj(m_device);
1488 pipelineobj.AddShader(&vs);
1489 pipelineobj.AddShader(&ps);
1490
1491 XglDescriptorSetObj descriptorSet(m_device);
1492
Tony Barboura3953b82014-12-03 15:46:29 -07001493 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001494 QueueCommandBuffer(NULL, 0);
1495}
1496
1497TEST_F(XglRenderTest, TriVertFetchAndVertID)
1498{
1499 // This tests that attributes work in the presence of gl_VertexID
1500
1501 static const char *vertShaderText =
1502 "#version 140\n"
1503 "#extension GL_ARB_separate_shader_objects : enable\n"
1504 "#extension GL_ARB_shading_language_420pack : enable\n"
1505 //XYZ1( -1, -1, -1 )
1506 "layout (location = 0) in vec4 pos;\n"
1507 //XYZ1( 0.f, 0.f, 0.f )
1508 "layout (location = 1) in vec4 inColor;\n"
1509 "layout (location = 0) out vec4 outColor;\n"
1510 "void main() {\n"
1511 " outColor = inColor;\n"
1512 " vec4 vertices[3];"
1513 " vertices[gl_VertexID % 3] = pos;\n"
1514 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1515 "}\n";
1516
1517
1518 static const char *fragShaderText =
1519 "#version 140\n"
1520 "#extension GL_ARB_separate_shader_objects : enable\n"
1521 "#extension GL_ARB_shading_language_420pack : enable\n"
1522 "layout (location = 0) in vec4 color;\n"
1523 "void main() {\n"
1524 " gl_FragColor = color;\n"
1525 "}\n";
1526
1527 ASSERT_NO_FATAL_FAILURE(InitState());
1528 ASSERT_NO_FATAL_FAILURE(InitViewport());
1529
1530 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1531 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1532
1533 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1534 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1535
1536 XglPipelineObj pipelineobj(m_device);
1537 pipelineobj.AddShader(&vs);
1538 pipelineobj.AddShader(&ps);
1539
1540 XglDescriptorSetObj descriptorSet(m_device);
1541 descriptorSet.AttachMemoryView(&meshBuffer);
1542
1543 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1544 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1545 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1546 };
1547
1548 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1549 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1550 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1551 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1552 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1553 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1554 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1555 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1556 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1557
1558 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1559 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1560 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1561
Tony Barboura3953b82014-12-03 15:46:29 -07001562 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001563 QueueCommandBuffer(NULL, 0);
1564}
1565
1566TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1567{
1568 // This tests that attributes work in the presence of gl_VertexID
1569 // and a dead attribute in position 0. Draws a triangle with yellow,
1570 // red and green corners, starting at top and going clockwise.
1571
1572 static const char *vertShaderText =
1573 "#version 140\n"
1574 "#extension GL_ARB_separate_shader_objects : enable\n"
1575 "#extension GL_ARB_shading_language_420pack : enable\n"
1576 //XYZ1( -1, -1, -1 )
1577 "layout (location = 0) in vec4 pos;\n"
1578 //XYZ1( 0.f, 0.f, 0.f )
1579 "layout (location = 1) in vec4 inColor;\n"
1580 "layout (location = 0) out vec4 outColor;\n"
1581 "void main() {\n"
1582 " outColor = inColor;\n"
1583 " vec2 vertices[3];"
1584 " vertices[0] = vec2(-1.0, -1.0);\n"
1585 " vertices[1] = vec2( 1.0, -1.0);\n"
1586 " vertices[2] = vec2( 0.0, 1.0);\n"
1587 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1588 "}\n";
1589
1590
1591 static const char *fragShaderText =
1592 "#version 140\n"
1593 "#extension GL_ARB_separate_shader_objects : enable\n"
1594 "#extension GL_ARB_shading_language_420pack : enable\n"
1595 "layout (location = 0) in vec4 color;\n"
1596 "void main() {\n"
1597 " gl_FragColor = color;\n"
1598 "}\n";
1599
1600 ASSERT_NO_FATAL_FAILURE(InitState());
1601 ASSERT_NO_FATAL_FAILURE(InitViewport());
1602
1603 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1604 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1605
1606 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1607 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1608
1609 XglPipelineObj pipelineobj(m_device);
1610 pipelineobj.AddShader(&vs);
1611 pipelineobj.AddShader(&ps);
1612
1613 XglDescriptorSetObj descriptorSet(m_device);
1614 descriptorSet.AttachMemoryView(&meshBuffer);
1615
1616 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1617 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1618 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1619 };
1620
1621 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1622 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1623 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1624 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1625 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1626 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1627 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1628 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1629 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1630
1631 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1632 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1633 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1634
Tony Barbour09da2212014-12-03 16:13:23 -07001635 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001636 QueueCommandBuffer(NULL, 0);
1637
1638}
1639
1640TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001641{
1642 static const char *vertShaderText =
1643 "#version 140\n"
1644 "layout (std140) uniform bufferVals {\n"
1645 " mat4 mvp;\n"
1646 "} myBufferVals;\n"
1647 "in vec4 pos;\n"
1648 "in vec4 inColor;\n"
1649 "out vec4 outColor;\n"
1650 "void main() {\n"
1651 " outColor = inColor;\n"
1652 " gl_Position = myBufferVals.mvp * pos;\n"
1653 "}\n";
1654
1655 static const char *fragShaderText =
1656 "#version 130\n"
1657 "in vec4 color;\n"
1658 "void main() {\n"
1659 " gl_FragColor = color;\n"
1660 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001661 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001662
Tony Barbourf43b6982014-11-25 13:18:32 -07001663 glm::mat4 View = glm::lookAt(
1664 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1665 glm::vec3(0,0,0), // and looks at the origin
1666 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1667 );
1668
1669 glm::mat4 Model = glm::mat4(1.0f);
1670
1671 glm::mat4 MVP = Projection * View * Model;
1672
1673 ASSERT_NO_FATAL_FAILURE(InitState());
1674 ASSERT_NO_FATAL_FAILURE(InitViewport());
1675 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1676
1677 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1678 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1679
1680 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1681
1682 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1683 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1684 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1685
1686 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1687
1688 XglPipelineObj pipelineobj(m_device);
1689 pipelineobj.AddShader(&vs);
1690 pipelineobj.AddShader(&ps);
1691
1692 XglDescriptorSetObj descriptorSet(m_device);
1693 descriptorSet.AttachMemoryView(&MVPBuffer);
1694
1695 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1696 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1697
1698 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1699 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1700 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1701 };
1702
1703 // this is the current description of g_vbData
1704 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1705 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1706 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1707 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1708 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1709 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1710 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1711 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1712 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1713
1714 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1715 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1716 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1717
Tony Barboura3953b82014-12-03 15:46:29 -07001718 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07001719
1720 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1721
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001722}
1723
Tony Barbourf43b6982014-11-25 13:18:32 -07001724TEST_F(XglRenderTest, VSTexture)
1725{
1726 // The expected result from this test is a green and red triangle;
1727 // one red vertex on the left, two green vertices on the right.
1728 static const char *vertShaderText =
1729 "#version 130\n"
1730 "out vec4 texColor;\n"
1731 "uniform sampler2D surface;\n"
1732 "void main() {\n"
1733 " vec2 vertices[3];"
1734 " vertices[0] = vec2(-0.5, -0.5);\n"
1735 " vertices[1] = vec2( 0.5, -0.5);\n"
1736 " vertices[2] = vec2( 0.5, 0.5);\n"
1737 " vec2 positions[3];"
1738 " positions[0] = vec2( 0.0, 0.0);\n"
1739 " positions[1] = vec2( 0.25, 0.1);\n"
1740 " positions[2] = vec2( 0.1, 0.25);\n"
1741 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1742 " texColor = textureLod(surface, samplePos, 0.0);\n"
1743 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1744 "}\n";
1745
1746 static const char *fragShaderText =
1747 "#version 130\n"
1748 "in vec4 texColor;\n"
1749 "void main() {\n"
1750 " gl_FragColor = texColor;\n"
1751 "}\n";
1752
1753 ASSERT_NO_FATAL_FAILURE(InitState());
1754 ASSERT_NO_FATAL_FAILURE(InitViewport());
1755
1756 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1757 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1758 XglSamplerObj sampler(m_device);
1759 XglTextureObj texture(m_device);
1760
1761 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1762 vs.BindShaderEntitySlotToSampler(0, &sampler);
1763
1764 XglPipelineObj pipelineobj(m_device);
1765 pipelineobj.AddShader(&vs);
1766 pipelineobj.AddShader(&ps);
1767
1768 XglDescriptorSetObj descriptorSet(m_device);
1769 descriptorSet.AttachImageView(&texture);
1770 descriptorSet.AttachSampler(&sampler);
1771
1772 m_memoryRefManager.AddMemoryRef(&texture);
1773
Tony Barboura3953b82014-12-03 15:46:29 -07001774 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001775 QueueCommandBuffer(NULL, 0);
1776
1777}
1778TEST_F(XglRenderTest, TexturedTriangle)
1779{
1780 // The expected result from this test is a red and green checkered triangle
1781 static const char *vertShaderText =
1782 "#version 140\n"
1783 "#extension GL_ARB_separate_shader_objects : enable\n"
1784 "#extension GL_ARB_shading_language_420pack : enable\n"
1785 "layout (location = 0) out vec2 samplePos;\n"
1786 "void main() {\n"
1787 " vec2 vertices[3];"
1788 " vertices[0] = vec2(-0.5, -0.5);\n"
1789 " vertices[1] = vec2( 0.5, -0.5);\n"
1790 " vertices[2] = vec2( 0.5, 0.5);\n"
1791 " vec2 positions[3];"
1792 " positions[0] = vec2( 0.0, 0.0);\n"
1793 " positions[1] = vec2( 1.0, 0.0);\n"
1794 " positions[2] = vec2( 1.0, 1.0);\n"
1795 " samplePos = positions[gl_VertexID % 3];\n"
1796 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1797 "}\n";
1798
1799 static const char *fragShaderText =
1800 "#version 140\n"
1801 "#extension GL_ARB_separate_shader_objects : enable\n"
1802 "#extension GL_ARB_shading_language_420pack : enable\n"
1803 "layout (location = 0) in vec2 samplePos;\n"
1804 "layout (binding = 0) uniform sampler2D surface;\n"
1805 "layout (location=0) out vec4 outColor;\n"
1806 "void main() {\n"
1807 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1808 " outColor = texColor;\n"
1809 "}\n";
1810
1811 ASSERT_NO_FATAL_FAILURE(InitState());
1812 ASSERT_NO_FATAL_FAILURE(InitViewport());
1813
1814 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1815 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1816 XglSamplerObj sampler(m_device);
1817 XglTextureObj texture(m_device);
1818
1819 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1820 ps.BindShaderEntitySlotToSampler(0, &sampler);
1821
1822 XglPipelineObj pipelineobj(m_device);
1823 pipelineobj.AddShader(&vs);
1824 pipelineobj.AddShader(&ps);
1825
1826 XglDescriptorSetObj descriptorSet(m_device);
1827 descriptorSet.AttachImageView(&texture);
1828 descriptorSet.AttachSampler(&sampler);
1829
1830 m_memoryRefManager.AddMemoryRef(&texture);
1831
Tony Barboura3953b82014-12-03 15:46:29 -07001832 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001833 QueueCommandBuffer(NULL, 0);
1834}
1835TEST_F(XglRenderTest, TexturedTriangleClip)
1836{
1837 // The expected result from this test is a red and green checkered triangle
1838 static const char *vertShaderText =
1839 "#version 330\n"
1840 "#extension GL_ARB_separate_shader_objects : enable\n"
1841 "#extension GL_ARB_shading_language_420pack : enable\n"
1842 "layout (location = 0) out vec2 samplePos;\n"
1843 "out gl_PerVertex {\n"
1844 " vec4 gl_Position;\n"
1845 " float gl_ClipDistance[1];\n"
1846 "};\n"
1847 "void main() {\n"
1848 " vec2 vertices[3];"
1849 " vertices[0] = vec2(-0.5, -0.5);\n"
1850 " vertices[1] = vec2( 0.5, -0.5);\n"
1851 " vertices[2] = vec2( 0.5, 0.5);\n"
1852 " vec2 positions[3];"
1853 " positions[0] = vec2( 0.0, 0.0);\n"
1854 " positions[1] = vec2( 1.0, 0.0);\n"
1855 " positions[2] = vec2( 1.0, 1.0);\n"
1856 " float dists[3];\n"
1857 " dists[0] = 1.0;\n"
1858 " dists[1] = 1.0;\n"
1859 " dists[2] = -1.0;\n"
1860 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1861 " samplePos = positions[gl_VertexID % 3];\n"
1862 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1863 "}\n";
1864
1865 static const char *fragShaderText =
1866 "#version 140\n"
1867 "#extension GL_ARB_separate_shader_objects : enable\n"
1868 "#extension GL_ARB_shading_language_420pack : enable\n"
1869 "layout (location = 0) in vec2 samplePos;\n"
1870 "layout (binding = 0) uniform sampler2D surface;\n"
1871 "layout (location=0) out vec4 outColor;\n"
1872 "void main() {\n"
1873 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1874 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1875 " outColor = texColor;\n"
1876 "}\n";
1877
1878
1879 ASSERT_NO_FATAL_FAILURE(InitState());
1880 ASSERT_NO_FATAL_FAILURE(InitViewport());
1881
1882 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1883 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1884 XglSamplerObj sampler(m_device);
1885 XglTextureObj texture(m_device);
1886
1887 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1888 ps.BindShaderEntitySlotToSampler(0, &sampler);
1889
1890 XglPipelineObj pipelineobj(m_device);
1891 pipelineobj.AddShader(&vs);
1892 pipelineobj.AddShader(&ps);
1893
1894 XglDescriptorSetObj descriptorSet(m_device);
1895 descriptorSet.AttachImageView(&texture);
1896 descriptorSet.AttachSampler(&sampler);
1897
1898 m_memoryRefManager.AddMemoryRef(&texture);
1899
Tony Barboura3953b82014-12-03 15:46:29 -07001900 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001901 QueueCommandBuffer(NULL, 0);
1902}
1903TEST_F(XglRenderTest, FSTriangle)
1904{
1905 // The expected result from this test is a red and green checkered triangle
1906 static const char *vertShaderText =
1907 "#version 140\n"
1908 "#extension GL_ARB_separate_shader_objects : enable\n"
1909 "#extension GL_ARB_shading_language_420pack : enable\n"
1910 "layout (location = 0) out vec2 samplePos;\n"
1911 "void main() {\n"
1912 " vec2 vertices[3];"
1913 " vertices[0] = vec2(-0.5, -0.5);\n"
1914 " vertices[1] = vec2( 0.5, -0.5);\n"
1915 " vertices[2] = vec2( 0.5, 0.5);\n"
1916 " vec2 positions[3];"
1917 " positions[0] = vec2( 0.0, 0.0);\n"
1918 " positions[1] = vec2( 1.0, 0.0);\n"
1919 " positions[2] = vec2( 1.0, 1.0);\n"
1920 " samplePos = positions[gl_VertexID % 3];\n"
1921 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1922 "}\n";
1923
1924 static const char *fragShaderText =
1925 "#version 140\n"
1926 "#extension GL_ARB_separate_shader_objects : enable\n"
1927 "#extension GL_ARB_shading_language_420pack : enable\n"
1928 "layout (location = 0) in vec2 samplePos;\n"
1929 "layout (binding = 0) uniform sampler2D surface;\n"
1930 "layout (location=0) out vec4 outColor;\n"
1931 "void main() {\n"
1932 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1933 " outColor = texColor;\n"
1934 "}\n";
1935
1936 ASSERT_NO_FATAL_FAILURE(InitState());
1937 ASSERT_NO_FATAL_FAILURE(InitViewport());
1938
1939 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1940 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1941 XglSamplerObj sampler(m_device);
1942 XglTextureObj texture(m_device);
1943
1944 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1945 ps.BindShaderEntitySlotToSampler(0, &sampler);
1946
1947 XglPipelineObj pipelineobj(m_device);
1948 pipelineobj.AddShader(&vs);
1949 pipelineobj.AddShader(&ps);
1950
1951 XglDescriptorSetObj descriptorSet(m_device);
1952 descriptorSet.AttachImageView(&texture);
1953 descriptorSet.AttachSampler(&sampler);
1954
1955 m_memoryRefManager.AddMemoryRef(&texture);
1956
Tony Barboura3953b82014-12-03 15:46:29 -07001957 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001958 QueueCommandBuffer(NULL, 0);
1959}
1960TEST_F(XglRenderTest, SamplerBindingsTriangle)
1961{
1962 // This test sets bindings on the samplers
1963 // For now we are asserting that sampler and texture pairs
1964 // march in lock step, and are set via GLSL binding. This can
1965 // and will probably change.
1966 // The sampler bindings should match the sampler and texture slot
1967 // number set up by the application.
1968 // This test will result in a blue triangle
1969 static const char *vertShaderText =
1970 "#version 140\n"
1971 "#extension GL_ARB_separate_shader_objects : enable\n"
1972 "#extension GL_ARB_shading_language_420pack : enable\n"
1973 "layout (location = 0) out vec4 samplePos;\n"
1974 "void main() {\n"
1975 " vec2 vertices[3];"
1976 " vertices[0] = vec2(-0.5, -0.5);\n"
1977 " vertices[1] = vec2( 0.5, -0.5);\n"
1978 " vertices[2] = vec2( 0.5, 0.5);\n"
1979 " vec2 positions[3];"
1980 " positions[0] = vec2( 0.0, 0.0);\n"
1981 " positions[1] = vec2( 1.0, 0.0);\n"
1982 " positions[2] = vec2( 1.0, 1.0);\n"
1983 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
1984 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1985 "}\n";
1986
1987 static const char *fragShaderText =
1988 "#version 140\n"
1989 "#extension GL_ARB_separate_shader_objects : enable\n"
1990 "#extension GL_ARB_shading_language_420pack : enable\n"
1991 "layout (location = 0) in vec4 samplePos;\n"
1992 "layout (binding = 0) uniform sampler2D surface0;\n"
1993 "layout (binding = 1) uniform sampler2D surface1;\n"
1994 "layout (binding = 12) uniform sampler2D surface2;\n"
1995 "void main() {\n"
1996 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
1997 "}\n";
1998
1999 ASSERT_NO_FATAL_FAILURE(InitState());
2000 ASSERT_NO_FATAL_FAILURE(InitViewport());
2001
2002 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2003 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2004
2005 XglSamplerObj sampler1(m_device);
2006 XglSamplerObj sampler2(m_device);
2007 XglSamplerObj sampler3(m_device);
2008
2009 XglTextureObj texture1(m_device); // Red
2010 texture1.ChangeColors(0xffff0000,0xffff0000);
2011 XglTextureObj texture2(m_device); // Green
2012 texture2.ChangeColors(0xff00ff00,0xff00ff00);
2013 XglTextureObj texture3(m_device); // Blue
2014 texture3.ChangeColors(0xff0000ff,0xff0000ff);
2015
2016 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture1);
2017 ps.BindShaderEntitySlotToSampler(0, &sampler1);
2018
2019 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_RESOURCE, &texture2);
2020 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2021
2022 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_RESOURCE, &texture3);
2023 ps.BindShaderEntitySlotToSampler(12, &sampler3);
2024
2025 XglPipelineObj pipelineobj(m_device);
2026 pipelineobj.AddShader(&vs);
2027 pipelineobj.AddShader(&ps);
2028
2029 XglDescriptorSetObj descriptorSet(m_device);
2030 descriptorSet.AttachImageView(&texture1);
2031 descriptorSet.AttachSampler(&sampler1);
2032 descriptorSet.AttachImageView(&texture2);
2033 descriptorSet.AttachSampler(&sampler2);
2034 descriptorSet.AttachImageView(&texture3);
2035 descriptorSet.AttachSampler(&sampler3);
2036
2037 m_memoryRefManager.AddMemoryRef(&texture1);
2038 m_memoryRefManager.AddMemoryRef(&texture2);
2039 m_memoryRefManager.AddMemoryRef(&texture3);
2040
Tony Barboura3953b82014-12-03 15:46:29 -07002041 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002042 QueueCommandBuffer(NULL, 0);
2043
2044}
2045
2046TEST_F(XglRenderTest, TriangleVSUniformBlock)
2047{
2048 // The expected result from this test is a blue triangle
2049
2050 static const char *vertShaderText =
2051 "#version 140\n"
2052 "#extension GL_ARB_separate_shader_objects : enable\n"
2053 "#extension GL_ARB_shading_language_420pack : enable\n"
2054 "layout (location = 0) out vec4 outColor;\n"
2055 "layout (std140, binding = 0) uniform bufferVals {\n"
2056 " vec4 red;\n"
2057 " vec4 green;\n"
2058 " vec4 blue;\n"
2059 " vec4 white;\n"
2060 "} myBufferVals;\n"
2061 "void main() {\n"
2062 " vec2 vertices[3];"
2063 " vertices[0] = vec2(-0.5, -0.5);\n"
2064 " vertices[1] = vec2( 0.5, -0.5);\n"
2065 " vertices[2] = vec2( 0.5, 0.5);\n"
2066 " outColor = myBufferVals.blue;\n"
2067 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2068 "}\n";
2069
2070 static const char *fragShaderText =
2071 "#version 140\n"
2072 "#extension GL_ARB_separate_shader_objects : enable\n"
2073 "#extension GL_ARB_shading_language_420pack : enable\n"
2074 "layout (location = 0) in vec4 inColor;\n"
2075 "void main() {\n"
2076 " gl_FragColor = inColor;\n"
2077 "}\n";
2078
2079 ASSERT_NO_FATAL_FAILURE(InitState());
2080 ASSERT_NO_FATAL_FAILURE(InitViewport());
2081
2082 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2083 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2084
2085 // Let's populate our buffer with the following:
2086 // vec4 red;
2087 // vec4 green;
2088 // vec4 blue;
2089 // vec4 white;
2090 const int valCount = 4 * 4;
2091 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2092 0.0, 1.0, 0.0, 1.0,
2093 0.0, 0.0, 1.0, 1.0,
2094 1.0, 1.0, 1.0, 1.0 };
2095
2096 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2097 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2098
2099 XglPipelineObj pipelineobj(m_device);
2100 pipelineobj.AddShader(&vs);
2101 pipelineobj.AddShader(&ps);
2102
2103 XglDescriptorSetObj descriptorSet(m_device);
2104 descriptorSet.AttachMemoryView(&colorBuffer);
2105
Tony Barboura3953b82014-12-03 15:46:29 -07002106 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002107 QueueCommandBuffer(NULL, 0);
2108
2109}
2110
2111TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2112{
2113 // This test allows the shader to select which buffer it is
2114 // pulling from using layout binding qualifier.
2115 // There are corresponding changes in the compiler stack that
2116 // will select the buffer using binding directly.
2117 // The binding number should match the slot number set up by
2118 // the application.
2119 // The expected result from this test is a purple triangle
2120
2121 static const char *vertShaderText =
2122 "#version 140\n"
2123 "#extension GL_ARB_separate_shader_objects : enable\n"
2124 "#extension GL_ARB_shading_language_420pack : enable\n"
2125 "void main() {\n"
2126 " vec2 vertices[3];"
2127 " vertices[0] = vec2(-0.5, -0.5);\n"
2128 " vertices[1] = vec2( 0.5, -0.5);\n"
2129 " vertices[2] = vec2( 0.5, 0.5);\n"
2130 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2131 "}\n";
2132
2133 static const char *fragShaderText =
2134 "#version 140\n"
2135 "#extension GL_ARB_separate_shader_objects : enable\n"
2136 "#extension GL_ARB_shading_language_420pack : enable\n"
2137 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2138 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2139 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2140 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2141 "void main() {\n"
2142 " gl_FragColor = myBlueVal.color;\n"
2143 " gl_FragColor += myRedVal.color;\n"
2144 "}\n";
2145
2146 ASSERT_NO_FATAL_FAILURE(InitState());
2147 ASSERT_NO_FATAL_FAILURE(InitViewport());
2148
2149 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2150 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2151
2152 // We're going to create a number of uniform buffers, and then allow
2153 // the shader to select which it wants to read from with a binding
2154
2155 // Let's populate the buffers with a single color each:
2156 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2157 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2158 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2159 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2160
2161 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2162 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2163 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2164 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2165
2166 const int redCount = sizeof(redVals) / sizeof(float);
2167 const int greenCount = sizeof(greenVals) / sizeof(float);
2168 const int blueCount = sizeof(blueVals) / sizeof(float);
2169 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2170
2171 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2172 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2173
2174 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2175 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2176
2177 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2178 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2179
2180 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2181 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2182
2183 XglPipelineObj pipelineobj(m_device);
2184 pipelineobj.AddShader(&vs);
2185 pipelineobj.AddShader(&ps);
2186
2187 XglDescriptorSetObj descriptorSet(m_device);
2188 descriptorSet.AttachMemoryView(&redBuffer);
2189 descriptorSet.AttachMemoryView(&greenBuffer);
2190 descriptorSet.AttachMemoryView(&blueBuffer);
2191 descriptorSet.AttachMemoryView(&whiteBuffer);
2192
Tony Barboura3953b82014-12-03 15:46:29 -07002193 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002194 QueueCommandBuffer(NULL, 0);
2195
2196}
2197
2198TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2199{
2200 // This test is the same as TriangleFSUniformBlockBinding, but
2201 // it does not provide an instance name.
2202 // The expected result from this test is a purple triangle
2203
2204 static const char *vertShaderText =
2205 "#version 140\n"
2206 "#extension GL_ARB_separate_shader_objects : enable\n"
2207 "#extension GL_ARB_shading_language_420pack : enable\n"
2208 "void main() {\n"
2209 " vec2 vertices[3];"
2210 " vertices[0] = vec2(-0.5, -0.5);\n"
2211 " vertices[1] = vec2( 0.5, -0.5);\n"
2212 " vertices[2] = vec2( 0.5, 0.5);\n"
2213 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2214 "}\n";
2215
2216 static const char *fragShaderText =
2217 "#version 430\n"
2218 "#extension GL_ARB_separate_shader_objects : enable\n"
2219 "#extension GL_ARB_shading_language_420pack : enable\n"
2220 "layout (location = 0) uniform vec4 foo;\n"
2221 "layout (location = 1) uniform vec4 bar;\n"
2222 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2223 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2224 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2225 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
Cody Northrop68a10f62014-12-05 15:44:14 -07002226 "layout (location = 0) out vec4 outColor;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002227 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -07002228 " outColor = blue;\n"
2229 " outColor += red;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002230 "}\n";
2231 ASSERT_NO_FATAL_FAILURE(InitState());
2232 ASSERT_NO_FATAL_FAILURE(InitViewport());
2233
2234 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2235 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2236
2237 // We're going to create a number of uniform buffers, and then allow
2238 // the shader to select which it wants to read from with a binding
2239
2240 // Let's populate the buffers with a single color each:
2241 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2242 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2243 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2244 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2245
2246 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2247 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2248 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2249 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2250
2251 const int redCount = sizeof(redVals) / sizeof(float);
2252 const int greenCount = sizeof(greenVals) / sizeof(float);
2253 const int blueCount = sizeof(blueVals) / sizeof(float);
2254 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2255
2256 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2257 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2258
2259 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2260 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2261
2262 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2263 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2264
2265 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2266 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2267
2268 XglPipelineObj pipelineobj(m_device);
2269 pipelineobj.AddShader(&vs);
2270 pipelineobj.AddShader(&ps);
2271
2272 XglDescriptorSetObj descriptorSet(m_device);
2273 descriptorSet.AttachMemoryView(&redBuffer);
2274 descriptorSet.AttachMemoryView(&greenBuffer);
2275 descriptorSet.AttachMemoryView(&blueBuffer);
2276 descriptorSet.AttachMemoryView(&whiteBuffer);
2277
Tony Barboura3953b82014-12-03 15:46:29 -07002278 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002279 QueueCommandBuffer(NULL, 0);
2280
2281}
2282
2283TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2284{
2285 static const char *vertShaderText =
2286 "#version 140\n"
2287 "#extension GL_ARB_separate_shader_objects : enable\n"
2288 "#extension GL_ARB_shading_language_420pack : enable\n"
2289 "layout (std140, binding=0) uniform bufferVals {\n"
2290 " mat4 mvp;\n"
2291 "} myBufferVals;\n"
2292 "layout (location=0) in vec4 pos;\n"
2293 "layout (location=0) out vec2 UV;\n"
2294 "void main() {\n"
2295 " vec2 positions[3];"
2296 " positions[0] = vec2( 0.0, 0.0);\n"
2297 " positions[1] = vec2( 0.25, 0.1);\n"
2298 " positions[2] = vec2( 0.1, 0.25);\n"
2299 " UV = positions[gl_VertexID % 3];\n"
2300 " gl_Position = myBufferVals.mvp * pos;\n"
2301 "}\n";
2302
2303 static const char *fragShaderText =
2304 "#version 140\n"
2305 "#extension GL_ARB_separate_shader_objects : enable\n"
2306 "#extension GL_ARB_shading_language_420pack : enable\n"
2307 "layout (binding=0) uniform sampler2D surface;\n"
2308 "layout (location=0) out vec4 outColor;\n"
2309 "layout (location=0) in vec2 UV;\n"
2310 "void main() {\n"
2311 " outColor= textureLod(surface, UV, 0.0);\n"
2312 "}\n";
2313 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2314
2315 glm::mat4 View = glm::lookAt(
2316 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2317 glm::vec3(0,0,0), // and looks at the origin
2318 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2319 );
2320
2321 glm::mat4 Model = glm::mat4(1.0f);
2322
2323 glm::mat4 MVP = Projection * View * Model;
2324
2325
2326 ASSERT_NO_FATAL_FAILURE(InitState());
2327 ASSERT_NO_FATAL_FAILURE(InitViewport());
2328 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2329
2330 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2331 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
2332 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
2333
2334
2335 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2336
2337 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2338 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2339 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2340 XglSamplerObj sampler(m_device);
2341 XglTextureObj texture(m_device);
2342
2343 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2344 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
2345 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
2346 ps.BindShaderEntitySlotToSampler(0, &sampler);
2347
2348 XglPipelineObj pipelineobj(m_device);
2349 pipelineobj.AddShader(&vs);
2350 pipelineobj.AddShader(&ps);
2351
2352 XglDescriptorSetObj descriptorSet(m_device);
2353
2354 descriptorSet.AttachMemoryView(&mvpBuffer);
2355 descriptorSet.AttachImageView(&texture);
2356 descriptorSet.AttachSampler(&sampler);
2357
2358 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2359 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2360 m_memoryRefManager.AddMemoryRef(&texture);
2361
2362 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2363 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2364 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2365 };
2366
2367 // this is the current description of g_vbData
2368 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2369 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2370 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2371 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2372 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2373 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2374 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2375 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2376 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2377
2378 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2379 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2380 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2381
Tony Barboura3953b82014-12-03 15:46:29 -07002382 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07002383
2384 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2385
2386}
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002387int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002388 int result;
2389
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002390 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002391 XglTestFramework::InitArgs(&argc, argv);
2392
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002393 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
2394
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002395 result = RUN_ALL_TESTS();
2396
2397 XglTestFramework::Finish();
2398 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002399}