mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Added Textures
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using DirectX.D3D11;
|
||||
using DirectX.Common;
|
||||
using DirectXTK;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
extension Texture2D
|
||||
{
|
||||
internal ID3D11Texture2D* nativeTexture ~ _?.Release();
|
||||
internal ID3D11ShaderResourceView* nativeView ~ _?.Release();
|
||||
internal Texture2DDescription nativeDesc;
|
||||
|
||||
public override uint32 Width => nativeDesc.Width;
|
||||
public override uint32 Height => nativeDesc.Height;
|
||||
public override uint32 ArraySize => nativeDesc.ArraySize;
|
||||
public override uint32 MipLevels => nativeDesc.MipLevels;
|
||||
|
||||
protected override void LoadTexturePlatform()
|
||||
{
|
||||
nativeTexture?.Release();
|
||||
nativeView?.Release();
|
||||
|
||||
HResult loadResult = DDSTextureLoader.CreateDDSTextureFromFile(_context.nativeDevice, _path.ToScopedNativeWChar!(),
|
||||
(.)&nativeTexture, &nativeView);
|
||||
|
||||
if(loadResult.Failed)
|
||||
{
|
||||
Log.EngineLogger.Error($"Failed to load texture \"{_path}\". Error({(int)loadResult}): {loadResult}");
|
||||
|
||||
nativeTexture?.Release();
|
||||
nativeView?.Release();
|
||||
|
||||
// TODO: load fallback texture
|
||||
}
|
||||
|
||||
nativeTexture.GetDescription(out nativeDesc);
|
||||
}
|
||||
|
||||
public override void Bind(uint32 slot)
|
||||
{
|
||||
_context.nativeContext.VertexShader.SetShaderResources(slot, 1, &nativeView);
|
||||
_context.nativeContext.PixelShader.SetShaderResources(slot, 1, &nativeView);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public abstract class Texture : RefCounted
|
||||
{
|
||||
protected GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
public GraphicsContext Context => _context;
|
||||
|
||||
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);
|
||||
|
||||
protected this(GraphicsContext context)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
}
|
||||
}
|
||||
|
||||
public class Texture2D : Texture
|
||||
{
|
||||
protected String _path ~ delete _;
|
||||
|
||||
public override extern uint32 Width {get;}
|
||||
public override extern uint32 Height {get;}
|
||||
public override uint32 Depth => 1;
|
||||
public override extern uint32 ArraySize {get;}
|
||||
public override extern uint32 MipLevels {get;}
|
||||
|
||||
public override extern void Bind(uint32 slot = 0);
|
||||
|
||||
public this(GraphicsContext context, String path) : base(context)
|
||||
{
|
||||
this._path = new String(path);
|
||||
LoadTexturePlatform();
|
||||
}
|
||||
|
||||
protected extern void LoadTexturePlatform();
|
||||
}
|
||||
}
|
||||
Vendored
+1
-1
Submodule GlitchyEngine/vendor/DirectXTK updated: 8fc6870ac0...b53d2f196e
Binary file not shown.
@@ -11,7 +11,6 @@ cbuffer ObjectConstants
|
||||
float4x4 Transform;
|
||||
}
|
||||
|
||||
|
||||
cbuffer Constants
|
||||
{
|
||||
float4 BaseColor;
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
Texture2D ColorTexture;
|
||||
|
||||
SamplerState TextureSampler;
|
||||
|
||||
cbuffer SceneConstants
|
||||
{
|
||||
float4x4 ViewProjection = float4x4(1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1);
|
||||
}
|
||||
|
||||
cbuffer ObjectConstants
|
||||
{
|
||||
float4x4 Transform;
|
||||
}
|
||||
|
||||
cbuffer Constants
|
||||
{
|
||||
float4 BaseColor;
|
||||
}
|
||||
|
||||
struct VS_IN
|
||||
{
|
||||
float3 Position : POSITION;
|
||||
float4 Color : COLOR;
|
||||
float2 TexCoord : TEXCOORD;
|
||||
};
|
||||
|
||||
struct PS_IN
|
||||
{
|
||||
float4 Position : SV_POSITION;
|
||||
float4 Color : COLOR;
|
||||
float2 TexCoord : TEXCOORD;
|
||||
};
|
||||
|
||||
PS_IN VS(VS_IN input)
|
||||
{
|
||||
PS_IN output;
|
||||
|
||||
float4 worldPosition = mul(Transform, float4(input.Position, 1));
|
||||
|
||||
output.Position = mul(ViewProjection, worldPosition);
|
||||
output.Color = input.Color;
|
||||
output.TexCoord = input.TexCoord;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 PS(PS_IN input) : SV_TARGET
|
||||
{
|
||||
return input.Color * ColorTexture.Sample(TextureSampler, input.TexCoord);
|
||||
}
|
||||
+53
-23
@@ -14,10 +14,12 @@ namespace Sandbox
|
||||
{
|
||||
private OrthographicCamera _camera ~ delete _; // PerspectiveCamera
|
||||
|
||||
struct VertexColor : IVertexData
|
||||
[Ordered]
|
||||
struct VertexColorTexture : IVertexData
|
||||
{
|
||||
public Vector3 Position;
|
||||
public Color Color;
|
||||
public Vector2 TexCoord;
|
||||
|
||||
public this() => this = default;
|
||||
|
||||
@@ -25,6 +27,14 @@ namespace Sandbox
|
||||
{
|
||||
Position = pos;
|
||||
Color = color;
|
||||
TexCoord = .();
|
||||
}
|
||||
|
||||
public this(Vector3 pos, Color color, Vector2 texCoord)
|
||||
{
|
||||
Position = pos;
|
||||
Color = color;
|
||||
TexCoord = texCoord;
|
||||
}
|
||||
|
||||
//public static readonly InputElementDescription[] InputLayout ~ delete _;
|
||||
@@ -52,11 +62,14 @@ namespace Sandbox
|
||||
RasterizerState _rasterizerState ~ delete _;
|
||||
|
||||
Effect _effect ~ _?.ReleaseRef();
|
||||
Effect _textureEffect ~ _?.ReleaseRef();
|
||||
|
||||
ConstantBuffer _cBuffer ~ _?.ReleaseRef();
|
||||
|
||||
GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
Texture2D _texture ~ _?.ReleaseRef();
|
||||
|
||||
private Vector3 CircleCoord(float angle)
|
||||
{
|
||||
return .(Math.Cos(angle), Math.Sin(angle), 0);
|
||||
@@ -79,12 +92,25 @@ namespace Sandbox
|
||||
ps.ReleaseRef();
|
||||
}
|
||||
|
||||
{
|
||||
_textureEffect = new Effect();
|
||||
|
||||
let vs = Shader.FromFile!<VertexShader>(_context, "content\\textureShader.hlsl", "VS");
|
||||
_textureEffect.VertexShader = vs;
|
||||
vs.ReleaseRef();
|
||||
|
||||
let ps = Shader.FromFile!<PixelShader>(_context, "content\\textureShader.hlsl", "PS");
|
||||
_textureEffect.PixelShader = ps;
|
||||
ps.ReleaseRef();
|
||||
}
|
||||
|
||||
// Create Input Layout
|
||||
|
||||
_vertexLayout = new VertexLayout(_context, new .(
|
||||
VertexElement(.R32G32B32_Float, "POSITION"),
|
||||
VertexElement(.R8G8B8A8_UNorm, "COLOR"),
|
||||
), _effect.VertexShader);
|
||||
VertexElement(.R32G32_Float, "TEXCOORD"),
|
||||
), _textureEffect.VertexShader);
|
||||
|
||||
|
||||
_cBuffer = _effect.PixelShader.Buffers["Constants"] as ConstantBuffer;
|
||||
@@ -97,17 +123,17 @@ namespace Sandbox
|
||||
_geometryBinding.SetVertexLayout(_vertexLayout);
|
||||
|
||||
float pO3 = Math.PI_f / 3.0f;
|
||||
VertexColor[?] vertices = .(
|
||||
VertexColor(.Zero, Color(255,255,255)),
|
||||
VertexColor(CircleCoord(0), Color(255, 0, 0)),
|
||||
VertexColor(CircleCoord(pO3), Color(255,255, 0)),
|
||||
VertexColor(CircleCoord(pO3*2), Color( 0,255, 0)),
|
||||
VertexColor(CircleCoord(Math.PI_f), Color( 0,255,255)),
|
||||
VertexColor(CircleCoord(-pO3*2), Color( 0, 0,255)),
|
||||
VertexColor(CircleCoord(-pO3), Color(255, 0,255)),
|
||||
VertexColorTexture[?] vertices = .(
|
||||
VertexColorTexture(.Zero, Color(255,255,255)),
|
||||
VertexColorTexture(CircleCoord(0), Color(255, 0, 0)),
|
||||
VertexColorTexture(CircleCoord(pO3), Color(255,255, 0)),
|
||||
VertexColorTexture(CircleCoord(pO3*2), Color( 0,255, 0)),
|
||||
VertexColorTexture(CircleCoord(Math.PI_f), Color( 0,255,255)),
|
||||
VertexColorTexture(CircleCoord(-pO3*2), Color( 0, 0,255)),
|
||||
VertexColorTexture(CircleCoord(-pO3), Color(255, 0,255)),
|
||||
);
|
||||
|
||||
_vertexBuffer = new VertexBuffer(_context, typeof(VertexColor), (.)vertices.Count, .Immutable);
|
||||
_vertexBuffer = new VertexBuffer(_context, typeof(VertexColorTexture), (.)vertices.Count, .Immutable);
|
||||
_vertexBuffer.SetData(vertices);
|
||||
_geometryBinding.SetVertexBufferSlot(_vertexBuffer, 0);
|
||||
|
||||
@@ -130,14 +156,14 @@ namespace Sandbox
|
||||
_quadGeometryBinding.SetPrimitiveTopology(.TriangleList);
|
||||
_quadGeometryBinding.SetVertexLayout(_vertexLayout);
|
||||
|
||||
VertexColor[?] vertices = .(
|
||||
VertexColor(Vector3(-0.75f, 0.75f, 0), Color.White),
|
||||
VertexColor(Vector3(-0.75f, -0.75f, 0), Color.White),
|
||||
VertexColor(Vector3(0.75f, -0.75f, 0), Color.White),
|
||||
VertexColor(Vector3(0.75f, 0.75f, 0), Color.White),
|
||||
VertexColorTexture[?] vertices = .(
|
||||
VertexColorTexture(Vector3(-0.75f, 0.75f, 0), Color.White, .(0, 0)),
|
||||
VertexColorTexture(Vector3(-0.75f, -0.75f, 0), Color.White, .(0, 1)),
|
||||
VertexColorTexture(Vector3(0.75f, -0.75f, 0), Color.White, .(1, 1)),
|
||||
VertexColorTexture(Vector3(0.75f, 0.75f, 0), Color.White, .(1, 0)),
|
||||
);
|
||||
|
||||
_quadVertexBuffer = new VertexBuffer(_context, typeof(VertexColor), (.)vertices.Count, .Immutable);
|
||||
_quadVertexBuffer = new VertexBuffer(_context, typeof(VertexColorTexture), (.)vertices.Count, .Immutable);
|
||||
_quadVertexBuffer.SetData(vertices);
|
||||
_quadGeometryBinding.SetVertexBufferSlot(_quadVertexBuffer, 0);
|
||||
|
||||
@@ -165,6 +191,8 @@ namespace Sandbox
|
||||
_camera.FovY = Math.PI_f / 4;
|
||||
_camera.Position = .(0, -1, -5);
|
||||
*/
|
||||
|
||||
_texture = new Texture2D(_context, "content/Textures/Checkerboard.dds");
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
@@ -216,12 +244,6 @@ namespace Sandbox
|
||||
|
||||
Renderer.BeginScene(_camera);
|
||||
|
||||
_cBuffer["BaseColor"].SetData(ColorRGBA.White);
|
||||
_cBuffer.Update();
|
||||
|
||||
Renderer.Submit(_geometryBinding, _effect);
|
||||
Renderer.Submit(_quadGeometryBinding, _effect, .Translation(2, 0, 0));
|
||||
|
||||
for(int x < 20)
|
||||
for(int y < 20)
|
||||
{
|
||||
@@ -236,6 +258,14 @@ namespace Sandbox
|
||||
Renderer.Submit(_quadGeometryBinding, _effect, transform);
|
||||
}
|
||||
|
||||
_cBuffer["BaseColor"].SetData(ColorRGBA.White);
|
||||
_cBuffer.Update();
|
||||
|
||||
_texture.Bind();
|
||||
|
||||
Renderer.Submit(_geometryBinding, _effect);
|
||||
Renderer.Submit(_quadGeometryBinding, _textureEffect, .Scaling(1.5f));
|
||||
|
||||
Renderer.EndScene();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user