Weird behavior from CUDA (Libtorch) and OpenGL interop

I am trying to write functions to transform an OpenGL texture to a PyTorch tensor and back in a C++ app. To test that it works I added 128 to the tensor to basically brighten the image, and then rendered the resulting texture on a quad. It mostly works but I’m experiencing a weird behavior in which part of the texture is unaffected.

is the original texture, and

is the texture after adding 128 to every element in the tensor. Note that like 1/4 of the image is not affected by this operation

These are the relevant sections of code. textureColorbuffer uses the format GL_RGB (which if I understood correctly is bitdepth 8 for each channel). This is where I call the functions and add to the tensor:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cudaGraphicsGLRegisterImage(&cudaResource, textureColorbuffer, GL_TEXTURE_2D, cudaGraphicsMapFlagsNone);
torch::Tensor tensor = resourceToTensor(cudaResource, WIDTH, HEIGHT);
tensor = tensor + static_cast<unsigned char>(128);
tensorToResource(tensor, cudaResource, WIDTH, HEIGHT);
</code>
<code>cudaGraphicsGLRegisterImage(&cudaResource, textureColorbuffer, GL_TEXTURE_2D, cudaGraphicsMapFlagsNone); torch::Tensor tensor = resourceToTensor(cudaResource, WIDTH, HEIGHT); tensor = tensor + static_cast<unsigned char>(128); tensorToResource(tensor, cudaResource, WIDTH, HEIGHT); </code>
cudaGraphicsGLRegisterImage(&cudaResource, textureColorbuffer, GL_TEXTURE_2D, cudaGraphicsMapFlagsNone);
torch::Tensor tensor = resourceToTensor(cudaResource, WIDTH, HEIGHT);
tensor = tensor + static_cast<unsigned char>(128);
tensorToResource(tensor, cudaResource, WIDTH, HEIGHT);

