blob: 4d2e6152091dadbdc28d7129816aca17a9f0570b [file] [log] [blame]
Tony Barbour32f368c2014-10-30 14:29:04 -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
53// Basic rendering tests
54
55#include <stdlib.h>
56#include <stdio.h>
57#include <stdbool.h>
58#include <string.h>
59#include <iostream>
60#include <fstream>
61using namespace std;
62
63#include <xgl.h>
64#include "gtest-1.7.0/include/gtest/gtest.h"
65
66#include "xgldevice.h"
67#include "xglimage.h"
68#include "icd-bil.h"
69
70#define GLM_FORCE_RADIANS
71#include "glm/glm.hpp"
72#include <glm/gtc/matrix_transform.hpp>
73
74#include "xglrenderframework.h"
Tony Barbour32f368c2014-10-30 14:29:04 -060075
76#undef ASSERT_NO_FATAL_FAILURE
77#define ASSERT_NO_FATAL_FAILURE(x) x
78
79//--------------------------------------------------------------------------------------
80// Mesh and VertexFormat Data
81//--------------------------------------------------------------------------------------
82struct Vertex
83{
84 XGL_FLOAT posX, posY, posZ, posW; // Position data
85 XGL_FLOAT r, g, b, a; // Color
86};
87
88#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
89
90static const Vertex g_vbData[] =
91{
92 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
93 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
94 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
95 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
96 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
97 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
98
99 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
100 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
101 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
102 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
103 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
104 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
105
106 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
107 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
108 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
109 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
110 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
111 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
112
113 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
114 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
115 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
116 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
117 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
118 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
119
120 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 1.f, 1.f ) },
121 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
122 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
123 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
124 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
125 { XYZ1( -1, 1, -1 ), XYZ1( 0.f, 1.f, 0.f ) },
126
127 { XYZ1( 1, -1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
128 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
129 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
130 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
131 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
132 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 0.f, 0.f ) },
133};
134
135static const Vertex g_vb_solid_face_colors_Data[] =
136{
137 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
138 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
139 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
140 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
141 { XYZ1( 1, -1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
142 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 0.f ) },
143
144 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
145 { XYZ1( -1, 1, 1 ), XYZ1( 0.f, 1.f, 0.f ) },
146 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 0.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, 1.f, 0.f ) },
150
151 { XYZ1( 1, 1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
152 { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
153 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
154 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 0.f, 1.f ) },
155 { XYZ1( 1, 1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
156 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 0.f, 1.f ) },
157
158 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
159 { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
160 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
161 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
162 { XYZ1( -1, -1, 1 ), XYZ1( 1.f, 1.f, 0.f ) },
163 { XYZ1( -1, -1, -1 ), XYZ1( 1.f, 1.f, 0.f ) },
164
165 { XYZ1( 1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
166 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
167 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
168 { XYZ1( 1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
169 { XYZ1( -1, 1, 1 ), XYZ1( 1.f, 0.f, 1.f ) },
170 { XYZ1( -1, 1, -1 ), XYZ1( 1.f, 0.f, 1.f ) },
171
172 { XYZ1( 1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
173 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
174 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
175 { XYZ1( -1, -1, 1 ), XYZ1( 0.f, 1.f, 1.f ) },
176 { XYZ1( 1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
177 { XYZ1( -1, -1, -1 ), XYZ1( 0.f, 1.f, 1.f ) },
178};
179
180class XglRenderTest : public XglRenderFramework
181{
182public:
Tony Barbour32f368c2014-10-30 14:29:04 -0600183 void InitTexture();
184 void InitSampler();
185 void DrawTriangleTest(const char *vertShaderText, const char *fragShaderText);
186 void RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
187 XglConstantBufferObj constantBuffer);
188 void GenericDrawTriangleTest(XglPipelineObj pipelineobj, XglDescriptorSetObj descriptorSet, int numTris);
189 void QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs);
190
191 void ClearDepthStencil(XGL_FLOAT value);
192 void ClearRenderBuffer(XGL_UINT32 clear_color);
193 void InitDepthStencil();
194 void DrawRotatedTriangleTest();
195 void GenerateClearAndPrepareBufferCmds();
196 void XGLTriangleTest(const char *vertShaderText, const char *fragShaderText);
197
198
199protected:
200 XGL_IMAGE m_texture;
201 XGL_IMAGE_VIEW m_textureView;
202 XGL_IMAGE_VIEW_ATTACH_INFO m_textureViewInfo;
203 XGL_GPU_MEMORY m_textureMem;
204
205 XGL_SAMPLER m_sampler;
206
207 XGL_FORMAT m_depth_stencil_fmt;
208 XGL_IMAGE m_depthStencilImage;
209 XGL_GPU_MEMORY m_depthStencilMem;
210 XGL_DEPTH_STENCIL_VIEW m_depthStencilView;
211 XglMemoryRefManager m_memoryRefManager;
212
Tony Barbour32f368c2014-10-30 14:29:04 -0600213
214 virtual void SetUp() {
215
216 this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO;
217 this->app_info.pNext = NULL;
218 this->app_info.pAppName = (const XGL_CHAR *) "render_tests";
219 this->app_info.appVersion = 1;
220 this->app_info.pEngineName = (const XGL_CHAR *) "unittest";
221 this->app_info.engineVersion = 1;
222 this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0);
223
224 memset(&m_textureViewInfo, 0, sizeof(m_textureViewInfo));
225 m_textureViewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
226
227 InitFramework();
228 }
229
230 virtual void TearDown() {
231 // Clean up resources before we reset
232 ShutdownFramework();
233 }
234};
235
Tony Barbour32f368c2014-10-30 14:29:04 -0600236
237void XglRenderTest::DrawRotatedTriangleTest()
238{
239 // TODO : This test will pass a matrix into VS to affect triangle orientation.
240}
241void XglRenderTest::GenericDrawTriangleTest(XglPipelineObj pipelineobj, XglDescriptorSetObj descriptorSet,int numTris)
242{
243 XGL_PIPELINE pipeline; // Not really used but it's useful to keep original tests working to compare
244 XGL_RESULT err = XGL_SUCCESS;
245
246 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
247 // Build command buffer
248 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
249 ASSERT_XGL_SUCCESS(err);
250
251 GenerateClearAndPrepareBufferCmds();
252 GenerateBindRenderTargetCmd();
253 GenerateBindStateAndPipelineCmds(&pipeline);
254
255 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
256 descriptorSet.BindCommandBuffer(m_cmdBuffer);
257
258 // render the triangle
259 xglCmdDraw( m_cmdBuffer, 0, 3*numTris, 0, 1 );
260
261 // finalize recording of the command buffer
262 err = xglEndCommandBuffer( m_cmdBuffer );
263 ASSERT_XGL_SUCCESS( err );
264}
265
266void XglRenderTest::QueueCommandBuffer(XGL_MEMORY_REF *memRefs, XGL_UINT32 numMemRefs)
267{
268 XGL_RESULT err = XGL_SUCCESS;
269
270 // submit the command buffer to the universal queue
271 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, numMemRefs, memRefs, NULL );
272 ASSERT_XGL_SUCCESS( err );
273
274 err = xglQueueWaitIdle( m_device->m_queue );
275 ASSERT_XGL_SUCCESS( err );
276
277 // Wait for work to finish before cleaning up.
278 xglDeviceWaitIdle(m_device->device());
279
280 RecordImage(m_renderTarget);
281
282}
283
284void XglRenderTest::DrawTriangleTest(const char *vertShaderText, const char *fragShaderText)
285{
286 XGL_PIPELINE pipeline;
287 XGL_RESULT err;
288
289 ASSERT_NO_FATAL_FAILURE(InitState());
290 ASSERT_NO_FATAL_FAILURE(InitViewport());
291
292 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
293 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
294
295
296 XglPipelineObj pipelineobj(m_device);
297 pipelineobj.AddShader(&vs);
298 pipelineobj.AddShader(&ps);
299
300 ASSERT_NO_FATAL_FAILURE(InitRenderTarget());
301
302 // Create descriptor set
303 XglDescriptorSetObj descriptorSet(m_device);
304
305 // Build command buffer
306 err = xglBeginCommandBuffer(m_cmdBuffer, 0);
307 ASSERT_XGL_SUCCESS(err);
308
309 GenerateClearAndPrepareBufferCmds();
310 GenerateBindRenderTargetCmd();
311 GenerateBindStateAndPipelineCmds(&pipeline);
312
313 pipelineobj.BindPipelineCommandBuffer(m_cmdBuffer,descriptorSet);
314 descriptorSet.BindCommandBuffer(m_cmdBuffer);
315
316
317 // render the triangle
318 xglCmdDraw( m_cmdBuffer, 0, 3, 0, 1 );
319
320 // finalize recording of the command buffer
321 err = xglEndCommandBuffer( m_cmdBuffer );
322 ASSERT_XGL_SUCCESS( err );
323
324 // this command buffer only uses the vertex buffer memory
325 m_numMemRefs = 0;
326
327 // submit the command buffer to the universal queue
328 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_numMemRefs, m_memRefs, NULL );
329 ASSERT_XGL_SUCCESS( err );
330
331 err = xglQueueWaitIdle( m_device->m_queue );
332 ASSERT_XGL_SUCCESS( err );
333
334 // Wait for work to finish before cleaning up.
335 xglDeviceWaitIdle(m_device->device());
336
337 RecordImage(m_renderTarget);
338
339}
340
341void XglRenderTest::RotateTriangleVSUniform(glm::mat4 Projection, glm::mat4 View, glm::mat4 Model,
342 XglConstantBufferObj constantBuffer)
343{
344 int i;
345 glm::mat4 MVP;
346 int matrixSize = sizeof(MVP);
347 XGL_RESULT err;
348
349 for (i = 0; i < 8; i++) {
350 XGL_UINT8 *pData;
351 err = xglMapMemory(constantBuffer.m_constantBufferMem, 0, (XGL_VOID **) &pData);
352 ASSERT_XGL_SUCCESS(err);
353
354 Model = glm::rotate(Model, glm::radians(22.5f), glm::vec3(0.0f, 1.0f, 0.0f));
355 MVP = Projection * View * Model;
356 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
357
358 err = xglUnmapMemory(constantBuffer.m_constantBufferMem);
359 ASSERT_XGL_SUCCESS(err);
360
361 // submit the command buffer to the universal queue
362 err = xglQueueSubmit( m_device->m_queue, 1, &m_cmdBuffer, m_memoryRefManager.GetNumRefs(), m_memoryRefManager.GetMemoryRefList(), NULL );
363 ASSERT_XGL_SUCCESS( err );
364
365 err = xglQueueWaitIdle( m_device->m_queue );
366 ASSERT_XGL_SUCCESS( err );
367
368 // Wait for work to finish before cleaning up.
369 xglDeviceWaitIdle(m_device->device());
370
371 RecordImage(m_renderTarget);
372 }
373}
374
375void dumpMatrix(const char *note, glm::mat4 MVP)
376{
377 int i,j;
378
379 printf("%s: \n", note);
380 for (i=0; i<4; i++) {
381 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
382 }
383 printf("\n");
384 fflush(stdout);
385}
386
387void dumpVec4(const char *note, glm::vec4 vector)
388{
389 printf("%s: \n", note);
390 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
391 printf("\n");
392 fflush(stdout);
393}
394
395void XglRenderTest::GenerateClearAndPrepareBufferCmds()
396{
397 XglRenderFramework::GenerateClearAndPrepareBufferCmds();
398
399 if (0) {
400// if (m_depthStencilImage) {
401 XGL_IMAGE_SUBRESOURCE_RANGE dsRange = {};
402 dsRange.aspect = XGL_IMAGE_ASPECT_DEPTH;
403 dsRange.baseMipLevel = 0;
404 dsRange.mipLevels = XGL_LAST_MIP_OR_SLICE;
405 dsRange.baseArraySlice = 0;
406 dsRange.arraySize = XGL_LAST_MIP_OR_SLICE;
407
408 // prepare the depth buffer for clear
409 XGL_IMAGE_STATE_TRANSITION transitionToClear = {};
410 transitionToClear.image = m_depthStencilImage;
411 transitionToClear.oldState = m_depthStencilBinding.depthState;
412 transitionToClear.newState = XGL_IMAGE_STATE_CLEAR;
413 transitionToClear.subresourceRange = dsRange;
414 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToClear );
415 m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
416
417 xglCmdClearDepthStencil(m_cmdBuffer, m_depthStencilImage, 1.0f, 0, 1, &dsRange);
418
419 // prepare depth buffer for rendering
420 XGL_IMAGE_STATE_TRANSITION transitionToRender = {};
421 transitionToRender.image = m_renderTarget->image();
422 transitionToRender.oldState = XGL_IMAGE_STATE_CLEAR;
423 transitionToRender.newState = m_depthStencilBinding.depthState;
424 transitionToRender.subresourceRange = dsRange;
425 xglCmdPrepareImages( m_cmdBuffer, 1, &transitionToRender );
426 m_renderTarget->state(( XGL_IMAGE_STATE ) transitionToClear.newState);
427 }
428}
429
430
431void XglRenderTest::ClearDepthStencil(XGL_FLOAT value)
432/* clear the buffer */
433{
434 XGL_RESULT err;
435 const uint16_t depth_value = (uint16_t) (value * 65535);
436 const XGL_INT tw = 128 / sizeof(uint16_t);
437 const XGL_INT th = 32;
438 XGL_INT i, j, w, h;
439 XGL_VOID *data;
440
441 w = (m_width + tw - 1) / tw;
442 h = (m_height + th - 1) / th;
443
444 err = xglMapMemory(m_depthStencilMem, 0, &data);
445 ASSERT_XGL_SUCCESS(err);
446
447 for (i = 0; i < w * h; i++) {
448 uint16_t *tile = (uint16_t *) ((char *) data + 4096 * i);
449
450 for (j = 0; j < 2048; j++)
451 tile[j] = depth_value;
452 }
453
454 err = xglUnmapMemory(m_depthStencilMem);
455 ASSERT_XGL_SUCCESS(err);
456}
457
458void XglRenderTest::ClearRenderBuffer(XGL_UINT32 clear_color)
459/* clear the buffer */
460{
461 XGL_RESULT err;
462 const XGL_IMAGE_SUBRESOURCE sr = {
463 XGL_IMAGE_ASPECT_COLOR, 0, 0
464 };
465 XGL_SUBRESOURCE_LAYOUT sr_layout;
466 XGL_UINT data_size = sizeof(sr_layout);
467 XGL_VOID *ptr;
468
469 err = xglGetImageSubresourceInfo( m_renderTarget->image(),
470 &sr, XGL_INFO_TYPE_SUBRESOURCE_LAYOUT,
471 &data_size, &sr_layout);
472 ASSERT_XGL_SUCCESS( err );
473 ASSERT_EQ(data_size, sizeof(sr_layout));
474
475 err = m_renderTarget->MapMemory( &ptr );
476 ASSERT_XGL_SUCCESS( err );
477
478 ptr = (void *) ((char *) ptr + sr_layout.offset);
479
480 memset(ptr, clear_color, m_width * m_height *sizeof(XGL_UINT32));
481
482 err = m_renderTarget->UnmapMemory();
483 ASSERT_XGL_SUCCESS(err);
484}
485
486void XglRenderTest::InitDepthStencil()
487{
488 XGL_RESULT err;
489 XGL_IMAGE_CREATE_INFO image;
490 XGL_MEMORY_ALLOC_INFO mem_alloc;
491 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view;
492 XGL_MEMORY_REQUIREMENTS mem_reqs;
493 XGL_SIZE mem_reqs_size;
494
495 // Clean up default state created by framework
496 if (m_stateDepthStencil) xglDestroyObject(m_stateDepthStencil);
497
498 m_depth_stencil_fmt.channelFormat = XGL_CH_FMT_R16;
499 m_depth_stencil_fmt.numericFormat = XGL_NUM_FMT_DS;
500
501 image.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
502 image.pNext = NULL;
503 image.imageType = XGL_IMAGE_2D;
504 image.format = m_depth_stencil_fmt;
505 image.extent.width = m_width;
506 image.extent.height = m_height;
507 image.extent.depth = 1;
508 image.mipLevels = 1;
509 image.arraySize = 1;
510 image.samples = 1;
511 image.tiling = XGL_OPTIMAL_TILING;
512 image.usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT;
513 image.flags = 0;
514
515 mem_alloc.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
516 mem_alloc.pNext = NULL;
517 mem_alloc.allocationSize = 0;
518 mem_alloc.alignment = 0;
519 mem_alloc.flags = 0;
520 mem_alloc.heapCount = 0;
521 mem_alloc.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
522
523 /* create image */
524 err = xglCreateImage(device(), &image,
525 &m_depthStencilImage);
526 ASSERT_XGL_SUCCESS(err);
527
528 err = xglGetObjectInfo(m_depthStencilImage,
529 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
530 &mem_reqs_size, &mem_reqs);
531 ASSERT_XGL_SUCCESS(err);
532 ASSERT_EQ(mem_reqs_size, sizeof(mem_reqs));
533
534 mem_alloc.allocationSize = mem_reqs.size;
535 mem_alloc.alignment = mem_reqs.alignment;
536 mem_alloc.heapCount = mem_reqs.heapCount;
537 memcpy(mem_alloc.heaps, mem_reqs.heaps,
538 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
539
540 /* allocate memory */
541 err = xglAllocMemory(device(), &mem_alloc, &m_depthStencilMem);
542 ASSERT_XGL_SUCCESS(err);
543
544 /* bind memory */
545 err = xglBindObjectMemory(m_depthStencilImage, m_depthStencilMem, 0);
546 ASSERT_XGL_SUCCESS(err);
547
548 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depthStencil = {};
549 depthStencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
550 depthStencil.depthTestEnable = XGL_TRUE;
551 depthStencil.depthWriteEnable = XGL_TRUE;
552 depthStencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
553 depthStencil.depthBoundsEnable = XGL_FALSE;
554 depthStencil.minDepth = 0.f;
555 depthStencil.maxDepth = 1.f;
556 depthStencil.back.stencilDepthFailOp = XGL_STENCIL_OP_KEEP;
557 depthStencil.back.stencilFailOp = XGL_STENCIL_OP_KEEP;
558 depthStencil.back.stencilPassOp = XGL_STENCIL_OP_KEEP;
559 depthStencil.back.stencilRef = 0x00;
560 depthStencil.back.stencilFunc = XGL_COMPARE_ALWAYS;
561 depthStencil.front = depthStencil.back;
562
563 err = xglCreateDepthStencilState( device(), &depthStencil, &m_stateDepthStencil );
564 ASSERT_XGL_SUCCESS( err );
565
566 /* create image view */
567 view.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO;
568 view.pNext = NULL;
569 view.image = XGL_NULL_HANDLE;
570 view.mipLevel = 0;
571 view.baseArraySlice = 0;
572 view.arraySize = 1;
573 view.flags = 0;
574 view.image = m_depthStencilImage;
575 err = xglCreateDepthStencilView(device(), &view, &m_depthStencilView);
576 ASSERT_XGL_SUCCESS(err);
577
578 m_depthStencilBinding.view = m_depthStencilView;
579 m_depthStencilBinding.depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
580 m_depthStencilBinding.stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL;
581}
582
583struct xgltriangle_vs_uniform {
584 // Must start with MVP
585 XGL_FLOAT mvp[4][4];
586 XGL_FLOAT position[3][4];
587 XGL_FLOAT color[3][4];
588};
589
590void XglRenderTest::XGLTriangleTest(const char *vertShaderText, const char *fragShaderText)
591{
592 // Create identity matrix
593 int i;
594 struct xgltriangle_vs_uniform data;
595
596 glm::mat4 Projection = glm::mat4(1.0f);
597 glm::mat4 View = glm::mat4(1.0f);
598 glm::mat4 Model = glm::mat4(1.0f);
599 glm::mat4 MVP = Projection * View * Model;
600 const int matrixSize = sizeof(MVP);
601 const int bufSize = sizeof(xgltriangle_vs_uniform) / sizeof(XGL_FLOAT);
602 memcpy(&data.mvp, &MVP[0][0], matrixSize);
603
604 static const Vertex tri_data[] =
605 {
606 { XYZ1( -1, -1, 0 ), XYZ1( 1.f, 0.f, 0.f ) },
607 { XYZ1( 1, -1, 0 ), XYZ1( 0.f, 1.f, 0.f ) },
608 { XYZ1( 0, 1, 0 ), XYZ1( 0.f, 0.f, 1.f ) },
609 };
610
611 for (i=0; i<3; i++) {
612 data.position[i][0] = tri_data[i].posX;
613 data.position[i][1] = tri_data[i].posY;
614 data.position[i][2] = tri_data[i].posZ;
615 data.position[i][3] = tri_data[i].posW;
616 data.color[i][0] = tri_data[i].r;
617 data.color[i][1] = tri_data[i].g;
618 data.color[i][2] = tri_data[i].b;
619 data.color[i][3] = tri_data[i].a;
620 }
621
622 ASSERT_NO_FATAL_FAILURE(InitState());
623 ASSERT_NO_FATAL_FAILURE(InitViewport());
624
625 XglConstantBufferObj constantBuffer(m_device, bufSize*2, sizeof(XGL_FLOAT), (const void*) &data);
626
627 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
628 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
Tony Barbour685f4332014-11-20 15:06:56 -0700629 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -0600630
631 XglPipelineObj pipelineobj(m_device);
632 pipelineobj.AddShader(&vs);
633 pipelineobj.AddShader(&ps);
634
635 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -0700636 descriptorSet.AttachMemoryView(&constantBuffer);
637 m_memoryRefManager.AddMemoryRef(&constantBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -0600638
639 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
640 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
641
642 RotateTriangleVSUniform(Projection, View, Model, constantBuffer);
643
644}
645
646TEST_F(XglRenderTest, XGLTriangle_FragColor)
647{
648 static const char *vertShaderText =
649 "#version 140\n"
650 "#extension GL_ARB_separate_shader_objects : enable\n"
651 "#extension GL_ARB_shading_language_420pack : enable\n"
652 "\n"
653 "layout(binding = 0) uniform buf {\n"
654 " mat4 MVP;\n"
655 " vec4 position[3];\n"
656 " vec4 color[3];\n"
657 "} ubuf;\n"
658 "\n"
659 "layout (location = 0) out vec4 outColor;\n"
660 "\n"
661 "void main() \n"
662 "{\n"
663 " outColor = ubuf.color[gl_VertexID];\n"
664 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
665 "}\n";
666
667 static const char *fragShaderText =
668 "#version 140\n"
669 "#extension GL_ARB_separate_shader_objects : enable\n"
670 "#extension GL_ARB_shading_language_420pack : enable\n"
671 "\n"
672 "layout (location = 0) in vec4 inColor;\n"
673 "\n"
674 "void main()\n"
675 "{\n"
676 " gl_FragColor = inColor;\n"
677 "}\n";
678
679 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to GLSL built-in gl_FragColor");
680 XGLTriangleTest(vertShaderText, fragShaderText);
681}
682
683TEST_F(XglRenderTest, XGLTriangle_OutputLocation)
684{
685 static const char *vertShaderText =
686 "#version 140\n"
687 "#extension GL_ARB_separate_shader_objects : enable\n"
688 "#extension GL_ARB_shading_language_420pack : enable\n"
689 "\n"
690 "layout(binding = 0) uniform buf {\n"
691 " mat4 MVP;\n"
692 " vec4 position[3];\n"
693 " vec4 color[3];\n"
694 "} ubuf;\n"
695 "\n"
696 "layout (location = 0) out vec4 outColor;\n"
697 "\n"
698 "void main() \n"
699 "{\n"
700 " outColor = ubuf.color[gl_VertexID];\n"
701 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
702 "}\n";
703
704 static const char *fragShaderText =
705 "#version 140\n"
706 "#extension GL_ARB_separate_shader_objects : enable\n"
707 "#extension GL_ARB_shading_language_420pack : enable\n"
708 "\n"
709 "layout (location = 0) in vec4 inColor;\n"
710 "layout (location = 0) out vec4 outColor;\n"
711 "\n"
712 "void main()\n"
713 "{\n"
714 " outColor = inColor;\n"
715 "}\n";
716
717 TEST_DESCRIPTION("XGL-style shaders where fragment shader outputs to output location 0, which should be the same as gl_FragColor");
718
719 XGLTriangleTest(vertShaderText, fragShaderText);
720}
721
722TEST_F(XglRenderTest, BIL_XGLTriangle)
723{
724 bool saved_use_bil = XglTestFramework::m_use_bil;
725
726 static const char *vertShaderText =
727 "#version 140\n"
728 "#extension GL_ARB_separate_shader_objects : enable\n"
729 "#extension GL_ARB_shading_language_420pack : enable\n"
730 "\n"
731 "layout(binding = 0) uniform buf {\n"
732 " mat4 MVP;\n"
733 " vec4 position[3];\n"
734 " vec4 color[3];\n"
735 "} ubuf;\n"
736 "\n"
737 "layout (location = 0) out vec4 outColor;\n"
738 "\n"
739 "void main() \n"
740 "{\n"
741 " outColor = ubuf.color[gl_VertexID];\n"
742 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
743 "}\n";
744
745 static const char *fragShaderText =
746 "#version 140\n"
747 "#extension GL_ARB_separate_shader_objects : enable\n"
748 "#extension GL_ARB_shading_language_420pack : enable\n"
749 "\n"
750 "layout (location = 0) in vec4 inColor;\n"
751 "\n"
752 "void main()\n"
753 "{\n"
754 " gl_FragColor = inColor;\n"
755 "}\n";
756
757 TEST_DESCRIPTION("XGL-style shaders, but force test framework to compile shader to BIL and pass BIL to driver.");
758
759 XglTestFramework::m_use_bil = true;
760
761 XGLTriangleTest(vertShaderText, fragShaderText);
762
763 XglTestFramework::m_use_bil = saved_use_bil;
764}
765
766TEST_F(XglRenderTest, GreenTriangle)
767{
768 static const char *vertShaderText =
769 "#version 130\n"
770 "vec2 vertices[3];\n"
771 "void main() {\n"
772 " vertices[0] = vec2(-1.0, -1.0);\n"
773 " vertices[1] = vec2( 1.0, -1.0);\n"
774 " vertices[2] = vec2( 0.0, 1.0);\n"
775 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
776 "}\n";
777
778 static const char *fragShaderText =
779 "#version 130\n"
780 "void main() {\n"
781 " gl_FragColor = vec4(0,1,0,1);\n"
782 "}\n";
783
784 TEST_DESCRIPTION("Basic shader that renders a fixed Green triangle coded as part of the vertex shader.");
785
786 DrawTriangleTest(vertShaderText, fragShaderText);
787}
788
789TEST_F(XglRenderTest, BIL_GreenTriangle)
790{
791 bool saved_use_bil = XglTestFramework::m_use_bil;
792
793 static const char *vertShaderText =
794 "#version 130\n"
795 "vec2 vertices[3];\n"
796 "void main() {\n"
797 " vertices[0] = vec2(-1.0, -1.0);\n"
798 " vertices[1] = vec2( 1.0, -1.0);\n"
799 " vertices[2] = vec2( 0.0, 1.0);\n"
800 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
801 "}\n";
802
803 static const char *fragShaderText =
804 "#version 130\n"
805 "void main() {\n"
806 " gl_FragColor = vec4(0,1,0,1);\n"
807 "}\n";
808
809 TEST_DESCRIPTION("Same shader as GreenTriangle, but compiles shader to BIL and gives BIL to driver.");
810
811 XglTestFramework::m_use_bil = true;
812 DrawTriangleTest(vertShaderText, fragShaderText);
813 XglTestFramework::m_use_bil = saved_use_bil;
814}
815
816TEST_F(XglRenderTest, TriangleFragUniform)
817{
818
819 static const char *vertShaderText =
820 "#version 130\n"
821 "out vec4 color;\n"
822 "out vec4 scale;\n"
823 "vec2 vertices[3];\n"
824 "void main() {\n"
825 "vec2 vertices[3];\n"
826 " vertices[0] = vec2(-0.5, -0.5);\n"
827 " vertices[1] = vec2( 0.5, -0.5);\n"
828 " vertices[2] = vec2( 0.5, 0.5);\n"
829 "vec4 colors[3];\n"
830 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
831 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
832 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
833 " color = colors[gl_VertexID % 3];\n"
834 " scale = vec4(1.0, 1.0, 1.0, 1.0);\n"
835 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
836 "}\n";
837
838 static const char *fragShaderText =
839 "#version 130\n"
840 "in vec4 color;\n"
841 "in vec4 scale;\n"
842 "uniform vec4 foo;\n"
843 "void main() {\n"
844 " gl_FragColor = color * scale + foo;\n"
845 "}\n";
846
847 DrawTriangleTest(vertShaderText, fragShaderText);
848}
849
850TEST_F(XglRenderTest, YellowTriangle)
851{
852 static const char *vertShaderText =
853 "#version 130\n"
854 "void main() {\n"
855 " vec2 vertices[3];"
856 " vertices[0] = vec2(-0.5, -0.5);\n"
857 " vertices[1] = vec2( 0.5, -0.5);\n"
858 " vertices[2] = vec2( 0.5, 0.5);\n"
859 " vec4 colors[3];\n"
860 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
861 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
862 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
863 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
864 "}\n";
865
866 static const char *fragShaderText =
867 "#version 130\n"
868 "void main() {\n"
869 " gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
870 "}\n";
871
872 DrawTriangleTest(vertShaderText, fragShaderText);
873}
874
875TEST_F(XglRenderTest, RotatedTriangle) {
876 DrawRotatedTriangleTest();
877}
878
879TEST_F(XglRenderTest, TriangleTwoFSUniforms)
880{
881 static const char *vertShaderText =
882 "#version 130\n"
883 "out vec4 color;\n"
884 "out vec4 scale;\n"
885 "out vec2 samplePos;\n"
886 "void main() {\n"
887 " vec2 vertices[3];"
888 " vertices[0] = vec2(-0.5, -0.5);\n"
889 " vertices[1] = vec2( 0.5, -0.5);\n"
890 " vertices[2] = vec2( 0.5, 0.5);\n"
891 " vec4 colors[3];\n"
892 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
893 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
894 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
895 " color = colors[gl_VertexID % 3];\n"
896 " vec2 positions[3];"
897 " positions[0] = vec2( 0.0, 0.0);\n"
898 " positions[1] = vec2( 1.0, 0.0);\n"
899 " positions[2] = vec2( 1.0, 1.0);\n"
900 " scale = vec4(0.0, 0.0, 0.0, 0.0);\n"
901 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
902 "}\n";
903
904
905 static const char *fragShaderText =
906 "#version 430\n"
907 "in vec4 color;\n"
908 "in vec4 scale;\n"
909 "uniform vec4 foo;\n"
910 "uniform vec4 bar;\n"
911 "void main() {\n"
912 // by default, with no location or blocks
913 // the compiler will read them from buffer
914 // in reverse order of first use in shader
915 // The buffer contains red, followed by blue,
916 // so foo should be blue, bar should be red
917 " gl_FragColor = color * scale * foo * bar + foo;\n"
918 "}\n";
919
920 ASSERT_NO_FATAL_FAILURE(InitState());
921 ASSERT_NO_FATAL_FAILURE(InitViewport());
922
923 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
924 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
925
926 const int constantCount = 8;
927 const float constants[constantCount] = { 1.0, 0.0, 0.0, 1.0,
928 0.0, 0.0, 1.0, 1.0 };
929 XglConstantBufferObj constantBuffer(m_device,constantCount, sizeof(constants[0]), (const void*) constants);
Tony Barbour685f4332014-11-20 15:06:56 -0700930 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &constantBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -0600931
932 XglPipelineObj pipelineobj(m_device);
933 pipelineobj.AddShader(&vs);
934 pipelineobj.AddShader(&ps);
935
936 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -0700937 descriptorSet.AttachMemoryView(&constantBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -0600938
939 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
940 QueueCommandBuffer(NULL, 0);
941
942}
943
944TEST_F(XglRenderTest, TriangleWithVertexFetch)
945{
946 static const char *vertShaderText =
947 "#version 130\n"
948 //XYZ1( -1, -1, -1 )
949 "in vec4 pos;\n"
950 //XYZ1( 0.f, 0.f, 0.f )
951 "in vec4 inColor;\n"
952 "out vec4 outColor;\n"
953 "void main() {\n"
954 " outColor = inColor;\n"
955 " gl_Position = pos;\n"
956 "}\n";
957
958
959 static const char *fragShaderText =
960 "#version 430\n"
961 "in vec4 color;\n"
962 "void main() {\n"
963 " gl_FragColor = color;\n"
964 "}\n";
965
966
967 ASSERT_NO_FATAL_FAILURE(InitState());
968 ASSERT_NO_FATAL_FAILURE(InitViewport());
969
970 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
Tony Barbour84fa5cd2014-12-01 13:28:32 -0700971 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
Tony Barbour32f368c2014-10-30 14:29:04 -0600972
973 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
974 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
Tony Barbour32f368c2014-10-30 14:29:04 -0600975
976 XglPipelineObj pipelineobj(m_device);
977 pipelineobj.AddShader(&vs);
978 pipelineobj.AddShader(&ps);
979
980 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -0700981 descriptorSet.AttachMemoryView(&meshBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -0600982
983 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
984 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
985 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
986 };
987
988 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
989 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
990 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
991 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
992 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
993 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
994 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
995 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
996 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
997
998 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
999 pipelineobj.AddVertexInputBindings(&vi_binding,1);
Tony Barbour2af38f82014-12-01 13:29:55 -07001000 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
Tony Barbour32f368c2014-10-30 14:29:04 -06001001
Tony Barbourf12bfb92014-11-19 16:33:11 -07001002 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
Tony Barbour32f368c2014-10-30 14:29:04 -06001003 QueueCommandBuffer(NULL, 0);
1004}
1005
1006TEST_F(XglRenderTest, TriangleVSUniform)
1007{
1008 static const char *vertShaderText =
1009 "#version 140\n"
1010 "#extension GL_ARB_separate_shader_objects : enable\n"
1011 "#extension GL_ARB_shading_language_420pack : enable\n"
1012 "\n"
1013 "layout(binding = 0) uniform buf {\n"
1014 " mat4 MVP;\n"
1015 "} ubuf;\n"
1016 "void main() {\n"
1017 " vec2 vertices[3];"
1018 " vertices[0] = vec2(-0.5, -0.5);\n"
1019 " vertices[1] = vec2( 0.5, -0.5);\n"
1020 " vertices[2] = vec2( 0.5, 0.5);\n"
1021 " gl_Position = ubuf.MVP * vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1022 "}\n";
1023
1024 static const char *fragShaderText =
1025 "#version 130\n"
1026 "void main() {\n"
1027 " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
1028 "}\n";
1029
1030 ASSERT_NO_FATAL_FAILURE(InitState());
1031 ASSERT_NO_FATAL_FAILURE(InitViewport());
1032
1033 // Create identity matrix
1034 glm::mat4 Projection = glm::mat4(1.0f);
1035 glm::mat4 View = glm::mat4(1.0f);
1036 glm::mat4 Model = glm::mat4(1.0f);
1037 glm::mat4 MVP = Projection * View * Model;
1038 const int matrixSize = sizeof(MVP) / sizeof(MVP[0]);
1039
Tony Barbour1f9859c2014-12-01 13:25:15 -07001040 XglConstantBufferObj MVPBuffer(m_device, matrixSize, sizeof(MVP[0]), (const void*) &MVP[0][0]);
Tony Barbour32f368c2014-10-30 14:29:04 -06001041 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1042 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1043
Tony Barbour685f4332014-11-20 15:06:56 -07001044 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -06001045
1046 XglPipelineObj pipelineobj(m_device);
1047 pipelineobj.AddShader(&vs);
1048 pipelineobj.AddShader(&ps);
1049
1050 // Create descriptor set and attach the constant buffer to it
Tony Barbour685f4332014-11-20 15:06:56 -07001051 XglDescriptorSetObj descriptorSet(m_device);
1052 descriptorSet.AttachMemoryView(&MVPBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -06001053
Tony Barbour685f4332014-11-20 15:06:56 -07001054 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -06001055
1056 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1057 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1058
Tony Barbour1f9859c2014-12-01 13:25:15 -07001059 RotateTriangleVSUniform(Projection, View, Model, MVPBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -06001060}
1061
Tony Barbourf12bfb92014-11-19 16:33:11 -07001062TEST_F(XglRenderTest, MixTriangle)
1063{
1064 // This tests location applied to varyings. Notice that we have switched foo
1065 // and bar in the FS. The triangle should be blended with red, green and blue
1066 // corners.
1067 static const char *vertShaderText =
1068 "#version 140\n"
1069 "#extension GL_ARB_separate_shader_objects : enable\n"
1070 "#extension GL_ARB_shading_language_420pack : enable\n"
1071 "layout (location=0) out vec4 bar;\n"
1072 "layout (location=1) out vec4 foo;\n"
1073 "layout (location=2) out float scale;\n"
1074 "vec2 vertices[3];\n"
1075 "void main() {\n"
1076 " vertices[0] = vec2(-1.0, -1.0);\n"
1077 " vertices[1] = vec2( 1.0, -1.0);\n"
1078 " vertices[2] = vec2( 0.0, 1.0);\n"
1079 "vec4 colors[3];\n"
1080 " colors[0] = vec4(1.0, 0.0, 0.0, 1.0);\n"
1081 " colors[1] = vec4(0.0, 1.0, 0.0, 1.0);\n"
1082 " colors[2] = vec4(0.0, 0.0, 1.0, 1.0);\n"
1083 " foo = colors[gl_VertexID % 3];\n"
1084 " bar = vec4(1.0, 1.0, 1.0, 1.0);\n"
1085 " scale = 1.0;\n"
1086 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1087 "}\n";
1088
1089 static const char *fragShaderText =
1090 "#version 140\n"
1091 "#extension GL_ARB_separate_shader_objects : enable\n"
1092 "#extension GL_ARB_shading_language_420pack : enable\n"
1093 "layout (location = 1) in vec4 bar;\n"
1094 "layout (location = 0) in vec4 foo;\n"
1095 "layout (location = 2) in float scale;\n"
1096 "void main() {\n"
1097 " gl_FragColor = bar * scale + foo * (1.0-scale);\n"
1098 "}\n";
1099
1100 ASSERT_NO_FATAL_FAILURE(InitState());
1101 ASSERT_NO_FATAL_FAILURE(InitViewport());
1102
1103 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1104 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1105
1106 XglPipelineObj pipelineobj(m_device);
1107 pipelineobj.AddShader(&vs);
1108 pipelineobj.AddShader(&ps);
1109
1110 XglDescriptorSetObj descriptorSet(m_device);
1111
1112 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1113 QueueCommandBuffer(NULL, 0);
1114}
1115
1116TEST_F(XglRenderTest, TriVertFetchAndVertID)
1117{
1118 // This tests that attributes work in the presence of gl_VertexID
1119
1120 static const char *vertShaderText =
1121 "#version 140\n"
1122 "#extension GL_ARB_separate_shader_objects : enable\n"
1123 "#extension GL_ARB_shading_language_420pack : enable\n"
1124 //XYZ1( -1, -1, -1 )
1125 "layout (location = 0) in vec4 pos;\n"
1126 //XYZ1( 0.f, 0.f, 0.f )
1127 "layout (location = 1) in vec4 inColor;\n"
1128 "layout (location = 0) out vec4 outColor;\n"
1129 "void main() {\n"
1130 " outColor = inColor;\n"
1131 " vec4 vertices[3];"
1132 " vertices[gl_VertexID % 3] = pos;\n"
1133 " gl_Position = vertices[(gl_VertexID + 3) % 3];\n"
1134 "}\n";
1135
1136
1137 static const char *fragShaderText =
1138 "#version 140\n"
1139 "#extension GL_ARB_separate_shader_objects : enable\n"
1140 "#extension GL_ARB_shading_language_420pack : enable\n"
1141 "layout (location = 0) in vec4 color;\n"
1142 "void main() {\n"
1143 " gl_FragColor = color;\n"
1144 "}\n";
1145
1146 ASSERT_NO_FATAL_FAILURE(InitState());
1147 ASSERT_NO_FATAL_FAILURE(InitViewport());
1148
1149 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1150 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1151
1152 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1153 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1154
1155 XglPipelineObj pipelineobj(m_device);
1156 pipelineobj.AddShader(&vs);
1157 pipelineobj.AddShader(&ps);
1158
1159 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001160 descriptorSet.AttachMemoryView(&meshBuffer);
Tony Barbourf12bfb92014-11-19 16:33:11 -07001161
1162 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1163 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1164 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1165 };
1166
1167 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1168 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1169 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1170 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1171 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1172 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1173 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1174 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1175 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1176
1177 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1178 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1179 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1180
1181 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1182 QueueCommandBuffer(NULL, 0);
1183}
Tony Barboura91c4972014-11-19 17:36:06 -07001184
1185TEST_F(XglRenderTest, TriVertFetchDeadAttr)
1186{
1187 // This tests that attributes work in the presence of gl_VertexID
1188 // and a dead attribute in position 0. Draws a triangle with yellow,
1189 // red and green corners, starting at top and going clockwise.
1190
1191 static const char *vertShaderText =
1192 "#version 140\n"
1193 "#extension GL_ARB_separate_shader_objects : enable\n"
1194 "#extension GL_ARB_shading_language_420pack : enable\n"
1195 //XYZ1( -1, -1, -1 )
1196 "layout (location = 0) in vec4 pos;\n"
1197 //XYZ1( 0.f, 0.f, 0.f )
1198 "layout (location = 1) in vec4 inColor;\n"
1199 "layout (location = 0) out vec4 outColor;\n"
1200 "void main() {\n"
1201 " outColor = inColor;\n"
1202 " vec2 vertices[3];"
1203 " vertices[0] = vec2(-1.0, -1.0);\n"
1204 " vertices[1] = vec2( 1.0, -1.0);\n"
1205 " vertices[2] = vec2( 0.0, 1.0);\n"
1206 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1207 "}\n";
1208
1209
1210 static const char *fragShaderText =
1211 "#version 140\n"
1212 "#extension GL_ARB_separate_shader_objects : enable\n"
1213 "#extension GL_ARB_shading_language_420pack : enable\n"
1214 "layout (location = 0) in vec4 color;\n"
1215 "void main() {\n"
1216 " gl_FragColor = color;\n"
1217 "}\n";
1218
1219 ASSERT_NO_FATAL_FAILURE(InitState());
1220 ASSERT_NO_FATAL_FAILURE(InitViewport());
1221
1222 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vbData)/sizeof(g_vbData[0]),sizeof(g_vbData[0]), g_vbData);
1223 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1224
1225 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1226 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1227
1228 XglPipelineObj pipelineobj(m_device);
1229 pipelineobj.AddShader(&vs);
1230 pipelineobj.AddShader(&ps);
1231
1232 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001233 descriptorSet.AttachMemoryView(&meshBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001234
1235 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1236 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1237 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1238 };
1239
1240 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1241 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1242 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1243 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1244 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1245 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1246 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1247 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1248 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1249
1250 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
1251 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1252 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
1253
1254 GenericDrawTriangleTest(pipelineobj, descriptorSet, 2);
1255 QueueCommandBuffer(NULL, 0);
1256
1257}
1258
Tony Barbour88ad4b22014-11-18 17:01:30 -07001259TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Tony Barbour32f368c2014-10-30 14:29:04 -06001260{
1261 static const char *vertShaderText =
1262 "#version 140\n"
1263 "layout (std140) uniform bufferVals {\n"
1264 " mat4 mvp;\n"
1265 "} myBufferVals;\n"
1266 "in vec4 pos;\n"
1267 "in vec4 inColor;\n"
1268 "out vec4 outColor;\n"
1269 "void main() {\n"
1270 " outColor = inColor;\n"
1271 " gl_Position = myBufferVals.mvp * pos;\n"
1272 "}\n";
1273
1274 static const char *fragShaderText =
1275 "#version 130\n"
1276 "in vec4 color;\n"
1277 "void main() {\n"
1278 " gl_FragColor = color;\n"
1279 "}\n";
1280 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
1281
1282 glm::mat4 View = glm::lookAt(
1283 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1284 glm::vec3(0,0,0), // and looks at the origin
1285 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1286 );
1287
1288 glm::mat4 Model = glm::mat4(1.0f);
1289
1290 glm::mat4 MVP = Projection * View * Model;
1291
1292 ASSERT_NO_FATAL_FAILURE(InitState());
1293 ASSERT_NO_FATAL_FAILURE(InitViewport());
1294 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1295
1296 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1297 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1298
Tony Barbour32f368c2014-10-30 14:29:04 -06001299 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1300
Tony Barbour1f9859c2014-12-01 13:25:15 -07001301 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
Tony Barbour32f368c2014-10-30 14:29:04 -06001302 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1303 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1304
Tony Barbour685f4332014-11-20 15:06:56 -07001305 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &MVPBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -06001306
1307 XglPipelineObj pipelineobj(m_device);
1308 pipelineobj.AddShader(&vs);
1309 pipelineobj.AddShader(&ps);
1310
1311 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001312 descriptorSet.AttachMemoryView(&MVPBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -06001313
Tony Barbour685f4332014-11-20 15:06:56 -07001314 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1315 m_memoryRefManager.AddMemoryRef(&MVPBuffer);
Tony Barbour32f368c2014-10-30 14:29:04 -06001316
1317 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1318 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1319 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1320 };
1321
1322 // this is the current description of g_vbData
1323 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1324 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1325 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1326 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1327 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1328 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1329 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1330 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1331 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1332
1333 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1334 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
Tony Barbour2af38f82014-12-01 13:29:55 -07001335 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
Tony Barbour32f368c2014-10-30 14:29:04 -06001336
1337 ClearDepthStencil(1.0f);
1338 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
1339
1340 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1341
1342}
1343
1344TEST_F(XglRenderTest, VSTexture)
1345{
1346 // The expected result from this test is a green and red triangle;
1347 // one red vertex on the left, two green vertices on the right.
1348 static const char *vertShaderText =
1349 "#version 130\n"
1350 "out vec4 texColor;\n"
1351 "uniform sampler2D surface;\n"
1352 "void main() {\n"
1353 " vec2 vertices[3];"
1354 " vertices[0] = vec2(-0.5, -0.5);\n"
1355 " vertices[1] = vec2( 0.5, -0.5);\n"
1356 " vertices[2] = vec2( 0.5, 0.5);\n"
1357 " vec2 positions[3];"
1358 " positions[0] = vec2( 0.0, 0.0);\n"
1359 " positions[1] = vec2( 0.25, 0.1);\n"
1360 " positions[2] = vec2( 0.1, 0.25);\n"
1361 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1362 " texColor = textureLod(surface, samplePos, 0.0);\n"
1363 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1364 "}\n";
1365
1366 static const char *fragShaderText =
1367 "#version 130\n"
1368 "in vec4 texColor;\n"
1369 "void main() {\n"
1370 " gl_FragColor = texColor;\n"
1371 "}\n";
1372
1373 ASSERT_NO_FATAL_FAILURE(InitState());
1374 ASSERT_NO_FATAL_FAILURE(InitViewport());
1375
1376 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1377 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1378 XglSamplerObj sampler(m_device);
1379 XglTextureObj texture(m_device);
1380
Tony Barbour685f4332014-11-20 15:06:56 -07001381 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1382 vs.BindShaderEntitySlotToSampler(0, &sampler);
Tony Barbour32f368c2014-10-30 14:29:04 -06001383
1384 XglPipelineObj pipelineobj(m_device);
1385 pipelineobj.AddShader(&vs);
1386 pipelineobj.AddShader(&ps);
1387
1388 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001389 descriptorSet.AttachImageView(&texture);
1390 descriptorSet.AttachSampler(&sampler);
Tony Barbour32f368c2014-10-30 14:29:04 -06001391
Tony Barbour685f4332014-11-20 15:06:56 -07001392 m_memoryRefManager.AddMemoryRef(&texture);
Tony Barbour32f368c2014-10-30 14:29:04 -06001393
1394 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1395 QueueCommandBuffer(NULL, 0);
1396
1397}
Tony Barbourf12bfb92014-11-19 16:33:11 -07001398TEST_F(XglRenderTest, TexturedTriangle)
1399{
1400 // The expected result from this test is a red and green checkered triangle
1401 static const char *vertShaderText =
1402 "#version 140\n"
1403 "#extension GL_ARB_separate_shader_objects : enable\n"
1404 "#extension GL_ARB_shading_language_420pack : enable\n"
1405 "layout (location = 0) out vec2 samplePos;\n"
1406 "void main() {\n"
1407 " vec2 vertices[3];"
1408 " vertices[0] = vec2(-0.5, -0.5);\n"
1409 " vertices[1] = vec2( 0.5, -0.5);\n"
1410 " vertices[2] = vec2( 0.5, 0.5);\n"
1411 " vec2 positions[3];"
1412 " positions[0] = vec2( 0.0, 0.0);\n"
1413 " positions[1] = vec2( 1.0, 0.0);\n"
1414 " positions[2] = vec2( 1.0, 1.0);\n"
1415 " samplePos = positions[gl_VertexID % 3];\n"
1416 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1417 "}\n";
Tony Barbour32f368c2014-10-30 14:29:04 -06001418
Tony Barbourf12bfb92014-11-19 16:33:11 -07001419 static const char *fragShaderText =
1420 "#version 140\n"
1421 "#extension GL_ARB_separate_shader_objects : enable\n"
1422 "#extension GL_ARB_shading_language_420pack : enable\n"
1423 "layout (location = 0) in vec2 samplePos;\n"
1424 "layout (binding = 0) uniform sampler2D surface;\n"
1425 "layout (location=0) out vec4 outColor;\n"
1426 "void main() {\n"
1427 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1428 " outColor = texColor;\n"
1429 "}\n";
1430
1431 ASSERT_NO_FATAL_FAILURE(InitState());
1432 ASSERT_NO_FATAL_FAILURE(InitViewport());
1433
1434 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1435 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1436 XglSamplerObj sampler(m_device);
1437 XglTextureObj texture(m_device);
1438
Tony Barbour685f4332014-11-20 15:06:56 -07001439 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1440 ps.BindShaderEntitySlotToSampler(0, &sampler);
Tony Barbourf12bfb92014-11-19 16:33:11 -07001441
1442 XglPipelineObj pipelineobj(m_device);
1443 pipelineobj.AddShader(&vs);
1444 pipelineobj.AddShader(&ps);
1445
1446 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001447 descriptorSet.AttachImageView(&texture);
1448 descriptorSet.AttachSampler(&sampler);
Tony Barbourf12bfb92014-11-19 16:33:11 -07001449
Tony Barbour685f4332014-11-20 15:06:56 -07001450 m_memoryRefManager.AddMemoryRef(&texture);
Tony Barbourf12bfb92014-11-19 16:33:11 -07001451
1452 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1453 QueueCommandBuffer(NULL, 0);
1454}
1455TEST_F(XglRenderTest, TexturedTriangleClip)
1456{
1457 // The expected result from this test is a red and green checkered triangle
1458 static const char *vertShaderText =
1459 "#version 330\n"
1460 "#extension GL_ARB_separate_shader_objects : enable\n"
1461 "#extension GL_ARB_shading_language_420pack : enable\n"
1462 "layout (location = 0) out vec2 samplePos;\n"
1463 "out gl_PerVertex {\n"
1464 " vec4 gl_Position;\n"
1465 " float gl_ClipDistance[1];\n"
1466 "};\n"
1467 "void main() {\n"
1468 " vec2 vertices[3];"
1469 " vertices[0] = vec2(-0.5, -0.5);\n"
1470 " vertices[1] = vec2( 0.5, -0.5);\n"
1471 " vertices[2] = vec2( 0.5, 0.5);\n"
1472 " vec2 positions[3];"
1473 " positions[0] = vec2( 0.0, 0.0);\n"
1474 " positions[1] = vec2( 1.0, 0.0);\n"
1475 " positions[2] = vec2( 1.0, 1.0);\n"
1476 " float dists[3];\n"
1477 " dists[0] = 1.0;\n"
1478 " dists[1] = 1.0;\n"
1479 " dists[2] = -1.0;\n"
1480 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1481 " samplePos = positions[gl_VertexID % 3];\n"
1482 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1483 "}\n";
1484
1485 static const char *fragShaderText =
1486 "#version 140\n"
1487 "#extension GL_ARB_separate_shader_objects : enable\n"
1488 "#extension GL_ARB_shading_language_420pack : enable\n"
1489 "layout (location = 0) in vec2 samplePos;\n"
1490 "layout (binding = 0) uniform sampler2D surface;\n"
1491 "layout (location=0) out vec4 outColor;\n"
1492 "void main() {\n"
1493 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1494 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1495 " outColor = texColor;\n"
1496 "}\n";
1497
1498
1499 ASSERT_NO_FATAL_FAILURE(InitState());
1500 ASSERT_NO_FATAL_FAILURE(InitViewport());
1501
1502 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1503 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1504 XglSamplerObj sampler(m_device);
1505 XglTextureObj texture(m_device);
1506
Tony Barbour685f4332014-11-20 15:06:56 -07001507 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1508 ps.BindShaderEntitySlotToSampler(0, &sampler);
Tony Barbourf12bfb92014-11-19 16:33:11 -07001509
1510 XglPipelineObj pipelineobj(m_device);
1511 pipelineobj.AddShader(&vs);
1512 pipelineobj.AddShader(&ps);
1513
1514 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001515 descriptorSet.AttachImageView(&texture);
1516 descriptorSet.AttachSampler(&sampler);
Tony Barbourf12bfb92014-11-19 16:33:11 -07001517
Tony Barbour685f4332014-11-20 15:06:56 -07001518 m_memoryRefManager.AddMemoryRef(&texture);
Tony Barbourf12bfb92014-11-19 16:33:11 -07001519
1520 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1521 QueueCommandBuffer(NULL, 0);
1522}
Tony Barbour88ad4b22014-11-18 17:01:30 -07001523TEST_F(XglRenderTest, FSTriangle)
1524{
1525 // The expected result from this test is a red and green checkered triangle
1526 static const char *vertShaderText =
1527 "#version 140\n"
1528 "#extension GL_ARB_separate_shader_objects : enable\n"
1529 "#extension GL_ARB_shading_language_420pack : enable\n"
1530 "layout (location = 0) out vec2 samplePos;\n"
1531 "void main() {\n"
1532 " vec2 vertices[3];"
1533 " vertices[0] = vec2(-0.5, -0.5);\n"
1534 " vertices[1] = vec2( 0.5, -0.5);\n"
1535 " vertices[2] = vec2( 0.5, 0.5);\n"
1536 " vec2 positions[3];"
1537 " positions[0] = vec2( 0.0, 0.0);\n"
1538 " positions[1] = vec2( 1.0, 0.0);\n"
1539 " positions[2] = vec2( 1.0, 1.0);\n"
1540 " samplePos = positions[gl_VertexID % 3];\n"
1541 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1542 "}\n";
1543
1544 static const char *fragShaderText =
1545 "#version 140\n"
1546 "#extension GL_ARB_separate_shader_objects : enable\n"
1547 "#extension GL_ARB_shading_language_420pack : enable\n"
1548 "layout (location = 0) in vec2 samplePos;\n"
1549 "layout (binding = 0) uniform sampler2D surface;\n"
1550 "layout (location=0) out vec4 outColor;\n"
1551 "void main() {\n"
1552 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1553 " outColor = texColor;\n"
1554 "}\n";
1555
1556 ASSERT_NO_FATAL_FAILURE(InitState());
1557 ASSERT_NO_FATAL_FAILURE(InitViewport());
1558
1559 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1560 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1561 XglSamplerObj sampler(m_device);
1562 XglTextureObj texture(m_device);
1563
Tony Barbour685f4332014-11-20 15:06:56 -07001564 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1565 ps.BindShaderEntitySlotToSampler(0, &sampler);
Tony Barbour88ad4b22014-11-18 17:01:30 -07001566
1567 XglPipelineObj pipelineobj(m_device);
1568 pipelineobj.AddShader(&vs);
1569 pipelineobj.AddShader(&ps);
1570
1571 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001572 descriptorSet.AttachImageView(&texture);
1573 descriptorSet.AttachSampler(&sampler);
Tony Barbour88ad4b22014-11-18 17:01:30 -07001574
Tony Barbour685f4332014-11-20 15:06:56 -07001575 m_memoryRefManager.AddMemoryRef(&texture);
Tony Barbour88ad4b22014-11-18 17:01:30 -07001576
1577 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1578 QueueCommandBuffer(NULL, 0);
1579}
1580
Tony Barboura91c4972014-11-19 17:36:06 -07001581TEST_F(XglRenderTest, TriangleVSUniformBlock)
1582{
1583 // The expected result from this test is a blue triangle
1584
1585 static const char *vertShaderText =
1586 "#version 140\n"
1587 "#extension GL_ARB_separate_shader_objects : enable\n"
1588 "#extension GL_ARB_shading_language_420pack : enable\n"
1589 "layout (location = 0) out vec4 outColor;\n"
1590 "layout (std140, binding = 0) uniform bufferVals {\n"
1591 " vec4 red;\n"
1592 " vec4 green;\n"
1593 " vec4 blue;\n"
1594 " vec4 white;\n"
1595 "} myBufferVals;\n"
1596 "void main() {\n"
1597 " vec2 vertices[3];"
1598 " vertices[0] = vec2(-0.5, -0.5);\n"
1599 " vertices[1] = vec2( 0.5, -0.5);\n"
1600 " vertices[2] = vec2( 0.5, 0.5);\n"
1601 " outColor = myBufferVals.blue;\n"
1602 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1603 "}\n";
1604
1605 static const char *fragShaderText =
1606 "#version 140\n"
1607 "#extension GL_ARB_separate_shader_objects : enable\n"
1608 "#extension GL_ARB_shading_language_420pack : enable\n"
1609 "layout (location = 0) in vec4 inColor;\n"
1610 "void main() {\n"
1611 " gl_FragColor = inColor;\n"
1612 "}\n";
1613
1614 ASSERT_NO_FATAL_FAILURE(InitState());
1615 ASSERT_NO_FATAL_FAILURE(InitViewport());
1616
1617 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1618 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1619
1620 // Let's populate our buffer with the following:
1621 // vec4 red;
1622 // vec4 green;
1623 // vec4 blue;
1624 // vec4 white;
1625 const int valCount = 4 * 4;
1626 const float bufferVals[valCount] = { 1.0, 0.0, 0.0, 1.0,
1627 0.0, 1.0, 0.0, 1.0,
1628 0.0, 0.0, 1.0, 1.0,
1629 1.0, 1.0, 1.0, 1.0 };
1630
1631 XglConstantBufferObj colorBuffer(m_device, valCount, sizeof(bufferVals[0]), (const void*) bufferVals);
Tony Barbour685f4332014-11-20 15:06:56 -07001632 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &colorBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001633
1634 XglPipelineObj pipelineobj(m_device);
1635 pipelineobj.AddShader(&vs);
1636 pipelineobj.AddShader(&ps);
1637
1638 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001639 descriptorSet.AttachMemoryView(&colorBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001640
1641 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1642 QueueCommandBuffer(NULL, 0);
1643
1644}
1645
1646TEST_F(XglRenderTest, TriangleFSUniformBlockBinding)
1647{
1648 // This test allows the shader to select which buffer it is
1649 // pulling from using layout binding qualifier.
1650 // There are corresponding changes in the compiler stack that
1651 // will select the buffer using binding directly.
1652 // The binding number should match the slot number set up by
1653 // the application.
1654 // The expected result from this test is a purple triangle
1655
1656 static const char *vertShaderText =
1657 "#version 140\n"
1658 "#extension GL_ARB_separate_shader_objects : enable\n"
1659 "#extension GL_ARB_shading_language_420pack : enable\n"
1660 "void main() {\n"
1661 " vec2 vertices[3];"
1662 " vertices[0] = vec2(-0.5, -0.5);\n"
1663 " vertices[1] = vec2( 0.5, -0.5);\n"
1664 " vertices[2] = vec2( 0.5, 0.5);\n"
1665 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1666 "}\n";
1667
1668 static const char *fragShaderText =
1669 "#version 140\n"
1670 "#extension GL_ARB_separate_shader_objects : enable\n"
1671 "#extension GL_ARB_shading_language_420pack : enable\n"
1672 "layout (std140, binding = 0) uniform redVal { vec4 color; } myRedVal\n;"
1673 "layout (std140, binding = 1) uniform greenVal { vec4 color; } myGreenVal\n;"
1674 "layout (std140, binding = 2) uniform blueVal { vec4 color; } myBlueVal\n;"
1675 "layout (std140, binding = 18) uniform whiteVal { vec4 color; } myWhiteVal\n;"
1676 "void main() {\n"
1677 " gl_FragColor = myBlueVal.color;\n"
1678 " gl_FragColor += myRedVal.color;\n"
1679 "}\n";
1680
1681 ASSERT_NO_FATAL_FAILURE(InitState());
1682 ASSERT_NO_FATAL_FAILURE(InitViewport());
1683
1684 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1685 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1686
1687 // We're going to create a number of uniform buffers, and then allow
1688 // the shader to select which it wants to read from with a binding
1689
1690 // Let's populate the buffers with a single color each:
1691 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
1692 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
1693 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
1694 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
1695
1696 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
1697 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
1698 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
1699 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
1700
1701 const int redCount = sizeof(redVals) / sizeof(float);
1702 const int greenCount = sizeof(greenVals) / sizeof(float);
1703 const int blueCount = sizeof(blueVals) / sizeof(float);
1704 const int whiteCount = sizeof(whiteVals) / sizeof(float);
1705
1706 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Tony Barbour685f4332014-11-20 15:06:56 -07001707 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001708
1709 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Tony Barbour685f4332014-11-20 15:06:56 -07001710 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001711
1712 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Tony Barbour685f4332014-11-20 15:06:56 -07001713 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001714
1715 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Tony Barbour685f4332014-11-20 15:06:56 -07001716 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001717
1718 XglPipelineObj pipelineobj(m_device);
1719 pipelineobj.AddShader(&vs);
1720 pipelineobj.AddShader(&ps);
1721
1722 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001723 descriptorSet.AttachMemoryView(&redBuffer);
1724 descriptorSet.AttachMemoryView(&greenBuffer);
1725 descriptorSet.AttachMemoryView(&blueBuffer);
1726 descriptorSet.AttachMemoryView(&whiteBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001727
1728 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1729 QueueCommandBuffer(NULL, 0);
1730
1731}
1732
1733TEST_F(XglRenderTest, TriangleFSAnonymousUniformBlockBinding)
1734{
1735 // This test is the same as TriangleFSUniformBlockBinding, but
1736 // it does not provide an instance name.
1737 // The expected result from this test is a purple triangle
1738
1739 static const char *vertShaderText =
1740 "#version 140\n"
1741 "#extension GL_ARB_separate_shader_objects : enable\n"
1742 "#extension GL_ARB_shading_language_420pack : enable\n"
1743 "void main() {\n"
1744 " vec2 vertices[3];"
1745 " vertices[0] = vec2(-0.5, -0.5);\n"
1746 " vertices[1] = vec2( 0.5, -0.5);\n"
1747 " vertices[2] = vec2( 0.5, 0.5);\n"
1748 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1749 "}\n";
1750
1751 static const char *fragShaderText =
1752 "#version 430\n"
1753 "#extension GL_ARB_separate_shader_objects : enable\n"
1754 "#extension GL_ARB_shading_language_420pack : enable\n"
1755 "layout (location = 0) uniform vec4 foo;\n"
1756 "layout (location = 1) uniform vec4 bar;\n"
1757 "layout (std140, binding = 0) uniform redVal { vec4 red; };"
1758 "layout (std140, binding = 1) uniform greenVal { vec4 green; };"
1759 "layout (std140, binding = 2) uniform blueVal { vec4 blue; };"
1760 "layout (std140, binding = 18) uniform whiteVal { vec4 white; };"
1761 "void main() {\n"
1762 " gl_FragColor = blue;\n"
1763 " gl_FragColor += red;\n"
1764 "}\n";
1765 ASSERT_NO_FATAL_FAILURE(InitState());
1766 ASSERT_NO_FATAL_FAILURE(InitViewport());
1767
1768 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1769 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1770
1771 // We're going to create a number of uniform buffers, and then allow
1772 // the shader to select which it wants to read from with a binding
1773
1774 // Let's populate the buffers with a single color each:
1775 // layout (std140, binding = 0) uniform bufferVals { vec4 red; } myRedVal;
1776 // layout (std140, binding = 1) uniform bufferVals { vec4 green; } myGreenVal;
1777 // layout (std140, binding = 2) uniform bufferVals { vec4 blue; } myBlueVal;
1778 // layout (std140, binding = 3) uniform bufferVals { vec4 white; } myWhiteVal;
1779
1780 const float redVals[4] = { 1.0, 0.0, 0.0, 1.0 };
1781 const float greenVals[4] = { 0.0, 1.0, 0.0, 1.0 };
1782 const float blueVals[4] = { 0.0, 0.0, 1.0, 1.0 };
1783 const float whiteVals[4] = { 1.0, 1.0, 1.0, 1.0 };
1784
1785 const int redCount = sizeof(redVals) / sizeof(float);
1786 const int greenCount = sizeof(greenVals) / sizeof(float);
1787 const int blueCount = sizeof(blueVals) / sizeof(float);
1788 const int whiteCount = sizeof(whiteVals) / sizeof(float);
1789
1790 XglConstantBufferObj redBuffer(m_device, redCount, sizeof(redVals[0]), (const void*) redVals);
Tony Barbour685f4332014-11-20 15:06:56 -07001791 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &redBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001792
1793 XglConstantBufferObj greenBuffer(m_device, greenCount, sizeof(greenVals[0]), (const void*) greenVals);
Tony Barbour685f4332014-11-20 15:06:56 -07001794 ps.BindShaderEntitySlotToMemory(1, XGL_SLOT_SHADER_RESOURCE, &greenBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001795
1796 XglConstantBufferObj blueBuffer(m_device, blueCount, sizeof(blueVals[0]), (const void*) blueVals);
Tony Barbour685f4332014-11-20 15:06:56 -07001797 ps.BindShaderEntitySlotToMemory(2, XGL_SLOT_SHADER_RESOURCE, &blueBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001798
1799 XglConstantBufferObj whiteBuffer(m_device, whiteCount, sizeof(whiteVals[0]), (const void*) whiteVals);
Tony Barbour685f4332014-11-20 15:06:56 -07001800 ps.BindShaderEntitySlotToMemory(3, XGL_SLOT_SHADER_RESOURCE, &whiteBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001801
1802 XglPipelineObj pipelineobj(m_device);
1803 pipelineobj.AddShader(&vs);
1804 pipelineobj.AddShader(&ps);
1805
1806 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour685f4332014-11-20 15:06:56 -07001807 descriptorSet.AttachMemoryView(&redBuffer);
1808 descriptorSet.AttachMemoryView(&greenBuffer);
1809 descriptorSet.AttachMemoryView(&blueBuffer);
1810 descriptorSet.AttachMemoryView(&whiteBuffer);
Tony Barboura91c4972014-11-19 17:36:06 -07001811
1812 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1813 QueueCommandBuffer(NULL, 0);
1814
1815}
1816
Tony Barbour88ad4b22014-11-18 17:01:30 -07001817TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
Tony Barbour32f368c2014-10-30 14:29:04 -06001818{
1819 static const char *vertShaderText =
1820 "#version 140\n"
1821 "#extension GL_ARB_separate_shader_objects : enable\n"
1822 "#extension GL_ARB_shading_language_420pack : enable\n"
1823 "layout (std140, binding=0) uniform bufferVals {\n"
1824 " mat4 mvp;\n"
1825 "} myBufferVals;\n"
1826 "layout (location=0) in vec4 pos;\n"
Tony Barbour88ad4b22014-11-18 17:01:30 -07001827 "layout (location=0) out vec2 UV;\n"
Tony Barbour32f368c2014-10-30 14:29:04 -06001828 "void main() {\n"
1829 " vec2 positions[3];"
1830 " positions[0] = vec2( 0.0, 0.0);\n"
1831 " positions[1] = vec2( 0.25, 0.1);\n"
1832 " positions[2] = vec2( 0.1, 0.25);\n"
Tony Barbour88ad4b22014-11-18 17:01:30 -07001833 " UV = positions[gl_VertexID % 3];\n"
Tony Barbour32f368c2014-10-30 14:29:04 -06001834 " gl_Position = myBufferVals.mvp * pos;\n"
1835 "}\n";
1836
1837 static const char *fragShaderText =
1838 "#version 140\n"
1839 "#extension GL_ARB_separate_shader_objects : enable\n"
1840 "#extension GL_ARB_shading_language_420pack : enable\n"
1841 "layout (binding=0) uniform sampler2D surface;\n"
1842 "layout (location=0) out vec4 outColor;\n"
Tony Barbour88ad4b22014-11-18 17:01:30 -07001843 "layout (location=0) in vec2 UV;\n"
Tony Barbour32f368c2014-10-30 14:29:04 -06001844 "void main() {\n"
Tony Barbour88ad4b22014-11-18 17:01:30 -07001845 " outColor= textureLod(surface, UV, 0.0);\n"
Tony Barbour32f368c2014-10-30 14:29:04 -06001846 "}\n";
1847 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
1848
1849 glm::mat4 View = glm::lookAt(
1850 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1851 glm::vec3(0,0,0), // and looks at the origin
1852 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1853 );
1854
1855 glm::mat4 Model = glm::mat4(1.0f);
1856
1857 glm::mat4 MVP = Projection * View * Model;
1858
1859
1860 ASSERT_NO_FATAL_FAILURE(InitState());
1861 ASSERT_NO_FATAL_FAILURE(InitViewport());
1862 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1863
1864 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1865 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1866 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1867
1868
1869 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1870
1871 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1872 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1873 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1874 XglSamplerObj sampler(m_device);
1875 XglTextureObj texture(m_device);
1876
1877 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
Tony Barbour685f4332014-11-20 15:06:56 -07001878 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, &mvpBuffer);
1879 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, &texture);
1880 ps.BindShaderEntitySlotToSampler(0, &sampler);
Tony Barbour32f368c2014-10-30 14:29:04 -06001881
1882 XglPipelineObj pipelineobj(m_device);
1883 pipelineobj.AddShader(&vs);
1884 pipelineobj.AddShader(&ps);
1885
1886 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour88ad4b22014-11-18 17:01:30 -07001887
Tony Barbour685f4332014-11-20 15:06:56 -07001888 descriptorSet.AttachMemoryView(&mvpBuffer);
1889 descriptorSet.AttachImageView(&texture);
1890 descriptorSet.AttachSampler(&sampler);
Tony Barbour32f368c2014-10-30 14:29:04 -06001891
Tony Barbour685f4332014-11-20 15:06:56 -07001892 m_memoryRefManager.AddMemoryRef(&meshBuffer);
1893 m_memoryRefManager.AddMemoryRef(&mvpBuffer);
1894 m_memoryRefManager.AddMemoryRef(&texture);
Tony Barbour32f368c2014-10-30 14:29:04 -06001895
1896 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1897 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1898 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1899 };
1900
1901 // this is the current description of g_vbData
1902 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1903 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1904 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1905 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1906 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1907 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1908 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1909 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1910 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1911
1912 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1913 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
Tony Barbour2af38f82014-12-01 13:29:55 -07001914 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
Tony Barbour32f368c2014-10-30 14:29:04 -06001915
1916 ClearDepthStencil(1.0f);
1917 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
1918
1919 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1920
1921}
1922int main(int argc, char **argv) {
1923 int result;
1924
1925 ::testing::InitGoogleTest(&argc, argv);
1926 XglTestFramework::InitArgs(&argc, argv);
1927
1928 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
1929
1930 result = RUN_ALL_TESTS();
1931
1932 XglTestFramework::Finish();
1933 return result;
1934}