From 9219ccf9d8c0d2713b389916b6f7b2e009ae6a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Mon, 8 Feb 2021 15:48:00 +0100 Subject: [PATCH] Textures can now hold SamplerStates --- .../src/Platform/DX11/Renderer/Dx11Texture.bf | 2 +- GlitchyEngine/src/Renderer/Texture.bf | 30 +++++++++++++++++-- Sandbox/src/SandboxApp.bf | 7 +++-- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf index 153547b..6699c0a 100644 --- a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf +++ b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11Texture.bf @@ -39,7 +39,7 @@ namespace GlitchyEngine.Renderer nativeTexture.GetDescription(out nativeDesc); } - public override void Bind(uint32 slot) + protected override void ImplBind(uint32 slot) { _context.nativeContext.VertexShader.SetShaderResources(slot, 1, &nativeView); _context.nativeContext.PixelShader.SetShaderResources(slot, 1, &nativeView); diff --git a/GlitchyEngine/src/Renderer/Texture.bf b/GlitchyEngine/src/Renderer/Texture.bf index 5cc05a2..97eec96 100644 --- a/GlitchyEngine/src/Renderer/Texture.bf +++ b/GlitchyEngine/src/Renderer/Texture.bf @@ -5,16 +5,40 @@ namespace GlitchyEngine.Renderer public abstract class Texture : RefCounted { protected GraphicsContext _context ~ _?.ReleaseRef(); - + + protected SamplerState _samplerState ~ _?.ReleaseRef(); + public GraphicsContext Context => _context; + public SamplerState SamplerState + { + get => _samplerState; + set + { + if(_samplerState == value) + return; + + _samplerState?.ReleaseRef(); + _samplerState = value; + _samplerState?.AddRef(); + } + } + public abstract uint32 Width {get;} public abstract uint32 Height {get;} public abstract uint32 Depth {get;} public abstract uint32 ArraySize {get;} public abstract uint32 MipLevels {get;} - public abstract void Bind(uint32 slot = 0); + public void Bind(uint32 slot = 0) + { + ImplBind(slot); + + if(_samplerState != null) + _samplerState.Bind(slot); + } + + protected abstract void ImplBind(uint32 slot); protected this(GraphicsContext context) { @@ -32,7 +56,7 @@ namespace GlitchyEngine.Renderer public override extern uint32 ArraySize {get;} public override extern uint32 MipLevels {get;} - public override extern void Bind(uint32 slot = 0); + protected override extern void ImplBind(uint32 slot); public this(GraphicsContext context, String path) : base(context) { diff --git a/Sandbox/src/SandboxApp.bf b/Sandbox/src/SandboxApp.bf index fe348d9..5420c97 100644 --- a/Sandbox/src/SandboxApp.bf +++ b/Sandbox/src/SandboxApp.bf @@ -69,7 +69,6 @@ namespace Sandbox GraphicsContext _context ~ _?.ReleaseRef(); Texture2D _texture ~ _?.ReleaseRef(); - SamplerState _sampler ~ _?.ReleaseRef(); private Vector3 CircleCoord(float angle) { @@ -195,11 +194,14 @@ namespace Sandbox _texture = new Texture2D(_context, "content/Textures/Checkerboard.dds"); - _sampler = new SamplerState(_context, + let sampler = new SamplerState(_context, SamplerStateDescription() { MagFilter = .Point }); + + _texture.SamplerState = sampler; + sampler.ReleaseRef(); } public override void Update(GameTime gameTime) @@ -269,7 +271,6 @@ namespace Sandbox _cBuffer.Update(); _texture.Bind(); - _sampler.Bind(); Renderer.Submit(_geometryBinding, _effect); Renderer.Submit(_quadGeometryBinding, _textureEffect, .Scaling(1.5f));