And these are the used functions:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>torch::Tensor resourceToTensor(cudaGraphicsResource* cudaResource, int width, int height) {
CUDA_CHECK_ERROR(cudaGraphicsMapResources(1, &cudaResource, 0));
cudaArray* textureArray;
CUDA_CHECK_ERROR(cudaGraphicsSubResourceGetMappedArray(&textureArray, cudaResource, 0, 0));
unsigned char* devicePtr;
size_t size = width * height * 3 * sizeof(unsigned char);
CUDA_CHECK_ERROR(cudaMalloc(&devicePtr, size));
CUDA_CHECK_ERROR(cudaMemcpyFromArray(devicePtr, textureArray, 0, 0, size, cudaMemcpyDeviceToDevice));
auto options = torch::TensorOptions().dtype(torch::kUInt8).device(torch::kCUDA);
torch::Tensor tensor = torch::from_blob(devicePtr, { height, width, 3 }, options);
CUDA_CHECK_ERROR(cudaGraphicsUnmapResources(1, &cudaResource, 0));
torch::Tensor clonedTensor = tensor.clone();
CUDA_CHECK_ERROR(cudaFree(devicePtr));
return clonedTensor;
}
void tensorToResource(torch::Tensor tensor, cudaGraphicsResource* cudaResource, int width, int height) {
tensor = tensor.to(torch::kCUDA);
CUDA_CHECK_ERROR(cudaGraphicsMapResources(1, &cudaResource, 0));
cudaArray* textureArray;
CUDA_CHECK_ERROR(cudaGraphicsSubResourceGetMappedArray(&textureArray, cudaResource, 0, 0));
const unsigned char* devicePtr = tensor.data_ptr<unsigned char>();
size_t size = width * height * 3 * sizeof(unsigned char);
CUDA_CHECK_ERROR(cudaMemcpyToArray(textureArray, 0, 0, devicePtr, size, cudaMemcpyDeviceToDevice));
CUDA_CHECK_ERROR(cudaGraphicsUnmapResources(1, &cudaResource, 0));
}
</code>
<code>torch::Tensor resourceToTensor(cudaGraphicsResource* cudaResource, int width, int height) { CUDA_CHECK_ERROR(cudaGraphicsMapResources(1, &cudaResource, 0)); cudaArray* textureArray; CUDA_CHECK_ERROR(cudaGraphicsSubResourceGetMappedArray(&textureArray, cudaResource, 0, 0)); unsigned char* devicePtr; size_t size = width * height * 3 * sizeof(unsigned char); CUDA_CHECK_ERROR(cudaMalloc(&devicePtr, size)); CUDA_CHECK_ERROR(cudaMemcpyFromArray(devicePtr, textureArray, 0, 0, size, cudaMemcpyDeviceToDevice)); auto options = torch::TensorOptions().dtype(torch::kUInt8).device(torch::kCUDA); torch::Tensor tensor = torch::from_blob(devicePtr, { height, width, 3 }, options); CUDA_CHECK_ERROR(cudaGraphicsUnmapResources(1, &cudaResource, 0)); torch::Tensor clonedTensor = tensor.clone(); CUDA_CHECK_ERROR(cudaFree(devicePtr)); return clonedTensor; } void tensorToResource(torch::Tensor tensor, cudaGraphicsResource* cudaResource, int width, int height) { tensor = tensor.to(torch::kCUDA); CUDA_CHECK_ERROR(cudaGraphicsMapResources(1, &cudaResource, 0)); cudaArray* textureArray; CUDA_CHECK_ERROR(cudaGraphicsSubResourceGetMappedArray(&textureArray, cudaResource, 0, 0)); const unsigned char* devicePtr = tensor.data_ptr<unsigned char>(); size_t size = width * height * 3 * sizeof(unsigned char); CUDA_CHECK_ERROR(cudaMemcpyToArray(textureArray, 0, 0, devicePtr, size, cudaMemcpyDeviceToDevice)); CUDA_CHECK_ERROR(cudaGraphicsUnmapResources(1, &cudaResource, 0)); } </code>
torch::Tensor resourceToTensor(cudaGraphicsResource* cudaResource, int width, int height) {
    CUDA_CHECK_ERROR(cudaGraphicsMapResources(1, &cudaResource, 0));
    cudaArray* textureArray;
    CUDA_CHECK_ERROR(cudaGraphicsSubResourceGetMappedArray(&textureArray, cudaResource, 0, 0));

    unsigned char* devicePtr;
    size_t size = width * height * 3 * sizeof(unsigned char);
    CUDA_CHECK_ERROR(cudaMalloc(&devicePtr, size));

    CUDA_CHECK_ERROR(cudaMemcpyFromArray(devicePtr, textureArray, 0, 0, size, cudaMemcpyDeviceToDevice));
    auto options = torch::TensorOptions().dtype(torch::kUInt8).device(torch::kCUDA);
    torch::Tensor tensor = torch::from_blob(devicePtr, { height, width, 3 }, options);

    CUDA_CHECK_ERROR(cudaGraphicsUnmapResources(1, &cudaResource, 0));

    torch::Tensor clonedTensor = tensor.clone();

    CUDA_CHECK_ERROR(cudaFree(devicePtr));
    return clonedTensor;
}

void tensorToResource(torch::Tensor tensor, cudaGraphicsResource* cudaResource, int width, int height) {
    tensor = tensor.to(torch::kCUDA);

    CUDA_CHECK_ERROR(cudaGraphicsMapResources(1, &cudaResource, 0));
    cudaArray* textureArray;
    CUDA_CHECK_ERROR(cudaGraphicsSubResourceGetMappedArray(&textureArray, cudaResource, 0, 0));

    const unsigned char* devicePtr = tensor.data_ptr<unsigned char>();
    size_t size = width * height * 3 * sizeof(unsigned char);
    CUDA_CHECK_ERROR(cudaMemcpyToArray(textureArray, 0, 0, devicePtr, size, cudaMemcpyDeviceToDevice));

    CUDA_CHECK_ERROR(cudaGraphicsUnmapResources(1, &cudaResource, 0));
}

Does anybody know what could be the cause of this? Did I make a mistake with the sizes of the buffers and arrays?

New contributor

asmo_192 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật