allocator.cpp 18.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#ifndef __APPLE__
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/gl.h>
#else
#include <GL/glew.h>
#include <OpenGL/gl.h>
#endif
#include "cuda/error.hpp"
#include "../common/glAllocator.hpp"
#include <cuda_gl_interop.h>
#include <cuda_runtime.h>

#include "surface.hpp"
#include "deviceBuffer.hpp"

#include "../common/allocStats.hpp"

#include "gpu/allocator.hpp"
#include "gpu/memcpy.hpp"

namespace VideoStitch {
namespace Core {

unsigned int getCudaGLMemAllocType(OpenGLAllocator::BufferAllocType flag) {
  switch (flag) {
    case OpenGLAllocator::BufferAllocType::ReadWrite:
      return cudaGraphicsMapFlagsNone;
    case OpenGLAllocator::BufferAllocType::ReadOnly:
      return cudaGraphicsMapFlagsReadOnly;
    case OpenGLAllocator::BufferAllocType::WriteOnly:
      return cudaGraphicsMapFlagsWriteDiscard;
  }

  assert(false);
  return cudaGraphicsMapFlagsNone;
}

Potential<SourceSurface> OffscreenAllocator::createAlphaSurface(size_t width, size_t height, const char* name) {
  cudaArray_t array;
  cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 0, 0, 0, cudaChannelFormatKindUnsigned);
  FAIL_RETURN(CUDA_ERROR(cudaMallocArray(&array, &channelDesc, width, height, cudaArraySurfaceLoadStore)));
  deviceStats.addPtr(name, array, width * height);

  // Specify surface
  struct cudaResourceDesc resDesc = {};
  resDesc.resType = cudaResourceTypeArray;
  // Create the surface objects
  resDesc.res.array.array = array;
  cudaSurfaceObject_t surface;
  FAIL_RETURN(CUDA_ERROR(cudaCreateSurfaceObject(&surface, &resDesc)));
  // create a texture
  cudaTextureObject_t tex;
  cudaTextureDesc texDesc = {};
  texDesc.filterMode = cudaFilterModeLinear;
  texDesc.addressMode[0] = cudaAddressModeWrap;
  texDesc.addressMode[1] = cudaAddressModeClamp;
  texDesc.normalizedCoords = false;
  texDesc.readMode = cudaReadModeNormalizedFloat;
  cudaResourceViewDesc resViewDesc = {};
  resViewDesc.format = cudaResViewFormatUnsignedChar1;
  resViewDesc.width = width;
  resViewDesc.height = height;
  cudaCreateTextureObject(&tex, &resDesc, &texDesc, &resViewDesc);

  auto gsurface = new GPU::Surface(new GPU::DeviceSurface(array, tex, surface, true), width, height);

  Potential<SourceSurface::Pimpl> impl = SourceSurface::Pimpl::create(gsurface);
  FAIL_RETURN(impl.status());

  return new SourceSurface(impl.release());
}

namespace {
Potential<GPU::Surface> makeSurface(std::string name, size_t width, size_t height) {
  cudaArray_t array;
  cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 8, 8, 8, cudaChannelFormatKindUnsigned);
  FAIL_RETURN(CUDA_ERROR(cudaMallocArray(&array, &channelDesc, width, height, cudaArraySurfaceLoadStore)));
  deviceStats.addPtr(name.c_str(), array, width * height * 4);

