blob: 7f05d113d7c96fb232d78dcb83f39d11baaa1eb3 [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"
Chia-I Wu4115c892014-08-28 11:56:29 +080076#include "icd-bil.h"
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060077
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -060078#define GLM_FORCE_RADIANS
79#include "glm/glm.hpp"
80#include <glm/gtc/matrix_transform.hpp>
81
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060082#include "xglrenderframework.h"
Tobin Ehlis791a49c2014-11-10 12:29:12 -070083#ifdef DEBUG_CALLBACK
84XGL_VOID XGLAPI myDbgFunc(
85 XGL_DBG_MSG_TYPE msgType,
86 XGL_VALIDATION_LEVEL validationLevel,
87 XGL_BASE_OBJECT srcObject,
88 XGL_SIZE location,
89 XGL_INT msgCode,
90 const XGL_CHAR* pMsg,
91 XGL_VOID* pUserData)
92{
Tobin Ehlis3c26a542014-11-18 11:28:33 -070093 switch (msgType)
94 {
95 case XGL_DBG_MSG_WARNING:
96 printf("CALLBACK WARNING : %s\n", pMsg);
97 break;
98 case XGL_DBG_MSG_ERROR:
99 printf("CALLBACK ERROR : %s\n", pMsg);
100 break;
101 default:
102 printf("EATING Msg of type %u\n", msgType);
103 break;
104 }
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700105}
106#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700107
108
109#undef ASSERT_NO_FATAL_FAILURE
110#define ASSERT_NO_FATAL_FAILURE(x) x
111
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600112//--------------------------------------------------------------------------------------
113// Mesh and VertexFormat Data
114//--------------------------------------------------------------------------------------
115struct Vertex
116{
117 XGL_FLOAT posX, posY, posZ, posW; // Position data
118 XGL_FLOAT r, g, b, a; // Color
119};
120
121#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
122
123static const Vertex g_vbData[] =
124{
125 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
126 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
127 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
128 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
129 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
130 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
131
132 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
133 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
134 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
135 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
136 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
137 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
138
139 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
140 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
141 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
142 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
143 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
144 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
145
146 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
147 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
148 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
149 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
150 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
151 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
152
153 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
154 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
155 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
156 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
157 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
158 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
159
160 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
161 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
162 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
163 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
164 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
165 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
166};
167
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600168static const Vertex g_vb_solid_face_colors_Data[] =
169{
170 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
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
177 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
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
184 { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
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
191 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
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
198 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
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
205 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
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};
212
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600213class XglRenderTest : public XglRenderFramework
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600214{
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600215public:
Cody Northrop4e6b4762014-10-09 21:25:22 -0600216
Tony Barbourf43b6982014-11-25 13:18:32 -0700217 void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText);
218 void RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
Tony Barbour09da2212014-12-03 16:13:23 -0700219 XglConstantBufferObj *constantBuffer);
Tony Barboura3953b82014-12-03 15:46:29 -0700220 void GenericDrawTriangleTest(XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet, int numTris);
Tony Barbourf43b6982014-11-25 13:18:32 -0700221 void QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs);
222
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600223 void InitDepthStencil();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600224 void GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600225 void XGLTriangleTest(const char *vertShaderText, const char *fragShaderText);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600226
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600227
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600228protected:
Cody Northrop350727b2014-10-06 15:42:00 -0600229 XGL_IMAGE m_texture;
230 XGL_IMAGE_VIEW m_textureView;
231 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
232 XGL_GPU_MEMORY m_textureMem;
233
234 XGL_SAMPLER m_sampler;
235
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600236 XGL_FORMAT m_depth_stencil_fmt;
237 XGL_IMAGE m_depthStencilImage;
238 XGL_GPU_MEMORY m_depthStencilMem;
239 XGL_DEPTH_STENCIL_VIEW m_depthStencilView;
Tony Barbourf43b6982014-11-25 13:18:32 -0700240 XglMemoryRefManager m_memoryRefManager;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600241
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600242
243 virtual void SetUp() {
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600244
245 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
246 this->app_info.pNext = NULL;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800247 this->app_info.pAppName = "render_tests";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600248 this->app_info.appVersion = 1;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800249 this->app_info.pEngineName = "unittest";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600250 this->app_info.engineVersion = 1;
251 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
252
Cody Northrop350727b2014-10-06 15:42:00 -0600253 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
254 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barbour97a36232014-12-04 17:14:45 -0700255 memset(&m_depthStencilImage, 0, sizeof(m_depthStencilImage));
Cody Northrop350727b2014-10-06 15:42:00 -0600256
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600257 InitFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600258 }
259
260 virtual void TearDown() {
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600261 // Clean up resources before we reset
262 ShutdownFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600263 }
264};
265
Tony Barbourf43b6982014-11-25 13:18:32 -0700266
Tony Barboura3953b82014-12-03 15:46:29 -0700267void XglRenderTest::GenericDrawTriangleTest(XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet,int numTris)
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600268{
269 XGL_RESULT err = XGL_SUCCESS;
270
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600271 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600272 // Build command buffer
273 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
274 ASSERT_XGL_SUCCESS(err);
275
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600276 GenerateClearAndPrepareBufferCmds();
277 GenerateBindRenderTargetCmd();
Tobin Ehlis266473d2014-12-16 17:34:50 -0700278
Tony Barbourf43b6982014-11-25 13:18:32 -0700279 GenerateBindStateAndPipelineCmds();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600280
Tony Barboura3953b82014-12-03 15:46:29 -0700281 pipelineobj->BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
Tony Barbourbf678472014-12-03 13:58:15 -0700282 descriptorSet->BindCommandBuffer(m_cmdBuffer);
Tobin Ehlis266473d2014-12-16 17:34:50 -0700283#ifdef DUMP_STATE_DOT
284 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
285 pDSDumpDot((char*)"triTest.dot");
286 DRAW_STATE_DUMP_PNG_FILE pDSDumpPng = (DRAW_STATE_DUMP_PNG_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpPngFile");
287 pDSDumpPng((char*)"triTest.png");
288#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700289 // render the triangle
290 xglCmdDraw( m_cmdBuffer, 0, 3*numTris, 0, 1 );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600291
292 // finalize recording of the command buffer
293 err = xglEndCommandBuffer( m_cmdBuffer );
294 ASSERT_XGL_SUCCESS( err );
Tony Barbourf43b6982014-11-25 13:18:32 -0700295}
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600296
Tony Barbourf43b6982014-11-25 13:18:32 -0700297void XglRenderTest::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
298{
299 XGL_RESULT err = XGL_SUCCESS;
Chia-I Wue7748802014-12-05 10:45:15 +0800300 XGL_UINT i;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600301
302 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700303 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, numMemRefs, memRefs, NULL );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600304 ASSERT_XGL_SUCCESS( err );
305
Chia-I Wuf34ac502014-08-27 14:58:05 +0800306 err = xglQueueWaitIdle( m_device->m_queue );
307 ASSERT_XGL_SUCCESS( err );
308
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600309 // Wait for work to finish before cleaning up.
310 xglDeviceWaitIdle(m_device->device());
311
Chia-I Wue7748802014-12-05 10:45:15 +0800312 for (i = 0; i < m_renderTargetCount; i++)
313 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600314}
315
Tony Barbourf43b6982014-11-25 13:18:32 -0700316void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600317{
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600318 XGL_RESULT err;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600319
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600320 ASSERT_NO_FATAL_FAILURE(InitState());
321 ASSERT_NO_FATAL_FAILURE(InitViewport());
322
Tony Barbourf43b6982014-11-25 13:18:32 -0700323 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
324 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600325
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600326
Tony Barbourf43b6982014-11-25 13:18:32 -0700327 XglPipelineObj pipelineobj(m_device);
328 pipelineobj.AddShader(&vs);
329 pipelineobj.AddShader(&ps);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600330
331 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
332
Tony Barbourf43b6982014-11-25 13:18:32 -0700333 // Create descriptor set
334 XglDescriptorSetObj descriptorSet(m_device);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600335
336 // Build command buffer
337 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
338 ASSERT_XGL_SUCCESS(err);
339
340 GenerateClearAndPrepareBufferCmds();
341 GenerateBindRenderTargetCmd();
Tony Barbourf43b6982014-11-25 13:18:32 -0700342 GenerateBindStateAndPipelineCmds();
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600343
Tobin Ehlis266473d2014-12-16 17:34:50 -0700344 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,&descriptorSet);
345 descriptorSet.BindCommandBuffer(m_cmdBuffer);
Tobin Ehlis31446e52014-11-28 11:17:19 -0700346#ifdef DUMP_STATE_DOT
347 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
348 pDSDumpDot((char*)"triUniFS.dot");
349#endif
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++) {
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800381 void *pData = constantBuffer->map();
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600382
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600383 Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
384 MVP = Projection * View * Model;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600385 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
386
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800387 constantBuffer->unmap();
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600388
389 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700390 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_memoryRefManager.GetNumRefs(), m_memoryRefManager.GetMemoryRefList(), NULL );
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600391 ASSERT_XGL_SUCCESS( err );
392
393 err = xglQueueWaitIdle( m_device->m_queue );
394 ASSERT_XGL_SUCCESS( err );
395
396 // Wait for work to finish before cleaning up.
397 xglDeviceWaitIdle(m_device->device());
398
Chia-I Wue7748802014-12-05 10:45:15 +0800399 assert(m_renderTargetCount == 1);
400 RecordImage(m_renderTargets[0]);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600401 }
Cody Northrop7a1f0462014-10-10 14:49:36 -0600402}
403
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600404void dumpMatrix(const char *note, glm::mat4 MVP)
405{
Chia-I Wu7133fdc2014-12-15 23:57:34 +0800406 int i;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600407
408 printf("%s: \n", note);
409 for (i=0; i<4; i++) {
410 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
411 }
412 printf("\n");
413 fflush(stdout);
414}
415
416void dumpVec4(const char *note, glm::vec4 vector)
417{
418 printf("%s: \n", note);
419 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
420 printf("\n");
421 fflush(stdout);
422}
423
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600424void XglRenderTest::GenerateClearAndPrepareBufferCmds()
425{
426 XglRenderFramework::GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600427
Tony Barbour97a36232014-12-04 17:14:45 -0700428 if (m_depthStencilImage) {
Tony Barbour97a36232014-12-04 17:14:45 -0700429 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
430 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
431 dsRange.baseMipLevel = 0;
432 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
433 dsRange.baseArraySlice = 0;
434 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
Cody Northrop7a1f0462014-10-10 14:49:36 -0600435
Tony Barbour97a36232014-12-04 17:14:45 -0700436 // prepare the depth buffer for clear
437 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
438 transitionToClear.image = m_depthStencilImage;
439 transitionToClear.oldState = m_depthStencilBinding.depthState;
440 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
441 transitionToClear.subresourceRange = dsRange;
442 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
443 m_depthStencilBinding.depthState = transitionToClear.newState;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600444
Tony Barbour97a36232014-12-04 17:14:45 -0700445 xglCmdClearDepthStencil(m_cmdBuffer, m_depthStencilImage, 1.0f, 0, 1, &dsRange);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600446
Tony Barbour97a36232014-12-04 17:14:45 -0700447 // prepare depth buffer for rendering
448 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
Chia-I Wu18749b02014-12-05 10:48:20 +0800449 transitionToRender.image = m_depthStencilImage;
Tony Barbour97a36232014-12-04 17:14:45 -0700450 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
451 transitionToRender.newState = m_depthStencilBinding.depthState;
452 transitionToRender.subresourceRange = dsRange;
453 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
454 m_depthStencilBinding.depthState = transitionToClear.newState;
455 }
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600456}
457
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600458void XglRenderTest::InitDepthStencil()
459{
460 XGL_RESULT err;
461 XGL_IMAGE_CREATE_INFO image;
462 XGL_MEMORY_ALLOC_INFO mem_alloc;
463 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
464 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn6317c492014-11-21 11:33:20 -0700465 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600466
467 // Clean up default state created by framework
468 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
469
470 m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16;
471 m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS;
472
473 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
474 image.pNext = NULL;
475 image.imageType = XGL_IMAGE_2D;
476 image.format = m_depth_stencil_fmt;
477 image.extent.width = m_width;
478 image.extent.height = m_height;
479 image.extent.depth = 1;
480 image.mipLevels = 1;
481 image.arraySize = 1;
482 image.samples = 1;
483 image.tiling = XGL_OPTIMAL_TILING;
484 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
485 image.flags = 0;
486
487 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
488 mem_alloc.pNext = NULL;
489 mem_alloc.allocationSize = 0;
490 mem_alloc.alignment = 0;
491 mem_alloc.flags = 0;
492 mem_alloc.heapCount = 0;
493 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
494
495 /* create image */
496 err = xglCreateImage(device(), &image,
497 &m_depthStencilImage);
498 ASSERT_XGL_SUCCESS(err);
499
500 err = xglGetObjectInfo(m_depthStencilImage,
501 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
502 &mem_reqs_size, &mem_reqs);
503 ASSERT_XGL_SUCCESS(err);
504 ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs));
505
506 mem_alloc.allocationSize = mem_reqs.size;
507 mem_alloc.alignment = mem_reqs.alignment;
508 mem_alloc.heapCount = mem_reqs.heapCount;
509 memcpy(mem_alloc.heaps, mem_reqs.heaps,
510 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
511
512 /* allocate memory */
513 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem);
514 ASSERT_XGL_SUCCESS(err);
515
516 /* bind memory */
517 err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0);
518 ASSERT_XGL_SUCCESS(err);
519
520 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
521 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
522 depthStencil.depthTestEnable = XGL_TRUE;
523 depthStencil.depthWriteEnable = XGL_TRUE;
524 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
525 depthStencil.depthBoundsEnable = XGL_FALSE;
526 depthStencil.minDepth = 0.f;
527 depthStencil.maxDepth = 1.f;
528 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
529 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
530 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
531 depthStencil.back.stencilRef = 0x00;
532 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
533 depthStencil.front = depthStencil.back;
534
535 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
536 ASSERT_XGL_SUCCESS( err );
537
538 /* create image view */
539 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
540 view.pNext = NULL;
541 view.image = XGL_NULL_HANDLE;
542 view.mipLevel = 0;
543 view.baseArraySlice = 0;
544 view.arraySize = 1;
545 view.flags = 0;
546 view.image = m_depthStencilImage;
547 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
548 ASSERT_XGL_SUCCESS(err);
549
550 m_depthStencilBinding.view = m_depthStencilView;
551 m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
552 m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
553}
554
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600555struct xgltriangle_vs_uniform {
556 // Must start with MVP
557 XGL_FLOAT mvp[4][4];
558 XGL_FLOAT position[3][4];
559 XGL_FLOAT color[3][4];
560};
561
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600562void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600563{
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700564#ifdef DEBUG_CALLBACK
565 xglDbgRegisterMsgCallback(myDbgFunc, NULL);
566#endif
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600567 // Create identity matrix
568 int i;
569 struct xgltriangle_vs_uniform data;
570
571 glm::mat4 Projection = glm::mat4(1.0f);
572 glm::mat4 View = glm::mat4(1.0f);
573 glm::mat4 Model = glm::mat4(1.0f);
574 glm::mat4 MVP = Projection * View * Model;
575 const int matrixSize = sizeof(MVP);
576 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT);
577 memcpy(&data.mvp, &MVP[0][0], matrixSize);
578
579 static const Vertex tri_data[] =
580 {
581 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
582 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
583 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
584 };
585
586 for (i=0; i<3; i++) {
587 data.position[i][0] = tri_data[i].posX;
588 data.position[i][1] = tri_data[i].posY;
589 data.position[i][2] = tri_data[i].posZ;
590 data.position[i][3] = tri_data[i].posW;
591 data.color[i][0] = tri_data[i].r;
592 data.color[i][1] = tri_data[i].g;
593 data.color[i][2] = tri_data[i].b;
594 data.color[i][3] = tri_data[i].a;
595 }
596
Tony Barbourf43b6982014-11-25 13:18:32 -0700597 ASSERT_NO_FATAL_FAILURE(InitState());
598 ASSERT_NO_FATAL_FAILURE(InitViewport());
599
600 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data);
601
602 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
603 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
604 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
605
606 XglPipelineObj pipelineobj(m_device);
607 pipelineobj.AddShader(&vs);
608 pipelineobj.AddShader(&ps);
609
610 XglDescriptorSetObj descriptorSet(m_device);
611 descriptorSet.AttachMemoryView(&constantBuffer);
612 m_memoryRefManager.AddMemoryRef(&constantBuffer);
613
Tony Barboura3953b82014-12-03 15:46:29 -0700614 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -0700615 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
616
Tony Barbour09da2212014-12-03 16:13:23 -0700617 RotateTriangleVSUniform(Projection, View, Model, &constantBuffer);
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700618#ifdef PRINT_OBJECTS
619 //XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)
620 OBJ_TRACK_GET_OBJECT_COUNT pObjTrackGetObjectCount = (OBJ_TRACK_GET_OBJECT_COUNT)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjectCount");
621 XGL_UINT64 numObjects = pObjTrackGetObjectCount(XGL_OBJECT_TYPE_ANY);
622 //OBJ_TRACK_GET_OBJECTS pGetObjsFunc = xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
623 printf("DEBUG : Number of Objects : %lu\n", numObjects);
624 OBJ_TRACK_GET_OBJECTS pObjTrackGetObjs = (OBJ_TRACK_GET_OBJECTS)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
625 OBJTRACK_NODE* pObjNodeArray = (OBJTRACK_NODE*)malloc(sizeof(OBJTRACK_NODE)*numObjects);
626 pObjTrackGetObjs(XGL_OBJECT_TYPE_ANY, numObjects, pObjNodeArray);
627 for (i=0; i < numObjects; i++) {
628 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);
629 }
630 free(pObjNodeArray);
631#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700632
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600633}
634
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600635TEST_F(XglRenderTest, XGLTriangle_FragColor)
636{
637 static const char *vertShaderText =
638 "#version 140\n"
639 "#extension GL_ARB_separate_shader_objects : enable\n"
640 "#extension GL_ARB_shading_language_420pack : enable\n"
641 "\n"
642 "layout(binding = 0) uniform buf {\n"
643 " mat4 MVP;\n"
644 " vec4 position[3];\n"
645 " vec4 color[3];\n"
646 "} ubuf;\n"
647 "\n"
648 "layout (location = 0) out vec4 outColor;\n"
649 "\n"
650 "void main() \n"
651 "{\n"
652 " outColor = ubuf.color[gl_VertexID];\n"
653 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
654 "}\n";
655
656 static const char *fragShaderText =
657 "#version 140\n"
658 "#extension GL_ARB_separate_shader_objects : enable\n"
659 "#extension GL_ARB_shading_language_420pack : enable\n"
660 "\n"
661 "layout (location = 0) in vec4 inColor;\n"
662 "\n"
663 "void main()\n"
664 "{\n"
665 " gl_FragColor = inColor;\n"
666 "}\n";
667
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600668 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600669 XGLTriangleTest(vertShaderText, fragShaderText);
670}
671
672TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
673{
674 static const char *vertShaderText =
675 "#version 140\n"
676 "#extension GL_ARB_separate_shader_objects : enable\n"
677 "#extension GL_ARB_shading_language_420pack : enable\n"
678 "\n"
679 "layout(binding = 0) uniform buf {\n"
680 " mat4 MVP;\n"
681 " vec4 position[3];\n"
682 " vec4 color[3];\n"
683 "} ubuf;\n"
684 "\n"
685 "layout (location = 0) out vec4 outColor;\n"
686 "\n"
687 "void main() \n"
688 "{\n"
689 " outColor = ubuf.color[gl_VertexID];\n"
690 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
691 "}\n";
692
693 static const char *fragShaderText =
694 "#version 140\n"
695 "#extension GL_ARB_separate_shader_objects : enable\n"
696 "#extension GL_ARB_shading_language_420pack : enable\n"
697 "\n"
698 "layout (location = 0) in vec4 inColor;\n"
699 "layout (location = 0) out vec4 outColor;\n"
700 "\n"
701 "void main()\n"
702 "{\n"
703 " outColor = inColor;\n"
704 "}\n";
705
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600706 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 -0600707
708 XGLTriangleTest(vertShaderText, fragShaderText);
709}
710
Tony Barbourf43b6982014-11-25 13:18:32 -0700711TEST_F(XglRenderTest, BIL_XGLTriangle)
712{
713 bool saved_use_bil = XglTestFramework::m_use_bil;
714
715 static const char *vertShaderText =
716 "#version 140\n"
717 "#extension GL_ARB_separate_shader_objects : enable\n"
718 "#extension GL_ARB_shading_language_420pack : enable\n"
719 "\n"
720 "layout(binding = 0) uniform buf {\n"
721 " mat4 MVP;\n"
722 " vec4 position[3];\n"
723 " vec4 color[3];\n"
724 "} ubuf;\n"
725 "\n"
726 "layout (location = 0) out vec4 outColor;\n"
727 "\n"
728 "void main() \n"
729 "{\n"
730 " outColor = ubuf.color[gl_VertexID];\n"
731 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
732 "}\n";
733
734 static const char *fragShaderText =
735 "#version 140\n"
736 "#extension GL_ARB_separate_shader_objects : enable\n"
737 "#extension GL_ARB_shading_language_420pack : enable\n"
738 "\n"
739 "layout (location = 0) in vec4 inColor;\n"
740 "\n"
741 "void main()\n"
742 "{\n"
743 " gl_FragColor = inColor;\n"
744 "}\n";
745
746 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver.");
747
748 XglTestFramework::m_use_bil = true;
749
750 XGLTriangleTest(vertShaderText, fragShaderText);
751
752 XglTestFramework::m_use_bil = saved_use_bil;
753}
754
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600755TEST_F(XglRenderTest, GreenTriangle)
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600756{
757 static const char *vertShaderText =
758 "#version 130\n"
759 "vec2 vertices[3];\n"
760 "void main() {\n"
761 " vertices[0] = vec2(-1.0, -1.0);\n"
762 " vertices[1] = vec2( 1.0, -1.0);\n"
763 " vertices[2] = vec2( 0.0, 1.0);\n"
764 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
765 "}\n";
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600766
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600767 static const char *fragShaderText =
768 "#version 130\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600769 "void main() {\n"
Steve K10b32512014-10-10 08:54:29 -0600770 " gl_FragColor = vec4(0,1,0,1);\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600771 "}\n";
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600772
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600773 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
774
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600775 DrawTriangleTest(vertShaderText, fragShaderText);
776}
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600777
Tony Barbourf43b6982014-11-25 13:18:32 -0700778TEST_F(XglRenderTest, BIL_GreenTriangle)
779{
780 bool saved_use_bil = XglTestFramework::m_use_bil;
781
782 static const char *vertShaderText =
783 "#version 130\n"
784 "vec2 vertices[3];\n"
785 "void main() {\n"
786 " vertices[0] = vec2(-1.0, -1.0);\n"
787 " vertices[1] = vec2( 1.0, -1.0);\n"
788 " vertices[2] = vec2( 0.0, 1.0);\n"
789 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
790 "}\n";
791
792 static const char *fragShaderText =
793 "#version 130\n"
794 "void main() {\n"
795 " gl_FragColor = vec4(0,1,0,1);\n"
796 "}\n";
797
798 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver.");
799
800 XglTestFramework::m_use_bil = true;
801 DrawTriangleTest(vertShaderText, fragShaderText);
802 XglTestFramework::m_use_bil = saved_use_bil;
803}
804
805TEST_F(XglRenderTest, YellowTriangle)
806{
807 static const char *vertShaderText =
808 "#version 130\n"
809 "void main() {\n"
810 " vec2 vertices[3];"
811 " vertices[0] = vec2(-0.5, -0.5);\n"
812 " vertices[1] = vec2( 0.5, -0.5);\n"
813 " vertices[2] = vec2( 0.5, 0.5);\n"
814 " vec4 colors[3];\n"
815 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
816 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
817 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
818 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
819 "}\n";
820
821 static const char *fragShaderText =
822 "#version 130\n"
823 "void main() {\n"
824 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
825 "}\n";
826
827 DrawTriangleTest(vertShaderText, fragShaderText);
828}
829
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600830TEST_F(XglRenderTest, TriangleWithVertexFetch)
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600831{
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600832 static const char *vertShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700833 "#version 130\n"
834 //XYZ1( -1, -1, -1 )
835 "in vec4 pos;\n"
836 //XYZ1( 0.f, 0.f, 0.f )
837 "in vec4 inColor;\n"
838 "out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600839 "void main() {\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -0600840 " outColor = inColor;\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600841 " gl_Position = pos;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600842 "}\n";
843
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600844
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600845 static const char *fragShaderText =
Cody Northrop68a10f62014-12-05 15:44:14 -0700846 "#version 140\n"
847 "#extension GL_ARB_separate_shader_objects : enable\n"
848 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -0700849 "in vec4 color;\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700850 "layout (location = 0) out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600851 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700852 " outColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600853 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600854
Tony Barbourf43b6982014-11-25 13:18:32 -0700855
856
857 ASSERT_NO_FATAL_FAILURE(InitState());
858 ASSERT_NO_FATAL_FAILURE(InitViewport());
859
860 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -0700861 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -0700862
863 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
864 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
865
866 XglPipelineObj pipelineobj(m_device);
867 pipelineobj.AddShader(&vs);
868 pipelineobj.AddShader(&ps);
869
870 XglDescriptorSetObj descriptorSet(m_device);
871 descriptorSet.AttachMemoryView(&meshBuffer);
872
873 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
874 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
875 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
876 };
877
878 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
879 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
880 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
881 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
882 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
883 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
884 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
885 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
886 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
887
888 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
889 pipelineobj.AddVertexInputBindings(&vi_binding,1);
890 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
891
Tony Barboura3953b82014-12-03 15:46:29 -0700892 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -0700893 QueueCommandBuffer(NULL, 0);
894
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600895}
896
Chia-I Wue09d1a72014-12-05 10:32:23 +0800897TEST_F(XglRenderTest, TriangleMRT)
898{
899 static const char *vertShaderText =
900 "#version 130\n"
901 "in vec4 pos;\n"
902 "void main() {\n"
903 " gl_Position = pos;\n"
904 "}\n";
905
906 static const char *fragShaderText =
907 "#version 130\n"
908 "void main() {\n"
909 " gl_FragData[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
910 " gl_FragData[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
911 "}\n";
912 const XGL_FLOAT vb_data[][2] = {
913 { -1.0f, -1.0f },
914 { 1.0f, -1.0f },
915 { -1.0f, 1.0f }
916 };
917
918 ASSERT_NO_FATAL_FAILURE(InitState());
919 ASSERT_NO_FATAL_FAILURE(InitViewport());
920
921 XglConstantBufferObj meshBuffer(m_device, sizeof(vb_data) / sizeof(vb_data[0]), sizeof(vb_data[0]), vb_data);
Tony Barboure6152042014-12-10 17:40:15 -0700922 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Chia-I Wue09d1a72014-12-05 10:32:23 +0800923
924 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
925 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
926
927 XglPipelineObj pipelineobj(m_device);
928 pipelineobj.AddShader(&vs);
929 pipelineobj.AddShader(&ps);
930
931 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
932 sizeof(vb_data[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
933 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
934 };
935
936 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attrib;
937 vi_attrib.binding = 0; // index into vertexBindingDescriptions
938 vi_attrib.format.channelFormat = XGL_CH_FMT_R32G32; // format of source data
939 vi_attrib.format.numericFormat = XGL_NUM_FMT_FLOAT;
940 vi_attrib.offsetInBytes = 0; // Offset of first element in bytes from base of vertex
941
942 pipelineobj.AddVertexInputAttribs(&vi_attrib, 1);
943 pipelineobj.AddVertexInputBindings(&vi_binding,1);
944 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
945
946 XglDescriptorSetObj descriptorSet(m_device);
947
948 m_renderTargetCount = 2;
949
950 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
951 att.blendEnable = XGL_FALSE;
952 att.format = m_render_target_fmt;
953 att.channelWriteMask = 0xf;
954 pipelineobj.SetColorAttachment(1, &att);
955
956 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
957 QueueCommandBuffer(NULL, 0);
958}
959
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700960TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
961{
962 static const char *vertShaderText =
963 "#version 140\n"
964 "#extension GL_ARB_separate_shader_objects : enable\n"
965 "#extension GL_ARB_shading_language_420pack : enable\n"
966 "layout(location = 0) in vec4 pos;\n"
967 "layout(location = 1) in vec4 inColor;\n"
968 "layout(location = 0) out vec4 outColor;\n"
969 "void main() {\n"
970 " outColor = inColor;\n"
971 " gl_Position = pos;\n"
972 "}\n";
973
974
975 static const char *fragShaderText =
976 "#version 140\n"
977 "#extension GL_ARB_separate_shader_objects : enable\n"
978 "#extension GL_ARB_shading_language_420pack : enable\n"
979 "layout(location = 0) in vec4 color;\n"
980 "void main() {\n"
981 " gl_FragColor = color;\n"
982 "}\n";
983
984 const Vertex g_vbData[] =
985 {
986 // first tri
987 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
988 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
989 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
990
991 // second tri
992 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
993 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
994 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
995 };
996
997 const uint16_t g_idxData[6] = {
998 0, 1, 2,
999 3, 4, 5,
1000 };
1001
1002 ASSERT_NO_FATAL_FAILURE(InitState());
1003 ASSERT_NO_FATAL_FAILURE(InitViewport());
1004
1005 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001006 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001007
1008 XglIndexBufferObj indexBuffer(m_device);
1009 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
Tony Barboure6152042014-12-10 17:40:15 -07001010 indexBuffer.SetMemoryState(XGL_MEMORY_STATE_INDEX_DATA);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001011
1012 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1013 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1014
1015 XglPipelineObj pipelineobj(m_device);
1016 pipelineobj.AddShader(&vs);
1017 pipelineobj.AddShader(&ps);
1018
1019 XglDescriptorSetObj descriptorSet(m_device);
1020
1021 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1022 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1023 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1024 };
1025
1026 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1027 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1028 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1029 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1030 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1031 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1032 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1033 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1034 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1035
1036 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1037 pipelineobj.AddVertexInputBindings(&vi_binding,1);
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001038 pipelineobj.CreateXGLPipeline(&descriptorSet);
1039 descriptorSet.CreateXGLDescriptorSet();
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001040
1041 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001042 XglCommandBufferObj cmdBuffer(m_device);
1043 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1044 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
1045 cmdBuffer.ClearAllBuffers(NULL, NULL);
1046 cmdBuffer.BindAttachments(NULL);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001047
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001048#ifdef DUMP_STATE_DOT
1049 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
Tobin Ehlis266473d2014-12-16 17:34:50 -07001050 pDSDumpDot((char*)"triTest2.dot");
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001051#endif
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001052 cmdBuffer.BindState(m_stateRaster, m_stateViewport, m_colorBlend, m_stateDepthStencil, m_stateMsaa);
1053 cmdBuffer.BindPipeline(pipelineobj.GetPipelineHandle());
1054 cmdBuffer.BindDescriptorSet(descriptorSet.GetDescriptorSetHandle());
1055 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1056 cmdBuffer.BindIndexBuffer(&indexBuffer,0);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001057
1058 // render two triangles
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001059 cmdBuffer.DrawIndexed(0, 6, 0, 0, 1);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001060
1061 // finalize recording of the command buffer
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001062 cmdBuffer.EndCommandBuffer();
1063 cmdBuffer.QueueCommandBuffer(NULL, 0);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001064
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001065 for (int i = 0; i < m_renderTargetCount; i++)
1066 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001067
1068}
1069
GregF6bef1212014-12-02 15:41:44 -07001070TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1071{
1072 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001073
GregF6bef1212014-12-02 15:41:44 -07001074 static const char *vertShaderText =
1075 "#version 140\n"
1076 "#extension GL_ARB_separate_shader_objects : enable\n"
1077 "#extension GL_ARB_shading_language_420pack : enable\n"
1078 "layout (location = 0) in vec4 pos;\n"
1079 "layout (location = 0) out vec4 outColor;\n"
1080 "layout (location = 1) out vec4 outColor2;\n"
1081 "void main() {\n"
1082 " gl_Position = pos;\n"
1083 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1084 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1085 "}\n";
1086
1087 static const char *fragShaderText =
1088 //"#version 140\n"
1089 "#version 330\n"
1090 "#extension GL_ARB_separate_shader_objects : enable\n"
1091 "#extension GL_ARB_shading_language_420pack : enable\n"
1092 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1093 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1094 "layout (location = 0) in vec4 color;\n"
1095 "layout (location = 1) in vec4 color2;\n"
1096 "void main() {\n"
1097 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1098 " float dist_squared = dot(pos, pos);\n"
1099 " gl_FragColor = (dist_squared < 400.0)\n"
1100 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1101 " : color2;\n"
1102 "}\n";
1103
1104 ASSERT_NO_FATAL_FAILURE(InitState());
1105 ASSERT_NO_FATAL_FAILURE(InitViewport());
1106
1107 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001108 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001109
1110 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1111 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1112
1113 XglPipelineObj pipelineobj(m_device);
1114 pipelineobj.AddShader(&vs);
1115 pipelineobj.AddShader(&ps);
1116
1117 XglDescriptorSetObj descriptorSet(m_device);
1118 descriptorSet.AttachMemoryView(&meshBuffer);
1119
1120 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1121 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1122 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1123 };
1124
1125 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1126 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1127 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1128 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1129 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1130 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1131 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1132 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1133 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1134
1135 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1136 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1137 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1138
Tony Barbour09da2212014-12-03 16:13:23 -07001139 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001140 QueueCommandBuffer(NULL, 0);
1141
1142}
1143
1144TEST_F(XglRenderTest, RedCirclesonBlue)
1145{
1146 // This tests that we correctly handle unread fragment inputs
1147
1148 static const char *vertShaderText =
1149 "#version 140\n"
1150 "#extension GL_ARB_separate_shader_objects : enable\n"
1151 "#extension GL_ARB_shading_language_420pack : enable\n"
1152 "layout (location = 0) in vec4 pos;\n"
1153 "layout (location = 0) out vec4 outColor;\n"
1154 "layout (location = 1) out vec4 outColor2;\n"
1155 "void main() {\n"
1156 " gl_Position = pos;\n"
1157 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1158 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1159 "}\n";
1160
1161 static const char *fragShaderText =
1162 //"#version 140\n"
1163 "#version 330\n"
1164 "#extension GL_ARB_separate_shader_objects : enable\n"
1165 "#extension GL_ARB_shading_language_420pack : enable\n"
1166 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1167 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1168 "layout (location = 0) in vec4 color;\n"
1169 "layout (location = 1) in vec4 color2;\n"
1170 "void main() {\n"
1171 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1172 " float dist_squared = dot(pos, pos);\n"
1173 " gl_FragColor = (dist_squared < 400.0)\n"
1174 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1175 " : color2;\n"
1176 "}\n";
1177
1178 ASSERT_NO_FATAL_FAILURE(InitState());
1179 ASSERT_NO_FATAL_FAILURE(InitViewport());
1180
1181 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001182 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001183
1184 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1185 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1186
1187 XglPipelineObj pipelineobj(m_device);
1188 pipelineobj.AddShader(&vs);
1189 pipelineobj.AddShader(&ps);
1190
1191 XglDescriptorSetObj descriptorSet(m_device);
1192 descriptorSet.AttachMemoryView(&meshBuffer);
1193
1194 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1195 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1196 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1197 };
1198
1199 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1200 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1201 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1202 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1203 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1204 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1205 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1206 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1207 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1208
1209 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1210 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1211 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1212
Tony Barbour09da2212014-12-03 16:13:23 -07001213 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001214 QueueCommandBuffer(NULL, 0);
1215
1216}
1217
1218TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1219{
1220 // This tests reading gl_ClipDistance from FS
1221
1222 static const char *vertShaderText =
1223 "#version 330\n"
1224 "#extension GL_ARB_separate_shader_objects : enable\n"
1225 "#extension GL_ARB_shading_language_420pack : enable\n"
1226 "out gl_PerVertex {\n"
1227 " vec4 gl_Position;\n"
1228 " float gl_ClipDistance[1];\n"
1229 "};\n"
1230 "layout (location = 0) in vec4 pos;\n"
1231 "layout (location = 0) out vec4 outColor;\n"
1232 "layout (location = 1) out vec4 outColor2;\n"
1233 "void main() {\n"
1234 " gl_Position = pos;\n"
1235 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1236 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1237 " float dists[3];\n"
1238 " dists[0] = 0.0;\n"
1239 " dists[1] = 1.0;\n"
1240 " dists[2] = 1.0;\n"
1241 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1242 "}\n";
1243
1244
1245 static const char *fragShaderText =
1246 //"#version 140\n"
1247 "#version 330\n"
1248 "#extension GL_ARB_separate_shader_objects : enable\n"
1249 "#extension GL_ARB_shading_language_420pack : enable\n"
1250 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1251 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1252 "layout (location = 0) in vec4 color;\n"
1253 "layout (location = 1) in vec4 color2;\n"
1254 "void main() {\n"
1255 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1256 " float dist_squared = dot(pos, pos);\n"
1257 " gl_FragColor = (dist_squared < 400.0)\n"
1258 " ? color * gl_ClipDistance[0]\n"
1259 " : color2;\n"
1260 "}\n";
1261
1262 ASSERT_NO_FATAL_FAILURE(InitState());
1263 ASSERT_NO_FATAL_FAILURE(InitViewport());
1264
1265 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001266 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001267
1268 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1269 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1270
1271 XglPipelineObj pipelineobj(m_device);
1272 pipelineobj.AddShader(&vs);
1273 pipelineobj.AddShader(&ps);
1274
1275 XglDescriptorSetObj descriptorSet(m_device);
1276 descriptorSet.AttachMemoryView(&meshBuffer);
1277
1278 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1279 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1280 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1281 };
1282
1283 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1284 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1285 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1286 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1287 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1288 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1289 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1290 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1291 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1292
1293 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1294 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1295 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1296
Tony Barboura3953b82014-12-03 15:46:29 -07001297 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001298 QueueCommandBuffer(NULL, 0);
1299
1300}
Tony Barbourf43b6982014-11-25 13:18:32 -07001301
GregF7a23c792014-12-02 17:19:34 -07001302TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1303{
1304 static const char *vertShaderText =
1305 "#version 140\n"
1306 "#extension GL_ARB_separate_shader_objects : enable\n"
1307 "#extension GL_ARB_shading_language_420pack : enable\n"
1308 "layout (location = 0) in vec4 pos;\n"
1309 "layout (location = 0) out vec4 outColor;\n"
1310 "layout (location = 1) out vec4 outColor2;\n"
1311 "void main() {\n"
1312 " gl_Position = pos;\n"
1313 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1314 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1315 "}\n";
1316
1317
1318 static const char *fragShaderText =
1319 //"#version 140\n"
1320 "#version 330\n"
1321 "#extension GL_ARB_separate_shader_objects : enable\n"
1322 "#extension GL_ARB_shading_language_420pack : enable\n"
1323 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1324 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1325 "layout (location = 0) in vec4 color;\n"
1326 "layout (location = 1) in vec4 color2;\n"
1327 "void main() {\n"
1328 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1329 " float dist_squared = dot(pos, pos);\n"
1330 " if (dist_squared < 100.0)\n"
1331 " discard;\n"
1332 " gl_FragColor = (dist_squared < 400.0)\n"
1333 " ? color\n"
1334 " : color2;\n"
1335 "}\n";
1336
1337 ASSERT_NO_FATAL_FAILURE(InitState());
1338 ASSERT_NO_FATAL_FAILURE(InitViewport());
1339
1340 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001341 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF7a23c792014-12-02 17:19:34 -07001342
1343 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1344 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1345
1346 XglPipelineObj pipelineobj(m_device);
1347 pipelineobj.AddShader(&vs);
1348 pipelineobj.AddShader(&ps);
1349
1350 XglDescriptorSetObj descriptorSet(m_device);
1351 descriptorSet.AttachMemoryView(&meshBuffer);
1352
1353 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1354 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1355 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1356 };
1357
1358 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1359 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1360 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1361 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1362 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1363 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1364 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1365 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1366 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1367
1368 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1369 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1370 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1371
Tony Barbour09da2212014-12-03 16:13:23 -07001372 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF7a23c792014-12-02 17:19:34 -07001373 QueueCommandBuffer(NULL, 0);
1374
1375}
1376
1377
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001378TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001379{
1380 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001381 "#version 140\n"
1382 "#extension GL_ARB_separate_shader_objects : enable\n"
1383 "#extension GL_ARB_shading_language_420pack : enable\n"
1384 "\n"
1385 "layout(binding = 0) uniform buf {\n"
1386 " mat4 MVP;\n"
1387 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001388 "void main() {\n"
1389 " vec2 vertices[3];"
1390 " vertices[0] = vec2(-0.5, -0.5);\n"
1391 " vertices[1] = vec2( 0.5, -0.5);\n"
1392 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001393 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001394 "}\n";
1395
1396 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001397 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001398 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001399 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001400 "}\n";
1401
Tony Barbourf43b6982014-11-25 13:18:32 -07001402 ASSERT_NO_FATAL_FAILURE(InitState());
1403 ASSERT_NO_FATAL_FAILURE(InitViewport());
1404
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001405 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001406 glm::mat4 Projection = glm::mat4(1.0f);
1407 glm::mat4 View = glm::mat4(1.0f);
1408 glm::mat4 Model = glm::mat4(1.0f);
1409 glm::mat4 MVP = Projection * View * Model;
1410 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1411
Tony Barbourf43b6982014-11-25 13:18:32 -07001412 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1413 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1414 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001415
Tony Barbourf43b6982014-11-25 13:18:32 -07001416 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1417
1418 XglPipelineObj pipelineobj(m_device);
1419 pipelineobj.AddShader(&vs);
1420 pipelineobj.AddShader(&ps);
1421
1422 // Create descriptor set and attach the constant buffer to it
1423 XglDescriptorSetObj descriptorSet(m_device);
1424 descriptorSet.AttachMemoryView(&MVPBuffer);
1425
1426 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1427
Tony Barboura3953b82014-12-03 15:46:29 -07001428 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001429 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1430
Tony Barbour09da2212014-12-03 16:13:23 -07001431 RotateTriangleVSUniform(Projection, View, Model, &MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001432}
1433
Tony Barbourf43b6982014-11-25 13:18:32 -07001434TEST_F(XglRenderTest, MixTriangle)
1435{
1436 // This tests location applied to varyings. Notice that we have switched foo
1437 // and bar in the FS. The triangle should be blended with red, green and blue
1438 // corners.
1439 static const char *vertShaderText =
1440 "#version 140\n"
1441 "#extension GL_ARB_separate_shader_objects : enable\n"
1442 "#extension GL_ARB_shading_language_420pack : enable\n"
1443 "layout (location=0) out vec4 bar;\n"
1444 "layout (location=1) out vec4 foo;\n"
1445 "layout (location=2) out float scale;\n"
1446 "vec2 vertices[3];\n"
1447 "void main() {\n"
1448 " vertices[0] = vec2(-1.0, -1.0);\n"
1449 " vertices[1] = vec2( 1.0, -1.0);\n"
1450 " vertices[2] = vec2( 0.0, 1.0);\n"
1451 "vec4 colors[3];\n"
1452 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1453 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1454 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1455 " foo = colors[gl_VertexID % 3];\n"
1456 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1457 " scale = 1.0;\n"
1458 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1459 "}\n";
1460
1461 static const char *fragShaderText =
1462 "#version 140\n"
1463 "#extension GL_ARB_separate_shader_objects : enable\n"
1464 "#extension GL_ARB_shading_language_420pack : enable\n"
1465 "layout (location = 1) in vec4 bar;\n"
1466 "layout (location = 0) in vec4 foo;\n"
1467 "layout (location = 2) in float scale;\n"
1468 "void main() {\n"
1469 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1470 "}\n";
1471
1472 ASSERT_NO_FATAL_FAILURE(InitState());
1473 ASSERT_NO_FATAL_FAILURE(InitViewport());
1474
1475 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1476 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1477
1478 XglPipelineObj pipelineobj(m_device);
1479 pipelineobj.AddShader(&vs);
1480 pipelineobj.AddShader(&ps);
1481
1482 XglDescriptorSetObj descriptorSet(m_device);
1483
Tony Barboura3953b82014-12-03 15:46:29 -07001484 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001485 QueueCommandBuffer(NULL, 0);
1486}
1487
1488TEST_F(XglRenderTest, TriVertFetchAndVertID)
1489{
1490 // This tests that attributes work in the presence of gl_VertexID
1491
1492 static const char *vertShaderText =
1493 "#version 140\n"
1494 "#extension GL_ARB_separate_shader_objects : enable\n"
1495 "#extension GL_ARB_shading_language_420pack : enable\n"
1496 //XYZ1( -1, -1, -1 )
1497 "layout (location = 0) in vec4 pos;\n"
1498 //XYZ1( 0.f, 0.f, 0.f )
1499 "layout (location = 1) in vec4 inColor;\n"
1500 "layout (location = 0) out vec4 outColor;\n"
1501 "void main() {\n"
1502 " outColor = inColor;\n"
1503 " vec4 vertices[3];"
1504 " vertices[gl_VertexID % 3] = pos;\n"
1505 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1506 "}\n";
1507
1508
1509 static const char *fragShaderText =
1510 "#version 140\n"
1511 "#extension GL_ARB_separate_shader_objects : enable\n"
1512 "#extension GL_ARB_shading_language_420pack : enable\n"
1513 "layout (location = 0) in vec4 color;\n"
1514 "void main() {\n"
1515 " gl_FragColor = color;\n"
1516 "}\n";
1517
1518 ASSERT_NO_FATAL_FAILURE(InitState());
1519 ASSERT_NO_FATAL_FAILURE(InitViewport());
1520
1521 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001522 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001523
1524 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1525 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1526
1527 XglPipelineObj pipelineobj(m_device);
1528 pipelineobj.AddShader(&vs);
1529 pipelineobj.AddShader(&ps);
1530
1531 XglDescriptorSetObj descriptorSet(m_device);
1532 descriptorSet.AttachMemoryView(&meshBuffer);
1533
1534 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1535 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1536 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1537 };
1538
1539 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1540 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1541 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1542 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1543 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1544 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1545 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1546 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1547 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1548
1549 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1550 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1551 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1552
Tony Barboura3953b82014-12-03 15:46:29 -07001553 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001554 QueueCommandBuffer(NULL, 0);
1555}
1556
1557TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1558{
1559 // This tests that attributes work in the presence of gl_VertexID
1560 // and a dead attribute in position 0. Draws a triangle with yellow,
1561 // red and green corners, starting at top and going clockwise.
1562
1563 static const char *vertShaderText =
1564 "#version 140\n"
1565 "#extension GL_ARB_separate_shader_objects : enable\n"
1566 "#extension GL_ARB_shading_language_420pack : enable\n"
1567 //XYZ1( -1, -1, -1 )
1568 "layout (location = 0) in vec4 pos;\n"
1569 //XYZ1( 0.f, 0.f, 0.f )
1570 "layout (location = 1) in vec4 inColor;\n"
1571 "layout (location = 0) out vec4 outColor;\n"
1572 "void main() {\n"
1573 " outColor = inColor;\n"
1574 " vec2 vertices[3];"
1575 " vertices[0] = vec2(-1.0, -1.0);\n"
1576 " vertices[1] = vec2( 1.0, -1.0);\n"
1577 " vertices[2] = vec2( 0.0, 1.0);\n"
1578 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1579 "}\n";
1580
1581
1582 static const char *fragShaderText =
1583 "#version 140\n"
1584 "#extension GL_ARB_separate_shader_objects : enable\n"
1585 "#extension GL_ARB_shading_language_420pack : enable\n"
1586 "layout (location = 0) in vec4 color;\n"
1587 "void main() {\n"
1588 " gl_FragColor = color;\n"
1589 "}\n";
1590
1591 ASSERT_NO_FATAL_FAILURE(InitState());
1592 ASSERT_NO_FATAL_FAILURE(InitViewport());
1593
1594 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001595 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001596
1597 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1598 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1599
1600 XglPipelineObj pipelineobj(m_device);
1601 pipelineobj.AddShader(&vs);
1602 pipelineobj.AddShader(&ps);
1603
1604 XglDescriptorSetObj descriptorSet(m_device);
1605 descriptorSet.AttachMemoryView(&meshBuffer);
1606
1607 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1608 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1609 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1610 };
1611
1612 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1613 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1614 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1615 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1616 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1617 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1618 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1619 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1620 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1621
1622 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1623 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1624 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1625
Tony Barbour09da2212014-12-03 16:13:23 -07001626 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001627 QueueCommandBuffer(NULL, 0);
1628
1629}
1630
1631TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001632{
1633 static const char *vertShaderText =
1634 "#version 140\n"
1635 "layout (std140) uniform bufferVals {\n"
1636 " mat4 mvp;\n"
1637 "} myBufferVals;\n"
1638 "in vec4 pos;\n"
1639 "in vec4 inColor;\n"
1640 "out vec4 outColor;\n"
1641 "void main() {\n"
1642 " outColor = inColor;\n"
1643 " gl_Position = myBufferVals.mvp * pos;\n"
1644 "}\n";
1645
1646 static const char *fragShaderText =
1647 "#version 130\n"
1648 "in vec4 color;\n"
1649 "void main() {\n"
1650 " gl_FragColor = color;\n"
1651 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001652 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001653
Tony Barbourf43b6982014-11-25 13:18:32 -07001654 glm::mat4 View = glm::lookAt(
1655 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1656 glm::vec3(0,0,0), // and looks at the origin
1657 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1658 );
1659
1660 glm::mat4 Model = glm::mat4(1.0f);
1661
1662 glm::mat4 MVP = Projection * View * Model;
1663
1664 ASSERT_NO_FATAL_FAILURE(InitState());
1665 ASSERT_NO_FATAL_FAILURE(InitViewport());
1666 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1667
1668 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1669 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1670
1671 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1672
1673 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1674 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1675 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1676
1677 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1678
1679 XglPipelineObj pipelineobj(m_device);
1680 pipelineobj.AddShader(&vs);
1681 pipelineobj.AddShader(&ps);
1682
1683 XglDescriptorSetObj descriptorSet(m_device);
1684 descriptorSet.AttachMemoryView(&MVPBuffer);
1685
1686 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1687 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1688
1689 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1690 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1691 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1692 };
1693
1694 // this is the current description of g_vbData
1695 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1696 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1697 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1698 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1699 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1700 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1701 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1702 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1703 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1704
1705 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1706 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1707 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1708
Tony Barboura3953b82014-12-03 15:46:29 -07001709 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07001710
1711 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1712
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001713}
1714
Tony Barbourf43b6982014-11-25 13:18:32 -07001715TEST_F(XglRenderTest, VSTexture)
1716{
1717 // The expected result from this test is a green and red triangle;
1718 // one red vertex on the left, two green vertices on the right.
1719 static const char *vertShaderText =
1720 "#version 130\n"
1721 "out vec4 texColor;\n"
1722 "uniform sampler2D surface;\n"
1723 "void main() {\n"
1724 " vec2 vertices[3];"
1725 " vertices[0] = vec2(-0.5, -0.5);\n"
1726 " vertices[1] = vec2( 0.5, -0.5);\n"
1727 " vertices[2] = vec2( 0.5, 0.5);\n"
1728 " vec2 positions[3];"
1729 " positions[0] = vec2( 0.0, 0.0);\n"
1730 " positions[1] = vec2( 0.25, 0.1);\n"
1731 " positions[2] = vec2( 0.1, 0.25);\n"
1732 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1733 " texColor = textureLod(surface, samplePos, 0.0);\n"
1734 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1735 "}\n";
1736
1737 static const char *fragShaderText =
1738 "#version 130\n"
1739 "in vec4 texColor;\n"
1740 "void main() {\n"
1741 " gl_FragColor = texColor;\n"
1742 "}\n";
1743
1744 ASSERT_NO_FATAL_FAILURE(InitState());
1745 ASSERT_NO_FATAL_FAILURE(InitViewport());
1746
1747 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1748 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1749 XglSamplerObj sampler(m_device);
1750 XglTextureObj texture(m_device);
1751
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001752 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001753 vs.BindShaderEntitySlotToSampler(0, &sampler);
1754
1755 XglPipelineObj pipelineobj(m_device);
1756 pipelineobj.AddShader(&vs);
1757 pipelineobj.AddShader(&ps);
1758
1759 XglDescriptorSetObj descriptorSet(m_device);
1760 descriptorSet.AttachImageView(&texture);
1761 descriptorSet.AttachSampler(&sampler);
1762
1763 m_memoryRefManager.AddMemoryRef(&texture);
1764
Tony Barboura3953b82014-12-03 15:46:29 -07001765 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001766 QueueCommandBuffer(NULL, 0);
1767
1768}
1769TEST_F(XglRenderTest, TexturedTriangle)
1770{
1771 // The expected result from this test is a red and green checkered triangle
1772 static const char *vertShaderText =
1773 "#version 140\n"
1774 "#extension GL_ARB_separate_shader_objects : enable\n"
1775 "#extension GL_ARB_shading_language_420pack : enable\n"
1776 "layout (location = 0) out vec2 samplePos;\n"
1777 "void main() {\n"
1778 " vec2 vertices[3];"
1779 " vertices[0] = vec2(-0.5, -0.5);\n"
1780 " vertices[1] = vec2( 0.5, -0.5);\n"
1781 " vertices[2] = vec2( 0.5, 0.5);\n"
1782 " vec2 positions[3];"
1783 " positions[0] = vec2( 0.0, 0.0);\n"
1784 " positions[1] = vec2( 1.0, 0.0);\n"
1785 " positions[2] = vec2( 1.0, 1.0);\n"
1786 " samplePos = positions[gl_VertexID % 3];\n"
1787 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1788 "}\n";
1789
1790 static const char *fragShaderText =
1791 "#version 140\n"
1792 "#extension GL_ARB_separate_shader_objects : enable\n"
1793 "#extension GL_ARB_shading_language_420pack : enable\n"
1794 "layout (location = 0) in vec2 samplePos;\n"
1795 "layout (binding = 0) uniform sampler2D surface;\n"
1796 "layout (location=0) out vec4 outColor;\n"
1797 "void main() {\n"
1798 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1799 " outColor = texColor;\n"
1800 "}\n";
1801
1802 ASSERT_NO_FATAL_FAILURE(InitState());
1803 ASSERT_NO_FATAL_FAILURE(InitViewport());
1804
1805 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1806 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1807 XglSamplerObj sampler(m_device);
1808 XglTextureObj texture(m_device);
1809
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001810 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001811 ps.BindShaderEntitySlotToSampler(0, &sampler);
1812
1813 XglPipelineObj pipelineobj(m_device);
1814 pipelineobj.AddShader(&vs);
1815 pipelineobj.AddShader(&ps);
1816
1817 XglDescriptorSetObj descriptorSet(m_device);
1818 descriptorSet.AttachImageView(&texture);
1819 descriptorSet.AttachSampler(&sampler);
1820
1821 m_memoryRefManager.AddMemoryRef(&texture);
1822
Tony Barboura3953b82014-12-03 15:46:29 -07001823 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001824 QueueCommandBuffer(NULL, 0);
1825}
1826TEST_F(XglRenderTest, TexturedTriangleClip)
1827{
1828 // The expected result from this test is a red and green checkered triangle
1829 static const char *vertShaderText =
1830 "#version 330\n"
1831 "#extension GL_ARB_separate_shader_objects : enable\n"
1832 "#extension GL_ARB_shading_language_420pack : enable\n"
1833 "layout (location = 0) out vec2 samplePos;\n"
1834 "out gl_PerVertex {\n"
1835 " vec4 gl_Position;\n"
1836 " float gl_ClipDistance[1];\n"
1837 "};\n"
1838 "void main() {\n"
1839 " vec2 vertices[3];"
1840 " vertices[0] = vec2(-0.5, -0.5);\n"
1841 " vertices[1] = vec2( 0.5, -0.5);\n"
1842 " vertices[2] = vec2( 0.5, 0.5);\n"
1843 " vec2 positions[3];"
1844 " positions[0] = vec2( 0.0, 0.0);\n"
1845 " positions[1] = vec2( 1.0, 0.0);\n"
1846 " positions[2] = vec2( 1.0, 1.0);\n"
1847 " float dists[3];\n"
1848 " dists[0] = 1.0;\n"
1849 " dists[1] = 1.0;\n"
1850 " dists[2] = -1.0;\n"
1851 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1852 " samplePos = positions[gl_VertexID % 3];\n"
1853 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1854 "}\n";
1855
1856 static const char *fragShaderText =
1857 "#version 140\n"
1858 "#extension GL_ARB_separate_shader_objects : enable\n"
1859 "#extension GL_ARB_shading_language_420pack : enable\n"
1860 "layout (location = 0) in vec2 samplePos;\n"
1861 "layout (binding = 0) uniform sampler2D surface;\n"
1862 "layout (location=0) out vec4 outColor;\n"
1863 "void main() {\n"
1864 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1865 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1866 " outColor = texColor;\n"
1867 "}\n";
1868
1869
1870 ASSERT_NO_FATAL_FAILURE(InitState());
1871 ASSERT_NO_FATAL_FAILURE(InitViewport());
1872
1873 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1874 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1875 XglSamplerObj sampler(m_device);
1876 XglTextureObj texture(m_device);
1877
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001878 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001879 ps.BindShaderEntitySlotToSampler(0, &sampler);
1880
1881 XglPipelineObj pipelineobj(m_device);
1882 pipelineobj.AddShader(&vs);
1883 pipelineobj.AddShader(&ps);
1884
1885 XglDescriptorSetObj descriptorSet(m_device);
1886 descriptorSet.AttachImageView(&texture);
1887 descriptorSet.AttachSampler(&sampler);
1888
1889 m_memoryRefManager.AddMemoryRef(&texture);
1890
Tony Barboura3953b82014-12-03 15:46:29 -07001891 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001892 QueueCommandBuffer(NULL, 0);
1893}
1894TEST_F(XglRenderTest, FSTriangle)
1895{
1896 // The expected result from this test is a red and green checkered triangle
1897 static const char *vertShaderText =
1898 "#version 140\n"
1899 "#extension GL_ARB_separate_shader_objects : enable\n"
1900 "#extension GL_ARB_shading_language_420pack : enable\n"
1901 "layout (location = 0) out vec2 samplePos;\n"
1902 "void main() {\n"
1903 " vec2 vertices[3];"
1904 " vertices[0] = vec2(-0.5, -0.5);\n"
1905 " vertices[1] = vec2( 0.5, -0.5);\n"
1906 " vertices[2] = vec2( 0.5, 0.5);\n"
1907 " vec2 positions[3];"
1908 " positions[0] = vec2( 0.0, 0.0);\n"
1909 " positions[1] = vec2( 1.0, 0.0);\n"
1910 " positions[2] = vec2( 1.0, 1.0);\n"
1911 " samplePos = positions[gl_VertexID % 3];\n"
1912 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1913 "}\n";
1914
1915 static const char *fragShaderText =
1916 "#version 140\n"
1917 "#extension GL_ARB_separate_shader_objects : enable\n"
1918 "#extension GL_ARB_shading_language_420pack : enable\n"
1919 "layout (location = 0) in vec2 samplePos;\n"
1920 "layout (binding = 0) uniform sampler2D surface;\n"
1921 "layout (location=0) out vec4 outColor;\n"
1922 "void main() {\n"
1923 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1924 " outColor = texColor;\n"
1925 "}\n";
1926
1927 ASSERT_NO_FATAL_FAILURE(InitState());
1928 ASSERT_NO_FATAL_FAILURE(InitViewport());
1929
1930 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1931 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1932 XglSamplerObj sampler(m_device);
1933 XglTextureObj texture(m_device);
1934
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001935 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001936 ps.BindShaderEntitySlotToSampler(0, &sampler);
1937
1938 XglPipelineObj pipelineobj(m_device);
1939 pipelineobj.AddShader(&vs);
1940 pipelineobj.AddShader(&ps);
1941
1942 XglDescriptorSetObj descriptorSet(m_device);
1943 descriptorSet.AttachImageView(&texture);
1944 descriptorSet.AttachSampler(&sampler);
1945
1946 m_memoryRefManager.AddMemoryRef(&texture);
1947
Tony Barboura3953b82014-12-03 15:46:29 -07001948 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001949 QueueCommandBuffer(NULL, 0);
1950}
1951TEST_F(XglRenderTest, SamplerBindingsTriangle)
1952{
1953 // This test sets bindings on the samplers
1954 // For now we are asserting that sampler and texture pairs
1955 // march in lock step, and are set via GLSL binding. This can
1956 // and will probably change.
1957 // The sampler bindings should match the sampler and texture slot
1958 // number set up by the application.
1959 // This test will result in a blue triangle
1960 static const char *vertShaderText =
1961 "#version 140\n"
1962 "#extension GL_ARB_separate_shader_objects : enable\n"
1963 "#extension GL_ARB_shading_language_420pack : enable\n"
1964 "layout (location = 0) out vec4 samplePos;\n"
1965 "void main() {\n"
1966 " vec2 vertices[3];"
1967 " vertices[0] = vec2(-0.5, -0.5);\n"
1968 " vertices[1] = vec2( 0.5, -0.5);\n"
1969 " vertices[2] = vec2( 0.5, 0.5);\n"
1970 " vec2 positions[3];"
1971 " positions[0] = vec2( 0.0, 0.0);\n"
1972 " positions[1] = vec2( 1.0, 0.0);\n"
1973 " positions[2] = vec2( 1.0, 1.0);\n"
1974 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
1975 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1976 "}\n";
1977
1978 static const char *fragShaderText =
1979 "#version 140\n"
1980 "#extension GL_ARB_separate_shader_objects : enable\n"
1981 "#extension GL_ARB_shading_language_420pack : enable\n"
1982 "layout (location = 0) in vec4 samplePos;\n"
1983 "layout (binding = 0) uniform sampler2D surface0;\n"
1984 "layout (binding = 1) uniform sampler2D surface1;\n"
1985 "layout (binding = 12) uniform sampler2D surface2;\n"
1986 "void main() {\n"
1987 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
1988 "}\n";
1989
1990 ASSERT_NO_FATAL_FAILURE(InitState());
1991 ASSERT_NO_FATAL_FAILURE(InitViewport());
1992
1993 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1994 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1995
1996 XglSamplerObj sampler1(m_device);
1997 XglSamplerObj sampler2(m_device);
1998 XglSamplerObj sampler3(m_device);
1999
2000 XglTextureObj texture1(m_device); // Red
2001 texture1.ChangeColors(0xffff0000,0xffff0000);
2002 XglTextureObj texture2(m_device); // Green
2003 texture2.ChangeColors(0xff00ff00,0xff00ff00);
2004 XglTextureObj texture3(m_device); // Blue
2005 texture3.ChangeColors(0xff0000ff,0xff0000ff);
2006
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002007 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002008 ps.BindShaderEntitySlotToSampler(0, &sampler1);
2009
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002010 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Tony Barbourf43b6982014-11-25 13:18:32 -07002011 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2012
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002013 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture3);
Tony Barbourf43b6982014-11-25 13:18:32 -07002014 ps.BindShaderEntitySlotToSampler(12, &sampler3);
2015
2016 XglPipelineObj pipelineobj(m_device);
2017 pipelineobj.AddShader(&vs);
2018 pipelineobj.AddShader(&ps);
2019
2020 XglDescriptorSetObj descriptorSet(m_device);
2021 descriptorSet.AttachImageView(&texture1);
2022 descriptorSet.AttachSampler(&sampler1);
2023 descriptorSet.AttachImageView(&texture2);
2024 descriptorSet.AttachSampler(&sampler2);
2025 descriptorSet.AttachImageView(&texture3);
2026 descriptorSet.AttachSampler(&sampler3);
2027
2028 m_memoryRefManager.AddMemoryRef(&texture1);
2029 m_memoryRefManager.AddMemoryRef(&texture2);
2030 m_memoryRefManager.AddMemoryRef(&texture3);
2031
Tony Barboura3953b82014-12-03 15:46:29 -07002032 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002033 QueueCommandBuffer(NULL, 0);
2034
2035}
2036
2037TEST_F(XglRenderTest, TriangleVSUniformBlock)
2038{
2039 // The expected result from this test is a blue triangle
2040
2041 static const char *vertShaderText =
2042 "#version 140\n"
2043 "#extension GL_ARB_separate_shader_objects : enable\n"
2044 "#extension GL_ARB_shading_language_420pack : enable\n"
2045 "layout (location = 0) out vec4 outColor;\n"
2046 "layout (std140, binding = 0) uniform bufferVals {\n"
2047 " vec4 red;\n"
2048 " vec4 green;\n"
2049 " vec4 blue;\n"
2050 " vec4 white;\n"
2051 "} myBufferVals;\n"
2052 "void main() {\n"
2053 " vec2 vertices[3];"
2054 " vertices[0] = vec2(-0.5, -0.5);\n"
2055 " vertices[1] = vec2( 0.5, -0.5);\n"
2056 " vertices[2] = vec2( 0.5, 0.5);\n"
2057 " outColor = myBufferVals.blue;\n"
2058 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2059 "}\n";
2060
2061 static const char *fragShaderText =
2062 "#version 140\n"
2063 "#extension GL_ARB_separate_shader_objects : enable\n"
2064 "#extension GL_ARB_shading_language_420pack : enable\n"
2065 "layout (location = 0) in vec4 inColor;\n"
2066 "void main() {\n"
2067 " gl_FragColor = inColor;\n"
2068 "}\n";
2069
2070 ASSERT_NO_FATAL_FAILURE(InitState());
2071 ASSERT_NO_FATAL_FAILURE(InitViewport());
2072
2073 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2074 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2075
2076 // Let's populate our buffer with the following:
2077 // vec4 red;
2078 // vec4 green;
2079 // vec4 blue;
2080 // vec4 white;
2081 const int valCount = 4 * 4;
2082 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2083 0.0, 1.0, 0.0, 1.0,
2084 0.0, 0.0, 1.0, 1.0,
2085 1.0, 1.0, 1.0, 1.0 };
2086
2087 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2088 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2089
2090 XglPipelineObj pipelineobj(m_device);
2091 pipelineobj.AddShader(&vs);
2092 pipelineobj.AddShader(&ps);
2093
2094 XglDescriptorSetObj descriptorSet(m_device);
2095 descriptorSet.AttachMemoryView(&colorBuffer);
2096
Tony Barboura3953b82014-12-03 15:46:29 -07002097 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002098 QueueCommandBuffer(NULL, 0);
2099
2100}
2101
2102TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2103{
2104 // This test allows the shader to select which buffer it is
2105 // pulling from using layout binding qualifier.
2106 // There are corresponding changes in the compiler stack that
2107 // will select the buffer using binding directly.
2108 // The binding number should match the slot number set up by
2109 // the application.
2110 // The expected result from this test is a purple triangle
2111
2112 static const char *vertShaderText =
2113 "#version 140\n"
2114 "#extension GL_ARB_separate_shader_objects : enable\n"
2115 "#extension GL_ARB_shading_language_420pack : enable\n"
2116 "void main() {\n"
2117 " vec2 vertices[3];"
2118 " vertices[0] = vec2(-0.5, -0.5);\n"
2119 " vertices[1] = vec2( 0.5, -0.5);\n"
2120 " vertices[2] = vec2( 0.5, 0.5);\n"
2121 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2122 "}\n";
2123
2124 static const char *fragShaderText =
2125 "#version 140\n"
2126 "#extension GL_ARB_separate_shader_objects : enable\n"
2127 "#extension GL_ARB_shading_language_420pack : enable\n"
2128 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2129 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2130 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2131 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2132 "void main() {\n"
2133 " gl_FragColor = myBlueVal.color;\n"
2134 " gl_FragColor += myRedVal.color;\n"
2135 "}\n";
2136
2137 ASSERT_NO_FATAL_FAILURE(InitState());
2138 ASSERT_NO_FATAL_FAILURE(InitViewport());
2139
2140 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2141 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2142
2143 // We're going to create a number of uniform buffers, and then allow
2144 // the shader to select which it wants to read from with a binding
2145
2146 // Let's populate the buffers with a single color each:
2147 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2148 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2149 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2150 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2151
2152 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2153 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2154 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2155 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2156
2157 const int redCount = sizeof(redVals) / sizeof(float);
2158 const int greenCount = sizeof(greenVals) / sizeof(float);
2159 const int blueCount = sizeof(blueVals) / sizeof(float);
2160 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2161
2162 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2163 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2164
2165 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2166 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2167
2168 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2169 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2170
2171 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2172 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2173
2174 XglPipelineObj pipelineobj(m_device);
2175 pipelineobj.AddShader(&vs);
2176 pipelineobj.AddShader(&ps);
2177
2178 XglDescriptorSetObj descriptorSet(m_device);
2179 descriptorSet.AttachMemoryView(&redBuffer);
2180 descriptorSet.AttachMemoryView(&greenBuffer);
2181 descriptorSet.AttachMemoryView(&blueBuffer);
2182 descriptorSet.AttachMemoryView(&whiteBuffer);
2183
Tony Barboura3953b82014-12-03 15:46:29 -07002184 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002185 QueueCommandBuffer(NULL, 0);
2186
2187}
2188
2189TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2190{
2191 // This test is the same as TriangleFSUniformBlockBinding, but
2192 // it does not provide an instance name.
2193 // The expected result from this test is a purple triangle
2194
2195 static const char *vertShaderText =
2196 "#version 140\n"
2197 "#extension GL_ARB_separate_shader_objects : enable\n"
2198 "#extension GL_ARB_shading_language_420pack : enable\n"
2199 "void main() {\n"
2200 " vec2 vertices[3];"
2201 " vertices[0] = vec2(-0.5, -0.5);\n"
2202 " vertices[1] = vec2( 0.5, -0.5);\n"
2203 " vertices[2] = vec2( 0.5, 0.5);\n"
2204 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2205 "}\n";
2206
2207 static const char *fragShaderText =
2208 "#version 430\n"
2209 "#extension GL_ARB_separate_shader_objects : enable\n"
2210 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002211 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2212 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2213 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2214 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
Cody Northrop68a10f62014-12-05 15:44:14 -07002215 "layout (location = 0) out vec4 outColor;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002216 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -07002217 " outColor = blue;\n"
2218 " outColor += red;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002219 "}\n";
2220 ASSERT_NO_FATAL_FAILURE(InitState());
2221 ASSERT_NO_FATAL_FAILURE(InitViewport());
2222
2223 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2224 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2225
2226 // We're going to create a number of uniform buffers, and then allow
2227 // the shader to select which it wants to read from with a binding
2228
2229 // Let's populate the buffers with a single color each:
2230 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2231 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2232 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2233 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2234
2235 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2236 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2237 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2238 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2239
2240 const int redCount = sizeof(redVals) / sizeof(float);
2241 const int greenCount = sizeof(greenVals) / sizeof(float);
2242 const int blueCount = sizeof(blueVals) / sizeof(float);
2243 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2244
2245 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2246 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2247
2248 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2249 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2250
2251 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2252 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2253
2254 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002255 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07002256
2257 XglPipelineObj pipelineobj(m_device);
2258 pipelineobj.AddShader(&vs);
2259 pipelineobj.AddShader(&ps);
2260
2261 XglDescriptorSetObj descriptorSet(m_device);
2262 descriptorSet.AttachMemoryView(&redBuffer);
2263 descriptorSet.AttachMemoryView(&greenBuffer);
2264 descriptorSet.AttachMemoryView(&blueBuffer);
2265 descriptorSet.AttachMemoryView(&whiteBuffer);
2266
Tony Barboura3953b82014-12-03 15:46:29 -07002267 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002268 QueueCommandBuffer(NULL, 0);
2269
2270}
2271
2272TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2273{
2274 static const char *vertShaderText =
2275 "#version 140\n"
2276 "#extension GL_ARB_separate_shader_objects : enable\n"
2277 "#extension GL_ARB_shading_language_420pack : enable\n"
2278 "layout (std140, binding=0) uniform bufferVals {\n"
2279 " mat4 mvp;\n"
2280 "} myBufferVals;\n"
2281 "layout (location=0) in vec4 pos;\n"
2282 "layout (location=0) out vec2 UV;\n"
2283 "void main() {\n"
2284 " vec2 positions[3];"
2285 " positions[0] = vec2( 0.0, 0.0);\n"
2286 " positions[1] = vec2( 0.25, 0.1);\n"
2287 " positions[2] = vec2( 0.1, 0.25);\n"
2288 " UV = positions[gl_VertexID % 3];\n"
2289 " gl_Position = myBufferVals.mvp * pos;\n"
2290 "}\n";
2291
2292 static const char *fragShaderText =
2293 "#version 140\n"
2294 "#extension GL_ARB_separate_shader_objects : enable\n"
2295 "#extension GL_ARB_shading_language_420pack : enable\n"
2296 "layout (binding=0) uniform sampler2D surface;\n"
2297 "layout (location=0) out vec4 outColor;\n"
2298 "layout (location=0) in vec2 UV;\n"
2299 "void main() {\n"
2300 " outColor= textureLod(surface, UV, 0.0);\n"
2301 "}\n";
2302 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2303
2304 glm::mat4 View = glm::lookAt(
2305 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2306 glm::vec3(0,0,0), // and looks at the origin
2307 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2308 );
2309
2310 glm::mat4 Model = glm::mat4(1.0f);
2311
2312 glm::mat4 MVP = Projection * View * Model;
2313
2314
2315 ASSERT_NO_FATAL_FAILURE(InitState());
2316 ASSERT_NO_FATAL_FAILURE(InitViewport());
2317 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2318
2319 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2320 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
Tony Barboure6152042014-12-10 17:40:15 -07002321 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07002322
2323
2324 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2325
2326 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2327 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2328 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2329 XglSamplerObj sampler(m_device);
2330 XglTextureObj texture(m_device);
2331
2332 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2333 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002334 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002335 ps.BindShaderEntitySlotToSampler(0, &sampler);
2336
2337 XglPipelineObj pipelineobj(m_device);
2338 pipelineobj.AddShader(&vs);
2339 pipelineobj.AddShader(&ps);
2340
2341 XglDescriptorSetObj descriptorSet(m_device);
2342
2343 descriptorSet.AttachMemoryView(&mvpBuffer);
2344 descriptorSet.AttachImageView(&texture);
2345 descriptorSet.AttachSampler(&sampler);
2346
2347 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2348 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2349 m_memoryRefManager.AddMemoryRef(&texture);
2350
2351 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2352 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2353 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2354 };
2355
2356 // this is the current description of g_vbData
2357 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2358 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2359 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2360 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2361 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2362 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2363 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2364 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2365 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2366
2367 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2368 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2369 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2370
Tony Barboura3953b82014-12-03 15:46:29 -07002371 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07002372
2373 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2374
2375}
Cody Northropd1ce7842014-12-09 11:17:01 -07002376
2377TEST_F(XglRenderTest, TriangleMixedSamplerUniformBlockBinding)
2378{
2379 // This test mixes binding slots of textures and buffers, ensuring
2380 // that sparse and overlapping assignments work.
Cody Northropa0410942014-12-09 13:59:39 -07002381 // The expected result from this test is a purple triangle, although
Cody Northropd1ce7842014-12-09 11:17:01 -07002382 // you can modify it to move the desired result around.
2383
2384 static const char *vertShaderText =
2385 "#version 140\n"
2386 "#extension GL_ARB_separate_shader_objects : enable\n"
2387 "#extension GL_ARB_shading_language_420pack : enable\n"
2388 "void main() {\n"
2389 " vec2 vertices[3];"
2390 " vertices[0] = vec2(-0.5, -0.5);\n"
2391 " vertices[1] = vec2( 0.5, -0.5);\n"
2392 " vertices[2] = vec2( 0.5, 0.5);\n"
2393 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2394 "}\n";
2395
2396 static const char *fragShaderText =
2397 "#version 430\n"
2398 "#extension GL_ARB_separate_shader_objects : enable\n"
2399 "#extension GL_ARB_shading_language_420pack : enable\n"
2400 "layout (binding = 0) uniform sampler2D surface0;\n"
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002401 "layout (binding = 9) uniform sampler2D surface1;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002402 "layout (binding = 2) uniform sampler2D surface2;\n"
Cody Northropa0410942014-12-09 13:59:39 -07002403 "layout (binding = 4) uniform sampler2D surface3;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002404
Cody Northropa0410942014-12-09 13:59:39 -07002405
2406 "layout (std140, binding = 10) uniform redVal { vec4 red; };"
2407 "layout (std140, binding = 15) uniform greenVal { vec4 green; };"
2408 "layout (std140, binding = 13) uniform blueVal { vec4 blue; };"
2409 "layout (std140, binding = 17) uniform whiteVal { vec4 white; };"
Cody Northropd1ce7842014-12-09 11:17:01 -07002410 "layout (location = 0) out vec4 outColor;\n"
2411 "void main() {\n"
Cody Northropa0410942014-12-09 13:59:39 -07002412 " outColor = red * vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002413 " outColor += white * vec4(0.00001);\n"
2414 " outColor += textureLod(surface2, vec2(0.5), 0.0)* vec4(0.00001);\n"
Cody Northropa0410942014-12-09 13:59:39 -07002415 " outColor += textureLod(surface1, vec2(0.0), 0.0);//* vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002416 "}\n";
2417 ASSERT_NO_FATAL_FAILURE(InitState());
2418 ASSERT_NO_FATAL_FAILURE(InitViewport());
2419
2420 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2421 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2422
Cody Northropd1ce7842014-12-09 11:17:01 -07002423 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2424 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2425 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2426 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2427
2428 const int redCount = sizeof(redVals) / sizeof(float);
2429 const int greenCount = sizeof(greenVals) / sizeof(float);
2430 const int blueCount = sizeof(blueVals) / sizeof(float);
2431 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2432
2433 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Cody Northropa0410942014-12-09 13:59:39 -07002434 ps.BindShaderEntitySlotToMemory(10, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002435
2436 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Cody Northropa0410942014-12-09 13:59:39 -07002437 ps.BindShaderEntitySlotToMemory(15, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002438
2439 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Cody Northropa0410942014-12-09 13:59:39 -07002440 ps.BindShaderEntitySlotToMemory(13, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002441
2442 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropa0410942014-12-09 13:59:39 -07002443 ps.BindShaderEntitySlotToMemory(17, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002444
2445 XglSamplerObj sampler0(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002446 XglTextureObj texture0(m_device); // Light Red
2447 texture0.ChangeColors(0xff800000,0xff800000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002448 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
Cody Northropd1ce7842014-12-09 11:17:01 -07002449 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2450 XglSamplerObj sampler2(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002451 XglTextureObj texture2(m_device); // Light Blue
2452 texture2.ChangeColors(0xff000080,0xff000080);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002453 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Cody Northropd1ce7842014-12-09 11:17:01 -07002454 ps.BindShaderEntitySlotToSampler(2, &sampler2);
2455 XglSamplerObj sampler4(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002456 XglTextureObj texture4(m_device); // Light Green
2457 texture4.ChangeColors(0xff008000,0xff008000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002458 ps.BindShaderEntitySlotToImage(4, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
Cody Northropd1ce7842014-12-09 11:17:01 -07002459 ps.BindShaderEntitySlotToSampler(4, &sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002460
2461 // NOTE: Bindings 1,3,5,7,8,9,11,12,14,16 work for this sampler, but 6 does not!!!
2462 // TODO: Get back here ASAP and understand why.
2463 XglSamplerObj sampler7(m_device);
2464 XglTextureObj texture7(m_device); // Red and Blue
2465 texture7.ChangeColors(0xffff00ff,0xffff00ff);
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002466 ps.BindShaderEntitySlotToImage(9, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2467 ps.BindShaderEntitySlotToSampler(9, &sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002468
2469
2470 XglPipelineObj pipelineobj(m_device);
2471 pipelineobj.AddShader(&vs);
2472 pipelineobj.AddShader(&ps);
2473
2474 XglDescriptorSetObj descriptorSet(m_device);
2475 descriptorSet.AttachMemoryView(&redBuffer);
2476 descriptorSet.AttachMemoryView(&greenBuffer);
2477 descriptorSet.AttachMemoryView(&blueBuffer);
2478 descriptorSet.AttachMemoryView(&whiteBuffer);
2479 descriptorSet.AttachImageView(&texture0);
2480 descriptorSet.AttachSampler(&sampler0);
2481 descriptorSet.AttachImageView(&texture2);
2482 descriptorSet.AttachSampler(&sampler2);
2483 descriptorSet.AttachImageView(&texture4);
2484 descriptorSet.AttachSampler(&sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002485 descriptorSet.AttachImageView(&texture7);
2486 descriptorSet.AttachSampler(&sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002487
2488 m_memoryRefManager.AddMemoryRef(&texture0);
2489 m_memoryRefManager.AddMemoryRef(&texture2);
2490 m_memoryRefManager.AddMemoryRef(&texture4);
Cody Northropa0410942014-12-09 13:59:39 -07002491 m_memoryRefManager.AddMemoryRef(&texture7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002492
2493
2494 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2495 QueueCommandBuffer(NULL, 0);
2496
2497}
2498
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002499TEST_F(XglRenderTest, TriangleMatchingSamplerUniformBlockBinding)
2500{
2501 // This test matches binding slots of textures and buffers, requiring
2502 // the driver to give them distinct number spaces.
2503 // The expected result from this test is a red triangle, although
2504 // you can modify it to move the desired result around.
2505
2506 static const char *vertShaderText =
2507 "#version 140\n"
2508 "#extension GL_ARB_separate_shader_objects : enable\n"
2509 "#extension GL_ARB_shading_language_420pack : enable\n"
2510 "void main() {\n"
2511 " vec2 vertices[3];"
2512 " vertices[0] = vec2(-0.5, -0.5);\n"
2513 " vertices[1] = vec2( 0.5, -0.5);\n"
2514 " vertices[2] = vec2( 0.5, 0.5);\n"
2515 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2516 "}\n";
2517
2518 static const char *fragShaderText =
2519 "#version 430\n"
2520 "#extension GL_ARB_separate_shader_objects : enable\n"
2521 "#extension GL_ARB_shading_language_420pack : enable\n"
2522 "layout (binding = 0) uniform sampler2D surface0;\n"
2523 "layout (binding = 1) uniform sampler2D surface1;\n"
2524 "layout (binding = 2) uniform sampler2D surface2;\n"
2525 "layout (binding = 3) uniform sampler2D surface3;\n"
2526 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2527 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2528 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2529 "layout (std140, binding = 3) uniform whiteVal { vec4 white; };"
2530 "layout (location = 0) out vec4 outColor;\n"
2531 "void main() {\n"
2532 " outColor = red;// * vec4(0.00001);\n"
2533 " outColor += white * vec4(0.00001);\n"
2534 " outColor += textureLod(surface1, vec2(0.5), 0.0)* vec4(0.00001);\n"
2535 " outColor += textureLod(surface3, vec2(0.0), 0.0)* vec4(0.00001);\n"
2536 "}\n";
2537 ASSERT_NO_FATAL_FAILURE(InitState());
2538 ASSERT_NO_FATAL_FAILURE(InitViewport());
2539
2540 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2541 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2542
2543 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2544 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2545 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2546 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2547
2548 const int redCount = sizeof(redVals) / sizeof(float);
2549 const int greenCount = sizeof(greenVals) / sizeof(float);
2550 const int blueCount = sizeof(blueVals) / sizeof(float);
2551 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2552
2553 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2554 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2555
2556 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2557 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2558
2559 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2560 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2561
2562 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2563 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2564
2565 XglSamplerObj sampler0(m_device);
2566 XglTextureObj texture0(m_device); // Light Red
2567 texture0.ChangeColors(0xff800000,0xff800000);
2568 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
2569 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2570 XglSamplerObj sampler2(m_device);
2571 XglTextureObj texture2(m_device); // Light Blue
2572 texture2.ChangeColors(0xff000080,0xff000080);
2573 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
2574 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2575 XglSamplerObj sampler4(m_device);
2576 XglTextureObj texture4(m_device); // Light Green
2577 texture4.ChangeColors(0xff008000,0xff008000);
2578 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
2579 ps.BindShaderEntitySlotToSampler(2, &sampler4);
2580 XglSamplerObj sampler7(m_device);
2581 XglTextureObj texture7(m_device); // Red and Blue
2582 texture7.ChangeColors(0xffff00ff,0xffff00ff);
2583 ps.BindShaderEntitySlotToImage(3, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2584 ps.BindShaderEntitySlotToSampler(3, &sampler7);
2585
2586
2587 XglPipelineObj pipelineobj(m_device);
2588 pipelineobj.AddShader(&vs);
2589 pipelineobj.AddShader(&ps);
2590
2591 XglDescriptorSetObj descriptorSet(m_device);
2592 descriptorSet.AttachMemoryView(&redBuffer);
2593 descriptorSet.AttachMemoryView(&greenBuffer);
2594 descriptorSet.AttachMemoryView(&blueBuffer);
2595 descriptorSet.AttachMemoryView(&whiteBuffer);
2596 descriptorSet.AttachImageView(&texture0);
2597 descriptorSet.AttachSampler(&sampler0);
2598 descriptorSet.AttachImageView(&texture2);
2599 descriptorSet.AttachSampler(&sampler2);
2600 descriptorSet.AttachImageView(&texture4);
2601 descriptorSet.AttachSampler(&sampler4);
2602 descriptorSet.AttachImageView(&texture7);
2603 descriptorSet.AttachSampler(&sampler7);
2604
2605 m_memoryRefManager.AddMemoryRef(&texture0);
2606 m_memoryRefManager.AddMemoryRef(&texture2);
2607 m_memoryRefManager.AddMemoryRef(&texture4);
2608 m_memoryRefManager.AddMemoryRef(&texture7);
2609
2610
2611 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2612 QueueCommandBuffer(NULL, 0);
2613
2614}
2615
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002616int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002617 int result;
2618
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002619 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002620 XglTestFramework::InitArgs(&argc, argv);
2621
Chia-I Wu7133fdc2014-12-15 23:57:34 +08002622 ::testing::AddGlobalTestEnvironment(new TestEnvironment);
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002623
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002624 result = RUN_ALL_TESTS();
2625
2626 XglTestFramework::Finish();
2627 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002628}