Ejemplo n.º 1
0
 internal VertexBufferBuilder(ResourceUsage usage, uint length, ArraySlice <TVertex>?initialData)
     : base(usage, initialData)
 {
     Assure.True(typeof(TVertex).IsBlittable());
     Assure.GreaterThan(UnsafeUtils.SizeOf <TVertex>(), 0);
     this.length = length;
 }
Ejemplo n.º 2
0
 internal BufferBuilder(ResourceUsage usage, uint length, GPUBindings permittedBindings,
                        ArraySlice <TElement>?initialData)
     : base(usage, initialData)
 {
     Assure.True(typeof(TElement).IsBlittable());
     Assure.GreaterThan(UnsafeUtils.SizeOf <TElement>(), 0);
     this.length            = length;
     this.permittedBindings = permittedBindings;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new <see cref="Buffer{TElement}"/> with the supplied builder parameters.
        /// </summary>
        /// <remarks>
        /// In debug mode, this method will check a large number of <see cref="Assure">assurances</see>
        /// on the builder parameters before creating the resource.
        /// </remarks>
        /// <returns>A new <see cref="Buffer{TElement}"/>.</returns>
        public unsafe override Buffer <TElement> Create()
        {
            Assure.True(Usage != ResourceUsage.Immutable || InitialData != null, "You must supply initial data to an immutable resource.");
            Assure.False(
                (Usage == ResourceUsage.Immutable || Usage == ResourceUsage.DiscardWrite) && permittedBindings == GPUBindings.None,
                "An immutable or discard-write resource with no permitted bindings is useless."
                );
            Assure.False(
                Usage.GetUsage() == 0x3 && permittedBindings != GPUBindings.None,
                "Staging resources can not be bound to the pipeline."
                );
            Assure.GreaterThan(length, 0U, "Can not create a buffer with 0 elements.");

            InteropBool isStructured   = (BaseResource.GetFormatForType(typeof(TElement)) == ResourceFormat.Unknown);
            InteropBool allowRawAccess =
                !isStructured &&
                (int)(permittedBindings & (GPUBindings.WritableShaderResource | GPUBindings.ReadableShaderResource)) != 0;

            GCHandle?pinnedArrayHandle = null;
            IntPtr   initialDataPtr    = IntPtr.Zero;

            try {
                int elementSizeBytes = UnsafeUtils.SizeOf <TElement>();

                if (InitialData != null)
                {
                    pinnedArrayHandle = GCHandle.Alloc(InitialData.Value.ContainingArray, GCHandleType.Pinned);
                    initialDataPtr    = pinnedArrayHandle.Value.AddrOfPinnedObject() + (elementSizeBytes * (int)InitialData.Value.Offset);
                }

                BufferResourceHandle outResourceHandle;
                InteropUtils.CallNative(NativeMethods.ResourceFactory_CreateBuffer,
                                        RenderingModule.Device,
                                        (uint)elementSizeBytes,
                                        length,
                                        Usage.GetUsage(),
                                        Usage.GetCPUUsage(),
                                        (PipelineBindings)permittedBindings,
                                        isStructured,
                                        allowRawAccess,
                                        initialDataPtr,
                                        (IntPtr)(&outResourceHandle)
                                        ).ThrowOnFailure();

                return(new Buffer <TElement>(outResourceHandle, Usage, (uint)elementSizeBytes, length, permittedBindings, isStructured));
            }
            finally {
                if (pinnedArrayHandle != null)
                {
                    pinnedArrayHandle.Value.Free();
                }
            }
        }
 internal Texture1DBuilder(ResourceUsage usage, ArraySlice <TTexel>?initialData, GPUBindings permittedBindings,
                           uint width, bool mipAllocation, bool mipGenerationTarget, bool dynamicDetail) : base(usage, initialData)
 {
     Assure.True(typeof(TTexel).IsBlittable());
     Assure.GreaterThan(UnsafeUtils.SizeOf <TTexel>(), 0);
     this.permittedBindings   = permittedBindings;
     this.width               = width;
     this.mipAllocation       = mipAllocation;
     this.mipGenerationTarget = mipGenerationTarget;
     this.dynamicDetail       = dynamicDetail;
     this.numMips             = mipAllocation ? TextureUtils.GetNumMips(width) : 1U;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new <see cref="ConstantBuffer{TConstants}"/> with the supplied builder parameters.
        /// </summary>
        /// <remarks>
        /// In debug mode, this method will check a large number of <see cref="Assure">assurances</see>
        /// on the builder parameters before creating the resource.
        /// </remarks>
        /// <returns>A new <see cref="ConstantBuffer{TConstants}"/>.</returns>
        public unsafe override ConstantBuffer <TConstants> Create()
        {
            uint  structSizeBytes    = (uint)UnsafeUtils.SizeOf <TConstants>();
            byte *initValueStackCopy = stackalloc byte[(int)structSizeBytes];

            UnsafeUtils.WriteGenericToPtr((IntPtr)initValueStackCopy, InitialData, structSizeBytes);

            BufferResourceHandle outResourceHandle;

            InteropUtils.CallNative(NativeMethods.ResourceFactory_CreateConstantBuffer,
                                    RenderingModule.Device,
                                    structSizeBytes,
                                    Usage.GetUsage(),
                                    Usage.GetCPUUsage(),
                                    (IntPtr)initValueStackCopy,
                                    (IntPtr)(&outResourceHandle)
                                    ).ThrowOnFailure();

            return(new ConstantBuffer <TConstants>(outResourceHandle, Usage, structSizeBytes));
        }
Ejemplo n.º 6
0
        public static AlignedAllocation <T> AllocArray(long alignment, uint arrLen)
        {
            Assure.GreaterThan(alignment, 0L, "Alignment must be positive.");

            uint sizeOfT         = (uint)UnsafeUtils.SizeOf <T>();
            uint reservationSize = sizeOfT * arrLen;

            IntPtr allocStart       = Marshal.AllocHGlobal(new IntPtr(reservationSize + alignment - 1));
            long   allocationOffset = (long)allocStart % alignment;

            IntPtr alignedStart;

            if (allocationOffset == 0L)
            {
                alignedStart = allocStart;
            }
            else
            {
                alignedStart = allocStart + (int)(alignment - allocationOffset);
            }

            return(new AlignedAllocation <T>(alignedStart, sizeOfT, allocStart));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a new <see cref="VertexBuffer{TVertex}"/> with the supplied builder parameters.
        /// </summary>
        /// <remarks>
        /// In debug mode, this method will check a large number of <see cref="Assure">assurances</see>
        /// on the builder parameters before creating the resource.
        /// </remarks>
        /// <returns>A new <see cref="VertexBuffer{TVertex}"/>.</returns>
        public unsafe override VertexBuffer <TVertex> Create()
        {
            Assure.True(Usage != ResourceUsage.Immutable || InitialData != null, "You must supply initial data to an immutable resource.");
            Assure.GreaterThan(length, 0U, "Can not create a vertex buffer with 0 vertices.");

            GCHandle?pinnedArrayHandle = null;
            IntPtr   initialDataPtr    = IntPtr.Zero;

            try {
                int vertexSize = UnsafeUtils.SizeOf <TVertex>();

                if (InitialData != null)
                {
                    pinnedArrayHandle = GCHandle.Alloc(InitialData.Value.ContainingArray, GCHandleType.Pinned);
                    initialDataPtr    = pinnedArrayHandle.Value.AddrOfPinnedObject() + (vertexSize * (int)InitialData.Value.Offset);
                }

                BufferResourceHandle outResourceHandle;
                InteropUtils.CallNative(NativeMethods.ResourceFactory_CreateVertexBuffer,
                                        RenderingModule.Device,
                                        (uint)vertexSize,
                                        length,
                                        Usage.GetUsage(),
                                        Usage.GetCPUUsage(),
                                        initialDataPtr,
                                        (IntPtr)(&outResourceHandle)
                                        ).ThrowOnFailure();

                return(new VertexBuffer <TVertex>(outResourceHandle, Usage, (uint)vertexSize, length));
            }
            finally {
                if (pinnedArrayHandle != null)
                {
                    pinnedArrayHandle.Value.Free();
                }
            }
        }
Ejemplo n.º 8
0
 private unsafe D3D11_VIEWPORT GetViewportStruct(SceneViewport vp)
 {
     return(*((D3D11_VIEWPORT *)UnsafeUtils.Reinterpret <ViewportHandle, IntPtr>(vp.ViewportHandle, UnsafeUtils.SizeOf <ViewportHandle>())));
 }
Ejemplo n.º 9
0
 internal ConstantBufferBuilder(ResourceUsage usage, TConstants initialData) : base(usage, initialData)
 {
     Assure.True(typeof(TConstants).IsBlittable());
     Assure.GreaterThan(UnsafeUtils.SizeOf <TConstants>(), 0);
     Assure.Equal(UnsafeUtils.SizeOf <TConstants>() % 16, 0, "Constant buffer size must be a multiple of 16 bytes.");
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Creates a new aligned <typeparamref name="T"/> with the given <paramref name="alignment"/> in the unmanaged heap.
 /// </summary>
 /// <param name="alignment">The alignment. The allocated pointer's address will be divisible by this value.
 /// Must be a positive value.</param>
 public AlignedAllocation(long alignment) : this(alignment, (uint)UnsafeUtils.SizeOf <T>())
 {
 }