  // Specify surface
  struct cudaResourceDesc resDesc = {};
  resDesc.resType = cudaResourceTypeArray;
  // Create the surface objects
  resDesc.res.array.array = array;
  cudaSurfaceObject_t surface;
  FAIL_RETURN(CUDA_ERROR(cudaCreateSurfaceObject(&surface, &resDesc)));
  // create a texture
  cudaTextureObject_t tex;
  cudaTextureDesc texDesc = {};
  texDesc.filterMode = cudaFilterModeLinear;
  texDesc.addressMode[0] = cudaAddressModeWrap;
  texDesc.addressMode[1] = cudaAddressModeClamp;
  texDesc.normalizedCoords = false;
  texDesc.readMode = cudaReadModeNormalizedFloat;
  cudaResourceViewDesc resViewDesc = {};
  resViewDesc.format = cudaResViewFormatUnsignedChar4;
  resViewDesc.width = width;
  resViewDesc.height = height;
  FAIL_RETURN(CUDA_ERROR(cudaCreateTextureObject(&tex, &resDesc, &texDesc, &resViewDesc)));
  return new GPU::Surface(new GPU::DeviceSurface(array, tex, surface, true), width, height);
}

Potential<GPU::Surface> makeSurface_F32_C1(std::string name, size_t width, size_t height) {
  cudaArray_t array;
  cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
  FAIL_RETURN(CUDA_ERROR(cudaMallocArray(&array, &channelDesc, width, height, cudaArraySurfaceLoadStore)));
  deviceStats.addPtr(name.c_str(), array, width * height * sizeof(float));

  // Specify surface
  struct cudaResourceDesc resDesc;
  memset(&resDesc, 0, sizeof(resDesc));
  resDesc.resType = cudaResourceTypeArray;
  // Create the surface objects
  resDesc.res.array.array = array;
  cudaSurfaceObject_t surface;
  FAIL_RETURN(CUDA_ERROR(cudaCreateSurfaceObject(&surface, &resDesc)));
  // create a texture
  cudaTextureObject_t tex;
  cudaTextureDesc texDesc = {};
  texDesc.filterMode = cudaFilterModeLinear;
  texDesc.addressMode[0] = cudaAddressModeWrap;
  texDesc.addressMode[1] = cudaAddressModeClamp;
  texDesc.normalizedCoords = false;
  texDesc.readMode = cudaReadModeElementType;
  cudaResourceViewDesc resViewDesc;
  memset(&resViewDesc, 0, sizeof(cudaResourceViewDesc));
  resViewDesc.format = cudaResViewFormatFloat1;
  resViewDesc.width = width;
  resViewDesc.height = height;
  FAIL_RETURN(CUDA_ERROR(cudaCreateTextureObject(&tex, &resDesc, &texDesc, &resViewDesc)));
  return new GPU::Surface(new GPU::DeviceSurface(array, tex, surface, true), width, height);
}
}  // namespace

Potential<SourceSurface> OffscreenAllocator::createSourceSurface(size_t width, size_t height, const char* name) {
  Potential<GPU::Surface> potSurf = makeSurface(name, width, height);
  if (!potSurf.ok()) {
    return potSurf.status();
  }

  Potential<SourceSurface::Pimpl> impl = SourceSurface::Pimpl::create(potSurf.release());
  FAIL_RETURN(impl.status());

  return new SourceSurface(impl.release());
}

Potential<SourceSurface> OffscreenAllocator::createDepthSurface(size_t width, size_t height, const char* name) {
  Potential<GPU::Surface> potSurf = makeSurface_F32_C1(name, width, height);
  if (!potSurf.ok()) {
    return potSurf.status();
  }

  Potential<SourceSurface::Pimpl> impl = SourceSurface::Pimpl::create(potSurf.release());
  FAIL_RETURN(impl.status());

  return new SourceSurface(impl.release());
}

