Ejemplo n.º 1
0
        public unsafe void SetMaterialConstantValue <TValue>(ConstantBufferBinding binding, TValue value) where TValue : struct
        {
            Assure.True(Shader.ContainsBinding(binding), "Binding is not attributed to the FragmentShader set for this material.");
            Assure.True(
                binding.GetBoundResource() is ConstantBuffer <TValue>,
                "Expected a resource of type 'ConstantBuffer<" + typeof(TValue).Name + ">' set at " +
                binding + ", but instead found " + binding.GetBoundResource().ToStringNullSafe("no binding") + "."
                );
            Assure.True(
                binding.GetBoundResource().CanDiscardWrite,
                "Given shader resource (" + binding.GetBoundResource() + ") must have discard-write capability."
                );

            if (cbufferValuePtrs.ContainsKey(binding))
            {
                Marshal.FreeHGlobal(cbufferValuePtrs[binding]);
            }
            IntPtr valuePtr = Marshal.AllocHGlobal(new IntPtr(binding.BufferSizeBytes));

            UnsafeUtils.WriteGenericToPtr(valuePtr, value, binding.BufferSizeBytes);
            using (RenderingModule.RenderStateBarrier.AcquirePermit(withLock: instanceMutationLock)) {
                cbufferValuePtrs[binding] = valuePtr;
                fragmentShaderResources.SetValue(binding, valuePtr);
            }
        }
 /// <summary>
 /// Gets or sets the data at the requested co-ordinates.
 /// </summary>
 /// <remarks>
 /// For reading/writing single elements, using the this member is recommended, but may be slow when attempting
 /// to copy large sections of the data. In these circumstances, consider using something like
 /// <see cref="UnsafeUtils.CopyGenericArray{T}(Ophidian.Losgap.ArraySlice{T},System.IntPtr,uint)"/> /
 /// <see cref="UnsafeUtils.CopyGenericArray{T}(System.IntPtr,Ophidian.Losgap.ArraySlice{T},uint)"/> in combination with the
 /// <see cref="Data"/> member.
 /// </remarks>
 /// <param name="u">The u-coordinate to copy.</param>
 /// <returns>A copy of the data at the requested co-ordinate.</returns>
 public T this[uint u] {
     get {
         Assure.LessThan(u, Width, "Index out of bounds: u");
         return(UnsafeUtils.ReadGenericFromPtr <T>(Data + (int)(sizeOfT * u), sizeOfT));
     }
     set {
         Assure.LessThan(u, Width, "Index out of bounds: u");
         UnsafeUtils.WriteGenericToPtr(Data + (int)(sizeOfT * u), value, sizeOfT);
     }
 }
 /// <summary>
 /// Gets or sets the data at the requested co-ordinates.
 /// </summary>
 /// <remarks>
 /// For reading/writing single elements, using this member is recommended, but may be slow when attempting
 /// to copy large sections of the data. In these circumstances, consider using something like
 /// <see cref="UnsafeUtils.CopyGenericArray{T}(Ophidian.Losgap.ArraySlice{T},System.IntPtr,uint)"/> /
 /// <see cref="UnsafeUtils.CopyGenericArray{T}(System.IntPtr,Ophidian.Losgap.ArraySlice{T},uint)"/> in combination with the
 /// <see cref="Data"/> member.
 /// </remarks>
 /// <param name="u">The u-coordinate to copy.</param>
 /// <param name="v">The v-coordinate to copy.</param>
 /// <returns>A copy of the data at the requested co-ordinate.</returns>
 public T this[uint u, uint v] {
     get {
         Assure.LessThan(u, Width, "Index out of bounds: u");
         Assure.LessThan(v, Height, "Index out of bounds: v");
         return(UnsafeUtils.ReadGenericFromPtr <T>(Data + (int)(u * sizeOfT + v * rowStrideBytes), sizeOfT));
     }
     set {
         Assure.LessThan(u, Width, "Index out of bounds: u");
         Assure.LessThan(v, Height, "Index out of bounds: v");
         UnsafeUtils.WriteGenericToPtr(Data + (int)(u * sizeOfT + v * rowStrideBytes), value, sizeOfT);
     }
 }
Ejemplo n.º 4
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.º 5
0
 /// <summary>
 /// Write a new value to the <see cref="AlignedPointer"/>.
 /// </summary>
 /// <param name="value">The new value to store in this allocation.</param>
 public void Write(T value)
 {
     Assure.NotEqual(AlignedPointer, IntPtr.Zero);
     UnsafeUtils.WriteGenericToPtr(AlignedPointer, value, sizeOfT);
 }