blob: 1bd781fa3495c8cf0932e5966a07a6d676747ee9 [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 Barbourf43b6982014-11-25 13:18:32 -0700220 void QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs);
221
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600222 void InitDepthStencil();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600223 void GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600224 void XGLTriangleTest(const char *vertShaderText, const char *fragShaderText);
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600225
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600226
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600227protected:
Cody Northrop350727b2014-10-06 15:42:00 -0600228 XGL_IMAGE m_texture;
229 XGL_IMAGE_VIEW m_textureView;
230 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
231 XGL_GPU_MEMORY m_textureMem;
232
233 XGL_SAMPLER m_sampler;
234
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600235 XGL_FORMAT m_depth_stencil_fmt;
236 XGL_IMAGE m_depthStencilImage;
237 XGL_GPU_MEMORY m_depthStencilMem;
238 XGL_DEPTH_STENCIL_VIEW m_depthStencilView;
Tony Barbourf43b6982014-11-25 13:18:32 -0700239 XglMemoryRefManager m_memoryRefManager;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600240
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600241
242 virtual void SetUp() {
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600243
244 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
245 this->app_info.pNext = NULL;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800246 this->app_info.pAppName = "render_tests";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600247 this->app_info.appVersion = 1;
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800248 this->app_info.pEngineName = "unittest";
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600249 this->app_info.engineVersion = 1;
250 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
251
Cody Northrop350727b2014-10-06 15:42:00 -0600252 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
253 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Tony Barbour97a36232014-12-04 17:14:45 -0700254 memset(&m_depthStencilImage, 0, sizeof(m_depthStencilImage));
Cody Northrop350727b2014-10-06 15:42:00 -0600255
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600256 InitFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600257 }
258
259 virtual void TearDown() {
Courtney Goeltzenleuchtercb5a89c2014-10-08 12:20:26 -0600260 // Clean up resources before we reset
261 ShutdownFramework();
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -0600262 }
263};
264
Tony Barbourf43b6982014-11-25 13:18:32 -0700265
Tony Barboura3953b82014-12-03 15:46:29 -0700266void XglRenderTest::GenericDrawTriangleTest(XglPipelineObj *pipelineobj, XglDescriptorSetObj *descriptorSet,int numTris)
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600267{
268 XGL_RESULT err = XGL_SUCCESS;
269
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600270 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600271 // Build command buffer
272 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
273 ASSERT_XGL_SUCCESS(err);
274
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600275 GenerateClearAndPrepareBufferCmds();
276 GenerateBindRenderTargetCmd();
Tobin Ehlis266473d2014-12-16 17:34:50 -0700277
Tony Barbourf43b6982014-11-25 13:18:32 -0700278 GenerateBindStateAndPipelineCmds();
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600279
Tony Barboura3953b82014-12-03 15:46:29 -0700280 pipelineobj->BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
Tony Barbourbf678472014-12-03 13:58:15 -0700281 descriptorSet->BindCommandBuffer(m_cmdBuffer);
Tobin Ehlis266473d2014-12-16 17:34:50 -0700282#ifdef DUMP_STATE_DOT
283 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
284 pDSDumpDot((char*)"triTest.dot");
285 DRAW_STATE_DUMP_PNG_FILE pDSDumpPng = (DRAW_STATE_DUMP_PNG_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpPngFile");
286 pDSDumpPng((char*)"triTest.png");
287#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700288 // render the triangle
289 xglCmdDraw( m_cmdBuffer, 0, 3*numTris, 0, 1 );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600290
291 // finalize recording of the command buffer
292 err = xglEndCommandBuffer( m_cmdBuffer );
293 ASSERT_XGL_SUCCESS( err );
Tony Barbourf43b6982014-11-25 13:18:32 -0700294}
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600295
Tony Barbourf43b6982014-11-25 13:18:32 -0700296void XglRenderTest::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
297{
298 XGL_RESULT err = XGL_SUCCESS;
Chia-I Wue7748802014-12-05 10:45:15 +0800299 XGL_UINT i;
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600300
301 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700302 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, numMemRefs, memRefs, NULL );
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600303 ASSERT_XGL_SUCCESS( err );
304
Chia-I Wuf34ac502014-08-27 14:58:05 +0800305 err = xglQueueWaitIdle( m_device->m_queue );
306 ASSERT_XGL_SUCCESS( err );
307
Courtney Goeltzenleuchter68cfe612014-08-26 18:16:41 -0600308 // Wait for work to finish before cleaning up.
309 xglDeviceWaitIdle(m_device->device());
310
Chia-I Wue7748802014-12-05 10:45:15 +0800311 for (i = 0; i < m_renderTargetCount; i++)
312 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchtera948d842014-08-22 16:27:58 -0600313}
314
Tony Barbourf43b6982014-11-25 13:18:32 -0700315void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600316{
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600317 XGL_RESULT err;
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600318
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600319 ASSERT_NO_FATAL_FAILURE(InitState());
320 ASSERT_NO_FATAL_FAILURE(InitViewport());
321
Tony Barbourf43b6982014-11-25 13:18:32 -0700322 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
323 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600324
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600325
Tony Barbourf43b6982014-11-25 13:18:32 -0700326 XglPipelineObj pipelineobj(m_device);
327 pipelineobj.AddShader(&vs);
328 pipelineobj.AddShader(&ps);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600329
330 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
331
Tony Barbourf43b6982014-11-25 13:18:32 -0700332 // Create descriptor set
333 XglDescriptorSetObj descriptorSet(m_device);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600334
335 // Build command buffer
336 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
337 ASSERT_XGL_SUCCESS(err);
338
339 GenerateClearAndPrepareBufferCmds();
340 GenerateBindRenderTargetCmd();
Tony Barbourf43b6982014-11-25 13:18:32 -0700341 GenerateBindStateAndPipelineCmds();
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600342
Tobin Ehlis266473d2014-12-16 17:34:50 -0700343 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,&descriptorSet);
344 descriptorSet.BindCommandBuffer(m_cmdBuffer);
Tobin Ehlis31446e52014-11-28 11:17:19 -0700345#ifdef DUMP_STATE_DOT
346 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
347 pDSDumpDot((char*)"triUniFS.dot");
348#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700349 // render the triangle
350 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600351
352 // finalize recording of the command buffer
353 err = xglEndCommandBuffer( m_cmdBuffer );
354 ASSERT_XGL_SUCCESS( err );
355
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600356 // submit the command buffer to the universal queue
Tony Barbour75420772014-12-04 17:17:26 -0700357 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, 0, m_memRefs, NULL );
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600358 ASSERT_XGL_SUCCESS( err );
359
360 err = xglQueueWaitIdle( m_device->m_queue );
361 ASSERT_XGL_SUCCESS( err );
362
363 // Wait for work to finish before cleaning up.
364 xglDeviceWaitIdle(m_device->device());
365
Chia-I Wue7748802014-12-05 10:45:15 +0800366 assert(m_renderTargetCount == 1);
367 RecordImage(m_renderTargets[0]);
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600368
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600369}
370
Tony Barbourf43b6982014-11-25 13:18:32 -0700371void XglRenderTest::RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
Tony Barbour09da2212014-12-03 16:13:23 -0700372 XglConstantBufferObj *constantBuffer)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600373{
374 int i;
375 glm::mat4 MVP;
376 int matrixSize = sizeof(MVP);
377 XGL_RESULT err;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600378
379 for (i = 0; i < 8; i++) {
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800380 void *pData = constantBuffer->map();
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600381
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600382 Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
383 MVP = Projection * View * Model;
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600384 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
385
Chia-I Wu67ee00f2014-12-28 15:26:08 +0800386 constantBuffer->unmap();
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600387
388 // submit the command buffer to the universal queue
Tony Barbourf43b6982014-11-25 13:18:32 -0700389 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_memoryRefManager.GetNumRefs(), m_memoryRefManager.GetMemoryRefList(), NULL );
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600390 ASSERT_XGL_SUCCESS( err );
391
392 err = xglQueueWaitIdle( m_device->m_queue );
393 ASSERT_XGL_SUCCESS( err );
394
395 // Wait for work to finish before cleaning up.
396 xglDeviceWaitIdle(m_device->device());
397
Chia-I Wue7748802014-12-05 10:45:15 +0800398 assert(m_renderTargetCount == 1);
399 RecordImage(m_renderTargets[0]);
Courtney Goeltzenleuchter3c601d82014-10-13 13:03:31 -0600400 }
Cody Northrop7a1f0462014-10-10 14:49:36 -0600401}
402
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600403void dumpMatrix(const char *note, glm::mat4 MVP)
404{
Chia-I Wu7133fdc2014-12-15 23:57:34 +0800405 int i;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600406
407 printf("%s: \n", note);
408 for (i=0; i<4; i++) {
409 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
410 }
411 printf("\n");
412 fflush(stdout);
413}
414
415void dumpVec4(const char *note, glm::vec4 vector)
416{
417 printf("%s: \n", note);
418 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
419 printf("\n");
420 fflush(stdout);
421}
422
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600423void XglRenderTest::GenerateClearAndPrepareBufferCmds()
424{
425 XglRenderFramework::GenerateClearAndPrepareBufferCmds();
Courtney Goeltzenleuchterd04e38c2014-10-22 18:02:30 -0600426
Tony Barbour97a36232014-12-04 17:14:45 -0700427 if (m_depthStencilImage) {
Tony Barbour97a36232014-12-04 17:14:45 -0700428 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
429 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
430 dsRange.baseMipLevel = 0;
431 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
432 dsRange.baseArraySlice = 0;
433 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
Cody Northrop7a1f0462014-10-10 14:49:36 -0600434
Tony Barbour97a36232014-12-04 17:14:45 -0700435 // prepare the depth buffer for clear
436 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
437 transitionToClear.image = m_depthStencilImage;
438 transitionToClear.oldState = m_depthStencilBinding.depthState;
439 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
440 transitionToClear.subresourceRange = dsRange;
441 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
442 m_depthStencilBinding.depthState = transitionToClear.newState;
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600443
Tony Barbour97a36232014-12-04 17:14:45 -0700444 xglCmdClearDepthStencil(m_cmdBuffer, m_depthStencilImage, 1.0f, 0, 1, &dsRange);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600445
Tony Barbour97a36232014-12-04 17:14:45 -0700446 // prepare depth buffer for rendering
447 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
Chia-I Wu18749b02014-12-05 10:48:20 +0800448 transitionToRender.image = m_depthStencilImage;
Tony Barbour97a36232014-12-04 17:14:45 -0700449 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
450 transitionToRender.newState = m_depthStencilBinding.depthState;
451 transitionToRender.subresourceRange = dsRange;
452 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
453 m_depthStencilBinding.depthState = transitionToClear.newState;
454 }
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600455}
456
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600457void XglRenderTest::InitDepthStencil()
458{
459 XGL_RESULT err;
460 XGL_IMAGE_CREATE_INFO image;
461 XGL_MEMORY_ALLOC_INFO mem_alloc;
462 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
463 XGL_MEMORY_REQUIREMENTS mem_reqs;
Jon Ashburn6317c492014-11-21 11:33:20 -0700464 XGL_SIZE mem_reqs_size=sizeof(XGL_MEMORY_REQUIREMENTS);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -0600465
466 // Clean up default state created by framework
467 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
468
469 m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16;
470 m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS;
471
472 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
473 image.pNext = NULL;
474 image.imageType = XGL_IMAGE_2D;
475 image.format = m_depth_stencil_fmt;
476 image.extent.width = m_width;
477 image.extent.height = m_height;
478 image.extent.depth = 1;
479 image.mipLevels = 1;
480 image.arraySize = 1;
481 image.samples = 1;
482 image.tiling = XGL_OPTIMAL_TILING;
483 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
484 image.flags = 0;
485
486 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
487 mem_alloc.pNext = NULL;
488 mem_alloc.allocationSize = 0;
489 mem_alloc.alignment = 0;
490 mem_alloc.flags = 0;
491 mem_alloc.heapCount = 0;
492 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
493
494 /* create image */
495 err = xglCreateImage(device(), &image,
496 &m_depthStencilImage);
497 ASSERT_XGL_SUCCESS(err);
498
499 err = xglGetObjectInfo(m_depthStencilImage,
500 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
501 &mem_reqs_size, &mem_reqs);
502 ASSERT_XGL_SUCCESS(err);
503 ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs));
504
505 mem_alloc.allocationSize = mem_reqs.size;
506 mem_alloc.alignment = mem_reqs.alignment;
507 mem_alloc.heapCount = mem_reqs.heapCount;
508 memcpy(mem_alloc.heaps, mem_reqs.heaps,
509 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
510
511 /* allocate memory */
512 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem);
513 ASSERT_XGL_SUCCESS(err);
514
515 /* bind memory */
516 err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0);
517 ASSERT_XGL_SUCCESS(err);
518
519 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
520 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
521 depthStencil.depthTestEnable = XGL_TRUE;
522 depthStencil.depthWriteEnable = XGL_TRUE;
523 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
524 depthStencil.depthBoundsEnable = XGL_FALSE;
525 depthStencil.minDepth = 0.f;
526 depthStencil.maxDepth = 1.f;
527 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
528 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
529 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
530 depthStencil.back.stencilRef = 0x00;
531 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
532 depthStencil.front = depthStencil.back;
533
534 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
535 ASSERT_XGL_SUCCESS( err );
536
537 /* create image view */
538 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
539 view.pNext = NULL;
540 view.image = XGL_NULL_HANDLE;
541 view.mipLevel = 0;
542 view.baseArraySlice = 0;
543 view.arraySize = 1;
544 view.flags = 0;
545 view.image = m_depthStencilImage;
546 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
547 ASSERT_XGL_SUCCESS(err);
548
549 m_depthStencilBinding.view = m_depthStencilView;
550 m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
551 m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
552}
553
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600554struct xgltriangle_vs_uniform {
555 // Must start with MVP
556 XGL_FLOAT mvp[4][4];
557 XGL_FLOAT position[3][4];
558 XGL_FLOAT color[3][4];
559};
560
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600561void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText)
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600562{
Tobin Ehlis791a49c2014-11-10 12:29:12 -0700563#ifdef DEBUG_CALLBACK
564 xglDbgRegisterMsgCallback(myDbgFunc, NULL);
565#endif
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600566 // Create identity matrix
567 int i;
568 struct xgltriangle_vs_uniform data;
569
570 glm::mat4 Projection = glm::mat4(1.0f);
571 glm::mat4 View = glm::mat4(1.0f);
572 glm::mat4 Model = glm::mat4(1.0f);
573 glm::mat4 MVP = Projection * View * Model;
574 const int matrixSize = sizeof(MVP);
575 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT);
576 memcpy(&data.mvp, &MVP[0][0], matrixSize);
577
578 static const Vertex tri_data[] =
579 {
580 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
581 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
582 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
583 };
584
585 for (i=0; i<3; i++) {
586 data.position[i][0] = tri_data[i].posX;
587 data.position[i][1] = tri_data[i].posY;
588 data.position[i][2] = tri_data[i].posZ;
589 data.position[i][3] = tri_data[i].posW;
590 data.color[i][0] = tri_data[i].r;
591 data.color[i][1] = tri_data[i].g;
592 data.color[i][2] = tri_data[i].b;
593 data.color[i][3] = tri_data[i].a;
594 }
595
Tony Barbourf43b6982014-11-25 13:18:32 -0700596 ASSERT_NO_FATAL_FAILURE(InitState());
597 ASSERT_NO_FATAL_FAILURE(InitViewport());
598
599 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data);
600
601 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
602 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
603 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
604
605 XglPipelineObj pipelineobj(m_device);
606 pipelineobj.AddShader(&vs);
607 pipelineobj.AddShader(&ps);
608
609 XglDescriptorSetObj descriptorSet(m_device);
610 descriptorSet.AttachMemoryView(&constantBuffer);
611 m_memoryRefManager.AddMemoryRef(&constantBuffer);
612
Tony Barboura3953b82014-12-03 15:46:29 -0700613 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -0700614 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
615
Tony Barbour09da2212014-12-03 16:13:23 -0700616 RotateTriangleVSUniform(Projection, View, Model, &constantBuffer);
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700617#ifdef PRINT_OBJECTS
618 //XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)
619 OBJ_TRACK_GET_OBJECT_COUNT pObjTrackGetObjectCount = (OBJ_TRACK_GET_OBJECT_COUNT)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjectCount");
620 XGL_UINT64 numObjects = pObjTrackGetObjectCount(XGL_OBJECT_TYPE_ANY);
621 //OBJ_TRACK_GET_OBJECTS pGetObjsFunc = xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
622 printf("DEBUG : Number of Objects : %lu\n", numObjects);
623 OBJ_TRACK_GET_OBJECTS pObjTrackGetObjs = (OBJ_TRACK_GET_OBJECTS)xglGetProcAddr(gpu(), (XGL_CHAR*)"objTrackGetObjects");
624 OBJTRACK_NODE* pObjNodeArray = (OBJTRACK_NODE*)malloc(sizeof(OBJTRACK_NODE)*numObjects);
625 pObjTrackGetObjs(XGL_OBJECT_TYPE_ANY, numObjects, pObjNodeArray);
626 for (i=0; i < numObjects; i++) {
627 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);
628 }
629 free(pObjNodeArray);
630#endif
Tony Barbourf43b6982014-11-25 13:18:32 -0700631
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600632}
633
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600634TEST_F(XglRenderTest, XGLTriangle_FragColor)
635{
636 static const char *vertShaderText =
637 "#version 140\n"
638 "#extension GL_ARB_separate_shader_objects : enable\n"
639 "#extension GL_ARB_shading_language_420pack : enable\n"
640 "\n"
641 "layout(binding = 0) uniform buf {\n"
642 " mat4 MVP;\n"
643 " vec4 position[3];\n"
644 " vec4 color[3];\n"
645 "} ubuf;\n"
646 "\n"
647 "layout (location = 0) out vec4 outColor;\n"
648 "\n"
649 "void main() \n"
650 "{\n"
651 " outColor = ubuf.color[gl_VertexID];\n"
652 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
653 "}\n";
654
655 static const char *fragShaderText =
656 "#version 140\n"
657 "#extension GL_ARB_separate_shader_objects : enable\n"
658 "#extension GL_ARB_shading_language_420pack : enable\n"
659 "\n"
660 "layout (location = 0) in vec4 inColor;\n"
661 "\n"
662 "void main()\n"
663 "{\n"
664 " gl_FragColor = inColor;\n"
665 "}\n";
666
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600667 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
Courtney Goeltzenleuchter6acb8022014-10-27 13:08:55 -0600668 XGLTriangleTest(vertShaderText, fragShaderText);
669}
670
671TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
672{
673 static const char *vertShaderText =
674 "#version 140\n"
675 "#extension GL_ARB_separate_shader_objects : enable\n"
676 "#extension GL_ARB_shading_language_420pack : enable\n"
677 "\n"
678 "layout(binding = 0) uniform buf {\n"
679 " mat4 MVP;\n"
680 " vec4 position[3];\n"
681 " vec4 color[3];\n"
682 "} ubuf;\n"
683 "\n"
684 "layout (location = 0) out vec4 outColor;\n"
685 "\n"
686 "void main() \n"
687 "{\n"
688 " outColor = ubuf.color[gl_VertexID];\n"
689 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
690 "}\n";
691
692 static const char *fragShaderText =
693 "#version 140\n"
694 "#extension GL_ARB_separate_shader_objects : enable\n"
695 "#extension GL_ARB_shading_language_420pack : enable\n"
696 "\n"
697 "layout (location = 0) in vec4 inColor;\n"
698 "layout (location = 0) out vec4 outColor;\n"
699 "\n"
700 "void main()\n"
701 "{\n"
702 " outColor = inColor;\n"
703 "}\n";
704
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600705 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 -0600706
707 XGLTriangleTest(vertShaderText, fragShaderText);
708}
709
Tony Barbourf43b6982014-11-25 13:18:32 -0700710TEST_F(XglRenderTest, BIL_XGLTriangle)
711{
712 bool saved_use_bil = XglTestFramework::m_use_bil;
713
714 static const char *vertShaderText =
715 "#version 140\n"
716 "#extension GL_ARB_separate_shader_objects : enable\n"
717 "#extension GL_ARB_shading_language_420pack : enable\n"
718 "\n"
719 "layout(binding = 0) uniform buf {\n"
720 " mat4 MVP;\n"
721 " vec4 position[3];\n"
722 " vec4 color[3];\n"
723 "} ubuf;\n"
724 "\n"
725 "layout (location = 0) out vec4 outColor;\n"
726 "\n"
727 "void main() \n"
728 "{\n"
729 " outColor = ubuf.color[gl_VertexID];\n"
730 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
731 "}\n";
732
733 static const char *fragShaderText =
734 "#version 140\n"
735 "#extension GL_ARB_separate_shader_objects : enable\n"
736 "#extension GL_ARB_shading_language_420pack : enable\n"
737 "\n"
738 "layout (location = 0) in vec4 inColor;\n"
739 "\n"
740 "void main()\n"
741 "{\n"
742 " gl_FragColor = inColor;\n"
743 "}\n";
744
745 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver.");
746
747 XglTestFramework::m_use_bil = true;
748
749 XGLTriangleTest(vertShaderText, fragShaderText);
750
751 XglTestFramework::m_use_bil = saved_use_bil;
752}
753
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600754TEST_F(XglRenderTest, GreenTriangle)
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600755{
756 static const char *vertShaderText =
757 "#version 130\n"
758 "vec2 vertices[3];\n"
759 "void main() {\n"
760 " vertices[0] = vec2(-1.0, -1.0);\n"
761 " vertices[1] = vec2( 1.0, -1.0);\n"
762 " vertices[2] = vec2( 0.0, 1.0);\n"
763 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
764 "}\n";
Courtney Goeltzenleuchter98e49432014-10-09 15:40:19 -0600765
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600766 static const char *fragShaderText =
767 "#version 130\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600768 "void main() {\n"
Steve K10b32512014-10-10 08:54:29 -0600769 " gl_FragColor = vec4(0,1,0,1);\n"
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600770 "}\n";
Courtney Goeltzenleuchter7bbb4202014-10-24 16:26:12 -0600771
Courtney Goeltzenleuchtereab90102014-10-27 13:09:23 -0600772 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
773
Cody Northrop5b7d85a2014-10-09 21:26:47 -0600774 DrawTriangleTest(vertShaderText, fragShaderText);
775}
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600776
Tony Barbourf43b6982014-11-25 13:18:32 -0700777TEST_F(XglRenderTest, BIL_GreenTriangle)
778{
779 bool saved_use_bil = XglTestFramework::m_use_bil;
780
781 static const char *vertShaderText =
782 "#version 130\n"
783 "vec2 vertices[3];\n"
784 "void main() {\n"
785 " vertices[0] = vec2(-1.0, -1.0);\n"
786 " vertices[1] = vec2( 1.0, -1.0);\n"
787 " vertices[2] = vec2( 0.0, 1.0);\n"
788 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
789 "}\n";
790
791 static const char *fragShaderText =
792 "#version 130\n"
793 "void main() {\n"
794 " gl_FragColor = vec4(0,1,0,1);\n"
795 "}\n";
796
797 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver.");
798
799 XglTestFramework::m_use_bil = true;
800 DrawTriangleTest(vertShaderText, fragShaderText);
801 XglTestFramework::m_use_bil = saved_use_bil;
802}
803
804TEST_F(XglRenderTest, YellowTriangle)
805{
806 static const char *vertShaderText =
807 "#version 130\n"
808 "void main() {\n"
809 " vec2 vertices[3];"
810 " vertices[0] = vec2(-0.5, -0.5);\n"
811 " vertices[1] = vec2( 0.5, -0.5);\n"
812 " vertices[2] = vec2( 0.5, 0.5);\n"
813 " vec4 colors[3];\n"
814 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
815 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
816 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
817 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
818 "}\n";
819
820 static const char *fragShaderText =
821 "#version 130\n"
822 "void main() {\n"
823 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
824 "}\n";
825
826 DrawTriangleTest(vertShaderText, fragShaderText);
827}
828
Courtney Goeltzenleuchter08ccb482014-10-10 17:02:53 -0600829TEST_F(XglRenderTest, TriangleWithVertexFetch)
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600830{
Courtney Goeltzenleuchtere5409342014-10-08 14:26:40 -0600831 static const char *vertShaderText =
Tony Barbourf43b6982014-11-25 13:18:32 -0700832 "#version 130\n"
833 //XYZ1( -1, -1, -1 )
834 "in vec4 pos;\n"
835 //XYZ1( 0.f, 0.f, 0.f )
836 "in vec4 inColor;\n"
837 "out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600838 "void main() {\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -0600839 " outColor = inColor;\n"
Cody Northrop4e6b4762014-10-09 21:25:22 -0600840 " gl_Position = pos;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600841 "}\n";
842
Cody Northrop0dbe84f2014-10-09 19:55:56 -0600843
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600844 static const char *fragShaderText =
Cody Northrop68a10f62014-12-05 15:44:14 -0700845 "#version 140\n"
846 "#extension GL_ARB_separate_shader_objects : enable\n"
847 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -0700848 "in vec4 color;\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700849 "layout (location = 0) out vec4 outColor;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600850 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -0700851 " outColor = color;\n"
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600852 "}\n";
Courtney Goeltzenleuchter9b4ab892014-10-09 15:37:21 -0600853
Tony Barbourf43b6982014-11-25 13:18:32 -0700854
855
856 ASSERT_NO_FATAL_FAILURE(InitState());
857 ASSERT_NO_FATAL_FAILURE(InitViewport());
858
859 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -0700860 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -0700861
862 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
863 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
864
865 XglPipelineObj pipelineobj(m_device);
866 pipelineobj.AddShader(&vs);
867 pipelineobj.AddShader(&ps);
868
869 XglDescriptorSetObj descriptorSet(m_device);
870 descriptorSet.AttachMemoryView(&meshBuffer);
871
872 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
873 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
874 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
875 };
876
877 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
878 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
879 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
880 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
881 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
882 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
883 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
884 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
885 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
886
887 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
888 pipelineobj.AddVertexInputBindings(&vi_binding,1);
889 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
890
Tony Barboura3953b82014-12-03 15:46:29 -0700891 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -0700892 QueueCommandBuffer(NULL, 0);
893
Tobin Ehlis34e0e442014-10-07 14:41:29 -0600894}
895
Chia-I Wue09d1a72014-12-05 10:32:23 +0800896TEST_F(XglRenderTest, TriangleMRT)
897{
898 static const char *vertShaderText =
899 "#version 130\n"
900 "in vec4 pos;\n"
901 "void main() {\n"
902 " gl_Position = pos;\n"
903 "}\n";
904
905 static const char *fragShaderText =
906 "#version 130\n"
907 "void main() {\n"
908 " gl_FragData[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
909 " gl_FragData[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
910 "}\n";
911 const XGL_FLOAT vb_data[][2] = {
912 { -1.0f, -1.0f },
913 { 1.0f, -1.0f },
914 { -1.0f, 1.0f }
915 };
916
917 ASSERT_NO_FATAL_FAILURE(InitState());
918 ASSERT_NO_FATAL_FAILURE(InitViewport());
919
920 XglConstantBufferObj meshBuffer(m_device, sizeof(vb_data) / sizeof(vb_data[0]), sizeof(vb_data[0]), vb_data);
Tony Barboure6152042014-12-10 17:40:15 -0700921 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Chia-I Wue09d1a72014-12-05 10:32:23 +0800922
923 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
924 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
925
926 XglPipelineObj pipelineobj(m_device);
927 pipelineobj.AddShader(&vs);
928 pipelineobj.AddShader(&ps);
929
930 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
931 sizeof(vb_data[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
932 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
933 };
934
935 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attrib;
936 vi_attrib.binding = 0; // index into vertexBindingDescriptions
937 vi_attrib.format.channelFormat = XGL_CH_FMT_R32G32; // format of source data
938 vi_attrib.format.numericFormat = XGL_NUM_FMT_FLOAT;
939 vi_attrib.offsetInBytes = 0; // Offset of first element in bytes from base of vertex
940
941 pipelineobj.AddVertexInputAttribs(&vi_attrib, 1);
942 pipelineobj.AddVertexInputBindings(&vi_binding,1);
943 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
944
945 XglDescriptorSetObj descriptorSet(m_device);
946
947 m_renderTargetCount = 2;
948
949 XGL_PIPELINE_CB_ATTACHMENT_STATE att = {};
950 att.blendEnable = XGL_FALSE;
951 att.format = m_render_target_fmt;
952 att.channelWriteMask = 0xf;
953 pipelineobj.SetColorAttachment(1, &att);
954
955 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
956 QueueCommandBuffer(NULL, 0);
957}
958
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700959TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
960{
961 static const char *vertShaderText =
962 "#version 140\n"
963 "#extension GL_ARB_separate_shader_objects : enable\n"
964 "#extension GL_ARB_shading_language_420pack : enable\n"
965 "layout(location = 0) in vec4 pos;\n"
966 "layout(location = 1) in vec4 inColor;\n"
967 "layout(location = 0) out vec4 outColor;\n"
968 "void main() {\n"
969 " outColor = inColor;\n"
970 " gl_Position = pos;\n"
971 "}\n";
972
973
974 static const char *fragShaderText =
975 "#version 140\n"
976 "#extension GL_ARB_separate_shader_objects : enable\n"
977 "#extension GL_ARB_shading_language_420pack : enable\n"
978 "layout(location = 0) in vec4 color;\n"
979 "void main() {\n"
980 " gl_FragColor = color;\n"
981 "}\n";
982
983 const Vertex g_vbData[] =
984 {
985 // first tri
986 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
987 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
988 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
989
990 // second tri
991 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
992 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
993 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
994 };
995
996 const uint16_t g_idxData[6] = {
997 0, 1, 2,
998 3, 4, 5,
999 };
1000
1001 ASSERT_NO_FATAL_FAILURE(InitState());
1002 ASSERT_NO_FATAL_FAILURE(InitViewport());
1003
1004 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001005 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001006
1007 XglIndexBufferObj indexBuffer(m_device);
1008 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
Tony Barboure6152042014-12-10 17:40:15 -07001009 indexBuffer.SetMemoryState(XGL_MEMORY_STATE_INDEX_DATA);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001010
1011 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1012 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1013
1014 XglPipelineObj pipelineobj(m_device);
1015 pipelineobj.AddShader(&vs);
1016 pipelineobj.AddShader(&ps);
1017
1018 XglDescriptorSetObj descriptorSet(m_device);
1019
1020 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1021 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1022 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1023 };
1024
1025 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1026 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1027 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1028 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1029 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1030 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1031 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1032 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1033 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1034
1035 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1036 pipelineobj.AddVertexInputBindings(&vi_binding,1);
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001037 pipelineobj.CreateXGLPipeline(&descriptorSet);
1038 descriptorSet.CreateXGLDescriptorSet();
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001039
1040 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001041 XglCommandBufferObj cmdBuffer(m_device);
1042 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1043 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
1044 cmdBuffer.ClearAllBuffers(NULL, NULL);
1045 cmdBuffer.BindAttachments(NULL);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001046
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001047#ifdef DUMP_STATE_DOT
1048 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
Tobin Ehlis266473d2014-12-16 17:34:50 -07001049 pDSDumpDot((char*)"triTest2.dot");
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001050#endif
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001051 cmdBuffer.BindState(m_stateRaster, m_stateViewport, m_colorBlend, m_stateDepthStencil, m_stateMsaa);
1052 cmdBuffer.BindPipeline(pipelineobj.GetPipelineHandle());
1053 cmdBuffer.BindDescriptorSet(descriptorSet.GetDescriptorSetHandle());
1054 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1055 cmdBuffer.BindIndexBuffer(&indexBuffer,0);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001056
1057 // render two triangles
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001058 cmdBuffer.DrawIndexed(0, 6, 0, 0, 1);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001059
1060 // finalize recording of the command buffer
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001061 cmdBuffer.EndCommandBuffer();
1062 cmdBuffer.QueueCommandBuffer(NULL, 0);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001063
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001064 for (int i = 0; i < m_renderTargetCount; i++)
1065 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001066
1067}
1068
GregF6bef1212014-12-02 15:41:44 -07001069TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1070{
1071 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001072
GregF6bef1212014-12-02 15:41:44 -07001073 static const char *vertShaderText =
1074 "#version 140\n"
1075 "#extension GL_ARB_separate_shader_objects : enable\n"
1076 "#extension GL_ARB_shading_language_420pack : enable\n"
1077 "layout (location = 0) in vec4 pos;\n"
1078 "layout (location = 0) out vec4 outColor;\n"
1079 "layout (location = 1) out vec4 outColor2;\n"
1080 "void main() {\n"
1081 " gl_Position = pos;\n"
1082 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1083 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1084 "}\n";
1085
1086 static const char *fragShaderText =
1087 //"#version 140\n"
1088 "#version 330\n"
1089 "#extension GL_ARB_separate_shader_objects : enable\n"
1090 "#extension GL_ARB_shading_language_420pack : enable\n"
1091 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1092 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1093 "layout (location = 0) in vec4 color;\n"
1094 "layout (location = 1) in vec4 color2;\n"
1095 "void main() {\n"
1096 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1097 " float dist_squared = dot(pos, pos);\n"
1098 " gl_FragColor = (dist_squared < 400.0)\n"
1099 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1100 " : color2;\n"
1101 "}\n";
1102
1103 ASSERT_NO_FATAL_FAILURE(InitState());
1104 ASSERT_NO_FATAL_FAILURE(InitViewport());
1105
1106 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001107 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001108
1109 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1110 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1111
1112 XglPipelineObj pipelineobj(m_device);
1113 pipelineobj.AddShader(&vs);
1114 pipelineobj.AddShader(&ps);
1115
1116 XglDescriptorSetObj descriptorSet(m_device);
1117 descriptorSet.AttachMemoryView(&meshBuffer);
1118
1119 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1120 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1121 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1122 };
1123
1124 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1125 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1126 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1127 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1128 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1129 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1130 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1131 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1132 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1133
1134 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1135 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1136 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1137
Tony Barbour09da2212014-12-03 16:13:23 -07001138 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001139 QueueCommandBuffer(NULL, 0);
1140
1141}
1142
1143TEST_F(XglRenderTest, RedCirclesonBlue)
1144{
1145 // This tests that we correctly handle unread fragment inputs
1146
1147 static const char *vertShaderText =
1148 "#version 140\n"
1149 "#extension GL_ARB_separate_shader_objects : enable\n"
1150 "#extension GL_ARB_shading_language_420pack : enable\n"
1151 "layout (location = 0) in vec4 pos;\n"
1152 "layout (location = 0) out vec4 outColor;\n"
1153 "layout (location = 1) out vec4 outColor2;\n"
1154 "void main() {\n"
1155 " gl_Position = pos;\n"
1156 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1157 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1158 "}\n";
1159
1160 static const char *fragShaderText =
1161 //"#version 140\n"
1162 "#version 330\n"
1163 "#extension GL_ARB_separate_shader_objects : enable\n"
1164 "#extension GL_ARB_shading_language_420pack : enable\n"
1165 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1166 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1167 "layout (location = 0) in vec4 color;\n"
1168 "layout (location = 1) in vec4 color2;\n"
1169 "void main() {\n"
1170 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1171 " float dist_squared = dot(pos, pos);\n"
1172 " gl_FragColor = (dist_squared < 400.0)\n"
1173 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1174 " : color2;\n"
1175 "}\n";
1176
1177 ASSERT_NO_FATAL_FAILURE(InitState());
1178 ASSERT_NO_FATAL_FAILURE(InitViewport());
1179
1180 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001181 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001182
1183 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1184 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1185
1186 XglPipelineObj pipelineobj(m_device);
1187 pipelineobj.AddShader(&vs);
1188 pipelineobj.AddShader(&ps);
1189
1190 XglDescriptorSetObj descriptorSet(m_device);
1191 descriptorSet.AttachMemoryView(&meshBuffer);
1192
1193 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1194 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1195 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1196 };
1197
1198 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1199 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1200 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1201 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1202 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1203 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1204 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1205 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1206 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1207
1208 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1209 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1210 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1211
Tony Barbour09da2212014-12-03 16:13:23 -07001212 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001213 QueueCommandBuffer(NULL, 0);
1214
1215}
1216
1217TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1218{
1219 // This tests reading gl_ClipDistance from FS
1220
1221 static const char *vertShaderText =
1222 "#version 330\n"
1223 "#extension GL_ARB_separate_shader_objects : enable\n"
1224 "#extension GL_ARB_shading_language_420pack : enable\n"
1225 "out gl_PerVertex {\n"
1226 " vec4 gl_Position;\n"
1227 " float gl_ClipDistance[1];\n"
1228 "};\n"
1229 "layout (location = 0) in vec4 pos;\n"
1230 "layout (location = 0) out vec4 outColor;\n"
1231 "layout (location = 1) out vec4 outColor2;\n"
1232 "void main() {\n"
1233 " gl_Position = pos;\n"
1234 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1235 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1236 " float dists[3];\n"
1237 " dists[0] = 0.0;\n"
1238 " dists[1] = 1.0;\n"
1239 " dists[2] = 1.0;\n"
1240 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1241 "}\n";
1242
1243
1244 static const char *fragShaderText =
1245 //"#version 140\n"
1246 "#version 330\n"
1247 "#extension GL_ARB_separate_shader_objects : enable\n"
1248 "#extension GL_ARB_shading_language_420pack : enable\n"
1249 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1250 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1251 "layout (location = 0) in vec4 color;\n"
1252 "layout (location = 1) in vec4 color2;\n"
1253 "void main() {\n"
1254 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1255 " float dist_squared = dot(pos, pos);\n"
1256 " gl_FragColor = (dist_squared < 400.0)\n"
1257 " ? color * gl_ClipDistance[0]\n"
1258 " : color2;\n"
1259 "}\n";
1260
1261 ASSERT_NO_FATAL_FAILURE(InitState());
1262 ASSERT_NO_FATAL_FAILURE(InitViewport());
1263
1264 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001265 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001266
1267 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1268 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1269
1270 XglPipelineObj pipelineobj(m_device);
1271 pipelineobj.AddShader(&vs);
1272 pipelineobj.AddShader(&ps);
1273
1274 XglDescriptorSetObj descriptorSet(m_device);
1275 descriptorSet.AttachMemoryView(&meshBuffer);
1276
1277 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1278 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1279 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1280 };
1281
1282 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1283 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1284 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1285 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1286 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1287 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1288 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1289 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1290 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1291
1292 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1293 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1294 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1295
Tony Barboura3953b82014-12-03 15:46:29 -07001296 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001297 QueueCommandBuffer(NULL, 0);
1298
1299}
Tony Barbourf43b6982014-11-25 13:18:32 -07001300
GregF7a23c792014-12-02 17:19:34 -07001301TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1302{
1303 static const char *vertShaderText =
1304 "#version 140\n"
1305 "#extension GL_ARB_separate_shader_objects : enable\n"
1306 "#extension GL_ARB_shading_language_420pack : enable\n"
1307 "layout (location = 0) in vec4 pos;\n"
1308 "layout (location = 0) out vec4 outColor;\n"
1309 "layout (location = 1) out vec4 outColor2;\n"
1310 "void main() {\n"
1311 " gl_Position = pos;\n"
1312 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1313 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1314 "}\n";
1315
1316
1317 static const char *fragShaderText =
1318 //"#version 140\n"
1319 "#version 330\n"
1320 "#extension GL_ARB_separate_shader_objects : enable\n"
1321 "#extension GL_ARB_shading_language_420pack : enable\n"
1322 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1323 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1324 "layout (location = 0) in vec4 color;\n"
1325 "layout (location = 1) in vec4 color2;\n"
1326 "void main() {\n"
1327 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1328 " float dist_squared = dot(pos, pos);\n"
1329 " if (dist_squared < 100.0)\n"
1330 " discard;\n"
1331 " gl_FragColor = (dist_squared < 400.0)\n"
1332 " ? color\n"
1333 " : color2;\n"
1334 "}\n";
1335
1336 ASSERT_NO_FATAL_FAILURE(InitState());
1337 ASSERT_NO_FATAL_FAILURE(InitViewport());
1338
1339 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001340 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF7a23c792014-12-02 17:19:34 -07001341
1342 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1343 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1344
1345 XglPipelineObj pipelineobj(m_device);
1346 pipelineobj.AddShader(&vs);
1347 pipelineobj.AddShader(&ps);
1348
1349 XglDescriptorSetObj descriptorSet(m_device);
1350 descriptorSet.AttachMemoryView(&meshBuffer);
1351
1352 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1353 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1354 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1355 };
1356
1357 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1358 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1359 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1360 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1361 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1362 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1363 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1364 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1365 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1366
1367 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1368 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1369 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1370
Tony Barbour09da2212014-12-03 16:13:23 -07001371 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF7a23c792014-12-02 17:19:34 -07001372 QueueCommandBuffer(NULL, 0);
1373
1374}
1375
1376
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001377TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001378{
1379 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001380 "#version 140\n"
1381 "#extension GL_ARB_separate_shader_objects : enable\n"
1382 "#extension GL_ARB_shading_language_420pack : enable\n"
1383 "\n"
1384 "layout(binding = 0) uniform buf {\n"
1385 " mat4 MVP;\n"
1386 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001387 "void main() {\n"
1388 " vec2 vertices[3];"
1389 " vertices[0] = vec2(-0.5, -0.5);\n"
1390 " vertices[1] = vec2( 0.5, -0.5);\n"
1391 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001392 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001393 "}\n";
1394
1395 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001396 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001397 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001398 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001399 "}\n";
1400
Tony Barbourf43b6982014-11-25 13:18:32 -07001401 ASSERT_NO_FATAL_FAILURE(InitState());
1402 ASSERT_NO_FATAL_FAILURE(InitViewport());
1403
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001404 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001405 glm::mat4 Projection = glm::mat4(1.0f);
1406 glm::mat4 View = glm::mat4(1.0f);
1407 glm::mat4 Model = glm::mat4(1.0f);
1408 glm::mat4 MVP = Projection * View * Model;
1409 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1410
Tony Barbourf43b6982014-11-25 13:18:32 -07001411 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1412 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1413 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001414
Tony Barbourf43b6982014-11-25 13:18:32 -07001415 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1416
1417 XglPipelineObj pipelineobj(m_device);
1418 pipelineobj.AddShader(&vs);
1419 pipelineobj.AddShader(&ps);
1420
1421 // Create descriptor set and attach the constant buffer to it
1422 XglDescriptorSetObj descriptorSet(m_device);
1423 descriptorSet.AttachMemoryView(&MVPBuffer);
1424
1425 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1426
Tony Barboura3953b82014-12-03 15:46:29 -07001427 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001428 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1429
Tony Barbour09da2212014-12-03 16:13:23 -07001430 RotateTriangleVSUniform(Projection, View, Model, &MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001431}
1432
Tony Barbourf43b6982014-11-25 13:18:32 -07001433TEST_F(XglRenderTest, MixTriangle)
1434{
1435 // This tests location applied to varyings. Notice that we have switched foo
1436 // and bar in the FS. The triangle should be blended with red, green and blue
1437 // corners.
1438 static const char *vertShaderText =
1439 "#version 140\n"
1440 "#extension GL_ARB_separate_shader_objects : enable\n"
1441 "#extension GL_ARB_shading_language_420pack : enable\n"
1442 "layout (location=0) out vec4 bar;\n"
1443 "layout (location=1) out vec4 foo;\n"
1444 "layout (location=2) out float scale;\n"
1445 "vec2 vertices[3];\n"
1446 "void main() {\n"
1447 " vertices[0] = vec2(-1.0, -1.0);\n"
1448 " vertices[1] = vec2( 1.0, -1.0);\n"
1449 " vertices[2] = vec2( 0.0, 1.0);\n"
1450 "vec4 colors[3];\n"
1451 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1452 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1453 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1454 " foo = colors[gl_VertexID % 3];\n"
1455 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1456 " scale = 1.0;\n"
1457 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1458 "}\n";
1459
1460 static const char *fragShaderText =
1461 "#version 140\n"
1462 "#extension GL_ARB_separate_shader_objects : enable\n"
1463 "#extension GL_ARB_shading_language_420pack : enable\n"
1464 "layout (location = 1) in vec4 bar;\n"
1465 "layout (location = 0) in vec4 foo;\n"
1466 "layout (location = 2) in float scale;\n"
1467 "void main() {\n"
1468 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1469 "}\n";
1470
1471 ASSERT_NO_FATAL_FAILURE(InitState());
1472 ASSERT_NO_FATAL_FAILURE(InitViewport());
1473
1474 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1475 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1476
1477 XglPipelineObj pipelineobj(m_device);
1478 pipelineobj.AddShader(&vs);
1479 pipelineobj.AddShader(&ps);
1480
1481 XglDescriptorSetObj descriptorSet(m_device);
1482
Tony Barboura3953b82014-12-03 15:46:29 -07001483 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001484 QueueCommandBuffer(NULL, 0);
1485}
1486
1487TEST_F(XglRenderTest, TriVertFetchAndVertID)
1488{
1489 // This tests that attributes work in the presence of gl_VertexID
1490
1491 static const char *vertShaderText =
1492 "#version 140\n"
1493 "#extension GL_ARB_separate_shader_objects : enable\n"
1494 "#extension GL_ARB_shading_language_420pack : enable\n"
1495 //XYZ1( -1, -1, -1 )
1496 "layout (location = 0) in vec4 pos;\n"
1497 //XYZ1( 0.f, 0.f, 0.f )
1498 "layout (location = 1) in vec4 inColor;\n"
1499 "layout (location = 0) out vec4 outColor;\n"
1500 "void main() {\n"
1501 " outColor = inColor;\n"
1502 " vec4 vertices[3];"
1503 " vertices[gl_VertexID % 3] = pos;\n"
1504 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1505 "}\n";
1506
1507
1508 static const char *fragShaderText =
1509 "#version 140\n"
1510 "#extension GL_ARB_separate_shader_objects : enable\n"
1511 "#extension GL_ARB_shading_language_420pack : enable\n"
1512 "layout (location = 0) in vec4 color;\n"
1513 "void main() {\n"
1514 " gl_FragColor = color;\n"
1515 "}\n";
1516
1517 ASSERT_NO_FATAL_FAILURE(InitState());
1518 ASSERT_NO_FATAL_FAILURE(InitViewport());
1519
1520 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001521 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001522
1523 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1524 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1525
1526 XglPipelineObj pipelineobj(m_device);
1527 pipelineobj.AddShader(&vs);
1528 pipelineobj.AddShader(&ps);
1529
1530 XglDescriptorSetObj descriptorSet(m_device);
1531 descriptorSet.AttachMemoryView(&meshBuffer);
1532
1533 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1534 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1535 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1536 };
1537
1538 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1539 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1540 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1541 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1542 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1543 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1544 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1545 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1546 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1547
1548 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1549 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1550 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1551
Tony Barboura3953b82014-12-03 15:46:29 -07001552 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001553 QueueCommandBuffer(NULL, 0);
1554}
1555
1556TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1557{
1558 // This tests that attributes work in the presence of gl_VertexID
1559 // and a dead attribute in position 0. Draws a triangle with yellow,
1560 // red and green corners, starting at top and going clockwise.
1561
1562 static const char *vertShaderText =
1563 "#version 140\n"
1564 "#extension GL_ARB_separate_shader_objects : enable\n"
1565 "#extension GL_ARB_shading_language_420pack : enable\n"
1566 //XYZ1( -1, -1, -1 )
1567 "layout (location = 0) in vec4 pos;\n"
1568 //XYZ1( 0.f, 0.f, 0.f )
1569 "layout (location = 1) in vec4 inColor;\n"
1570 "layout (location = 0) out vec4 outColor;\n"
1571 "void main() {\n"
1572 " outColor = inColor;\n"
1573 " vec2 vertices[3];"
1574 " vertices[0] = vec2(-1.0, -1.0);\n"
1575 " vertices[1] = vec2( 1.0, -1.0);\n"
1576 " vertices[2] = vec2( 0.0, 1.0);\n"
1577 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1578 "}\n";
1579
1580
1581 static const char *fragShaderText =
1582 "#version 140\n"
1583 "#extension GL_ARB_separate_shader_objects : enable\n"
1584 "#extension GL_ARB_shading_language_420pack : enable\n"
1585 "layout (location = 0) in vec4 color;\n"
1586 "void main() {\n"
1587 " gl_FragColor = color;\n"
1588 "}\n";
1589
1590 ASSERT_NO_FATAL_FAILURE(InitState());
1591 ASSERT_NO_FATAL_FAILURE(InitViewport());
1592
1593 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001594 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001595
1596 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1597 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1598
1599 XglPipelineObj pipelineobj(m_device);
1600 pipelineobj.AddShader(&vs);
1601 pipelineobj.AddShader(&ps);
1602
1603 XglDescriptorSetObj descriptorSet(m_device);
1604 descriptorSet.AttachMemoryView(&meshBuffer);
1605
1606 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1607 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1608 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1609 };
1610
1611 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1612 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1613 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1614 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1615 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1616 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1617 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1618 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1619 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1620
1621 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1622 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1623 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1624
Tony Barbour09da2212014-12-03 16:13:23 -07001625 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001626 QueueCommandBuffer(NULL, 0);
1627
1628}
1629
1630TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001631{
1632 static const char *vertShaderText =
1633 "#version 140\n"
1634 "layout (std140) uniform bufferVals {\n"
1635 " mat4 mvp;\n"
1636 "} myBufferVals;\n"
1637 "in vec4 pos;\n"
1638 "in vec4 inColor;\n"
1639 "out vec4 outColor;\n"
1640 "void main() {\n"
1641 " outColor = inColor;\n"
1642 " gl_Position = myBufferVals.mvp * pos;\n"
1643 "}\n";
1644
1645 static const char *fragShaderText =
1646 "#version 130\n"
1647 "in vec4 color;\n"
1648 "void main() {\n"
1649 " gl_FragColor = color;\n"
1650 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001651 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001652
Tony Barbourf43b6982014-11-25 13:18:32 -07001653 glm::mat4 View = glm::lookAt(
1654 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1655 glm::vec3(0,0,0), // and looks at the origin
1656 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1657 );
1658
1659 glm::mat4 Model = glm::mat4(1.0f);
1660
1661 glm::mat4 MVP = Projection * View * Model;
1662
1663 ASSERT_NO_FATAL_FAILURE(InitState());
1664 ASSERT_NO_FATAL_FAILURE(InitViewport());
1665 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1666
1667 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1668 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1669
1670 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1671
1672 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1673 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1674 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1675
1676 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1677
1678 XglPipelineObj pipelineobj(m_device);
1679 pipelineobj.AddShader(&vs);
1680 pipelineobj.AddShader(&ps);
1681
1682 XglDescriptorSetObj descriptorSet(m_device);
1683 descriptorSet.AttachMemoryView(&MVPBuffer);
1684
1685 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1686 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1687
1688 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1689 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1690 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1691 };
1692
1693 // this is the current description of g_vbData
1694 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1695 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1696 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1697 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1698 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1699 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1700 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1701 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1702 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1703
1704 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1705 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1706 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1707
Tony Barboura3953b82014-12-03 15:46:29 -07001708 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07001709
1710 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1711
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001712}
1713
Tony Barbourf43b6982014-11-25 13:18:32 -07001714TEST_F(XglRenderTest, VSTexture)
1715{
1716 // The expected result from this test is a green and red triangle;
1717 // one red vertex on the left, two green vertices on the right.
1718 static const char *vertShaderText =
1719 "#version 130\n"
1720 "out vec4 texColor;\n"
1721 "uniform sampler2D surface;\n"
1722 "void main() {\n"
1723 " vec2 vertices[3];"
1724 " vertices[0] = vec2(-0.5, -0.5);\n"
1725 " vertices[1] = vec2( 0.5, -0.5);\n"
1726 " vertices[2] = vec2( 0.5, 0.5);\n"
1727 " vec2 positions[3];"
1728 " positions[0] = vec2( 0.0, 0.0);\n"
1729 " positions[1] = vec2( 0.25, 0.1);\n"
1730 " positions[2] = vec2( 0.1, 0.25);\n"
1731 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1732 " texColor = textureLod(surface, samplePos, 0.0);\n"
1733 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1734 "}\n";
1735
1736 static const char *fragShaderText =
1737 "#version 130\n"
1738 "in vec4 texColor;\n"
1739 "void main() {\n"
1740 " gl_FragColor = texColor;\n"
1741 "}\n";
1742
1743 ASSERT_NO_FATAL_FAILURE(InitState());
1744 ASSERT_NO_FATAL_FAILURE(InitViewport());
1745
1746 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1747 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1748 XglSamplerObj sampler(m_device);
1749 XglTextureObj texture(m_device);
1750
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001751 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001752 vs.BindShaderEntitySlotToSampler(0, &sampler);
1753
1754 XglPipelineObj pipelineobj(m_device);
1755 pipelineobj.AddShader(&vs);
1756 pipelineobj.AddShader(&ps);
1757
1758 XglDescriptorSetObj descriptorSet(m_device);
1759 descriptorSet.AttachImageView(&texture);
1760 descriptorSet.AttachSampler(&sampler);
1761
1762 m_memoryRefManager.AddMemoryRef(&texture);
1763
Tony Barboura3953b82014-12-03 15:46:29 -07001764 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001765 QueueCommandBuffer(NULL, 0);
1766
1767}
1768TEST_F(XglRenderTest, TexturedTriangle)
1769{
1770 // The expected result from this test is a red and green checkered triangle
1771 static const char *vertShaderText =
1772 "#version 140\n"
1773 "#extension GL_ARB_separate_shader_objects : enable\n"
1774 "#extension GL_ARB_shading_language_420pack : enable\n"
1775 "layout (location = 0) out vec2 samplePos;\n"
1776 "void main() {\n"
1777 " vec2 vertices[3];"
1778 " vertices[0] = vec2(-0.5, -0.5);\n"
1779 " vertices[1] = vec2( 0.5, -0.5);\n"
1780 " vertices[2] = vec2( 0.5, 0.5);\n"
1781 " vec2 positions[3];"
1782 " positions[0] = vec2( 0.0, 0.0);\n"
1783 " positions[1] = vec2( 1.0, 0.0);\n"
1784 " positions[2] = vec2( 1.0, 1.0);\n"
1785 " samplePos = positions[gl_VertexID % 3];\n"
1786 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1787 "}\n";
1788
1789 static const char *fragShaderText =
1790 "#version 140\n"
1791 "#extension GL_ARB_separate_shader_objects : enable\n"
1792 "#extension GL_ARB_shading_language_420pack : enable\n"
1793 "layout (location = 0) in vec2 samplePos;\n"
1794 "layout (binding = 0) uniform sampler2D surface;\n"
1795 "layout (location=0) out vec4 outColor;\n"
1796 "void main() {\n"
1797 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1798 " outColor = texColor;\n"
1799 "}\n";
1800
1801 ASSERT_NO_FATAL_FAILURE(InitState());
1802 ASSERT_NO_FATAL_FAILURE(InitViewport());
1803
1804 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1805 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1806 XglSamplerObj sampler(m_device);
1807 XglTextureObj texture(m_device);
1808
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001809 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001810 ps.BindShaderEntitySlotToSampler(0, &sampler);
1811
1812 XglPipelineObj pipelineobj(m_device);
1813 pipelineobj.AddShader(&vs);
1814 pipelineobj.AddShader(&ps);
1815
1816 XglDescriptorSetObj descriptorSet(m_device);
1817 descriptorSet.AttachImageView(&texture);
1818 descriptorSet.AttachSampler(&sampler);
1819
1820 m_memoryRefManager.AddMemoryRef(&texture);
1821
Tony Barboura3953b82014-12-03 15:46:29 -07001822 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001823 QueueCommandBuffer(NULL, 0);
1824}
1825TEST_F(XglRenderTest, TexturedTriangleClip)
1826{
1827 // The expected result from this test is a red and green checkered triangle
1828 static const char *vertShaderText =
1829 "#version 330\n"
1830 "#extension GL_ARB_separate_shader_objects : enable\n"
1831 "#extension GL_ARB_shading_language_420pack : enable\n"
1832 "layout (location = 0) out vec2 samplePos;\n"
1833 "out gl_PerVertex {\n"
1834 " vec4 gl_Position;\n"
1835 " float gl_ClipDistance[1];\n"
1836 "};\n"
1837 "void main() {\n"
1838 " vec2 vertices[3];"
1839 " vertices[0] = vec2(-0.5, -0.5);\n"
1840 " vertices[1] = vec2( 0.5, -0.5);\n"
1841 " vertices[2] = vec2( 0.5, 0.5);\n"
1842 " vec2 positions[3];"
1843 " positions[0] = vec2( 0.0, 0.0);\n"
1844 " positions[1] = vec2( 1.0, 0.0);\n"
1845 " positions[2] = vec2( 1.0, 1.0);\n"
1846 " float dists[3];\n"
1847 " dists[0] = 1.0;\n"
1848 " dists[1] = 1.0;\n"
1849 " dists[2] = -1.0;\n"
1850 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1851 " samplePos = positions[gl_VertexID % 3];\n"
1852 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1853 "}\n";
1854
1855 static const char *fragShaderText =
1856 "#version 140\n"
1857 "#extension GL_ARB_separate_shader_objects : enable\n"
1858 "#extension GL_ARB_shading_language_420pack : enable\n"
1859 "layout (location = 0) in vec2 samplePos;\n"
1860 "layout (binding = 0) uniform sampler2D surface;\n"
1861 "layout (location=0) out vec4 outColor;\n"
1862 "void main() {\n"
1863 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1864 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1865 " outColor = texColor;\n"
1866 "}\n";
1867
1868
1869 ASSERT_NO_FATAL_FAILURE(InitState());
1870 ASSERT_NO_FATAL_FAILURE(InitViewport());
1871
1872 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1873 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1874 XglSamplerObj sampler(m_device);
1875 XglTextureObj texture(m_device);
1876
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001877 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001878 ps.BindShaderEntitySlotToSampler(0, &sampler);
1879
1880 XglPipelineObj pipelineobj(m_device);
1881 pipelineobj.AddShader(&vs);
1882 pipelineobj.AddShader(&ps);
1883
1884 XglDescriptorSetObj descriptorSet(m_device);
1885 descriptorSet.AttachImageView(&texture);
1886 descriptorSet.AttachSampler(&sampler);
1887
1888 m_memoryRefManager.AddMemoryRef(&texture);
1889
Tony Barboura3953b82014-12-03 15:46:29 -07001890 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001891 QueueCommandBuffer(NULL, 0);
1892}
1893TEST_F(XglRenderTest, FSTriangle)
1894{
1895 // The expected result from this test is a red and green checkered triangle
1896 static const char *vertShaderText =
1897 "#version 140\n"
1898 "#extension GL_ARB_separate_shader_objects : enable\n"
1899 "#extension GL_ARB_shading_language_420pack : enable\n"
1900 "layout (location = 0) out vec2 samplePos;\n"
1901 "void main() {\n"
1902 " vec2 vertices[3];"
1903 " vertices[0] = vec2(-0.5, -0.5);\n"
1904 " vertices[1] = vec2( 0.5, -0.5);\n"
1905 " vertices[2] = vec2( 0.5, 0.5);\n"
1906 " vec2 positions[3];"
1907 " positions[0] = vec2( 0.0, 0.0);\n"
1908 " positions[1] = vec2( 1.0, 0.0);\n"
1909 " positions[2] = vec2( 1.0, 1.0);\n"
1910 " samplePos = positions[gl_VertexID % 3];\n"
1911 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1912 "}\n";
1913
1914 static const char *fragShaderText =
1915 "#version 140\n"
1916 "#extension GL_ARB_separate_shader_objects : enable\n"
1917 "#extension GL_ARB_shading_language_420pack : enable\n"
1918 "layout (location = 0) in vec2 samplePos;\n"
1919 "layout (binding = 0) uniform sampler2D surface;\n"
1920 "layout (location=0) out vec4 outColor;\n"
1921 "void main() {\n"
1922 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1923 " outColor = texColor;\n"
1924 "}\n";
1925
1926 ASSERT_NO_FATAL_FAILURE(InitState());
1927 ASSERT_NO_FATAL_FAILURE(InitViewport());
1928
1929 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1930 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1931 XglSamplerObj sampler(m_device);
1932 XglTextureObj texture(m_device);
1933
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001934 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001935 ps.BindShaderEntitySlotToSampler(0, &sampler);
1936
1937 XglPipelineObj pipelineobj(m_device);
1938 pipelineobj.AddShader(&vs);
1939 pipelineobj.AddShader(&ps);
1940
1941 XglDescriptorSetObj descriptorSet(m_device);
1942 descriptorSet.AttachImageView(&texture);
1943 descriptorSet.AttachSampler(&sampler);
1944
1945 m_memoryRefManager.AddMemoryRef(&texture);
1946
Tony Barboura3953b82014-12-03 15:46:29 -07001947 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001948 QueueCommandBuffer(NULL, 0);
1949}
1950TEST_F(XglRenderTest, SamplerBindingsTriangle)
1951{
1952 // This test sets bindings on the samplers
1953 // For now we are asserting that sampler and texture pairs
1954 // march in lock step, and are set via GLSL binding. This can
1955 // and will probably change.
1956 // The sampler bindings should match the sampler and texture slot
1957 // number set up by the application.
1958 // This test will result in a blue triangle
1959 static const char *vertShaderText =
1960 "#version 140\n"
1961 "#extension GL_ARB_separate_shader_objects : enable\n"
1962 "#extension GL_ARB_shading_language_420pack : enable\n"
1963 "layout (location = 0) out vec4 samplePos;\n"
1964 "void main() {\n"
1965 " vec2 vertices[3];"
1966 " vertices[0] = vec2(-0.5, -0.5);\n"
1967 " vertices[1] = vec2( 0.5, -0.5);\n"
1968 " vertices[2] = vec2( 0.5, 0.5);\n"
1969 " vec2 positions[3];"
1970 " positions[0] = vec2( 0.0, 0.0);\n"
1971 " positions[1] = vec2( 1.0, 0.0);\n"
1972 " positions[2] = vec2( 1.0, 1.0);\n"
1973 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
1974 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1975 "}\n";
1976
1977 static const char *fragShaderText =
1978 "#version 140\n"
1979 "#extension GL_ARB_separate_shader_objects : enable\n"
1980 "#extension GL_ARB_shading_language_420pack : enable\n"
1981 "layout (location = 0) in vec4 samplePos;\n"
1982 "layout (binding = 0) uniform sampler2D surface0;\n"
1983 "layout (binding = 1) uniform sampler2D surface1;\n"
1984 "layout (binding = 12) uniform sampler2D surface2;\n"
1985 "void main() {\n"
1986 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
1987 "}\n";
1988
1989 ASSERT_NO_FATAL_FAILURE(InitState());
1990 ASSERT_NO_FATAL_FAILURE(InitViewport());
1991
1992 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1993 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1994
1995 XglSamplerObj sampler1(m_device);
1996 XglSamplerObj sampler2(m_device);
1997 XglSamplerObj sampler3(m_device);
1998
1999 XglTextureObj texture1(m_device); // Red
2000 texture1.ChangeColors(0xffff0000,0xffff0000);
2001 XglTextureObj texture2(m_device); // Green
2002 texture2.ChangeColors(0xff00ff00,0xff00ff00);
2003 XglTextureObj texture3(m_device); // Blue
2004 texture3.ChangeColors(0xff0000ff,0xff0000ff);
2005
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002006 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002007 ps.BindShaderEntitySlotToSampler(0, &sampler1);
2008
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002009 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Tony Barbourf43b6982014-11-25 13:18:32 -07002010 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2011
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002012 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture3);
Tony Barbourf43b6982014-11-25 13:18:32 -07002013 ps.BindShaderEntitySlotToSampler(12, &sampler3);
2014
2015 XglPipelineObj pipelineobj(m_device);
2016 pipelineobj.AddShader(&vs);
2017 pipelineobj.AddShader(&ps);
2018
2019 XglDescriptorSetObj descriptorSet(m_device);
2020 descriptorSet.AttachImageView(&texture1);
2021 descriptorSet.AttachSampler(&sampler1);
2022 descriptorSet.AttachImageView(&texture2);
2023 descriptorSet.AttachSampler(&sampler2);
2024 descriptorSet.AttachImageView(&texture3);
2025 descriptorSet.AttachSampler(&sampler3);
2026
2027 m_memoryRefManager.AddMemoryRef(&texture1);
2028 m_memoryRefManager.AddMemoryRef(&texture2);
2029 m_memoryRefManager.AddMemoryRef(&texture3);
2030
Tony Barboura3953b82014-12-03 15:46:29 -07002031 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002032 QueueCommandBuffer(NULL, 0);
2033
2034}
2035
2036TEST_F(XglRenderTest, TriangleVSUniformBlock)
2037{
2038 // The expected result from this test is a blue triangle
2039
2040 static const char *vertShaderText =
2041 "#version 140\n"
2042 "#extension GL_ARB_separate_shader_objects : enable\n"
2043 "#extension GL_ARB_shading_language_420pack : enable\n"
2044 "layout (location = 0) out vec4 outColor;\n"
2045 "layout (std140, binding = 0) uniform bufferVals {\n"
2046 " vec4 red;\n"
2047 " vec4 green;\n"
2048 " vec4 blue;\n"
2049 " vec4 white;\n"
2050 "} myBufferVals;\n"
2051 "void main() {\n"
2052 " vec2 vertices[3];"
2053 " vertices[0] = vec2(-0.5, -0.5);\n"
2054 " vertices[1] = vec2( 0.5, -0.5);\n"
2055 " vertices[2] = vec2( 0.5, 0.5);\n"
2056 " outColor = myBufferVals.blue;\n"
2057 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2058 "}\n";
2059
2060 static const char *fragShaderText =
2061 "#version 140\n"
2062 "#extension GL_ARB_separate_shader_objects : enable\n"
2063 "#extension GL_ARB_shading_language_420pack : enable\n"
2064 "layout (location = 0) in vec4 inColor;\n"
2065 "void main() {\n"
2066 " gl_FragColor = inColor;\n"
2067 "}\n";
2068
2069 ASSERT_NO_FATAL_FAILURE(InitState());
2070 ASSERT_NO_FATAL_FAILURE(InitViewport());
2071
2072 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2073 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2074
2075 // Let's populate our buffer with the following:
2076 // vec4 red;
2077 // vec4 green;
2078 // vec4 blue;
2079 // vec4 white;
2080 const int valCount = 4 * 4;
2081 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2082 0.0, 1.0, 0.0, 1.0,
2083 0.0, 0.0, 1.0, 1.0,
2084 1.0, 1.0, 1.0, 1.0 };
2085
2086 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2087 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2088
2089 XglPipelineObj pipelineobj(m_device);
2090 pipelineobj.AddShader(&vs);
2091 pipelineobj.AddShader(&ps);
2092
2093 XglDescriptorSetObj descriptorSet(m_device);
2094 descriptorSet.AttachMemoryView(&colorBuffer);
2095
Tony Barboura3953b82014-12-03 15:46:29 -07002096 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002097 QueueCommandBuffer(NULL, 0);
2098
2099}
2100
2101TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2102{
2103 // This test allows the shader to select which buffer it is
2104 // pulling from using layout binding qualifier.
2105 // There are corresponding changes in the compiler stack that
2106 // will select the buffer using binding directly.
2107 // The binding number should match the slot number set up by
2108 // the application.
2109 // The expected result from this test is a purple 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 "void main() {\n"
2116 " vec2 vertices[3];"
2117 " vertices[0] = vec2(-0.5, -0.5);\n"
2118 " vertices[1] = vec2( 0.5, -0.5);\n"
2119 " vertices[2] = vec2( 0.5, 0.5);\n"
2120 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2121 "}\n";
2122
2123 static const char *fragShaderText =
2124 "#version 140\n"
2125 "#extension GL_ARB_separate_shader_objects : enable\n"
2126 "#extension GL_ARB_shading_language_420pack : enable\n"
2127 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2128 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2129 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2130 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2131 "void main() {\n"
2132 " gl_FragColor = myBlueVal.color;\n"
2133 " gl_FragColor += myRedVal.color;\n"
2134 "}\n";
2135
2136 ASSERT_NO_FATAL_FAILURE(InitState());
2137 ASSERT_NO_FATAL_FAILURE(InitViewport());
2138
2139 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2140 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2141
2142 // We're going to create a number of uniform buffers, and then allow
2143 // the shader to select which it wants to read from with a binding
2144
2145 // Let's populate the buffers with a single color each:
2146 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2147 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2148 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2149 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2150
2151 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2152 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2153 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2154 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2155
2156 const int redCount = sizeof(redVals) / sizeof(float);
2157 const int greenCount = sizeof(greenVals) / sizeof(float);
2158 const int blueCount = sizeof(blueVals) / sizeof(float);
2159 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2160
2161 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2162 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2163
2164 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2165 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2166
2167 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2168 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2169
2170 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2171 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2172
2173 XglPipelineObj pipelineobj(m_device);
2174 pipelineobj.AddShader(&vs);
2175 pipelineobj.AddShader(&ps);
2176
2177 XglDescriptorSetObj descriptorSet(m_device);
2178 descriptorSet.AttachMemoryView(&redBuffer);
2179 descriptorSet.AttachMemoryView(&greenBuffer);
2180 descriptorSet.AttachMemoryView(&blueBuffer);
2181 descriptorSet.AttachMemoryView(&whiteBuffer);
2182
Tony Barboura3953b82014-12-03 15:46:29 -07002183 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002184 QueueCommandBuffer(NULL, 0);
2185
2186}
2187
2188TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2189{
2190 // This test is the same as TriangleFSUniformBlockBinding, but
2191 // it does not provide an instance name.
2192 // The expected result from this test is a purple triangle
2193
2194 static const char *vertShaderText =
2195 "#version 140\n"
2196 "#extension GL_ARB_separate_shader_objects : enable\n"
2197 "#extension GL_ARB_shading_language_420pack : enable\n"
2198 "void main() {\n"
2199 " vec2 vertices[3];"
2200 " vertices[0] = vec2(-0.5, -0.5);\n"
2201 " vertices[1] = vec2( 0.5, -0.5);\n"
2202 " vertices[2] = vec2( 0.5, 0.5);\n"
2203 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2204 "}\n";
2205
2206 static const char *fragShaderText =
2207 "#version 430\n"
2208 "#extension GL_ARB_separate_shader_objects : enable\n"
2209 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002210 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2211 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2212 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2213 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
Cody Northrop68a10f62014-12-05 15:44:14 -07002214 "layout (location = 0) out vec4 outColor;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002215 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -07002216 " outColor = blue;\n"
2217 " outColor += red;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002218 "}\n";
2219 ASSERT_NO_FATAL_FAILURE(InitState());
2220 ASSERT_NO_FATAL_FAILURE(InitViewport());
2221
2222 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2223 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2224
2225 // We're going to create a number of uniform buffers, and then allow
2226 // the shader to select which it wants to read from with a binding
2227
2228 // Let's populate the buffers with a single color each:
2229 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2230 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2231 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2232 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2233
2234 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2235 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2236 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2237 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2238
2239 const int redCount = sizeof(redVals) / sizeof(float);
2240 const int greenCount = sizeof(greenVals) / sizeof(float);
2241 const int blueCount = sizeof(blueVals) / sizeof(float);
2242 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2243
2244 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2245 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2246
2247 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2248 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2249
2250 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2251 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2252
2253 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002254 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07002255
2256 XglPipelineObj pipelineobj(m_device);
2257 pipelineobj.AddShader(&vs);
2258 pipelineobj.AddShader(&ps);
2259
2260 XglDescriptorSetObj descriptorSet(m_device);
2261 descriptorSet.AttachMemoryView(&redBuffer);
2262 descriptorSet.AttachMemoryView(&greenBuffer);
2263 descriptorSet.AttachMemoryView(&blueBuffer);
2264 descriptorSet.AttachMemoryView(&whiteBuffer);
2265
Tony Barboura3953b82014-12-03 15:46:29 -07002266 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002267 QueueCommandBuffer(NULL, 0);
2268
2269}
2270
2271TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2272{
2273 static const char *vertShaderText =
2274 "#version 140\n"
2275 "#extension GL_ARB_separate_shader_objects : enable\n"
2276 "#extension GL_ARB_shading_language_420pack : enable\n"
2277 "layout (std140, binding=0) uniform bufferVals {\n"
2278 " mat4 mvp;\n"
2279 "} myBufferVals;\n"
2280 "layout (location=0) in vec4 pos;\n"
2281 "layout (location=0) out vec2 UV;\n"
2282 "void main() {\n"
2283 " vec2 positions[3];"
2284 " positions[0] = vec2( 0.0, 0.0);\n"
2285 " positions[1] = vec2( 0.25, 0.1);\n"
2286 " positions[2] = vec2( 0.1, 0.25);\n"
2287 " UV = positions[gl_VertexID % 3];\n"
2288 " gl_Position = myBufferVals.mvp * pos;\n"
2289 "}\n";
2290
2291 static const char *fragShaderText =
2292 "#version 140\n"
2293 "#extension GL_ARB_separate_shader_objects : enable\n"
2294 "#extension GL_ARB_shading_language_420pack : enable\n"
2295 "layout (binding=0) uniform sampler2D surface;\n"
2296 "layout (location=0) out vec4 outColor;\n"
2297 "layout (location=0) in vec2 UV;\n"
2298 "void main() {\n"
2299 " outColor= textureLod(surface, UV, 0.0);\n"
2300 "}\n";
2301 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2302
2303 glm::mat4 View = glm::lookAt(
2304 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2305 glm::vec3(0,0,0), // and looks at the origin
2306 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2307 );
2308
2309 glm::mat4 Model = glm::mat4(1.0f);
2310
2311 glm::mat4 MVP = Projection * View * Model;
2312
2313
2314 ASSERT_NO_FATAL_FAILURE(InitState());
2315 ASSERT_NO_FATAL_FAILURE(InitViewport());
2316 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2317
2318 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2319 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
Tony Barboure6152042014-12-10 17:40:15 -07002320 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07002321
2322
2323 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2324
2325 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2326 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2327 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2328 XglSamplerObj sampler(m_device);
2329 XglTextureObj texture(m_device);
2330
2331 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2332 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002333 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002334 ps.BindShaderEntitySlotToSampler(0, &sampler);
2335
2336 XglPipelineObj pipelineobj(m_device);
2337 pipelineobj.AddShader(&vs);
2338 pipelineobj.AddShader(&ps);
2339
2340 XglDescriptorSetObj descriptorSet(m_device);
2341
2342 descriptorSet.AttachMemoryView(&mvpBuffer);
2343 descriptorSet.AttachImageView(&texture);
2344 descriptorSet.AttachSampler(&sampler);
2345
2346 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2347 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2348 m_memoryRefManager.AddMemoryRef(&texture);
2349
2350 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2351 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2352 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2353 };
2354
2355 // this is the current description of g_vbData
2356 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2357 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2358 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2359 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2360 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2361 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2362 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2363 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2364 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2365
2366 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2367 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2368 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2369
Tony Barboura3953b82014-12-03 15:46:29 -07002370 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07002371
2372 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2373
2374}
Cody Northropd1ce7842014-12-09 11:17:01 -07002375
2376TEST_F(XglRenderTest, TriangleMixedSamplerUniformBlockBinding)
2377{
2378 // This test mixes binding slots of textures and buffers, ensuring
2379 // that sparse and overlapping assignments work.
Cody Northropa0410942014-12-09 13:59:39 -07002380 // The expected result from this test is a purple triangle, although
Cody Northropd1ce7842014-12-09 11:17:01 -07002381 // you can modify it to move the desired result around.
2382
2383 static const char *vertShaderText =
2384 "#version 140\n"
2385 "#extension GL_ARB_separate_shader_objects : enable\n"
2386 "#extension GL_ARB_shading_language_420pack : enable\n"
2387 "void main() {\n"
2388 " vec2 vertices[3];"
2389 " vertices[0] = vec2(-0.5, -0.5);\n"
2390 " vertices[1] = vec2( 0.5, -0.5);\n"
2391 " vertices[2] = vec2( 0.5, 0.5);\n"
2392 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2393 "}\n";
2394
2395 static const char *fragShaderText =
2396 "#version 430\n"
2397 "#extension GL_ARB_separate_shader_objects : enable\n"
2398 "#extension GL_ARB_shading_language_420pack : enable\n"
2399 "layout (binding = 0) uniform sampler2D surface0;\n"
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002400 "layout (binding = 9) uniform sampler2D surface1;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002401 "layout (binding = 2) uniform sampler2D surface2;\n"
Cody Northropa0410942014-12-09 13:59:39 -07002402 "layout (binding = 4) uniform sampler2D surface3;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002403
Cody Northropa0410942014-12-09 13:59:39 -07002404
2405 "layout (std140, binding = 10) uniform redVal { vec4 red; };"
2406 "layout (std140, binding = 15) uniform greenVal { vec4 green; };"
2407 "layout (std140, binding = 13) uniform blueVal { vec4 blue; };"
2408 "layout (std140, binding = 17) uniform whiteVal { vec4 white; };"
Cody Northropd1ce7842014-12-09 11:17:01 -07002409 "layout (location = 0) out vec4 outColor;\n"
2410 "void main() {\n"
Cody Northropa0410942014-12-09 13:59:39 -07002411 " outColor = red * vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002412 " outColor += white * vec4(0.00001);\n"
2413 " outColor += textureLod(surface2, vec2(0.5), 0.0)* vec4(0.00001);\n"
Cody Northropa0410942014-12-09 13:59:39 -07002414 " outColor += textureLod(surface1, vec2(0.0), 0.0);//* vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002415 "}\n";
2416 ASSERT_NO_FATAL_FAILURE(InitState());
2417 ASSERT_NO_FATAL_FAILURE(InitViewport());
2418
2419 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2420 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2421
Cody Northropd1ce7842014-12-09 11:17:01 -07002422 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2423 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2424 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2425 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2426
2427 const int redCount = sizeof(redVals) / sizeof(float);
2428 const int greenCount = sizeof(greenVals) / sizeof(float);
2429 const int blueCount = sizeof(blueVals) / sizeof(float);
2430 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2431
2432 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Cody Northropa0410942014-12-09 13:59:39 -07002433 ps.BindShaderEntitySlotToMemory(10, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002434
2435 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Cody Northropa0410942014-12-09 13:59:39 -07002436 ps.BindShaderEntitySlotToMemory(15, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002437
2438 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Cody Northropa0410942014-12-09 13:59:39 -07002439 ps.BindShaderEntitySlotToMemory(13, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002440
2441 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropa0410942014-12-09 13:59:39 -07002442 ps.BindShaderEntitySlotToMemory(17, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002443
2444 XglSamplerObj sampler0(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002445 XglTextureObj texture0(m_device); // Light Red
2446 texture0.ChangeColors(0xff800000,0xff800000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002447 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
Cody Northropd1ce7842014-12-09 11:17:01 -07002448 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2449 XglSamplerObj sampler2(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002450 XglTextureObj texture2(m_device); // Light Blue
2451 texture2.ChangeColors(0xff000080,0xff000080);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002452 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Cody Northropd1ce7842014-12-09 11:17:01 -07002453 ps.BindShaderEntitySlotToSampler(2, &sampler2);
2454 XglSamplerObj sampler4(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002455 XglTextureObj texture4(m_device); // Light Green
2456 texture4.ChangeColors(0xff008000,0xff008000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002457 ps.BindShaderEntitySlotToImage(4, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
Cody Northropd1ce7842014-12-09 11:17:01 -07002458 ps.BindShaderEntitySlotToSampler(4, &sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002459
2460 // NOTE: Bindings 1,3,5,7,8,9,11,12,14,16 work for this sampler, but 6 does not!!!
2461 // TODO: Get back here ASAP and understand why.
2462 XglSamplerObj sampler7(m_device);
2463 XglTextureObj texture7(m_device); // Red and Blue
2464 texture7.ChangeColors(0xffff00ff,0xffff00ff);
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002465 ps.BindShaderEntitySlotToImage(9, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2466 ps.BindShaderEntitySlotToSampler(9, &sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002467
2468
2469 XglPipelineObj pipelineobj(m_device);
2470 pipelineobj.AddShader(&vs);
2471 pipelineobj.AddShader(&ps);
2472
2473 XglDescriptorSetObj descriptorSet(m_device);
2474 descriptorSet.AttachMemoryView(&redBuffer);
2475 descriptorSet.AttachMemoryView(&greenBuffer);
2476 descriptorSet.AttachMemoryView(&blueBuffer);
2477 descriptorSet.AttachMemoryView(&whiteBuffer);
2478 descriptorSet.AttachImageView(&texture0);
2479 descriptorSet.AttachSampler(&sampler0);
2480 descriptorSet.AttachImageView(&texture2);
2481 descriptorSet.AttachSampler(&sampler2);
2482 descriptorSet.AttachImageView(&texture4);
2483 descriptorSet.AttachSampler(&sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002484 descriptorSet.AttachImageView(&texture7);
2485 descriptorSet.AttachSampler(&sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002486
2487 m_memoryRefManager.AddMemoryRef(&texture0);
2488 m_memoryRefManager.AddMemoryRef(&texture2);
2489 m_memoryRefManager.AddMemoryRef(&texture4);
Cody Northropa0410942014-12-09 13:59:39 -07002490 m_memoryRefManager.AddMemoryRef(&texture7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002491
2492
2493 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2494 QueueCommandBuffer(NULL, 0);
2495
2496}
2497
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002498TEST_F(XglRenderTest, TriangleMatchingSamplerUniformBlockBinding)
2499{
2500 // This test matches binding slots of textures and buffers, requiring
2501 // the driver to give them distinct number spaces.
2502 // The expected result from this test is a red triangle, although
2503 // you can modify it to move the desired result around.
2504
2505 static const char *vertShaderText =
2506 "#version 140\n"
2507 "#extension GL_ARB_separate_shader_objects : enable\n"
2508 "#extension GL_ARB_shading_language_420pack : enable\n"
2509 "void main() {\n"
2510 " vec2 vertices[3];"
2511 " vertices[0] = vec2(-0.5, -0.5);\n"
2512 " vertices[1] = vec2( 0.5, -0.5);\n"
2513 " vertices[2] = vec2( 0.5, 0.5);\n"
2514 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2515 "}\n";
2516
2517 static const char *fragShaderText =
2518 "#version 430\n"
2519 "#extension GL_ARB_separate_shader_objects : enable\n"
2520 "#extension GL_ARB_shading_language_420pack : enable\n"
2521 "layout (binding = 0) uniform sampler2D surface0;\n"
2522 "layout (binding = 1) uniform sampler2D surface1;\n"
2523 "layout (binding = 2) uniform sampler2D surface2;\n"
2524 "layout (binding = 3) uniform sampler2D surface3;\n"
2525 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2526 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2527 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2528 "layout (std140, binding = 3) uniform whiteVal { vec4 white; };"
2529 "layout (location = 0) out vec4 outColor;\n"
2530 "void main() {\n"
2531 " outColor = red;// * vec4(0.00001);\n"
2532 " outColor += white * vec4(0.00001);\n"
2533 " outColor += textureLod(surface1, vec2(0.5), 0.0)* vec4(0.00001);\n"
2534 " outColor += textureLod(surface3, vec2(0.0), 0.0)* vec4(0.00001);\n"
2535 "}\n";
2536 ASSERT_NO_FATAL_FAILURE(InitState());
2537 ASSERT_NO_FATAL_FAILURE(InitViewport());
2538
2539 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2540 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2541
2542 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2543 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2544 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2545 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2546
2547 const int redCount = sizeof(redVals) / sizeof(float);
2548 const int greenCount = sizeof(greenVals) / sizeof(float);
2549 const int blueCount = sizeof(blueVals) / sizeof(float);
2550 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2551
2552 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2553 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2554
2555 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2556 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2557
2558 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2559 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2560
2561 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2562 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2563
2564 XglSamplerObj sampler0(m_device);
2565 XglTextureObj texture0(m_device); // Light Red
2566 texture0.ChangeColors(0xff800000,0xff800000);
2567 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
2568 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2569 XglSamplerObj sampler2(m_device);
2570 XglTextureObj texture2(m_device); // Light Blue
2571 texture2.ChangeColors(0xff000080,0xff000080);
2572 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
2573 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2574 XglSamplerObj sampler4(m_device);
2575 XglTextureObj texture4(m_device); // Light Green
2576 texture4.ChangeColors(0xff008000,0xff008000);
2577 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
2578 ps.BindShaderEntitySlotToSampler(2, &sampler4);
2579 XglSamplerObj sampler7(m_device);
2580 XglTextureObj texture7(m_device); // Red and Blue
2581 texture7.ChangeColors(0xffff00ff,0xffff00ff);
2582 ps.BindShaderEntitySlotToImage(3, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2583 ps.BindShaderEntitySlotToSampler(3, &sampler7);
2584
2585
2586 XglPipelineObj pipelineobj(m_device);
2587 pipelineobj.AddShader(&vs);
2588 pipelineobj.AddShader(&ps);
2589
2590 XglDescriptorSetObj descriptorSet(m_device);
2591 descriptorSet.AttachMemoryView(&redBuffer);
2592 descriptorSet.AttachMemoryView(&greenBuffer);
2593 descriptorSet.AttachMemoryView(&blueBuffer);
2594 descriptorSet.AttachMemoryView(&whiteBuffer);
2595 descriptorSet.AttachImageView(&texture0);
2596 descriptorSet.AttachSampler(&sampler0);
2597 descriptorSet.AttachImageView(&texture2);
2598 descriptorSet.AttachSampler(&sampler2);
2599 descriptorSet.AttachImageView(&texture4);
2600 descriptorSet.AttachSampler(&sampler4);
2601 descriptorSet.AttachImageView(&texture7);
2602 descriptorSet.AttachSampler(&sampler7);
2603
2604 m_memoryRefManager.AddMemoryRef(&texture0);
2605 m_memoryRefManager.AddMemoryRef(&texture2);
2606 m_memoryRefManager.AddMemoryRef(&texture4);
2607 m_memoryRefManager.AddMemoryRef(&texture7);
2608
2609
2610 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2611 QueueCommandBuffer(NULL, 0);
2612
2613}
2614
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002615int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002616 int result;
2617
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002618 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002619 XglTestFramework::InitArgs(&argc, argv);
2620
Chia-I Wu7133fdc2014-12-15 23:57:34 +08002621 ::testing::AddGlobalTestEnvironment(new TestEnvironment);
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002622
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002623 result = RUN_ALL_TESTS();
2624
2625 XglTestFramework::Finish();
2626 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002627}