Potential<SourceSurface> OffscreenAllocator::createCoordSurface(size_t width, size_t height, const char* name) {
  cudaArray_t array;
  cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 32, 0, 0, cudaChannelFormatKindFloat);
  FAIL_RETURN(CUDA_ERROR(cudaMallocArray(&array, &channelDesc, width, height, cudaArraySurfaceLoadStore)));
  deviceStats.addPtr(name, array, width * height * 8);

  // Specify surface
  struct cudaResourceDesc resDesc = {};
  resDesc.resType = cudaResourceTypeArray;
  // Create the surface objects
  resDesc.res.array.array = array;
  cudaSurfaceObject_t surface;
  FAIL_RETURN(CUDA_ERROR(cudaCreateSurfaceObject(&surface, &resDesc)));
  // create a texture
  cudaTextureObject_t tex;
  cudaTextureDesc texDesc = {};
  texDesc.filterMode = cudaFilterModeLinear;
  texDesc.addressMode[0] = cudaAddressModeWrap;
  texDesc.addressMode[1] = cudaAddressModeClamp;
  texDesc.normalizedCoords = false;
  texDesc.readMode = cudaReadModeElementType;
  cudaResourceViewDesc resViewDesc = {};
  resViewDesc.format = cudaResViewFormatFloat2;
  resViewDesc.width = width;
  resViewDesc.height = height;
  FAIL_RETURN(CUDA_ERROR(cudaCreateTextureObject(&tex, &resDesc, &texDesc, &resViewDesc)));

  auto gsurface = new GPU::Surface(new GPU::DeviceSurface(array, tex, surface, true), width, height);

  Potential<SourceSurface::Pimpl> impl = SourceSurface::Pimpl::create(gsurface);
  FAIL_RETURN(impl.status());

  return new SourceSurface(impl.release());
}

Potential<SourceOpenGLSurface> OpenGLAllocator::createSourceSurface(size_t width, size_t height) {
  auto allocPotSurf = [](GLuint texture, size_t width, size_t height) -> Potential<SourceOpenGLSurface> {
    cudaGraphicsResource* image;
    FAIL_RETURN(CUDA_ERROR(
        cudaGraphicsGLRegisterImage(&image, texture, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsSurfaceLoadStore)))
    FAIL_RETURN(CUDA_ERROR(cudaGraphicsMapResources(1, &image, cudaStreamPerThread)))

    cudaArray_t array;
    FAIL_RETURN(CUDA_ERROR(cudaGraphicsSubResourceGetMappedArray(&array, image, 0, 0)))
    FAIL_RETURN(CUDA_ERROR(cudaGraphicsUnmapResources(1, &image, cudaStreamPerThread)))

    // Specify surface
    struct cudaResourceDesc resDesc = {};
    resDesc.resType = cudaResourceTypeArray;
    // Create the surface objects
    resDesc.res.array.array = array;
    cudaSurfaceObject_t surface;
    FAIL_RETURN(CUDA_ERROR(cudaCreateSurfaceObject(&surface, &resDesc)))

    // create a texture
    cudaTextureObject_t tex;
    cudaTextureDesc texDesc = {};
    texDesc.filterMode = cudaFilterModeLinear;
    texDesc.addressMode[0] = cudaAddressModeClamp;
    texDesc.addressMode[1] = cudaAddressModeClamp;
    texDesc.normalizedCoords = false;
    texDesc.readMode = cudaReadModeNormalizedFloat;

    cudaResourceViewDesc resViewDesc = {};
    resViewDesc.format = cudaResViewFormatUnsignedChar4;
    resViewDesc.width = width;
    resViewDesc.height = height;
    FAIL_RETURN(CUDA_ERROR(cudaCreateTextureObject(&tex, &resDesc, &texDesc, &resViewDesc)))

    {
      auto gsurface = std::make_unique<GPU::Surface>(new GPU::DeviceSurface(array, tex, surface, false), width, height);
      Potential<SourceOpenGLSurface::Pimpl> impl = SourceOpenGLSurface::Pimpl::create(image, std::move(gsurface));
      FAIL_RETURN(impl.status())

      SourceOpenGLSurface* surf = new SourceOpenGLSurface(impl.release());
      surf->texture = texture;

      return surf;
    }
  };

  PotentialValue<GLuint> potTexture = createSourceSurfaceTexture(width, height);
  FAIL_RETURN(potTexture.status());

  GLuint texture = potTexture.value();

  auto potSurf = allocPotSurf(texture, width, height);

  if (!potSurf.ok()) {
    glDeleteTextures(1, &texture);
    return Status{Origin::GPU, ErrType::RuntimeError, "Could not map OpenGL surface to CUDA.", potSurf.status()};
  }

  return potSurf;
}

Potential<PanoOpenGLSurface> OpenGLAllocator::createPanoSurface(size_t width, size_t height, BufferAllocType flag) {
  auto allocPotSurf = [](GLuint pixelbuffer, size_t width, size_t height,
                         BufferAllocType flag) -> Potential<PanoOpenGLSurface> {
    cudaGraphicsResource* pbo;
    unsigned int memFlag = getCudaGLMemAllocType(flag);

    FAIL_RETURN(CUDA_ERROR(cudaGraphicsGLRegisterBuffer(&pbo, pixelbuffer, memFlag)))
    FAIL_RETURN(CUDA_ERROR(cudaGraphicsMapResources(1, &pbo, cudaStreamPerThread)))

    void* devPtr;
    size_t size;
    FAIL_RETURN(CUDA_ERROR(cudaGraphicsResourceGetMappedPointer(&devPtr, &size, pbo)))

    FAIL_RETURN(CUDA_ERROR(cudaGraphicsUnmapResources(1, &pbo, cudaStreamPerThread)))

    GPU::Buffer<uint32_t> buffer = GPU::DeviceBuffer<uint32_t>::createBuffer((uint32_t*)devPtr, width * height);
    FAIL_RETURN(GPU::memsetToZeroBlocking(buffer, width * height * sizeof(uint32_t)))

    Potential<GPU::Surface> potremapSurf = makeSurface("Remap Buffer", width, height);
    FAIL_RETURN(potremapSurf.status())

    Potential<PanoOpenGLSurface::Pimpl> impl =
        PanoOpenGLSurface::Pimpl::create(pbo, buffer, potremapSurf.object(), width, height);
    FAIL_RETURN(impl.status())

    // ownership transferred to impl
    potremapSurf.release();

    PanoOpenGLSurface* surf = new PanoOpenGLSurface(impl.release());
    surf->pixelbuffer = pixelbuffer;
    surf->pimpl->externalAlloc = true;

    return surf;
  };

  auto pbtex = createPanoSurfacePB(width, height);
  FAIL_RETURN(pbtex.status());

  GLuint pixelbuffer = pbtex.value();

  auto potSurf = allocPotSurf(pixelbuffer, width, height, flag);

  if (!potSurf.ok()) {
    glDeleteBuffers(1, &pixelbuffer);
    return Status{Origin::GPU, ErrType::RuntimeError, "Could not map OpenGL surface to CUDA.", potSurf.status()};
  }

  return potSurf;
}

namespace {
Potential<GPU::CubemapSurface> makeCubemapSurface(std::string name, size_t width) {
  cudaArray_t array;
  cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(8, 8, 8, 8, cudaChannelFormatKindUnsigned);
  FAIL_RETURN(CUDA_ERROR(cudaMalloc3DArray(&array, &channelDesc, make_cudaExtent(width, width, 6),
                                           cudaArrayCubemap | cudaArraySurfaceLoadStore)));
  deviceStats.addPtr("Remap Buffer", array, width * width * 6);

  // Specify surface
  struct cudaResourceDesc resDesc = {};
  resDesc.resType = cudaResourceTypeArray;
  // Create the surface objects
  resDesc.res.array.array = array;
  cudaSurfaceObject_t surface;
  FAIL_RETURN(CUDA_ERROR(cudaCreateSurfaceObject(&surface, &resDesc)));
  // create a texture
  cudaTextureObject_t tex;

  cudaTextureDesc texDesc = {};
  texDesc.filterMode = cudaFilterModeLinear;
  texDesc.addressMode[0] = cudaAddressModeClamp;
  texDesc.addressMode[1] = cudaAddressModeClamp;
  texDesc.normalizedCoords = false;
  texDesc.readMode = cudaReadModeNormalizedFloat;

  cudaResourceViewDesc resViewDesc = {};
  resViewDesc.format = cudaResViewFormatUnsignedChar4;
  resViewDesc.width = width;
  resViewDesc.height = width;
  resViewDesc.depth = 6;
  cudaCreateTextureObject(&tex, &resDesc, &texDesc, &resViewDesc);

  return new GPU::CubemapSurface(new GPU::DeviceCubemapSurface(array, tex, surface, true), width);
}
}  // namespace

