Skip to content

cudev: fix CUDA texture pitch alignment for createContinuous GpuMat#4068

Open
Shubh3155 wants to merge 1 commit intoopencv:4.xfrom
Shubh3155:fix-cuda-texture-pitch-alignment
Open

cudev: fix CUDA texture pitch alignment for createContinuous GpuMat#4068
Shubh3155 wants to merge 1 commit intoopencv:4.xfrom
Shubh3155:fix-cuda-texture-pitch-alignment

Conversation

@Shubh3155
Copy link

Summary

This PR fixes a CUDA texture creation failure when using cv::cuda::createContinuous().

createContinuous() may produce a GpuMat with a pitch that is not aligned to
cudaDeviceProp::texturePitchAlignment. When such a matrix is used in CUDA
texture-backed operations (e.g. cv::cuda::resize), createTextureObject()
fails with an invalid argument error.

The fix extends the existing fallback logic to also handle misaligned pitch
values by copying the data into an aligned pitched buffer when required.

Root Cause

createContinuous() allocates memory via cudaMalloc and reshapes it, which
can result in an unaligned step. CUDA Pitch2D textures require the pitch to be
aligned to texturePitchAlignment, but the previous logic only handled
single-row or single-column cases.

Changes

  • Extend the texture fallback condition to also check pitch alignment
  • Preserve the fast path for already-aligned pitched allocations
  • No behavior change for correctly aligned GpuMat instances

Testing

  • Verified locally with a minimal reproducer using cv::cuda::resize
  • Existing behavior remains unchanged for pitched GpuMat allocations

Copilot AI review requested due to automatic review settings January 23, 2026 12:48
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a CUDA texture creation failure when using cv::cuda::createContinuous() with texture-backed operations. The issue occurs because createContinuous() can produce a GpuMat with a pitch that is not aligned to cudaDeviceProp::texturePitchAlignment, causing texture creation to fail.

Changes:

  • Extended the fallback logic in texture creation to check pitch alignment in addition to single row/column cases
  • When pitch is misaligned, data is copied into an aligned pitched buffer allocated via cudaMallocPitch
  • Reformatted multi-line function calls for better readability

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +182 to +184
cudaDeviceProp prop;
CV_CUDEV_SAFE_CALL(cudaGetDeviceProperties(&prop, currentDevice));

Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance consideration: cudaGetDevice and cudaGetDeviceProperties are called every time this function is invoked. For frequently created textures, this could add overhead. Consider caching the device properties or checking if there's a pattern in the codebase for reusing device properties. However, this is a minor concern since texture creation is typically not done in a tight loop, and the fix is necessary for correctness.

Suggested change
cudaDeviceProp prop;
CV_CUDEV_SAFE_CALL(cudaGetDeviceProperties(&prop, currentDevice));
// Cache device properties per device to avoid repeated queries.
static int cachedDevice = -1;
static cudaDeviceProp cachedProp;
static bool propInitialized = false;
if (!propInitialized || currentDevice != cachedDevice)
{
CV_CUDEV_SAFE_CALL(cudaGetDeviceProperties(&cachedProp, currentDevice));
cachedDevice = currentDevice;
propInitialized = true;
}
const cudaDeviceProp& prop = cachedProp;

Copilot uses AI. Check for mistakes.
@@ -176,17 +176,28 @@ namespace cv { namespace cudev {
texRes.res.pitch2D.height = rows;
texRes.res.pitch2D.width = cols;
// temporary fix for single row/columns until TexturePtr is reworked
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "temporary fix for single row/columns until TexturePtr is reworked" is now outdated. The fix now also handles pitch alignment issues, not just single row/column cases. Consider updating the comment to reflect the expanded scope, such as: "temporary fix for single row/columns and misaligned pitch until TexturePtr is reworked".

Suggested change
// temporary fix for single row/columns until TexturePtr is reworked
// temporary fix for single row/columns and misaligned pitch until TexturePtr is reworked

Copilot uses AI. Check for mistakes.
Comment on lines +195 to 199
}
else {
texRes.res.pitch2D.devPtr = data;
texRes.res.pitch2D.pitchInBytes = step;
}
Copy link

Copilot AI Jan 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation: the closing brace and else statement use incorrect indentation. They should align with the opening brace on line 187, using 12 spaces instead of 11.

Suggested change
}
else {
texRes.res.pitch2D.devPtr = data;
texRes.res.pitch2D.pitchInBytes = step;
}
}
else {
texRes.res.pitch2D.devPtr = data;
texRes.res.pitch2D.pitchInBytes = step;
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant