Courtney Goeltzenleuchter | b85c581 | 2014-08-19 18:35:50 -0600 | [diff] [blame^] | 1 | // 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 | |
| 54 | // Basic rendering tests |
| 55 | |
| 56 | #include <stdlib.h> |
| 57 | #include <stdio.h> |
| 58 | #include <stdbool.h> |
| 59 | #include <string.h> |
| 60 | |
| 61 | #include <xgl.h> |
| 62 | #include "gtest-1.7.0/include/gtest/gtest.h" |
| 63 | |
| 64 | #include "xgldevice.h" |
| 65 | |
| 66 | class XglRenderTest : public ::testing::Test { |
| 67 | public: |
| 68 | void CreateImage(XGL_UINT w, XGL_UINT h); |
| 69 | void DestroyImage(); |
| 70 | |
| 71 | void CreateImageView(XGL_IMAGE_VIEW_CREATE_INFO* pCreateInfo, |
| 72 | XGL_IMAGE_VIEW* pView); |
| 73 | void DestroyImageView(XGL_IMAGE_VIEW imageView); |
| 74 | XGL_DEVICE device() {return m_device->device();} |
| 75 | |
| 76 | protected: |
| 77 | XGL_APPLICATION_INFO app_info; |
| 78 | XGL_PHYSICAL_GPU objs[MAX_GPUS]; |
| 79 | XGL_UINT gpu_count; |
| 80 | XGL_IMAGE m_image; |
| 81 | XGL_GPU_MEMORY m_image_mem; |
| 82 | XglDevice *m_device; |
| 83 | |
| 84 | virtual void SetUp() { |
| 85 | XGL_RESULT err; |
| 86 | |
| 87 | this->app_info.sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO; |
| 88 | this->app_info.pNext = NULL; |
| 89 | this->app_info.pAppName = (const XGL_CHAR *) "base"; |
| 90 | this->app_info.appVersion = 1; |
| 91 | this->app_info.pEngineName = (const XGL_CHAR *) "unittest"; |
| 92 | this->app_info.engineVersion = 1; |
| 93 | this->app_info.apiVersion = XGL_MAKE_VERSION(0, 22, 0); |
| 94 | |
| 95 | err = xglInitAndEnumerateGpus(&app_info, NULL, |
| 96 | MAX_GPUS, &this->gpu_count, objs); |
| 97 | ASSERT_XGL_SUCCESS(err); |
| 98 | ASSERT_GE(1, this->gpu_count) << "No GPU available"; |
| 99 | |
| 100 | m_device = new XglDevice(0, objs[0]); |
| 101 | m_device->get_device_queue(); |
| 102 | } |
| 103 | |
| 104 | virtual void TearDown() { |
| 105 | xglInitAndEnumerateGpus(&this->app_info, XGL_NULL_HANDLE, 0, &gpu_count, XGL_NULL_HANDLE); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | |
| 110 | void XglRenderTest::CreateImage(XGL_UINT w, XGL_UINT h) |
| 111 | { |
| 112 | XGL_RESULT err; |
| 113 | XGL_IMAGE image; |
| 114 | XGL_UINT mipCount; |
| 115 | XGL_SIZE size; |
| 116 | XGL_FORMAT fmt; |
| 117 | XGL_FORMAT_PROPERTIES image_fmt; |
| 118 | |
| 119 | mipCount = 0; |
| 120 | |
| 121 | XGL_UINT _w = w; |
| 122 | XGL_UINT _h = h; |
| 123 | while( ( _w > 0 ) || ( _h > 0 ) ) |
| 124 | { |
| 125 | _w >>= 1; |
| 126 | _h >>= 1; |
| 127 | mipCount++; |
| 128 | } |
| 129 | |
| 130 | fmt.channelFormat = XGL_CH_FMT_R8G8B8A8; |
| 131 | fmt.numericFormat = XGL_NUM_FMT_UINT; |
| 132 | // TODO: Pick known good format rather than just expect common format |
| 133 | /* |
| 134 | * XXX: What should happen if given NULL HANDLE for the pData argument? |
| 135 | * We're not requesting XGL_INFO_TYPE_MEMORY_REQUIREMENTS so there is |
| 136 | * an expectation that pData is a valid pointer. |
| 137 | * However, why include a returned size value? That implies that the |
| 138 | * amount of data may vary and that doesn't work well for using a |
| 139 | * fixed structure. |
| 140 | */ |
| 141 | |
| 142 | err = xglGetFormatInfo(this->m_device->device(), fmt, |
| 143 | XGL_INFO_TYPE_FORMAT_PROPERTIES, |
| 144 | &size, &image_fmt); |
| 145 | ASSERT_XGL_SUCCESS(err); |
| 146 | |
| 147 | // typedef struct _XGL_IMAGE_CREATE_INFO |
| 148 | // { |
| 149 | // XGL_STRUCTURE_TYPE sType; // Must be XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO |
| 150 | // const XGL_VOID* pNext; // Pointer to next structure. |
| 151 | // XGL_IMAGE_TYPE imageType; |
| 152 | // XGL_FORMAT format; |
| 153 | // XGL_EXTENT3D extent; |
| 154 | // XGL_UINT mipLevels; |
| 155 | // XGL_UINT arraySize; |
| 156 | // XGL_UINT samples; |
| 157 | // XGL_IMAGE_TILING tiling; |
| 158 | // XGL_FLAGS usage; // XGL_IMAGE_USAGE_FLAGS |
| 159 | // XGL_FLAGS flags; // XGL_IMAGE_CREATE_FLAGS |
| 160 | // } XGL_IMAGE_CREATE_INFO; |
| 161 | |
| 162 | |
| 163 | XGL_IMAGE_CREATE_INFO imageCreateInfo = {}; |
| 164 | imageCreateInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO; |
| 165 | imageCreateInfo.imageType = XGL_IMAGE_2D; |
| 166 | imageCreateInfo.format = fmt; |
| 167 | imageCreateInfo.arraySize = 1; |
| 168 | imageCreateInfo.extent.width = w; |
| 169 | imageCreateInfo.extent.height = h; |
| 170 | imageCreateInfo.extent.depth = 1; |
| 171 | imageCreateInfo.mipLevels = mipCount; |
| 172 | imageCreateInfo.samples = 1; |
| 173 | imageCreateInfo.tiling = XGL_LINEAR_TILING; |
| 174 | |
| 175 | // Image usage flags |
| 176 | // typedef enum _XGL_IMAGE_USAGE_FLAGS |
| 177 | // { |
| 178 | // XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT = 0x00000001, |
| 179 | // XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT = 0x00000002, |
| 180 | // XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT = 0x00000004, |
| 181 | // XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT = 0x00000008, |
| 182 | // } XGL_IMAGE_USAGE_FLAGS; |
| 183 | imageCreateInfo.usage = XGL_IMAGE_USAGE_SHADER_ACCESS_WRITE_BIT | XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 184 | |
| 185 | // XGL_RESULT XGLAPI xglCreateImage( |
| 186 | // XGL_DEVICE device, |
| 187 | // const XGL_IMAGE_CREATE_INFO* pCreateInfo, |
| 188 | // XGL_IMAGE* pImage); |
| 189 | err = xglCreateImage(device(), &imageCreateInfo, &m_image); |
| 190 | ASSERT_XGL_SUCCESS(err); |
| 191 | |
| 192 | XGL_MEMORY_REQUIREMENTS mem_req; |
| 193 | XGL_UINT data_size; |
| 194 | err = xglGetObjectInfo(image, XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 195 | &data_size, &mem_req); |
| 196 | ASSERT_XGL_SUCCESS(err); |
| 197 | ASSERT_EQ(data_size, sizeof(mem_req)); |
| 198 | ASSERT_NE(0, mem_req.size) << "xglGetObjectInfo (Event): Failed - expect images to require memory"; |
| 199 | |
| 200 | // XGL_RESULT XGLAPI xglAllocMemory( |
| 201 | // XGL_DEVICE device, |
| 202 | // const XGL_MEMORY_ALLOC_INFO* pAllocInfo, |
| 203 | // XGL_GPU_MEMORY* pMem); |
| 204 | XGL_MEMORY_ALLOC_INFO mem_info; |
| 205 | |
| 206 | memset(&mem_info, 0, sizeof(mem_info)); |
| 207 | mem_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 208 | mem_info.allocationSize = mem_req.size; |
| 209 | mem_info.alignment = mem_req.alignment; |
| 210 | mem_info.heapCount = mem_req.heapCount; |
| 211 | memcpy(mem_info.heaps, mem_req.heaps, sizeof(XGL_UINT)*XGL_MAX_MEMORY_HEAPS); |
| 212 | mem_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 213 | mem_info.flags = XGL_MEMORY_ALLOC_SHAREABLE_BIT; |
| 214 | err = xglAllocMemory(device(), &mem_info, &m_image_mem); |
| 215 | ASSERT_XGL_SUCCESS(err); |
| 216 | |
| 217 | err = xglBindObjectMemory(image, m_image_mem, 0); |
| 218 | ASSERT_XGL_SUCCESS(err); |
| 219 | } |
| 220 | |
| 221 | void XglRenderTest::DestroyImage() |
| 222 | { |
| 223 | // All done with image memory, clean up |
| 224 | ASSERT_XGL_SUCCESS(xglBindObjectMemory(m_image, XGL_NULL_HANDLE, 0)); |
| 225 | |
| 226 | ASSERT_XGL_SUCCESS(xglFreeMemory(m_image_mem)); |
| 227 | |
| 228 | ASSERT_XGL_SUCCESS(xglDestroyObject(m_image)); |
| 229 | } |
| 230 | |
| 231 | void XglRenderTest::CreateImageView(XGL_IMAGE_VIEW_CREATE_INFO *pCreateInfo, |
| 232 | XGL_IMAGE_VIEW *pView) |
| 233 | { |
| 234 | pCreateInfo->image = this->m_image; |
| 235 | ASSERT_XGL_SUCCESS(xglCreateImageView(device(), pCreateInfo, pView)); |
| 236 | } |
| 237 | |
| 238 | void XglRenderTest::DestroyImageView(XGL_IMAGE_VIEW imageView) |
| 239 | { |
| 240 | ASSERT_XGL_SUCCESS(xglDestroyObject(imageView)); |
| 241 | } |
| 242 | |
| 243 | TEST_F(XglRenderTest, DrawTriangleTest) { |
| 244 | XGL_FORMAT fmt; |
| 245 | XGL_IMAGE_VIEW imageView; |
| 246 | XGL_RESULT err; |
| 247 | |
| 248 | fmt.channelFormat = XGL_CH_FMT_R8G8B8A8; |
| 249 | fmt.numericFormat = XGL_NUM_FMT_UINT; |
| 250 | |
| 251 | CreateImage(512, 256); |
| 252 | |
| 253 | // typedef struct _XGL_IMAGE_VIEW_CREATE_INFO |
| 254 | // { |
| 255 | // XGL_STRUCTURE_TYPE sType; // Must be XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO |
| 256 | // const XGL_VOID* pNext; // Pointer to next structure |
| 257 | // XGL_IMAGE image; |
| 258 | // XGL_IMAGE_VIEW_TYPE viewType; |
| 259 | // XGL_FORMAT format; |
| 260 | // XGL_CHANNEL_MAPPING channels; |
| 261 | // XGL_IMAGE_SUBRESOURCE_RANGE subresourceRange; |
| 262 | // XGL_FLOAT minLod; |
| 263 | // } XGL_IMAGE_VIEW_CREATE_INFO; |
| 264 | XGL_IMAGE_VIEW_CREATE_INFO viewInfo = {}; |
| 265 | viewInfo.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; |
| 266 | viewInfo.viewType = XGL_IMAGE_VIEW_2D; |
| 267 | viewInfo.format = fmt; |
| 268 | |
| 269 | viewInfo.channels.r = XGL_CHANNEL_SWIZZLE_R; |
| 270 | viewInfo.channels.g = XGL_CHANNEL_SWIZZLE_G; |
| 271 | viewInfo.channels.b = XGL_CHANNEL_SWIZZLE_B; |
| 272 | viewInfo.channels.a = XGL_CHANNEL_SWIZZLE_A; |
| 273 | |
| 274 | viewInfo.subresourceRange.baseArraySlice = 0; |
| 275 | viewInfo.subresourceRange.arraySize = 1; |
| 276 | viewInfo.subresourceRange.baseMipLevel = 0; |
| 277 | viewInfo.subresourceRange.mipLevels = 1; |
| 278 | viewInfo.subresourceRange.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 279 | |
| 280 | // XGL_RESULT XGLAPI xglCreateImageView( |
| 281 | // XGL_DEVICE device, |
| 282 | // const XGL_IMAGE_VIEW_CREATE_INFO* pCreateInfo, |
| 283 | // XGL_IMAGE_VIEW* pView); |
| 284 | |
| 285 | CreateImageView(&viewInfo, &imageView); |
| 286 | |
| 287 | DestroyImageView(imageView); |
| 288 | } |
| 289 | |
| 290 | int main(int argc, char **argv) { |
| 291 | ::testing::InitGoogleTest(&argc, argv); |
| 292 | return RUN_ALL_TESTS(); |
| 293 | } |