blob: cc966987e1a414e58287b4956c17115f0215d6df [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{
Chia-I Wu7133fdc2014-12-15 23:57:34 +0800409 int i;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600410
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) {
Tony Barbour97a36232014-12-04 17:14:45 -0700432 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
433 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
434 dsRange.baseMipLevel = 0;
435 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
436 dsRange.baseArraySlice = 0;
437 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
Cody Northrop7a1f0462014-10-10 14:49:36 -0600438
Tony Barbour97a36232014-12-04 17:14:45 -0700439 // prepare the depth buffer for clear
440 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
441 transitionToClear.image = m_depthStencilImage;
442 transitionToClear.oldState = m_depthStencilBinding.depthState;
443 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
444 transitionToClear.subresourceRange = dsRange;
445 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
446 m_depthStencilBinding.depthState = transitionToClear.newState;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600447
Tony Barbour97a36232014-12-04 17:14:45 -0700448 xglCmdClearDepthStencil(m_cmdBuffer, m_depthStencilImage, 1.0f, 0, 1, &dsRange);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600449
Tony Barbour97a36232014-12-04 17:14:45 -0700450 // prepare depth buffer for rendering
451 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
Chia-I Wu18749b02014-12-05 10:48:20 +0800452 transitionToRender.image = m_depthStencilImage;
Tony Barbour97a36232014-12-04 17:14:45 -0700453 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
454 transitionToRender.newState = m_depthStencilBinding.depthState;
455 transitionToRender.subresourceRange = dsRange;
456 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
457 m_depthStencilBinding.depthState = transitionToClear.newState;
458 }
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600459}
460
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600461void XglRenderTest::InitDepthStencil()
462{
463 XGL_RESULT err;
464 XGL_IMAGE_CREATE_INFO image;
465 XGL_MEMORY_ALLOC_INFO mem_alloc;
466 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
467 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn6317c492014-11-21 11:33:20 -0700468 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600469
470 // Clean up default state created by framework
471 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
472
473 m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16;
474 m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS;
475
476 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
477 image.pNext = NULL;
478 image.imageType = XGL_IMAGE_2D;
479 image.format = m_depth_stencil_fmt;
480 image.extent.width = m_width;
481 image.extent.height = m_height;
482 image.extent.depth = 1;
483 image.mipLevels = 1;
484 image.arraySize = 1;
485 image.samples = 1;
486 image.tiling = XGL_OPTIMAL_TILING;
487 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
488 image.flags = 0;
489
490 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
491 mem_alloc.pNext = NULL;
492 mem_alloc.allocationSize = 0;
493 mem_alloc.alignment = 0;
494 mem_alloc.flags = 0;
495 mem_alloc.heapCount = 0;
496 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
497
498 /* create image */
499 err = xglCreateImage(device(), &image,
500 &m_depthStencilImage);
501 ASSERT_XGL_SUCCESS(err);
502
503 err = xglGetObjectInfo(m_depthStencilImage,
504 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
505 &mem_reqs_size, &mem_reqs);
506 ASSERT_XGL_SUCCESS(err);
507 ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs));
508
509 mem_alloc.allocationSize = mem_reqs.size;
510 mem_alloc.alignment = mem_reqs.alignment;
511 mem_alloc.heapCount = mem_reqs.heapCount;
512 memcpy(mem_alloc.heaps, mem_reqs.heaps,
513 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
514
515 /* allocate memory */
516 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem);
517 ASSERT_XGL_SUCCESS(err);
518
519 /* bind memory */
520 err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0);
521 ASSERT_XGL_SUCCESS(err);
522
523 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
524 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
525 depthStencil.depthTestEnable = XGL_TRUE;
526 depthStencil.depthWriteEnable = XGL_TRUE;
527 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
528 depthStencil.depthBoundsEnable = XGL_FALSE;
529 depthStencil.minDepth = 0.f;
530 depthStencil.maxDepth = 1.f;
531 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
532 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
533 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
534 depthStencil.back.stencilRef = 0x00;
535 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
536 depthStencil.front = depthStencil.back;
537
538 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
539 ASSERT_XGL_SUCCESS( err );
540
541 /* create image view */
542 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
543 view.pNext = NULL;
544 view.image = XGL_NULL_HANDLE;
545 view.mipLevel = 0;
546 view.baseArraySlice = 0;
547 view.arraySize = 1;
548 view.flags = 0;
549 view.image = m_depthStencilImage;
550 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
551 ASSERT_XGL_SUCCESS(err);
552
553 m_depthStencilBinding.view = m_depthStencilView;
554 m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
555 m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
556}
557
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600558struct xgltriangle_vs_uniform {
559 // Must start with MVP
560 XGL_FLOAT mvp[4][4];
561 XGL_FLOAT position[3][4];
562 XGL_FLOAT color[3][4];
563};
564
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600565void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600566{
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700567#ifdef DEBUG_CALLBACK
568 xglDbgRegisterMsgCallback(myDbgFunc, NULL);
569#endif
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600570 // Create identity matrix
571 int i;
572 struct xgltriangle_vs_uniform data;
573
574 glm::mat4 Projection = glm::mat4(1.0f);
575 glm::mat4 View = glm::mat4(1.0f);
576 glm::mat4 Model = glm::mat4(1.0f);
577 glm::mat4 MVP = Projection * View * Model;
578 const int matrixSize = sizeof(MVP);
579 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT);
580 memcpy(&data.mvp, &MVP[0][0], matrixSize);
581
582 static const Vertex tri_data[] =
583 {
584 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
585 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
586 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
587 };
588
589 for (i=0; i<3; i++) {
590 data.position[i][0] = tri_data[i].posX;
591 data.position[i][1] = tri_data[i].posY;
592 data.position[i][2] = tri_data[i].posZ;
593 data.position[i][3] = tri_data[i].posW;
594 data.color[i][0] = tri_data[i].r;
595 data.color[i][1] = tri_data[i].g;
596 data.color[i][2] = tri_data[i].b;
597 data.color[i][3] = tri_data[i].a;
598 }
599
Tony Barbourf43b6982014-11-25 13:18:32 -0700600 ASSERT_NO_FATAL_FAILURE(InitState());
601 ASSERT_NO_FATAL_FAILURE(InitViewport());
602
603 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data);
604
605 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
606 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
607 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
608
609 XglPipelineObj pipelineobj(m_device);
610 pipelineobj.AddShader(&vs);
611 pipelineobj.AddShader(&ps);
612
613 XglDescriptorSetObj descriptorSet(m_device);
614 descriptorSet.AttachMemoryView(&constantBuffer);
615 m_memoryRefManager.AddMemoryRef(&constantBuffer);
616
Tony Barboura3953b82014-12-03 15:46:29 -0700617 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -0700618 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
619
Tony Barbour09da2212014-12-03 16:13:23 -0700620 RotateTriangleVSUniform(Projection, View, Model, &constantBuffer);
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700621#ifdef PRINT_OBJECTS
622 //XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)
623 OBJ_TRACK_GET_OBJECT_COUNT pObjTrackGetObjectCount = (OBJ_TRACK_GET_OBJECT_COUNT)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjectCount");
624 XGL_UINT64 numObjects = pObjTrackGetObjectCount(XGL_OBJECT_TYPE_ANY);
625 //OBJ_TRACK_GET_OBJECTS pGetObjsFunc = xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
626 printf("DEBUG : Number of Objects : %lu\n", numObjects);
627 OBJ_TRACK_GET_OBJECTS pObjTrackGetObjs = (OBJ_TRACK_GET_OBJECTS)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
628 OBJTRACK_NODE* pObjNodeArray = (OBJTRACK_NODE*)malloc(sizeof(OBJTRACK_NODE)*numObjects);
629 pObjTrackGetObjs(XGL_OBJECT_TYPE_ANY, numObjects, pObjNodeArray);
630 for (i=0; i < numObjects; i++) {
631 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);
632 }
633 free(pObjNodeArray);
634#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700635
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600636}
637
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600638TEST_F(XglRenderTest, XGLTriangle_FragColor)
639{
640 static const char *vertShaderText =
641 "#version 140\n"
642 "#extension GL_ARB_separate_shader_objects : enable\n"
643 "#extension GL_ARB_shading_language_420pack : enable\n"
644 "\n"
645 "layout(binding = 0) uniform buf {\n"
646 " mat4 MVP;\n"
647 " vec4 position[3];\n"
648 " vec4 color[3];\n"
649 "} ubuf;\n"
650 "\n"
651 "layout (location = 0) out vec4 outColor;\n"
652 "\n"
653 "void main() \n"
654 "{\n"
655 " outColor = ubuf.color[gl_VertexID];\n"
656 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
657 "}\n";
658
659 static const char *fragShaderText =
660 "#version 140\n"
661 "#extension GL_ARB_separate_shader_objects : enable\n"
662 "#extension GL_ARB_shading_language_420pack : enable\n"
663 "\n"
664 "layout (location = 0) in vec4 inColor;\n"
665 "\n"
666 "void main()\n"
667 "{\n"
668 " gl_FragColor = inColor;\n"
669 "}\n";
670
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600671 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600672 XGLTriangleTest(vertShaderText, fragShaderText);
673}
674
675TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
676{
677 static const char *vertShaderText =
678 "#version 140\n"
679 "#extension GL_ARB_separate_shader_objects : enable\n"
680 "#extension GL_ARB_shading_language_420pack : enable\n"
681 "\n"
682 "layout(binding = 0) uniform buf {\n"
683 " mat4 MVP;\n"
684 " vec4 position[3];\n"
685 " vec4 color[3];\n"
686 "} ubuf;\n"
687 "\n"
688 "layout (location = 0) out vec4 outColor;\n"
689 "\n"
690 "void main() \n"
691 "{\n"
692 " outColor = ubuf.color[gl_VertexID];\n"
693 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
694 "}\n";
695
696 static const char *fragShaderText =
697 "#version 140\n"
698 "#extension GL_ARB_separate_shader_objects : enable\n"
699 "#extension GL_ARB_shading_language_420pack : enable\n"
700 "\n"
701 "layout (location = 0) in vec4 inColor;\n"
702 "layout (location = 0) out vec4 outColor;\n"
703 "\n"
704 "void main()\n"
705 "{\n"
706 " outColor = inColor;\n"
707 "}\n";
708
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600709 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 -0600710
711 XGLTriangleTest(vertShaderText, fragShaderText);
712}
713
Tony Barbourf43b6982014-11-25 13:18:32 -0700714TEST_F(XglRenderTest, BIL_XGLTriangle)
715{
716 bool saved_use_bil = XglTestFramework::m_use_bil;
717
718 static const char *vertShaderText =
719 "#version 140\n"
720 "#extension GL_ARB_separate_shader_objects : enable\n"
721 "#extension GL_ARB_shading_language_420pack : enable\n"
722 "\n"
723 "layout(binding = 0) uniform buf {\n"
724 " mat4 MVP;\n"
725 " vec4 position[3];\n"
726 " vec4 color[3];\n"
727 "} ubuf;\n"
728 "\n"
729 "layout (location = 0) out vec4 outColor;\n"
730 "\n"
731 "void main() \n"
732 "{\n"
733 " outColor = ubuf.color[gl_VertexID];\n"
734 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
735 "}\n";
736
737 static const char *fragShaderText =
738 "#version 140\n"
739 "#extension GL_ARB_separate_shader_objects : enable\n"
740 "#extension GL_ARB_shading_language_420pack : enable\n"
741 "\n"
742 "layout (location = 0) in vec4 inColor;\n"
743 "\n"
744 "void main()\n"
745 "{\n"
746 " gl_FragColor = inColor;\n"
747 "}\n";
748
749 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver.");
750
751 XglTestFramework::m_use_bil = true;
752
753 XGLTriangleTest(vertShaderText, fragShaderText);
754
755 XglTestFramework::m_use_bil = saved_use_bil;
756}
757
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600758TEST_F(XglRenderTest, GreenTriangle)
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600759{
760 static const char *vertShaderText =
761 "#version 130\n"
762 "vec2 vertices[3];\n"
763 "void main() {\n"
764 " vertices[0] = vec2(-1.0, -1.0);\n"
765 " vertices[1] = vec2( 1.0, -1.0);\n"
766 " vertices[2] = vec2( 0.0, 1.0);\n"
767 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
768 "}\n";
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600769
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600770 static const char *fragShaderText =
771 "#version 130\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600772 "void main() {\n"
Steve K10b32512014-10-10 08:54:29 -0600773 " gl_FragColor = vec4(0,1,0,1);\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600774 "}\n";
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600775
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600776 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
777
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600778 DrawTriangleTest(vertShaderText, fragShaderText);
779}
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600780
Tony Barbourf43b6982014-11-25 13:18:32 -0700781TEST_F(XglRenderTest, BIL_GreenTriangle)
782{
783 bool saved_use_bil = XglTestFramework::m_use_bil;
784
785 static const char *vertShaderText =
786 "#version 130\n"
787 "vec2 vertices[3];\n"
788 "void main() {\n"
789 " vertices[0] = vec2(-1.0, -1.0);\n"
790 " vertices[1] = vec2( 1.0, -1.0);\n"
791 " vertices[2] = vec2( 0.0, 1.0);\n"
792 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
793 "}\n";
794
795 static const char *fragShaderText =
796 "#version 130\n"
797 "void main() {\n"
798 " gl_FragColor = vec4(0,1,0,1);\n"
799 "}\n";
800
801 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver.");
802
803 XglTestFramework::m_use_bil = true;
804 DrawTriangleTest(vertShaderText, fragShaderText);
805 XglTestFramework::m_use_bil = saved_use_bil;
806}
807
808TEST_F(XglRenderTest, YellowTriangle)
809{
810 static const char *vertShaderText =
811 "#version 130\n"
812 "void main() {\n"
813 " vec2 vertices[3];"
814 " vertices[0] = vec2(-0.5, -0.5);\n"
815 " vertices[1] = vec2( 0.5, -0.5);\n"
816 " vertices[2] = vec2( 0.5, 0.5);\n"
817 " vec4 colors[3];\n"
818 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
819 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
820 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
821 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
822 "}\n";
823
824 static const char *fragShaderText =
825 "#version 130\n"
826 "void main() {\n"
827 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
828 "}\n";
829
830 DrawTriangleTest(vertShaderText, fragShaderText);
831}
832
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600833TEST_F(XglRenderTest, TriangleWithVertexFetch)
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600834{
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600835 static const char *vertShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700836 "#version 130\n"
837 //XYZ1( -1, -1, -1 )
838 "in vec4 pos;\n"
839 //XYZ1( 0.f, 0.f, 0.f )
840 "in vec4 inColor;\n"
841 "out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600842 "void main() {\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -0600843 " outColor = inColor;\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600844 " gl_Position = pos;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600845 "}\n";
846
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600847
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600848 static const char *fragShaderText =
Cody Northrop68a10f62014-12-05 15:44:14 -0700849 "#version 140\n"
850 "#extension GL_ARB_separate_shader_objects : enable\n"
851 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -0700852 "in vec4 color;\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700853 "layout (location = 0) out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600854 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700855 " outColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600856 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600857
Tony Barbourf43b6982014-11-25 13:18:32 -0700858
859
860 ASSERT_NO_FATAL_FAILURE(InitState());
861 ASSERT_NO_FATAL_FAILURE(InitViewport());
862
863 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -0700864 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -0700865
866 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
867 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
868
869 XglPipelineObj pipelineobj(m_device);
870 pipelineobj.AddShader(&vs);
871 pipelineobj.AddShader(&ps);
872
873 XglDescriptorSetObj descriptorSet(m_device);
874 descriptorSet.AttachMemoryView(&meshBuffer);
875
876 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
877 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
878 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
879 };
880
881 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
882 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
883 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
884 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
885 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
886 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
887 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
888 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
889 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
890
891 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
892 pipelineobj.AddVertexInputBindings(&vi_binding,1);
893 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
894
Tony Barboura3953b82014-12-03 15:46:29 -0700895 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -0700896 QueueCommandBuffer(NULL, 0);
897
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600898}
899
Chia-I Wue09d1a72014-12-05 10:32:23 +0800900TEST_F(XglRenderTest, TriangleMRT)
901{
902 static const char *vertShaderText =
903 "#version 130\n"
904 "in vec4 pos;\n"
905 "void main() {\n"
906 " gl_Position = pos;\n"
907 "}\n";
908
909 static const char *fragShaderText =
910 "#version 130\n"
911 "void main() {\n"
912 " gl_FragData[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
913 " gl_FragData[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
914 "}\n";
915 const XGL_FLOAT vb_data[][2] = {
916 { -1.0f, -1.0f },
917 { 1.0f, -1.0f },
918 { -1.0f, 1.0f }
919 };
920
921 ASSERT_NO_FATAL_FAILURE(InitState());
922 ASSERT_NO_FATAL_FAILURE(InitViewport());
923
924 XglConstantBufferObj meshBuffer(m_device, sizeof(vb_data) / sizeof(vb_data[0]), sizeof(vb_data[0]), vb_data);
Tony Barboure6152042014-12-10 17:40:15 -0700925 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Chia-I Wue09d1a72014-12-05 10:32:23 +0800926
927 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
928 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
929
930 XglPipelineObj pipelineobj(m_device);
931 pipelineobj.AddShader(&vs);
932 pipelineobj.AddShader(&ps);
933
934 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
935 sizeof(vb_data[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
936 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
937 };
938
939 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attrib;
940 vi_attrib.binding = 0; // index into vertexBindingDescriptions
941 vi_attrib.format.channelFormat = XGL_CH_FMT_R32G32; // format of source data
942 vi_attrib.format.numericFormat = XGL_NUM_FMT_FLOAT;
943 vi_attrib.offsetInBytes = 0; // Offset of first element in bytes from base of vertex
944
945 pipelineobj.AddVertexInputAttribs(&vi_attrib, 1);
946 pipelineobj.AddVertexInputBindings(&vi_binding,1);
947 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
948
949 XglDescriptorSetObj descriptorSet(m_device);
950
951 m_renderTargetCount = 2;
952
953 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
954 att.blendEnable = XGL_FALSE;
955 att.format = m_render_target_fmt;
956 att.channelWriteMask = 0xf;
957 pipelineobj.SetColorAttachment(1, &att);
958
959 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
960 QueueCommandBuffer(NULL, 0);
961}
962
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700963TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
964{
965 static const char *vertShaderText =
966 "#version 140\n"
967 "#extension GL_ARB_separate_shader_objects : enable\n"
968 "#extension GL_ARB_shading_language_420pack : enable\n"
969 "layout(location = 0) in vec4 pos;\n"
970 "layout(location = 1) in vec4 inColor;\n"
971 "layout(location = 0) out vec4 outColor;\n"
972 "void main() {\n"
973 " outColor = inColor;\n"
974 " gl_Position = pos;\n"
975 "}\n";
976
977
978 static const char *fragShaderText =
979 "#version 140\n"
980 "#extension GL_ARB_separate_shader_objects : enable\n"
981 "#extension GL_ARB_shading_language_420pack : enable\n"
982 "layout(location = 0) in vec4 color;\n"
983 "void main() {\n"
984 " gl_FragColor = color;\n"
985 "}\n";
986
987 const Vertex g_vbData[] =
988 {
989 // first tri
990 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
991 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
992 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
993
994 // second tri
995 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
996 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
997 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
998 };
999
1000 const uint16_t g_idxData[6] = {
1001 0, 1, 2,
1002 3, 4, 5,
1003 };
1004
1005 ASSERT_NO_FATAL_FAILURE(InitState());
1006 ASSERT_NO_FATAL_FAILURE(InitViewport());
1007
1008 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001009 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001010
1011 XglIndexBufferObj indexBuffer(m_device);
1012 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
Tony Barboure6152042014-12-10 17:40:15 -07001013 indexBuffer.SetMemoryState(XGL_MEMORY_STATE_INDEX_DATA);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001014
1015 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1016 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1017
1018 XglPipelineObj pipelineobj(m_device);
1019 pipelineobj.AddShader(&vs);
1020 pipelineobj.AddShader(&ps);
1021
1022 XglDescriptorSetObj descriptorSet(m_device);
1023
1024 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1025 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1026 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1027 };
1028
1029 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1030 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1031 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1032 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1033 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1034 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1035 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1036 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1037 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1038
1039 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1040 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1041
1042 XGL_RESULT err = XGL_SUCCESS;
1043
1044 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1045 // Build command buffer
1046 // TODO: SetMemoryState above also uses this command buffer, do we need to make sure
1047 // that job is complete? Or maybe we shouldn't queue those actions?
1048 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
1049 ASSERT_XGL_SUCCESS(err);
1050
1051 GenerateClearAndPrepareBufferCmds();
1052 GenerateBindRenderTargetCmd();
1053#ifdef DUMP_STATE_DOT
1054 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
1055 pDSDumpDot((char*)"triTest.dot");
1056#endif
1057 GenerateBindStateAndPipelineCmds();
1058
Tony Barbour9d3c3172014-12-04 17:27:08 -07001059 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,&descriptorSet);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001060 descriptorSet.BindCommandBuffer(m_cmdBuffer);
1061
1062 meshBuffer.Bind(m_cmdBuffer, 0, 0);
1063 indexBuffer.Bind(m_cmdBuffer, /* offset */ 0);
1064
1065 // render two triangles
1066 xglCmdDrawIndexed(m_cmdBuffer, 0, 6, 0, 0, 1);
1067
1068 // finalize recording of the command buffer
1069 err = xglEndCommandBuffer( m_cmdBuffer );
1070 ASSERT_XGL_SUCCESS( err );
1071
1072 QueueCommandBuffer(NULL, 0);
1073
1074}
1075
GregF6bef1212014-12-02 15:41:44 -07001076TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1077{
1078 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001079
GregF6bef1212014-12-02 15:41:44 -07001080 static const char *vertShaderText =
1081 "#version 140\n"
1082 "#extension GL_ARB_separate_shader_objects : enable\n"
1083 "#extension GL_ARB_shading_language_420pack : enable\n"
1084 "layout (location = 0) in vec4 pos;\n"
1085 "layout (location = 0) out vec4 outColor;\n"
1086 "layout (location = 1) out vec4 outColor2;\n"
1087 "void main() {\n"
1088 " gl_Position = pos;\n"
1089 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1090 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1091 "}\n";
1092
1093 static const char *fragShaderText =
1094 //"#version 140\n"
1095 "#version 330\n"
1096 "#extension GL_ARB_separate_shader_objects : enable\n"
1097 "#extension GL_ARB_shading_language_420pack : enable\n"
1098 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1099 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1100 "layout (location = 0) in vec4 color;\n"
1101 "layout (location = 1) in vec4 color2;\n"
1102 "void main() {\n"
1103 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1104 " float dist_squared = dot(pos, pos);\n"
1105 " gl_FragColor = (dist_squared < 400.0)\n"
1106 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1107 " : color2;\n"
1108 "}\n";
1109
1110 ASSERT_NO_FATAL_FAILURE(InitState());
1111 ASSERT_NO_FATAL_FAILURE(InitViewport());
1112
1113 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001114 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001115
1116 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1117 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1118
1119 XglPipelineObj pipelineobj(m_device);
1120 pipelineobj.AddShader(&vs);
1121 pipelineobj.AddShader(&ps);
1122
1123 XglDescriptorSetObj descriptorSet(m_device);
1124 descriptorSet.AttachMemoryView(&meshBuffer);
1125
1126 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1127 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1128 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1129 };
1130
1131 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1132 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1133 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1134 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1135 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1136 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1137 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1138 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1139 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1140
1141 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1142 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1143 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1144
Tony Barbour09da2212014-12-03 16:13:23 -07001145 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001146 QueueCommandBuffer(NULL, 0);
1147
1148}
1149
1150TEST_F(XglRenderTest, RedCirclesonBlue)
1151{
1152 // This tests that we correctly handle unread fragment inputs
1153
1154 static const char *vertShaderText =
1155 "#version 140\n"
1156 "#extension GL_ARB_separate_shader_objects : enable\n"
1157 "#extension GL_ARB_shading_language_420pack : enable\n"
1158 "layout (location = 0) in vec4 pos;\n"
1159 "layout (location = 0) out vec4 outColor;\n"
1160 "layout (location = 1) out vec4 outColor2;\n"
1161 "void main() {\n"
1162 " gl_Position = pos;\n"
1163 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1164 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1165 "}\n";
1166
1167 static const char *fragShaderText =
1168 //"#version 140\n"
1169 "#version 330\n"
1170 "#extension GL_ARB_separate_shader_objects : enable\n"
1171 "#extension GL_ARB_shading_language_420pack : enable\n"
1172 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1173 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1174 "layout (location = 0) in vec4 color;\n"
1175 "layout (location = 1) in vec4 color2;\n"
1176 "void main() {\n"
1177 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1178 " float dist_squared = dot(pos, pos);\n"
1179 " gl_FragColor = (dist_squared < 400.0)\n"
1180 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1181 " : color2;\n"
1182 "}\n";
1183
1184 ASSERT_NO_FATAL_FAILURE(InitState());
1185 ASSERT_NO_FATAL_FAILURE(InitViewport());
1186
1187 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001188 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001189
1190 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1191 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1192
1193 XglPipelineObj pipelineobj(m_device);
1194 pipelineobj.AddShader(&vs);
1195 pipelineobj.AddShader(&ps);
1196
1197 XglDescriptorSetObj descriptorSet(m_device);
1198 descriptorSet.AttachMemoryView(&meshBuffer);
1199
1200 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1201 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1202 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1203 };
1204
1205 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1206 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1207 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1208 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1209 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1210 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1211 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1212 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1213 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1214
1215 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1216 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1217 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1218
Tony Barbour09da2212014-12-03 16:13:23 -07001219 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001220 QueueCommandBuffer(NULL, 0);
1221
1222}
1223
1224TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1225{
1226 // This tests reading gl_ClipDistance from FS
1227
1228 static const char *vertShaderText =
1229 "#version 330\n"
1230 "#extension GL_ARB_separate_shader_objects : enable\n"
1231 "#extension GL_ARB_shading_language_420pack : enable\n"
1232 "out gl_PerVertex {\n"
1233 " vec4 gl_Position;\n"
1234 " float gl_ClipDistance[1];\n"
1235 "};\n"
1236 "layout (location = 0) in vec4 pos;\n"
1237 "layout (location = 0) out vec4 outColor;\n"
1238 "layout (location = 1) out vec4 outColor2;\n"
1239 "void main() {\n"
1240 " gl_Position = pos;\n"
1241 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1242 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1243 " float dists[3];\n"
1244 " dists[0] = 0.0;\n"
1245 " dists[1] = 1.0;\n"
1246 " dists[2] = 1.0;\n"
1247 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1248 "}\n";
1249
1250
1251 static const char *fragShaderText =
1252 //"#version 140\n"
1253 "#version 330\n"
1254 "#extension GL_ARB_separate_shader_objects : enable\n"
1255 "#extension GL_ARB_shading_language_420pack : enable\n"
1256 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1257 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1258 "layout (location = 0) in vec4 color;\n"
1259 "layout (location = 1) in vec4 color2;\n"
1260 "void main() {\n"
1261 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1262 " float dist_squared = dot(pos, pos);\n"
1263 " gl_FragColor = (dist_squared < 400.0)\n"
1264 " ? color * gl_ClipDistance[0]\n"
1265 " : color2;\n"
1266 "}\n";
1267
1268 ASSERT_NO_FATAL_FAILURE(InitState());
1269 ASSERT_NO_FATAL_FAILURE(InitViewport());
1270
1271 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001272 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001273
1274 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1275 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1276
1277 XglPipelineObj pipelineobj(m_device);
1278 pipelineobj.AddShader(&vs);
1279 pipelineobj.AddShader(&ps);
1280
1281 XglDescriptorSetObj descriptorSet(m_device);
1282 descriptorSet.AttachMemoryView(&meshBuffer);
1283
1284 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1285 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1286 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1287 };
1288
1289 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1290 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1291 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1292 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1293 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1294 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1295 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1296 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1297 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1298
1299 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1300 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1301 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1302
Tony Barboura3953b82014-12-03 15:46:29 -07001303 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001304 QueueCommandBuffer(NULL, 0);
1305
1306}
Tony Barbourf43b6982014-11-25 13:18:32 -07001307
GregF7a23c792014-12-02 17:19:34 -07001308TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1309{
1310 static const char *vertShaderText =
1311 "#version 140\n"
1312 "#extension GL_ARB_separate_shader_objects : enable\n"
1313 "#extension GL_ARB_shading_language_420pack : enable\n"
1314 "layout (location = 0) in vec4 pos;\n"
1315 "layout (location = 0) out vec4 outColor;\n"
1316 "layout (location = 1) out vec4 outColor2;\n"
1317 "void main() {\n"
1318 " gl_Position = pos;\n"
1319 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1320 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1321 "}\n";
1322
1323
1324 static const char *fragShaderText =
1325 //"#version 140\n"
1326 "#version 330\n"
1327 "#extension GL_ARB_separate_shader_objects : enable\n"
1328 "#extension GL_ARB_shading_language_420pack : enable\n"
1329 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1330 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1331 "layout (location = 0) in vec4 color;\n"
1332 "layout (location = 1) in vec4 color2;\n"
1333 "void main() {\n"
1334 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1335 " float dist_squared = dot(pos, pos);\n"
1336 " if (dist_squared < 100.0)\n"
1337 " discard;\n"
1338 " gl_FragColor = (dist_squared < 400.0)\n"
1339 " ? color\n"
1340 " : color2;\n"
1341 "}\n";
1342
1343 ASSERT_NO_FATAL_FAILURE(InitState());
1344 ASSERT_NO_FATAL_FAILURE(InitViewport());
1345
1346 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001347 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF7a23c792014-12-02 17:19:34 -07001348
1349 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1350 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1351
1352 XglPipelineObj pipelineobj(m_device);
1353 pipelineobj.AddShader(&vs);
1354 pipelineobj.AddShader(&ps);
1355
1356 XglDescriptorSetObj descriptorSet(m_device);
1357 descriptorSet.AttachMemoryView(&meshBuffer);
1358
1359 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1360 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1361 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1362 };
1363
1364 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1365 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1366 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1367 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1368 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1369 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1370 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1371 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1372 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1373
1374 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1375 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1376 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1377
Tony Barbour09da2212014-12-03 16:13:23 -07001378 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF7a23c792014-12-02 17:19:34 -07001379 QueueCommandBuffer(NULL, 0);
1380
1381}
1382
1383
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001384TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001385{
1386 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001387 "#version 140\n"
1388 "#extension GL_ARB_separate_shader_objects : enable\n"
1389 "#extension GL_ARB_shading_language_420pack : enable\n"
1390 "\n"
1391 "layout(binding = 0) uniform buf {\n"
1392 " mat4 MVP;\n"
1393 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001394 "void main() {\n"
1395 " vec2 vertices[3];"
1396 " vertices[0] = vec2(-0.5, -0.5);\n"
1397 " vertices[1] = vec2( 0.5, -0.5);\n"
1398 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001399 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001400 "}\n";
1401
1402 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001403 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001404 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001405 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001406 "}\n";
1407
Tony Barbourf43b6982014-11-25 13:18:32 -07001408 ASSERT_NO_FATAL_FAILURE(InitState());
1409 ASSERT_NO_FATAL_FAILURE(InitViewport());
1410
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001411 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001412 glm::mat4 Projection = glm::mat4(1.0f);
1413 glm::mat4 View = glm::mat4(1.0f);
1414 glm::mat4 Model = glm::mat4(1.0f);
1415 glm::mat4 MVP = Projection * View * Model;
1416 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1417
Tony Barbourf43b6982014-11-25 13:18:32 -07001418 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1419 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1420 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001421
Tony Barbourf43b6982014-11-25 13:18:32 -07001422 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1423
1424 XglPipelineObj pipelineobj(m_device);
1425 pipelineobj.AddShader(&vs);
1426 pipelineobj.AddShader(&ps);
1427
1428 // Create descriptor set and attach the constant buffer to it
1429 XglDescriptorSetObj descriptorSet(m_device);
1430 descriptorSet.AttachMemoryView(&MVPBuffer);
1431
1432 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1433
Tony Barboura3953b82014-12-03 15:46:29 -07001434 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001435 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1436
Tony Barbour09da2212014-12-03 16:13:23 -07001437 RotateTriangleVSUniform(Projection, View, Model, &MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001438}
1439
Tony Barbourf43b6982014-11-25 13:18:32 -07001440TEST_F(XglRenderTest, MixTriangle)
1441{
1442 // This tests location applied to varyings. Notice that we have switched foo
1443 // and bar in the FS. The triangle should be blended with red, green and blue
1444 // corners.
1445 static const char *vertShaderText =
1446 "#version 140\n"
1447 "#extension GL_ARB_separate_shader_objects : enable\n"
1448 "#extension GL_ARB_shading_language_420pack : enable\n"
1449 "layout (location=0) out vec4 bar;\n"
1450 "layout (location=1) out vec4 foo;\n"
1451 "layout (location=2) out float scale;\n"
1452 "vec2 vertices[3];\n"
1453 "void main() {\n"
1454 " vertices[0] = vec2(-1.0, -1.0);\n"
1455 " vertices[1] = vec2( 1.0, -1.0);\n"
1456 " vertices[2] = vec2( 0.0, 1.0);\n"
1457 "vec4 colors[3];\n"
1458 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1459 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1460 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1461 " foo = colors[gl_VertexID % 3];\n"
1462 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1463 " scale = 1.0;\n"
1464 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1465 "}\n";
1466
1467 static const char *fragShaderText =
1468 "#version 140\n"
1469 "#extension GL_ARB_separate_shader_objects : enable\n"
1470 "#extension GL_ARB_shading_language_420pack : enable\n"
1471 "layout (location = 1) in vec4 bar;\n"
1472 "layout (location = 0) in vec4 foo;\n"
1473 "layout (location = 2) in float scale;\n"
1474 "void main() {\n"
1475 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1476 "}\n";
1477
1478 ASSERT_NO_FATAL_FAILURE(InitState());
1479 ASSERT_NO_FATAL_FAILURE(InitViewport());
1480
1481 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1482 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1483
1484 XglPipelineObj pipelineobj(m_device);
1485 pipelineobj.AddShader(&vs);
1486 pipelineobj.AddShader(&ps);
1487
1488 XglDescriptorSetObj descriptorSet(m_device);
1489
Tony Barboura3953b82014-12-03 15:46:29 -07001490 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001491 QueueCommandBuffer(NULL, 0);
1492}
1493
1494TEST_F(XglRenderTest, TriVertFetchAndVertID)
1495{
1496 // This tests that attributes work in the presence of gl_VertexID
1497
1498 static const char *vertShaderText =
1499 "#version 140\n"
1500 "#extension GL_ARB_separate_shader_objects : enable\n"
1501 "#extension GL_ARB_shading_language_420pack : enable\n"
1502 //XYZ1( -1, -1, -1 )
1503 "layout (location = 0) in vec4 pos;\n"
1504 //XYZ1( 0.f, 0.f, 0.f )
1505 "layout (location = 1) in vec4 inColor;\n"
1506 "layout (location = 0) out vec4 outColor;\n"
1507 "void main() {\n"
1508 " outColor = inColor;\n"
1509 " vec4 vertices[3];"
1510 " vertices[gl_VertexID % 3] = pos;\n"
1511 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1512 "}\n";
1513
1514
1515 static const char *fragShaderText =
1516 "#version 140\n"
1517 "#extension GL_ARB_separate_shader_objects : enable\n"
1518 "#extension GL_ARB_shading_language_420pack : enable\n"
1519 "layout (location = 0) in vec4 color;\n"
1520 "void main() {\n"
1521 " gl_FragColor = color;\n"
1522 "}\n";
1523
1524 ASSERT_NO_FATAL_FAILURE(InitState());
1525 ASSERT_NO_FATAL_FAILURE(InitViewport());
1526
1527 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001528 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001529
1530 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1531 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1532
1533 XglPipelineObj pipelineobj(m_device);
1534 pipelineobj.AddShader(&vs);
1535 pipelineobj.AddShader(&ps);
1536
1537 XglDescriptorSetObj descriptorSet(m_device);
1538 descriptorSet.AttachMemoryView(&meshBuffer);
1539
1540 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1541 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1542 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1543 };
1544
1545 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1546 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1547 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1548 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1549 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1550 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1551 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1552 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1553 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1554
1555 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1556 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1557 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1558
Tony Barboura3953b82014-12-03 15:46:29 -07001559 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001560 QueueCommandBuffer(NULL, 0);
1561}
1562
1563TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1564{
1565 // This tests that attributes work in the presence of gl_VertexID
1566 // and a dead attribute in position 0. Draws a triangle with yellow,
1567 // red and green corners, starting at top and going clockwise.
1568
1569 static const char *vertShaderText =
1570 "#version 140\n"
1571 "#extension GL_ARB_separate_shader_objects : enable\n"
1572 "#extension GL_ARB_shading_language_420pack : enable\n"
1573 //XYZ1( -1, -1, -1 )
1574 "layout (location = 0) in vec4 pos;\n"
1575 //XYZ1( 0.f, 0.f, 0.f )
1576 "layout (location = 1) in vec4 inColor;\n"
1577 "layout (location = 0) out vec4 outColor;\n"
1578 "void main() {\n"
1579 " outColor = inColor;\n"
1580 " vec2 vertices[3];"
1581 " vertices[0] = vec2(-1.0, -1.0);\n"
1582 " vertices[1] = vec2( 1.0, -1.0);\n"
1583 " vertices[2] = vec2( 0.0, 1.0);\n"
1584 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1585 "}\n";
1586
1587
1588 static const char *fragShaderText =
1589 "#version 140\n"
1590 "#extension GL_ARB_separate_shader_objects : enable\n"
1591 "#extension GL_ARB_shading_language_420pack : enable\n"
1592 "layout (location = 0) in vec4 color;\n"
1593 "void main() {\n"
1594 " gl_FragColor = color;\n"
1595 "}\n";
1596
1597 ASSERT_NO_FATAL_FAILURE(InitState());
1598 ASSERT_NO_FATAL_FAILURE(InitViewport());
1599
1600 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001601 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001602
1603 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1604 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1605
1606 XglPipelineObj pipelineobj(m_device);
1607 pipelineobj.AddShader(&vs);
1608 pipelineobj.AddShader(&ps);
1609
1610 XglDescriptorSetObj descriptorSet(m_device);
1611 descriptorSet.AttachMemoryView(&meshBuffer);
1612
1613 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1614 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1615 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1616 };
1617
1618 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1619 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1620 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1621 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1622 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1623 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1624 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1625 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1626 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1627
1628 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1629 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1630 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1631
Tony Barbour09da2212014-12-03 16:13:23 -07001632 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001633 QueueCommandBuffer(NULL, 0);
1634
1635}
1636
1637TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001638{
1639 static const char *vertShaderText =
1640 "#version 140\n"
1641 "layout (std140) uniform bufferVals {\n"
1642 " mat4 mvp;\n"
1643 "} myBufferVals;\n"
1644 "in vec4 pos;\n"
1645 "in vec4 inColor;\n"
1646 "out vec4 outColor;\n"
1647 "void main() {\n"
1648 " outColor = inColor;\n"
1649 " gl_Position = myBufferVals.mvp * pos;\n"
1650 "}\n";
1651
1652 static const char *fragShaderText =
1653 "#version 130\n"
1654 "in vec4 color;\n"
1655 "void main() {\n"
1656 " gl_FragColor = color;\n"
1657 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001658 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001659
Tony Barbourf43b6982014-11-25 13:18:32 -07001660 glm::mat4 View = glm::lookAt(
1661 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1662 glm::vec3(0,0,0), // and looks at the origin
1663 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1664 );
1665
1666 glm::mat4 Model = glm::mat4(1.0f);
1667
1668 glm::mat4 MVP = Projection * View * Model;
1669
1670 ASSERT_NO_FATAL_FAILURE(InitState());
1671 ASSERT_NO_FATAL_FAILURE(InitViewport());
1672 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1673
1674 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1675 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1676
1677 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1678
1679 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1680 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1681 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1682
1683 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1684
1685 XglPipelineObj pipelineobj(m_device);
1686 pipelineobj.AddShader(&vs);
1687 pipelineobj.AddShader(&ps);
1688
1689 XglDescriptorSetObj descriptorSet(m_device);
1690 descriptorSet.AttachMemoryView(&MVPBuffer);
1691
1692 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1693 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1694
1695 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1696 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1697 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1698 };
1699
1700 // this is the current description of g_vbData
1701 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1702 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1703 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1704 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1705 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1706 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1707 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1708 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1709 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1710
1711 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1712 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1713 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1714
Tony Barboura3953b82014-12-03 15:46:29 -07001715 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07001716
1717 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1718
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001719}
1720
Tony Barbourf43b6982014-11-25 13:18:32 -07001721TEST_F(XglRenderTest, VSTexture)
1722{
1723 // The expected result from this test is a green and red triangle;
1724 // one red vertex on the left, two green vertices on the right.
1725 static const char *vertShaderText =
1726 "#version 130\n"
1727 "out vec4 texColor;\n"
1728 "uniform sampler2D surface;\n"
1729 "void main() {\n"
1730 " vec2 vertices[3];"
1731 " vertices[0] = vec2(-0.5, -0.5);\n"
1732 " vertices[1] = vec2( 0.5, -0.5);\n"
1733 " vertices[2] = vec2( 0.5, 0.5);\n"
1734 " vec2 positions[3];"
1735 " positions[0] = vec2( 0.0, 0.0);\n"
1736 " positions[1] = vec2( 0.25, 0.1);\n"
1737 " positions[2] = vec2( 0.1, 0.25);\n"
1738 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1739 " texColor = textureLod(surface, samplePos, 0.0);\n"
1740 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1741 "}\n";
1742
1743 static const char *fragShaderText =
1744 "#version 130\n"
1745 "in vec4 texColor;\n"
1746 "void main() {\n"
1747 " gl_FragColor = texColor;\n"
1748 "}\n";
1749
1750 ASSERT_NO_FATAL_FAILURE(InitState());
1751 ASSERT_NO_FATAL_FAILURE(InitViewport());
1752
1753 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1754 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1755 XglSamplerObj sampler(m_device);
1756 XglTextureObj texture(m_device);
1757
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001758 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001759 vs.BindShaderEntitySlotToSampler(0, &sampler);
1760
1761 XglPipelineObj pipelineobj(m_device);
1762 pipelineobj.AddShader(&vs);
1763 pipelineobj.AddShader(&ps);
1764
1765 XglDescriptorSetObj descriptorSet(m_device);
1766 descriptorSet.AttachImageView(&texture);
1767 descriptorSet.AttachSampler(&sampler);
1768
1769 m_memoryRefManager.AddMemoryRef(&texture);
1770
Tony Barboura3953b82014-12-03 15:46:29 -07001771 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001772 QueueCommandBuffer(NULL, 0);
1773
1774}
1775TEST_F(XglRenderTest, TexturedTriangle)
1776{
1777 // The expected result from this test is a red and green checkered triangle
1778 static const char *vertShaderText =
1779 "#version 140\n"
1780 "#extension GL_ARB_separate_shader_objects : enable\n"
1781 "#extension GL_ARB_shading_language_420pack : enable\n"
1782 "layout (location = 0) out vec2 samplePos;\n"
1783 "void main() {\n"
1784 " vec2 vertices[3];"
1785 " vertices[0] = vec2(-0.5, -0.5);\n"
1786 " vertices[1] = vec2( 0.5, -0.5);\n"
1787 " vertices[2] = vec2( 0.5, 0.5);\n"
1788 " vec2 positions[3];"
1789 " positions[0] = vec2( 0.0, 0.0);\n"
1790 " positions[1] = vec2( 1.0, 0.0);\n"
1791 " positions[2] = vec2( 1.0, 1.0);\n"
1792 " samplePos = positions[gl_VertexID % 3];\n"
1793 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1794 "}\n";
1795
1796 static const char *fragShaderText =
1797 "#version 140\n"
1798 "#extension GL_ARB_separate_shader_objects : enable\n"
1799 "#extension GL_ARB_shading_language_420pack : enable\n"
1800 "layout (location = 0) in vec2 samplePos;\n"
1801 "layout (binding = 0) uniform sampler2D surface;\n"
1802 "layout (location=0) out vec4 outColor;\n"
1803 "void main() {\n"
1804 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1805 " outColor = texColor;\n"
1806 "}\n";
1807
1808 ASSERT_NO_FATAL_FAILURE(InitState());
1809 ASSERT_NO_FATAL_FAILURE(InitViewport());
1810
1811 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1812 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1813 XglSamplerObj sampler(m_device);
1814 XglTextureObj texture(m_device);
1815
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001816 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001817 ps.BindShaderEntitySlotToSampler(0, &sampler);
1818
1819 XglPipelineObj pipelineobj(m_device);
1820 pipelineobj.AddShader(&vs);
1821 pipelineobj.AddShader(&ps);
1822
1823 XglDescriptorSetObj descriptorSet(m_device);
1824 descriptorSet.AttachImageView(&texture);
1825 descriptorSet.AttachSampler(&sampler);
1826
1827 m_memoryRefManager.AddMemoryRef(&texture);
1828
Tony Barboura3953b82014-12-03 15:46:29 -07001829 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001830 QueueCommandBuffer(NULL, 0);
1831}
1832TEST_F(XglRenderTest, TexturedTriangleClip)
1833{
1834 // The expected result from this test is a red and green checkered triangle
1835 static const char *vertShaderText =
1836 "#version 330\n"
1837 "#extension GL_ARB_separate_shader_objects : enable\n"
1838 "#extension GL_ARB_shading_language_420pack : enable\n"
1839 "layout (location = 0) out vec2 samplePos;\n"
1840 "out gl_PerVertex {\n"
1841 " vec4 gl_Position;\n"
1842 " float gl_ClipDistance[1];\n"
1843 "};\n"
1844 "void main() {\n"
1845 " vec2 vertices[3];"
1846 " vertices[0] = vec2(-0.5, -0.5);\n"
1847 " vertices[1] = vec2( 0.5, -0.5);\n"
1848 " vertices[2] = vec2( 0.5, 0.5);\n"
1849 " vec2 positions[3];"
1850 " positions[0] = vec2( 0.0, 0.0);\n"
1851 " positions[1] = vec2( 1.0, 0.0);\n"
1852 " positions[2] = vec2( 1.0, 1.0);\n"
1853 " float dists[3];\n"
1854 " dists[0] = 1.0;\n"
1855 " dists[1] = 1.0;\n"
1856 " dists[2] = -1.0;\n"
1857 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1858 " samplePos = positions[gl_VertexID % 3];\n"
1859 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1860 "}\n";
1861
1862 static const char *fragShaderText =
1863 "#version 140\n"
1864 "#extension GL_ARB_separate_shader_objects : enable\n"
1865 "#extension GL_ARB_shading_language_420pack : enable\n"
1866 "layout (location = 0) in vec2 samplePos;\n"
1867 "layout (binding = 0) uniform sampler2D surface;\n"
1868 "layout (location=0) out vec4 outColor;\n"
1869 "void main() {\n"
1870 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1871 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1872 " outColor = texColor;\n"
1873 "}\n";
1874
1875
1876 ASSERT_NO_FATAL_FAILURE(InitState());
1877 ASSERT_NO_FATAL_FAILURE(InitViewport());
1878
1879 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1880 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1881 XglSamplerObj sampler(m_device);
1882 XglTextureObj texture(m_device);
1883
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001884 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001885 ps.BindShaderEntitySlotToSampler(0, &sampler);
1886
1887 XglPipelineObj pipelineobj(m_device);
1888 pipelineobj.AddShader(&vs);
1889 pipelineobj.AddShader(&ps);
1890
1891 XglDescriptorSetObj descriptorSet(m_device);
1892 descriptorSet.AttachImageView(&texture);
1893 descriptorSet.AttachSampler(&sampler);
1894
1895 m_memoryRefManager.AddMemoryRef(&texture);
1896
Tony Barboura3953b82014-12-03 15:46:29 -07001897 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001898 QueueCommandBuffer(NULL, 0);
1899}
1900TEST_F(XglRenderTest, FSTriangle)
1901{
1902 // The expected result from this test is a red and green checkered triangle
1903 static const char *vertShaderText =
1904 "#version 140\n"
1905 "#extension GL_ARB_separate_shader_objects : enable\n"
1906 "#extension GL_ARB_shading_language_420pack : enable\n"
1907 "layout (location = 0) out vec2 samplePos;\n"
1908 "void main() {\n"
1909 " vec2 vertices[3];"
1910 " vertices[0] = vec2(-0.5, -0.5);\n"
1911 " vertices[1] = vec2( 0.5, -0.5);\n"
1912 " vertices[2] = vec2( 0.5, 0.5);\n"
1913 " vec2 positions[3];"
1914 " positions[0] = vec2( 0.0, 0.0);\n"
1915 " positions[1] = vec2( 1.0, 0.0);\n"
1916 " positions[2] = vec2( 1.0, 1.0);\n"
1917 " samplePos = positions[gl_VertexID % 3];\n"
1918 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1919 "}\n";
1920
1921 static const char *fragShaderText =
1922 "#version 140\n"
1923 "#extension GL_ARB_separate_shader_objects : enable\n"
1924 "#extension GL_ARB_shading_language_420pack : enable\n"
1925 "layout (location = 0) in vec2 samplePos;\n"
1926 "layout (binding = 0) uniform sampler2D surface;\n"
1927 "layout (location=0) out vec4 outColor;\n"
1928 "void main() {\n"
1929 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1930 " outColor = texColor;\n"
1931 "}\n";
1932
1933 ASSERT_NO_FATAL_FAILURE(InitState());
1934 ASSERT_NO_FATAL_FAILURE(InitViewport());
1935
1936 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1937 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1938 XglSamplerObj sampler(m_device);
1939 XglTextureObj texture(m_device);
1940
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001941 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001942 ps.BindShaderEntitySlotToSampler(0, &sampler);
1943
1944 XglPipelineObj pipelineobj(m_device);
1945 pipelineobj.AddShader(&vs);
1946 pipelineobj.AddShader(&ps);
1947
1948 XglDescriptorSetObj descriptorSet(m_device);
1949 descriptorSet.AttachImageView(&texture);
1950 descriptorSet.AttachSampler(&sampler);
1951
1952 m_memoryRefManager.AddMemoryRef(&texture);
1953
Tony Barboura3953b82014-12-03 15:46:29 -07001954 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001955 QueueCommandBuffer(NULL, 0);
1956}
1957TEST_F(XglRenderTest, SamplerBindingsTriangle)
1958{
1959 // This test sets bindings on the samplers
1960 // For now we are asserting that sampler and texture pairs
1961 // march in lock step, and are set via GLSL binding. This can
1962 // and will probably change.
1963 // The sampler bindings should match the sampler and texture slot
1964 // number set up by the application.
1965 // This test will result in a blue triangle
1966 static const char *vertShaderText =
1967 "#version 140\n"
1968 "#extension GL_ARB_separate_shader_objects : enable\n"
1969 "#extension GL_ARB_shading_language_420pack : enable\n"
1970 "layout (location = 0) out vec4 samplePos;\n"
1971 "void main() {\n"
1972 " vec2 vertices[3];"
1973 " vertices[0] = vec2(-0.5, -0.5);\n"
1974 " vertices[1] = vec2( 0.5, -0.5);\n"
1975 " vertices[2] = vec2( 0.5, 0.5);\n"
1976 " vec2 positions[3];"
1977 " positions[0] = vec2( 0.0, 0.0);\n"
1978 " positions[1] = vec2( 1.0, 0.0);\n"
1979 " positions[2] = vec2( 1.0, 1.0);\n"
1980 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
1981 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1982 "}\n";
1983
1984 static const char *fragShaderText =
1985 "#version 140\n"
1986 "#extension GL_ARB_separate_shader_objects : enable\n"
1987 "#extension GL_ARB_shading_language_420pack : enable\n"
1988 "layout (location = 0) in vec4 samplePos;\n"
1989 "layout (binding = 0) uniform sampler2D surface0;\n"
1990 "layout (binding = 1) uniform sampler2D surface1;\n"
1991 "layout (binding = 12) uniform sampler2D surface2;\n"
1992 "void main() {\n"
1993 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
1994 "}\n";
1995
1996 ASSERT_NO_FATAL_FAILURE(InitState());
1997 ASSERT_NO_FATAL_FAILURE(InitViewport());
1998
1999 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2000 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2001
2002 XglSamplerObj sampler1(m_device);
2003 XglSamplerObj sampler2(m_device);
2004 XglSamplerObj sampler3(m_device);
2005
2006 XglTextureObj texture1(m_device); // Red
2007 texture1.ChangeColors(0xffff0000,0xffff0000);
2008 XglTextureObj texture2(m_device); // Green
2009 texture2.ChangeColors(0xff00ff00,0xff00ff00);
2010 XglTextureObj texture3(m_device); // Blue
2011 texture3.ChangeColors(0xff0000ff,0xff0000ff);
2012
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002013 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002014 ps.BindShaderEntitySlotToSampler(0, &sampler1);
2015
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002016 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Tony Barbourf43b6982014-11-25 13:18:32 -07002017 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2018
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002019 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture3);
Tony Barbourf43b6982014-11-25 13:18:32 -07002020 ps.BindShaderEntitySlotToSampler(12, &sampler3);
2021
2022 XglPipelineObj pipelineobj(m_device);
2023 pipelineobj.AddShader(&vs);
2024 pipelineobj.AddShader(&ps);
2025
2026 XglDescriptorSetObj descriptorSet(m_device);
2027 descriptorSet.AttachImageView(&texture1);
2028 descriptorSet.AttachSampler(&sampler1);
2029 descriptorSet.AttachImageView(&texture2);
2030 descriptorSet.AttachSampler(&sampler2);
2031 descriptorSet.AttachImageView(&texture3);
2032 descriptorSet.AttachSampler(&sampler3);
2033
2034 m_memoryRefManager.AddMemoryRef(&texture1);
2035 m_memoryRefManager.AddMemoryRef(&texture2);
2036 m_memoryRefManager.AddMemoryRef(&texture3);
2037
Tony Barboura3953b82014-12-03 15:46:29 -07002038 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002039 QueueCommandBuffer(NULL, 0);
2040
2041}
2042
2043TEST_F(XglRenderTest, TriangleVSUniformBlock)
2044{
2045 // The expected result from this test is a blue triangle
2046
2047 static const char *vertShaderText =
2048 "#version 140\n"
2049 "#extension GL_ARB_separate_shader_objects : enable\n"
2050 "#extension GL_ARB_shading_language_420pack : enable\n"
2051 "layout (location = 0) out vec4 outColor;\n"
2052 "layout (std140, binding = 0) uniform bufferVals {\n"
2053 " vec4 red;\n"
2054 " vec4 green;\n"
2055 " vec4 blue;\n"
2056 " vec4 white;\n"
2057 "} myBufferVals;\n"
2058 "void main() {\n"
2059 " vec2 vertices[3];"
2060 " vertices[0] = vec2(-0.5, -0.5);\n"
2061 " vertices[1] = vec2( 0.5, -0.5);\n"
2062 " vertices[2] = vec2( 0.5, 0.5);\n"
2063 " outColor = myBufferVals.blue;\n"
2064 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2065 "}\n";
2066
2067 static const char *fragShaderText =
2068 "#version 140\n"
2069 "#extension GL_ARB_separate_shader_objects : enable\n"
2070 "#extension GL_ARB_shading_language_420pack : enable\n"
2071 "layout (location = 0) in vec4 inColor;\n"
2072 "void main() {\n"
2073 " gl_FragColor = inColor;\n"
2074 "}\n";
2075
2076 ASSERT_NO_FATAL_FAILURE(InitState());
2077 ASSERT_NO_FATAL_FAILURE(InitViewport());
2078
2079 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2080 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2081
2082 // Let's populate our buffer with the following:
2083 // vec4 red;
2084 // vec4 green;
2085 // vec4 blue;
2086 // vec4 white;
2087 const int valCount = 4 * 4;
2088 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2089 0.0, 1.0, 0.0, 1.0,
2090 0.0, 0.0, 1.0, 1.0,
2091 1.0, 1.0, 1.0, 1.0 };
2092
2093 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2094 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2095
2096 XglPipelineObj pipelineobj(m_device);
2097 pipelineobj.AddShader(&vs);
2098 pipelineobj.AddShader(&ps);
2099
2100 XglDescriptorSetObj descriptorSet(m_device);
2101 descriptorSet.AttachMemoryView(&colorBuffer);
2102
Tony Barboura3953b82014-12-03 15:46:29 -07002103 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002104 QueueCommandBuffer(NULL, 0);
2105
2106}
2107
2108TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2109{
2110 // This test allows the shader to select which buffer it is
2111 // pulling from using layout binding qualifier.
2112 // There are corresponding changes in the compiler stack that
2113 // will select the buffer using binding directly.
2114 // The binding number should match the slot number set up by
2115 // the application.
2116 // The expected result from this test is a purple triangle
2117
2118 static const char *vertShaderText =
2119 "#version 140\n"
2120 "#extension GL_ARB_separate_shader_objects : enable\n"
2121 "#extension GL_ARB_shading_language_420pack : enable\n"
2122 "void main() {\n"
2123 " vec2 vertices[3];"
2124 " vertices[0] = vec2(-0.5, -0.5);\n"
2125 " vertices[1] = vec2( 0.5, -0.5);\n"
2126 " vertices[2] = vec2( 0.5, 0.5);\n"
2127 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2128 "}\n";
2129
2130 static const char *fragShaderText =
2131 "#version 140\n"
2132 "#extension GL_ARB_separate_shader_objects : enable\n"
2133 "#extension GL_ARB_shading_language_420pack : enable\n"
2134 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2135 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2136 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2137 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2138 "void main() {\n"
2139 " gl_FragColor = myBlueVal.color;\n"
2140 " gl_FragColor += myRedVal.color;\n"
2141 "}\n";
2142
2143 ASSERT_NO_FATAL_FAILURE(InitState());
2144 ASSERT_NO_FATAL_FAILURE(InitViewport());
2145
2146 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2147 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2148
2149 // We're going to create a number of uniform buffers, and then allow
2150 // the shader to select which it wants to read from with a binding
2151
2152 // Let's populate the buffers with a single color each:
2153 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2154 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2155 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2156 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2157
2158 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2159 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2160 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2161 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2162
2163 const int redCount = sizeof(redVals) / sizeof(float);
2164 const int greenCount = sizeof(greenVals) / sizeof(float);
2165 const int blueCount = sizeof(blueVals) / sizeof(float);
2166 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2167
2168 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2169 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2170
2171 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2172 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2173
2174 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2175 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2176
2177 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2178 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2179
2180 XglPipelineObj pipelineobj(m_device);
2181 pipelineobj.AddShader(&vs);
2182 pipelineobj.AddShader(&ps);
2183
2184 XglDescriptorSetObj descriptorSet(m_device);
2185 descriptorSet.AttachMemoryView(&redBuffer);
2186 descriptorSet.AttachMemoryView(&greenBuffer);
2187 descriptorSet.AttachMemoryView(&blueBuffer);
2188 descriptorSet.AttachMemoryView(&whiteBuffer);
2189
Tony Barboura3953b82014-12-03 15:46:29 -07002190 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002191 QueueCommandBuffer(NULL, 0);
2192
2193}
2194
2195TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2196{
2197 // This test is the same as TriangleFSUniformBlockBinding, but
2198 // it does not provide an instance name.
2199 // The expected result from this test is a purple triangle
2200
2201 static const char *vertShaderText =
2202 "#version 140\n"
2203 "#extension GL_ARB_separate_shader_objects : enable\n"
2204 "#extension GL_ARB_shading_language_420pack : enable\n"
2205 "void main() {\n"
2206 " vec2 vertices[3];"
2207 " vertices[0] = vec2(-0.5, -0.5);\n"
2208 " vertices[1] = vec2( 0.5, -0.5);\n"
2209 " vertices[2] = vec2( 0.5, 0.5);\n"
2210 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2211 "}\n";
2212
2213 static const char *fragShaderText =
2214 "#version 430\n"
2215 "#extension GL_ARB_separate_shader_objects : enable\n"
2216 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002217 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2218 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2219 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2220 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
Cody Northrop68a10f62014-12-05 15:44:14 -07002221 "layout (location = 0) out vec4 outColor;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002222 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -07002223 " outColor = blue;\n"
2224 " outColor += red;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002225 "}\n";
2226 ASSERT_NO_FATAL_FAILURE(InitState());
2227 ASSERT_NO_FATAL_FAILURE(InitViewport());
2228
2229 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2230 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2231
2232 // We're going to create a number of uniform buffers, and then allow
2233 // the shader to select which it wants to read from with a binding
2234
2235 // Let's populate the buffers with a single color each:
2236 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2237 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2238 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2239 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2240
2241 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2242 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2243 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2244 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2245
2246 const int redCount = sizeof(redVals) / sizeof(float);
2247 const int greenCount = sizeof(greenVals) / sizeof(float);
2248 const int blueCount = sizeof(blueVals) / sizeof(float);
2249 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2250
2251 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2252 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2253
2254 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2255 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2256
2257 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2258 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2259
2260 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002261 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07002262
2263 XglPipelineObj pipelineobj(m_device);
2264 pipelineobj.AddShader(&vs);
2265 pipelineobj.AddShader(&ps);
2266
2267 XglDescriptorSetObj descriptorSet(m_device);
2268 descriptorSet.AttachMemoryView(&redBuffer);
2269 descriptorSet.AttachMemoryView(&greenBuffer);
2270 descriptorSet.AttachMemoryView(&blueBuffer);
2271 descriptorSet.AttachMemoryView(&whiteBuffer);
2272
Tony Barboura3953b82014-12-03 15:46:29 -07002273 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002274 QueueCommandBuffer(NULL, 0);
2275
2276}
2277
2278TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2279{
2280 static const char *vertShaderText =
2281 "#version 140\n"
2282 "#extension GL_ARB_separate_shader_objects : enable\n"
2283 "#extension GL_ARB_shading_language_420pack : enable\n"
2284 "layout (std140, binding=0) uniform bufferVals {\n"
2285 " mat4 mvp;\n"
2286 "} myBufferVals;\n"
2287 "layout (location=0) in vec4 pos;\n"
2288 "layout (location=0) out vec2 UV;\n"
2289 "void main() {\n"
2290 " vec2 positions[3];"
2291 " positions[0] = vec2( 0.0, 0.0);\n"
2292 " positions[1] = vec2( 0.25, 0.1);\n"
2293 " positions[2] = vec2( 0.1, 0.25);\n"
2294 " UV = positions[gl_VertexID % 3];\n"
2295 " gl_Position = myBufferVals.mvp * pos;\n"
2296 "}\n";
2297
2298 static const char *fragShaderText =
2299 "#version 140\n"
2300 "#extension GL_ARB_separate_shader_objects : enable\n"
2301 "#extension GL_ARB_shading_language_420pack : enable\n"
2302 "layout (binding=0) uniform sampler2D surface;\n"
2303 "layout (location=0) out vec4 outColor;\n"
2304 "layout (location=0) in vec2 UV;\n"
2305 "void main() {\n"
2306 " outColor= textureLod(surface, UV, 0.0);\n"
2307 "}\n";
2308 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2309
2310 glm::mat4 View = glm::lookAt(
2311 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2312 glm::vec3(0,0,0), // and looks at the origin
2313 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2314 );
2315
2316 glm::mat4 Model = glm::mat4(1.0f);
2317
2318 glm::mat4 MVP = Projection * View * Model;
2319
2320
2321 ASSERT_NO_FATAL_FAILURE(InitState());
2322 ASSERT_NO_FATAL_FAILURE(InitViewport());
2323 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2324
2325 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2326 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
Tony Barboure6152042014-12-10 17:40:15 -07002327 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07002328
2329
2330 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2331
2332 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2333 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2334 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2335 XglSamplerObj sampler(m_device);
2336 XglTextureObj texture(m_device);
2337
2338 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2339 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002340 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002341 ps.BindShaderEntitySlotToSampler(0, &sampler);
2342
2343 XglPipelineObj pipelineobj(m_device);
2344 pipelineobj.AddShader(&vs);
2345 pipelineobj.AddShader(&ps);
2346
2347 XglDescriptorSetObj descriptorSet(m_device);
2348
2349 descriptorSet.AttachMemoryView(&mvpBuffer);
2350 descriptorSet.AttachImageView(&texture);
2351 descriptorSet.AttachSampler(&sampler);
2352
2353 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2354 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2355 m_memoryRefManager.AddMemoryRef(&texture);
2356
2357 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2358 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2359 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2360 };
2361
2362 // this is the current description of g_vbData
2363 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2364 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2365 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2366 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2367 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2368 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2369 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2370 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2371 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2372
2373 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2374 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2375 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2376
Tony Barboura3953b82014-12-03 15:46:29 -07002377 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07002378
2379 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2380
2381}
Cody Northropd1ce7842014-12-09 11:17:01 -07002382
2383TEST_F(XglRenderTest, TriangleMixedSamplerUniformBlockBinding)
2384{
2385 // This test mixes binding slots of textures and buffers, ensuring
2386 // that sparse and overlapping assignments work.
Cody Northropa0410942014-12-09 13:59:39 -07002387 // The expected result from this test is a purple triangle, although
Cody Northropd1ce7842014-12-09 11:17:01 -07002388 // you can modify it to move the desired result around.
2389
2390 static const char *vertShaderText =
2391 "#version 140\n"
2392 "#extension GL_ARB_separate_shader_objects : enable\n"
2393 "#extension GL_ARB_shading_language_420pack : enable\n"
2394 "void main() {\n"
2395 " vec2 vertices[3];"
2396 " vertices[0] = vec2(-0.5, -0.5);\n"
2397 " vertices[1] = vec2( 0.5, -0.5);\n"
2398 " vertices[2] = vec2( 0.5, 0.5);\n"
2399 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2400 "}\n";
2401
2402 static const char *fragShaderText =
2403 "#version 430\n"
2404 "#extension GL_ARB_separate_shader_objects : enable\n"
2405 "#extension GL_ARB_shading_language_420pack : enable\n"
2406 "layout (binding = 0) uniform sampler2D surface0;\n"
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002407 "layout (binding = 9) uniform sampler2D surface1;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002408 "layout (binding = 2) uniform sampler2D surface2;\n"
Cody Northropa0410942014-12-09 13:59:39 -07002409 "layout (binding = 4) uniform sampler2D surface3;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002410
Cody Northropa0410942014-12-09 13:59:39 -07002411
2412 "layout (std140, binding = 10) uniform redVal { vec4 red; };"
2413 "layout (std140, binding = 15) uniform greenVal { vec4 green; };"
2414 "layout (std140, binding = 13) uniform blueVal { vec4 blue; };"
2415 "layout (std140, binding = 17) uniform whiteVal { vec4 white; };"
Cody Northropd1ce7842014-12-09 11:17:01 -07002416 "layout (location = 0) out vec4 outColor;\n"
2417 "void main() {\n"
Cody Northropa0410942014-12-09 13:59:39 -07002418 " outColor = red * vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002419 " outColor += white * vec4(0.00001);\n"
2420 " outColor += textureLod(surface2, vec2(0.5), 0.0)* vec4(0.00001);\n"
Cody Northropa0410942014-12-09 13:59:39 -07002421 " outColor += textureLod(surface1, vec2(0.0), 0.0);//* vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002422 "}\n";
2423 ASSERT_NO_FATAL_FAILURE(InitState());
2424 ASSERT_NO_FATAL_FAILURE(InitViewport());
2425
2426 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2427 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2428
Cody Northropd1ce7842014-12-09 11:17:01 -07002429 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2430 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2431 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2432 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2433
2434 const int redCount = sizeof(redVals) / sizeof(float);
2435 const int greenCount = sizeof(greenVals) / sizeof(float);
2436 const int blueCount = sizeof(blueVals) / sizeof(float);
2437 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2438
2439 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Cody Northropa0410942014-12-09 13:59:39 -07002440 ps.BindShaderEntitySlotToMemory(10, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002441
2442 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Cody Northropa0410942014-12-09 13:59:39 -07002443 ps.BindShaderEntitySlotToMemory(15, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002444
2445 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Cody Northropa0410942014-12-09 13:59:39 -07002446 ps.BindShaderEntitySlotToMemory(13, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002447
2448 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropa0410942014-12-09 13:59:39 -07002449 ps.BindShaderEntitySlotToMemory(17, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002450
2451 XglSamplerObj sampler0(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002452 XglTextureObj texture0(m_device); // Light Red
2453 texture0.ChangeColors(0xff800000,0xff800000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002454 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
Cody Northropd1ce7842014-12-09 11:17:01 -07002455 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2456 XglSamplerObj sampler2(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002457 XglTextureObj texture2(m_device); // Light Blue
2458 texture2.ChangeColors(0xff000080,0xff000080);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002459 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Cody Northropd1ce7842014-12-09 11:17:01 -07002460 ps.BindShaderEntitySlotToSampler(2, &sampler2);
2461 XglSamplerObj sampler4(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002462 XglTextureObj texture4(m_device); // Light Green
2463 texture4.ChangeColors(0xff008000,0xff008000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002464 ps.BindShaderEntitySlotToImage(4, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
Cody Northropd1ce7842014-12-09 11:17:01 -07002465 ps.BindShaderEntitySlotToSampler(4, &sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002466
2467 // NOTE: Bindings 1,3,5,7,8,9,11,12,14,16 work for this sampler, but 6 does not!!!
2468 // TODO: Get back here ASAP and understand why.
2469 XglSamplerObj sampler7(m_device);
2470 XglTextureObj texture7(m_device); // Red and Blue
2471 texture7.ChangeColors(0xffff00ff,0xffff00ff);
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002472 ps.BindShaderEntitySlotToImage(9, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2473 ps.BindShaderEntitySlotToSampler(9, &sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002474
2475
2476 XglPipelineObj pipelineobj(m_device);
2477 pipelineobj.AddShader(&vs);
2478 pipelineobj.AddShader(&ps);
2479
2480 XglDescriptorSetObj descriptorSet(m_device);
2481 descriptorSet.AttachMemoryView(&redBuffer);
2482 descriptorSet.AttachMemoryView(&greenBuffer);
2483 descriptorSet.AttachMemoryView(&blueBuffer);
2484 descriptorSet.AttachMemoryView(&whiteBuffer);
2485 descriptorSet.AttachImageView(&texture0);
2486 descriptorSet.AttachSampler(&sampler0);
2487 descriptorSet.AttachImageView(&texture2);
2488 descriptorSet.AttachSampler(&sampler2);
2489 descriptorSet.AttachImageView(&texture4);
2490 descriptorSet.AttachSampler(&sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002491 descriptorSet.AttachImageView(&texture7);
2492 descriptorSet.AttachSampler(&sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002493
2494 m_memoryRefManager.AddMemoryRef(&texture0);
2495 m_memoryRefManager.AddMemoryRef(&texture2);
2496 m_memoryRefManager.AddMemoryRef(&texture4);
Cody Northropa0410942014-12-09 13:59:39 -07002497 m_memoryRefManager.AddMemoryRef(&texture7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002498
2499
2500 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2501 QueueCommandBuffer(NULL, 0);
2502
2503}
2504
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002505TEST_F(XglRenderTest, TriangleMatchingSamplerUniformBlockBinding)
2506{
2507 // This test matches binding slots of textures and buffers, requiring
2508 // the driver to give them distinct number spaces.
2509 // The expected result from this test is a red triangle, although
2510 // you can modify it to move the desired result around.
2511
2512 static const char *vertShaderText =
2513 "#version 140\n"
2514 "#extension GL_ARB_separate_shader_objects : enable\n"
2515 "#extension GL_ARB_shading_language_420pack : enable\n"
2516 "void main() {\n"
2517 " vec2 vertices[3];"
2518 " vertices[0] = vec2(-0.5, -0.5);\n"
2519 " vertices[1] = vec2( 0.5, -0.5);\n"
2520 " vertices[2] = vec2( 0.5, 0.5);\n"
2521 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2522 "}\n";
2523
2524 static const char *fragShaderText =
2525 "#version 430\n"
2526 "#extension GL_ARB_separate_shader_objects : enable\n"
2527 "#extension GL_ARB_shading_language_420pack : enable\n"
2528 "layout (binding = 0) uniform sampler2D surface0;\n"
2529 "layout (binding = 1) uniform sampler2D surface1;\n"
2530 "layout (binding = 2) uniform sampler2D surface2;\n"
2531 "layout (binding = 3) uniform sampler2D surface3;\n"
2532 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2533 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2534 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2535 "layout (std140, binding = 3) uniform whiteVal { vec4 white; };"
2536 "layout (location = 0) out vec4 outColor;\n"
2537 "void main() {\n"
2538 " outColor = red;// * vec4(0.00001);\n"
2539 " outColor += white * vec4(0.00001);\n"
2540 " outColor += textureLod(surface1, vec2(0.5), 0.0)* vec4(0.00001);\n"
2541 " outColor += textureLod(surface3, vec2(0.0), 0.0)* vec4(0.00001);\n"
2542 "}\n";
2543 ASSERT_NO_FATAL_FAILURE(InitState());
2544 ASSERT_NO_FATAL_FAILURE(InitViewport());
2545
2546 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2547 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2548
2549 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2550 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2551 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2552 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2553
2554 const int redCount = sizeof(redVals) / sizeof(float);
2555 const int greenCount = sizeof(greenVals) / sizeof(float);
2556 const int blueCount = sizeof(blueVals) / sizeof(float);
2557 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2558
2559 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2560 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2561
2562 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2563 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2564
2565 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2566 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2567
2568 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2569 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2570
2571 XglSamplerObj sampler0(m_device);
2572 XglTextureObj texture0(m_device); // Light Red
2573 texture0.ChangeColors(0xff800000,0xff800000);
2574 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
2575 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2576 XglSamplerObj sampler2(m_device);
2577 XglTextureObj texture2(m_device); // Light Blue
2578 texture2.ChangeColors(0xff000080,0xff000080);
2579 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
2580 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2581 XglSamplerObj sampler4(m_device);
2582 XglTextureObj texture4(m_device); // Light Green
2583 texture4.ChangeColors(0xff008000,0xff008000);
2584 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
2585 ps.BindShaderEntitySlotToSampler(2, &sampler4);
2586 XglSamplerObj sampler7(m_device);
2587 XglTextureObj texture7(m_device); // Red and Blue
2588 texture7.ChangeColors(0xffff00ff,0xffff00ff);
2589 ps.BindShaderEntitySlotToImage(3, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2590 ps.BindShaderEntitySlotToSampler(3, &sampler7);
2591
2592
2593 XglPipelineObj pipelineobj(m_device);
2594 pipelineobj.AddShader(&vs);
2595 pipelineobj.AddShader(&ps);
2596
2597 XglDescriptorSetObj descriptorSet(m_device);
2598 descriptorSet.AttachMemoryView(&redBuffer);
2599 descriptorSet.AttachMemoryView(&greenBuffer);
2600 descriptorSet.AttachMemoryView(&blueBuffer);
2601 descriptorSet.AttachMemoryView(&whiteBuffer);
2602 descriptorSet.AttachImageView(&texture0);
2603 descriptorSet.AttachSampler(&sampler0);
2604 descriptorSet.AttachImageView(&texture2);
2605 descriptorSet.AttachSampler(&sampler2);
2606 descriptorSet.AttachImageView(&texture4);
2607 descriptorSet.AttachSampler(&sampler4);
2608 descriptorSet.AttachImageView(&texture7);
2609 descriptorSet.AttachSampler(&sampler7);
2610
2611 m_memoryRefManager.AddMemoryRef(&texture0);
2612 m_memoryRefManager.AddMemoryRef(&texture2);
2613 m_memoryRefManager.AddMemoryRef(&texture4);
2614 m_memoryRefManager.AddMemoryRef(&texture7);
2615
2616
2617 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2618 QueueCommandBuffer(NULL, 0);
2619
2620}
2621
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002622int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002623 int result;
2624
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002625 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002626 XglTestFramework::InitArgs(&argc, argv);
2627
Chia-I Wu7133fdc2014-12-15 23:57:34 +08002628 ::testing::AddGlobalTestEnvironment(new TestEnvironment);
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002629
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002630 result = RUN_ALL_TESTS();
2631
2632 XglTestFramework::Finish();
2633 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002634}