blob: 32dad5449c493ba2ab83818e81e55e3909d447da [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
Chia-I Wu4115c892014-08-28 11:56:29 +080075#include "icd-bil.h"
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -060076
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -060077#define GLM_FORCE_RADIANS
78#include "glm/glm.hpp"
79#include <glm/gtc/matrix_transform.hpp>
80
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -060081#include "xglrenderframework.h"
Tobin Ehlis791a49c2014-11-10 12:29:12 -070082#ifdef DEBUG_CALLBACK
83XGL_VOID XGLAPI myDbgFunc(
84 XGL_DBG_MSG_TYPE msgType,
85 XGL_VALIDATION_LEVEL validationLevel,
86 XGL_BASE_OBJECT srcObject,
87 XGL_SIZE location,
88 XGL_INT msgCode,
89 const XGL_CHAR* pMsg,
90 XGL_VOID* pUserData)
91{
Tobin Ehlis3c26a542014-11-18 11:28:33 -070092 switch (msgType)
93 {
94 case XGL_DBG_MSG_WARNING:
95 printf("CALLBACK WARNING : %s\n", pMsg);
96 break;
97 case XGL_DBG_MSG_ERROR:
98 printf("CALLBACK ERROR : %s\n", pMsg);
99 break;
100 default:
101 printf("EATING Msg of type %u\n", msgType);
102 break;
103 }
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700104}
105#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700106
107
108#undef ASSERT_NO_FATAL_FAILURE
109#define ASSERT_NO_FATAL_FAILURE(x) x
110
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600111//--------------------------------------------------------------------------------------
112// Mesh and VertexFormat Data
113//--------------------------------------------------------------------------------------
114struct Vertex
115{
116 XGL_FLOAT posX, posY, posZ, posW; // Position data
117 XGL_FLOAT r, g, b, a; // Color
118};
119
120#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
121
122static const Vertex g_vbData[] =
123{
124 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
125 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
126 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
127 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
128 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
129 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
130
131 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
132 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
133 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
134 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
135 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
136 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
137
138 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
139 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
140 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
141 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
142 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
143 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
144
145 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
146 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
147 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
148 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
149 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
150 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
151
152 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
153 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
154 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
155 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
156 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
157 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
158
159 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
160 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
161 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
162 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
163 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
164 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
165};
166
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600167static const Vertex g_vb_solid_face_colors_Data[] =
168{
169 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
170 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
171 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
172 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
173 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
174 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
175
176 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
177 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
178 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
179 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
180 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
181 { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
182
183 { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
184 { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
185 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
186 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
187 { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
188 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
189
190 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
191 { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
192 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
193 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
194 { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
195 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
196
197 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
198 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
199 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
200 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
201 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
202 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
203
204 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
205 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
206 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
207 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
208 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
209 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
210};
211
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600212class XglRenderTest : public XglRenderFramework
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -0600213{
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600214public:
Cody Northrop4e6b4762014-10-09 21:25:22 -0600215
Tony Barbourf43b6982014-11-25 13:18:32 -0700216 void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText);
217 void RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
Tony Barbour09da2212014-12-03 16:13:23 -0700218 XglConstantBufferObj *constantBuffer);
Tony Barboura3953b82014-12-03 15:46:29 -0700219 void GenericDrawTriangleTest(XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet, int numTris);
Tony Barbour02472db2015-01-08 17:08:28 -0700220 void GenericDrawPreparation(XglCommandBufferObj *cmdBuffer, XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet);
Tony Barbourf43b6982014-11-25 13:18:32 -0700221 void QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs);
222
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600223 void InitDepthStencil();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600224 void GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600225 void XGLTriangleTest(const char *vertShaderText, const char *fragShaderText);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600226
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600227
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600228protected:
Cody Northrop350727b2014-10-06 15:42:00 -0600229 XGL_IMAGE m_texture;
230 XGL_IMAGE_VIEW m_textureView;
231 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
232 XGL_GPU_MEMORY m_textureMem;
233
234 XGL_SAMPLER m_sampler;
235
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600236 XGL_FORMAT m_depth_stencil_fmt;
237 XGL_IMAGE m_depthStencilImage;
238 XGL_GPU_MEMORY m_depthStencilMem;
239 XGL_DEPTH_STENCIL_VIEW m_depthStencilView;
Tony Barbourf43b6982014-11-25 13:18:32 -0700240 XglMemoryRefManager m_memoryRefManager;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600241
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600242
243 virtual void SetUp() {
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600244
245 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
246 this->app_info.pNext = NULL;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800247 this->app_info.pAppName = "render_tests";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600248 this->app_info.appVersion = 1;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800249 this->app_info.pEngineName = "unittest";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600250 this->app_info.engineVersion = 1;
251 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
252
Cody Northrop350727b2014-10-06 15:42:00 -0600253 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
254 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barbour97a36232014-12-04 17:14:45 -0700255 memset(&m_depthStencilImage, 0, sizeof(m_depthStencilImage));
Cody Northrop350727b2014-10-06 15:42:00 -0600256
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600257 InitFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600258 }
259
260 virtual void TearDown() {
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600261 // Clean up resources before we reset
262 ShutdownFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600263 }
264};
265
Tony Barbour02472db2015-01-08 17:08:28 -0700266void XglRenderTest::GenericDrawPreparation(XglCommandBufferObj *cmdBuffer, XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet)
267{
268 cmdBuffer->ClearAllBuffers(&m_depthStencilBinding, m_depthStencilImage);
269 cmdBuffer->BindAttachments(&m_depthStencilBinding);
270 cmdBuffer->BindStateObject(XGL_STATE_BIND_RASTER, m_stateRaster);
271 cmdBuffer->BindStateObject(XGL_STATE_BIND_VIEWPORT, m_stateViewport);
272 cmdBuffer->BindStateObject(XGL_STATE_BIND_COLOR_BLEND, m_colorBlend);
273 cmdBuffer->BindStateObject(XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil);
274 cmdBuffer->BindStateObject(XGL_STATE_BIND_MSAA, m_stateMsaa);
275 pipelineobj->CreateXGLPipeline(descriptorSet);
276 cmdBuffer->BindPipeline(pipelineobj->GetPipelineHandle());
277 descriptorSet->CreateXGLDescriptorSet();
278 cmdBuffer->BindDescriptorSet(descriptorSet->GetDescriptorSetHandle());
279}
Tony Barbourf43b6982014-11-25 13:18:32 -0700280
Tony Barboura3953b82014-12-03 15:46:29 -0700281void XglRenderTest::GenericDrawTriangleTest(XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet,int numTris)
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600282{
283 XGL_RESULT err = XGL_SUCCESS;
284
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600285 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600286 // Build command buffer
287 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
288 ASSERT_XGL_SUCCESS(err);
289
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600290 GenerateClearAndPrepareBufferCmds();
291 GenerateBindRenderTargetCmd();
Tobin Ehlis266473d2014-12-16 17:34:50 -0700292
Tony Barbourf43b6982014-11-25 13:18:32 -0700293 GenerateBindStateAndPipelineCmds();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600294
Tony Barboura3953b82014-12-03 15:46:29 -0700295 pipelineobj->BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
Tony Barbourbf678472014-12-03 13:58:15 -0700296 descriptorSet->BindCommandBuffer(m_cmdBuffer);
Tobin Ehlis266473d2014-12-16 17:34:50 -0700297#ifdef DUMP_STATE_DOT
298 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
299 pDSDumpDot((char*)"triTest.dot");
300 DRAW_STATE_DUMP_PNG_FILE pDSDumpPng = (DRAW_STATE_DUMP_PNG_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpPngFile");
301 pDSDumpPng((char*)"triTest.png");
302#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700303 // render the triangle
304 xglCmdDraw( m_cmdBuffer, 0, 3*numTris, 0, 1 );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600305
306 // finalize recording of the command buffer
307 err = xglEndCommandBuffer( m_cmdBuffer );
308 ASSERT_XGL_SUCCESS( err );
Tony Barbourf43b6982014-11-25 13:18:32 -0700309}
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600310
Tony Barbourf43b6982014-11-25 13:18:32 -0700311void XglRenderTest::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
312{
313 XGL_RESULT err = XGL_SUCCESS;
Chia-I Wue7748802014-12-05 10:45:15 +0800314 XGL_UINT i;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600315
316 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700317 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, numMemRefs, memRefs, NULL );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600318 ASSERT_XGL_SUCCESS( err );
319
Chia-I Wuf34ac502014-08-27 14:58:05 +0800320 err = xglQueueWaitIdle( m_device->m_queue );
321 ASSERT_XGL_SUCCESS( err );
322
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600323 // Wait for work to finish before cleaning up.
324 xglDeviceWaitIdle(m_device->device());
325
Chia-I Wue7748802014-12-05 10:45:15 +0800326 for (i = 0; i < m_renderTargetCount; i++)
327 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600328}
329
Tony Barbourf43b6982014-11-25 13:18:32 -0700330void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600331{
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600332 XGL_RESULT err;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600333
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600334 ASSERT_NO_FATAL_FAILURE(InitState());
335 ASSERT_NO_FATAL_FAILURE(InitViewport());
336
Tony Barbourf43b6982014-11-25 13:18:32 -0700337 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
338 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600339
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600340
Tony Barbourf43b6982014-11-25 13:18:32 -0700341 XglPipelineObj pipelineobj(m_device);
342 pipelineobj.AddShader(&vs);
343 pipelineobj.AddShader(&ps);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600344
345 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
346
Tony Barbourf43b6982014-11-25 13:18:32 -0700347 // Create descriptor set
348 XglDescriptorSetObj descriptorSet(m_device);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600349
350 // Build command buffer
351 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
352 ASSERT_XGL_SUCCESS(err);
353
354 GenerateClearAndPrepareBufferCmds();
355 GenerateBindRenderTargetCmd();
Tony Barbourf43b6982014-11-25 13:18:32 -0700356 GenerateBindStateAndPipelineCmds();
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600357
Tobin Ehlis266473d2014-12-16 17:34:50 -0700358 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,&descriptorSet);
359 descriptorSet.BindCommandBuffer(m_cmdBuffer);
Tobin Ehlis31446e52014-11-28 11:17:19 -0700360#ifdef DUMP_STATE_DOT
361 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
362 pDSDumpDot((char*)"triUniFS.dot");
363#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700364 // render the triangle
365 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600366
367 // finalize recording of the command buffer
368 err = xglEndCommandBuffer( m_cmdBuffer );
369 ASSERT_XGL_SUCCESS( err );
370
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600371 // submit the command buffer to the universal queue
Tony Barbour75420772014-12-04 17:17:26 -0700372 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, 0, m_memRefs, NULL );
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600373 ASSERT_XGL_SUCCESS( err );
374
375 err = xglQueueWaitIdle( m_device->m_queue );
376 ASSERT_XGL_SUCCESS( err );
377
378 // Wait for work to finish before cleaning up.
379 xglDeviceWaitIdle(m_device->device());
380
Chia-I Wue7748802014-12-05 10:45:15 +0800381 assert(m_renderTargetCount == 1);
382 RecordImage(m_renderTargets[0]);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600383
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600384}
385
Tony Barbourf43b6982014-11-25 13:18:32 -0700386void XglRenderTest::RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
Tony Barbour09da2212014-12-03 16:13:23 -0700387 XglConstantBufferObj *constantBuffer)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600388{
389 int i;
390 glm::mat4 MVP;
391 int matrixSize = sizeof(MVP);
392 XGL_RESULT err;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600393
394 for (i = 0; i < 8; i++) {
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800395 void *pData = constantBuffer->map();
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600396
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600397 Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
398 MVP = Projection * View * Model;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600399 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
400
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800401 constantBuffer->unmap();
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600402
403 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700404 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_memoryRefManager.GetNumRefs(), m_memoryRefManager.GetMemoryRefList(), NULL );
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600405 ASSERT_XGL_SUCCESS( err );
406
407 err = xglQueueWaitIdle( m_device->m_queue );
408 ASSERT_XGL_SUCCESS( err );
409
410 // Wait for work to finish before cleaning up.
411 xglDeviceWaitIdle(m_device->device());
412
Chia-I Wue7748802014-12-05 10:45:15 +0800413 assert(m_renderTargetCount == 1);
414 RecordImage(m_renderTargets[0]);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600415 }
Cody Northrop7a1f0462014-10-10 14:49:36 -0600416}
417
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600418void dumpMatrix(const char *note, glm::mat4 MVP)
419{
Chia-I Wu7133fdc2014-12-15 23:57:34 +0800420 int i;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600421
422 printf("%s: \n", note);
423 for (i=0; i<4; i++) {
424 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
425 }
426 printf("\n");
427 fflush(stdout);
428}
429
430void dumpVec4(const char *note, glm::vec4 vector)
431{
432 printf("%s: \n", note);
433 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
434 printf("\n");
435 fflush(stdout);
436}
437
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600438void XglRenderTest::GenerateClearAndPrepareBufferCmds()
439{
440 XglRenderFramework::GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600441
Tony Barbour97a36232014-12-04 17:14:45 -0700442 if (m_depthStencilImage) {
Tony Barbour97a36232014-12-04 17:14:45 -0700443 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
444 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
445 dsRange.baseMipLevel = 0;
446 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
447 dsRange.baseArraySlice = 0;
448 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
Cody Northrop7a1f0462014-10-10 14:49:36 -0600449
Tony Barbour97a36232014-12-04 17:14:45 -0700450 // prepare the depth buffer for clear
451 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
452 transitionToClear.image = m_depthStencilImage;
453 transitionToClear.oldState = m_depthStencilBinding.depthState;
454 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
455 transitionToClear.subresourceRange = dsRange;
456 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
457 m_depthStencilBinding.depthState = transitionToClear.newState;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600458
Tony Barbour97a36232014-12-04 17:14:45 -0700459 xglCmdClearDepthStencil(m_cmdBuffer, m_depthStencilImage, 1.0f, 0, 1, &dsRange);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600460
Tony Barbour97a36232014-12-04 17:14:45 -0700461 // prepare depth buffer for rendering
462 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
Chia-I Wu18749b02014-12-05 10:48:20 +0800463 transitionToRender.image = m_depthStencilImage;
Tony Barbour97a36232014-12-04 17:14:45 -0700464 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
465 transitionToRender.newState = m_depthStencilBinding.depthState;
466 transitionToRender.subresourceRange = dsRange;
467 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
468 m_depthStencilBinding.depthState = transitionToClear.newState;
469 }
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600470}
471
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600472void XglRenderTest::InitDepthStencil()
473{
474 XGL_RESULT err;
475 XGL_IMAGE_CREATE_INFO image;
476 XGL_MEMORY_ALLOC_INFO mem_alloc;
477 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
478 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn6317c492014-11-21 11:33:20 -0700479 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600480
481 // Clean up default state created by framework
482 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
483
484 m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16;
485 m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS;
486
487 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
488 image.pNext = NULL;
489 image.imageType = XGL_IMAGE_2D;
490 image.format = m_depth_stencil_fmt;
491 image.extent.width = m_width;
492 image.extent.height = m_height;
493 image.extent.depth = 1;
494 image.mipLevels = 1;
495 image.arraySize = 1;
496 image.samples = 1;
497 image.tiling = XGL_OPTIMAL_TILING;
498 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
499 image.flags = 0;
500
501 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
502 mem_alloc.pNext = NULL;
503 mem_alloc.allocationSize = 0;
504 mem_alloc.alignment = 0;
505 mem_alloc.flags = 0;
506 mem_alloc.heapCount = 0;
507 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
508
509 /* create image */
510 err = xglCreateImage(device(), &image,
511 &m_depthStencilImage);
512 ASSERT_XGL_SUCCESS(err);
513
514 err = xglGetObjectInfo(m_depthStencilImage,
515 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
516 &mem_reqs_size, &mem_reqs);
517 ASSERT_XGL_SUCCESS(err);
518 ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs));
519
520 mem_alloc.allocationSize = mem_reqs.size;
521 mem_alloc.alignment = mem_reqs.alignment;
522 mem_alloc.heapCount = mem_reqs.heapCount;
523 memcpy(mem_alloc.heaps, mem_reqs.heaps,
524 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
525
526 /* allocate memory */
527 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem);
528 ASSERT_XGL_SUCCESS(err);
529
530 /* bind memory */
531 err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0);
532 ASSERT_XGL_SUCCESS(err);
533
534 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
535 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
536 depthStencil.depthTestEnable = XGL_TRUE;
537 depthStencil.depthWriteEnable = XGL_TRUE;
538 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
539 depthStencil.depthBoundsEnable = XGL_FALSE;
540 depthStencil.minDepth = 0.f;
541 depthStencil.maxDepth = 1.f;
542 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
543 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
544 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
545 depthStencil.back.stencilRef = 0x00;
546 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
547 depthStencil.front = depthStencil.back;
548
549 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
550 ASSERT_XGL_SUCCESS( err );
551
552 /* create image view */
553 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
554 view.pNext = NULL;
555 view.image = XGL_NULL_HANDLE;
556 view.mipLevel = 0;
557 view.baseArraySlice = 0;
558 view.arraySize = 1;
559 view.flags = 0;
560 view.image = m_depthStencilImage;
561 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
562 ASSERT_XGL_SUCCESS(err);
563
564 m_depthStencilBinding.view = m_depthStencilView;
565 m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
566 m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
567}
568
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600569struct xgltriangle_vs_uniform {
570 // Must start with MVP
571 XGL_FLOAT mvp[4][4];
572 XGL_FLOAT position[3][4];
573 XGL_FLOAT color[3][4];
574};
575
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600576void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600577{
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700578#ifdef DEBUG_CALLBACK
579 xglDbgRegisterMsgCallback(myDbgFunc, NULL);
580#endif
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600581 // Create identity matrix
582 int i;
583 struct xgltriangle_vs_uniform data;
584
585 glm::mat4 Projection = glm::mat4(1.0f);
586 glm::mat4 View = glm::mat4(1.0f);
587 glm::mat4 Model = glm::mat4(1.0f);
588 glm::mat4 MVP = Projection * View * Model;
589 const int matrixSize = sizeof(MVP);
590 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT);
591 memcpy(&data.mvp, &MVP[0][0], matrixSize);
592
593 static const Vertex tri_data[] =
594 {
595 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
596 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
597 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
598 };
599
600 for (i=0; i<3; i++) {
601 data.position[i][0] = tri_data[i].posX;
602 data.position[i][1] = tri_data[i].posY;
603 data.position[i][2] = tri_data[i].posZ;
604 data.position[i][3] = tri_data[i].posW;
605 data.color[i][0] = tri_data[i].r;
606 data.color[i][1] = tri_data[i].g;
607 data.color[i][2] = tri_data[i].b;
608 data.color[i][3] = tri_data[i].a;
609 }
610
Tony Barbourf43b6982014-11-25 13:18:32 -0700611 ASSERT_NO_FATAL_FAILURE(InitState());
612 ASSERT_NO_FATAL_FAILURE(InitViewport());
613
614 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data);
615
616 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
617 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
618 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
619
620 XglPipelineObj pipelineobj(m_device);
621 pipelineobj.AddShader(&vs);
622 pipelineobj.AddShader(&ps);
623
624 XglDescriptorSetObj descriptorSet(m_device);
625 descriptorSet.AttachMemoryView(&constantBuffer);
626 m_memoryRefManager.AddMemoryRef(&constantBuffer);
627
Tony Barboura3953b82014-12-03 15:46:29 -0700628 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -0700629 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
630
Tony Barbour09da2212014-12-03 16:13:23 -0700631 RotateTriangleVSUniform(Projection, View, Model, &constantBuffer);
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700632#ifdef PRINT_OBJECTS
633 //XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)
634 OBJ_TRACK_GET_OBJECT_COUNT pObjTrackGetObjectCount = (OBJ_TRACK_GET_OBJECT_COUNT)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjectCount");
635 XGL_UINT64 numObjects = pObjTrackGetObjectCount(XGL_OBJECT_TYPE_ANY);
636 //OBJ_TRACK_GET_OBJECTS pGetObjsFunc = xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
637 printf("DEBUG : Number of Objects : %lu\n", numObjects);
638 OBJ_TRACK_GET_OBJECTS pObjTrackGetObjs = (OBJ_TRACK_GET_OBJECTS)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
639 OBJTRACK_NODE* pObjNodeArray = (OBJTRACK_NODE*)malloc(sizeof(OBJTRACK_NODE)*numObjects);
640 pObjTrackGetObjs(XGL_OBJECT_TYPE_ANY, numObjects, pObjNodeArray);
641 for (i=0; i < numObjects; i++) {
642 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);
643 }
644 free(pObjNodeArray);
645#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700646
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600647}
648
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600649TEST_F(XglRenderTest, XGLTriangle_FragColor)
650{
651 static const char *vertShaderText =
652 "#version 140\n"
653 "#extension GL_ARB_separate_shader_objects : enable\n"
654 "#extension GL_ARB_shading_language_420pack : enable\n"
655 "\n"
656 "layout(binding = 0) uniform buf {\n"
657 " mat4 MVP;\n"
658 " vec4 position[3];\n"
659 " vec4 color[3];\n"
660 "} ubuf;\n"
661 "\n"
662 "layout (location = 0) out vec4 outColor;\n"
663 "\n"
664 "void main() \n"
665 "{\n"
666 " outColor = ubuf.color[gl_VertexID];\n"
667 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
668 "}\n";
669
670 static const char *fragShaderText =
671 "#version 140\n"
672 "#extension GL_ARB_separate_shader_objects : enable\n"
673 "#extension GL_ARB_shading_language_420pack : enable\n"
674 "\n"
675 "layout (location = 0) in vec4 inColor;\n"
676 "\n"
677 "void main()\n"
678 "{\n"
679 " gl_FragColor = inColor;\n"
680 "}\n";
681
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600682 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600683 XGLTriangleTest(vertShaderText, fragShaderText);
684}
685
686TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
687{
688 static const char *vertShaderText =
689 "#version 140\n"
690 "#extension GL_ARB_separate_shader_objects : enable\n"
691 "#extension GL_ARB_shading_language_420pack : enable\n"
692 "\n"
693 "layout(binding = 0) uniform buf {\n"
694 " mat4 MVP;\n"
695 " vec4 position[3];\n"
696 " vec4 color[3];\n"
697 "} ubuf;\n"
698 "\n"
699 "layout (location = 0) out vec4 outColor;\n"
700 "\n"
701 "void main() \n"
702 "{\n"
703 " outColor = ubuf.color[gl_VertexID];\n"
704 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
705 "}\n";
706
707 static const char *fragShaderText =
708 "#version 140\n"
709 "#extension GL_ARB_separate_shader_objects : enable\n"
710 "#extension GL_ARB_shading_language_420pack : enable\n"
711 "\n"
712 "layout (location = 0) in vec4 inColor;\n"
713 "layout (location = 0) out vec4 outColor;\n"
714 "\n"
715 "void main()\n"
716 "{\n"
717 " outColor = inColor;\n"
718 "}\n";
719
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600720 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 -0600721
722 XGLTriangleTest(vertShaderText, fragShaderText);
723}
724
Tony Barbourf43b6982014-11-25 13:18:32 -0700725TEST_F(XglRenderTest, BIL_XGLTriangle)
726{
727 bool saved_use_bil = XglTestFramework::m_use_bil;
728
729 static const char *vertShaderText =
730 "#version 140\n"
731 "#extension GL_ARB_separate_shader_objects : enable\n"
732 "#extension GL_ARB_shading_language_420pack : enable\n"
733 "\n"
734 "layout(binding = 0) uniform buf {\n"
735 " mat4 MVP;\n"
736 " vec4 position[3];\n"
737 " vec4 color[3];\n"
738 "} ubuf;\n"
739 "\n"
740 "layout (location = 0) out vec4 outColor;\n"
741 "\n"
742 "void main() \n"
743 "{\n"
744 " outColor = ubuf.color[gl_VertexID];\n"
745 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
746 "}\n";
747
748 static const char *fragShaderText =
749 "#version 140\n"
750 "#extension GL_ARB_separate_shader_objects : enable\n"
751 "#extension GL_ARB_shading_language_420pack : enable\n"
752 "\n"
753 "layout (location = 0) in vec4 inColor;\n"
754 "\n"
755 "void main()\n"
756 "{\n"
757 " gl_FragColor = inColor;\n"
758 "}\n";
759
760 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver.");
761
762 XglTestFramework::m_use_bil = true;
763
764 XGLTriangleTest(vertShaderText, fragShaderText);
765
766 XglTestFramework::m_use_bil = saved_use_bil;
767}
768
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600769TEST_F(XglRenderTest, GreenTriangle)
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600770{
771 static const char *vertShaderText =
772 "#version 130\n"
773 "vec2 vertices[3];\n"
774 "void main() {\n"
775 " vertices[0] = vec2(-1.0, -1.0);\n"
776 " vertices[1] = vec2( 1.0, -1.0);\n"
777 " vertices[2] = vec2( 0.0, 1.0);\n"
778 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
779 "}\n";
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600780
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600781 static const char *fragShaderText =
782 "#version 130\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600783 "void main() {\n"
Steve K10b32512014-10-10 08:54:29 -0600784 " gl_FragColor = vec4(0,1,0,1);\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600785 "}\n";
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600786
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600787 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
788
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600789 DrawTriangleTest(vertShaderText, fragShaderText);
790}
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600791
Tony Barbourf43b6982014-11-25 13:18:32 -0700792TEST_F(XglRenderTest, BIL_GreenTriangle)
793{
794 bool saved_use_bil = XglTestFramework::m_use_bil;
795
796 static const char *vertShaderText =
797 "#version 130\n"
798 "vec2 vertices[3];\n"
799 "void main() {\n"
800 " vertices[0] = vec2(-1.0, -1.0);\n"
801 " vertices[1] = vec2( 1.0, -1.0);\n"
802 " vertices[2] = vec2( 0.0, 1.0);\n"
803 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
804 "}\n";
805
806 static const char *fragShaderText =
807 "#version 130\n"
808 "void main() {\n"
809 " gl_FragColor = vec4(0,1,0,1);\n"
810 "}\n";
811
812 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver.");
813
814 XglTestFramework::m_use_bil = true;
815 DrawTriangleTest(vertShaderText, fragShaderText);
816 XglTestFramework::m_use_bil = saved_use_bil;
817}
818
819TEST_F(XglRenderTest, YellowTriangle)
820{
821 static const char *vertShaderText =
822 "#version 130\n"
823 "void main() {\n"
824 " vec2 vertices[3];"
825 " vertices[0] = vec2(-0.5, -0.5);\n"
826 " vertices[1] = vec2( 0.5, -0.5);\n"
827 " vertices[2] = vec2( 0.5, 0.5);\n"
828 " vec4 colors[3];\n"
829 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
830 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
831 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
832 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
833 "}\n";
834
835 static const char *fragShaderText =
836 "#version 130\n"
837 "void main() {\n"
838 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
839 "}\n";
840
841 DrawTriangleTest(vertShaderText, fragShaderText);
842}
843
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600844TEST_F(XglRenderTest, TriangleWithVertexFetch)
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600845{
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600846 static const char *vertShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700847 "#version 130\n"
848 //XYZ1( -1, -1, -1 )
849 "in vec4 pos;\n"
850 //XYZ1( 0.f, 0.f, 0.f )
851 "in vec4 inColor;\n"
852 "out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600853 "void main() {\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -0600854 " outColor = inColor;\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600855 " gl_Position = pos;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600856 "}\n";
857
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600858
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600859 static const char *fragShaderText =
Cody Northrop68a10f62014-12-05 15:44:14 -0700860 "#version 140\n"
861 "#extension GL_ARB_separate_shader_objects : enable\n"
862 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -0700863 "in vec4 color;\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700864 "layout (location = 0) out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600865 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700866 " outColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600867 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600868
Tony Barbourf43b6982014-11-25 13:18:32 -0700869
870
871 ASSERT_NO_FATAL_FAILURE(InitState());
872 ASSERT_NO_FATAL_FAILURE(InitViewport());
873
874 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -0700875 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -0700876
877 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
878 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
879
880 XglPipelineObj pipelineobj(m_device);
881 pipelineobj.AddShader(&vs);
882 pipelineobj.AddShader(&ps);
883
884 XglDescriptorSetObj descriptorSet(m_device);
885 descriptorSet.AttachMemoryView(&meshBuffer);
886
887 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
888 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
889 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
890 };
891
892 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
893 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
894 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
895 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
896 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
897 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
898 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
899 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
900 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
901
902 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
903 pipelineobj.AddVertexInputBindings(&vi_binding,1);
904 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
905
Tony Barboura3953b82014-12-03 15:46:29 -0700906 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -0700907 QueueCommandBuffer(NULL, 0);
908
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600909}
910
Chia-I Wue09d1a72014-12-05 10:32:23 +0800911TEST_F(XglRenderTest, TriangleMRT)
912{
913 static const char *vertShaderText =
914 "#version 130\n"
915 "in vec4 pos;\n"
916 "void main() {\n"
917 " gl_Position = pos;\n"
918 "}\n";
919
920 static const char *fragShaderText =
921 "#version 130\n"
922 "void main() {\n"
923 " gl_FragData[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
924 " gl_FragData[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
925 "}\n";
926 const XGL_FLOAT vb_data[][2] = {
927 { -1.0f, -1.0f },
928 { 1.0f, -1.0f },
929 { -1.0f, 1.0f }
930 };
931
932 ASSERT_NO_FATAL_FAILURE(InitState());
933 ASSERT_NO_FATAL_FAILURE(InitViewport());
934
935 XglConstantBufferObj meshBuffer(m_device, sizeof(vb_data) / sizeof(vb_data[0]), sizeof(vb_data[0]), vb_data);
Tony Barboure6152042014-12-10 17:40:15 -0700936 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Chia-I Wue09d1a72014-12-05 10:32:23 +0800937
938 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
939 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
940
941 XglPipelineObj pipelineobj(m_device);
942 pipelineobj.AddShader(&vs);
943 pipelineobj.AddShader(&ps);
944
945 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
946 sizeof(vb_data[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
947 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
948 };
949
950 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attrib;
951 vi_attrib.binding = 0; // index into vertexBindingDescriptions
952 vi_attrib.format.channelFormat = XGL_CH_FMT_R32G32; // format of source data
953 vi_attrib.format.numericFormat = XGL_NUM_FMT_FLOAT;
954 vi_attrib.offsetInBytes = 0; // Offset of first element in bytes from base of vertex
955
956 pipelineobj.AddVertexInputAttribs(&vi_attrib, 1);
957 pipelineobj.AddVertexInputBindings(&vi_binding,1);
958 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
959
960 XglDescriptorSetObj descriptorSet(m_device);
961
962 m_renderTargetCount = 2;
963
964 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
965 att.blendEnable = XGL_FALSE;
966 att.format = m_render_target_fmt;
967 att.channelWriteMask = 0xf;
968 pipelineobj.SetColorAttachment(1, &att);
969
Tony Barbour5ed79702015-01-07 14:31:52 -0700970 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
971 XglCommandBufferObj cmdBuffer(m_device);
972 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
973 cmdBuffer.AddRenderTarget(m_renderTargets[1]);
974 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
975 cmdBuffer.ClearAllBuffers(NULL, NULL);
Tony Barbour02472db2015-01-08 17:08:28 -0700976 cmdBuffer.BindAttachments(&m_depthStencilBinding);
Tony Barbour5ed79702015-01-07 14:31:52 -0700977
Tony Barbour02472db2015-01-08 17:08:28 -0700978 cmdBuffer.BindStateObject(XGL_STATE_BIND_RASTER, m_stateRaster);
979 cmdBuffer.BindStateObject(XGL_STATE_BIND_VIEWPORT, m_stateViewport);
980 cmdBuffer.BindStateObject(XGL_STATE_BIND_COLOR_BLEND, m_colorBlend);
981 cmdBuffer.BindStateObject(XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil);
982 cmdBuffer.BindStateObject(XGL_STATE_BIND_MSAA, m_stateMsaa);
Tony Barbour5ed79702015-01-07 14:31:52 -0700983 pipelineobj.CreateXGLPipeline(&descriptorSet);
984 cmdBuffer.BindPipeline(pipelineobj.GetPipelineHandle());
985 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
986
987 descriptorSet.CreateXGLDescriptorSet();
988 cmdBuffer.BindDescriptorSet(descriptorSet.GetDescriptorSetHandle());
989#ifdef DUMP_STATE_DOT
990 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
991 pDSDumpDot((char*)"triTest2.dot");
992#endif
993 // render triangle
994 cmdBuffer.Draw(0, 3, 0, 1);
995
996 // finalize recording of the command buffer
997 cmdBuffer.EndCommandBuffer();
998 cmdBuffer.QueueCommandBuffer(NULL, 0);
999
1000 for (int i = 0; i < m_renderTargetCount; i++)
1001 RecordImage(m_renderTargets[i]);
1002
1003 // GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
1004 // QueueCommandBuffer(NULL, 0);
1005
Chia-I Wue09d1a72014-12-05 10:32:23 +08001006}
1007
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001008TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
1009{
1010 static const char *vertShaderText =
1011 "#version 140\n"
1012 "#extension GL_ARB_separate_shader_objects : enable\n"
1013 "#extension GL_ARB_shading_language_420pack : enable\n"
1014 "layout(location = 0) in vec4 pos;\n"
1015 "layout(location = 1) in vec4 inColor;\n"
1016 "layout(location = 0) out vec4 outColor;\n"
1017 "void main() {\n"
1018 " outColor = inColor;\n"
1019 " gl_Position = pos;\n"
1020 "}\n";
1021
1022
1023 static const char *fragShaderText =
1024 "#version 140\n"
1025 "#extension GL_ARB_separate_shader_objects : enable\n"
1026 "#extension GL_ARB_shading_language_420pack : enable\n"
1027 "layout(location = 0) in vec4 color;\n"
1028 "void main() {\n"
1029 " gl_FragColor = color;\n"
1030 "}\n";
1031
1032 const Vertex g_vbData[] =
1033 {
1034 // first tri
1035 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
1036 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1037 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1038
1039 // second tri
1040 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1041 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1042 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
1043 };
1044
1045 const uint16_t g_idxData[6] = {
1046 0, 1, 2,
1047 3, 4, 5,
1048 };
1049
1050 ASSERT_NO_FATAL_FAILURE(InitState());
1051 ASSERT_NO_FATAL_FAILURE(InitViewport());
1052
1053 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001054 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001055
1056 XglIndexBufferObj indexBuffer(m_device);
1057 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
Tony Barboure6152042014-12-10 17:40:15 -07001058 indexBuffer.SetMemoryState(XGL_MEMORY_STATE_INDEX_DATA);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001059
1060 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1061 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1062
1063 XglPipelineObj pipelineobj(m_device);
1064 pipelineobj.AddShader(&vs);
1065 pipelineobj.AddShader(&ps);
1066
1067 XglDescriptorSetObj descriptorSet(m_device);
1068
1069 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1070 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1071 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1072 };
1073
1074 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1075 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1076 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1077 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1078 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1079 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1080 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1081 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1082 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1083
1084 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1085 pipelineobj.AddVertexInputBindings(&vi_binding,1);
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001086 pipelineobj.CreateXGLPipeline(&descriptorSet);
1087 descriptorSet.CreateXGLDescriptorSet();
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001088
1089 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001090 XglCommandBufferObj cmdBuffer(m_device);
1091 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1092 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
1093 cmdBuffer.ClearAllBuffers(NULL, NULL);
Tony Barbour02472db2015-01-08 17:08:28 -07001094 cmdBuffer.BindAttachments(&m_depthStencilBinding);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001095
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001096#ifdef DUMP_STATE_DOT
1097 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
Tobin Ehlis266473d2014-12-16 17:34:50 -07001098 pDSDumpDot((char*)"triTest2.dot");
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001099#endif
Tony Barbour02472db2015-01-08 17:08:28 -07001100 cmdBuffer.BindStateObject(XGL_STATE_BIND_RASTER, m_stateRaster);
1101 cmdBuffer.BindStateObject(XGL_STATE_BIND_VIEWPORT, m_stateViewport);
1102 cmdBuffer.BindStateObject(XGL_STATE_BIND_COLOR_BLEND, m_colorBlend);
1103 cmdBuffer.BindStateObject(XGL_STATE_BIND_DEPTH_STENCIL, m_stateDepthStencil);
1104 cmdBuffer.BindStateObject(XGL_STATE_BIND_MSAA, m_stateMsaa);
1105
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001106 cmdBuffer.BindPipeline(pipelineobj.GetPipelineHandle());
1107 cmdBuffer.BindDescriptorSet(descriptorSet.GetDescriptorSetHandle());
1108 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1109 cmdBuffer.BindIndexBuffer(&indexBuffer,0);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001110
1111 // render two triangles
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001112 cmdBuffer.DrawIndexed(0, 6, 0, 0, 1);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001113
1114 // finalize recording of the command buffer
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001115 cmdBuffer.EndCommandBuffer();
1116 cmdBuffer.QueueCommandBuffer(NULL, 0);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001117
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001118 for (int i = 0; i < m_renderTargetCount; i++)
1119 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001120
1121}
1122
GregF6bef1212014-12-02 15:41:44 -07001123TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1124{
1125 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001126
GregF6bef1212014-12-02 15:41:44 -07001127 static const char *vertShaderText =
1128 "#version 140\n"
1129 "#extension GL_ARB_separate_shader_objects : enable\n"
1130 "#extension GL_ARB_shading_language_420pack : enable\n"
1131 "layout (location = 0) in vec4 pos;\n"
1132 "layout (location = 0) out vec4 outColor;\n"
1133 "layout (location = 1) out vec4 outColor2;\n"
1134 "void main() {\n"
1135 " gl_Position = pos;\n"
1136 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1137 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1138 "}\n";
1139
1140 static const char *fragShaderText =
1141 //"#version 140\n"
1142 "#version 330\n"
1143 "#extension GL_ARB_separate_shader_objects : enable\n"
1144 "#extension GL_ARB_shading_language_420pack : enable\n"
1145 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1146 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1147 "layout (location = 0) in vec4 color;\n"
1148 "layout (location = 1) in vec4 color2;\n"
1149 "void main() {\n"
1150 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1151 " float dist_squared = dot(pos, pos);\n"
1152 " gl_FragColor = (dist_squared < 400.0)\n"
1153 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1154 " : color2;\n"
1155 "}\n";
1156
1157 ASSERT_NO_FATAL_FAILURE(InitState());
1158 ASSERT_NO_FATAL_FAILURE(InitViewport());
1159
1160 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001161 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001162
1163 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1164 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1165
1166 XglPipelineObj pipelineobj(m_device);
1167 pipelineobj.AddShader(&vs);
1168 pipelineobj.AddShader(&ps);
1169
1170 XglDescriptorSetObj descriptorSet(m_device);
1171 descriptorSet.AttachMemoryView(&meshBuffer);
1172
1173 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1174 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1175 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1176 };
1177
1178 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1179 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1180 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1181 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1182 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1183 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1184 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1185 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1186 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1187
1188 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1189 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1190 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1191
Tony Barbour09da2212014-12-03 16:13:23 -07001192 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001193 QueueCommandBuffer(NULL, 0);
1194
1195}
1196
1197TEST_F(XglRenderTest, RedCirclesonBlue)
1198{
1199 // This tests that we correctly handle unread fragment inputs
1200
1201 static const char *vertShaderText =
1202 "#version 140\n"
1203 "#extension GL_ARB_separate_shader_objects : enable\n"
1204 "#extension GL_ARB_shading_language_420pack : enable\n"
1205 "layout (location = 0) in vec4 pos;\n"
1206 "layout (location = 0) out vec4 outColor;\n"
1207 "layout (location = 1) out vec4 outColor2;\n"
1208 "void main() {\n"
1209 " gl_Position = pos;\n"
1210 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1211 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1212 "}\n";
1213
1214 static const char *fragShaderText =
1215 //"#version 140\n"
1216 "#version 330\n"
1217 "#extension GL_ARB_separate_shader_objects : enable\n"
1218 "#extension GL_ARB_shading_language_420pack : enable\n"
1219 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1220 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1221 "layout (location = 0) in vec4 color;\n"
1222 "layout (location = 1) in vec4 color2;\n"
1223 "void main() {\n"
1224 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1225 " float dist_squared = dot(pos, pos);\n"
1226 " gl_FragColor = (dist_squared < 400.0)\n"
1227 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1228 " : color2;\n"
1229 "}\n";
1230
1231 ASSERT_NO_FATAL_FAILURE(InitState());
1232 ASSERT_NO_FATAL_FAILURE(InitViewport());
1233
1234 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001235 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001236
1237 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1238 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1239
1240 XglPipelineObj pipelineobj(m_device);
1241 pipelineobj.AddShader(&vs);
1242 pipelineobj.AddShader(&ps);
1243
1244 XglDescriptorSetObj descriptorSet(m_device);
1245 descriptorSet.AttachMemoryView(&meshBuffer);
1246
1247 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1248 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1249 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1250 };
1251
1252 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1253 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1254 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1255 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1256 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1257 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1258 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1259 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1260 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1261
1262 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1263 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1264 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1265
Tony Barbour09da2212014-12-03 16:13:23 -07001266 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001267 QueueCommandBuffer(NULL, 0);
1268
1269}
1270
1271TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1272{
1273 // This tests reading gl_ClipDistance from FS
1274
1275 static const char *vertShaderText =
1276 "#version 330\n"
1277 "#extension GL_ARB_separate_shader_objects : enable\n"
1278 "#extension GL_ARB_shading_language_420pack : enable\n"
1279 "out gl_PerVertex {\n"
1280 " vec4 gl_Position;\n"
1281 " float gl_ClipDistance[1];\n"
1282 "};\n"
1283 "layout (location = 0) in vec4 pos;\n"
1284 "layout (location = 0) out vec4 outColor;\n"
1285 "layout (location = 1) out vec4 outColor2;\n"
1286 "void main() {\n"
1287 " gl_Position = pos;\n"
1288 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1289 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1290 " float dists[3];\n"
1291 " dists[0] = 0.0;\n"
1292 " dists[1] = 1.0;\n"
1293 " dists[2] = 1.0;\n"
1294 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1295 "}\n";
1296
1297
1298 static const char *fragShaderText =
1299 //"#version 140\n"
1300 "#version 330\n"
1301 "#extension GL_ARB_separate_shader_objects : enable\n"
1302 "#extension GL_ARB_shading_language_420pack : enable\n"
1303 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1304 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1305 "layout (location = 0) in vec4 color;\n"
1306 "layout (location = 1) in vec4 color2;\n"
1307 "void main() {\n"
1308 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1309 " float dist_squared = dot(pos, pos);\n"
1310 " gl_FragColor = (dist_squared < 400.0)\n"
1311 " ? color * gl_ClipDistance[0]\n"
1312 " : color2;\n"
1313 "}\n";
1314
1315 ASSERT_NO_FATAL_FAILURE(InitState());
1316 ASSERT_NO_FATAL_FAILURE(InitViewport());
1317
1318 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001319 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001320
1321 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1322 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1323
1324 XglPipelineObj pipelineobj(m_device);
1325 pipelineobj.AddShader(&vs);
1326 pipelineobj.AddShader(&ps);
1327
1328 XglDescriptorSetObj descriptorSet(m_device);
1329 descriptorSet.AttachMemoryView(&meshBuffer);
1330
1331 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1332 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1333 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1334 };
1335
1336 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1337 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1338 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1339 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1340 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1341 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1342 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1343 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1344 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1345
1346 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1347 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1348 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1349
Tony Barboura3953b82014-12-03 15:46:29 -07001350 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001351 QueueCommandBuffer(NULL, 0);
1352
1353}
Tony Barbourf43b6982014-11-25 13:18:32 -07001354
GregF7a23c792014-12-02 17:19:34 -07001355TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1356{
1357 static const char *vertShaderText =
1358 "#version 140\n"
1359 "#extension GL_ARB_separate_shader_objects : enable\n"
1360 "#extension GL_ARB_shading_language_420pack : enable\n"
1361 "layout (location = 0) in vec4 pos;\n"
1362 "layout (location = 0) out vec4 outColor;\n"
1363 "layout (location = 1) out vec4 outColor2;\n"
1364 "void main() {\n"
1365 " gl_Position = pos;\n"
1366 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1367 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1368 "}\n";
1369
1370
1371 static const char *fragShaderText =
1372 //"#version 140\n"
1373 "#version 330\n"
1374 "#extension GL_ARB_separate_shader_objects : enable\n"
1375 "#extension GL_ARB_shading_language_420pack : enable\n"
1376 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1377 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1378 "layout (location = 0) in vec4 color;\n"
1379 "layout (location = 1) in vec4 color2;\n"
1380 "void main() {\n"
1381 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1382 " float dist_squared = dot(pos, pos);\n"
1383 " if (dist_squared < 100.0)\n"
1384 " discard;\n"
1385 " gl_FragColor = (dist_squared < 400.0)\n"
1386 " ? color\n"
1387 " : color2;\n"
1388 "}\n";
1389
1390 ASSERT_NO_FATAL_FAILURE(InitState());
1391 ASSERT_NO_FATAL_FAILURE(InitViewport());
1392
1393 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001394 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF7a23c792014-12-02 17:19:34 -07001395
1396 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1397 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1398
1399 XglPipelineObj pipelineobj(m_device);
1400 pipelineobj.AddShader(&vs);
1401 pipelineobj.AddShader(&ps);
1402
1403 XglDescriptorSetObj descriptorSet(m_device);
1404 descriptorSet.AttachMemoryView(&meshBuffer);
1405
1406 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1407 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1408 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1409 };
1410
1411 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1412 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1413 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1414 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1415 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1416 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1417 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1418 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1419 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1420
1421 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1422 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1423 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1424
Tony Barbour09da2212014-12-03 16:13:23 -07001425 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF7a23c792014-12-02 17:19:34 -07001426 QueueCommandBuffer(NULL, 0);
1427
1428}
1429
1430
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001431TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001432{
1433 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001434 "#version 140\n"
1435 "#extension GL_ARB_separate_shader_objects : enable\n"
1436 "#extension GL_ARB_shading_language_420pack : enable\n"
1437 "\n"
1438 "layout(binding = 0) uniform buf {\n"
1439 " mat4 MVP;\n"
1440 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001441 "void main() {\n"
1442 " vec2 vertices[3];"
1443 " vertices[0] = vec2(-0.5, -0.5);\n"
1444 " vertices[1] = vec2( 0.5, -0.5);\n"
1445 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001446 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001447 "}\n";
1448
1449 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001450 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001451 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001452 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001453 "}\n";
1454
Tony Barbourf43b6982014-11-25 13:18:32 -07001455 ASSERT_NO_FATAL_FAILURE(InitState());
1456 ASSERT_NO_FATAL_FAILURE(InitViewport());
1457
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001458 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001459 glm::mat4 Projection = glm::mat4(1.0f);
1460 glm::mat4 View = glm::mat4(1.0f);
1461 glm::mat4 Model = glm::mat4(1.0f);
1462 glm::mat4 MVP = Projection * View * Model;
1463 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1464
Tony Barbourf43b6982014-11-25 13:18:32 -07001465 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1466 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1467 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001468
Tony Barbourf43b6982014-11-25 13:18:32 -07001469 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1470
1471 XglPipelineObj pipelineobj(m_device);
1472 pipelineobj.AddShader(&vs);
1473 pipelineobj.AddShader(&ps);
1474
1475 // Create descriptor set and attach the constant buffer to it
1476 XglDescriptorSetObj descriptorSet(m_device);
1477 descriptorSet.AttachMemoryView(&MVPBuffer);
1478
1479 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1480
Tony Barboura3953b82014-12-03 15:46:29 -07001481 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001482 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1483
Tony Barbour09da2212014-12-03 16:13:23 -07001484 RotateTriangleVSUniform(Projection, View, Model, &MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001485}
1486
Tony Barbourf43b6982014-11-25 13:18:32 -07001487TEST_F(XglRenderTest, MixTriangle)
1488{
1489 // This tests location applied to varyings. Notice that we have switched foo
1490 // and bar in the FS. The triangle should be blended with red, green and blue
1491 // corners.
1492 static const char *vertShaderText =
1493 "#version 140\n"
1494 "#extension GL_ARB_separate_shader_objects : enable\n"
1495 "#extension GL_ARB_shading_language_420pack : enable\n"
1496 "layout (location=0) out vec4 bar;\n"
1497 "layout (location=1) out vec4 foo;\n"
1498 "layout (location=2) out float scale;\n"
1499 "vec2 vertices[3];\n"
1500 "void main() {\n"
1501 " vertices[0] = vec2(-1.0, -1.0);\n"
1502 " vertices[1] = vec2( 1.0, -1.0);\n"
1503 " vertices[2] = vec2( 0.0, 1.0);\n"
1504 "vec4 colors[3];\n"
1505 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1506 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1507 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1508 " foo = colors[gl_VertexID % 3];\n"
1509 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1510 " scale = 1.0;\n"
1511 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1512 "}\n";
1513
1514 static const char *fragShaderText =
1515 "#version 140\n"
1516 "#extension GL_ARB_separate_shader_objects : enable\n"
1517 "#extension GL_ARB_shading_language_420pack : enable\n"
1518 "layout (location = 1) in vec4 bar;\n"
1519 "layout (location = 0) in vec4 foo;\n"
1520 "layout (location = 2) in float scale;\n"
1521 "void main() {\n"
1522 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1523 "}\n";
1524
1525 ASSERT_NO_FATAL_FAILURE(InitState());
1526 ASSERT_NO_FATAL_FAILURE(InitViewport());
1527
1528 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1529 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1530
1531 XglPipelineObj pipelineobj(m_device);
1532 pipelineobj.AddShader(&vs);
1533 pipelineobj.AddShader(&ps);
1534
1535 XglDescriptorSetObj descriptorSet(m_device);
1536
Tony Barboura3953b82014-12-03 15:46:29 -07001537 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001538 QueueCommandBuffer(NULL, 0);
1539}
1540
1541TEST_F(XglRenderTest, TriVertFetchAndVertID)
1542{
1543 // This tests that attributes work in the presence of gl_VertexID
1544
1545 static const char *vertShaderText =
1546 "#version 140\n"
1547 "#extension GL_ARB_separate_shader_objects : enable\n"
1548 "#extension GL_ARB_shading_language_420pack : enable\n"
1549 //XYZ1( -1, -1, -1 )
1550 "layout (location = 0) in vec4 pos;\n"
1551 //XYZ1( 0.f, 0.f, 0.f )
1552 "layout (location = 1) in vec4 inColor;\n"
1553 "layout (location = 0) out vec4 outColor;\n"
1554 "void main() {\n"
1555 " outColor = inColor;\n"
1556 " vec4 vertices[3];"
1557 " vertices[gl_VertexID % 3] = pos;\n"
1558 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1559 "}\n";
1560
1561
1562 static const char *fragShaderText =
1563 "#version 140\n"
1564 "#extension GL_ARB_separate_shader_objects : enable\n"
1565 "#extension GL_ARB_shading_language_420pack : enable\n"
1566 "layout (location = 0) in vec4 color;\n"
1567 "void main() {\n"
1568 " gl_FragColor = color;\n"
1569 "}\n";
1570
1571 ASSERT_NO_FATAL_FAILURE(InitState());
1572 ASSERT_NO_FATAL_FAILURE(InitViewport());
1573
1574 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001575 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001576
1577 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1578 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1579
1580 XglPipelineObj pipelineobj(m_device);
1581 pipelineobj.AddShader(&vs);
1582 pipelineobj.AddShader(&ps);
1583
1584 XglDescriptorSetObj descriptorSet(m_device);
1585 descriptorSet.AttachMemoryView(&meshBuffer);
1586
1587 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1588 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1589 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1590 };
1591
1592 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1593 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1594 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1595 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1596 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1597 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1598 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1599 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1600 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1601
1602 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1603 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1604 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1605
Tony Barboura3953b82014-12-03 15:46:29 -07001606 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001607 QueueCommandBuffer(NULL, 0);
1608}
1609
1610TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1611{
1612 // This tests that attributes work in the presence of gl_VertexID
1613 // and a dead attribute in position 0. Draws a triangle with yellow,
1614 // red and green corners, starting at top and going clockwise.
1615
1616 static const char *vertShaderText =
1617 "#version 140\n"
1618 "#extension GL_ARB_separate_shader_objects : enable\n"
1619 "#extension GL_ARB_shading_language_420pack : enable\n"
1620 //XYZ1( -1, -1, -1 )
1621 "layout (location = 0) in vec4 pos;\n"
1622 //XYZ1( 0.f, 0.f, 0.f )
1623 "layout (location = 1) in vec4 inColor;\n"
1624 "layout (location = 0) out vec4 outColor;\n"
1625 "void main() {\n"
1626 " outColor = inColor;\n"
1627 " vec2 vertices[3];"
1628 " vertices[0] = vec2(-1.0, -1.0);\n"
1629 " vertices[1] = vec2( 1.0, -1.0);\n"
1630 " vertices[2] = vec2( 0.0, 1.0);\n"
1631 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1632 "}\n";
1633
1634
1635 static const char *fragShaderText =
1636 "#version 140\n"
1637 "#extension GL_ARB_separate_shader_objects : enable\n"
1638 "#extension GL_ARB_shading_language_420pack : enable\n"
1639 "layout (location = 0) in vec4 color;\n"
1640 "void main() {\n"
1641 " gl_FragColor = color;\n"
1642 "}\n";
1643
1644 ASSERT_NO_FATAL_FAILURE(InitState());
1645 ASSERT_NO_FATAL_FAILURE(InitViewport());
1646
1647 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001648 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001649
1650 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1651 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1652
1653 XglPipelineObj pipelineobj(m_device);
1654 pipelineobj.AddShader(&vs);
1655 pipelineobj.AddShader(&ps);
1656
1657 XglDescriptorSetObj descriptorSet(m_device);
1658 descriptorSet.AttachMemoryView(&meshBuffer);
1659
1660 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1661 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1662 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1663 };
1664
1665 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1666 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1667 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1668 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1669 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1670 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1671 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1672 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1673 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1674
1675 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1676 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1677 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1678
Tony Barbour09da2212014-12-03 16:13:23 -07001679 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001680 QueueCommandBuffer(NULL, 0);
1681
1682}
1683
1684TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001685{
1686 static const char *vertShaderText =
1687 "#version 140\n"
1688 "layout (std140) uniform bufferVals {\n"
1689 " mat4 mvp;\n"
1690 "} myBufferVals;\n"
1691 "in vec4 pos;\n"
1692 "in vec4 inColor;\n"
1693 "out vec4 outColor;\n"
1694 "void main() {\n"
1695 " outColor = inColor;\n"
1696 " gl_Position = myBufferVals.mvp * pos;\n"
1697 "}\n";
1698
1699 static const char *fragShaderText =
1700 "#version 130\n"
1701 "in vec4 color;\n"
1702 "void main() {\n"
1703 " gl_FragColor = color;\n"
1704 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001705 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001706
Tony Barbourf43b6982014-11-25 13:18:32 -07001707 glm::mat4 View = glm::lookAt(
1708 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1709 glm::vec3(0,0,0), // and looks at the origin
1710 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1711 );
1712
1713 glm::mat4 Model = glm::mat4(1.0f);
1714
1715 glm::mat4 MVP = Projection * View * Model;
1716
1717 ASSERT_NO_FATAL_FAILURE(InitState());
1718 ASSERT_NO_FATAL_FAILURE(InitViewport());
1719 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1720
1721 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1722 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1723
1724 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1725
1726 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1727 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1728 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1729
1730 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1731
1732 XglPipelineObj pipelineobj(m_device);
1733 pipelineobj.AddShader(&vs);
1734 pipelineobj.AddShader(&ps);
1735
1736 XglDescriptorSetObj descriptorSet(m_device);
1737 descriptorSet.AttachMemoryView(&MVPBuffer);
1738
1739 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1740 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1741
1742 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1743 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1744 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1745 };
1746
1747 // this is the current description of g_vbData
1748 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1749 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1750 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1751 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1752 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1753 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1754 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1755 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1756 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1757
1758 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1759 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1760 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1761
Tony Barboura3953b82014-12-03 15:46:29 -07001762 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07001763
1764 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1765
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001766}
1767
Tony Barbourf43b6982014-11-25 13:18:32 -07001768TEST_F(XglRenderTest, VSTexture)
1769{
1770 // The expected result from this test is a green and red triangle;
1771 // one red vertex on the left, two green vertices on the right.
1772 static const char *vertShaderText =
1773 "#version 130\n"
1774 "out vec4 texColor;\n"
1775 "uniform sampler2D surface;\n"
1776 "void main() {\n"
1777 " vec2 vertices[3];"
1778 " vertices[0] = vec2(-0.5, -0.5);\n"
1779 " vertices[1] = vec2( 0.5, -0.5);\n"
1780 " vertices[2] = vec2( 0.5, 0.5);\n"
1781 " vec2 positions[3];"
1782 " positions[0] = vec2( 0.0, 0.0);\n"
1783 " positions[1] = vec2( 0.25, 0.1);\n"
1784 " positions[2] = vec2( 0.1, 0.25);\n"
1785 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1786 " texColor = textureLod(surface, samplePos, 0.0);\n"
1787 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1788 "}\n";
1789
1790 static const char *fragShaderText =
1791 "#version 130\n"
1792 "in vec4 texColor;\n"
1793 "void main() {\n"
1794 " gl_FragColor = texColor;\n"
1795 "}\n";
1796
1797 ASSERT_NO_FATAL_FAILURE(InitState());
1798 ASSERT_NO_FATAL_FAILURE(InitViewport());
1799
1800 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1801 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1802 XglSamplerObj sampler(m_device);
1803 XglTextureObj texture(m_device);
1804
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001805 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001806 vs.BindShaderEntitySlotToSampler(0, &sampler);
1807
1808 XglPipelineObj pipelineobj(m_device);
1809 pipelineobj.AddShader(&vs);
1810 pipelineobj.AddShader(&ps);
1811
1812 XglDescriptorSetObj descriptorSet(m_device);
1813 descriptorSet.AttachImageView(&texture);
1814 descriptorSet.AttachSampler(&sampler);
1815
1816 m_memoryRefManager.AddMemoryRef(&texture);
1817
Tony Barboura3953b82014-12-03 15:46:29 -07001818 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001819 QueueCommandBuffer(NULL, 0);
1820
1821}
1822TEST_F(XglRenderTest, TexturedTriangle)
1823{
1824 // The expected result from this test is a red and green checkered triangle
1825 static const char *vertShaderText =
1826 "#version 140\n"
1827 "#extension GL_ARB_separate_shader_objects : enable\n"
1828 "#extension GL_ARB_shading_language_420pack : enable\n"
1829 "layout (location = 0) out vec2 samplePos;\n"
1830 "void main() {\n"
1831 " vec2 vertices[3];"
1832 " vertices[0] = vec2(-0.5, -0.5);\n"
1833 " vertices[1] = vec2( 0.5, -0.5);\n"
1834 " vertices[2] = vec2( 0.5, 0.5);\n"
1835 " vec2 positions[3];"
1836 " positions[0] = vec2( 0.0, 0.0);\n"
1837 " positions[1] = vec2( 1.0, 0.0);\n"
1838 " positions[2] = vec2( 1.0, 1.0);\n"
1839 " samplePos = positions[gl_VertexID % 3];\n"
1840 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1841 "}\n";
1842
1843 static const char *fragShaderText =
1844 "#version 140\n"
1845 "#extension GL_ARB_separate_shader_objects : enable\n"
1846 "#extension GL_ARB_shading_language_420pack : enable\n"
1847 "layout (location = 0) in vec2 samplePos;\n"
1848 "layout (binding = 0) uniform sampler2D surface;\n"
1849 "layout (location=0) out vec4 outColor;\n"
1850 "void main() {\n"
1851 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1852 " outColor = texColor;\n"
1853 "}\n";
1854
1855 ASSERT_NO_FATAL_FAILURE(InitState());
1856 ASSERT_NO_FATAL_FAILURE(InitViewport());
1857
1858 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1859 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1860 XglSamplerObj sampler(m_device);
1861 XglTextureObj texture(m_device);
1862
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001863 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001864 ps.BindShaderEntitySlotToSampler(0, &sampler);
1865
1866 XglPipelineObj pipelineobj(m_device);
1867 pipelineobj.AddShader(&vs);
1868 pipelineobj.AddShader(&ps);
1869
1870 XglDescriptorSetObj descriptorSet(m_device);
1871 descriptorSet.AttachImageView(&texture);
1872 descriptorSet.AttachSampler(&sampler);
1873
1874 m_memoryRefManager.AddMemoryRef(&texture);
1875
Tony Barboura3953b82014-12-03 15:46:29 -07001876 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001877 QueueCommandBuffer(NULL, 0);
1878}
1879TEST_F(XglRenderTest, TexturedTriangleClip)
1880{
1881 // The expected result from this test is a red and green checkered triangle
1882 static const char *vertShaderText =
1883 "#version 330\n"
1884 "#extension GL_ARB_separate_shader_objects : enable\n"
1885 "#extension GL_ARB_shading_language_420pack : enable\n"
1886 "layout (location = 0) out vec2 samplePos;\n"
1887 "out gl_PerVertex {\n"
1888 " vec4 gl_Position;\n"
1889 " float gl_ClipDistance[1];\n"
1890 "};\n"
1891 "void main() {\n"
1892 " vec2 vertices[3];"
1893 " vertices[0] = vec2(-0.5, -0.5);\n"
1894 " vertices[1] = vec2( 0.5, -0.5);\n"
1895 " vertices[2] = vec2( 0.5, 0.5);\n"
1896 " vec2 positions[3];"
1897 " positions[0] = vec2( 0.0, 0.0);\n"
1898 " positions[1] = vec2( 1.0, 0.0);\n"
1899 " positions[2] = vec2( 1.0, 1.0);\n"
1900 " float dists[3];\n"
1901 " dists[0] = 1.0;\n"
1902 " dists[1] = 1.0;\n"
1903 " dists[2] = -1.0;\n"
1904 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1905 " samplePos = positions[gl_VertexID % 3];\n"
1906 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1907 "}\n";
1908
1909 static const char *fragShaderText =
1910 "#version 140\n"
1911 "#extension GL_ARB_separate_shader_objects : enable\n"
1912 "#extension GL_ARB_shading_language_420pack : enable\n"
1913 "layout (location = 0) in vec2 samplePos;\n"
1914 "layout (binding = 0) uniform sampler2D surface;\n"
1915 "layout (location=0) out vec4 outColor;\n"
1916 "void main() {\n"
1917 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1918 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1919 " outColor = texColor;\n"
1920 "}\n";
1921
1922
1923 ASSERT_NO_FATAL_FAILURE(InitState());
1924 ASSERT_NO_FATAL_FAILURE(InitViewport());
1925
1926 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1927 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1928 XglSamplerObj sampler(m_device);
1929 XglTextureObj texture(m_device);
1930
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001931 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001932 ps.BindShaderEntitySlotToSampler(0, &sampler);
1933
1934 XglPipelineObj pipelineobj(m_device);
1935 pipelineobj.AddShader(&vs);
1936 pipelineobj.AddShader(&ps);
1937
1938 XglDescriptorSetObj descriptorSet(m_device);
1939 descriptorSet.AttachImageView(&texture);
1940 descriptorSet.AttachSampler(&sampler);
1941
1942 m_memoryRefManager.AddMemoryRef(&texture);
1943
Tony Barboura3953b82014-12-03 15:46:29 -07001944 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001945 QueueCommandBuffer(NULL, 0);
1946}
1947TEST_F(XglRenderTest, FSTriangle)
1948{
1949 // The expected result from this test is a red and green checkered triangle
1950 static const char *vertShaderText =
1951 "#version 140\n"
1952 "#extension GL_ARB_separate_shader_objects : enable\n"
1953 "#extension GL_ARB_shading_language_420pack : enable\n"
1954 "layout (location = 0) out vec2 samplePos;\n"
1955 "void main() {\n"
1956 " vec2 vertices[3];"
1957 " vertices[0] = vec2(-0.5, -0.5);\n"
1958 " vertices[1] = vec2( 0.5, -0.5);\n"
1959 " vertices[2] = vec2( 0.5, 0.5);\n"
1960 " vec2 positions[3];"
1961 " positions[0] = vec2( 0.0, 0.0);\n"
1962 " positions[1] = vec2( 1.0, 0.0);\n"
1963 " positions[2] = vec2( 1.0, 1.0);\n"
1964 " samplePos = positions[gl_VertexID % 3];\n"
1965 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1966 "}\n";
1967
1968 static const char *fragShaderText =
1969 "#version 140\n"
1970 "#extension GL_ARB_separate_shader_objects : enable\n"
1971 "#extension GL_ARB_shading_language_420pack : enable\n"
1972 "layout (location = 0) in vec2 samplePos;\n"
1973 "layout (binding = 0) uniform sampler2D surface;\n"
1974 "layout (location=0) out vec4 outColor;\n"
1975 "void main() {\n"
1976 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1977 " outColor = texColor;\n"
1978 "}\n";
1979
1980 ASSERT_NO_FATAL_FAILURE(InitState());
1981 ASSERT_NO_FATAL_FAILURE(InitViewport());
1982
1983 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1984 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1985 XglSamplerObj sampler(m_device);
1986 XglTextureObj texture(m_device);
1987
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001988 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001989 ps.BindShaderEntitySlotToSampler(0, &sampler);
1990
1991 XglPipelineObj pipelineobj(m_device);
1992 pipelineobj.AddShader(&vs);
1993 pipelineobj.AddShader(&ps);
1994
1995 XglDescriptorSetObj descriptorSet(m_device);
1996 descriptorSet.AttachImageView(&texture);
1997 descriptorSet.AttachSampler(&sampler);
1998
1999 m_memoryRefManager.AddMemoryRef(&texture);
2000
Tony Barboura3953b82014-12-03 15:46:29 -07002001 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002002 QueueCommandBuffer(NULL, 0);
2003}
2004TEST_F(XglRenderTest, SamplerBindingsTriangle)
2005{
2006 // This test sets bindings on the samplers
2007 // For now we are asserting that sampler and texture pairs
2008 // march in lock step, and are set via GLSL binding. This can
2009 // and will probably change.
2010 // The sampler bindings should match the sampler and texture slot
2011 // number set up by the application.
2012 // This test will result in a blue triangle
2013 static const char *vertShaderText =
2014 "#version 140\n"
2015 "#extension GL_ARB_separate_shader_objects : enable\n"
2016 "#extension GL_ARB_shading_language_420pack : enable\n"
2017 "layout (location = 0) out vec4 samplePos;\n"
2018 "void main() {\n"
2019 " vec2 vertices[3];"
2020 " vertices[0] = vec2(-0.5, -0.5);\n"
2021 " vertices[1] = vec2( 0.5, -0.5);\n"
2022 " vertices[2] = vec2( 0.5, 0.5);\n"
2023 " vec2 positions[3];"
2024 " positions[0] = vec2( 0.0, 0.0);\n"
2025 " positions[1] = vec2( 1.0, 0.0);\n"
2026 " positions[2] = vec2( 1.0, 1.0);\n"
2027 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
2028 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2029 "}\n";
2030
2031 static const char *fragShaderText =
2032 "#version 140\n"
2033 "#extension GL_ARB_separate_shader_objects : enable\n"
2034 "#extension GL_ARB_shading_language_420pack : enable\n"
2035 "layout (location = 0) in vec4 samplePos;\n"
2036 "layout (binding = 0) uniform sampler2D surface0;\n"
2037 "layout (binding = 1) uniform sampler2D surface1;\n"
2038 "layout (binding = 12) uniform sampler2D surface2;\n"
2039 "void main() {\n"
2040 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
2041 "}\n";
2042
2043 ASSERT_NO_FATAL_FAILURE(InitState());
2044 ASSERT_NO_FATAL_FAILURE(InitViewport());
2045
2046 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2047 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2048
2049 XglSamplerObj sampler1(m_device);
2050 XglSamplerObj sampler2(m_device);
2051 XglSamplerObj sampler3(m_device);
2052
2053 XglTextureObj texture1(m_device); // Red
2054 texture1.ChangeColors(0xffff0000,0xffff0000);
2055 XglTextureObj texture2(m_device); // Green
2056 texture2.ChangeColors(0xff00ff00,0xff00ff00);
2057 XglTextureObj texture3(m_device); // Blue
2058 texture3.ChangeColors(0xff0000ff,0xff0000ff);
2059
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002060 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002061 ps.BindShaderEntitySlotToSampler(0, &sampler1);
2062
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002063 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Tony Barbourf43b6982014-11-25 13:18:32 -07002064 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2065
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002066 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture3);
Tony Barbourf43b6982014-11-25 13:18:32 -07002067 ps.BindShaderEntitySlotToSampler(12, &sampler3);
2068
2069 XglPipelineObj pipelineobj(m_device);
2070 pipelineobj.AddShader(&vs);
2071 pipelineobj.AddShader(&ps);
2072
2073 XglDescriptorSetObj descriptorSet(m_device);
2074 descriptorSet.AttachImageView(&texture1);
2075 descriptorSet.AttachSampler(&sampler1);
2076 descriptorSet.AttachImageView(&texture2);
2077 descriptorSet.AttachSampler(&sampler2);
2078 descriptorSet.AttachImageView(&texture3);
2079 descriptorSet.AttachSampler(&sampler3);
2080
2081 m_memoryRefManager.AddMemoryRef(&texture1);
2082 m_memoryRefManager.AddMemoryRef(&texture2);
2083 m_memoryRefManager.AddMemoryRef(&texture3);
2084
Tony Barboura3953b82014-12-03 15:46:29 -07002085 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002086 QueueCommandBuffer(NULL, 0);
2087
2088}
2089
2090TEST_F(XglRenderTest, TriangleVSUniformBlock)
2091{
2092 // The expected result from this test is a blue triangle
2093
2094 static const char *vertShaderText =
2095 "#version 140\n"
2096 "#extension GL_ARB_separate_shader_objects : enable\n"
2097 "#extension GL_ARB_shading_language_420pack : enable\n"
2098 "layout (location = 0) out vec4 outColor;\n"
2099 "layout (std140, binding = 0) uniform bufferVals {\n"
2100 " vec4 red;\n"
2101 " vec4 green;\n"
2102 " vec4 blue;\n"
2103 " vec4 white;\n"
2104 "} myBufferVals;\n"
2105 "void main() {\n"
2106 " vec2 vertices[3];"
2107 " vertices[0] = vec2(-0.5, -0.5);\n"
2108 " vertices[1] = vec2( 0.5, -0.5);\n"
2109 " vertices[2] = vec2( 0.5, 0.5);\n"
2110 " outColor = myBufferVals.blue;\n"
2111 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2112 "}\n";
2113
2114 static const char *fragShaderText =
2115 "#version 140\n"
2116 "#extension GL_ARB_separate_shader_objects : enable\n"
2117 "#extension GL_ARB_shading_language_420pack : enable\n"
2118 "layout (location = 0) in vec4 inColor;\n"
2119 "void main() {\n"
2120 " gl_FragColor = inColor;\n"
2121 "}\n";
2122
2123 ASSERT_NO_FATAL_FAILURE(InitState());
2124 ASSERT_NO_FATAL_FAILURE(InitViewport());
2125
2126 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2127 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2128
2129 // Let's populate our buffer with the following:
2130 // vec4 red;
2131 // vec4 green;
2132 // vec4 blue;
2133 // vec4 white;
2134 const int valCount = 4 * 4;
2135 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2136 0.0, 1.0, 0.0, 1.0,
2137 0.0, 0.0, 1.0, 1.0,
2138 1.0, 1.0, 1.0, 1.0 };
2139
2140 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2141 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2142
2143 XglPipelineObj pipelineobj(m_device);
2144 pipelineobj.AddShader(&vs);
2145 pipelineobj.AddShader(&ps);
2146
2147 XglDescriptorSetObj descriptorSet(m_device);
2148 descriptorSet.AttachMemoryView(&colorBuffer);
2149
Tony Barboura3953b82014-12-03 15:46:29 -07002150 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002151 QueueCommandBuffer(NULL, 0);
2152
2153}
2154
2155TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2156{
2157 // This test allows the shader to select which buffer it is
2158 // pulling from using layout binding qualifier.
2159 // There are corresponding changes in the compiler stack that
2160 // will select the buffer using binding directly.
2161 // The binding number should match the slot number set up by
2162 // the application.
2163 // The expected result from this test is a purple triangle
2164
2165 static const char *vertShaderText =
2166 "#version 140\n"
2167 "#extension GL_ARB_separate_shader_objects : enable\n"
2168 "#extension GL_ARB_shading_language_420pack : enable\n"
2169 "void main() {\n"
2170 " vec2 vertices[3];"
2171 " vertices[0] = vec2(-0.5, -0.5);\n"
2172 " vertices[1] = vec2( 0.5, -0.5);\n"
2173 " vertices[2] = vec2( 0.5, 0.5);\n"
2174 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2175 "}\n";
2176
2177 static const char *fragShaderText =
2178 "#version 140\n"
2179 "#extension GL_ARB_separate_shader_objects : enable\n"
2180 "#extension GL_ARB_shading_language_420pack : enable\n"
2181 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2182 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2183 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2184 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2185 "void main() {\n"
2186 " gl_FragColor = myBlueVal.color;\n"
2187 " gl_FragColor += myRedVal.color;\n"
2188 "}\n";
2189
2190 ASSERT_NO_FATAL_FAILURE(InitState());
2191 ASSERT_NO_FATAL_FAILURE(InitViewport());
2192
2193 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2194 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2195
2196 // We're going to create a number of uniform buffers, and then allow
2197 // the shader to select which it wants to read from with a binding
2198
2199 // Let's populate the buffers with a single color each:
2200 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2201 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2202 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2203 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2204
2205 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2206 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2207 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2208 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2209
2210 const int redCount = sizeof(redVals) / sizeof(float);
2211 const int greenCount = sizeof(greenVals) / sizeof(float);
2212 const int blueCount = sizeof(blueVals) / sizeof(float);
2213 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2214
2215 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2216 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2217
2218 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2219 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2220
2221 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2222 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2223
2224 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2225 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2226
2227 XglPipelineObj pipelineobj(m_device);
2228 pipelineobj.AddShader(&vs);
2229 pipelineobj.AddShader(&ps);
2230
2231 XglDescriptorSetObj descriptorSet(m_device);
2232 descriptorSet.AttachMemoryView(&redBuffer);
2233 descriptorSet.AttachMemoryView(&greenBuffer);
2234 descriptorSet.AttachMemoryView(&blueBuffer);
2235 descriptorSet.AttachMemoryView(&whiteBuffer);
2236
Tony Barboura3953b82014-12-03 15:46:29 -07002237 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002238 QueueCommandBuffer(NULL, 0);
2239
2240}
2241
2242TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2243{
2244 // This test is the same as TriangleFSUniformBlockBinding, but
2245 // it does not provide an instance name.
2246 // The expected result from this test is a purple triangle
2247
2248 static const char *vertShaderText =
2249 "#version 140\n"
2250 "#extension GL_ARB_separate_shader_objects : enable\n"
2251 "#extension GL_ARB_shading_language_420pack : enable\n"
2252 "void main() {\n"
2253 " vec2 vertices[3];"
2254 " vertices[0] = vec2(-0.5, -0.5);\n"
2255 " vertices[1] = vec2( 0.5, -0.5);\n"
2256 " vertices[2] = vec2( 0.5, 0.5);\n"
2257 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2258 "}\n";
2259
2260 static const char *fragShaderText =
2261 "#version 430\n"
2262 "#extension GL_ARB_separate_shader_objects : enable\n"
2263 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002264 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2265 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2266 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2267 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
Cody Northrop68a10f62014-12-05 15:44:14 -07002268 "layout (location = 0) out vec4 outColor;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002269 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -07002270 " outColor = blue;\n"
2271 " outColor += red;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002272 "}\n";
2273 ASSERT_NO_FATAL_FAILURE(InitState());
2274 ASSERT_NO_FATAL_FAILURE(InitViewport());
2275
2276 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2277 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2278
2279 // We're going to create a number of uniform buffers, and then allow
2280 // the shader to select which it wants to read from with a binding
2281
2282 // Let's populate the buffers with a single color each:
2283 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2284 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2285 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2286 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2287
2288 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2289 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2290 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2291 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2292
2293 const int redCount = sizeof(redVals) / sizeof(float);
2294 const int greenCount = sizeof(greenVals) / sizeof(float);
2295 const int blueCount = sizeof(blueVals) / sizeof(float);
2296 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2297
2298 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2299 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2300
2301 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2302 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2303
2304 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2305 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2306
2307 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002308 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07002309
2310 XglPipelineObj pipelineobj(m_device);
2311 pipelineobj.AddShader(&vs);
2312 pipelineobj.AddShader(&ps);
2313
2314 XglDescriptorSetObj descriptorSet(m_device);
2315 descriptorSet.AttachMemoryView(&redBuffer);
2316 descriptorSet.AttachMemoryView(&greenBuffer);
2317 descriptorSet.AttachMemoryView(&blueBuffer);
2318 descriptorSet.AttachMemoryView(&whiteBuffer);
2319
Tony Barboura3953b82014-12-03 15:46:29 -07002320 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002321 QueueCommandBuffer(NULL, 0);
2322
2323}
2324
2325TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2326{
2327 static const char *vertShaderText =
2328 "#version 140\n"
2329 "#extension GL_ARB_separate_shader_objects : enable\n"
2330 "#extension GL_ARB_shading_language_420pack : enable\n"
2331 "layout (std140, binding=0) uniform bufferVals {\n"
2332 " mat4 mvp;\n"
2333 "} myBufferVals;\n"
2334 "layout (location=0) in vec4 pos;\n"
2335 "layout (location=0) out vec2 UV;\n"
2336 "void main() {\n"
2337 " vec2 positions[3];"
2338 " positions[0] = vec2( 0.0, 0.0);\n"
2339 " positions[1] = vec2( 0.25, 0.1);\n"
2340 " positions[2] = vec2( 0.1, 0.25);\n"
2341 " UV = positions[gl_VertexID % 3];\n"
2342 " gl_Position = myBufferVals.mvp * pos;\n"
2343 "}\n";
2344
2345 static const char *fragShaderText =
2346 "#version 140\n"
2347 "#extension GL_ARB_separate_shader_objects : enable\n"
2348 "#extension GL_ARB_shading_language_420pack : enable\n"
2349 "layout (binding=0) uniform sampler2D surface;\n"
2350 "layout (location=0) out vec4 outColor;\n"
2351 "layout (location=0) in vec2 UV;\n"
2352 "void main() {\n"
2353 " outColor= textureLod(surface, UV, 0.0);\n"
2354 "}\n";
2355 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2356
2357 glm::mat4 View = glm::lookAt(
2358 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2359 glm::vec3(0,0,0), // and looks at the origin
2360 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2361 );
2362
2363 glm::mat4 Model = glm::mat4(1.0f);
2364
2365 glm::mat4 MVP = Projection * View * Model;
2366
2367
2368 ASSERT_NO_FATAL_FAILURE(InitState());
2369 ASSERT_NO_FATAL_FAILURE(InitViewport());
2370 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2371
2372 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2373 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
Tony Barboure6152042014-12-10 17:40:15 -07002374 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07002375
2376
2377 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2378
2379 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2380 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2381 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2382 XglSamplerObj sampler(m_device);
2383 XglTextureObj texture(m_device);
2384
2385 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2386 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002387 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002388 ps.BindShaderEntitySlotToSampler(0, &sampler);
2389
2390 XglPipelineObj pipelineobj(m_device);
2391 pipelineobj.AddShader(&vs);
2392 pipelineobj.AddShader(&ps);
2393
2394 XglDescriptorSetObj descriptorSet(m_device);
2395
2396 descriptorSet.AttachMemoryView(&mvpBuffer);
2397 descriptorSet.AttachImageView(&texture);
2398 descriptorSet.AttachSampler(&sampler);
2399
2400 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2401 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2402 m_memoryRefManager.AddMemoryRef(&texture);
2403
2404 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2405 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2406 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2407 };
2408
2409 // this is the current description of g_vbData
2410 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2411 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2412 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2413 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2414 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2415 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2416 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2417 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2418 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2419
2420 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2421 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2422 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2423
Tony Barboura3953b82014-12-03 15:46:29 -07002424 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07002425
2426 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2427
2428}
Cody Northropd1ce7842014-12-09 11:17:01 -07002429
2430TEST_F(XglRenderTest, TriangleMixedSamplerUniformBlockBinding)
2431{
2432 // This test mixes binding slots of textures and buffers, ensuring
2433 // that sparse and overlapping assignments work.
Cody Northropa0410942014-12-09 13:59:39 -07002434 // The expected result from this test is a purple triangle, although
Cody Northropd1ce7842014-12-09 11:17:01 -07002435 // you can modify it to move the desired result around.
2436
2437 static const char *vertShaderText =
2438 "#version 140\n"
2439 "#extension GL_ARB_separate_shader_objects : enable\n"
2440 "#extension GL_ARB_shading_language_420pack : enable\n"
2441 "void main() {\n"
2442 " vec2 vertices[3];"
2443 " vertices[0] = vec2(-0.5, -0.5);\n"
2444 " vertices[1] = vec2( 0.5, -0.5);\n"
2445 " vertices[2] = vec2( 0.5, 0.5);\n"
2446 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2447 "}\n";
2448
2449 static const char *fragShaderText =
2450 "#version 430\n"
2451 "#extension GL_ARB_separate_shader_objects : enable\n"
2452 "#extension GL_ARB_shading_language_420pack : enable\n"
2453 "layout (binding = 0) uniform sampler2D surface0;\n"
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002454 "layout (binding = 9) uniform sampler2D surface1;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002455 "layout (binding = 2) uniform sampler2D surface2;\n"
Cody Northropa0410942014-12-09 13:59:39 -07002456 "layout (binding = 4) uniform sampler2D surface3;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002457
Cody Northropa0410942014-12-09 13:59:39 -07002458
2459 "layout (std140, binding = 10) uniform redVal { vec4 red; };"
2460 "layout (std140, binding = 15) uniform greenVal { vec4 green; };"
2461 "layout (std140, binding = 13) uniform blueVal { vec4 blue; };"
2462 "layout (std140, binding = 17) uniform whiteVal { vec4 white; };"
Cody Northropd1ce7842014-12-09 11:17:01 -07002463 "layout (location = 0) out vec4 outColor;\n"
2464 "void main() {\n"
Cody Northropa0410942014-12-09 13:59:39 -07002465 " outColor = red * vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002466 " outColor += white * vec4(0.00001);\n"
2467 " outColor += textureLod(surface2, vec2(0.5), 0.0)* vec4(0.00001);\n"
Cody Northropa0410942014-12-09 13:59:39 -07002468 " outColor += textureLod(surface1, vec2(0.0), 0.0);//* vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002469 "}\n";
2470 ASSERT_NO_FATAL_FAILURE(InitState());
2471 ASSERT_NO_FATAL_FAILURE(InitViewport());
2472
2473 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2474 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2475
Cody Northropd1ce7842014-12-09 11:17:01 -07002476 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2477 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2478 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2479 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2480
2481 const int redCount = sizeof(redVals) / sizeof(float);
2482 const int greenCount = sizeof(greenVals) / sizeof(float);
2483 const int blueCount = sizeof(blueVals) / sizeof(float);
2484 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2485
2486 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Cody Northropa0410942014-12-09 13:59:39 -07002487 ps.BindShaderEntitySlotToMemory(10, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002488
2489 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Cody Northropa0410942014-12-09 13:59:39 -07002490 ps.BindShaderEntitySlotToMemory(15, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002491
2492 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Cody Northropa0410942014-12-09 13:59:39 -07002493 ps.BindShaderEntitySlotToMemory(13, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002494
2495 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropa0410942014-12-09 13:59:39 -07002496 ps.BindShaderEntitySlotToMemory(17, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002497
2498 XglSamplerObj sampler0(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002499 XglTextureObj texture0(m_device); // Light Red
2500 texture0.ChangeColors(0xff800000,0xff800000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002501 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
Cody Northropd1ce7842014-12-09 11:17:01 -07002502 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2503 XglSamplerObj sampler2(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002504 XglTextureObj texture2(m_device); // Light Blue
2505 texture2.ChangeColors(0xff000080,0xff000080);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002506 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Cody Northropd1ce7842014-12-09 11:17:01 -07002507 ps.BindShaderEntitySlotToSampler(2, &sampler2);
2508 XglSamplerObj sampler4(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002509 XglTextureObj texture4(m_device); // Light Green
2510 texture4.ChangeColors(0xff008000,0xff008000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002511 ps.BindShaderEntitySlotToImage(4, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
Cody Northropd1ce7842014-12-09 11:17:01 -07002512 ps.BindShaderEntitySlotToSampler(4, &sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002513
2514 // NOTE: Bindings 1,3,5,7,8,9,11,12,14,16 work for this sampler, but 6 does not!!!
2515 // TODO: Get back here ASAP and understand why.
2516 XglSamplerObj sampler7(m_device);
2517 XglTextureObj texture7(m_device); // Red and Blue
2518 texture7.ChangeColors(0xffff00ff,0xffff00ff);
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002519 ps.BindShaderEntitySlotToImage(9, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2520 ps.BindShaderEntitySlotToSampler(9, &sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002521
2522
2523 XglPipelineObj pipelineobj(m_device);
2524 pipelineobj.AddShader(&vs);
2525 pipelineobj.AddShader(&ps);
2526
2527 XglDescriptorSetObj descriptorSet(m_device);
2528 descriptorSet.AttachMemoryView(&redBuffer);
2529 descriptorSet.AttachMemoryView(&greenBuffer);
2530 descriptorSet.AttachMemoryView(&blueBuffer);
2531 descriptorSet.AttachMemoryView(&whiteBuffer);
2532 descriptorSet.AttachImageView(&texture0);
2533 descriptorSet.AttachSampler(&sampler0);
2534 descriptorSet.AttachImageView(&texture2);
2535 descriptorSet.AttachSampler(&sampler2);
2536 descriptorSet.AttachImageView(&texture4);
2537 descriptorSet.AttachSampler(&sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002538 descriptorSet.AttachImageView(&texture7);
2539 descriptorSet.AttachSampler(&sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002540
2541 m_memoryRefManager.AddMemoryRef(&texture0);
2542 m_memoryRefManager.AddMemoryRef(&texture2);
2543 m_memoryRefManager.AddMemoryRef(&texture4);
Cody Northropa0410942014-12-09 13:59:39 -07002544 m_memoryRefManager.AddMemoryRef(&texture7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002545
2546
2547 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2548 QueueCommandBuffer(NULL, 0);
2549
2550}
2551
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002552TEST_F(XglRenderTest, TriangleMatchingSamplerUniformBlockBinding)
2553{
2554 // This test matches binding slots of textures and buffers, requiring
2555 // the driver to give them distinct number spaces.
2556 // The expected result from this test is a red triangle, although
2557 // you can modify it to move the desired result around.
2558
2559 static const char *vertShaderText =
2560 "#version 140\n"
2561 "#extension GL_ARB_separate_shader_objects : enable\n"
2562 "#extension GL_ARB_shading_language_420pack : enable\n"
2563 "void main() {\n"
2564 " vec2 vertices[3];"
2565 " vertices[0] = vec2(-0.5, -0.5);\n"
2566 " vertices[1] = vec2( 0.5, -0.5);\n"
2567 " vertices[2] = vec2( 0.5, 0.5);\n"
2568 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2569 "}\n";
2570
2571 static const char *fragShaderText =
2572 "#version 430\n"
2573 "#extension GL_ARB_separate_shader_objects : enable\n"
2574 "#extension GL_ARB_shading_language_420pack : enable\n"
2575 "layout (binding = 0) uniform sampler2D surface0;\n"
2576 "layout (binding = 1) uniform sampler2D surface1;\n"
2577 "layout (binding = 2) uniform sampler2D surface2;\n"
2578 "layout (binding = 3) uniform sampler2D surface3;\n"
2579 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2580 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2581 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2582 "layout (std140, binding = 3) uniform whiteVal { vec4 white; };"
2583 "layout (location = 0) out vec4 outColor;\n"
2584 "void main() {\n"
2585 " outColor = red;// * vec4(0.00001);\n"
2586 " outColor += white * vec4(0.00001);\n"
2587 " outColor += textureLod(surface1, vec2(0.5), 0.0)* vec4(0.00001);\n"
2588 " outColor += textureLod(surface3, vec2(0.0), 0.0)* vec4(0.00001);\n"
2589 "}\n";
2590 ASSERT_NO_FATAL_FAILURE(InitState());
2591 ASSERT_NO_FATAL_FAILURE(InitViewport());
2592
2593 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2594 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2595
2596 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2597 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2598 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2599 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2600
2601 const int redCount = sizeof(redVals) / sizeof(float);
2602 const int greenCount = sizeof(greenVals) / sizeof(float);
2603 const int blueCount = sizeof(blueVals) / sizeof(float);
2604 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2605
2606 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2607 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2608
2609 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2610 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2611
2612 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2613 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2614
2615 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2616 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2617
2618 XglSamplerObj sampler0(m_device);
2619 XglTextureObj texture0(m_device); // Light Red
2620 texture0.ChangeColors(0xff800000,0xff800000);
2621 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
2622 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2623 XglSamplerObj sampler2(m_device);
2624 XglTextureObj texture2(m_device); // Light Blue
2625 texture2.ChangeColors(0xff000080,0xff000080);
2626 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
2627 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2628 XglSamplerObj sampler4(m_device);
2629 XglTextureObj texture4(m_device); // Light Green
2630 texture4.ChangeColors(0xff008000,0xff008000);
2631 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
2632 ps.BindShaderEntitySlotToSampler(2, &sampler4);
2633 XglSamplerObj sampler7(m_device);
2634 XglTextureObj texture7(m_device); // Red and Blue
2635 texture7.ChangeColors(0xffff00ff,0xffff00ff);
2636 ps.BindShaderEntitySlotToImage(3, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2637 ps.BindShaderEntitySlotToSampler(3, &sampler7);
2638
2639
2640 XglPipelineObj pipelineobj(m_device);
2641 pipelineobj.AddShader(&vs);
2642 pipelineobj.AddShader(&ps);
2643
2644 XglDescriptorSetObj descriptorSet(m_device);
2645 descriptorSet.AttachMemoryView(&redBuffer);
2646 descriptorSet.AttachMemoryView(&greenBuffer);
2647 descriptorSet.AttachMemoryView(&blueBuffer);
2648 descriptorSet.AttachMemoryView(&whiteBuffer);
2649 descriptorSet.AttachImageView(&texture0);
2650 descriptorSet.AttachSampler(&sampler0);
2651 descriptorSet.AttachImageView(&texture2);
2652 descriptorSet.AttachSampler(&sampler2);
2653 descriptorSet.AttachImageView(&texture4);
2654 descriptorSet.AttachSampler(&sampler4);
2655 descriptorSet.AttachImageView(&texture7);
2656 descriptorSet.AttachSampler(&sampler7);
2657
2658 m_memoryRefManager.AddMemoryRef(&texture0);
2659 m_memoryRefManager.AddMemoryRef(&texture2);
2660 m_memoryRefManager.AddMemoryRef(&texture4);
2661 m_memoryRefManager.AddMemoryRef(&texture7);
2662
2663
2664 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2665 QueueCommandBuffer(NULL, 0);
2666
2667}
2668
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002669int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002670 int result;
2671
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002672 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002673 XglTestFramework::InitArgs(&argc, argv);
2674
Chia-I Wu7133fdc2014-12-15 23:57:34 +08002675 ::testing::AddGlobalTestEnvironment(new TestEnvironment);
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002676
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002677 result = RUN_ALL_TESTS();
2678
2679 XglTestFramework::Finish();
2680 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002681}