blob: 19964bd0bce7a16d7dab6db04971facbf7dd3b7e [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
Tony Barbour5ed79702015-01-07 14:31:52 -0700955 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
956 XglCommandBufferObj cmdBuffer(m_device);
957 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
958 cmdBuffer.AddRenderTarget(m_renderTargets[1]);
959 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
960 cmdBuffer.ClearAllBuffers(NULL, NULL);
961 cmdBuffer.BindAttachments(NULL);
962
963 cmdBuffer.BindState(m_stateRaster, m_stateViewport, m_colorBlend, m_stateDepthStencil, m_stateMsaa);
964 pipelineobj.CreateXGLPipeline(&descriptorSet);
965 cmdBuffer.BindPipeline(pipelineobj.GetPipelineHandle());
966 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
967
968 descriptorSet.CreateXGLDescriptorSet();
969 cmdBuffer.BindDescriptorSet(descriptorSet.GetDescriptorSetHandle());
970#ifdef DUMP_STATE_DOT
971 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
972 pDSDumpDot((char*)"triTest2.dot");
973#endif
974 // render triangle
975 cmdBuffer.Draw(0, 3, 0, 1);
976
977 // finalize recording of the command buffer
978 cmdBuffer.EndCommandBuffer();
979 cmdBuffer.QueueCommandBuffer(NULL, 0);
980
981 for (int i = 0; i < m_renderTargetCount; i++)
982 RecordImage(m_renderTargets[i]);
983
984 // GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
985 // QueueCommandBuffer(NULL, 0);
986
Chia-I Wue09d1a72014-12-05 10:32:23 +0800987}
988
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -0700989TEST_F(XglRenderTest, QuadWithIndexedVertexFetch)
990{
991 static const char *vertShaderText =
992 "#version 140\n"
993 "#extension GL_ARB_separate_shader_objects : enable\n"
994 "#extension GL_ARB_shading_language_420pack : enable\n"
995 "layout(location = 0) in vec4 pos;\n"
996 "layout(location = 1) in vec4 inColor;\n"
997 "layout(location = 0) out vec4 outColor;\n"
998 "void main() {\n"
999 " outColor = inColor;\n"
1000 " gl_Position = pos;\n"
1001 "}\n";
1002
1003
1004 static const char *fragShaderText =
1005 "#version 140\n"
1006 "#extension GL_ARB_separate_shader_objects : enable\n"
1007 "#extension GL_ARB_shading_language_420pack : enable\n"
1008 "layout(location = 0) in vec4 color;\n"
1009 "void main() {\n"
1010 " gl_FragColor = color;\n"
1011 "}\n";
1012
1013 const Vertex g_vbData[] =
1014 {
1015 // first tri
1016 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) }, // LL: black
1017 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1018 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1019
1020 // second tri
1021 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) }, // UL: green
1022 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) }, // LR: red
1023 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) }, // UR: yellow
1024 };
1025
1026 const uint16_t g_idxData[6] = {
1027 0, 1, 2,
1028 3, 4, 5,
1029 };
1030
1031 ASSERT_NO_FATAL_FAILURE(InitState());
1032 ASSERT_NO_FATAL_FAILURE(InitViewport());
1033
1034 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001035 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001036
1037 XglIndexBufferObj indexBuffer(m_device);
1038 indexBuffer.CreateAndInitBuffer(sizeof(g_idxData)/sizeof(g_idxData[0]), XGL_INDEX_16, g_idxData);
Tony Barboure6152042014-12-10 17:40:15 -07001039 indexBuffer.SetMemoryState(XGL_MEMORY_STATE_INDEX_DATA);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001040
1041 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1042 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1043
1044 XglPipelineObj pipelineobj(m_device);
1045 pipelineobj.AddShader(&vs);
1046 pipelineobj.AddShader(&ps);
1047
1048 XglDescriptorSetObj descriptorSet(m_device);
1049
1050 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1051 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1052 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1053 };
1054
1055 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1056 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1057 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1058 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1059 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1060 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1061 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1062 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1063 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1064
1065 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1066 pipelineobj.AddVertexInputBindings(&vi_binding,1);
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001067 pipelineobj.CreateXGLPipeline(&descriptorSet);
1068 descriptorSet.CreateXGLDescriptorSet();
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001069
1070 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001071 XglCommandBufferObj cmdBuffer(m_device);
1072 cmdBuffer.AddRenderTarget(m_renderTargets[0]);
1073 ASSERT_XGL_SUCCESS(cmdBuffer.BeginCommandBuffer(0));
1074 cmdBuffer.ClearAllBuffers(NULL, NULL);
1075 cmdBuffer.BindAttachments(NULL);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001076
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001077#ifdef DUMP_STATE_DOT
1078 DRAW_STATE_DUMP_DOT_FILE pDSDumpDot = (DRAW_STATE_DUMP_DOT_FILE)xglGetProcAddr(gpu(), (XGL_CHAR*)"drawStateDumpDotFile");
Tobin Ehlis266473d2014-12-16 17:34:50 -07001079 pDSDumpDot((char*)"triTest2.dot");
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001080#endif
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001081 cmdBuffer.BindState(m_stateRaster, m_stateViewport, m_colorBlend, m_stateDepthStencil, m_stateMsaa);
1082 cmdBuffer.BindPipeline(pipelineobj.GetPipelineHandle());
1083 cmdBuffer.BindDescriptorSet(descriptorSet.GetDescriptorSetHandle());
1084 cmdBuffer.BindVertexBuffer(&meshBuffer, 0, 0);
1085 cmdBuffer.BindIndexBuffer(&indexBuffer,0);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001086
1087 // render two triangles
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001088 cmdBuffer.DrawIndexed(0, 6, 0, 0, 1);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001089
1090 // finalize recording of the command buffer
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001091 cmdBuffer.EndCommandBuffer();
1092 cmdBuffer.QueueCommandBuffer(NULL, 0);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001093
Tony Barbourde9cf2e2014-12-17 11:16:05 -07001094 for (int i = 0; i < m_renderTargetCount; i++)
1095 RecordImage(m_renderTargets[i]);
Courtney Goeltzenleuchter4d6fbeb2014-12-04 15:45:16 -07001096
1097}
1098
GregF6bef1212014-12-02 15:41:44 -07001099TEST_F(XglRenderTest, GreyandRedCirclesonBlue)
1100{
1101 // This tests gl_FragCoord
Tony Barbourf43b6982014-11-25 13:18:32 -07001102
GregF6bef1212014-12-02 15:41:44 -07001103 static const char *vertShaderText =
1104 "#version 140\n"
1105 "#extension GL_ARB_separate_shader_objects : enable\n"
1106 "#extension GL_ARB_shading_language_420pack : enable\n"
1107 "layout (location = 0) in vec4 pos;\n"
1108 "layout (location = 0) out vec4 outColor;\n"
1109 "layout (location = 1) out vec4 outColor2;\n"
1110 "void main() {\n"
1111 " gl_Position = pos;\n"
1112 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1113 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1114 "}\n";
1115
1116 static const char *fragShaderText =
1117 //"#version 140\n"
1118 "#version 330\n"
1119 "#extension GL_ARB_separate_shader_objects : enable\n"
1120 "#extension GL_ARB_shading_language_420pack : enable\n"
1121 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1122 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1123 "layout (location = 0) in vec4 color;\n"
1124 "layout (location = 1) in vec4 color2;\n"
1125 "void main() {\n"
1126 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1127 " float dist_squared = dot(pos, pos);\n"
1128 " gl_FragColor = (dist_squared < 400.0)\n"
1129 " ? ((gl_FragCoord.y < 100.0) ? vec4(1.0, 0.0, 0.0, 0.0) : color)\n"
1130 " : color2;\n"
1131 "}\n";
1132
1133 ASSERT_NO_FATAL_FAILURE(InitState());
1134 ASSERT_NO_FATAL_FAILURE(InitViewport());
1135
1136 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001137 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001138
1139 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1140 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1141
1142 XglPipelineObj pipelineobj(m_device);
1143 pipelineobj.AddShader(&vs);
1144 pipelineobj.AddShader(&ps);
1145
1146 XglDescriptorSetObj descriptorSet(m_device);
1147 descriptorSet.AttachMemoryView(&meshBuffer);
1148
1149 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1150 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1151 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1152 };
1153
1154 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1155 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1156 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1157 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1158 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1159 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1160 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1161 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1162 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1163
1164 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1165 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1166 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1167
Tony Barbour09da2212014-12-03 16:13:23 -07001168 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001169 QueueCommandBuffer(NULL, 0);
1170
1171}
1172
1173TEST_F(XglRenderTest, RedCirclesonBlue)
1174{
1175 // This tests that we correctly handle unread fragment inputs
1176
1177 static const char *vertShaderText =
1178 "#version 140\n"
1179 "#extension GL_ARB_separate_shader_objects : enable\n"
1180 "#extension GL_ARB_shading_language_420pack : enable\n"
1181 "layout (location = 0) in vec4 pos;\n"
1182 "layout (location = 0) out vec4 outColor;\n"
1183 "layout (location = 1) out vec4 outColor2;\n"
1184 "void main() {\n"
1185 " gl_Position = pos;\n"
1186 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1187 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1188 "}\n";
1189
1190 static const char *fragShaderText =
1191 //"#version 140\n"
1192 "#version 330\n"
1193 "#extension GL_ARB_separate_shader_objects : enable\n"
1194 "#extension GL_ARB_shading_language_420pack : enable\n"
1195 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1196 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1197 "layout (location = 0) in vec4 color;\n"
1198 "layout (location = 1) in vec4 color2;\n"
1199 "void main() {\n"
1200 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1201 " float dist_squared = dot(pos, pos);\n"
1202 " gl_FragColor = (dist_squared < 400.0)\n"
1203 " ? vec4(1.0, 0.0, 0.0, 1.0)\n"
1204 " : color2;\n"
1205 "}\n";
1206
1207 ASSERT_NO_FATAL_FAILURE(InitState());
1208 ASSERT_NO_FATAL_FAILURE(InitViewport());
1209
1210 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001211 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001212
1213 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1214 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1215
1216 XglPipelineObj pipelineobj(m_device);
1217 pipelineobj.AddShader(&vs);
1218 pipelineobj.AddShader(&ps);
1219
1220 XglDescriptorSetObj descriptorSet(m_device);
1221 descriptorSet.AttachMemoryView(&meshBuffer);
1222
1223 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1224 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1225 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1226 };
1227
1228 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1229 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1230 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1231 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1232 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1233 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1234 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1235 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1236 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1237
1238 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1239 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1240 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1241
Tony Barbour09da2212014-12-03 16:13:23 -07001242 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001243 QueueCommandBuffer(NULL, 0);
1244
1245}
1246
1247TEST_F(XglRenderTest, GreyCirclesonBlueFade)
1248{
1249 // This tests reading gl_ClipDistance from FS
1250
1251 static const char *vertShaderText =
1252 "#version 330\n"
1253 "#extension GL_ARB_separate_shader_objects : enable\n"
1254 "#extension GL_ARB_shading_language_420pack : enable\n"
1255 "out gl_PerVertex {\n"
1256 " vec4 gl_Position;\n"
1257 " float gl_ClipDistance[1];\n"
1258 "};\n"
1259 "layout (location = 0) in vec4 pos;\n"
1260 "layout (location = 0) out vec4 outColor;\n"
1261 "layout (location = 1) out vec4 outColor2;\n"
1262 "void main() {\n"
1263 " gl_Position = pos;\n"
1264 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1265 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1266 " float dists[3];\n"
1267 " dists[0] = 0.0;\n"
1268 " dists[1] = 1.0;\n"
1269 " dists[2] = 1.0;\n"
1270 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1271 "}\n";
1272
1273
1274 static const char *fragShaderText =
1275 //"#version 140\n"
1276 "#version 330\n"
1277 "#extension GL_ARB_separate_shader_objects : enable\n"
1278 "#extension GL_ARB_shading_language_420pack : enable\n"
1279 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1280 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1281 "layout (location = 0) in vec4 color;\n"
1282 "layout (location = 1) in vec4 color2;\n"
1283 "void main() {\n"
1284 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1285 " float dist_squared = dot(pos, pos);\n"
1286 " gl_FragColor = (dist_squared < 400.0)\n"
1287 " ? color * gl_ClipDistance[0]\n"
1288 " : color2;\n"
1289 "}\n";
1290
1291 ASSERT_NO_FATAL_FAILURE(InitState());
1292 ASSERT_NO_FATAL_FAILURE(InitViewport());
1293
1294 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001295 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF6bef1212014-12-02 15:41:44 -07001296
1297 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1298 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1299
1300 XglPipelineObj pipelineobj(m_device);
1301 pipelineobj.AddShader(&vs);
1302 pipelineobj.AddShader(&ps);
1303
1304 XglDescriptorSetObj descriptorSet(m_device);
1305 descriptorSet.AttachMemoryView(&meshBuffer);
1306
1307 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1308 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1309 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1310 };
1311
1312 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1313 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1314 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1315 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1316 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1317 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1318 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1319 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1320 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1321
1322 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1323 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1324 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1325
Tony Barboura3953b82014-12-03 15:46:29 -07001326 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF6bef1212014-12-02 15:41:44 -07001327 QueueCommandBuffer(NULL, 0);
1328
1329}
Tony Barbourf43b6982014-11-25 13:18:32 -07001330
GregF7a23c792014-12-02 17:19:34 -07001331TEST_F(XglRenderTest, GreyCirclesonBlueDiscard)
1332{
1333 static const char *vertShaderText =
1334 "#version 140\n"
1335 "#extension GL_ARB_separate_shader_objects : enable\n"
1336 "#extension GL_ARB_shading_language_420pack : enable\n"
1337 "layout (location = 0) in vec4 pos;\n"
1338 "layout (location = 0) out vec4 outColor;\n"
1339 "layout (location = 1) out vec4 outColor2;\n"
1340 "void main() {\n"
1341 " gl_Position = pos;\n"
1342 " outColor = vec4(0.9, 0.9, 0.9, 1.0);\n"
1343 " outColor2 = vec4(0.2, 0.2, 0.4, 1.0);\n"
1344 "}\n";
1345
1346
1347 static const char *fragShaderText =
1348 //"#version 140\n"
1349 "#version 330\n"
1350 "#extension GL_ARB_separate_shader_objects : enable\n"
1351 "#extension GL_ARB_shading_language_420pack : enable\n"
1352 //"#extension GL_ARB_fragment_coord_conventions : enable\n"
1353 //"layout (pixel_center_integer) in vec4 gl_FragCoord;\n"
1354 "layout (location = 0) in vec4 color;\n"
1355 "layout (location = 1) in vec4 color2;\n"
1356 "void main() {\n"
1357 " vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);\n"
1358 " float dist_squared = dot(pos, pos);\n"
1359 " if (dist_squared < 100.0)\n"
1360 " discard;\n"
1361 " gl_FragColor = (dist_squared < 400.0)\n"
1362 " ? color\n"
1363 " : color2;\n"
1364 "}\n";
1365
1366 ASSERT_NO_FATAL_FAILURE(InitState());
1367 ASSERT_NO_FATAL_FAILURE(InitViewport());
1368
1369 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001370 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
GregF7a23c792014-12-02 17:19:34 -07001371
1372 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1373 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1374
1375 XglPipelineObj pipelineobj(m_device);
1376 pipelineobj.AddShader(&vs);
1377 pipelineobj.AddShader(&ps);
1378
1379 XglDescriptorSetObj descriptorSet(m_device);
1380 descriptorSet.AttachMemoryView(&meshBuffer);
1381
1382 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1383 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1384 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1385 };
1386
1387 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1388 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1389 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1390 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1391 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1392 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1393 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1394 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1395 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1396
1397 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1398 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1399 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1400
Tony Barbour09da2212014-12-03 16:13:23 -07001401 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
GregF7a23c792014-12-02 17:19:34 -07001402 QueueCommandBuffer(NULL, 0);
1403
1404}
1405
1406
Courtney Goeltzenleuchter3d10c9c2014-10-27 13:06:08 -06001407TEST_F(XglRenderTest, TriangleVSUniform)
Cody Northrop7a1f0462014-10-10 14:49:36 -06001408{
1409 static const char *vertShaderText =
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001410 "#version 140\n"
1411 "#extension GL_ARB_separate_shader_objects : enable\n"
1412 "#extension GL_ARB_shading_language_420pack : enable\n"
1413 "\n"
1414 "layout(binding = 0) uniform buf {\n"
1415 " mat4 MVP;\n"
1416 "} ubuf;\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001417 "void main() {\n"
1418 " vec2 vertices[3];"
1419 " vertices[0] = vec2(-0.5, -0.5);\n"
1420 " vertices[1] = vec2( 0.5, -0.5);\n"
1421 " vertices[2] = vec2( 0.5, 0.5);\n"
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001422 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001423 "}\n";
1424
1425 static const char *fragShaderText =
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001426 "#version 130\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001427 "void main() {\n"
Cody Northrop78eac042014-10-10 15:45:00 -06001428 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
Cody Northrop7a1f0462014-10-10 14:49:36 -06001429 "}\n";
1430
Tony Barbourf43b6982014-11-25 13:18:32 -07001431 ASSERT_NO_FATAL_FAILURE(InitState());
1432 ASSERT_NO_FATAL_FAILURE(InitViewport());
1433
Courtney Goeltzenleuchter34b81772014-10-10 18:04:39 -06001434 // Create identity matrix
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001435 glm::mat4 Projection = glm::mat4(1.0f);
1436 glm::mat4 View = glm::mat4(1.0f);
1437 glm::mat4 Model = glm::mat4(1.0f);
1438 glm::mat4 MVP = Projection * View * Model;
1439 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1440
Tony Barbourf43b6982014-11-25 13:18:32 -07001441 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1442 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1443 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001444
Tony Barbourf43b6982014-11-25 13:18:32 -07001445 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1446
1447 XglPipelineObj pipelineobj(m_device);
1448 pipelineobj.AddShader(&vs);
1449 pipelineobj.AddShader(&ps);
1450
1451 // Create descriptor set and attach the constant buffer to it
1452 XglDescriptorSetObj descriptorSet(m_device);
1453 descriptorSet.AttachMemoryView(&MVPBuffer);
1454
1455 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1456
Tony Barboura3953b82014-12-03 15:46:29 -07001457 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001458 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1459
Tony Barbour09da2212014-12-03 16:13:23 -07001460 RotateTriangleVSUniform(Projection, View, Model, &MVPBuffer);
Courtney Goeltzenleuchterc3f4ac82014-10-27 09:48:52 -06001461}
1462
Tony Barbourf43b6982014-11-25 13:18:32 -07001463TEST_F(XglRenderTest, MixTriangle)
1464{
1465 // This tests location applied to varyings. Notice that we have switched foo
1466 // and bar in the FS. The triangle should be blended with red, green and blue
1467 // corners.
1468 static const char *vertShaderText =
1469 "#version 140\n"
1470 "#extension GL_ARB_separate_shader_objects : enable\n"
1471 "#extension GL_ARB_shading_language_420pack : enable\n"
1472 "layout (location=0) out vec4 bar;\n"
1473 "layout (location=1) out vec4 foo;\n"
1474 "layout (location=2) out float scale;\n"
1475 "vec2 vertices[3];\n"
1476 "void main() {\n"
1477 " vertices[0] = vec2(-1.0, -1.0);\n"
1478 " vertices[1] = vec2( 1.0, -1.0);\n"
1479 " vertices[2] = vec2( 0.0, 1.0);\n"
1480 "vec4 colors[3];\n"
1481 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1482 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1483 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1484 " foo = colors[gl_VertexID % 3];\n"
1485 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1486 " scale = 1.0;\n"
1487 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1488 "}\n";
1489
1490 static const char *fragShaderText =
1491 "#version 140\n"
1492 "#extension GL_ARB_separate_shader_objects : enable\n"
1493 "#extension GL_ARB_shading_language_420pack : enable\n"
1494 "layout (location = 1) in vec4 bar;\n"
1495 "layout (location = 0) in vec4 foo;\n"
1496 "layout (location = 2) in float scale;\n"
1497 "void main() {\n"
1498 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1499 "}\n";
1500
1501 ASSERT_NO_FATAL_FAILURE(InitState());
1502 ASSERT_NO_FATAL_FAILURE(InitViewport());
1503
1504 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1505 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1506
1507 XglPipelineObj pipelineobj(m_device);
1508 pipelineobj.AddShader(&vs);
1509 pipelineobj.AddShader(&ps);
1510
1511 XglDescriptorSetObj descriptorSet(m_device);
1512
Tony Barboura3953b82014-12-03 15:46:29 -07001513 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001514 QueueCommandBuffer(NULL, 0);
1515}
1516
1517TEST_F(XglRenderTest, TriVertFetchAndVertID)
1518{
1519 // This tests that attributes work in the presence of gl_VertexID
1520
1521 static const char *vertShaderText =
1522 "#version 140\n"
1523 "#extension GL_ARB_separate_shader_objects : enable\n"
1524 "#extension GL_ARB_shading_language_420pack : enable\n"
1525 //XYZ1( -1, -1, -1 )
1526 "layout (location = 0) in vec4 pos;\n"
1527 //XYZ1( 0.f, 0.f, 0.f )
1528 "layout (location = 1) in vec4 inColor;\n"
1529 "layout (location = 0) out vec4 outColor;\n"
1530 "void main() {\n"
1531 " outColor = inColor;\n"
1532 " vec4 vertices[3];"
1533 " vertices[gl_VertexID % 3] = pos;\n"
1534 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1535 "}\n";
1536
1537
1538 static const char *fragShaderText =
1539 "#version 140\n"
1540 "#extension GL_ARB_separate_shader_objects : enable\n"
1541 "#extension GL_ARB_shading_language_420pack : enable\n"
1542 "layout (location = 0) in vec4 color;\n"
1543 "void main() {\n"
1544 " gl_FragColor = color;\n"
1545 "}\n";
1546
1547 ASSERT_NO_FATAL_FAILURE(InitState());
1548 ASSERT_NO_FATAL_FAILURE(InitViewport());
1549
1550 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001551 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001552
1553 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1554 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1555
1556 XglPipelineObj pipelineobj(m_device);
1557 pipelineobj.AddShader(&vs);
1558 pipelineobj.AddShader(&ps);
1559
1560 XglDescriptorSetObj descriptorSet(m_device);
1561 descriptorSet.AttachMemoryView(&meshBuffer);
1562
1563 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1564 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1565 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1566 };
1567
1568 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1569 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1570 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1571 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1572 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1573 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1574 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1575 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1576 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1577
1578 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1579 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1580 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1581
Tony Barboura3953b82014-12-03 15:46:29 -07001582 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001583 QueueCommandBuffer(NULL, 0);
1584}
1585
1586TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1587{
1588 // This tests that attributes work in the presence of gl_VertexID
1589 // and a dead attribute in position 0. Draws a triangle with yellow,
1590 // red and green corners, starting at top and going clockwise.
1591
1592 static const char *vertShaderText =
1593 "#version 140\n"
1594 "#extension GL_ARB_separate_shader_objects : enable\n"
1595 "#extension GL_ARB_shading_language_420pack : enable\n"
1596 //XYZ1( -1, -1, -1 )
1597 "layout (location = 0) in vec4 pos;\n"
1598 //XYZ1( 0.f, 0.f, 0.f )
1599 "layout (location = 1) in vec4 inColor;\n"
1600 "layout (location = 0) out vec4 outColor;\n"
1601 "void main() {\n"
1602 " outColor = inColor;\n"
1603 " vec2 vertices[3];"
1604 " vertices[0] = vec2(-1.0, -1.0);\n"
1605 " vertices[1] = vec2( 1.0, -1.0);\n"
1606 " vertices[2] = vec2( 0.0, 1.0);\n"
1607 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1608 "}\n";
1609
1610
1611 static const char *fragShaderText =
1612 "#version 140\n"
1613 "#extension GL_ARB_separate_shader_objects : enable\n"
1614 "#extension GL_ARB_shading_language_420pack : enable\n"
1615 "layout (location = 0) in vec4 color;\n"
1616 "void main() {\n"
1617 " gl_FragColor = color;\n"
1618 "}\n";
1619
1620 ASSERT_NO_FATAL_FAILURE(InitState());
1621 ASSERT_NO_FATAL_FAILURE(InitViewport());
1622
1623 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barboure6152042014-12-10 17:40:15 -07001624 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07001625
1626 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1627 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1628
1629 XglPipelineObj pipelineobj(m_device);
1630 pipelineobj.AddShader(&vs);
1631 pipelineobj.AddShader(&ps);
1632
1633 XglDescriptorSetObj descriptorSet(m_device);
1634 descriptorSet.AttachMemoryView(&meshBuffer);
1635
1636 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1637 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1638 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1639 };
1640
1641 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1642 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1643 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1644 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1645 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1646 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1647 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1648 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1649 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1650
1651 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1652 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1653 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1654
Tony Barbour09da2212014-12-03 16:13:23 -07001655 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 2);
Tony Barbourf43b6982014-11-25 13:18:32 -07001656 QueueCommandBuffer(NULL, 0);
1657
1658}
1659
1660TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001661{
1662 static const char *vertShaderText =
1663 "#version 140\n"
1664 "layout (std140) uniform bufferVals {\n"
1665 " mat4 mvp;\n"
1666 "} myBufferVals;\n"
1667 "in vec4 pos;\n"
1668 "in vec4 inColor;\n"
1669 "out vec4 outColor;\n"
1670 "void main() {\n"
1671 " outColor = inColor;\n"
1672 " gl_Position = myBufferVals.mvp * pos;\n"
1673 "}\n";
1674
1675 static const char *fragShaderText =
1676 "#version 130\n"
1677 "in vec4 color;\n"
1678 "void main() {\n"
1679 " gl_FragColor = color;\n"
1680 "}\n";
Tony Barbourf43b6982014-11-25 13:18:32 -07001681 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001682
Tony Barbourf43b6982014-11-25 13:18:32 -07001683 glm::mat4 View = glm::lookAt(
1684 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1685 glm::vec3(0,0,0), // and looks at the origin
1686 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1687 );
1688
1689 glm::mat4 Model = glm::mat4(1.0f);
1690
1691 glm::mat4 MVP = Projection * View * Model;
1692
1693 ASSERT_NO_FATAL_FAILURE(InitState());
1694 ASSERT_NO_FATAL_FAILURE(InitViewport());
1695 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1696
1697 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1698 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1699
1700 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1701
1702 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1703 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1704 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1705
1706 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
1707
1708 XglPipelineObj pipelineobj(m_device);
1709 pipelineobj.AddShader(&vs);
1710 pipelineobj.AddShader(&ps);
1711
1712 XglDescriptorSetObj descriptorSet(m_device);
1713 descriptorSet.AttachMemoryView(&MVPBuffer);
1714
1715 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1716 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
1717
1718 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1719 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1720 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1721 };
1722
1723 // this is the current description of g_vbData
1724 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1725 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1726 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1727 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1728 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1729 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1730 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1731 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1732 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1733
1734 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1735 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1736 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1737
Tony Barboura3953b82014-12-03 15:46:29 -07001738 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07001739
1740 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1741
Courtney Goeltzenleuchterd0556292014-10-20 16:33:15 -06001742}
1743
Tony Barbourf43b6982014-11-25 13:18:32 -07001744TEST_F(XglRenderTest, VSTexture)
1745{
1746 // The expected result from this test is a green and red triangle;
1747 // one red vertex on the left, two green vertices on the right.
1748 static const char *vertShaderText =
1749 "#version 130\n"
1750 "out vec4 texColor;\n"
1751 "uniform sampler2D surface;\n"
1752 "void main() {\n"
1753 " vec2 vertices[3];"
1754 " vertices[0] = vec2(-0.5, -0.5);\n"
1755 " vertices[1] = vec2( 0.5, -0.5);\n"
1756 " vertices[2] = vec2( 0.5, 0.5);\n"
1757 " vec2 positions[3];"
1758 " positions[0] = vec2( 0.0, 0.0);\n"
1759 " positions[1] = vec2( 0.25, 0.1);\n"
1760 " positions[2] = vec2( 0.1, 0.25);\n"
1761 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1762 " texColor = textureLod(surface, samplePos, 0.0);\n"
1763 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1764 "}\n";
1765
1766 static const char *fragShaderText =
1767 "#version 130\n"
1768 "in vec4 texColor;\n"
1769 "void main() {\n"
1770 " gl_FragColor = texColor;\n"
1771 "}\n";
1772
1773 ASSERT_NO_FATAL_FAILURE(InitState());
1774 ASSERT_NO_FATAL_FAILURE(InitViewport());
1775
1776 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1777 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1778 XglSamplerObj sampler(m_device);
1779 XglTextureObj texture(m_device);
1780
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001781 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001782 vs.BindShaderEntitySlotToSampler(0, &sampler);
1783
1784 XglPipelineObj pipelineobj(m_device);
1785 pipelineobj.AddShader(&vs);
1786 pipelineobj.AddShader(&ps);
1787
1788 XglDescriptorSetObj descriptorSet(m_device);
1789 descriptorSet.AttachImageView(&texture);
1790 descriptorSet.AttachSampler(&sampler);
1791
1792 m_memoryRefManager.AddMemoryRef(&texture);
1793
Tony Barboura3953b82014-12-03 15:46:29 -07001794 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001795 QueueCommandBuffer(NULL, 0);
1796
1797}
1798TEST_F(XglRenderTest, TexturedTriangle)
1799{
1800 // The expected result from this test is a red and green checkered triangle
1801 static const char *vertShaderText =
1802 "#version 140\n"
1803 "#extension GL_ARB_separate_shader_objects : enable\n"
1804 "#extension GL_ARB_shading_language_420pack : enable\n"
1805 "layout (location = 0) out vec2 samplePos;\n"
1806 "void main() {\n"
1807 " vec2 vertices[3];"
1808 " vertices[0] = vec2(-0.5, -0.5);\n"
1809 " vertices[1] = vec2( 0.5, -0.5);\n"
1810 " vertices[2] = vec2( 0.5, 0.5);\n"
1811 " vec2 positions[3];"
1812 " positions[0] = vec2( 0.0, 0.0);\n"
1813 " positions[1] = vec2( 1.0, 0.0);\n"
1814 " positions[2] = vec2( 1.0, 1.0);\n"
1815 " samplePos = positions[gl_VertexID % 3];\n"
1816 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1817 "}\n";
1818
1819 static const char *fragShaderText =
1820 "#version 140\n"
1821 "#extension GL_ARB_separate_shader_objects : enable\n"
1822 "#extension GL_ARB_shading_language_420pack : enable\n"
1823 "layout (location = 0) in vec2 samplePos;\n"
1824 "layout (binding = 0) uniform sampler2D surface;\n"
1825 "layout (location=0) out vec4 outColor;\n"
1826 "void main() {\n"
1827 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1828 " outColor = texColor;\n"
1829 "}\n";
1830
1831 ASSERT_NO_FATAL_FAILURE(InitState());
1832 ASSERT_NO_FATAL_FAILURE(InitViewport());
1833
1834 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1835 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1836 XglSamplerObj sampler(m_device);
1837 XglTextureObj texture(m_device);
1838
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001839 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001840 ps.BindShaderEntitySlotToSampler(0, &sampler);
1841
1842 XglPipelineObj pipelineobj(m_device);
1843 pipelineobj.AddShader(&vs);
1844 pipelineobj.AddShader(&ps);
1845
1846 XglDescriptorSetObj descriptorSet(m_device);
1847 descriptorSet.AttachImageView(&texture);
1848 descriptorSet.AttachSampler(&sampler);
1849
1850 m_memoryRefManager.AddMemoryRef(&texture);
1851
Tony Barboura3953b82014-12-03 15:46:29 -07001852 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001853 QueueCommandBuffer(NULL, 0);
1854}
1855TEST_F(XglRenderTest, TexturedTriangleClip)
1856{
1857 // The expected result from this test is a red and green checkered triangle
1858 static const char *vertShaderText =
1859 "#version 330\n"
1860 "#extension GL_ARB_separate_shader_objects : enable\n"
1861 "#extension GL_ARB_shading_language_420pack : enable\n"
1862 "layout (location = 0) out vec2 samplePos;\n"
1863 "out gl_PerVertex {\n"
1864 " vec4 gl_Position;\n"
1865 " float gl_ClipDistance[1];\n"
1866 "};\n"
1867 "void main() {\n"
1868 " vec2 vertices[3];"
1869 " vertices[0] = vec2(-0.5, -0.5);\n"
1870 " vertices[1] = vec2( 0.5, -0.5);\n"
1871 " vertices[2] = vec2( 0.5, 0.5);\n"
1872 " vec2 positions[3];"
1873 " positions[0] = vec2( 0.0, 0.0);\n"
1874 " positions[1] = vec2( 1.0, 0.0);\n"
1875 " positions[2] = vec2( 1.0, 1.0);\n"
1876 " float dists[3];\n"
1877 " dists[0] = 1.0;\n"
1878 " dists[1] = 1.0;\n"
1879 " dists[2] = -1.0;\n"
1880 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1881 " samplePos = positions[gl_VertexID % 3];\n"
1882 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1883 "}\n";
1884
1885 static const char *fragShaderText =
1886 "#version 140\n"
1887 "#extension GL_ARB_separate_shader_objects : enable\n"
1888 "#extension GL_ARB_shading_language_420pack : enable\n"
1889 "layout (location = 0) in vec2 samplePos;\n"
1890 "layout (binding = 0) uniform sampler2D surface;\n"
1891 "layout (location=0) out vec4 outColor;\n"
1892 "void main() {\n"
1893 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1894 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1895 " outColor = texColor;\n"
1896 "}\n";
1897
1898
1899 ASSERT_NO_FATAL_FAILURE(InitState());
1900 ASSERT_NO_FATAL_FAILURE(InitViewport());
1901
1902 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1903 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1904 XglSamplerObj sampler(m_device);
1905 XglTextureObj texture(m_device);
1906
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001907 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001908 ps.BindShaderEntitySlotToSampler(0, &sampler);
1909
1910 XglPipelineObj pipelineobj(m_device);
1911 pipelineobj.AddShader(&vs);
1912 pipelineobj.AddShader(&ps);
1913
1914 XglDescriptorSetObj descriptorSet(m_device);
1915 descriptorSet.AttachImageView(&texture);
1916 descriptorSet.AttachSampler(&sampler);
1917
1918 m_memoryRefManager.AddMemoryRef(&texture);
1919
Tony Barboura3953b82014-12-03 15:46:29 -07001920 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001921 QueueCommandBuffer(NULL, 0);
1922}
1923TEST_F(XglRenderTest, FSTriangle)
1924{
1925 // The expected result from this test is a red and green checkered triangle
1926 static const char *vertShaderText =
1927 "#version 140\n"
1928 "#extension GL_ARB_separate_shader_objects : enable\n"
1929 "#extension GL_ARB_shading_language_420pack : enable\n"
1930 "layout (location = 0) out vec2 samplePos;\n"
1931 "void main() {\n"
1932 " vec2 vertices[3];"
1933 " vertices[0] = vec2(-0.5, -0.5);\n"
1934 " vertices[1] = vec2( 0.5, -0.5);\n"
1935 " vertices[2] = vec2( 0.5, 0.5);\n"
1936 " vec2 positions[3];"
1937 " positions[0] = vec2( 0.0, 0.0);\n"
1938 " positions[1] = vec2( 1.0, 0.0);\n"
1939 " positions[2] = vec2( 1.0, 1.0);\n"
1940 " samplePos = positions[gl_VertexID % 3];\n"
1941 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1942 "}\n";
1943
1944 static const char *fragShaderText =
1945 "#version 140\n"
1946 "#extension GL_ARB_separate_shader_objects : enable\n"
1947 "#extension GL_ARB_shading_language_420pack : enable\n"
1948 "layout (location = 0) in vec2 samplePos;\n"
1949 "layout (binding = 0) uniform sampler2D surface;\n"
1950 "layout (location=0) out vec4 outColor;\n"
1951 "void main() {\n"
1952 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1953 " outColor = texColor;\n"
1954 "}\n";
1955
1956 ASSERT_NO_FATAL_FAILURE(InitState());
1957 ASSERT_NO_FATAL_FAILURE(InitViewport());
1958
1959 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
1960 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
1961 XglSamplerObj sampler(m_device);
1962 XglTextureObj texture(m_device);
1963
Cody Northrop5fcacbc2014-12-09 19:08:54 -07001964 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07001965 ps.BindShaderEntitySlotToSampler(0, &sampler);
1966
1967 XglPipelineObj pipelineobj(m_device);
1968 pipelineobj.AddShader(&vs);
1969 pipelineobj.AddShader(&ps);
1970
1971 XglDescriptorSetObj descriptorSet(m_device);
1972 descriptorSet.AttachImageView(&texture);
1973 descriptorSet.AttachSampler(&sampler);
1974
1975 m_memoryRefManager.AddMemoryRef(&texture);
1976
Tony Barboura3953b82014-12-03 15:46:29 -07001977 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07001978 QueueCommandBuffer(NULL, 0);
1979}
1980TEST_F(XglRenderTest, SamplerBindingsTriangle)
1981{
1982 // This test sets bindings on the samplers
1983 // For now we are asserting that sampler and texture pairs
1984 // march in lock step, and are set via GLSL binding. This can
1985 // and will probably change.
1986 // The sampler bindings should match the sampler and texture slot
1987 // number set up by the application.
1988 // This test will result in a blue triangle
1989 static const char *vertShaderText =
1990 "#version 140\n"
1991 "#extension GL_ARB_separate_shader_objects : enable\n"
1992 "#extension GL_ARB_shading_language_420pack : enable\n"
1993 "layout (location = 0) out vec4 samplePos;\n"
1994 "void main() {\n"
1995 " vec2 vertices[3];"
1996 " vertices[0] = vec2(-0.5, -0.5);\n"
1997 " vertices[1] = vec2( 0.5, -0.5);\n"
1998 " vertices[2] = vec2( 0.5, 0.5);\n"
1999 " vec2 positions[3];"
2000 " positions[0] = vec2( 0.0, 0.0);\n"
2001 " positions[1] = vec2( 1.0, 0.0);\n"
2002 " positions[2] = vec2( 1.0, 1.0);\n"
2003 " samplePos = vec4(positions[gl_VertexID % 3], 0.0, 0.0);\n"
2004 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2005 "}\n";
2006
2007 static const char *fragShaderText =
2008 "#version 140\n"
2009 "#extension GL_ARB_separate_shader_objects : enable\n"
2010 "#extension GL_ARB_shading_language_420pack : enable\n"
2011 "layout (location = 0) in vec4 samplePos;\n"
2012 "layout (binding = 0) uniform sampler2D surface0;\n"
2013 "layout (binding = 1) uniform sampler2D surface1;\n"
2014 "layout (binding = 12) uniform sampler2D surface2;\n"
2015 "void main() {\n"
2016 " gl_FragColor = textureLod(surface2, samplePos.xy, 0.0);\n"
2017 "}\n";
2018
2019 ASSERT_NO_FATAL_FAILURE(InitState());
2020 ASSERT_NO_FATAL_FAILURE(InitViewport());
2021
2022 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2023 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2024
2025 XglSamplerObj sampler1(m_device);
2026 XglSamplerObj sampler2(m_device);
2027 XglSamplerObj sampler3(m_device);
2028
2029 XglTextureObj texture1(m_device); // Red
2030 texture1.ChangeColors(0xffff0000,0xffff0000);
2031 XglTextureObj texture2(m_device); // Green
2032 texture2.ChangeColors(0xff00ff00,0xff00ff00);
2033 XglTextureObj texture3(m_device); // Blue
2034 texture3.ChangeColors(0xff0000ff,0xff0000ff);
2035
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002036 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002037 ps.BindShaderEntitySlotToSampler(0, &sampler1);
2038
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002039 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Tony Barbourf43b6982014-11-25 13:18:32 -07002040 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2041
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002042 ps.BindShaderEntitySlotToImage(12, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture3);
Tony Barbourf43b6982014-11-25 13:18:32 -07002043 ps.BindShaderEntitySlotToSampler(12, &sampler3);
2044
2045 XglPipelineObj pipelineobj(m_device);
2046 pipelineobj.AddShader(&vs);
2047 pipelineobj.AddShader(&ps);
2048
2049 XglDescriptorSetObj descriptorSet(m_device);
2050 descriptorSet.AttachImageView(&texture1);
2051 descriptorSet.AttachSampler(&sampler1);
2052 descriptorSet.AttachImageView(&texture2);
2053 descriptorSet.AttachSampler(&sampler2);
2054 descriptorSet.AttachImageView(&texture3);
2055 descriptorSet.AttachSampler(&sampler3);
2056
2057 m_memoryRefManager.AddMemoryRef(&texture1);
2058 m_memoryRefManager.AddMemoryRef(&texture2);
2059 m_memoryRefManager.AddMemoryRef(&texture3);
2060
Tony Barboura3953b82014-12-03 15:46:29 -07002061 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002062 QueueCommandBuffer(NULL, 0);
2063
2064}
2065
2066TEST_F(XglRenderTest, TriangleVSUniformBlock)
2067{
2068 // The expected result from this test is a blue triangle
2069
2070 static const char *vertShaderText =
2071 "#version 140\n"
2072 "#extension GL_ARB_separate_shader_objects : enable\n"
2073 "#extension GL_ARB_shading_language_420pack : enable\n"
2074 "layout (location = 0) out vec4 outColor;\n"
2075 "layout (std140, binding = 0) uniform bufferVals {\n"
2076 " vec4 red;\n"
2077 " vec4 green;\n"
2078 " vec4 blue;\n"
2079 " vec4 white;\n"
2080 "} myBufferVals;\n"
2081 "void main() {\n"
2082 " vec2 vertices[3];"
2083 " vertices[0] = vec2(-0.5, -0.5);\n"
2084 " vertices[1] = vec2( 0.5, -0.5);\n"
2085 " vertices[2] = vec2( 0.5, 0.5);\n"
2086 " outColor = myBufferVals.blue;\n"
2087 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2088 "}\n";
2089
2090 static const char *fragShaderText =
2091 "#version 140\n"
2092 "#extension GL_ARB_separate_shader_objects : enable\n"
2093 "#extension GL_ARB_shading_language_420pack : enable\n"
2094 "layout (location = 0) in vec4 inColor;\n"
2095 "void main() {\n"
2096 " gl_FragColor = inColor;\n"
2097 "}\n";
2098
2099 ASSERT_NO_FATAL_FAILURE(InitState());
2100 ASSERT_NO_FATAL_FAILURE(InitViewport());
2101
2102 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2103 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2104
2105 // Let's populate our buffer with the following:
2106 // vec4 red;
2107 // vec4 green;
2108 // vec4 blue;
2109 // vec4 white;
2110 const int valCount = 4 * 4;
2111 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
2112 0.0, 1.0, 0.0, 1.0,
2113 0.0, 0.0, 1.0, 1.0,
2114 1.0, 1.0, 1.0, 1.0 };
2115
2116 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
2117 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
2118
2119 XglPipelineObj pipelineobj(m_device);
2120 pipelineobj.AddShader(&vs);
2121 pipelineobj.AddShader(&ps);
2122
2123 XglDescriptorSetObj descriptorSet(m_device);
2124 descriptorSet.AttachMemoryView(&colorBuffer);
2125
Tony Barboura3953b82014-12-03 15:46:29 -07002126 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002127 QueueCommandBuffer(NULL, 0);
2128
2129}
2130
2131TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
2132{
2133 // This test allows the shader to select which buffer it is
2134 // pulling from using layout binding qualifier.
2135 // There are corresponding changes in the compiler stack that
2136 // will select the buffer using binding directly.
2137 // The binding number should match the slot number set up by
2138 // the application.
2139 // The expected result from this test is a purple triangle
2140
2141 static const char *vertShaderText =
2142 "#version 140\n"
2143 "#extension GL_ARB_separate_shader_objects : enable\n"
2144 "#extension GL_ARB_shading_language_420pack : enable\n"
2145 "void main() {\n"
2146 " vec2 vertices[3];"
2147 " vertices[0] = vec2(-0.5, -0.5);\n"
2148 " vertices[1] = vec2( 0.5, -0.5);\n"
2149 " vertices[2] = vec2( 0.5, 0.5);\n"
2150 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2151 "}\n";
2152
2153 static const char *fragShaderText =
2154 "#version 140\n"
2155 "#extension GL_ARB_separate_shader_objects : enable\n"
2156 "#extension GL_ARB_shading_language_420pack : enable\n"
2157 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
2158 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
2159 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
2160 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
2161 "void main() {\n"
2162 " gl_FragColor = myBlueVal.color;\n"
2163 " gl_FragColor += myRedVal.color;\n"
2164 "}\n";
2165
2166 ASSERT_NO_FATAL_FAILURE(InitState());
2167 ASSERT_NO_FATAL_FAILURE(InitViewport());
2168
2169 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2170 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2171
2172 // We're going to create a number of uniform buffers, and then allow
2173 // the shader to select which it wants to read from with a binding
2174
2175 // Let's populate the buffers with a single color each:
2176 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2177 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2178 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2179 // layout (std140, binding = 18) uniform bufferVals { vec4 white; } myWhiteVal;
2180
2181 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2182 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2183 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2184 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2185
2186 const int redCount = sizeof(redVals) / sizeof(float);
2187 const int greenCount = sizeof(greenVals) / sizeof(float);
2188 const int blueCount = sizeof(blueVals) / sizeof(float);
2189 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2190
2191 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2192 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2193
2194 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2195 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2196
2197 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2198 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2199
2200 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2201 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2202
2203 XglPipelineObj pipelineobj(m_device);
2204 pipelineobj.AddShader(&vs);
2205 pipelineobj.AddShader(&ps);
2206
2207 XglDescriptorSetObj descriptorSet(m_device);
2208 descriptorSet.AttachMemoryView(&redBuffer);
2209 descriptorSet.AttachMemoryView(&greenBuffer);
2210 descriptorSet.AttachMemoryView(&blueBuffer);
2211 descriptorSet.AttachMemoryView(&whiteBuffer);
2212
Tony Barboura3953b82014-12-03 15:46:29 -07002213 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002214 QueueCommandBuffer(NULL, 0);
2215
2216}
2217
2218TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
2219{
2220 // This test is the same as TriangleFSUniformBlockBinding, but
2221 // it does not provide an instance name.
2222 // The expected result from this test is a purple triangle
2223
2224 static const char *vertShaderText =
2225 "#version 140\n"
2226 "#extension GL_ARB_separate_shader_objects : enable\n"
2227 "#extension GL_ARB_shading_language_420pack : enable\n"
2228 "void main() {\n"
2229 " vec2 vertices[3];"
2230 " vertices[0] = vec2(-0.5, -0.5);\n"
2231 " vertices[1] = vec2( 0.5, -0.5);\n"
2232 " vertices[2] = vec2( 0.5, 0.5);\n"
2233 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2234 "}\n";
2235
2236 static const char *fragShaderText =
2237 "#version 430\n"
2238 "#extension GL_ARB_separate_shader_objects : enable\n"
2239 "#extension GL_ARB_shading_language_420pack : enable\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002240 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2241 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2242 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2243 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
Cody Northrop68a10f62014-12-05 15:44:14 -07002244 "layout (location = 0) out vec4 outColor;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002245 "void main() {\n"
Cody Northrop68a10f62014-12-05 15:44:14 -07002246 " outColor = blue;\n"
2247 " outColor += red;\n"
Tony Barbourf43b6982014-11-25 13:18:32 -07002248 "}\n";
2249 ASSERT_NO_FATAL_FAILURE(InitState());
2250 ASSERT_NO_FATAL_FAILURE(InitViewport());
2251
2252 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2253 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2254
2255 // We're going to create a number of uniform buffers, and then allow
2256 // the shader to select which it wants to read from with a binding
2257
2258 // Let's populate the buffers with a single color each:
2259 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
2260 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
2261 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
2262 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
2263
2264 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2265 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2266 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2267 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2268
2269 const int redCount = sizeof(redVals) / sizeof(float);
2270 const int greenCount = sizeof(greenVals) / sizeof(float);
2271 const int blueCount = sizeof(blueVals) / sizeof(float);
2272 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2273
2274 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2275 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2276
2277 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2278 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2279
2280 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2281 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2282
2283 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropd1ce7842014-12-09 11:17:01 -07002284 ps.BindShaderEntitySlotToMemory(18, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Tony Barbourf43b6982014-11-25 13:18:32 -07002285
2286 XglPipelineObj pipelineobj(m_device);
2287 pipelineobj.AddShader(&vs);
2288 pipelineobj.AddShader(&ps);
2289
2290 XglDescriptorSetObj descriptorSet(m_device);
2291 descriptorSet.AttachMemoryView(&redBuffer);
2292 descriptorSet.AttachMemoryView(&greenBuffer);
2293 descriptorSet.AttachMemoryView(&blueBuffer);
2294 descriptorSet.AttachMemoryView(&whiteBuffer);
2295
Tony Barboura3953b82014-12-03 15:46:29 -07002296 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
Tony Barbourf43b6982014-11-25 13:18:32 -07002297 QueueCommandBuffer(NULL, 0);
2298
2299}
2300
2301TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
2302{
2303 static const char *vertShaderText =
2304 "#version 140\n"
2305 "#extension GL_ARB_separate_shader_objects : enable\n"
2306 "#extension GL_ARB_shading_language_420pack : enable\n"
2307 "layout (std140, binding=0) uniform bufferVals {\n"
2308 " mat4 mvp;\n"
2309 "} myBufferVals;\n"
2310 "layout (location=0) in vec4 pos;\n"
2311 "layout (location=0) out vec2 UV;\n"
2312 "void main() {\n"
2313 " vec2 positions[3];"
2314 " positions[0] = vec2( 0.0, 0.0);\n"
2315 " positions[1] = vec2( 0.25, 0.1);\n"
2316 " positions[2] = vec2( 0.1, 0.25);\n"
2317 " UV = positions[gl_VertexID % 3];\n"
2318 " gl_Position = myBufferVals.mvp * pos;\n"
2319 "}\n";
2320
2321 static const char *fragShaderText =
2322 "#version 140\n"
2323 "#extension GL_ARB_separate_shader_objects : enable\n"
2324 "#extension GL_ARB_shading_language_420pack : enable\n"
2325 "layout (binding=0) uniform sampler2D surface;\n"
2326 "layout (location=0) out vec4 outColor;\n"
2327 "layout (location=0) in vec2 UV;\n"
2328 "void main() {\n"
2329 " outColor= textureLod(surface, UV, 0.0);\n"
2330 "}\n";
2331 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
2332
2333 glm::mat4 View = glm::lookAt(
2334 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
2335 glm::vec3(0,0,0), // and looks at the origin
2336 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
2337 );
2338
2339 glm::mat4 Model = glm::mat4(1.0f);
2340
2341 glm::mat4 MVP = Projection * View * Model;
2342
2343
2344 ASSERT_NO_FATAL_FAILURE(InitState());
2345 ASSERT_NO_FATAL_FAILURE(InitViewport());
2346 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
2347
2348 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
2349 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
Tony Barboure6152042014-12-10 17:40:15 -07002350 meshBuffer.SetMemoryState(XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbourf43b6982014-11-25 13:18:32 -07002351
2352
2353 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
2354
2355 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
2356 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2357 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2358 XglSamplerObj sampler(m_device);
2359 XglTextureObj texture(m_device);
2360
2361 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
2362 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002363 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture);
Tony Barbourf43b6982014-11-25 13:18:32 -07002364 ps.BindShaderEntitySlotToSampler(0, &sampler);
2365
2366 XglPipelineObj pipelineobj(m_device);
2367 pipelineobj.AddShader(&vs);
2368 pipelineobj.AddShader(&ps);
2369
2370 XglDescriptorSetObj descriptorSet(m_device);
2371
2372 descriptorSet.AttachMemoryView(&mvpBuffer);
2373 descriptorSet.AttachImageView(&texture);
2374 descriptorSet.AttachSampler(&sampler);
2375
2376 m_memoryRefManager.AddMemoryRef(&meshBuffer);
2377 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
2378 m_memoryRefManager.AddMemoryRef(&texture);
2379
2380 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
2381 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
2382 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
2383 };
2384
2385 // this is the current description of g_vbData
2386 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
2387 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
2388 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2389 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
2390 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
2391 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
2392 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
2393 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
2394 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
2395
2396 pipelineobj.AddVertexInputBindings(&vi_binding,1);
2397 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
2398 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
2399
Tony Barboura3953b82014-12-03 15:46:29 -07002400 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 12);
Tony Barbourf43b6982014-11-25 13:18:32 -07002401
2402 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
2403
2404}
Cody Northropd1ce7842014-12-09 11:17:01 -07002405
2406TEST_F(XglRenderTest, TriangleMixedSamplerUniformBlockBinding)
2407{
2408 // This test mixes binding slots of textures and buffers, ensuring
2409 // that sparse and overlapping assignments work.
Cody Northropa0410942014-12-09 13:59:39 -07002410 // The expected result from this test is a purple triangle, although
Cody Northropd1ce7842014-12-09 11:17:01 -07002411 // you can modify it to move the desired result around.
2412
2413 static const char *vertShaderText =
2414 "#version 140\n"
2415 "#extension GL_ARB_separate_shader_objects : enable\n"
2416 "#extension GL_ARB_shading_language_420pack : enable\n"
2417 "void main() {\n"
2418 " vec2 vertices[3];"
2419 " vertices[0] = vec2(-0.5, -0.5);\n"
2420 " vertices[1] = vec2( 0.5, -0.5);\n"
2421 " vertices[2] = vec2( 0.5, 0.5);\n"
2422 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2423 "}\n";
2424
2425 static const char *fragShaderText =
2426 "#version 430\n"
2427 "#extension GL_ARB_separate_shader_objects : enable\n"
2428 "#extension GL_ARB_shading_language_420pack : enable\n"
2429 "layout (binding = 0) uniform sampler2D surface0;\n"
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002430 "layout (binding = 9) uniform sampler2D surface1;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002431 "layout (binding = 2) uniform sampler2D surface2;\n"
Cody Northropa0410942014-12-09 13:59:39 -07002432 "layout (binding = 4) uniform sampler2D surface3;\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002433
Cody Northropa0410942014-12-09 13:59:39 -07002434
2435 "layout (std140, binding = 10) uniform redVal { vec4 red; };"
2436 "layout (std140, binding = 15) uniform greenVal { vec4 green; };"
2437 "layout (std140, binding = 13) uniform blueVal { vec4 blue; };"
2438 "layout (std140, binding = 17) uniform whiteVal { vec4 white; };"
Cody Northropd1ce7842014-12-09 11:17:01 -07002439 "layout (location = 0) out vec4 outColor;\n"
2440 "void main() {\n"
Cody Northropa0410942014-12-09 13:59:39 -07002441 " outColor = red * vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002442 " outColor += white * vec4(0.00001);\n"
2443 " outColor += textureLod(surface2, vec2(0.5), 0.0)* vec4(0.00001);\n"
Cody Northropa0410942014-12-09 13:59:39 -07002444 " outColor += textureLod(surface1, vec2(0.0), 0.0);//* vec4(0.00001);\n"
Cody Northropd1ce7842014-12-09 11:17:01 -07002445 "}\n";
2446 ASSERT_NO_FATAL_FAILURE(InitState());
2447 ASSERT_NO_FATAL_FAILURE(InitViewport());
2448
2449 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2450 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2451
Cody Northropd1ce7842014-12-09 11:17:01 -07002452 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2453 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2454 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2455 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2456
2457 const int redCount = sizeof(redVals) / sizeof(float);
2458 const int greenCount = sizeof(greenVals) / sizeof(float);
2459 const int blueCount = sizeof(blueVals) / sizeof(float);
2460 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2461
2462 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Cody Northropa0410942014-12-09 13:59:39 -07002463 ps.BindShaderEntitySlotToMemory(10, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002464
2465 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Cody Northropa0410942014-12-09 13:59:39 -07002466 ps.BindShaderEntitySlotToMemory(15, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002467
2468 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Cody Northropa0410942014-12-09 13:59:39 -07002469 ps.BindShaderEntitySlotToMemory(13, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002470
2471 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Cody Northropa0410942014-12-09 13:59:39 -07002472 ps.BindShaderEntitySlotToMemory(17, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Cody Northropd1ce7842014-12-09 11:17:01 -07002473
2474 XglSamplerObj sampler0(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002475 XglTextureObj texture0(m_device); // Light Red
2476 texture0.ChangeColors(0xff800000,0xff800000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002477 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
Cody Northropd1ce7842014-12-09 11:17:01 -07002478 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2479 XglSamplerObj sampler2(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002480 XglTextureObj texture2(m_device); // Light Blue
2481 texture2.ChangeColors(0xff000080,0xff000080);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002482 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
Cody Northropd1ce7842014-12-09 11:17:01 -07002483 ps.BindShaderEntitySlotToSampler(2, &sampler2);
2484 XglSamplerObj sampler4(m_device);
Cody Northropa0410942014-12-09 13:59:39 -07002485 XglTextureObj texture4(m_device); // Light Green
2486 texture4.ChangeColors(0xff008000,0xff008000);
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002487 ps.BindShaderEntitySlotToImage(4, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
Cody Northropd1ce7842014-12-09 11:17:01 -07002488 ps.BindShaderEntitySlotToSampler(4, &sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002489
2490 // NOTE: Bindings 1,3,5,7,8,9,11,12,14,16 work for this sampler, but 6 does not!!!
2491 // TODO: Get back here ASAP and understand why.
2492 XglSamplerObj sampler7(m_device);
2493 XglTextureObj texture7(m_device); // Red and Blue
2494 texture7.ChangeColors(0xffff00ff,0xffff00ff);
Courtney Goeltzenleuchter6cbffef2014-12-11 09:03:03 -07002495 ps.BindShaderEntitySlotToImage(9, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2496 ps.BindShaderEntitySlotToSampler(9, &sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002497
2498
2499 XglPipelineObj pipelineobj(m_device);
2500 pipelineobj.AddShader(&vs);
2501 pipelineobj.AddShader(&ps);
2502
2503 XglDescriptorSetObj descriptorSet(m_device);
2504 descriptorSet.AttachMemoryView(&redBuffer);
2505 descriptorSet.AttachMemoryView(&greenBuffer);
2506 descriptorSet.AttachMemoryView(&blueBuffer);
2507 descriptorSet.AttachMemoryView(&whiteBuffer);
2508 descriptorSet.AttachImageView(&texture0);
2509 descriptorSet.AttachSampler(&sampler0);
2510 descriptorSet.AttachImageView(&texture2);
2511 descriptorSet.AttachSampler(&sampler2);
2512 descriptorSet.AttachImageView(&texture4);
2513 descriptorSet.AttachSampler(&sampler4);
Cody Northropa0410942014-12-09 13:59:39 -07002514 descriptorSet.AttachImageView(&texture7);
2515 descriptorSet.AttachSampler(&sampler7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002516
2517 m_memoryRefManager.AddMemoryRef(&texture0);
2518 m_memoryRefManager.AddMemoryRef(&texture2);
2519 m_memoryRefManager.AddMemoryRef(&texture4);
Cody Northropa0410942014-12-09 13:59:39 -07002520 m_memoryRefManager.AddMemoryRef(&texture7);
Cody Northropd1ce7842014-12-09 11:17:01 -07002521
2522
2523 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2524 QueueCommandBuffer(NULL, 0);
2525
2526}
2527
Cody Northrop5fcacbc2014-12-09 19:08:54 -07002528TEST_F(XglRenderTest, TriangleMatchingSamplerUniformBlockBinding)
2529{
2530 // This test matches binding slots of textures and buffers, requiring
2531 // the driver to give them distinct number spaces.
2532 // The expected result from this test is a red triangle, although
2533 // you can modify it to move the desired result around.
2534
2535 static const char *vertShaderText =
2536 "#version 140\n"
2537 "#extension GL_ARB_separate_shader_objects : enable\n"
2538 "#extension GL_ARB_shading_language_420pack : enable\n"
2539 "void main() {\n"
2540 " vec2 vertices[3];"
2541 " vertices[0] = vec2(-0.5, -0.5);\n"
2542 " vertices[1] = vec2( 0.5, -0.5);\n"
2543 " vertices[2] = vec2( 0.5, 0.5);\n"
2544 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
2545 "}\n";
2546
2547 static const char *fragShaderText =
2548 "#version 430\n"
2549 "#extension GL_ARB_separate_shader_objects : enable\n"
2550 "#extension GL_ARB_shading_language_420pack : enable\n"
2551 "layout (binding = 0) uniform sampler2D surface0;\n"
2552 "layout (binding = 1) uniform sampler2D surface1;\n"
2553 "layout (binding = 2) uniform sampler2D surface2;\n"
2554 "layout (binding = 3) uniform sampler2D surface3;\n"
2555 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
2556 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
2557 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
2558 "layout (std140, binding = 3) uniform whiteVal { vec4 white; };"
2559 "layout (location = 0) out vec4 outColor;\n"
2560 "void main() {\n"
2561 " outColor = red;// * vec4(0.00001);\n"
2562 " outColor += white * vec4(0.00001);\n"
2563 " outColor += textureLod(surface1, vec2(0.5), 0.0)* vec4(0.00001);\n"
2564 " outColor += textureLod(surface3, vec2(0.0), 0.0)* vec4(0.00001);\n"
2565 "}\n";
2566 ASSERT_NO_FATAL_FAILURE(InitState());
2567 ASSERT_NO_FATAL_FAILURE(InitViewport());
2568
2569 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX, this);
2570 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT, this);
2571
2572 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
2573 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
2574 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
2575 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
2576
2577 const int redCount = sizeof(redVals) / sizeof(float);
2578 const int greenCount = sizeof(greenVals) / sizeof(float);
2579 const int blueCount = sizeof(blueVals) / sizeof(float);
2580 const int whiteCount = sizeof(whiteVals) / sizeof(float);
2581
2582 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
2583 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
2584
2585 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
2586 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
2587
2588 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
2589 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
2590
2591 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
2592 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
2593
2594 XglSamplerObj sampler0(m_device);
2595 XglTextureObj texture0(m_device); // Light Red
2596 texture0.ChangeColors(0xff800000,0xff800000);
2597 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture0);
2598 ps.BindShaderEntitySlotToSampler(0, &sampler0);
2599 XglSamplerObj sampler2(m_device);
2600 XglTextureObj texture2(m_device); // Light Blue
2601 texture2.ChangeColors(0xff000080,0xff000080);
2602 ps.BindShaderEntitySlotToImage(1, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture2);
2603 ps.BindShaderEntitySlotToSampler(1, &sampler2);
2604 XglSamplerObj sampler4(m_device);
2605 XglTextureObj texture4(m_device); // Light Green
2606 texture4.ChangeColors(0xff008000,0xff008000);
2607 ps.BindShaderEntitySlotToImage(2, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture4);
2608 ps.BindShaderEntitySlotToSampler(2, &sampler4);
2609 XglSamplerObj sampler7(m_device);
2610 XglTextureObj texture7(m_device); // Red and Blue
2611 texture7.ChangeColors(0xffff00ff,0xffff00ff);
2612 ps.BindShaderEntitySlotToImage(3, XGL_SLOT_SHADER_TEXTURE_RESOURCE, &texture7);
2613 ps.BindShaderEntitySlotToSampler(3, &sampler7);
2614
2615
2616 XglPipelineObj pipelineobj(m_device);
2617 pipelineobj.AddShader(&vs);
2618 pipelineobj.AddShader(&ps);
2619
2620 XglDescriptorSetObj descriptorSet(m_device);
2621 descriptorSet.AttachMemoryView(&redBuffer);
2622 descriptorSet.AttachMemoryView(&greenBuffer);
2623 descriptorSet.AttachMemoryView(&blueBuffer);
2624 descriptorSet.AttachMemoryView(&whiteBuffer);
2625 descriptorSet.AttachImageView(&texture0);
2626 descriptorSet.AttachSampler(&sampler0);
2627 descriptorSet.AttachImageView(&texture2);
2628 descriptorSet.AttachSampler(&sampler2);
2629 descriptorSet.AttachImageView(&texture4);
2630 descriptorSet.AttachSampler(&sampler4);
2631 descriptorSet.AttachImageView(&texture7);
2632 descriptorSet.AttachSampler(&sampler7);
2633
2634 m_memoryRefManager.AddMemoryRef(&texture0);
2635 m_memoryRefManager.AddMemoryRef(&texture2);
2636 m_memoryRefManager.AddMemoryRef(&texture4);
2637 m_memoryRefManager.AddMemoryRef(&texture7);
2638
2639
2640 GenericDrawTriangleTest(&pipelineobj, &descriptorSet, 1);
2641 QueueCommandBuffer(NULL, 0);
2642
2643}
2644
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002645int main(int argc, char **argv) {
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002646 int result;
2647
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002648 ::testing::InitGoogleTest(&argc, argv);
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002649 XglTestFramework::InitArgs(&argc, argv);
2650
Chia-I Wu7133fdc2014-12-15 23:57:34 +08002651 ::testing::AddGlobalTestEnvironment(new TestEnvironment);
Courtney Goeltzenleuchterf12c7762014-10-08 08:46:51 -06002652
Courtney Goeltzenleuchter28029792014-09-04 16:26:02 -06002653 result = RUN_ALL_TESTS();
2654
2655 XglTestFramework::Finish();
2656 return result;
Courtney Goeltzenleuchterb85c5812014-08-19 18:35:50 -06002657}