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); } }
private void SetVSResources() { if (dlLightVS != null && lightPlaneVertices != null) { vsResPackage.SetValue((VertexInputBinding)dlLightVS.GetBindingByIdentifier("POSITION"), lightPlaneVertices); lightVSScalarsBufferBinding = ((ConstantBufferBinding)dlLightVS.GetBindingByIdentifier("Scalars")); } }
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)); }
private Font(string name, uint lineHeightPixels, int kerningPixels, GeometryCache characterGeometry, ITexture2D[] characterPages, ShaderResourceView[] characterPageViews, Dictionary <char, FontCharacter> characters, ConstantBufferBinding textFsColorCbb, uint maxCharHeight) { Name = name; LineHeightPixels = lineHeightPixels; KerningPixels = kerningPixels; this.characterPages = characterPages; this.characters = characters; TextFSColorCBB = textFsColorCbb; MaxCharHeight = maxCharHeight; CharacterGeometry = characterGeometry; this.characterPageViews = characterPageViews; }
public IntPtr GetValue(ConstantBufferBinding binding) { lock (instanceMutationLock) { Assure.NotNull(binding); if (cbBindings.ContainsKey(binding)) { return(cbBindings[binding]); } else { return(binding.CurValuePtr); } } }
public VertexShader(string filename, params IShaderResourceBinding[] otherBindings) : base(ShaderType.Vertex, filename, otherBindings) { InstanceDataBinding = null; ViewProjMatBinding = null; this.InputBindings = ResourceBindings.OfType <VertexInputBinding>().ToArray(); if (InputBindings.Length > MAX_VS_INPUT_BINDINGS) { throw new ArgumentException("Can not specify more than " + MAX_VS_INPUT_BINDINGS + " vertex input bindings.", "otherBindings"); } this.NumInputSlots = InputBindings.Any() ? InputBindings.Max(bind => bind.SlotIndex) + 1U : 0U; if (NumInputSlots > MAX_VS_INPUT_BINDINGS) { throw new ArgumentException("Can not set more than " + MAX_VS_INPUT_BINDINGS + " input slots."); } }
public HUDTexture(FragmentShader fragmentShader, SceneLayer targetLayer, SceneViewport targetViewport) { lock (staticMutationLock) { if (hudTextureModel == null) { throw new InvalidOperationException("HUD Texture Model must be set before creating any HUDTexture objects."); } } this.targetViewport = targetViewport; this.material = new Material("HUDTexture Mat", fragmentShader); this.curModelInstance = targetLayer.CreateModelInstance(hudTextureModel.Value, material, Transform.DEFAULT_TRANSFORM); this.fsTexColorMultiplyBinding = (ConstantBufferBinding)fragmentShader.GetBindingByIdentifier(HUDFS_TEX_PROPS_CBB_NAME); this.fsTexBinding = (ResourceViewBinding)fragmentShader.GetBindingByIdentifier(HUDFS_TEX_BINDING_NAME); material.SetMaterialConstantValue(fsTexColorMultiplyBinding, color); targetViewport.TargetWindow.WindowResized += WindowResize; this.fragmentShader = fragmentShader; this.sceneLayer = targetLayer; }
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(); }
public void TestSettingMaterialProperties() { ConstantBuffer <Vector4> matColorBuffer = BufferFactory.NewConstantBuffer <Vector4>().WithUsage(ResourceUsage.DiscardWrite); TextureSampler textureSampler = new TextureSampler(TextureFilterType.Anisotropic, TextureWrapMode.Border, AnisotropicFilteringLevel.EightTimes); FragmentShader testFS = new FragmentShader( @"Tests\SimpleFS.cso", new ConstantBufferBinding(0U, "MaterialColor", matColorBuffer), new TextureSamplerBinding(0U, "DefaultSampler") ); Material testMaterial = new Material("Test Material", testFS); testMaterial.SetMaterialConstantValue((ConstantBufferBinding)testFS.GetBindingByIdentifier("MaterialColor"), Vector4.ONE); testMaterial.SetMaterialResource((TextureSamplerBinding)testFS.GetBindingByIdentifier("DefaultSampler"), textureSampler); #if !DEVELOPMENT && !RELEASE ConstantBufferBinding cb = new ConstantBufferBinding(1U, "Test", matColorBuffer); try { testMaterial.SetMaterialConstantValue(cb, Vector4.RIGHT); Assert.Fail(); } catch (AssuranceFailedException) { } finally { (cb as IDisposable).Dispose(); } try { testMaterial.SetMaterialConstantValue((ConstantBufferBinding)testFS.GetBindingByIdentifier("MaterialColor"), Matrix.IDENTITY); Assert.Fail(); } catch (AssuranceFailedException) { } #endif testFS.Dispose(); matColorBuffer.Dispose(); testMaterial.Dispose(); textureSampler.Dispose(); }
/// <summary> /// Sets the value for the given <paramref name="binding"/>. /// </summary> /// <param name="binding">The <see cref="ConstantBufferBinding"/> to pair a value with.</param> /// <param name="value">A pointer to the value to pair with the given <paramref name="binding"/>. The pointer must /// remain valid for as long as it is set as the value on this resource package.</param> public void SetValue(ConstantBufferBinding binding, IntPtr value) { lock (instanceMutationLock) { this.cbBindings[binding] = value; } }