|
@@ -9,7 +9,7 @@
|
|
|
using boost::iostreams::mapped_file;
|
|
using boost::iostreams::mapped_file;
|
|
|
|
|
|
|
|
std::filesystem::path shader_folder
|
|
std::filesystem::path shader_folder
|
|
|
- = "/home/tpx/project/DepthGuide/src/render/impl/shader"; // TODO: config shader path
|
|
|
|
|
|
|
+ = "/home/tpx/ext/project/DepthGuide/src/render/impl/shader"; // TODO: config shader path
|
|
|
|
|
|
|
|
cv::Size query_viewport_size() {
|
|
cv::Size query_viewport_size() {
|
|
|
struct {
|
|
struct {
|
|
@@ -164,6 +164,26 @@ void smart_texture::create(GLenum _format, cv::Size _size) {
|
|
|
set_filter(GL_LINEAR, GL_LINEAR);
|
|
set_filter(GL_LINEAR, GL_LINEAR);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+void smart_texture::upload(const sp_image &img) {
|
|
|
|
|
+ create(get_tex_internal_format(img.cv_type()), img.cv_size());
|
|
|
|
|
+ if (img.channels() == 3) {
|
|
|
|
|
+ img_pbo.upload(img);
|
|
|
|
|
+ upload(img_pbo.id, size, img.cv_type());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const auto status = img.mem->status();
|
|
|
|
|
+ if (status.cuda_available) {
|
|
|
|
|
+ const auto read_helper = read_access_helper(img.cuda());
|
|
|
|
|
+ const auto mem_v1 = to_mem_v1(img, read_helper.ptr(), MEM_CUDA);
|
|
|
|
|
+ upload_impl(mem_v1, nullptr);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ assert(status.host_available);
|
|
|
|
|
+ const auto read_helper = read_access_helper(img.host());
|
|
|
|
|
+ const auto mem_v1 = to_mem_v1(img, read_helper.ptr(), MEM_HOST);
|
|
|
|
|
+ upload_impl(mem_v1, nullptr);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void smart_texture::upload_impl(const image_mem_info &img, smart_cuda_stream *stream) {
|
|
void smart_texture::upload_impl(const image_mem_info &img, smart_cuda_stream *stream) {
|
|
|
if (cuda_res == nullptr) {
|
|
if (cuda_res == nullptr) {
|
|
|
CUDA_API_CHECK(cudaGraphicsGLRegisterImage(
|
|
CUDA_API_CHECK(cudaGraphicsGLRegisterImage(
|
|
@@ -172,14 +192,16 @@ void smart_texture::upload_impl(const image_mem_info &img, smart_cuda_stream *st
|
|
|
assert(cuda_res != nullptr);
|
|
assert(cuda_res != nullptr);
|
|
|
|
|
|
|
|
cudaArray_t tex_arr;
|
|
cudaArray_t tex_arr;
|
|
|
- CUDA_API_CHECK(cudaGraphicsMapResources(1, &cuda_res, stream->cuda));
|
|
|
|
|
|
|
+ CUDA_API_CHECK(cudaGraphicsMapResources(1, &cuda_res, GET_CUDA_STREAM(stream)));
|
|
|
CUDA_API_CHECK(cudaGraphicsSubResourceGetMappedArray(&tex_arr, cuda_res, 0, 0));
|
|
CUDA_API_CHECK(cudaGraphicsSubResourceGetMappedArray(&tex_arr, cuda_res, 0, 0));
|
|
|
CUDA_API_CHECK(cudaMemcpy2DToArrayAsync(
|
|
CUDA_API_CHECK(cudaMemcpy2DToArrayAsync(
|
|
|
tex_arr, 0, 0, img.ptr.get(), img.pitch, img.width, img.height,
|
|
tex_arr, 0, 0, img.ptr.get(), img.pitch, img.width, img.height,
|
|
|
img.loc == MEM_CUDA ? cudaMemcpyDeviceToDevice : cudaMemcpyHostToDevice,
|
|
img.loc == MEM_CUDA ? cudaMemcpyDeviceToDevice : cudaMemcpyHostToDevice,
|
|
|
- stream->cuda));
|
|
|
|
|
- CUDA_API_CHECK(cudaGraphicsUnmapResources(1, &cuda_res, stream->cuda));
|
|
|
|
|
- extend_pointer_life(img.ptr, stream);
|
|
|
|
|
|
|
+ GET_CUDA_STREAM(stream)));
|
|
|
|
|
+ CUDA_API_CHECK(cudaGraphicsUnmapResources(1, &cuda_res, GET_CUDA_STREAM(stream)));
|
|
|
|
|
+ if (stream != nullptr) {
|
|
|
|
|
+ extend_pointer_life(img.ptr, stream);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void smart_texture::upload_impl(GLuint pbo_id, GLenum _format, GLenum type) {
|
|
void smart_texture::upload_impl(GLuint pbo_id, GLenum _format, GLenum type) {
|
|
@@ -281,7 +303,7 @@ void *smart_pixel_buffer::up_mapped_ptr(smart_cuda_stream *stream, size_t *_ptr_
|
|
|
assert(cuda_res_up != nullptr);
|
|
assert(cuda_res_up != nullptr);
|
|
|
void *ptr = nullptr;
|
|
void *ptr = nullptr;
|
|
|
size_t ptr_size = 0;
|
|
size_t ptr_size = 0;
|
|
|
- CUDA_API_CHECK(cudaGraphicsMapResources(1, &cuda_res_up, stream->cuda));
|
|
|
|
|
|
|
+ CUDA_API_CHECK(cudaGraphicsMapResources(1, &cuda_res_up, GET_CUDA_STREAM(stream)));
|
|
|
CUDA_API_CHECK(cudaGraphicsResourceGetMappedPointer(&ptr, &ptr_size, cuda_res_up));
|
|
CUDA_API_CHECK(cudaGraphicsResourceGetMappedPointer(&ptr, &ptr_size, cuda_res_up));
|
|
|
if (_ptr_size != nullptr) {
|
|
if (_ptr_size != nullptr) {
|
|
|
*_ptr_size = ptr_size;
|
|
*_ptr_size = ptr_size;
|
|
@@ -290,7 +312,28 @@ void *smart_pixel_buffer::up_mapped_ptr(smart_cuda_stream *stream, size_t *_ptr_
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void smart_pixel_buffer::up_unmap(smart_cuda_stream *stream) {
|
|
void smart_pixel_buffer::up_unmap(smart_cuda_stream *stream) {
|
|
|
- CUDA_API_CHECK(cudaGraphicsUnmapResources(1, &cuda_res_up, stream->cuda));
|
|
|
|
|
|
|
+ CUDA_API_CHECK(cudaGraphicsUnmapResources(1, &cuda_res_up, GET_CUDA_STREAM(stream)));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void smart_pixel_buffer::upload(const sp_image &img) {
|
|
|
|
|
+ create(img.byte_size());
|
|
|
|
|
+ size_t ptr_size = 0;
|
|
|
|
|
+ const auto ptr = up_mapped_ptr(nullptr, &ptr_size);
|
|
|
|
|
+ assert(ptr_size >= img.byte_size());
|
|
|
|
|
+ const auto status = img.mem->status();
|
|
|
|
|
+ if (status.cuda_available) {
|
|
|
|
|
+ const auto read_helper = read_access_helper(img.cuda());
|
|
|
|
|
+ CUDA_API_CHECK(cudaMemcpy2DAsync(
|
|
|
|
|
+ ptr, img.byte_width(), img.start_ptr(read_helper.ptr()), img.pitch(),
|
|
|
|
|
+ img.byte_width(), img.height(), cudaMemcpyDeviceToDevice, current_cuda_stream()));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ assert(status.host_available);
|
|
|
|
|
+ const auto read_helper = read_access_helper(img.host());
|
|
|
|
|
+ CUDA_API_CHECK(cudaMemcpy2DAsync(
|
|
|
|
|
+ ptr, img.byte_width(), img.start_ptr(read_helper.ptr()), img.pitch(),
|
|
|
|
|
+ img.byte_width(), img.height(), cudaMemcpyHostToDevice, current_cuda_stream()));
|
|
|
|
|
+ }
|
|
|
|
|
+ up_unmap(nullptr);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void smart_pixel_buffer::upload_impl(const image_mem_info &img, smart_cuda_stream *stream) {
|
|
void smart_pixel_buffer::upload_impl(const image_mem_info &img, smart_cuda_stream *stream) {
|
|
@@ -341,6 +384,15 @@ image_ptr smart_pixel_buffer::download_viewport(GLenum format, GLenum type,
|
|
|
return img;
|
|
return img;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+sp_image smart_pixel_buffer::download_viewport_sp(GLenum format, GLenum type) {
|
|
|
|
|
+ const auto img_type = CV_MAKETYPE(get_type_depth(type), get_format_channels(format));
|
|
|
|
|
+ auto img = sp_image::create(img_type, query_viewport_size());
|
|
|
|
|
+ download_viewport(format, type);
|
|
|
|
|
+ const auto write_helper = write_access_helper(img.cuda());
|
|
|
|
|
+ download_impl(to_mem_v1(img, write_helper.ptr(), MEM_CUDA), nullptr);
|
|
|
|
|
+ return img;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void smart_pixel_buffer::download_impl(const image_mem_info &img, smart_cuda_stream *stream) {
|
|
void smart_pixel_buffer::download_impl(const image_mem_info &img, smart_cuda_stream *stream) {
|
|
|
if (cuda_res_down == nullptr) {
|
|
if (cuda_res_down == nullptr) {
|
|
|
CUDA_API_CHECK(cudaGraphicsGLRegisterBuffer(
|
|
CUDA_API_CHECK(cudaGraphicsGLRegisterBuffer(
|
|
@@ -350,14 +402,14 @@ void smart_pixel_buffer::download_impl(const image_mem_info &img, smart_cuda_str
|
|
|
|
|
|
|
|
void *ptr = nullptr;
|
|
void *ptr = nullptr;
|
|
|
size_t ptr_size = 0;
|
|
size_t ptr_size = 0;
|
|
|
- CUDA_API_CHECK(cudaGraphicsMapResources(1, &cuda_res_down, stream->cuda));
|
|
|
|
|
|
|
+ CUDA_API_CHECK(cudaGraphicsMapResources(1, &cuda_res_down, GET_CUDA_STREAM(stream)));
|
|
|
CUDA_API_CHECK(cudaGraphicsResourceGetMappedPointer(&ptr, &ptr_size, cuda_res_down));
|
|
CUDA_API_CHECK(cudaGraphicsResourceGetMappedPointer(&ptr, &ptr_size, cuda_res_down));
|
|
|
assert(ptr_size >= img.width * img.height);
|
|
assert(ptr_size >= img.width * img.height);
|
|
|
CUDA_API_CHECK(cudaMemcpy2DAsync(
|
|
CUDA_API_CHECK(cudaMemcpy2DAsync(
|
|
|
img.ptr.get(), img.pitch, ptr, img.width, img.width, img.height,
|
|
img.ptr.get(), img.pitch, ptr, img.width, img.width, img.height,
|
|
|
img.loc == MEM_CUDA ? cudaMemcpyDeviceToDevice : cudaMemcpyDeviceToHost,
|
|
img.loc == MEM_CUDA ? cudaMemcpyDeviceToDevice : cudaMemcpyDeviceToHost,
|
|
|
- stream->cuda));
|
|
|
|
|
- CUDA_API_CHECK(cudaGraphicsUnmapResources(1, &cuda_res_down, stream->cuda));
|
|
|
|
|
|
|
+ GET_CUDA_STREAM(stream)));
|
|
|
|
|
+ CUDA_API_CHECK(cudaGraphicsUnmapResources(1, &cuda_res_down, GET_CUDA_STREAM(stream)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
smart_frame_buffer::~smart_frame_buffer() {
|
|
smart_frame_buffer::~smart_frame_buffer() {
|