25 using SiliconStudio.Core;
26 using SiliconStudio.Core.Serialization.Converters;
28 namespace SiliconStudio.
Paradox.Graphics
36 [
DataConverter(AutoGenerate =
false, ContentReference =
true)]
37 public partial class Buffer : GraphicsResource
52 public int ElementCount
79 var stagingDesc = Description;
80 stagingDesc.Usage = GraphicsResourceUsage.Staging;
81 stagingDesc.BufferFlags = BufferFlags.None;
105 public TData[] GetData<TData>() where TData :
struct
107 var toData =
new TData[this.Description.SizeInBytes / Utilities.SizeOf<TData>()];
121 public void GetData<TData>(TData[] toData) where TData :
struct
127 GetData(
this, toData);
132 using (var throughStaging = this.ToStaging())
133 GetData(throughStaging, toData);
146 public void GetData<TData>(ref TData toData) where TData :
struct
152 GetData(
this, ref toData);
157 using (var throughStaging = this.ToStaging())
158 GetData(throughStaging, ref toData);
172 public unsafe
void GetData<TData>(
Buffer stagingTexture, ref TData toData) where TData :
struct
174 GetData(stagingTexture,
new DataPointer(Interop.Fixed(ref toData), Utilities.SizeOf<TData>()));
187 public unsafe
void GetData<TData>(
Buffer stagingTexture, TData[] toData) where TData :
struct
189 GetData(stagingTexture,
new DataPointer(Interop.Fixed(toData), toData.Length * Utilities.SizeOf<TData>()));
202 public void SetData<TData>(ref TData fromData,
int offsetInBytes = 0) where TData :
struct
217 public unsafe
void SetData<TData>(TData[] fromData,
int offsetInBytes = 0) where TData :
struct
247 public unsafe
void SetData<TData>(
GraphicsDevice device, ref TData fromData,
int offsetInBytes = 0) where TData :
struct
249 SetData(device,
new DataPointer(Interop.Fixed(ref fromData), Utilities.SizeOf<TData>()), offsetInBytes);
263 public unsafe
void SetData<TData>(
GraphicsDevice device, TData[] fromData,
int offsetInBytes = 0) where TData :
struct
265 SetData(device,
new DataPointer(Interop.Fixed(fromData), (fromData.Length * Utilities.SizeOf<TData>())), offsetInBytes);
281 if (toData.
Size >
this.Description.SizeInBytes)
282 throw new ArgumentException(
"Length of TData is larger than size of buffer");
285 if (!ReferenceEquals(
this, stagingTexture))
286 GraphicsDevice.Copy(
this, stagingTexture);
289 var mappedResource = GraphicsDevice.MapSubresource(stagingTexture, 0, MapMode.Read);
290 Utilities.CopyMemory(toData.Pointer, mappedResource.DataBox.DataPointer, toData.Size);
292 GraphicsDevice.UnmapSubresource(mappedResource);
308 if (fromData.
Size >
this.Description.SizeInBytes)
309 throw new ArgumentException(
"Size of data to upload larger than size of buffer");
315 if ((this.Description.BufferFlags &
BufferFlags.ConstantBuffer) != 0)
317 device.UpdateSubresource(
this, 0,
new DataBox(fromData.
Pointer, 0, 0));
321 var destRegion =
new ResourceRegion(offsetInBytes, 0, 0, offsetInBytes + fromData.
Size, 1, 1);
322 device.UpdateSubresource(
this, 0,
new DataBox(fromData.
Pointer, 0, 0), destRegion);
327 if (offsetInBytes > 0)
328 throw new ArgumentException(
"offset is only supported for textured declared with ResourceUsage.Default",
"offsetInBytes");
330 var mappedResource = device.MapSubresource(
this, 0, MapMode.WriteDiscard);
331 Utilities.CopyMemory(mappedResource.DataBox.DataPointer, fromData.Pointer, fromData.Size);
332 device.UnmapSubresource(mappedResource);
346 return new Buffer(device, description, bufferType, viewFormat, IntPtr.Zero);
359 return New(device, bufferSize, 0, bufferFlags,
PixelFormat.None, usage);
372 int bufferSize = Utilities.SizeOf<T>() * elementCount;
373 int elementSize = Utilities.SizeOf<T>();
375 var description = NewDescription(bufferSize, elementSize, bufferFlags, usage);
376 return new Buffer<T>(device, description, bufferFlags, PixelFormat.None, IntPtr.Zero);
390 return New(device, bufferSize, 0, bufferFlags, viewFormat, usage);
404 return New(device, bufferSize, elementSize, bufferFlags,
PixelFormat.None, usage);
419 viewFormat = CheckPixelFormat(bufferFlags, elementSize, viewFormat);
420 var description = NewDescription(bufferSize, elementSize, bufferFlags, usage);
421 return new Buffer(device, description, bufferFlags, viewFormat, IntPtr.Zero);
435 return New(device, ref value, bufferFlags,
PixelFormat.None, usage);
450 int bufferSize = Utilities.SizeOf<T>();
451 int elementSize = ((bufferFlags & BufferFlags.StructuredBuffer) != 0) ? Utilities.SizeOf<T>() : 0;
453 viewFormat = CheckPixelFormat(bufferFlags, elementSize, viewFormat);
455 var description = NewDescription(bufferSize, elementSize, bufferFlags, usage);
456 return new Buffer<T>(device, description, bufferFlags, viewFormat, (IntPtr)Interop.Fixed(ref value));
470 return New(device, initialValue, bufferFlags,
PixelFormat.None, usage);
485 int bufferSize = Utilities.SizeOf<T>() * initialValue.Length;
487 viewFormat = CheckPixelFormat(bufferFlags, elementSize, viewFormat);
489 var description = NewDescription(bufferSize, elementSize, bufferFlags, usage);
490 return new Buffer<T>(device, description, bufferFlags, viewFormat, (IntPtr)Interop.Fixed(initialValue));
505 int bufferSize = initialValue.Length;
506 viewFormat = CheckPixelFormat(bufferFlags, elementSize, viewFormat);
508 var description = NewDescription(bufferSize, elementSize, bufferFlags, usage);
509 return new Buffer(device, description, bufferFlags, viewFormat, (IntPtr)Interop.Fixed(initialValue));
523 return New(device, dataPointer, elementSize, bufferFlags,
PixelFormat.None, usage);
538 int bufferSize = dataPointer.Size;
539 viewFormat = CheckPixelFormat(bufferFlags, elementSize, viewFormat);
540 var description = NewDescription(bufferSize, elementSize, bufferFlags, usage);
541 return new Buffer(device, description, bufferFlags, viewFormat, dataPointer.
Pointer);
548 if (elementSize != 2 && elementSize != 4)
549 throw new ArgumentException(
"Element size must be set to sizeof(short) = 2 or sizeof(int) = 4 for index buffer if index buffer is bound to a ShaderResource",
"elementSize");
551 viewFormat = elementSize == 2 ? PixelFormat.R16_UInt : PixelFormat.R32_UInt;
558 return new BufferDescription() {
559 SizeInBytes = bufferSize,
560 StructureByteStride = (bufferFlags & BufferFlags.StructuredBuffer) != 0 ? elementSize : 0,
572 public Buffer RecreateWith<T>(T[] dataPointer) where T :
struct
574 Reload = (graphicsResource) => ((
Buffer)graphicsResource).Recreate(dataPointer);
587 Reload = (graphicsResource) => ((
Buffer)graphicsResource).Recreate(dataPointer);
597 public void Recreate<T>(T[] dataPointer) where T :
struct
599 Utilities.Pin(dataPointer, Recreate);
611 this.ElementSize = Utilities.SizeOf<T>();
612 this.ElementCount = Description.SizeInBytes / ElementSize;
641 public void SetData(ref T fromData,
int offsetInBytes = 0)
643 base.SetData(ref fromData, offsetInBytes);
654 public void SetData(T[] fromData,
int offsetInBytes = 0)
656 base.SetData(fromData, offsetInBytes);
671 base.SetData(device, ref fromData, offsetInBytes);
685 base.SetData(fromData, offsetInBytes);
static Buffer New(GraphicsDevice device, int bufferSize, int elementSize, BufferFlags bufferFlags, PixelFormat viewFormat, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Buffer instance.
T[] GetData()
Gets the content of this texture to an array of data.
static unsafe Buffer New(GraphicsDevice device, byte[] initialValue, int elementSize, BufferFlags bufferFlags, PixelFormat viewFormat=PixelFormat.None, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Creates a new Buffer instance from a byte array.
void SetData(DataPointer fromData, int offsetInBytes=0)
Copies the content an array of data on CPU memory to this buffer into GPU memory. ...
static Buffer New(GraphicsDevice device, int bufferSize, BufferFlags bufferFlags, PixelFormat viewFormat, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Buffer instance.
Buffer Clone()
Clones this instance.
Base class for converters to/from a data type.
GraphicsResourceUsage
Identifies expected resource use during rendering. The usage directly reflects whether a resource is ...
void SetData(GraphicsDevice device, T[] fromData, int offsetInBytes=0)
Copies the content an array of data from CPU memory to this buffer into GPU memory.
void SetData(GraphicsDevice device, ref T fromData, int offsetInBytes=0)
Copies the content of a single structure data from CPU memory to this buffer into GPU memory...
void SetData(T[] fromData, int offsetInBytes=0)
Copies the content an array of data from CPU memory to this buffer into GPU memory.
All-in-One Buffer class linked SharpDX.Direct3D11.Buffer.
SiliconStudio.Paradox.Graphics.Buffer Buffer
static Buffer New(GraphicsDevice device, DataPointer dataPointer, int elementSize, BufferFlags bufferFlags, PixelFormat viewFormat, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Buffer instance.
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
A buffer with typed information.
static Buffer New(GraphicsDevice device, BufferDescription description, PixelFormat viewFormat=PixelFormat.None)
Creates a new Buffer instance.
static Buffer New(GraphicsDevice device, int bufferSize, BufferFlags bufferFlags, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Buffer instance.
readonly int ElementSize
Gets the size of element T.
static Buffer New(GraphicsDevice device, int bufferSize, int elementSize, BufferFlags bufferFlags, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Buffer instance.
readonly PixelFormat ViewFormat
Gets the default view format of this buffer.
void GetData(Buffer stagingTexture, DataPointer toData)
Copies the content of this buffer from GPU memory to a CPU memory using a specific staging resource...
readonly BufferDescription Description
Gets the description of this buffer.
static Buffer New(GraphicsDevice device, DataPointer dataPointer, int elementSize, BufferFlags bufferFlags, GraphicsResourceUsage usage=GraphicsResourceUsage.Default)
Creates a new Buffer instance.
Provides access to data organized in 3D.
void SetData(ref T fromData, int offsetInBytes=0)
Copies the content of a single structure data from CPU memory to this buffer into GPU memory...
void SetData(GraphicsDevice device, DataPointer fromData, int offsetInBytes=0)
Copies the content an array of data on CPU memory to this buffer into GPU memory. ...
PixelFormat
Defines various types of pixel formats.
readonly BufferFlags BufferFlags
Gets the type of this belementCount ///
Buffer RecreateWith(IntPtr dataPointer)
Reload Buffer from given data if GraphicsDevice has been reset.
Buffer ToStaging()
Return an equivalent staging texture CPU read-writable from this instance.