blob: 8c691d46fa64541ce380ab06f44673a28dd977db [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);
629 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &constantBuffer.m_constantBufferView);
630
631 XglPipelineObj pipelineobj(m_device);
632 pipelineobj.AddShader(&vs);
633 pipelineobj.AddShader(&ps);
634
635 XglDescriptorSetObj descriptorSet(m_device);
636 descriptorSet.AttachMemoryView(&constantBuffer.m_constantBufferView);
637 m_memoryRefManager.AddMemoryRef(&constantBuffer.m_constantBufferMem);
638
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);
930 ps.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &constantBuffer.m_constantBufferView);
931
932 XglPipelineObj pipelineobj(m_device);
933 pipelineobj.AddShader(&vs);
934 pipelineobj.AddShader(&ps);
935
936 XglDescriptorSetObj descriptorSet(m_device);
937 descriptorSet.AttachMemoryView(&constantBuffer.m_constantBufferView);
938
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);
981 descriptorSet.AttachMemoryView(&meshBuffer.m_constantBufferView);
982
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 Barbour1f9859c2014-12-01 13:25:15 -07001044 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &MVPBuffer.m_constantBufferView);
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
1051 XglDescriptorSetObj descriptorSet(m_device); XGL_RESULT err = XGL_SUCCESS;
Tony Barbour1f9859c2014-12-01 13:25:15 -07001052 descriptorSet.AttachMemoryView(&MVPBuffer.m_constantBufferView);
Tony Barbour32f368c2014-10-30 14:29:04 -06001053
Tony Barbour1f9859c2014-12-01 13:25:15 -07001054 m_memoryRefManager.AddMemoryRef(&MVPBuffer.m_constantBufferMem);
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);
1160 descriptorSet.AttachMemoryView(&meshBuffer.m_constantBufferView);
1161
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 Barbour88ad4b22014-11-18 17:01:30 -07001184TEST_F(XglRenderTest, CubeWithVertexFetchAndMVP)
Tony Barbour32f368c2014-10-30 14:29:04 -06001185{
1186 static const char *vertShaderText =
1187 "#version 140\n"
1188 "layout (std140) uniform bufferVals {\n"
1189 " mat4 mvp;\n"
1190 "} myBufferVals;\n"
1191 "in vec4 pos;\n"
1192 "in vec4 inColor;\n"
1193 "out vec4 outColor;\n"
1194 "void main() {\n"
1195 " outColor = inColor;\n"
1196 " gl_Position = myBufferVals.mvp * pos;\n"
1197 "}\n";
1198
1199 static const char *fragShaderText =
1200 "#version 130\n"
1201 "in vec4 color;\n"
1202 "void main() {\n"
1203 " gl_FragColor = color;\n"
1204 "}\n";
1205 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
1206
1207 glm::mat4 View = glm::lookAt(
1208 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1209 glm::vec3(0,0,0), // and looks at the origin
1210 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1211 );
1212
1213 glm::mat4 Model = glm::mat4(1.0f);
1214
1215 glm::mat4 MVP = Projection * View * Model;
1216
1217 ASSERT_NO_FATAL_FAILURE(InitState());
1218 ASSERT_NO_FATAL_FAILURE(InitViewport());
1219 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1220
1221 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1222 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1223
Tony Barbour32f368c2014-10-30 14:29:04 -06001224 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1225
Tony Barbour1f9859c2014-12-01 13:25:15 -07001226 XglConstantBufferObj MVPBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
Tony Barbour32f368c2014-10-30 14:29:04 -06001227 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1228 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1229
Tony Barbour1f9859c2014-12-01 13:25:15 -07001230 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &MVPBuffer.m_constantBufferView);
Tony Barbour32f368c2014-10-30 14:29:04 -06001231
1232 XglPipelineObj pipelineobj(m_device);
1233 pipelineobj.AddShader(&vs);
1234 pipelineobj.AddShader(&ps);
1235
1236 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour1f9859c2014-12-01 13:25:15 -07001237 descriptorSet.AttachMemoryView(&MVPBuffer.m_constantBufferView);
Tony Barbour32f368c2014-10-30 14:29:04 -06001238
1239 m_memoryRefManager.AddMemoryRef(&meshBuffer.m_constantBufferMem);
Tony Barbour1f9859c2014-12-01 13:25:15 -07001240 m_memoryRefManager.AddMemoryRef(&MVPBuffer.m_constantBufferMem);
Tony Barbour32f368c2014-10-30 14:29:04 -06001241
1242 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1243 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1244 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1245 };
1246
1247 // this is the current description of g_vbData
1248 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1249 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1250 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1251 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1252 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1253 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1254 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1255 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1256 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1257
1258 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1259 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
Tony Barbour2af38f82014-12-01 13:29:55 -07001260 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
Tony Barbour32f368c2014-10-30 14:29:04 -06001261
1262 ClearDepthStencil(1.0f);
1263 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
1264
1265 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1266
1267}
1268
1269TEST_F(XglRenderTest, VSTexture)
1270{
1271 // The expected result from this test is a green and red triangle;
1272 // one red vertex on the left, two green vertices on the right.
1273 static const char *vertShaderText =
1274 "#version 130\n"
1275 "out vec4 texColor;\n"
1276 "uniform sampler2D surface;\n"
1277 "void main() {\n"
1278 " vec2 vertices[3];"
1279 " vertices[0] = vec2(-0.5, -0.5);\n"
1280 " vertices[1] = vec2( 0.5, -0.5);\n"
1281 " vertices[2] = vec2( 0.5, 0.5);\n"
1282 " vec2 positions[3];"
1283 " positions[0] = vec2( 0.0, 0.0);\n"
1284 " positions[1] = vec2( 0.25, 0.1);\n"
1285 " positions[2] = vec2( 0.1, 0.25);\n"
1286 " vec2 samplePos = positions[gl_VertexID % 3];\n"
1287 " texColor = textureLod(surface, samplePos, 0.0);\n"
1288 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1289 "}\n";
1290
1291 static const char *fragShaderText =
1292 "#version 130\n"
1293 "in vec4 texColor;\n"
1294 "void main() {\n"
1295 " gl_FragColor = texColor;\n"
1296 "}\n";
1297
1298 ASSERT_NO_FATAL_FAILURE(InitState());
1299 ASSERT_NO_FATAL_FAILURE(InitViewport());
1300
1301 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1302 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1303 XglSamplerObj sampler(m_device);
1304 XglTextureObj texture(m_device);
1305
1306 vs.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &texture.m_textureViewInfo);
1307 vs.BindShaderEntitySlotToSampler(0, (XGL_OBJECT) &sampler.m_sampler);
1308
1309 XglPipelineObj pipelineobj(m_device);
1310 pipelineobj.AddShader(&vs);
1311 pipelineobj.AddShader(&ps);
1312
1313 XglDescriptorSetObj descriptorSet(m_device);
1314 descriptorSet.AttachImageView(&texture.m_textureViewInfo);
1315 descriptorSet.AttachSampler(&sampler.m_sampler);
1316
1317 m_memoryRefManager.AddMemoryRef(&texture.m_textureMem);
1318
1319 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1320 QueueCommandBuffer(NULL, 0);
1321
1322}
Tony Barbourf12bfb92014-11-19 16:33:11 -07001323TEST_F(XglRenderTest, TexturedTriangle)
1324{
1325 // The expected result from this test is a red and green checkered triangle
1326 static const char *vertShaderText =
1327 "#version 140\n"
1328 "#extension GL_ARB_separate_shader_objects : enable\n"
1329 "#extension GL_ARB_shading_language_420pack : enable\n"
1330 "layout (location = 0) out vec2 samplePos;\n"
1331 "void main() {\n"
1332 " vec2 vertices[3];"
1333 " vertices[0] = vec2(-0.5, -0.5);\n"
1334 " vertices[1] = vec2( 0.5, -0.5);\n"
1335 " vertices[2] = vec2( 0.5, 0.5);\n"
1336 " vec2 positions[3];"
1337 " positions[0] = vec2( 0.0, 0.0);\n"
1338 " positions[1] = vec2( 1.0, 0.0);\n"
1339 " positions[2] = vec2( 1.0, 1.0);\n"
1340 " samplePos = positions[gl_VertexID % 3];\n"
1341 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1342 "}\n";
Tony Barbour32f368c2014-10-30 14:29:04 -06001343
Tony Barbourf12bfb92014-11-19 16:33:11 -07001344 static const char *fragShaderText =
1345 "#version 140\n"
1346 "#extension GL_ARB_separate_shader_objects : enable\n"
1347 "#extension GL_ARB_shading_language_420pack : enable\n"
1348 "layout (location = 0) in vec2 samplePos;\n"
1349 "layout (binding = 0) uniform sampler2D surface;\n"
1350 "layout (location=0) out vec4 outColor;\n"
1351 "void main() {\n"
1352 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1353 " outColor = texColor;\n"
1354 "}\n";
1355
1356 ASSERT_NO_FATAL_FAILURE(InitState());
1357 ASSERT_NO_FATAL_FAILURE(InitViewport());
1358
1359 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1360 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1361 XglSamplerObj sampler(m_device);
1362 XglTextureObj texture(m_device);
1363
1364 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &texture.m_textureViewInfo);
1365 ps.BindShaderEntitySlotToSampler(0, (XGL_OBJECT) &sampler.m_sampler);
1366
1367 XglPipelineObj pipelineobj(m_device);
1368 pipelineobj.AddShader(&vs);
1369 pipelineobj.AddShader(&ps);
1370
1371 XglDescriptorSetObj descriptorSet(m_device);
1372 descriptorSet.AttachImageView(&texture.m_textureViewInfo);
1373 descriptorSet.AttachSampler(&sampler.m_sampler);
1374
1375 m_memoryRefManager.AddMemoryRef(&texture.m_textureMem);
1376
1377 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1378 QueueCommandBuffer(NULL, 0);
1379}
1380TEST_F(XglRenderTest, TexturedTriangleClip)
1381{
1382 // The expected result from this test is a red and green checkered triangle
1383 static const char *vertShaderText =
1384 "#version 330\n"
1385 "#extension GL_ARB_separate_shader_objects : enable\n"
1386 "#extension GL_ARB_shading_language_420pack : enable\n"
1387 "layout (location = 0) out vec2 samplePos;\n"
1388 "out gl_PerVertex {\n"
1389 " vec4 gl_Position;\n"
1390 " float gl_ClipDistance[1];\n"
1391 "};\n"
1392 "void main() {\n"
1393 " vec2 vertices[3];"
1394 " vertices[0] = vec2(-0.5, -0.5);\n"
1395 " vertices[1] = vec2( 0.5, -0.5);\n"
1396 " vertices[2] = vec2( 0.5, 0.5);\n"
1397 " vec2 positions[3];"
1398 " positions[0] = vec2( 0.0, 0.0);\n"
1399 " positions[1] = vec2( 1.0, 0.0);\n"
1400 " positions[2] = vec2( 1.0, 1.0);\n"
1401 " float dists[3];\n"
1402 " dists[0] = 1.0;\n"
1403 " dists[1] = 1.0;\n"
1404 " dists[2] = -1.0;\n"
1405 " gl_ClipDistance[0] = dists[gl_VertexID % 3];\n"
1406 " samplePos = positions[gl_VertexID % 3];\n"
1407 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1408 "}\n";
1409
1410 static const char *fragShaderText =
1411 "#version 140\n"
1412 "#extension GL_ARB_separate_shader_objects : enable\n"
1413 "#extension GL_ARB_shading_language_420pack : enable\n"
1414 "layout (location = 0) in vec2 samplePos;\n"
1415 "layout (binding = 0) uniform sampler2D surface;\n"
1416 "layout (location=0) out vec4 outColor;\n"
1417 "void main() {\n"
1418 //" vec4 texColor = textureLod(surface, samplePos, 0.0 + gl_ClipDistance[0]);\n"
1419 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1420 " outColor = texColor;\n"
1421 "}\n";
1422
1423
1424 ASSERT_NO_FATAL_FAILURE(InitState());
1425 ASSERT_NO_FATAL_FAILURE(InitViewport());
1426
1427 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1428 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1429 XglSamplerObj sampler(m_device);
1430 XglTextureObj texture(m_device);
1431
1432 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &texture.m_textureViewInfo);
1433 ps.BindShaderEntitySlotToSampler(0, (XGL_OBJECT) &sampler.m_sampler);
1434
1435 XglPipelineObj pipelineobj(m_device);
1436 pipelineobj.AddShader(&vs);
1437 pipelineobj.AddShader(&ps);
1438
1439 XglDescriptorSetObj descriptorSet(m_device);
1440 descriptorSet.AttachImageView(&texture.m_textureViewInfo);
1441 descriptorSet.AttachSampler(&sampler.m_sampler);
1442
1443 m_memoryRefManager.AddMemoryRef(&texture.m_textureMem);
1444
1445 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1446 QueueCommandBuffer(NULL, 0);
1447}
Tony Barbour88ad4b22014-11-18 17:01:30 -07001448TEST_F(XglRenderTest, FSTriangle)
1449{
1450 // The expected result from this test is a red and green checkered triangle
1451 static const char *vertShaderText =
1452 "#version 140\n"
1453 "#extension GL_ARB_separate_shader_objects : enable\n"
1454 "#extension GL_ARB_shading_language_420pack : enable\n"
1455 "layout (location = 0) out vec2 samplePos;\n"
1456 "void main() {\n"
1457 " vec2 vertices[3];"
1458 " vertices[0] = vec2(-0.5, -0.5);\n"
1459 " vertices[1] = vec2( 0.5, -0.5);\n"
1460 " vertices[2] = vec2( 0.5, 0.5);\n"
1461 " vec2 positions[3];"
1462 " positions[0] = vec2( 0.0, 0.0);\n"
1463 " positions[1] = vec2( 1.0, 0.0);\n"
1464 " positions[2] = vec2( 1.0, 1.0);\n"
1465 " samplePos = positions[gl_VertexID % 3];\n"
1466 " gl_Position = vec4(vertices[gl_VertexID % 3], 0.0, 1.0);\n"
1467 "}\n";
1468
1469 static const char *fragShaderText =
1470 "#version 140\n"
1471 "#extension GL_ARB_separate_shader_objects : enable\n"
1472 "#extension GL_ARB_shading_language_420pack : enable\n"
1473 "layout (location = 0) in vec2 samplePos;\n"
1474 "layout (binding = 0) uniform sampler2D surface;\n"
1475 "layout (location=0) out vec4 outColor;\n"
1476 "void main() {\n"
1477 " vec4 texColor = textureLod(surface, samplePos, 0.0);\n"
1478 " outColor = texColor;\n"
1479 "}\n";
1480
1481 ASSERT_NO_FATAL_FAILURE(InitState());
1482 ASSERT_NO_FATAL_FAILURE(InitViewport());
1483
1484 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1485 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1486 XglSamplerObj sampler(m_device);
1487 XglTextureObj texture(m_device);
1488
1489 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &texture.m_textureViewInfo);
1490 ps.BindShaderEntitySlotToSampler(0, (XGL_OBJECT) &sampler.m_sampler);
1491
1492 XglPipelineObj pipelineobj(m_device);
1493 pipelineobj.AddShader(&vs);
1494 pipelineobj.AddShader(&ps);
1495
1496 XglDescriptorSetObj descriptorSet(m_device);
1497 descriptorSet.AttachImageView(&texture.m_textureViewInfo);
1498 descriptorSet.AttachSampler(&sampler.m_sampler);
1499
1500 m_memoryRefManager.AddMemoryRef(&texture.m_textureMem);
1501
1502 GenericDrawTriangleTest(pipelineobj, descriptorSet, 1);
1503 QueueCommandBuffer(NULL, 0);
1504}
1505
1506TEST_F(XglRenderTest, CubeWithVertexFetchAndMVPAndTexture)
Tony Barbour32f368c2014-10-30 14:29:04 -06001507{
1508 static const char *vertShaderText =
1509 "#version 140\n"
1510 "#extension GL_ARB_separate_shader_objects : enable\n"
1511 "#extension GL_ARB_shading_language_420pack : enable\n"
1512 "layout (std140, binding=0) uniform bufferVals {\n"
1513 " mat4 mvp;\n"
1514 "} myBufferVals;\n"
1515 "layout (location=0) in vec4 pos;\n"
Tony Barbour88ad4b22014-11-18 17:01:30 -07001516 "layout (location=0) out vec2 UV;\n"
Tony Barbour32f368c2014-10-30 14:29:04 -06001517 "void main() {\n"
1518 " vec2 positions[3];"
1519 " positions[0] = vec2( 0.0, 0.0);\n"
1520 " positions[1] = vec2( 0.25, 0.1);\n"
1521 " positions[2] = vec2( 0.1, 0.25);\n"
Tony Barbour88ad4b22014-11-18 17:01:30 -07001522 " UV = positions[gl_VertexID % 3];\n"
Tony Barbour32f368c2014-10-30 14:29:04 -06001523 " gl_Position = myBufferVals.mvp * pos;\n"
1524 "}\n";
1525
1526 static const char *fragShaderText =
1527 "#version 140\n"
1528 "#extension GL_ARB_separate_shader_objects : enable\n"
1529 "#extension GL_ARB_shading_language_420pack : enable\n"
1530 "layout (binding=0) uniform sampler2D surface;\n"
1531 "layout (location=0) out vec4 outColor;\n"
Tony Barbour88ad4b22014-11-18 17:01:30 -07001532 "layout (location=0) in vec2 UV;\n"
Tony Barbour32f368c2014-10-30 14:29:04 -06001533 "void main() {\n"
Tony Barbour88ad4b22014-11-18 17:01:30 -07001534 " outColor= textureLod(surface, UV, 0.0);\n"
Tony Barbour32f368c2014-10-30 14:29:04 -06001535 "}\n";
1536 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
1537
1538 glm::mat4 View = glm::lookAt(
1539 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
1540 glm::vec3(0,0,0), // and looks at the origin
1541 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
1542 );
1543
1544 glm::mat4 Model = glm::mat4(1.0f);
1545
1546 glm::mat4 MVP = Projection * View * Model;
1547
1548
1549 ASSERT_NO_FATAL_FAILURE(InitState());
1550 ASSERT_NO_FATAL_FAILURE(InitViewport());
1551 ASSERT_NO_FATAL_FAILURE(InitDepthStencil());
1552
1553 XglConstantBufferObj meshBuffer(m_device,sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]),
1554 sizeof(g_vb_solid_face_colors_Data[0]), g_vb_solid_face_colors_Data);
1555 meshBuffer.SetMemoryState(m_cmdBuffer,XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY);
1556
1557
1558 const int buf_size = sizeof(MVP) / sizeof(XGL_FLOAT);
1559
1560 XglConstantBufferObj mvpBuffer(m_device, buf_size, sizeof(MVP[0]), (const void*) &MVP[0][0]);
1561 XglShaderObj vs(m_device,vertShaderText,XGL_SHADER_STAGE_VERTEX );
1562 XglShaderObj ps(m_device,fragShaderText, XGL_SHADER_STAGE_FRAGMENT);
1563 XglSamplerObj sampler(m_device);
1564 XglTextureObj texture(m_device);
1565
1566 // vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_VERTEX_INPUT, (XGL_OBJECT) &meshBuffer.m_constantBufferView);
1567 vs.BindShaderEntitySlotToMemory(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &mvpBuffer.m_constantBufferView);
Tony Barbour88ad4b22014-11-18 17:01:30 -07001568 ps.BindShaderEntitySlotToImage(0, XGL_SLOT_SHADER_RESOURCE, (XGL_OBJECT) &texture.m_textureViewInfo);
Tony Barbour32f368c2014-10-30 14:29:04 -06001569 ps.BindShaderEntitySlotToSampler(0, (XGL_OBJECT) &sampler.m_sampler);
1570
1571 XglPipelineObj pipelineobj(m_device);
1572 pipelineobj.AddShader(&vs);
1573 pipelineobj.AddShader(&ps);
1574
1575 XglDescriptorSetObj descriptorSet(m_device);
Tony Barbour88ad4b22014-11-18 17:01:30 -07001576
Tony Barbour32f368c2014-10-30 14:29:04 -06001577 descriptorSet.AttachMemoryView(&mvpBuffer.m_constantBufferView);
Tony Barbour88ad4b22014-11-18 17:01:30 -07001578 // descriptorSet.AttachMemoryView(&meshBuffer.m_constantBufferView);
Tony Barbour32f368c2014-10-30 14:29:04 -06001579 descriptorSet.AttachImageView(&texture.m_textureViewInfo);
1580 descriptorSet.AttachSampler(&sampler.m_sampler);
1581
1582 m_memoryRefManager.AddMemoryRef(&meshBuffer.m_constantBufferMem);
1583 m_memoryRefManager.AddMemoryRef(&mvpBuffer.m_constantBufferMem);
1584 m_memoryRefManager.AddMemoryRef(&texture.m_textureMem);
1585
1586 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_binding = {
1587 sizeof(g_vbData[0]), // strideInBytes; Distance between vertices in bytes (0 = no advancement)
1588 XGL_VERTEX_INPUT_STEP_RATE_VERTEX // stepRate; // Rate at which binding is incremented
1589 };
1590
1591 // this is the current description of g_vbData
1592 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attribs[2];
1593 vi_attribs[0].binding = 0; // index into vertexBindingDescriptions
1594 vi_attribs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1595 vi_attribs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
1596 vi_attribs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
1597 vi_attribs[1].binding = 0; // index into vertexBindingDescriptions
1598 vi_attribs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
1599 vi_attribs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
1600 vi_attribs[1].offsetInBytes = 16; // Offset of first element in bytes from base of vertex
1601
1602 pipelineobj.AddVertexInputBindings(&vi_binding,1);
1603 pipelineobj.AddVertexInputAttribs(vi_attribs,2);
Tony Barbour2af38f82014-12-01 13:29:55 -07001604 pipelineobj.AddVertexDataBuffer(&meshBuffer,0);
Tony Barbour32f368c2014-10-30 14:29:04 -06001605
1606 ClearDepthStencil(1.0f);
1607 GenericDrawTriangleTest(pipelineobj, descriptorSet, 12);
1608
1609 QueueCommandBuffer(m_memoryRefManager.GetMemoryRefList(), m_memoryRefManager.GetNumRefs());
1610
1611}
1612int main(int argc, char **argv) {
1613 int result;
1614
1615 ::testing::InitGoogleTest(&argc, argv);
1616 XglTestFramework::InitArgs(&argc, argv);
1617
1618 ::testing::Environment* const xgl_test_env = ::testing::AddGlobalTestEnvironment(new TestEnvironment);
1619
1620 result = RUN_ALL_TESTS();
1621
1622 XglTestFramework::Finish();
1623 return result;
1624}