{"record":{"id":"d1e4ce036634898b","repo":"stride3d/stride","slug":"cannot-copy-data-between-buffers-and-textures","errorCode":null,"errorMessage":"Cannot copy data between Buffers and Textures.","messagePattern":"Cannot copy data between Buffers and Textures\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Graphics/Direct3D12/CommandList.Direct3D12.cs","lineNumber":1737,"sourceCode":"        ///     <see cref=\"CopyRegion\"/> only supports copy; it doesn't support any stretch, color key, or blend.\n        ///   </para>\n        /// </remarks>\n        public void CopyRegion(GraphicsResource source, int sourceSubResourceIndex, ResourceRegion? sourceRegion,\n                               GraphicsResource destination, int destinationSubResourceIndex, int dstX = 0, int dstY = 0, int dstZ = 0)\n        {\n            RecordDebugCounter(DebugCounterKind.Copy);\n\n            if (source is Texture sourceTexture &&\n                destination is Texture destinationTexture)\n            {\n                CopyBetweenTextures(sourceTexture, destinationTexture);\n            }\n            else if (source is Buffer sourceBuffer &&\n                     destination is Buffer destinationBuffer)\n            {\n                CopyBetweenBuffers(sourceBuffer, destinationBuffer);\n            }\n            else throw new InvalidOperationException(\"Cannot copy data between Buffers and Textures.\");\n\n            //\n            // Copies a region from a Texture to another Texture.\n            //\n            void CopyBetweenTextures(Texture sourceTexture, Texture destinationTexture)\n            {\n                if (sourceTexture.Usage == GraphicsResourceUsage.Staging &&\n                    destinationTexture.Usage == GraphicsResourceUsage.Staging)\n                {\n                    throw new NotImplementedException(\"Copy region of staging resources is not supported yet\"); // TODO: Implement copy region for staging resources\n                }\n\n                ResourceBarrierTransition(source, BarrierLayout.CopySource);\n                ResourceBarrierTransition(destination, BarrierLayout.CopyDest);\n                FlushResourceBarriers();\n\n                if (sourceTexture.Usage == GraphicsResourceUsage.Staging)\n                {","sourceCodeStart":1719,"sourceCodeEnd":1755,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Graphics/Direct3D12/CommandList.Direct3D12.cs#L1719-L1755","documentation":"The region-copy method (CopyRegion) supports only same-type pairs: Texture->Texture and Buffer->Buffer. Any other combination, including copying between a Buffer and a Texture, throws InvalidOperationException because the D3D12 region-copy path in this method does not implement cross-type conversions.","triggerScenarios":"Calling CommandList.CopyRegion(source, destination, ...) with source/destination of mixed types (Buffer vs Texture) or otherwise unmatched GraphicsResource types.","commonSituations":"Trying to upload a buffer into a texture region or read back a texture region into a buffer via CopyRegion; generic copy helpers passing arbitrary resources; argument order/type mistakes.","solutions":["Use the dedicated CopyCommandList.Copy(Buffer, Texture) / Copy(Texture, Buffer) methods for cross-type transfers.","Call CopyRegion only with matching types (both Buffers or both Textures).","Add a runtime type check before dispatching to CopyRegion."],"exampleFix":"// before\ncommandList.CopyRegion(buffer, texture, sourceRegion); // throws\n\n// after\nif (source is Texture t && destination is Texture d)\n    commandList.CopyRegion(t, d, sourceRegion);\nelse\n    commandList.CopyCommandList.Copy(source, destination); // cross-type path","handlingStrategy":"type-guard","validationCode":"bool ok = (source is Buffer && destination is Buffer) || (source is Texture && destination is Texture);\nif (!ok) throw new InvalidOperationException(\"CopyRegion supports only Buffer->Buffer or Texture->Texture\");","typeGuard":"static bool IsSameKindPair(GraphicsResource s, GraphicsResource d) =>\n    s.GetType() == d.GetType() && (s is Buffer || s is Texture);","tryCatchPattern":"try\n{\n    commandList.CopyRegion(source, destination, region);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Buffers and Textures\"))\n{\n    commandList.CopyCommandList.Copy(source, destination); // cross-type transfer\n}","preventionTips":["Route all Buffer<->Texture transfers through CopyCommandList.Copy, never CopyRegion.","Validate resource types before generic copy helpers call CopyRegion.","Add integration tests covering both same-type and cross-type copy paths."],"tags":["direct3d12","graphics","copy","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}