Potential<CubemapOpenGLSurface> OpenGLAllocator::createCubemapSurface(size_t width, bool equiangular,
                                                                      BufferAllocType flag) {
  std::array<GLuint, 6> pbo;

  glewInit();
  glEnable(GL_TEXTURE_CUBE_MAP);
#ifdef GL_VERSION_3_2
  glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
#endif
  {
    // clear error flag before mapping to CUDA
    GLenum glerr = glGetError();
    while (glerr != GL_NO_ERROR) glerr = glGetError();
  }

  auto allocPotSurf = [](const std::array<GLuint, 6>& pbo, size_t width, bool equiangular,
                         BufferAllocType flag) -> Potential<CubemapOpenGLSurface> {
    GLenum glerr = glGetError();
    if (glerr != GL_NO_ERROR) {
      return {Origin::GPU, ErrType::RuntimeError, "Could not allocate OpenGL buffer."};
    }

    cudaGraphicsResource* resources[6];
    GPU::Buffer<uint32_t> buffers[6];
    unsigned int memFlag = getCudaGLMemAllocType(flag);
    for (int i = 0; i < 6; ++i) {
      FAIL_RETURN(CUDA_ERROR(cudaGraphicsGLRegisterBuffer(&resources[i], pbo[i], memFlag)))
      FAIL_RETURN(CUDA_ERROR(cudaGraphicsMapResources(1, &resources[i], cudaStreamPerThread)))

      void* devPtr;
      size_t size;
      FAIL_RETURN(CUDA_ERROR(cudaGraphicsResourceGetMappedPointer(&devPtr, &size, resources[i])))

      buffers[i] = GPU::DeviceBuffer<uint32_t>::createBuffer((uint32_t*)devPtr, width * width);
      FAIL_RETURN(CUDA_ERROR(cudaGraphicsUnmapResources(1, &resources[i], cudaStreamPerThread)))
    }

    PotentialValue<GPU::Buffer<uint32_t>> buf = GPU::Buffer<uint32_t>::allocate(6 * width * width, "Offscreen Surface");
    FAIL_RETURN(buf.status())

    PotentialValue<GPU::Buffer<uint32_t>> potBuf = GPU::Buffer<uint32_t>::allocate(width * width, "Cubemap");
    FAIL_RETURN(potBuf.status())

    Potential<GPU::CubemapSurface> remapSurf = makeCubemapSurface("Remap Buffer", width);
    FAIL_RETURN(remapSurf.status())

    Potential<CubemapOpenGLSurface::Pimpl> impl = CubemapOpenGLSurface::Pimpl::create(
        resources, buffers, buf.value(), remapSurf.release(), potBuf.value(), width, equiangular);
    FAIL_RETURN(impl.status())
    impl->externalAlloc = true;

    return new CubemapOpenGLSurface(impl.release(), (int*)pbo.data());
  };

  for (int i = 0; i < 6; ++i) {
    glGenBuffers(1, &pbo[i]);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo[i]);
    glBufferData(GL_PIXEL_UNPACK_BUFFER, width * width * 4, NULL, GL_STREAM_DRAW);
    glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
  }

  auto potSurf = allocPotSurf(pbo, width, equiangular, flag);

  if (!potSurf.ok()) {
    glDeleteBuffers(6, pbo.data());
    return Status{Origin::GPU, ErrType::RuntimeError, "Could not allocate OpenGL Surface.", potSurf.status()};
  }

  return potSurf;
}

