Commit 1bc644a1 authored by Nianchen Deng's avatar Nianchen Deng
Browse files

sync

parent 6294701e
#pragma once
#include <map>
#include <vector>
#include <cuda_gl_interop.h>
#include "Logger.h"
class Resource {
class Resource
{
public:
virtual ~Resource() {}
virtual void *getBuffer() const = 0;
virtual void *data() const = 0;
virtual size_t size() const = 0;
};
class CudaBuffer : public Resource {
class CudaBuffer : public Resource
{
public:
CudaBuffer(void *buffer = nullptr, size_t size = 0)
: _buffer(buffer), _ownBuffer(false), _size(size) {}
CudaBuffer(size_t size) : _buffer(nullptr), _ownBuffer(true), _size(size) {
CudaBuffer(size_t size) : _buffer(nullptr), _ownBuffer(true), _size(size)
{
CHECK_EX(cudaMalloc(&_buffer, size));
}
CudaBuffer(const CudaBuffer &rhs) = delete;
virtual ~CudaBuffer() {
virtual ~CudaBuffer()
{
if (!_ownBuffer || _buffer == nullptr)
return;
try {
try
{
CHECK_EX(cudaFree(_buffer));
} catch (std::exception &ex) {
}
catch (std::exception &ex)
{
Logger::instance.warning(std::string("Exception raised in destructor: ") + ex.what());
}
_buffer = nullptr;
_ownBuffer = false;
}
virtual void *getBuffer() const { return _buffer; }
template <class T> T *getBuffer() const { return (T *)getBuffer(); }
virtual void *data() const { return _buffer; }
virtual size_t size() const { return _size; }
template <class T1>
T1 *data() const { return (T1 *)data(); }
template <class T1>
operator T1 *() const { return (T1 *)data(); }
private:
void *_buffer;
bool _ownBuffer;
size_t _size;
};
template <typename T> class CudaArray : public CudaBuffer {
template <typename T>
class CudaArray : public CudaBuffer
{
public:
CudaArray(size_t n) : CudaBuffer(n * sizeof(T)) {}
CudaArray(T *buffer, size_t n) : CudaBuffer(buffer, n * sizeof(T)) {}
CudaArray(const std::vector<T> &hostArray) : CudaBuffer(hostArray.size() * sizeof(T)) {
cudaMemcpy(getBuffer(), hostArray.data(), size(), cudaMemcpyHostToDevice);
CudaArray(const std::vector<T> &hostArray) : CudaBuffer(hostArray.size() * sizeof(T))
{
cudaMemcpy(data(), hostArray.data(), size(), cudaMemcpyHostToDevice);
}
CudaArray(const CudaArray<T> &rhs) = delete;
size_t n() const { return size() / sizeof(T); }
operator T *() { return (T *)getBuffer(); }
CudaArray<T> *subArray(size_t offset, size_t n = -1) {
operator T *() { return (T *)data(); }
CudaArray<T> *subArray(size_t offset, size_t n = -1)
{
if (n == -1)
n = this->n() - offset;
return new CudaArray<T>(*this + offset, n);
}
};
class GraphicsResource : public Resource {
class GraphicsResource : public Resource
{
public:
cudaGraphicsResource_t getHandler() { return _res; }
virtual ~GraphicsResource() {
virtual ~GraphicsResource()
{
if (_res == nullptr)
return;
try {
try
{
CHECK_EX(cudaGraphicsUnregisterResource(_res));
} catch (std::exception &ex) {
}
catch (std::exception &ex)
{
Logger::instance.warning(std::string("Exception raised in destructor: ") + ex.what());
}
_res = nullptr;
......@@ -86,9 +109,12 @@ protected:
GraphicsResource() : _res(nullptr), _size(0) {}
};
template <typename T> class GlTextureResource : public GraphicsResource {
template <typename T>
class GlTextureResource : public GraphicsResource
{
public:
GlTextureResource(GLuint textureID, glm::uvec2 textureSize) {
GlTextureResource(GLuint textureID, glm::uvec2 textureSize)
{
CHECK_EX(cudaGraphicsGLRegisterImage(&_res, textureID, GL_TEXTURE_2D,
cudaGraphicsRegisterFlagsWriteDiscard));
_size = textureSize.x * textureSize.y * sizeof(T);
......@@ -97,17 +123,21 @@ public:
virtual ~GlTextureResource() { cudaGraphicsUnmapResources(1, &_res, 0); }
virtual void *getBuffer() const {
virtual void *data() const
{
cudaArray_t buffer;
try {
try
{
CHECK_EX(cudaGraphicsSubResourceGetMappedArray(&buffer, _res, 0, 0));
} catch (...) {
}
catch (...)
{
return nullptr;
}
return buffer;
}
operator T *() { return (T *)getBuffer(); }
operator T *() { return (T *)data(); }
glm::uvec2 textureSize() { return _textureSize; }
......@@ -115,19 +145,22 @@ private:
glm::uvec2 _textureSize;
};
class Resources {
class Resources
{
public:
std::map<std::string, Resource *> resources;
std::vector<cudaGraphicsResource_t> graphicsResources;
void addResource(const std::string &name, Resource *res) {
void addResource(const std::string &name, Resource *res)
{
auto gres = dynamic_cast<GraphicsResource *>(res);
if (gres != nullptr)
graphicsResources.push_back(gres->getHandler());
resources[name] = res;
}
void clear() {
void clear()
{
resources.clear();
graphicsResources.clear();
}
......@@ -135,10 +168,11 @@ public:
template <typename T, typename T2 = T>
void dumpArray(std::ostream &so, CudaArray<T> &arr, size_t maxDumpRows = 0,
size_t elemsPerRow = 1) {
size_t elemsPerRow = 1)
{
int chns = sizeof(T) / sizeof(T2);
T2 *hostArr = new T2[arr.n() * chns];
cudaMemcpy(hostArr, arr.getBuffer(), arr.n() * sizeof(T), cudaMemcpyDeviceToHost);
cudaMemcpy(hostArr, arr.data(), arr.n() * sizeof(T), cudaMemcpyDeviceToHost);
dumpHostBuffer<T2>(so, hostArr, arr.n() * sizeof(T), chns * elemsPerRow, maxDumpRows);
delete[] hostArr;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment