blob: fb211a3d907555dc9c0f64b285cd1d76b440f6f0 [file] [log] [blame]
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06001// Copyright 2005, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31// XGL tests
32//
33// Copyright (C) 2014 LunarG, Inc.
34//
35// Permission is hereby granted, free of charge, to any person obtaining a
36// copy of this software and associated documentation files (the "Software"),
37// to deal in the Software without restriction, including without limitation
38// the rights to use, copy, modify, merge, publish, distribute, sublicense,
39// and/or sell copies of the Software, and to permit persons to whom the
40// Software is furnished to do so, subject to the following conditions:
41//
42// The above copyright notice and this permission notice shall be included
43// in all copies or substantial portions of the Software.
44//
45// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51// DEALINGS IN THE SOFTWARE.
52
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060053// Basic rendering tests
54
55#include <stdlib.h>
56#include <stdio.h>
57#include <stdbool.h>
58#include <string.h>
Courtney Goeltzenleuchter76a643b2014-08-21 17:34:22 -060059#include <iostream>
60#include <fstream>
61using namespace std;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060062
63#include <xgl.h>
Tobin Ehlis31446e52014-11-28 11:17:19 -070064#ifdef DUMP_STATE_DOT
65#include "../layers/draw_state.h"
66#endif
Tobin Ehlis3c26a542014-11-18 11:28:33 -070067#ifdef PRINT_OBJECTS
68#include "../layers/object_track.h"
69#endif
Tobin Ehlis791a49c2014-11-10 12:29:12 -070070#ifdef DEBUG_CALLBACK
71#include <xglDbg.h>
72#endif
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060073#include "gtest-1.7.0/include/gtest/gtest.h"
74
75#include "xgldevice.h"
Courtney Goeltzenleuchter04814f82014-09-01 16:37:18 -060076#include "xglimage.h"
Chia-I Wu4115c892014-08-28 11:56:29 +080077#include "icd-bil.h"
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060078
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -060079#define GLM_FORCE_RADIANS
80#include "glm/glm.hpp"
81#include <glm/gtc/matrix_transform.hpp>
82
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060083#include "xglrenderframework.h"
Tobin Ehlis791a49c2014-11-10 12:29:12 -070084#ifdef DEBUG_CALLBACK
85XGL_VOID XGLAPI myDbgFunc(
86 XGL_DBG_MSG_TYPE msgType,
87 XGL_VALIDATION_LEVEL validationLevel,
88 XGL_BASE_OBJECT srcObject,
89 XGL_SIZE location,
90 XGL_INT msgCode,
91 const XGL_CHAR* pMsg,
92 XGL_VOID* pUserData)
93{
Tobin Ehlis3c26a542014-11-18 11:28:33 -070094 switch (msgType)
95 {
96 case XGL_DBG_MSG_WARNING:
97 printf("CALLBACK WARNING : %s\n", pMsg);
98 break;
99 case XGL_DBG_MSG_ERROR:
100 printf("CALLBACK ERROR : %s\n", pMsg);
101 break;
102 default:
103 printf("EATING Msg of type %u\n", msgType);
104 break;
105 }
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700106}
107#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700108
109
110#undef ASSERT_NO_FATAL_FAILURE
111#define ASSERT_NO_FATAL_FAILURE(x) x
112
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600113//--------------------------------------------------------------------------------------
114// Mesh and VertexFormat Data
115//--------------------------------------------------------------------------------------
116struct Vertex
117{
118 XGL_FLOAT posX, posY, posZ, posW; // Position data
119 XGL_FLOAT r, g, b, a; // Color
120};
121
122#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
123
124static const Vertex g_vbData[] =
125{
126 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
127 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
128 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
129 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
130 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
131 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
132
133 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
134 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
135 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
136 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
137 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
138 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
139
140 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
141 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
142 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
143 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
144 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
145 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
146
147 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
148 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
149 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
150 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
151 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
152 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
153
154 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
155 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
156 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
157 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
158 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
159 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
160
161 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
162 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
163 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
164 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
165 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
166 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
167};
168
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600169static const Vertex g_vb_solid_face_colors_Data[] =
170{
171 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
172 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
173 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
174 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
175 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
176 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
177
178 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
179 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
180 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
181 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
182 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
183 { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
184
185 { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
186 { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
187 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
188 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
189 { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
190 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
191
192 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
193 { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
194 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
195 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
196 { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
197 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
198
199 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
200 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
201 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
202 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
203 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
204 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
205
206 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
207 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
208 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
209 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
210 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
211 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
212};
213
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600214class XglRenderTest : public XglRenderFramework
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600215{
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600216public:
Cody Northrop4e6b4762014-10-09 21:25:22 -0600217
Tony Barbourf43b6982014-11-25 13:18:32 -0700218 void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText);
219 void RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
220 XglConstantBufferObj constantBuffer);
221 void GenericDrawTriangleTest(XglPipelineObj pipelineobj, XglDescriptorSetObj descriptorSet, int numTris);
222 void QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs);
223
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600224 void ClearDepthStencil(XGL_FLOAT value);
225 void ClearRenderBuffer(XGL_UINT32 clear_color);
226 void InitDepthStencil();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600227 void GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600228 void XGLTriangleTest(const char *vertShaderText, const char *fragShaderText);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600229
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600230
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600231protected:
Cody Northrop350727b2014-10-06 15:42:00 -0600232 XGL_IMAGE m_texture;
233 XGL_IMAGE_VIEW m_textureView;
234 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
235 XGL_GPU_MEMORY m_textureMem;
236
237 XGL_SAMPLER m_sampler;
238
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600239 XGL_FORMAT m_depth_stencil_fmt;
240 XGL_IMAGE m_depthStencilImage;
241 XGL_GPU_MEMORY m_depthStencilMem;
242 XGL_DEPTH_STENCIL_VIEW m_depthStencilView;
Tony Barbourf43b6982014-11-25 13:18:32 -0700243 XglMemoryRefManager m_memoryRefManager;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600244
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600245
246 virtual void SetUp() {
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600247
248 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
249 this->app_info.pNext = NULL;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600250 this->app_info.pAppName = (const XGL_CHAR *) "render_tests";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600251 this->app_info.appVersion = 1;
252 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
253 this->app_info.engineVersion = 1;
254 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
255
Cody Northrop350727b2014-10-06 15:42:00 -0600256 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
257 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
258
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600259 InitFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600260 }
261
262 virtual void TearDown() {
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600263 // Clean up resources before we reset
264 ShutdownFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600265 }
266};
267
Tony Barbourf43b6982014-11-25 13:18:32 -0700268
269void XglRenderTest::GenericDrawTriangleTest(XglPipelineObj pipelineobj, XglDescriptorSetObj descriptorSet,int numTris)
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600270{
271 XGL_RESULT err = XGL_SUCCESS;
272
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600273 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600274 // Build command buffer
275 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
276 ASSERT_XGL_SUCCESS(err);
277
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600278 GenerateClearAndPrepareBufferCmds();
279 GenerateBindRenderTargetCmd();
Tobin Ehlis31446e52014-11-28 11:17:19 -0700280#ifdef DUMP_STATE_DOT
281 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
282 pDSDumpDot((char*)"triTest.dot");
283#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700284 GenerateBindStateAndPipelineCmds();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600285
Tony Barbourf43b6982014-11-25 13:18:32 -0700286 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
287 descriptorSet.BindCommandBuffer(m_cmdBuffer);
288
289 // render the triangle
290 xglCmdDraw( m_cmdBuffer, 0, 3*numTris, 0, 1 );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600291
292 // finalize recording of the command buffer
293 err = xglEndCommandBuffer( m_cmdBuffer );
294 ASSERT_XGL_SUCCESS( err );
Tony Barbourf43b6982014-11-25 13:18:32 -0700295}
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600296
Tony Barbourf43b6982014-11-25 13:18:32 -0700297void XglRenderTest::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
298{
299 XGL_RESULT err = XGL_SUCCESS;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600300
301 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700302 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, numMemRefs, memRefs, NULL );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600303 ASSERT_XGL_SUCCESS( err );
304
Chia-I Wuf34ac502014-08-27 14:58:05 +0800305 err = xglQueueWaitIdle( m_device->m_queue );
306 ASSERT_XGL_SUCCESS( err );
307
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600308 // Wait for work to finish before cleaning up.
309 xglDeviceWaitIdle(m_device->device());
310
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600311 RecordImage(m_renderTarget);
Chia-I Wuf070ec12014-08-30 23:58:36 +0800312
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600313}
314
Tony Barbourf43b6982014-11-25 13:18:32 -0700315void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600316{
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600317 XGL_RESULT err;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600318
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600319 ASSERT_NO_FATAL_FAILURE(InitState());
320 ASSERT_NO_FATAL_FAILURE(InitViewport());
321
Tony Barbourf43b6982014-11-25 13:18:32 -0700322 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
323 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600324
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600325
Tony Barbourf43b6982014-11-25 13:18:32 -0700326 XglPipelineObj pipelineobj(m_device);
327 pipelineobj.AddShader(&vs);
328 pipelineobj.AddShader(&ps);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600329
330 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
331
Tony Barbourf43b6982014-11-25 13:18:32 -0700332 // Create descriptor set
333 XglDescriptorSetObj descriptorSet(m_device);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600334
335 // Build command buffer
336 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
337 ASSERT_XGL_SUCCESS(err);
338
339 GenerateClearAndPrepareBufferCmds();
340 GenerateBindRenderTargetCmd();
Tony Barbourf43b6982014-11-25 13:18:32 -0700341 GenerateBindStateAndPipelineCmds();
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600342
Tobin Ehlis31446e52014-11-28 11:17:19 -0700343#ifdef DUMP_STATE_DOT
344 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
345 pDSDumpDot((char*)"triUniFS.dot");
346#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700347 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
348 descriptorSet.BindCommandBuffer(m_cmdBuffer);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600349
Tony Barbourf43b6982014-11-25 13:18:32 -0700350 // render the triangle
351 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600352
353 // finalize recording of the command buffer
354 err = xglEndCommandBuffer( m_cmdBuffer );
355 ASSERT_XGL_SUCCESS( err );
356
357 // this command buffer only uses the vertex buffer memory
358 m_numMemRefs = 0;
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600359
360 // submit the command buffer to the universal queue
361 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
362 ASSERT_XGL_SUCCESS( err );
363
364 err = xglQueueWaitIdle( m_device->m_queue );
365 ASSERT_XGL_SUCCESS( err );
366
367 // Wait for work to finish before cleaning up.
368 xglDeviceWaitIdle(m_device->device());
369
370 RecordImage(m_renderTarget);
371
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600372}
373
Tony Barbourf43b6982014-11-25 13:18:32 -0700374void XglRenderTest::RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
375 XglConstantBufferObj constantBuffer)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600376{
377 int i;
378 glm::mat4 MVP;
379 int matrixSize = sizeof(MVP);
380 XGL_RESULT err;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600381
382 for (i = 0; i < 8; i++) {
383 XGL_UINT8 *pData;
Tony Barbourf43b6982014-11-25 13:18:32 -0700384 err = xglMapMemory(constantBuffer.m_constantBufferMem, 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600385 ASSERT_XGL_SUCCESS(err);
386
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600387 Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
388 MVP = Projection * View * Model;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600389 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
390
Tony Barbourf43b6982014-11-25 13:18:32 -0700391 err = xglUnmapMemory(constantBuffer.m_constantBufferMem);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600392 ASSERT_XGL_SUCCESS(err);
393
394 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700395 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_memoryRefManager.GetNumRefs(), m_memoryRefManager.GetMemoryRefList(), NULL );
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600396 ASSERT_XGL_SUCCESS( err );
397
398 err = xglQueueWaitIdle( m_device->m_queue );
399 ASSERT_XGL_SUCCESS( err );
400
401 // Wait for work to finish before cleaning up.
402 xglDeviceWaitIdle(m_device->device());
403
404 RecordImage(m_renderTarget);
405 }
Cody Northrop7a1f0462014-10-10 14:49:36 -0600406}
407
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600408void dumpMatrix(const char *note, glm::mat4 MVP)
409{
410 int i,j;
411
412 printf("%s: \n", note);
413 for (i=0; i<4; i++) {
414 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
415 }
416 printf("\n");
417 fflush(stdout);
418}
419
420void dumpVec4(const char *note, glm::vec4 vector)
421{
422 printf("%s: \n", note);
423 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
424 printf("\n");
425 fflush(stdout);
426}
427
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600428void XglRenderTest::GenerateClearAndPrepareBufferCmds()
429{
430 XglRenderFramework::GenerateClearAndPrepareBufferCmds();
431
432 if (0) {
433// if (m_depthStencilImage) {
434 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
435 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
436 dsRange.baseMipLevel = 0;
437 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
438 dsRange.baseArraySlice = 0;
439 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
440
441 // prepare the depth buffer for clear
442 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
443 transitionToClear.image = m_depthStencilImage;
444 transitionToClear.oldState = m_depthStencilBinding.depthState;
445 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
446 transitionToClear.subresourceRange = dsRange;
447 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
448 m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
449
450 xglCmdClearDepthStencil(m_cmdBuffer, m_depthStencilImage, 1.0f, 0, 1, &dsRange);
451
452 // prepare depth buffer for rendering
453 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
454 transitionToRender.image = m_renderTarget->image();
455 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
456 transitionToRender.newState = m_depthStencilBinding.depthState;
457 transitionToRender.subresourceRange = dsRange;
458 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
459 m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
460 }
461}
462
Cody Northrop7a1f0462014-10-10 14:49:36 -0600463
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600464void XglRenderTest::ClearDepthStencil(XGL_FLOAT value)
465/* clear the buffer */
466{
467 XGL_RESULT err;
468 const uint16_t depth_value = (uint16_t) (value * 65535);
469 const XGL_INT tw = 128 / sizeof(uint16_t);
470 const XGL_INT th = 32;
471 XGL_INT i, j, w, h;
472 XGL_VOID *data;
473
474 w = (m_width + tw - 1) / tw;
475 h = (m_height + th - 1) / th;
476
477 err = xglMapMemory(m_depthStencilMem, 0, &data);
478 ASSERT_XGL_SUCCESS(err);
479
Tony Barbourf43b6982014-11-25 13:18:32 -0700480#ifdef DUMP_STATE_DOT
481 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
482 pDSDumpDot((char*)"triVtxFetchAndMVP.dot");
483#endif
484
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600485 for (i = 0; i < w * h; i++) {
486 uint16_t *tile = (uint16_t *) ((char *) data + 4096 * i);
487
488 for (j = 0; j < 2048; j++)
489 tile[j] = depth_value;
490 }
491
492 err = xglUnmapMemory(m_depthStencilMem);
493 ASSERT_XGL_SUCCESS(err);
494}
495
496void XglRenderTest::ClearRenderBuffer(XGL_UINT32 clear_color)
497/* clear the buffer */
498{
499 XGL_RESULT err;
500 const XGL_IMAGE_SUBRESOURCE sr = {
501 XGL_IMAGE_ASPECT_COLOR, 0, 0
502 };
503 XGL_SUBRESOURCE_LAYOUT sr_layout;
504 XGL_UINT data_size = sizeof(sr_layout);
505 XGL_VOID *ptr;
506
507 err = xglGetImageSubresourceInfo( m_renderTarget->image(),
508 &sr, XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
509 &data_size, &sr_layout);
510 ASSERT_XGL_SUCCESS( err );
511 ASSERT_EQ(data_size, sizeof(sr_layout));
512
513 err = m_renderTarget->MapMemory( &ptr );
514 ASSERT_XGL_SUCCESS( err );
515
516 ptr = (void *) ((char *) ptr + sr_layout.offset);
517
518 memset(ptr, clear_color, m_width * m_height *sizeof(XGL_UINT32));
519
520 err = m_renderTarget->UnmapMemory();
521 ASSERT_XGL_SUCCESS(err);
522}
523
524void XglRenderTest::InitDepthStencil()
525{
526 XGL_RESULT err;
527 XGL_IMAGE_CREATE_INFO image;
528 XGL_MEMORY_ALLOC_INFO mem_alloc;
529 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
530 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn6317c492014-11-21 11:33:20 -0700531 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600532
533 // Clean up default state created by framework
534 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
535
536 m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16;
537 m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS;
538
539 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
540 image.pNext = NULL;
541 image.imageType = XGL_IMAGE_2D;
542 image.format = m_depth_stencil_fmt;
543 image.extent.width = m_width;
544 image.extent.height = m_height;
545 image.extent.depth = 1;
546 image.mipLevels = 1;
547 image.arraySize = 1;
548 image.samples = 1;
549 image.tiling = XGL_OPTIMAL_TILING;
550 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
551 image.flags = 0;
552
553 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
554 mem_alloc.pNext = NULL;
555 mem_alloc.allocationSize = 0;
556 mem_alloc.alignment = 0;
557 mem_alloc.flags = 0;
558 mem_alloc.heapCount = 0;
559 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
560
561 /* create image */
562 err = xglCreateImage(device(), &image,
563 &m_depthStencilImage);
564 ASSERT_XGL_SUCCESS(err);
565
566 err = xglGetObjectInfo(m_depthStencilImage,
567 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
568 &mem_reqs_size, &mem_reqs);
569 ASSERT_XGL_SUCCESS(err);
570 ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs));
571
572 mem_alloc.allocationSize = mem_reqs.size;
573 mem_alloc.alignment = mem_reqs.alignment;
574 mem_alloc.heapCount = mem_reqs.heapCount;
575 memcpy(mem_alloc.heaps, mem_reqs.heaps,
576 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
577
578 /* allocate memory */
579 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem);
580 ASSERT_XGL_SUCCESS(err);
581
582 /* bind memory */
583 err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0);
584 ASSERT_XGL_SUCCESS(err);
585
586 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
587 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
588 depthStencil.depthTestEnable = XGL_TRUE;
589 depthStencil.depthWriteEnable = XGL_TRUE;
590 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
591 depthStencil.depthBoundsEnable = XGL_FALSE;
592 depthStencil.minDepth = 0.f;
593 depthStencil.maxDepth = 1.f;
594 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
595 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
596 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
597 depthStencil.back.stencilRef = 0x00;
598 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
599 depthStencil.front = depthStencil.back;
600
601 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
602 ASSERT_XGL_SUCCESS( err );
603
604 /* create image view */
605 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
606 view.pNext = NULL;
607 view.image = XGL_NULL_HANDLE;
608 view.mipLevel = 0;
609 view.baseArraySlice = 0;
610 view.arraySize = 1;
611 view.flags = 0;
612 view.image = m_depthStencilImage;
613 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
614 ASSERT_XGL_SUCCESS(err);
615
616 m_depthStencilBinding.view = m_depthStencilView;
617 m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
618 m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
619}
620
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600621struct xgltriangle_vs_uniform {
622 // Must start with MVP
623 XGL_FLOAT mvp[4][4];
624 XGL_FLOAT position[3][4];
625 XGL_FLOAT color[3][4];
626};
627
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600628void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600629{
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700630#ifdef DEBUG_CALLBACK
631 xglDbgRegisterMsgCallback(myDbgFunc, NULL);
632#endif
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600633 // Create identity matrix
634 int i;
635 struct xgltriangle_vs_uniform data;
636
637 glm::mat4 Projection = glm::mat4(1.0f);
638 glm::mat4 View = glm::mat4(1.0f);
639 glm::mat4 Model = glm::mat4(1.0f);
640 glm::mat4 MVP = Projection * View * Model;
641 const int matrixSize = sizeof(MVP);
642 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT);
643 memcpy(&data.mvp, &MVP[0][0], matrixSize);
644
645 static const Vertex tri_data[] =
646 {
647 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
648 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
649 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
650 };
651
652 for (i=0; i<3; i++) {
653 data.position[i][0] = tri_data[i].posX;
654 data.position[i][1] = tri_data[i].posY;
655 data.position[i][2] = tri_data[i].posZ;
656 data.position[i][3] = tri_data[i].posW;
657 data.color[i][0] = tri_data[i].r;
658 data.color[i][1] = tri_data[i].g;
659 data.color[i][2] = tri_data[i].b;
660 data.color[i][3] = tri_data[i].a;
661 }
662
Tony Barbourf43b6982014-11-25 13:18:32 -0700663 ASSERT_NO_FATAL_FAILURE(InitState());
664 ASSERT_NO_FATAL_FAILURE(InitViewport());
665
666 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data);
667
668 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
669 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
670 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
671
672 XglPipelineObj pipelineobj(m_device);
673 pipelineobj.AddShader(&vs);
674 pipelineobj.AddShader(&ps);
675
676 XglDescriptorSetObj descriptorSet(m_device);
677 descriptorSet.AttachMemoryView(&constantBuffer);
678 m_memoryRefManager.AddMemoryRef(&constantBuffer);
679
680 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
681 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
682
683 RotateTriangleVSUniform(Projection, View, Model, constantBuffer);
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700684#ifdef PRINT_OBJECTS
685 //XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)
686 OBJ_TRACK_GET_OBJECT_COUNT pObjTrackGetObjectCount = (OBJ_TRACK_GET_OBJECT_COUNT)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjectCount");
687 XGL_UINT64 numObjects = pObjTrackGetObjectCount(XGL_OBJECT_TYPE_ANY);
688 //OBJ_TRACK_GET_OBJECTS pGetObjsFunc = xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
689 printf("DEBUG : Number of Objects : %lu\n", numObjects);
690 OBJ_TRACK_GET_OBJECTS pObjTrackGetObjs = (OBJ_TRACK_GET_OBJECTS)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
691 OBJTRACK_NODE* pObjNodeArray = (OBJTRACK_NODE*)malloc(sizeof(OBJTRACK_NODE)*numObjects);
692 pObjTrackGetObjs(XGL_OBJECT_TYPE_ANY, numObjects, pObjNodeArray);
693 for (i=0; i < numObjects; i++) {
694 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);
695 }
696 free(pObjNodeArray);
697#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700698
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600699}
700
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600701TEST_F(XglRenderTest, XGLTriangle_FragColor)
702{
703 static const char *vertShaderText =
704 "#version 140\n"
705 "#extension GL_ARB_separate_shader_objects : enable\n"
706 "#extension GL_ARB_shading_language_420pack : enable\n"
707 "\n"
708 "layout(binding = 0) uniform buf {\n"
709 " mat4 MVP;\n"
710 " vec4 position[3];\n"
711 " vec4 color[3];\n"
712 "} ubuf;\n"
713 "\n"
714 "layout (location = 0) out vec4 outColor;\n"
715 "\n"
716 "void main() \n"
717 "{\n"
718 " outColor = ubuf.color[gl_VertexID];\n"
719 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
720 "}\n";
721
722 static const char *fragShaderText =
723 "#version 140\n"
724 "#extension GL_ARB_separate_shader_objects : enable\n"
725 "#extension GL_ARB_shading_language_420pack : enable\n"
726 "\n"
727 "layout (location = 0) in vec4 inColor;\n"
728 "\n"
729 "void main()\n"
730 "{\n"
731 " gl_FragColor = inColor;\n"
732 "}\n";
733
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600734 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600735 XGLTriangleTest(vertShaderText, fragShaderText);
736}
737
738TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
739{
740 static const char *vertShaderText =
741 "#version 140\n"
742 "#extension GL_ARB_separate_shader_objects : enable\n"
743 "#extension GL_ARB_shading_language_420pack : enable\n"
744 "\n"
745 "layout(binding = 0) uniform buf {\n"
746 " mat4 MVP;\n"
747 " vec4 position[3];\n"
748 " vec4 color[3];\n"
749 "} ubuf;\n"
750 "\n"
751 "layout (location = 0) out vec4 outColor;\n"
752 "\n"
753 "void main() \n"
754 "{\n"
755 " outColor = ubuf.color[gl_VertexID];\n"
756 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
757 "}\n";
758
759 static const char *fragShaderText =
760 "#version 140\n"
761 "#extension GL_ARB_separate_shader_objects : enable\n"
762 "#extension GL_ARB_shading_language_420pack : enable\n"
763 "\n"
764 "layout (location = 0) in vec4 inColor;\n"
765 "layout (location = 0) out vec4 outColor;\n"
766 "\n"
767 "void main()\n"
768 "{\n"
769 " outColor = inColor;\n"
770 "}\n";
771
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600772 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 -0600773
774 XGLTriangleTest(vertShaderText, fragShaderText);
775}
776
Tony Barbourf43b6982014-11-25 13:18:32 -0700777TEST_F(XglRenderTest, BIL_XGLTriangle)
778{
779 bool saved_use_bil = XglTestFramework::m_use_bil;
780
781 static const char *vertShaderText =
782 "#version 140\n"
783 "#extension GL_ARB_separate_shader_objects : enable\n"
784 "#extension GL_ARB_shading_language_420pack : enable\n"
785 "\n"
786 "layout(binding = 0) uniform buf {\n"
787 " mat4 MVP;\n"
788 " vec4 position[3];\n"
789 " vec4 color[3];\n"
790 "} ubuf;\n"
791 "\n"
792 "layout (location = 0) out vec4 outColor;\n"
793 "\n"
794 "void main() \n"
795 "{\n"
796 " outColor = ubuf.color[gl_VertexID];\n"
797 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
798 "}\n";
799
800 static const char *fragShaderText =
801 "#version 140\n"
802 "#extension GL_ARB_separate_shader_objects : enable\n"
803 "#extension GL_ARB_shading_language_420pack : enable\n"
804 "\n"
805 "layout (location = 0) in vec4 inColor;\n"
806 "\n"
807 "void main()\n"
808 "{\n"
809 " gl_FragColor = inColor;\n"
810 "}\n";
811
812 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver.");
813
814 XglTestFramework::m_use_bil = true;
815
816 XGLTriangleTest(vertShaderText, fragShaderText);
817
818 XglTestFramework::m_use_bil = saved_use_bil;
819}
820
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600821TEST_F(XglRenderTest, GreenTriangle)
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600822{
823 static const char *vertShaderText =
824 "#version 130\n"
825 "vec2 vertices[3];\n"
826 "void main() {\n"
827 " vertices[0] = vec2(-1.0, -1.0);\n"
828 " vertices[1] = vec2( 1.0, -1.0);\n"
829 " vertices[2] = vec2( 0.0, 1.0);\n"
830 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
831 "}\n";
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600832
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600833 static const char *fragShaderText =
834 "#version 130\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600835 "void main() {\n"
Steve K10b32512014-10-10 08:54:29 -0600836 " gl_FragColor = vec4(0,1,0,1);\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600837 "}\n";
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600838
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600839 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
840
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600841 DrawTriangleTest(vertShaderText, fragShaderText);
842}
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600843
Tony Barbourf43b6982014-11-25 13:18:32 -0700844TEST_F(XglRenderTest, BIL_GreenTriangle)
845{
846 bool saved_use_bil = XglTestFramework::m_use_bil;
847
848 static const char *vertShaderText =
849 "#version 130\n"
850 "vec2 vertices[3];\n"
851 "void main() {\n"
852 " vertices[0] = vec2(-1.0, -1.0);\n"
853 " vertices[1] = vec2( 1.0, -1.0);\n"
854 " vertices[2] = vec2( 0.0, 1.0);\n"
855 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
856 "}\n";
857
858 static const char *fragShaderText =
859 "#version 130\n"
860 "void main() {\n"
861 " gl_FragColor = vec4(0,1,0,1);\n"
862 "}\n";
863
864 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver.");
865
866 XglTestFramework::m_use_bil = true;
867 DrawTriangleTest(vertShaderText, fragShaderText);
868 XglTestFramework::m_use_bil = saved_use_bil;
869}
870
871TEST_F(XglRenderTest, YellowTriangle)
872{
873 static const char *vertShaderText =
874 "#version 130\n"
875 "void main() {\n"
876 " vec2 vertices[3];"
877 " vertices[0] = vec2(-0.5, -0.5);\n"
878 " vertices[1] = vec2( 0.5, -0.5);\n"
879 " vertices[2] = vec2( 0.5, 0.5);\n"
880 " vec4 colors[3];\n"
881 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
882 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
883 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
884 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
885 "}\n";
886
887 static const char *fragShaderText =
888 "#version 130\n"
889 "void main() {\n"
890 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
891 "}\n";
892
893 DrawTriangleTest(vertShaderText, fragShaderText);
894}
895
896TEST_F(XglRenderTest, TriangleTwoFSUniforms)
897{
898 static const char *vertShaderText =
899 "#version 130\n"
900 "out vec4 color;\n"
901 "out vec4 scale;\n"
902 "out vec2 samplePos;\n"
903 "void main() {\n"
904 " vec2 vertices[3];"
905 " vertices[0] = vec2(-0.5, -0.5);\n"
906 " vertices[1] = vec2( 0.5, -0.5);\n"
907 " vertices[2] = vec2( 0.5, 0.5);\n"
908 " vec4 colors[3];\n"
909 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
910 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
911 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
912 " color = colors[gl_VertexID % 3];\n"
913 " vec2 positions[3];"
914 " positions[0] = vec2( 0.0, 0.0);\n"
915 " positions[1] = vec2( 1.0, 0.0);\n"
916 " positions[2] = vec2( 1.0, 1.0);\n"
917 " scale = vec4(0.0, 0.0, 0.0, 0.0);\n"
918 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
919 "}\n";
920
921
922 static const char *fragShaderText =
923 "#version 430\n"
924 "in vec4 color;\n"
925 "in vec4 scale;\n"
926 "uniform vec4 foo;\n"
927 "uniform vec4 bar;\n"
928 "void main() {\n"
929 // by default, with no location or blocks
930 // the compiler will read them from buffer
931 // in reverse order of first use in shader
932 // The buffer contains red, followed by blue,
933 // so foo should be blue, bar should be red
934 " gl_FragColor = color * scale * foo * bar + foo;\n"
935 "}\n";
936
937 ASSERT_NO_FATAL_FAILURE(InitState());
938 ASSERT_NO_FATAL_FAILURE(InitViewport());
939
940 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
941 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
942
943 const int constantCount = 8;
944 const float constants[constantCount] = { 1.0, 0.0, 0.0, 1.0,
945 0.0, 0.0, 1.0, 1.0 };
946 XglConstantBufferObj constantBuffer(m_device,constantCount, sizeof(constants[0]), (const void*) constants);
947 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
948
949 XglPipelineObj pipelineobj(m_device);
950 pipelineobj.AddShader(&vs);
951 pipelineobj.AddShader(&ps);
952
953 XglDescriptorSetObj descriptorSet(m_device);
954 descriptorSet.AttachMemoryView(&constantBuffer);
955
956 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
957 QueueCommandBuffer(NULL, 0);
958
959}
960
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600961TEST_F(XglRenderTest, TriangleWithVertexFetch)
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600962{
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600963 static const char *vertShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700964 "#version 130\n"
965 //XYZ1( -1, -1, -1 )
966 "in vec4 pos;\n"
967 //XYZ1( 0.f, 0.f, 0.f )
968 "in vec4 inColor;\n"
969 "out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600970 "void main() {\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -0600971 " outColor = inColor;\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600972 " gl_Position = pos;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600973 "}\n";
974
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600975
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600976 static const char *fragShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700977 "#version 430\n"
978 "in vec4 color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600979 "void main() {\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600980 " gl_FragColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600981 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600982
Tony Barbourf43b6982014-11-25 13:18:32 -0700983
984
985 ASSERT_NO_FATAL_FAILURE(InitState());
986 ASSERT_NO_FATAL_FAILURE(InitViewport());
987
988 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
989 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
990
991 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
992 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
993
994 XglPipelineObj pipelineobj(m_device);
995 pipelineobj.AddShader(&vs);
996 pipelineobj.AddShader(&ps);
997
998 XglDescriptorSetObj descriptorSet(m_device);
999 descriptorSet.AttachMemoryView(&meshBuffer);
1000
1001 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1002 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1003 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1004 };
1005
1006 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1007 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1008 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1009 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1010 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1011 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1012 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1013 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1014 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1015
1016 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1017 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1018 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1019
1020 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1021 QueueCommandBuffer(NULL, 0);
1022
Tobin Ehlis34e0e442014-10-07 14:41:29 -06001023}
1024
GregF6bef1212014-12-02 15:41:44 -07001025TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1026{
1027 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001028
GregF6bef1212014-12-02 15:41:44 -07001029 static const char *vertShaderText =
1030 "#version 140\n"
1031 "#extension GL_ARB_separate_shader_objects : enable\n"
1032 "#extension GL_ARB_shading_language_420pack : enable\n"
1033 "layout (location = 0) in vec4 pos;\n"
1034 "layout (location = 0) out vec4 outColor;\n"
1035 "layout (location = 1) out vec4 outColor2;\n"
1036 "void main() {\n"
1037 " gl_Position = pos;\n"
1038 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1039 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1040 "}\n";
1041
1042 static const char *fragShaderText =
1043 //"#version 140\n"
1044 "#version 330\n"
1045 "#extension GL_ARB_separate_shader_objects : enable\n"
1046 "#extension GL_ARB_shading_language_420pack : enable\n"
1047 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1048 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1049 "layout (location = 0) in vec4 color;\n"
1050 "layout (location = 1) in vec4 color2;\n"
1051 "void main() {\n"
1052 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1053 " float dist_squared = dot(pos, pos);\n"
1054 " gl_FragColor = (dist_squared < 400.0)\n"
1055 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1056 " : color2;\n"
1057 "}\n";
1058
1059 ASSERT_NO_FATAL_FAILURE(InitState());
1060 ASSERT_NO_FATAL_FAILURE(InitViewport());
1061
1062 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1063 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1064
1065 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1066 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1067
1068 XglPipelineObj pipelineobj(m_device);
1069 pipelineobj.AddShader(&vs);
1070 pipelineobj.AddShader(&ps);
1071
1072 XglDescriptorSetObj descriptorSet(m_device);
1073 descriptorSet.AttachMemoryView(&meshBuffer);
1074
1075 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1076 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1077 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1078 };
1079
1080 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1081 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1082 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1083 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1084 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1085 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1086 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1087 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1088 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1089
1090 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1091 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1092 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1093
1094 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1095 QueueCommandBuffer(NULL, 0);
1096
1097}
1098
1099TEST_F(XglRenderTest, RedCirclesonBlue)
1100{
1101 // This tests that we correctly handle unread fragment inputs
1102
1103 static const char *vertShaderText =
1104 "#version 140\n"
1105 "#extension GL_ARB_separate_shader_objects : enable\n"
1106 "#extension GL_ARB_shading_language_420pack : enable\n"
1107 "layout (location = 0) in vec4 pos;\n"
1108 "layout (location = 0) out vec4 outColor;\n"
1109 "layout (location = 1) out vec4 outColor2;\n"
1110 "void main() {\n"
1111 " gl_Position = pos;\n"
1112 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1113 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1114 "}\n";
1115
1116 static const char *fragShaderText =
1117 //"#version 140\n"
1118 "#version 330\n"
1119 "#extension GL_ARB_separate_shader_objects : enable\n"
1120 "#extension GL_ARB_shading_language_420pack : enable\n"
1121 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1122 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1123 "layout (location = 0) in vec4 color;\n"
1124 "layout (location = 1) in vec4 color2;\n"
1125 "void main() {\n"
1126 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1127 " float dist_squared = dot(pos, pos);\n"
1128 " gl_FragColor = (dist_squared < 400.0)\n"
1129 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1130 " : color2;\n"
1131 "}\n";
1132
1133 ASSERT_NO_FATAL_FAILURE(InitState());
1134 ASSERT_NO_FATAL_FAILURE(InitViewport());
1135
1136 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1137 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1138
1139 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1140 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1141
1142 XglPipelineObj pipelineobj(m_device);
1143 pipelineobj.AddShader(&vs);
1144 pipelineobj.AddShader(&ps);
1145
1146 XglDescriptorSetObj descriptorSet(m_device);
1147 descriptorSet.AttachMemoryView(&meshBuffer);
1148
1149 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1150 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1151 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1152 };
1153
1154 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1155 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1156 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1157 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1158 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1159 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1160 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1161 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1162 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1163
1164 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1165 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1166 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1167
1168 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1169 QueueCommandBuffer(NULL, 0);
1170
1171}
1172
1173TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1174{
1175 // This tests reading gl_ClipDistance from FS
1176
1177 static const char *vertShaderText =
1178 "#version 330\n"
1179 "#extension GL_ARB_separate_shader_objects : enable\n"
1180 "#extension GL_ARB_shading_language_420pack : enable\n"
1181 "out gl_PerVertex {\n"
1182 " vec4 gl_Position;\n"
1183 " float gl_ClipDistance[1];\n"
1184 "};\n"
1185 "layout (location = 0) in vec4 pos;\n"
1186 "layout (location = 0) out vec4 outColor;\n"
1187 "layout (location = 1) out vec4 outColor2;\n"
1188 "void main() {\n"
1189 " gl_Position = pos;\n"
1190 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1191 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1192 " float dists[3];\n"
1193 " dists[0] = 0.0;\n"
1194 " dists[1] = 1.0;\n"
1195 " dists[2] = 1.0;\n"
1196 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1197 "}\n";
1198
1199
1200 static const char *fragShaderText =
1201 //"#version 140\n"
1202 "#version 330\n"
1203 "#extension GL_ARB_separate_shader_objects : enable\n"
1204 "#extension GL_ARB_shading_language_420pack : enable\n"
1205 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1206 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1207 "layout (location = 0) in vec4 color;\n"
1208 "layout (location = 1) in vec4 color2;\n"
1209 "void main() {\n"
1210 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1211 " float dist_squared = dot(pos, pos);\n"
1212 " gl_FragColor = (dist_squared < 400.0)\n"
1213 " ? color * gl_ClipDistance[0]\n"
1214 " : color2;\n"
1215 "}\n";
1216
1217 ASSERT_NO_FATAL_FAILURE(InitState());
1218 ASSERT_NO_FATAL_FAILURE(InitViewport());
1219
1220 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1221 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1222
1223 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1224 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1225
1226 XglPipelineObj pipelineobj(m_device);
1227 pipelineobj.AddShader(&vs);
1228 pipelineobj.AddShader(&ps);
1229
1230 XglDescriptorSetObj descriptorSet(m_device);
1231 descriptorSet.AttachMemoryView(&meshBuffer);
1232
1233 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1234 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1235 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1236 };
1237
1238 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1239 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1240 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1241 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1242 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1243 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1244 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1245 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1246 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1247
1248 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1249 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1250 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1251
1252 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1253 QueueCommandBuffer(NULL, 0);
1254
1255}
Tony Barbourf43b6982014-11-25 13:18:32 -07001256
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001257TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001258{
1259 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001260 "#version 140\n"
1261 "#extension GL_ARB_separate_shader_objects : enable\n"
1262 "#extension GL_ARB_shading_language_420pack : enable\n"
1263 "\n"
1264 "layout(binding = 0) uniform buf {\n"
1265 " mat4 MVP;\n"
1266 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001267 "void main() {\n"
1268 " vec2 vertices[3];"
1269 " vertices[0] = vec2(-0.5, -0.5);\n"
1270 " vertices[1] = vec2( 0.5, -0.5);\n"
1271 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001272 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001273 "}\n";
1274
1275 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001276 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001277 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001278 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001279 "}\n";
1280
Tony Barbourf43b6982014-11-25 13:18:32 -07001281 ASSERT_NO_FATAL_FAILURE(InitState());
1282 ASSERT_NO_FATAL_FAILURE(InitViewport());
1283
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001284 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001285 glm::mat4 Projection = glm::mat4(1.0f);
1286 glm::mat4 View = glm::mat4(1.0f);
1287 glm::mat4 Model = glm::mat4(1.0f);
1288 glm::mat4 MVP = Projection * View * Model;
1289 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1290
Tony Barbourf43b6982014-11-25 13:18:32 -07001291 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1292 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1293 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001294
Tony Barbourf43b6982014-11-25 13:18:32 -07001295 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1296
1297 XglPipelineObj pipelineobj(m_device);
1298 pipelineobj.AddShader(&vs);
1299 pipelineobj.AddShader(&ps);
1300
1301 // Create descriptor set and attach the constant buffer to it
1302 XglDescriptorSetObj descriptorSet(m_device);
1303 descriptorSet.AttachMemoryView(&MVPBuffer);
1304
1305 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1306
1307 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1308 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1309
1310 RotateTriangleVSUniform(Projection, View, Model, MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001311}
1312
Tony Barbourf43b6982014-11-25 13:18:32 -07001313TEST_F(XglRenderTest, MixTriangle)
1314{
1315 // This tests location applied to varyings. Notice that we have switched foo
1316 // and bar in the FS. The triangle should be blended with red, green and blue
1317 // corners.
1318 static const char *vertShaderText =
1319 "#version 140\n"
1320 "#extension GL_ARB_separate_shader_objects : enable\n"
1321 "#extension GL_ARB_shading_language_420pack : enable\n"
1322 "layout (location=0) out vec4 bar;\n"
1323 "layout (location=1) out vec4 foo;\n"
1324 "layout (location=2) out float scale;\n"
1325 "vec2 vertices[3];\n"
1326 "void main() {\n"
1327 " vertices[0] = vec2(-1.0, -1.0);\n"
1328 " vertices[1] = vec2( 1.0, -1.0);\n"
1329 " vertices[2] = vec2( 0.0, 1.0);\n"
1330 "vec4 colors[3];\n"
1331 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1332 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1333 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1334 " foo = colors[gl_VertexID % 3];\n"
1335 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1336 " scale = 1.0;\n"
1337 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1338 "}\n";
1339
1340 static const char *fragShaderText =
1341 "#version 140\n"
1342 "#extension GL_ARB_separate_shader_objects : enable\n"
1343 "#extension GL_ARB_shading_language_420pack : enable\n"
1344 "layout (location = 1) in vec4 bar;\n"
1345 "layout (location = 0) in vec4 foo;\n"
1346 "layout (location = 2) in float scale;\n"
1347 "void main() {\n"
1348 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1349 "}\n";
1350
1351 ASSERT_NO_FATAL_FAILURE(InitState());
1352 ASSERT_NO_FATAL_FAILURE(InitViewport());
1353
1354 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1355 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1356
1357 XglPipelineObj pipelineobj(m_device);
1358 pipelineobj.AddShader(&vs);
1359 pipelineobj.AddShader(&ps);
1360
1361 XglDescriptorSetObj descriptorSet(m_device);
1362
1363 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1364 QueueCommandBuffer(NULL, 0);
1365}
1366
1367TEST_F(XglRenderTest, TriVertFetchAndVertID)
1368{
1369 // This tests that attributes work in the presence of gl_VertexID
1370
1371 static const char *vertShaderText =
1372 "#version 140\n"
1373 "#extension GL_ARB_separate_shader_objects : enable\n"
1374 "#extension GL_ARB_shading_language_420pack : enable\n"
1375 //XYZ1( -1, -1, -1 )
1376 "layout (location = 0) in vec4 pos;\n"
1377 //XYZ1( 0.f, 0.f, 0.f )
1378 "layout (location = 1) in vec4 inColor;\n"
1379 "layout (location = 0) out vec4 outColor;\n"
1380 "void main() {\n"
1381 " outColor = inColor;\n"
1382 " vec4 vertices[3];"
1383 " vertices[gl_VertexID % 3] = pos;\n"
1384 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1385 "}\n";
1386
1387
1388 static const char *fragShaderText =
1389 "#version 140\n"
1390 "#extension GL_ARB_separate_shader_objects : enable\n"
1391 "#extension GL_ARB_shading_language_420pack : enable\n"
1392 "layout (location = 0) in vec4 color;\n"
1393 "void main() {\n"
1394 " gl_FragColor = color;\n"
1395 "}\n";
1396
1397 ASSERT_NO_FATAL_FAILURE(InitState());
1398 ASSERT_NO_FATAL_FAILURE(InitViewport());
1399
1400 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1401 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1402
1403 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1404 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1405
1406 XglPipelineObj pipelineobj(m_device);
1407 pipelineobj.AddShader(&vs);
1408 pipelineobj.AddShader(&ps);
1409
1410 XglDescriptorSetObj descriptorSet(m_device);
1411 descriptorSet.AttachMemoryView(&meshBuffer);
1412
1413 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1414 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1415 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1416 };
1417
1418 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1419 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1420 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1421 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1422 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1423 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1424 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1425 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1426 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1427
1428 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1429 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1430 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1431
1432 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1433 QueueCommandBuffer(NULL, 0);
1434}
1435
1436TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1437{
1438 // This tests that attributes work in the presence of gl_VertexID
1439 // and a dead attribute in position 0. Draws a triangle with yellow,
1440 // red and green corners, starting at top and going clockwise.
1441
1442 static const char *vertShaderText =
1443 "#version 140\n"
1444 "#extension GL_ARB_separate_shader_objects : enable\n"
1445 "#extension GL_ARB_shading_language_420pack : enable\n"
1446 //XYZ1( -1, -1, -1 )
1447 "layout (location = 0) in vec4 pos;\n"
1448 //XYZ1( 0.f, 0.f, 0.f )
1449 "layout (location = 1) in vec4 inColor;\n"
1450 "layout (location = 0) out vec4 outColor;\n"
1451 "void main() {\n"
1452 " outColor = inColor;\n"
1453 " vec2 vertices[3];"
1454 " vertices[0] = vec2(-1.0, -1.0);\n"
1455 " vertices[1] = vec2( 1.0, -1.0);\n"
1456 " vertices[2] = vec2( 0.0, 1.0);\n"
1457 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1458 "}\n";
1459
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 = 0) in vec4 color;\n"
1466 "void main() {\n"
1467 " gl_FragColor = color;\n"
1468 "}\n";
1469
1470 ASSERT_NO_FATAL_FAILURE(InitState());
1471 ASSERT_NO_FATAL_FAILURE(InitViewport());
1472
1473 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1474 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1475
1476 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1477 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1478
1479 XglPipelineObj pipelineobj(m_device);
1480 pipelineobj.AddShader(&vs);
1481 pipelineobj.AddShader(&ps);
1482
1483 XglDescriptorSetObj descriptorSet(m_device);
1484 descriptorSet.AttachMemoryView(&meshBuffer);
1485
1486 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1487 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1488 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1489 };
1490
1491 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1492 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1493 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1494 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1495 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1496 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1497 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1498 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1499 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1500
1501 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1502 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1503 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1504
1505 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1506 QueueCommandBuffer(NULL, 0);
1507
1508}
1509
1510TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001511{
1512 static const char *vertShaderText =
1513 "#version 140\n"
1514 "layout (std140) uniform bufferVals {\n"
1515 " mat4 mvp;\n"
1516 "} myBufferVals;\n"
1517 "in vec4 pos;\n"
1518 "in vec4 inColor;\n"
1519 "out vec4 outColor;\n"
1520 "void main() {\n"
1521 " outColor = inColor;\n"
1522 " gl_Position = myBufferVals.mvp * pos;\n"
1523 "}\n";
1524
1525 static const char *fragShaderText =
1526 "#version 130\n"
1527 "in vec4 color;\n"
1528 "void main() {\n"
1529 " gl_FragColor = color;\n"
1530 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001531 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001532
Tony Barbourf43b6982014-11-25 13:18:32 -07001533 glm::mat4 View = glm::lookAt(
1534 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1535 glm::vec3(0,0,0), // and looks at the origin
1536 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1537 );
1538
1539 glm::mat4 Model = glm::mat4(1.0f);
1540
1541 glm::mat4 MVP = Projection * View * Model;
1542
1543 ASSERT_NO_FATAL_FAILURE(InitState());
1544 ASSERT_NO_FATAL_FAILURE(InitViewport());
1545 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1546
1547 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1548 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1549
1550 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1551
1552 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1553 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1554 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1555
1556 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1557
1558 XglPipelineObj pipelineobj(m_device);
1559 pipelineobj.AddShader(&vs);
1560 pipelineobj.AddShader(&ps);
1561
1562 XglDescriptorSetObj descriptorSet(m_device);
1563 descriptorSet.AttachMemoryView(&MVPBuffer);
1564
1565 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1566 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1567
1568 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1569 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1570 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1571 };
1572
1573 // this is the current description of g_vbData
1574 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1575 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1576 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1577 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1578 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1579 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1580 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1581 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1582 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1583
1584 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1585 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1586 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1587
1588 ClearDepthStencil(1.0f);
1589 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
1590
1591 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1592
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001593}
1594
Tony Barbourf43b6982014-11-25 13:18:32 -07001595TEST_F(XglRenderTest, VSTexture)
1596{
1597 // The expected result from this test is a green and red triangle;
1598 // one red vertex on the left, two green vertices on the right.
1599 static const char *vertShaderText =
1600 "#version 130\n"
1601 "out vec4 texColor;\n"
1602 "uniform sampler2D surface;\n"
1603 "void main() {\n"
1604 " vec2 vertices[3];"
1605 " vertices[0] = vec2(-0.5, -0.5);\n"
1606 " vertices[1] = vec2( 0.5, -0.5);\n"
1607 " vertices[2] = vec2( 0.5, 0.5);\n"
1608 " vec2 positions[3];"
1609 " positions[0] = vec2( 0.0, 0.0);\n"
1610 " positions[1] = vec2( 0.25, 0.1);\n"
1611 " positions[2] = vec2( 0.1, 0.25);\n"
1612 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1613 " texColor = textureLod(surface, samplePos, 0.0);\n"
1614 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1615 "}\n";
1616
1617 static const char *fragShaderText =
1618 "#version 130\n"
1619 "in vec4 texColor;\n"
1620 "void main() {\n"
1621 " gl_FragColor = texColor;\n"
1622 "}\n";
1623
1624 ASSERT_NO_FATAL_FAILURE(InitState());
1625 ASSERT_NO_FATAL_FAILURE(InitViewport());
1626
1627 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1628 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1629 XglSamplerObj sampler(m_device);
1630 XglTextureObj texture(m_device);
1631
1632 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1633 vs.BindShaderEntitySlotToSampler(0, &sampler);
1634
1635 XglPipelineObj pipelineobj(m_device);
1636 pipelineobj.AddShader(&vs);
1637 pipelineobj.AddShader(&ps);
1638
1639 XglDescriptorSetObj descriptorSet(m_device);
1640 descriptorSet.AttachImageView(&texture);
1641 descriptorSet.AttachSampler(&sampler);
1642
1643 m_memoryRefManager.AddMemoryRef(&texture);
1644
1645 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1646 QueueCommandBuffer(NULL, 0);
1647
1648}
1649TEST_F(XglRenderTest, TexturedTriangle)
1650{
1651 // The expected result from this test is a red and green checkered triangle
1652 static const char *vertShaderText =
1653 "#version 140\n"
1654 "#extension GL_ARB_separate_shader_objects : enable\n"
1655 "#extension GL_ARB_shading_language_420pack : enable\n"
1656 "layout (location = 0) out vec2 samplePos;\n"
1657 "void main() {\n"
1658 " vec2 vertices[3];"
1659 " vertices[0] = vec2(-0.5, -0.5);\n"
1660 " vertices[1] = vec2( 0.5, -0.5);\n"
1661 " vertices[2] = vec2( 0.5, 0.5);\n"
1662 " vec2 positions[3];"
1663 " positions[0] = vec2( 0.0, 0.0);\n"
1664 " positions[1] = vec2( 1.0, 0.0);\n"
1665 " positions[2] = vec2( 1.0, 1.0);\n"
1666 " samplePos = positions[gl_VertexID % 3];\n"
1667 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1668 "}\n";
1669
1670 static const char *fragShaderText =
1671 "#version 140\n"
1672 "#extension GL_ARB_separate_shader_objects : enable\n"
1673 "#extension GL_ARB_shading_language_420pack : enable\n"
1674 "layout (location = 0) in vec2 samplePos;\n"
1675 "layout (binding = 0) uniform sampler2D surface;\n"
1676 "layout (location=0) out vec4 outColor;\n"
1677 "void main() {\n"
1678 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1679 " outColor = texColor;\n"
1680 "}\n";
1681
1682 ASSERT_NO_FATAL_FAILURE(InitState());
1683 ASSERT_NO_FATAL_FAILURE(InitViewport());
1684
1685 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1686 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1687 XglSamplerObj sampler(m_device);
1688 XglTextureObj texture(m_device);
1689
1690 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1691 ps.BindShaderEntitySlotToSampler(0, &sampler);
1692
1693 XglPipelineObj pipelineobj(m_device);
1694 pipelineobj.AddShader(&vs);
1695 pipelineobj.AddShader(&ps);
1696
1697 XglDescriptorSetObj descriptorSet(m_device);
1698 descriptorSet.AttachImageView(&texture);
1699 descriptorSet.AttachSampler(&sampler);
1700
1701 m_memoryRefManager.AddMemoryRef(&texture);
1702
1703 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1704 QueueCommandBuffer(NULL, 0);
1705}
1706TEST_F(XglRenderTest, TexturedTriangleClip)
1707{
1708 // The expected result from this test is a red and green checkered triangle
1709 static const char *vertShaderText =
1710 "#version 330\n"
1711 "#extension GL_ARB_separate_shader_objects : enable\n"
1712 "#extension GL_ARB_shading_language_420pack : enable\n"
1713 "layout (location = 0) out vec2 samplePos;\n"
1714 "out gl_PerVertex {\n"
1715 " vec4 gl_Position;\n"
1716 " float gl_ClipDistance[1];\n"
1717 "};\n"
1718 "void main() {\n"
1719 " vec2 vertices[3];"
1720 " vertices[0] = vec2(-0.5, -0.5);\n"
1721 " vertices[1] = vec2( 0.5, -0.5);\n"
1722 " vertices[2] = vec2( 0.5, 0.5);\n"
1723 " vec2 positions[3];"
1724 " positions[0] = vec2( 0.0, 0.0);\n"
1725 " positions[1] = vec2( 1.0, 0.0);\n"
1726 " positions[2] = vec2( 1.0, 1.0);\n"
1727 " float dists[3];\n"
1728 " dists[0] = 1.0;\n"
1729 " dists[1] = 1.0;\n"
1730 " dists[2] = -1.0;\n"
1731 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1732 " samplePos = positions[gl_VertexID % 3];\n"
1733 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1734 "}\n";
1735
1736 static const char *fragShaderText =
1737 "#version 140\n"
1738 "#extension GL_ARB_separate_shader_objects : enable\n"
1739 "#extension GL_ARB_shading_language_420pack : enable\n"
1740 "layout (location = 0) in vec2 samplePos;\n"
1741 "layout (binding = 0) uniform sampler2D surface;\n"
1742 "layout (location=0) out vec4 outColor;\n"
1743 "void main() {\n"
1744 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1745 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1746 " outColor = texColor;\n"
1747 "}\n";
1748
1749
1750 ASSERT_NO_FATAL_FAILURE(InitState());
1751 ASSERT_NO_FATAL_FAILURE(InitViewport());
1752
1753 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1754 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1755 XglSamplerObj sampler(m_device);
1756 XglTextureObj texture(m_device);
1757
1758 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1759 ps.BindShaderEntitySlotToSampler(0, &sampler);
1760
1761 XglPipelineObj pipelineobj(m_device);
1762 pipelineobj.AddShader(&vs);
1763 pipelineobj.AddShader(&ps);
1764
1765 XglDescriptorSetObj descriptorSet(m_device);
1766 descriptorSet.AttachImageView(&texture);
1767 descriptorSet.AttachSampler(&sampler);
1768
1769 m_memoryRefManager.AddMemoryRef(&texture);
1770
1771 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1772 QueueCommandBuffer(NULL, 0);
1773}
1774TEST_F(XglRenderTest, FSTriangle)
1775{
1776 // The expected result from this test is a red and green checkered triangle
1777 static const char *vertShaderText =
1778 "#version 140\n"
1779 "#extension GL_ARB_separate_shader_objects : enable\n"
1780 "#extension GL_ARB_shading_language_420pack : enable\n"
1781 "layout (location = 0) out vec2 samplePos;\n"
1782 "void main() {\n"
1783 " vec2 vertices[3];"
1784 " vertices[0] = vec2(-0.5, -0.5);\n"
1785 " vertices[1] = vec2( 0.5, -0.5);\n"
1786 " vertices[2] = vec2( 0.5, 0.5);\n"
1787 " vec2 positions[3];"
1788 " positions[0] = vec2( 0.0, 0.0);\n"
1789 " positions[1] = vec2( 1.0, 0.0);\n"
1790 " positions[2] = vec2( 1.0, 1.0);\n"
1791 " samplePos = positions[gl_VertexID % 3];\n"
1792 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1793 "}\n";
1794
1795 static const char *fragShaderText =
1796 "#version 140\n"
1797 "#extension GL_ARB_separate_shader_objects : enable\n"
1798 "#extension GL_ARB_shading_language_420pack : enable\n"
1799 "layout (location = 0) in vec2 samplePos;\n"
1800 "layout (binding = 0) uniform sampler2D surface;\n"
1801 "layout (location=0) out vec4 outColor;\n"
1802 "void main() {\n"
1803 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1804 " outColor = texColor;\n"
1805 "}\n";
1806
1807 ASSERT_NO_FATAL_FAILURE(InitState());
1808 ASSERT_NO_FATAL_FAILURE(InitViewport());
1809
1810 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1811 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1812 XglSamplerObj sampler(m_device);
1813 XglTextureObj texture(m_device);
1814
1815 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1816 ps.BindShaderEntitySlotToSampler(0, &sampler);
1817
1818 XglPipelineObj pipelineobj(m_device);
1819 pipelineobj.AddShader(&vs);
1820 pipelineobj.AddShader(&ps);
1821
1822 XglDescriptorSetObj descriptorSet(m_device);
1823 descriptorSet.AttachImageView(&texture);
1824 descriptorSet.AttachSampler(&sampler);
1825
1826 m_memoryRefManager.AddMemoryRef(&texture);
1827
1828 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1829 QueueCommandBuffer(NULL, 0);
1830}
1831TEST_F(XglRenderTest, SamplerBindingsTriangle)
1832{
1833 // This test sets bindings on the samplers
1834 // For now we are asserting that sampler and texture pairs
1835 // march in lock step, and are set via GLSL binding. This can
1836 // and will probably change.
1837 // The sampler bindings should match the sampler and texture slot
1838 // number set up by the application.
1839 // This test will result in a blue triangle
1840 static const char *vertShaderText =
1841 "#version 140\n"
1842 "#extension GL_ARB_separate_shader_objects : enable\n"
1843 "#extension GL_ARB_shading_language_420pack : enable\n"
1844 "layout (location = 0) out vec4 samplePos;\n"
1845 "void main() {\n"
1846 " vec2 vertices[3];"
1847 " vertices[0] = vec2(-0.5, -0.5);\n"
1848 " vertices[1] = vec2( 0.5, -0.5);\n"
1849 " vertices[2] = vec2( 0.5, 0.5);\n"
1850 " vec2 positions[3];"
1851 " positions[0] = vec2( 0.0, 0.0);\n"
1852 " positions[1] = vec2( 1.0, 0.0);\n"
1853 " positions[2] = vec2( 1.0, 1.0);\n"
1854 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
1855 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1856 "}\n";
1857
1858 static const char *fragShaderText =
1859 "#version 140\n"
1860 "#extension GL_ARB_separate_shader_objects : enable\n"
1861 "#extension GL_ARB_shading_language_420pack : enable\n"
1862 "layout (location = 0) in vec4 samplePos;\n"
1863 "layout (binding = 0) uniform sampler2D surface0;\n"
1864 "layout (binding = 1) uniform sampler2D surface1;\n"
1865 "layout (binding = 12) uniform sampler2D surface2;\n"
1866 "void main() {\n"
1867 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
1868 "}\n";
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
1876 XglSamplerObj sampler1(m_device);
1877 XglSamplerObj sampler2(m_device);
1878 XglSamplerObj sampler3(m_device);
1879
1880 XglTextureObj texture1(m_device); // Red
1881 texture1.ChangeColors(0xffff0000,0xffff0000);
1882 XglTextureObj texture2(m_device); // Green
1883 texture2.ChangeColors(0xff00ff00,0xff00ff00);
1884 XglTextureObj texture3(m_device); // Blue
1885 texture3.ChangeColors(0xff0000ff,0xff0000ff);
1886
1887 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture1);
1888 ps.BindShaderEntitySlotToSampler(0, &sampler1);
1889
1890 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_RESOURCE, &texture2);
1891 ps.BindShaderEntitySlotToSampler(1, &sampler2);
1892
1893 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_RESOURCE, &texture3);
1894 ps.BindShaderEntitySlotToSampler(12, &sampler3);
1895
1896 XglPipelineObj pipelineobj(m_device);
1897 pipelineobj.AddShader(&vs);
1898 pipelineobj.AddShader(&ps);
1899
1900 XglDescriptorSetObj descriptorSet(m_device);
1901 descriptorSet.AttachImageView(&texture1);
1902 descriptorSet.AttachSampler(&sampler1);
1903 descriptorSet.AttachImageView(&texture2);
1904 descriptorSet.AttachSampler(&sampler2);
1905 descriptorSet.AttachImageView(&texture3);
1906 descriptorSet.AttachSampler(&sampler3);
1907
1908 m_memoryRefManager.AddMemoryRef(&texture1);
1909 m_memoryRefManager.AddMemoryRef(&texture2);
1910 m_memoryRefManager.AddMemoryRef(&texture3);
1911
1912 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1913 QueueCommandBuffer(NULL, 0);
1914
1915}
1916
1917TEST_F(XglRenderTest, TriangleVSUniformBlock)
1918{
1919 // The expected result from this test is a blue triangle
1920
1921 static const char *vertShaderText =
1922 "#version 140\n"
1923 "#extension GL_ARB_separate_shader_objects : enable\n"
1924 "#extension GL_ARB_shading_language_420pack : enable\n"
1925 "layout (location = 0) out vec4 outColor;\n"
1926 "layout (std140, binding = 0) uniform bufferVals {\n"
1927 " vec4 red;\n"
1928 " vec4 green;\n"
1929 " vec4 blue;\n"
1930 " vec4 white;\n"
1931 "} myBufferVals;\n"
1932 "void main() {\n"
1933 " vec2 vertices[3];"
1934 " vertices[0] = vec2(-0.5, -0.5);\n"
1935 " vertices[1] = vec2( 0.5, -0.5);\n"
1936 " vertices[2] = vec2( 0.5, 0.5);\n"
1937 " outColor = myBufferVals.blue;\n"
1938 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1939 "}\n";
1940
1941 static const char *fragShaderText =
1942 "#version 140\n"
1943 "#extension GL_ARB_separate_shader_objects : enable\n"
1944 "#extension GL_ARB_shading_language_420pack : enable\n"
1945 "layout (location = 0) in vec4 inColor;\n"
1946 "void main() {\n"
1947 " gl_FragColor = inColor;\n"
1948 "}\n";
1949
1950 ASSERT_NO_FATAL_FAILURE(InitState());
1951 ASSERT_NO_FATAL_FAILURE(InitViewport());
1952
1953 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1954 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1955
1956 // Let's populate our buffer with the following:
1957 // vec4 red;
1958 // vec4 green;
1959 // vec4 blue;
1960 // vec4 white;
1961 const int valCount = 4 * 4;
1962 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
1963 0.0, 1.0, 0.0, 1.0,
1964 0.0, 0.0, 1.0, 1.0,
1965 1.0, 1.0, 1.0, 1.0 };
1966
1967 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
1968 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
1969
1970 XglPipelineObj pipelineobj(m_device);
1971 pipelineobj.AddShader(&vs);
1972 pipelineobj.AddShader(&ps);
1973
1974 XglDescriptorSetObj descriptorSet(m_device);
1975 descriptorSet.AttachMemoryView(&colorBuffer);
1976
1977 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1978 QueueCommandBuffer(NULL, 0);
1979
1980}
1981
1982TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
1983{
1984 // This test allows the shader to select which buffer it is
1985 // pulling from using layout binding qualifier.
1986 // There are corresponding changes in the compiler stack that
1987 // will select the buffer using binding directly.
1988 // The binding number should match the slot number set up by
1989 // the application.
1990 // The expected result from this test is a purple triangle
1991
1992 static const char *vertShaderText =
1993 "#version 140\n"
1994 "#extension GL_ARB_separate_shader_objects : enable\n"
1995 "#extension GL_ARB_shading_language_420pack : enable\n"
1996 "void main() {\n"
1997 " vec2 vertices[3];"
1998 " vertices[0] = vec2(-0.5, -0.5);\n"
1999 " vertices[1] = vec2( 0.5, -0.5);\n"
2000 " vertices[2] = vec2( 0.5, 0.5);\n"
2001 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2002 "}\n";
2003
2004 static const char *fragShaderText =
2005 "#version 140\n"
2006 "#extension GL_ARB_separate_shader_objects : enable\n"
2007 "#extension GL_ARB_shading_language_420pack : enable\n"
2008 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2009 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2010 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2011 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2012 "void main() {\n"
2013 " gl_FragColor = myBlueVal.color;\n"
2014 " gl_FragColor += myRedVal.color;\n"
2015 "}\n";
2016
2017 ASSERT_NO_FATAL_FAILURE(InitState());
2018 ASSERT_NO_FATAL_FAILURE(InitViewport());
2019
2020 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2021 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2022
2023 // We're going to create a number of uniform buffers, and then allow
2024 // the shader to select which it wants to read from with a binding
2025
2026 // Let's populate the buffers with a single color each:
2027 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2028 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2029 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2030 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2031
2032 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2033 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2034 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2035 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2036
2037 const int redCount = sizeof(redVals) / sizeof(float);
2038 const int greenCount = sizeof(greenVals) / sizeof(float);
2039 const int blueCount = sizeof(blueVals) / sizeof(float);
2040 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2041
2042 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2043 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2044
2045 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2046 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2047
2048 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2049 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2050
2051 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2052 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2053
2054 XglPipelineObj pipelineobj(m_device);
2055 pipelineobj.AddShader(&vs);
2056 pipelineobj.AddShader(&ps);
2057
2058 XglDescriptorSetObj descriptorSet(m_device);
2059 descriptorSet.AttachMemoryView(&redBuffer);
2060 descriptorSet.AttachMemoryView(&greenBuffer);
2061 descriptorSet.AttachMemoryView(&blueBuffer);
2062 descriptorSet.AttachMemoryView(&whiteBuffer);
2063
2064 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
2065 QueueCommandBuffer(NULL, 0);
2066
2067}
2068
2069TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2070{
2071 // This test is the same as TriangleFSUniformBlockBinding, but
2072 // it does not provide an instance name.
2073 // The expected result from this test is a purple triangle
2074
2075 static const char *vertShaderText =
2076 "#version 140\n"
2077 "#extension GL_ARB_separate_shader_objects : enable\n"
2078 "#extension GL_ARB_shading_language_420pack : enable\n"
2079 "void main() {\n"
2080 " vec2 vertices[3];"
2081 " vertices[0] = vec2(-0.5, -0.5);\n"
2082 " vertices[1] = vec2( 0.5, -0.5);\n"
2083 " vertices[2] = vec2( 0.5, 0.5);\n"
2084 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2085 "}\n";
2086
2087 static const char *fragShaderText =
2088 "#version 430\n"
2089 "#extension GL_ARB_separate_shader_objects : enable\n"
2090 "#extension GL_ARB_shading_language_420pack : enable\n"
2091 "layout (location = 0) uniform vec4 foo;\n"
2092 "layout (location = 1) uniform vec4 bar;\n"
2093 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2094 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2095 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2096 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
2097 "void main() {\n"
2098 " gl_FragColor = blue;\n"
2099 " gl_FragColor += red;\n"
2100 "}\n";
2101 ASSERT_NO_FATAL_FAILURE(InitState());
2102 ASSERT_NO_FATAL_FAILURE(InitViewport());
2103
2104 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2105 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2106
2107 // We're going to create a number of uniform buffers, and then allow
2108 // the shader to select which it wants to read from with a binding
2109
2110 // Let's populate the buffers with a single color each:
2111 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2112 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2113 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2114 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2115
2116 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2117 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2118 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2119 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2120
2121 const int redCount = sizeof(redVals) / sizeof(float);
2122 const int greenCount = sizeof(greenVals) / sizeof(float);
2123 const int blueCount = sizeof(blueVals) / sizeof(float);
2124 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2125
2126 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2127 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2128
2129 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2130 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2131
2132 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2133 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2134
2135 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2136 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2137
2138 XglPipelineObj pipelineobj(m_device);
2139 pipelineobj.AddShader(&vs);
2140 pipelineobj.AddShader(&ps);
2141
2142 XglDescriptorSetObj descriptorSet(m_device);
2143 descriptorSet.AttachMemoryView(&redBuffer);
2144 descriptorSet.AttachMemoryView(&greenBuffer);
2145 descriptorSet.AttachMemoryView(&blueBuffer);
2146 descriptorSet.AttachMemoryView(&whiteBuffer);
2147
2148 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
2149 QueueCommandBuffer(NULL, 0);
2150
2151}
2152
2153TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2154{
2155 static const char *vertShaderText =
2156 "#version 140\n"
2157 "#extension GL_ARB_separate_shader_objects : enable\n"
2158 "#extension GL_ARB_shading_language_420pack : enable\n"
2159 "layout (std140, binding=0) uniform bufferVals {\n"
2160 " mat4 mvp;\n"
2161 "} myBufferVals;\n"
2162 "layout (location=0) in vec4 pos;\n"
2163 "layout (location=0) out vec2 UV;\n"
2164 "void main() {\n"
2165 " vec2 positions[3];"
2166 " positions[0] = vec2( 0.0, 0.0);\n"
2167 " positions[1] = vec2( 0.25, 0.1);\n"
2168 " positions[2] = vec2( 0.1, 0.25);\n"
2169 " UV = positions[gl_VertexID % 3];\n"
2170 " gl_Position = myBufferVals.mvp * pos;\n"
2171 "}\n";
2172
2173 static const char *fragShaderText =
2174 "#version 140\n"
2175 "#extension GL_ARB_separate_shader_objects : enable\n"
2176 "#extension GL_ARB_shading_language_420pack : enable\n"
2177 "layout (binding=0) uniform sampler2D surface;\n"
2178 "layout (location=0) out vec4 outColor;\n"
2179 "layout (location=0) in vec2 UV;\n"
2180 "void main() {\n"
2181 " outColor= textureLod(surface, UV, 0.0);\n"
2182 "}\n";
2183 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2184
2185 glm::mat4 View = glm::lookAt(
2186 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2187 glm::vec3(0,0,0), // and looks at the origin
2188 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2189 );
2190
2191 glm::mat4 Model = glm::mat4(1.0f);
2192
2193 glm::mat4 MVP = Projection * View * Model;
2194
2195
2196 ASSERT_NO_FATAL_FAILURE(InitState());
2197 ASSERT_NO_FATAL_FAILURE(InitViewport());
2198 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2199
2200 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2201 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
2202 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
2203
2204
2205 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2206
2207 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2208 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2209 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2210 XglSamplerObj sampler(m_device);
2211 XglTextureObj texture(m_device);
2212
2213 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2214 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
2215 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
2216 ps.BindShaderEntitySlotToSampler(0, &sampler);
2217
2218 XglPipelineObj pipelineobj(m_device);
2219 pipelineobj.AddShader(&vs);
2220 pipelineobj.AddShader(&ps);
2221
2222 XglDescriptorSetObj descriptorSet(m_device);
2223
2224 descriptorSet.AttachMemoryView(&mvpBuffer);
2225 descriptorSet.AttachImageView(&texture);
2226 descriptorSet.AttachSampler(&sampler);
2227
2228 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2229 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2230 m_memoryRefManager.AddMemoryRef(&texture);
2231
2232 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2233 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2234 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2235 };
2236
2237 // this is the current description of g_vbData
2238 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2239 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2240 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2241 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2242 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2243 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2244 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2245 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2246 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2247
2248 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2249 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2250 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2251
2252 ClearDepthStencil(1.0f);
2253 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
2254
2255 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2256
2257}
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002258int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002259 int result;
2260
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002261 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002262 XglTestFramework::InitArgs(&argc, argv);
2263
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002264 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
2265
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002266 result = RUN_ALL_TESTS();
2267
2268 XglTestFramework::Finish();
2269 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002270}