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);
            }
        }
Ejemplo n.º 2
0
        public static RenderCommand DiscardWriteShaderConstantBuffer(ConstantBufferBinding binding, IntPtr valuePtr)
        {
            Assure.NotNull(binding);
            Assure.NotEqual(valuePtr, IntPtr.Zero, "valuePtr must not be IntPtr.Zero!");
            Assure.False(binding.IsDisposed || binding.GetBoundResource().IsDisposed, "Given binding or its resource was disposed.");
            IntPtr cbufferValPtr = AllocAndZeroTemp(binding.BufferSizeBytes);

            UnsafeUtils.MemCopy(valuePtr, cbufferValPtr, binding.BufferSizeBytes);
            return(new RenderCommand(RenderCommandInstruction.CBDiscardWrite, (IntPtr)binding.GetBoundResource().ResourceHandle, cbufferValPtr, binding.BufferSizeBytes));
        }
Ejemplo n.º 3
0
        public unsafe void TestDiscardWriteShaderConstantBuffer()
        {
            ConstantBuffer <Vector4> cb  = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite);
            ConstantBufferBinding    cbb = new ConstantBufferBinding(0U, "CB0", cb);
            Vector4 initialValue         = Vector4.FORWARD;

            cbb.SetValue((byte *)(&initialValue));

            RenderCommand testCommand = RenderCommand.DiscardWriteShaderConstantBuffer(cbb, cbb.CurValuePtr);

            Assert.AreEqual(RenderCommandInstruction.CBDiscardWrite, testCommand.Instruction);
            Assert.AreEqual((RenderCommandArgument)(IntPtr)cbb.GetBoundResource().ResourceHandle, testCommand.Arg1);
            Assert.AreEqual(*((Vector4 *)cbb.CurValuePtr), *((Vector4 *)new IntPtr(UnsafeUtils.Reinterpret <RenderCommandArgument, long>(testCommand.Arg2, sizeof(long)))));
            Assert.AreEqual((RenderCommandArgument)cbb.BufferSizeBytes, testCommand.Arg3);

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.DiscardWriteShaderConstantBuffer(null, cbb.CurValuePtr);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }

            try {
                RenderCommand.DiscardWriteShaderConstantBuffer(cbb, IntPtr.Zero);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            (cbb as IDisposable).Dispose();

#if !DEVELOPMENT && !RELEASE
            try {
                RenderCommand.DiscardWriteShaderConstantBuffer(cbb, cbb.CurValuePtr);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            cb.Dispose();
        }