Potential<PanoSurface> OffscreenAllocator::createPanoSurface(size_t width, size_t height, const char* name) {
  Status status;
  GPU::Buffer<uint32_t> buf;
  GPU::Surface* remapSurf = nullptr;

  PotentialValue<GPU::Buffer<uint32_t>> potbuf = GPU::Buffer<uint32_t>::allocate(width * height, name);
  status = potbuf.status();
  if (!status.ok()) {
    return status;
  }
  buf = potbuf.value();
  GPU::memsetToZeroBlocking(buf, width * height * sizeof(uint32_t));

  {
    Potential<GPU::Surface> potremapSurf = makeSurface("Remap Buffer", width, height);
    status = potremapSurf.status();
    if (!status.ok()) {
      goto error;
    }
    remapSurf = potremapSurf.release();
    Potential<PanoPimpl> impl = PanoPimpl::create(buf, remapSurf, width, height);
    status = impl.status();
    if (!status.ok()) {
      goto error;
    }
    PanoSurface* surf = new PanoSurface(impl.release());

    surf->pimpl->externalAlloc = false;

    return Potential<PanoSurface>(surf);
  }

error:
  buf.release();
  delete remapSurf;
  return status;
}

Potential<CubemapSurface> OffscreenAllocator::createCubemapSurface(size_t width, const char* name, bool equiangular) {
  GPU::Stream stream;
  GPU::Buffer<uint32_t> faces[6];
  GPU::Buffer<uint32_t> buf, tmp;
  Status status;

  for (int i = 0; i < 6; ++i) {
    PotentialValue<GPU::Buffer<uint32_t>> buf = GPU::Buffer<uint32_t>::allocate(width * width, name);
    status = buf.status();
    if (!status.ok()) {
      goto error_1;
    }
    GPU::memsetToZeroBlocking(buf.value(), width * width * sizeof(uint32_t));
    faces[i] = buf.value();
  }

  {
    PotentialValue<GPU::Stream> potStream = GPU::Stream::create();
    status = potStream.status();
    if (!status.ok()) {
      goto error_1;
    }
    stream = potStream.value();

    PotentialValue<GPU::Buffer<uint32_t>> potBuf =
        GPU::Buffer<uint32_t>::allocate(6 * width * width, "Offscreen Surface");
    status = potBuf.status();
    if (!status.ok()) {
      goto error_2;
    }
    buf = potBuf.value();

    potBuf = GPU::Buffer<uint32_t>::allocate(width * width, "Cubemap");
    status = potBuf.status();
    if (!status.ok()) {
      goto error_3;
    }
    tmp = potBuf.value();

    Potential<GPU::CubemapSurface> remapSurf = makeCubemapSurface("Remap Buffer", width);
    status = remapSurf.status();
    if (!status.ok()) {
      goto error_4;
    }

    CubemapPimpl* impl = new CubemapPimpl(equiangular, stream, &faces[0], buf, remapSurf.release(), tmp, width);
    CubemapSurface* surf = new CubemapSurface(impl);

    surf->pimpl->externalAlloc = false;

    return Potential<CubemapSurface>(surf);
  }

error_4:
  tmp.release();
error_3:
  buf.release();
error_2:
  stream.destroy();
error_1:
  for (int i = 0; i < 6; ++i) {
    faces[i].release();
  }

  return status;
}

SourceOpenGLSurface::SourceOpenGLSurface(Pimpl* pimpl) : SourceSurface(pimpl), texture(0) {}

SourceOpenGLSurface::~SourceOpenGLSurface() { glDeleteTextures(1, (GLuint*)&texture); }

PanoOpenGLSurface::PanoOpenGLSurface(Pimpl* pimpl) : PanoSurface(pimpl), pixelbuffer(0) {}

PanoOpenGLSurface::~PanoOpenGLSurface() { glDeleteBuffers(1, (GLuint*)&pixelbuffer); }

CubemapOpenGLSurface::CubemapOpenGLSurface(Pimpl* pimpl, int* f) : CubemapSurface(pimpl) {
  for (int i = 0; i < 6; ++i) {
    faces[i] = f[i];
  }
}

CubemapOpenGLSurface::~CubemapOpenGLSurface() {
  for (int i = 0; i < 6; ++i) {
    glDeleteBuffers(1, (GLuint*)&faces[i]);
  }
}

}  // namespace Core

}  // namespace VideoStitch