memcpy.hpp 7.05 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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#pragma once

#include "2dBuffer.hpp"
#include "buffer.hpp"
#include "surface.hpp"
#include "hostBuffer.hpp"
#include "stream.hpp"

namespace VideoStitch {
namespace GPU {

// Device --> Device
// * async
template <typename T>
Status memcpyAsync(Buffer<T> dst, Buffer<const T> src, size_t copySize, const Stream& stream);

template <typename T>
inline Status memcpyAsync(Buffer<T> dst, Buffer<T> src, size_t copySize, const Stream& stream) {
  return memcpyAsync(dst, src.as_const(), copySize, stream);
}

// * blocking
template <typename T>
Status memcpyBlocking(Buffer<T> dst, Buffer<const T> src, size_t copySize);

template <typename T>
inline Status memcpyBlocking(Buffer<T> dst, Buffer<T> src, size_t copySize) {
  return memcpyBlocking(dst, src.as_const(), copySize);
}

template <typename T>
inline Status memcpyBlocking(Buffer<T> dst, Buffer<T> src) {
  return memcpyBlocking(dst, src.as_const());
}

template <typename T>
inline Status memcpyBlocking(Buffer<T> dst, Buffer<const T> src) {
  if (dst.byteSize() < src.byteSize()) {
    return {Origin::GPU, ErrType::ImplementationError, "Copy destination is too small"};
  }
  return memcpyBlocking(dst, src, src.byteSize());
}

// Host --> Device
// * async
template <typename T>
Status memcpyAsync(Buffer<T> dst, const T* src, size_t copySize, const Stream& stream);

template <typename T>
inline Status memcpyAsync(Buffer<T> dst, const T* src, const Stream& stream) {
  return memcpyAsync(dst, src, dst.byteSize(), stream);
}

template <typename T>
inline Status memcpyAsync(Buffer<T> dst, HostBuffer<const T> src, const Stream& stream) {
  if (dst.byteSize() < src.byteSize()) {
    return {Origin::GPU, ErrType::ImplementationError, "Copy destination is too small"};
  }
  return memcpyAsync(dst, src.hostPtr(), src.byteSize(), stream);
}

template <typename T>
inline Status memcpyAsync(Buffer<T> dst, HostBuffer<const T> src, size_t copySize, const Stream& stream) {
  assert(copySize <= src.byteSize());
  return memcpyAsync(dst, src.hostPtr(), copySize, stream);
}

template <typename T>
inline Status memcpyAsync(Buffer<T> dst, HostBuffer<T> src, size_t copySize, const Stream& stream) {
  return memcpyAsync(dst, src.as_const(), copySize, stream);
}

// * blocking
template <typename T>
Status memcpyBlocking(Buffer<T> dst, const T* src, size_t copySize);

template <typename T>
inline Status memcpyBlocking(Buffer<T> dst, const T* src) {
  return memcpyBlocking(dst, src, dst.byteSize());
}

template <typename T>
inline Status memcpyBlocking(typename Buffer<T>::PotentialBuffer dst, const T* src) {
  if (!dst.status()) {
    return dst.status();
  }
  return memcpyBlocking(dst.value(), src, dst.byteSize());
}

// Copy memory Device --> Host
// * async
template <typename T>
Status memcpyAsync(T* dst, Buffer<const T> src, size_t copySize, const Stream& stream);

template <typename T>
inline Status memcpyAsync(T* dst, Buffer<const T> src, const Stream& stream) {
  return memcpyAsync(dst, src, src.byteSize(), stream);
}

template <typename T>
inline Status memcpyAsync(HostBuffer<T> dst, Buffer<const T> src, size_t copySize, const Stream& stream) {
  assert(copySize <= dst.byteSize());
  return memcpyAsync(dst.hostPtr(), src, copySize, stream);
}

template <typename T>
inline Status memcpyAsync(HostBuffer<T> dst, Buffer<const T> src, const Stream& stream) {
  if (dst.byteSize() < src.byteSize()) {
    return {Origin::GPU, ErrType::ImplementationError, "Copy destination is too small"};
  }
  return memcpyAsync(dst.hostPtr(), src, src.byteSize(), stream);
}

Status memcpyAsync(unsigned char* dst, Buffer2D src, const Stream& stream);
inline Status memcpyAsync(HostBuffer<unsigned char> dst, Buffer2D src, const Stream& stream) {
  if (dst.byteSize() < src.getWidth() * src.getHeight()) {
    return {Origin::GPU, ErrType::ImplementationError, "Copy destination is too small"};
  }
  return memcpyAsync(dst.hostPtr(), src, stream);
}

// * blocking
template <typename T>
Status memcpyBlocking(T* dst, Buffer<const T> src, size_t copySize);

template <typename T>
inline Status memcpyBlocking(T* dst, Buffer<const T> src) {
  return memcpyBlocking(dst, src, src.byteSize());
}

template <typename T>
inline Status memcpyBlocking(T* dst, Buffer<T> src) {
  return memcpyBlocking(dst, src.as_const());
}

template <typename T>
inline Status memcpyBlocking(HostBuffer<T> dst, Buffer<const T> src) {
  if (dst.byteSize() < src.byteSize()) {
    return {Origin::GPU, ErrType::ImplementationError, "Copy destination is too small"};
  }
  return memcpyBlocking(dst.hostPtr(), src);
}

Status memcpyBlocking(unsigned char* dst, Buffer2D src);
inline Status memcpyBlocking(HostBuffer<unsigned char> dst, Buffer2D src) {
  if (dst.byteSize() < src.getWidth() * src.getHeight()) {
    return {Origin::GPU, ErrType::ImplementationError, "Copy destination is too small"};
  }
  return memcpyBlocking(dst.hostPtr(), src);
}

Status memcpyBlocking(Buffer2D dst, const unsigned char* src);
inline Status memcpyBlocking(Buffer2D dst, HostBuffer<unsigned char> src) {
  if (src.byteSize() < dst.getWidth() * dst.getHeight()) {
    return {Origin::GPU, ErrType::ImplementationError, "Copy destination is too small"};
  }
  return memcpyBlocking(dst, src.hostPtr());
}

// Memset
// Note: memset to value implemented in render.hpp

// Set settingSize Bytes of GPU memory to 0
template <typename T>
Status memsetToZeroAsync(Buffer<T> devPtr, size_t settingSize, const Stream& stream);

template <typename T>
inline Status memsetToZeroAsync(Buffer<T> devPtr, const Stream& stream) {
  return memsetToZeroAsync(devPtr, devPtr.byteSize(), stream);
}

template <typename T>
Status memsetToZeroBlocking(Buffer<T> devPtr, size_t settingSize);

Status memsetToZeroAsync(Surface& dst, const Stream& stream);

// Device buffer to texture memory
template <typename T>
Status memcpyAsync(Surface& dst, Buffer<const T> src, const Stream& stream);
Status memcpyBlocking(Surface& dst, Buffer<const uint32_t> src);

// Texture memory to device buffer
template <typename T>
Status memcpyAsync(Buffer<T> dst, Surface& src, const Stream& stream);
template <typename T>
Status memcpyBlocking(Buffer<T> dst, Surface& src);

// Texture memory to host buffer
template <typename T>
Status memcpyAsync(T* dst, Surface& src, const Stream& stream);

template <typename T>
Status memcpyBlocking(T* dst, Surface& src);

// Host to texture memory
Status memcpyAsync(Surface& dst, uint32_t* src, const Stream& stream);
Status memcpyBlocking(Surface& dst, uint32_t* src);

Status memcpy2DAsync(Buffer<uint32_t> dst, Buffer<uint32_t> src, size_t src_origin_width, size_t src_origin_height,
                     size_t dst_origin_width, size_t dst_origin_height, size_t region_width, size_t region_height,
                     size_t src_pitch, size_t dst_pitch, const Stream& stream);

Status memcpyCubemapAsync(CubemapSurface& dst, Buffer<uint32_t> srcXP, Buffer<uint32_t> srcXN, Buffer<uint32_t> srcYP,
                          Buffer<uint32_t> srcYN, Buffer<uint32_t> srcZP, Buffer<uint32_t> srcZN, size_t faceDim,
                          const Stream& stream);

}  // namespace GPU
}  // namespace VideoStitch