blob: 45bb5b28f13bdc59f925628b8efbaefe79d79683 [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 Barboure4ed9942015-01-09 10:06:53 -0700906 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
907 XglCommandBufferObj cmdBuffer(m_device);
908 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -0700909
Tony Barboure4ed9942015-01-09 10:06:53 -0700910 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
911 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
912
913 //GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
914 //QueueCommandBuffer(NULL, 0);
915
916 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
917
918 // render two triangles
919 cmdBuffer.Draw(0, 6, 0, 1);
920
921 // finalize recording of the command buffer
922 cmdBuffer.EndCommandBuffer();
923 cmdBuffer.QueueCommandBuffer(NULL, 0);
924
925 for (int i = 0; i < m_renderTargetCount; i++)
926 RecordImage(m_renderTargets[i]);
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600927}
928
Chia-I Wue09d1a72014-12-05 10:32:23 +0800929TEST_F(XglRenderTest, TriangleMRT)
930{
931 static const char *vertShaderText =
932 "#version 130\n"
933 "in vec4 pos;\n"
934 "void main() {\n"
935 " gl_Position = pos;\n"
936 "}\n";
937
938 static const char *fragShaderText =
939 "#version 130\n"
940 "void main() {\n"
941 " gl_FragData[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
942 " gl_FragData[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
943 "}\n";
944 const XGL_FLOAT vb_data[][2] = {
945 { -1.0f, -1.0f },
946 { 1.0f, -1.0f },
947 { -1.0f, 1.0f }
948 };
949
950 ASSERT_NO_FATAL_FAILURE(InitState());
951 ASSERT_NO_FATAL_FAILURE(InitViewport());
952
953 XglConstantBufferObj meshBuffer(m_device, sizeof(vb_data) / sizeof(vb_data[0]), sizeof(vb_data[0]), vb_data);
Tony Barboure6152042014-12-10 17:40:15 -0700954 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Chia-I Wue09d1a72014-12-05 10:32:23 +0800955
956 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
957 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
958
959 XglPipelineObj pipelineobj(m_device);
960 pipelineobj.AddShader(&vs);
961 pipelineobj.AddShader(&ps);
962
963 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
964 sizeof(vb_data[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
965 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
966 };
967
968 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attrib;
969 vi_attrib.binding = 0; // index into vertexBindingDescriptions
970 vi_attrib.format.channelFormat = XGL_CH_FMT_R32G32; // format of source data
971 vi_attrib.format.numericFormat = XGL_NUM_FMT_FLOAT;
972 vi_attrib.offsetInBytes = 0; // Offset of first element in bytes from base of vertex
973
974 pipelineobj.AddVertexInputAttribs(&vi_attrib, 1);
975 pipelineobj.AddVertexInputBindings(&vi_binding,1);
976 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
977
978 XglDescriptorSetObj descriptorSet(m_device);
979
980 m_renderTargetCount = 2;
Tony Barboure4ed9942015-01-09 10:06:53 -0700981 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Chia-I Wue09d1a72014-12-05 10:32:23 +0800982
983 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
984 att.blendEnable = XGL_FALSE;
985 att.format = m_render_target_fmt;
986 att.channelWriteMask = 0xf;
987 pipelineobj.SetColorAttachment(1, &att);
988
Tony Barbour5ed79702015-01-07 14:31:52 -0700989 XglCommandBufferObj cmdBuffer(m_device);
Tony Barboure4ed9942015-01-09 10:06:53 -0700990
Tony Barbour5ed79702015-01-07 14:31:52 -0700991 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
992 cmdBuffer.AddRenderTarget(m_renderTargets[1]);
Tony Barboure4ed9942015-01-09 10:06:53 -0700993
Tony Barbour5ed79702015-01-07 14:31:52 -0700994 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
Tony Barbour5ed79702015-01-07 14:31:52 -0700995
Tony Barboure4ed9942015-01-09 10:06:53 -0700996 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
997
Tony Barbour5ed79702015-01-07 14:31:52 -0700998 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
Tony Barbour5ed79702015-01-07 14:31:52 -0700999#ifdef DUMP_STATE_DOT
1000 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
1001 pDSDumpDot((char*)"triTest2.dot");
1002#endif
1003 // render triangle
1004 cmdBuffer.Draw(0, 3, 0, 1);
1005
1006 // finalize recording of the command buffer
1007 cmdBuffer.EndCommandBuffer();
1008 cmdBuffer.QueueCommandBuffer(NULL, 0);
1009
1010 for (int i = 0; i < m_renderTargetCount; i++)
1011 RecordImage(m_renderTargets[i]);
1012
Chia-I Wue09d1a72014-12-05 10:32:23 +08001013}
1014
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001015TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
1016{
1017 static const char *vertShaderText =
1018 "#version 140\n"
1019 "#extension GL_ARB_separate_shader_objects : enable\n"
1020 "#extension GL_ARB_shading_language_420pack : enable\n"
1021 "layout(location = 0) in vec4 pos;\n"
1022 "layout(location = 1) in vec4 inColor;\n"
1023 "layout(location = 0) out vec4 outColor;\n"
1024 "void main() {\n"
1025 " outColor = inColor;\n"
1026 " gl_Position = pos;\n"
1027 "}\n";
1028
1029
1030 static const char *fragShaderText =
1031 "#version 140\n"
1032 "#extension GL_ARB_separate_shader_objects : enable\n"
1033 "#extension GL_ARB_shading_language_420pack : enable\n"
1034 "layout(location = 0) in vec4 color;\n"
1035 "void main() {\n"
1036 " gl_FragColor = color;\n"
1037 "}\n";
1038
1039 const Vertex g_vbData[] =
1040 {
1041 // first tri
1042 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
1043 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1044 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1045
1046 // second tri
1047 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1048 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1049 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
1050 };
1051
1052 const uint16_t g_idxData[6] = {
1053 0, 1, 2,
1054 3, 4, 5,
1055 };
1056
1057 ASSERT_NO_FATAL_FAILURE(InitState());
1058 ASSERT_NO_FATAL_FAILURE(InitViewport());
1059
1060 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001061 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001062
1063 XglIndexBufferObj indexBuffer(m_device);
1064 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
Tony Barboure6152042014-12-10 17:40:15 -07001065 indexBuffer.SetMemoryState(XGL_MEMORY_STATE_INDEX_DATA);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001066
1067 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1068 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1069
1070 XglPipelineObj pipelineobj(m_device);
1071 pipelineobj.AddShader(&vs);
1072 pipelineobj.AddShader(&ps);
1073
1074 XglDescriptorSetObj descriptorSet(m_device);
1075
1076 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1077 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1078 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1079 };
1080
1081 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1082 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1083 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1084 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1085 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1086 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1087 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1088 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1089 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1090
1091 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1092 pipelineobj.AddVertexInputBindings(&vi_binding,1);
Tony Barboure4ed9942015-01-09 10:06:53 -07001093 // pipelineobj.CreateXGLPipeline(&descriptorSet);
1094 // descriptorSet.CreateXGLDescriptorSet();
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001095
1096 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001097 XglCommandBufferObj cmdBuffer(m_device);
1098 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1099 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
Tony Barboure4ed9942015-01-09 10:06:53 -07001100
1101 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001102
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001103#ifdef DUMP_STATE_DOT
1104 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
Tobin Ehlis266473d2014-12-16 17:34:50 -07001105 pDSDumpDot((char*)"triTest2.dot");
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001106#endif
Tony Barbour02472db2015-01-08 17:08:28 -07001107
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001108 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 Barboure4ed9942015-01-09 10:06:53 -07001762 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
1763 XglCommandBufferObj cmdBuffer(m_device);
1764 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
Tony Barbourf43b6982014-11-25 13:18:32 -07001765
Tony Barboure4ed9942015-01-09 10:06:53 -07001766 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
1767 GenericDrawPreparation(&cmdBuffer, &pipelineobj, &descriptorSet);
Tony Barbourf43b6982014-11-25 13:18:32 -07001768
Tony Barboure4ed9942015-01-09 10:06:53 -07001769 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1770#ifdef DUMP_STATE_DOT
1771 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
1772 pDSDumpDot((char*)"triTest2.dot");
1773#endif
1774 // render triangle
1775 cmdBuffer.Draw(0, 36, 0, 1);
1776
1777 // finalize recording of the command buffer
1778 cmdBuffer.EndCommandBuffer();
1779 cmdBuffer.QueueCommandBuffer(NULL, 0);
1780
1781 for (int i = 0; i < m_renderTargetCount; i++)
1782 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001783}
1784
Tony Barbourf43b6982014-11-25 13:18:32 -07001785TEST_F(XglRenderTest, VSTexture)
1786{
1787 // The expected result from this test is a green and red triangle;
1788 // one red vertex on the left, two green vertices on the right.
1789 static const char *vertShaderText =
1790 "#version 130\n"
1791 "out vec4 texColor;\n"
1792 "uniform sampler2D surface;\n"
1793 "void main() {\n"
1794 " vec2 vertices[3];"
1795 " vertices[0] = vec2(-0.5, -0.5);\n"
1796 " vertices[1] = vec2( 0.5, -0.5);\n"
1797 " vertices[2] = vec2( 0.5, 0.5);\n"
1798 " vec2 positions[3];"
1799 " positions[0] = vec2( 0.0, 0.0);\n"
1800 " positions[1] = vec2( 0.25, 0.1);\n"
1801 " positions[2] = vec2( 0.1, 0.25);\n"
1802 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1803 " texColor = textureLod(surface, samplePos, 0.0);\n"
1804 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1805 "}\n";
1806
1807 static const char *fragShaderText =
1808 "#version 130\n"
1809 "in vec4 texColor;\n"
1810 "void main() {\n"
1811 " gl_FragColor = texColor;\n"
1812 "}\n";
1813
1814 ASSERT_NO_FATAL_FAILURE(InitState());
1815 ASSERT_NO_FATAL_FAILURE(InitViewport());
1816
1817 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1818 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1819 XglSamplerObj sampler(m_device);
1820 XglTextureObj texture(m_device);
1821
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001822 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001823 vs.BindShaderEntitySlotToSampler(0, &sampler);
1824
1825 XglPipelineObj pipelineobj(m_device);
1826 pipelineobj.AddShader(&vs);
1827 pipelineobj.AddShader(&ps);
1828
1829 XglDescriptorSetObj descriptorSet(m_device);
1830 descriptorSet.AttachImageView(&texture);
1831 descriptorSet.AttachSampler(&sampler);
1832
1833 m_memoryRefManager.AddMemoryRef(&texture);
1834
Tony Barboura3953b82014-12-03 15:46:29 -07001835 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001836 QueueCommandBuffer(NULL, 0);
1837
1838}
1839TEST_F(XglRenderTest, TexturedTriangle)
1840{
1841 // The expected result from this test is a red and green checkered triangle
1842 static const char *vertShaderText =
1843 "#version 140\n"
1844 "#extension GL_ARB_separate_shader_objects : enable\n"
1845 "#extension GL_ARB_shading_language_420pack : enable\n"
1846 "layout (location = 0) out vec2 samplePos;\n"
1847 "void main() {\n"
1848 " vec2 vertices[3];"
1849 " vertices[0] = vec2(-0.5, -0.5);\n"
1850 " vertices[1] = vec2( 0.5, -0.5);\n"
1851 " vertices[2] = vec2( 0.5, 0.5);\n"
1852 " vec2 positions[3];"
1853 " positions[0] = vec2( 0.0, 0.0);\n"
1854 " positions[1] = vec2( 1.0, 0.0);\n"
1855 " positions[2] = vec2( 1.0, 1.0);\n"
1856 " samplePos = positions[gl_VertexID % 3];\n"
1857 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1858 "}\n";
1859
1860 static const char *fragShaderText =
1861 "#version 140\n"
1862 "#extension GL_ARB_separate_shader_objects : enable\n"
1863 "#extension GL_ARB_shading_language_420pack : enable\n"
1864 "layout (location = 0) in vec2 samplePos;\n"
1865 "layout (binding = 0) uniform sampler2D surface;\n"
1866 "layout (location=0) out vec4 outColor;\n"
1867 "void main() {\n"
1868 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1869 " outColor = texColor;\n"
1870 "}\n";
1871
1872 ASSERT_NO_FATAL_FAILURE(InitState());
1873 ASSERT_NO_FATAL_FAILURE(InitViewport());
1874
1875 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1876 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1877 XglSamplerObj sampler(m_device);
1878 XglTextureObj texture(m_device);
1879
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001880 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001881 ps.BindShaderEntitySlotToSampler(0, &sampler);
1882
1883 XglPipelineObj pipelineobj(m_device);
1884 pipelineobj.AddShader(&vs);
1885 pipelineobj.AddShader(&ps);
1886
1887 XglDescriptorSetObj descriptorSet(m_device);
1888 descriptorSet.AttachImageView(&texture);
1889 descriptorSet.AttachSampler(&sampler);
1890
1891 m_memoryRefManager.AddMemoryRef(&texture);
1892
Tony Barboura3953b82014-12-03 15:46:29 -07001893 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001894 QueueCommandBuffer(NULL, 0);
1895}
1896TEST_F(XglRenderTest, TexturedTriangleClip)
1897{
1898 // The expected result from this test is a red and green checkered triangle
1899 static const char *vertShaderText =
1900 "#version 330\n"
1901 "#extension GL_ARB_separate_shader_objects : enable\n"
1902 "#extension GL_ARB_shading_language_420pack : enable\n"
1903 "layout (location = 0) out vec2 samplePos;\n"
1904 "out gl_PerVertex {\n"
1905 " vec4 gl_Position;\n"
1906 " float gl_ClipDistance[1];\n"
1907 "};\n"
1908 "void main() {\n"
1909 " vec2 vertices[3];"
1910 " vertices[0] = vec2(-0.5, -0.5);\n"
1911 " vertices[1] = vec2( 0.5, -0.5);\n"
1912 " vertices[2] = vec2( 0.5, 0.5);\n"
1913 " vec2 positions[3];"
1914 " positions[0] = vec2( 0.0, 0.0);\n"
1915 " positions[1] = vec2( 1.0, 0.0);\n"
1916 " positions[2] = vec2( 1.0, 1.0);\n"
1917 " float dists[3];\n"
1918 " dists[0] = 1.0;\n"
1919 " dists[1] = 1.0;\n"
1920 " dists[2] = -1.0;\n"
1921 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1922 " samplePos = positions[gl_VertexID % 3];\n"
1923 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1924 "}\n";
1925
1926 static const char *fragShaderText =
1927 "#version 140\n"
1928 "#extension GL_ARB_separate_shader_objects : enable\n"
1929 "#extension GL_ARB_shading_language_420pack : enable\n"
1930 "layout (location = 0) in vec2 samplePos;\n"
1931 "layout (binding = 0) uniform sampler2D surface;\n"
1932 "layout (location=0) out vec4 outColor;\n"
1933 "void main() {\n"
1934 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1935 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1936 " outColor = texColor;\n"
1937 "}\n";
1938
1939
1940 ASSERT_NO_FATAL_FAILURE(InitState());
1941 ASSERT_NO_FATAL_FAILURE(InitViewport());
1942
1943 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1944 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1945 XglSamplerObj sampler(m_device);
1946 XglTextureObj texture(m_device);
1947
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001948 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001949 ps.BindShaderEntitySlotToSampler(0, &sampler);
1950
1951 XglPipelineObj pipelineobj(m_device);
1952 pipelineobj.AddShader(&vs);
1953 pipelineobj.AddShader(&ps);
1954
1955 XglDescriptorSetObj descriptorSet(m_device);
1956 descriptorSet.AttachImageView(&texture);
1957 descriptorSet.AttachSampler(&sampler);
1958
1959 m_memoryRefManager.AddMemoryRef(&texture);
1960
Tony Barboura3953b82014-12-03 15:46:29 -07001961 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001962 QueueCommandBuffer(NULL, 0);
1963}
1964TEST_F(XglRenderTest, FSTriangle)
1965{
1966 // The expected result from this test is a red and green checkered triangle
1967 static const char *vertShaderText =
1968 "#version 140\n"
1969 "#extension GL_ARB_separate_shader_objects : enable\n"
1970 "#extension GL_ARB_shading_language_420pack : enable\n"
1971 "layout (location = 0) out vec2 samplePos;\n"
1972 "void main() {\n"
1973 " vec2 vertices[3];"
1974 " vertices[0] = vec2(-0.5, -0.5);\n"
1975 " vertices[1] = vec2( 0.5, -0.5);\n"
1976 " vertices[2] = vec2( 0.5, 0.5);\n"
1977 " vec2 positions[3];"
1978 " positions[0] = vec2( 0.0, 0.0);\n"
1979 " positions[1] = vec2( 1.0, 0.0);\n"
1980 " positions[2] = vec2( 1.0, 1.0);\n"
1981 " samplePos = positions[gl_VertexID % 3];\n"
1982 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1983 "}\n";
1984
1985 static const char *fragShaderText =
1986 "#version 140\n"
1987 "#extension GL_ARB_separate_shader_objects : enable\n"
1988 "#extension GL_ARB_shading_language_420pack : enable\n"
1989 "layout (location = 0) in vec2 samplePos;\n"
1990 "layout (binding = 0) uniform sampler2D surface;\n"
1991 "layout (location=0) out vec4 outColor;\n"
1992 "void main() {\n"
1993 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1994 " outColor = texColor;\n"
1995 "}\n";
1996
1997 ASSERT_NO_FATAL_FAILURE(InitState());
1998 ASSERT_NO_FATAL_FAILURE(InitViewport());
1999
2000 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2001 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2002 XglSamplerObj sampler(m_device);
2003 XglTextureObj texture(m_device);
2004
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002005 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002006 ps.BindShaderEntitySlotToSampler(0, &sampler);
2007
2008 XglPipelineObj pipelineobj(m_device);
2009 pipelineobj.AddShader(&vs);
2010 pipelineobj.AddShader(&ps);
2011
2012 XglDescriptorSetObj descriptorSet(m_device);
2013 descriptorSet.AttachImageView(&texture);
2014 descriptorSet.AttachSampler(&sampler);
2015
2016 m_memoryRefManager.AddMemoryRef(&texture);
2017
Tony Barboura3953b82014-12-03 15:46:29 -07002018 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002019 QueueCommandBuffer(NULL, 0);
2020}
2021TEST_F(XglRenderTest, SamplerBindingsTriangle)
2022{
2023 // This test sets bindings on the samplers
2024 // For now we are asserting that sampler and texture pairs
2025 // march in lock step, and are set via GLSL binding. This can
2026 // and will probably change.
2027 // The sampler bindings should match the sampler and texture slot
2028 // number set up by the application.
2029 // This test will result in a blue triangle
2030 static const char *vertShaderText =
2031 "#version 140\n"
2032 "#extension GL_ARB_separate_shader_objects : enable\n"
2033 "#extension GL_ARB_shading_language_420pack : enable\n"
2034 "layout (location = 0) out vec4 samplePos;\n"
2035 "void main() {\n"
2036 " vec2 vertices[3];"
2037 " vertices[0] = vec2(-0.5, -0.5);\n"
2038 " vertices[1] = vec2( 0.5, -0.5);\n"
2039 " vertices[2] = vec2( 0.5, 0.5);\n"
2040 " vec2 positions[3];"
2041 " positions[0] = vec2( 0.0, 0.0);\n"
2042 " positions[1] = vec2( 1.0, 0.0);\n"
2043 " positions[2] = vec2( 1.0, 1.0);\n"
2044 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
2045 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2046 "}\n";
2047
2048 static const char *fragShaderText =
2049 "#version 140\n"
2050 "#extension GL_ARB_separate_shader_objects : enable\n"
2051 "#extension GL_ARB_shading_language_420pack : enable\n"
2052 "layout (location = 0) in vec4 samplePos;\n"
2053 "layout (binding = 0) uniform sampler2D surface0;\n"
2054 "layout (binding = 1) uniform sampler2D surface1;\n"
2055 "layout (binding = 12) uniform sampler2D surface2;\n"
2056 "void main() {\n"
2057 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
2058 "}\n";
2059
2060 ASSERT_NO_FATAL_FAILURE(InitState());
2061 ASSERT_NO_FATAL_FAILURE(InitViewport());
2062
2063 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2064 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2065
2066 XglSamplerObj sampler1(m_device);
2067 XglSamplerObj sampler2(m_device);
2068 XglSamplerObj sampler3(m_device);
2069
2070 XglTextureObj texture1(m_device); // Red
2071 texture1.ChangeColors(0xffff0000,0xffff0000);
2072 XglTextureObj texture2(m_device); // Green
2073 texture2.ChangeColors(0xff00ff00,0xff00ff00);
2074 XglTextureObj texture3(m_device); // Blue
2075 texture3.ChangeColors(0xff0000ff,0xff0000ff);
2076
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002077 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002078 ps.BindShaderEntitySlotToSampler(0, &sampler1);
2079
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002080 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Tony Barbourf43b6982014-11-25 13:18:32 -07002081 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2082
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002083 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture3);
Tony Barbourf43b6982014-11-25 13:18:32 -07002084 ps.BindShaderEntitySlotToSampler(12, &sampler3);
2085
2086 XglPipelineObj pipelineobj(m_device);
2087 pipelineobj.AddShader(&vs);
2088 pipelineobj.AddShader(&ps);
2089
2090 XglDescriptorSetObj descriptorSet(m_device);
2091 descriptorSet.AttachImageView(&texture1);
2092 descriptorSet.AttachSampler(&sampler1);
2093 descriptorSet.AttachImageView(&texture2);
2094 descriptorSet.AttachSampler(&sampler2);
2095 descriptorSet.AttachImageView(&texture3);
2096 descriptorSet.AttachSampler(&sampler3);
2097
2098 m_memoryRefManager.AddMemoryRef(&texture1);
2099 m_memoryRefManager.AddMemoryRef(&texture2);
2100 m_memoryRefManager.AddMemoryRef(&texture3);
2101
Tony Barboura3953b82014-12-03 15:46:29 -07002102 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002103 QueueCommandBuffer(NULL, 0);
2104
2105}
2106
2107TEST_F(XglRenderTest, TriangleVSUniformBlock)
2108{
2109 // The expected result from this test is a blue triangle
2110
2111 static const char *vertShaderText =
2112 "#version 140\n"
2113 "#extension GL_ARB_separate_shader_objects : enable\n"
2114 "#extension GL_ARB_shading_language_420pack : enable\n"
2115 "layout (location = 0) out vec4 outColor;\n"
2116 "layout (std140, binding = 0) uniform bufferVals {\n"
2117 " vec4 red;\n"
2118 " vec4 green;\n"
2119 " vec4 blue;\n"
2120 " vec4 white;\n"
2121 "} myBufferVals;\n"
2122 "void main() {\n"
2123 " vec2 vertices[3];"
2124 " vertices[0] = vec2(-0.5, -0.5);\n"
2125 " vertices[1] = vec2( 0.5, -0.5);\n"
2126 " vertices[2] = vec2( 0.5, 0.5);\n"
2127 " outColor = myBufferVals.blue;\n"
2128 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2129 "}\n";
2130
2131 static const char *fragShaderText =
2132 "#version 140\n"
2133 "#extension GL_ARB_separate_shader_objects : enable\n"
2134 "#extension GL_ARB_shading_language_420pack : enable\n"
2135 "layout (location = 0) in vec4 inColor;\n"
2136 "void main() {\n"
2137 " gl_FragColor = inColor;\n"
2138 "}\n";
2139
2140 ASSERT_NO_FATAL_FAILURE(InitState());
2141 ASSERT_NO_FATAL_FAILURE(InitViewport());
2142
2143 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2144 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2145
2146 // Let's populate our buffer with the following:
2147 // vec4 red;
2148 // vec4 green;
2149 // vec4 blue;
2150 // vec4 white;
2151 const int valCount = 4 * 4;
2152 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2153 0.0, 1.0, 0.0, 1.0,
2154 0.0, 0.0, 1.0, 1.0,
2155 1.0, 1.0, 1.0, 1.0 };
2156
2157 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2158 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2159
2160 XglPipelineObj pipelineobj(m_device);
2161 pipelineobj.AddShader(&vs);
2162 pipelineobj.AddShader(&ps);
2163
2164 XglDescriptorSetObj descriptorSet(m_device);
2165 descriptorSet.AttachMemoryView(&colorBuffer);
2166
Tony Barboura3953b82014-12-03 15:46:29 -07002167 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002168 QueueCommandBuffer(NULL, 0);
2169
2170}
2171
2172TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2173{
2174 // This test allows the shader to select which buffer it is
2175 // pulling from using layout binding qualifier.
2176 // There are corresponding changes in the compiler stack that
2177 // will select the buffer using binding directly.
2178 // The binding number should match the slot number set up by
2179 // the application.
2180 // The expected result from this test is a purple triangle
2181
2182 static const char *vertShaderText =
2183 "#version 140\n"
2184 "#extension GL_ARB_separate_shader_objects : enable\n"
2185 "#extension GL_ARB_shading_language_420pack : enable\n"
2186 "void main() {\n"
2187 " vec2 vertices[3];"
2188 " vertices[0] = vec2(-0.5, -0.5);\n"
2189 " vertices[1] = vec2( 0.5, -0.5);\n"
2190 " vertices[2] = vec2( 0.5, 0.5);\n"
2191 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2192 "}\n";
2193
2194 static const char *fragShaderText =
2195 "#version 140\n"
2196 "#extension GL_ARB_separate_shader_objects : enable\n"
2197 "#extension GL_ARB_shading_language_420pack : enable\n"
2198 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2199 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2200 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2201 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2202 "void main() {\n"
2203 " gl_FragColor = myBlueVal.color;\n"
2204 " gl_FragColor += myRedVal.color;\n"
2205 "}\n";
2206
2207 ASSERT_NO_FATAL_FAILURE(InitState());
2208 ASSERT_NO_FATAL_FAILURE(InitViewport());
2209
2210 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2211 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2212
2213 // We're going to create a number of uniform buffers, and then allow
2214 // the shader to select which it wants to read from with a binding
2215
2216 // Let's populate the buffers with a single color each:
2217 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2218 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2219 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2220 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2221
2222 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2223 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2224 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2225 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2226
2227 const int redCount = sizeof(redVals) / sizeof(float);
2228 const int greenCount = sizeof(greenVals) / sizeof(float);
2229 const int blueCount = sizeof(blueVals) / sizeof(float);
2230 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2231
2232 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2233 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2234
2235 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2236 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2237
2238 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2239 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2240
2241 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2242 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2243
2244 XglPipelineObj pipelineobj(m_device);
2245 pipelineobj.AddShader(&vs);
2246 pipelineobj.AddShader(&ps);
2247
2248 XglDescriptorSetObj descriptorSet(m_device);
2249 descriptorSet.AttachMemoryView(&redBuffer);
2250 descriptorSet.AttachMemoryView(&greenBuffer);
2251 descriptorSet.AttachMemoryView(&blueBuffer);
2252 descriptorSet.AttachMemoryView(&whiteBuffer);
2253
Tony Barboura3953b82014-12-03 15:46:29 -07002254 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002255 QueueCommandBuffer(NULL, 0);
2256
2257}
2258
2259TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2260{
2261 // This test is the same as TriangleFSUniformBlockBinding, but
2262 // it does not provide an instance name.
2263 // The expected result from this test is a purple triangle
2264
2265 static const char *vertShaderText =
2266 "#version 140\n"
2267 "#extension GL_ARB_separate_shader_objects : enable\n"
2268 "#extension GL_ARB_shading_language_420pack : enable\n"
2269 "void main() {\n"
2270 " vec2 vertices[3];"
2271 " vertices[0] = vec2(-0.5, -0.5);\n"
2272 " vertices[1] = vec2( 0.5, -0.5);\n"
2273 " vertices[2] = vec2( 0.5, 0.5);\n"
2274 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2275 "}\n";
2276
2277 static const char *fragShaderText =
2278 "#version 430\n"
2279 "#extension GL_ARB_separate_shader_objects : enable\n"
2280 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002281 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2282 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2283 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2284 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
Cody Northrop68a10f62014-12-05 15:44:14 -07002285 "layout (location = 0) out vec4 outColor;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002286 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -07002287 " outColor = blue;\n"
2288 " outColor += red;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002289 "}\n";
2290 ASSERT_NO_FATAL_FAILURE(InitState());
2291 ASSERT_NO_FATAL_FAILURE(InitViewport());
2292
2293 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2294 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2295
2296 // We're going to create a number of uniform buffers, and then allow
2297 // the shader to select which it wants to read from with a binding
2298
2299 // Let's populate the buffers with a single color each:
2300 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2301 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2302 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2303 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2304
2305 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2306 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2307 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2308 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2309
2310 const int redCount = sizeof(redVals) / sizeof(float);
2311 const int greenCount = sizeof(greenVals) / sizeof(float);
2312 const int blueCount = sizeof(blueVals) / sizeof(float);
2313 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2314
2315 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2316 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2317
2318 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2319 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2320
2321 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2322 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2323
2324 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002325 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07002326
2327 XglPipelineObj pipelineobj(m_device);
2328 pipelineobj.AddShader(&vs);
2329 pipelineobj.AddShader(&ps);
2330
2331 XglDescriptorSetObj descriptorSet(m_device);
2332 descriptorSet.AttachMemoryView(&redBuffer);
2333 descriptorSet.AttachMemoryView(&greenBuffer);
2334 descriptorSet.AttachMemoryView(&blueBuffer);
2335 descriptorSet.AttachMemoryView(&whiteBuffer);
2336
Tony Barboura3953b82014-12-03 15:46:29 -07002337 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002338 QueueCommandBuffer(NULL, 0);
2339
2340}
2341
2342TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2343{
2344 static const char *vertShaderText =
2345 "#version 140\n"
2346 "#extension GL_ARB_separate_shader_objects : enable\n"
2347 "#extension GL_ARB_shading_language_420pack : enable\n"
2348 "layout (std140, binding=0) uniform bufferVals {\n"
2349 " mat4 mvp;\n"
2350 "} myBufferVals;\n"
2351 "layout (location=0) in vec4 pos;\n"
2352 "layout (location=0) out vec2 UV;\n"
2353 "void main() {\n"
2354 " vec2 positions[3];"
2355 " positions[0] = vec2( 0.0, 0.0);\n"
2356 " positions[1] = vec2( 0.25, 0.1);\n"
2357 " positions[2] = vec2( 0.1, 0.25);\n"
2358 " UV = positions[gl_VertexID % 3];\n"
2359 " gl_Position = myBufferVals.mvp * pos;\n"
2360 "}\n";
2361
2362 static const char *fragShaderText =
2363 "#version 140\n"
2364 "#extension GL_ARB_separate_shader_objects : enable\n"
2365 "#extension GL_ARB_shading_language_420pack : enable\n"
2366 "layout (binding=0) uniform sampler2D surface;\n"
2367 "layout (location=0) out vec4 outColor;\n"
2368 "layout (location=0) in vec2 UV;\n"
2369 "void main() {\n"
2370 " outColor= textureLod(surface, UV, 0.0);\n"
2371 "}\n";
2372 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2373
2374 glm::mat4 View = glm::lookAt(
2375 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2376 glm::vec3(0,0,0), // and looks at the origin
2377 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2378 );
2379
2380 glm::mat4 Model = glm::mat4(1.0f);
2381
2382 glm::mat4 MVP = Projection * View * Model;
2383
2384
2385 ASSERT_NO_FATAL_FAILURE(InitState());
2386 ASSERT_NO_FATAL_FAILURE(InitViewport());
2387 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2388
2389 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2390 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
Tony Barboure6152042014-12-10 17:40:15 -07002391 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07002392
2393
2394 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2395
2396 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2397 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2398 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2399 XglSamplerObj sampler(m_device);
2400 XglTextureObj texture(m_device);
2401
2402 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2403 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002404 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002405 ps.BindShaderEntitySlotToSampler(0, &sampler);
2406
2407 XglPipelineObj pipelineobj(m_device);
2408 pipelineobj.AddShader(&vs);
2409 pipelineobj.AddShader(&ps);
2410
2411 XglDescriptorSetObj descriptorSet(m_device);
2412
2413 descriptorSet.AttachMemoryView(&mvpBuffer);
2414 descriptorSet.AttachImageView(&texture);
2415 descriptorSet.AttachSampler(&sampler);
2416
2417 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2418 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2419 m_memoryRefManager.AddMemoryRef(&texture);
2420
2421 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2422 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2423 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2424 };
2425
2426 // this is the current description of g_vbData
2427 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2428 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2429 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2430 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2431 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2432 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2433 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2434 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2435 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2436
2437 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2438 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2439 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2440
Tony Barboura3953b82014-12-03 15:46:29 -07002441 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07002442
2443 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2444
2445}
Cody Northropd1ce7842014-12-09 11:17:01 -07002446
2447TEST_F(XglRenderTest, TriangleMixedSamplerUniformBlockBinding)
2448{
2449 // This test mixes binding slots of textures and buffers, ensuring
2450 // that sparse and overlapping assignments work.
Cody Northropa0410942014-12-09 13:59:39 -07002451 // The expected result from this test is a purple triangle, although
Cody Northropd1ce7842014-12-09 11:17:01 -07002452 // you can modify it to move the desired result around.
2453
2454 static const char *vertShaderText =
2455 "#version 140\n"
2456 "#extension GL_ARB_separate_shader_objects : enable\n"
2457 "#extension GL_ARB_shading_language_420pack : enable\n"
2458 "void main() {\n"
2459 " vec2 vertices[3];"
2460 " vertices[0] = vec2(-0.5, -0.5);\n"
2461 " vertices[1] = vec2( 0.5, -0.5);\n"
2462 " vertices[2] = vec2( 0.5, 0.5);\n"
2463 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2464 "}\n";
2465
2466 static const char *fragShaderText =
2467 "#version 430\n"
2468 "#extension GL_ARB_separate_shader_objects : enable\n"
2469 "#extension GL_ARB_shading_language_420pack : enable\n"
2470 "layout (binding = 0) uniform sampler2D surface0;\n"
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002471 "layout (binding = 9) uniform sampler2D surface1;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002472 "layout (binding = 2) uniform sampler2D surface2;\n"
Cody Northropa0410942014-12-09 13:59:39 -07002473 "layout (binding = 4) uniform sampler2D surface3;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002474
Cody Northropa0410942014-12-09 13:59:39 -07002475
2476 "layout (std140, binding = 10) uniform redVal { vec4 red; };"
2477 "layout (std140, binding = 15) uniform greenVal { vec4 green; };"
2478 "layout (std140, binding = 13) uniform blueVal { vec4 blue; };"
2479 "layout (std140, binding = 17) uniform whiteVal { vec4 white; };"
Cody Northropd1ce7842014-12-09 11:17:01 -07002480 "layout (location = 0) out vec4 outColor;\n"
2481 "void main() {\n"
Cody Northropa0410942014-12-09 13:59:39 -07002482 " outColor = red * vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002483 " outColor += white * vec4(0.00001);\n"
2484 " outColor += textureLod(surface2, vec2(0.5), 0.0)* vec4(0.00001);\n"
Cody Northropa0410942014-12-09 13:59:39 -07002485 " outColor += textureLod(surface1, vec2(0.0), 0.0);//* vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002486 "}\n";
2487 ASSERT_NO_FATAL_FAILURE(InitState());
2488 ASSERT_NO_FATAL_FAILURE(InitViewport());
2489
2490 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2491 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2492
Cody Northropd1ce7842014-12-09 11:17:01 -07002493 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2494 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2495 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2496 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2497
2498 const int redCount = sizeof(redVals) / sizeof(float);
2499 const int greenCount = sizeof(greenVals) / sizeof(float);
2500 const int blueCount = sizeof(blueVals) / sizeof(float);
2501 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2502
2503 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Cody Northropa0410942014-12-09 13:59:39 -07002504 ps.BindShaderEntitySlotToMemory(10, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002505
2506 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Cody Northropa0410942014-12-09 13:59:39 -07002507 ps.BindShaderEntitySlotToMemory(15, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002508
2509 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Cody Northropa0410942014-12-09 13:59:39 -07002510 ps.BindShaderEntitySlotToMemory(13, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002511
2512 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropa0410942014-12-09 13:59:39 -07002513 ps.BindShaderEntitySlotToMemory(17, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002514
2515 XglSamplerObj sampler0(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002516 XglTextureObj texture0(m_device); // Light Red
2517 texture0.ChangeColors(0xff800000,0xff800000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002518 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
Cody Northropd1ce7842014-12-09 11:17:01 -07002519 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2520 XglSamplerObj sampler2(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002521 XglTextureObj texture2(m_device); // Light Blue
2522 texture2.ChangeColors(0xff000080,0xff000080);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002523 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Cody Northropd1ce7842014-12-09 11:17:01 -07002524 ps.BindShaderEntitySlotToSampler(2, &sampler2);
2525 XglSamplerObj sampler4(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002526 XglTextureObj texture4(m_device); // Light Green
2527 texture4.ChangeColors(0xff008000,0xff008000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002528 ps.BindShaderEntitySlotToImage(4, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
Cody Northropd1ce7842014-12-09 11:17:01 -07002529 ps.BindShaderEntitySlotToSampler(4, &sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002530
2531 // NOTE: Bindings 1,3,5,7,8,9,11,12,14,16 work for this sampler, but 6 does not!!!
2532 // TODO: Get back here ASAP and understand why.
2533 XglSamplerObj sampler7(m_device);
2534 XglTextureObj texture7(m_device); // Red and Blue
2535 texture7.ChangeColors(0xffff00ff,0xffff00ff);
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002536 ps.BindShaderEntitySlotToImage(9, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2537 ps.BindShaderEntitySlotToSampler(9, &sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002538
2539
2540 XglPipelineObj pipelineobj(m_device);
2541 pipelineobj.AddShader(&vs);
2542 pipelineobj.AddShader(&ps);
2543
2544 XglDescriptorSetObj descriptorSet(m_device);
2545 descriptorSet.AttachMemoryView(&redBuffer);
2546 descriptorSet.AttachMemoryView(&greenBuffer);
2547 descriptorSet.AttachMemoryView(&blueBuffer);
2548 descriptorSet.AttachMemoryView(&whiteBuffer);
2549 descriptorSet.AttachImageView(&texture0);
2550 descriptorSet.AttachSampler(&sampler0);
2551 descriptorSet.AttachImageView(&texture2);
2552 descriptorSet.AttachSampler(&sampler2);
2553 descriptorSet.AttachImageView(&texture4);
2554 descriptorSet.AttachSampler(&sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002555 descriptorSet.AttachImageView(&texture7);
2556 descriptorSet.AttachSampler(&sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002557
2558 m_memoryRefManager.AddMemoryRef(&texture0);
2559 m_memoryRefManager.AddMemoryRef(&texture2);
2560 m_memoryRefManager.AddMemoryRef(&texture4);
Cody Northropa0410942014-12-09 13:59:39 -07002561 m_memoryRefManager.AddMemoryRef(&texture7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002562
2563
2564 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2565 QueueCommandBuffer(NULL, 0);
2566
2567}
2568
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002569TEST_F(XglRenderTest, TriangleMatchingSamplerUniformBlockBinding)
2570{
2571 // This test matches binding slots of textures and buffers, requiring
2572 // the driver to give them distinct number spaces.
2573 // The expected result from this test is a red triangle, although
2574 // you can modify it to move the desired result around.
2575
2576 static const char *vertShaderText =
2577 "#version 140\n"
2578 "#extension GL_ARB_separate_shader_objects : enable\n"
2579 "#extension GL_ARB_shading_language_420pack : enable\n"
2580 "void main() {\n"
2581 " vec2 vertices[3];"
2582 " vertices[0] = vec2(-0.5, -0.5);\n"
2583 " vertices[1] = vec2( 0.5, -0.5);\n"
2584 " vertices[2] = vec2( 0.5, 0.5);\n"
2585 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2586 "}\n";
2587
2588 static const char *fragShaderText =
2589 "#version 430\n"
2590 "#extension GL_ARB_separate_shader_objects : enable\n"
2591 "#extension GL_ARB_shading_language_420pack : enable\n"
2592 "layout (binding = 0) uniform sampler2D surface0;\n"
2593 "layout (binding = 1) uniform sampler2D surface1;\n"
2594 "layout (binding = 2) uniform sampler2D surface2;\n"
2595 "layout (binding = 3) uniform sampler2D surface3;\n"
2596 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2597 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2598 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2599 "layout (std140, binding = 3) uniform whiteVal { vec4 white; };"
2600 "layout (location = 0) out vec4 outColor;\n"
2601 "void main() {\n"
2602 " outColor = red;// * vec4(0.00001);\n"
2603 " outColor += white * vec4(0.00001);\n"
2604 " outColor += textureLod(surface1, vec2(0.5), 0.0)* vec4(0.00001);\n"
2605 " outColor += textureLod(surface3, vec2(0.0), 0.0)* vec4(0.00001);\n"
2606 "}\n";
2607 ASSERT_NO_FATAL_FAILURE(InitState());
2608 ASSERT_NO_FATAL_FAILURE(InitViewport());
2609
2610 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2611 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2612
2613 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2614 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2615 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2616 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2617
2618 const int redCount = sizeof(redVals) / sizeof(float);
2619 const int greenCount = sizeof(greenVals) / sizeof(float);
2620 const int blueCount = sizeof(blueVals) / sizeof(float);
2621 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2622
2623 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2624 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2625
2626 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2627 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2628
2629 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2630 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2631
2632 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2633 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2634
2635 XglSamplerObj sampler0(m_device);
2636 XglTextureObj texture0(m_device); // Light Red
2637 texture0.ChangeColors(0xff800000,0xff800000);
2638 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
2639 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2640 XglSamplerObj sampler2(m_device);
2641 XglTextureObj texture2(m_device); // Light Blue
2642 texture2.ChangeColors(0xff000080,0xff000080);
2643 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
2644 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2645 XglSamplerObj sampler4(m_device);
2646 XglTextureObj texture4(m_device); // Light Green
2647 texture4.ChangeColors(0xff008000,0xff008000);
2648 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
2649 ps.BindShaderEntitySlotToSampler(2, &sampler4);
2650 XglSamplerObj sampler7(m_device);
2651 XglTextureObj texture7(m_device); // Red and Blue
2652 texture7.ChangeColors(0xffff00ff,0xffff00ff);
2653 ps.BindShaderEntitySlotToImage(3, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2654 ps.BindShaderEntitySlotToSampler(3, &sampler7);
2655
2656
2657 XglPipelineObj pipelineobj(m_device);
2658 pipelineobj.AddShader(&vs);
2659 pipelineobj.AddShader(&ps);
2660
2661 XglDescriptorSetObj descriptorSet(m_device);
2662 descriptorSet.AttachMemoryView(&redBuffer);
2663 descriptorSet.AttachMemoryView(&greenBuffer);
2664 descriptorSet.AttachMemoryView(&blueBuffer);
2665 descriptorSet.AttachMemoryView(&whiteBuffer);
2666 descriptorSet.AttachImageView(&texture0);
2667 descriptorSet.AttachSampler(&sampler0);
2668 descriptorSet.AttachImageView(&texture2);
2669 descriptorSet.AttachSampler(&sampler2);
2670 descriptorSet.AttachImageView(&texture4);
2671 descriptorSet.AttachSampler(&sampler4);
2672 descriptorSet.AttachImageView(&texture7);
2673 descriptorSet.AttachSampler(&sampler7);
2674
2675 m_memoryRefManager.AddMemoryRef(&texture0);
2676 m_memoryRefManager.AddMemoryRef(&texture2);
2677 m_memoryRefManager.AddMemoryRef(&texture4);
2678 m_memoryRefManager.AddMemoryRef(&texture7);
2679
2680
2681 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2682 QueueCommandBuffer(NULL, 0);
2683
2684}
2685
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002686int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002687 int result;
2688
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002689 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002690 XglTestFramework::InitArgs(&argc, argv);
2691
Chia-I Wu7133fdc2014-12-15 23:57:34 +08002692 ::testing::AddGlobalTestEnvironment(new TestEnvironment);
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002693
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002694 result = RUN_ALL_TESTS();
2695
2696 XglTestFramework::Finish();
2697 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002698}