mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Use static D3D11 device and context
This commit is contained in:
@@ -43,11 +43,11 @@ namespace GlitchyEngine
|
||||
_rendererApi = new RendererAPI();
|
||||
_rendererApi.Context = _window.Context;
|
||||
|
||||
SamplerStateManager.Init(_window.Context);
|
||||
SamplerStateManager.Init();
|
||||
|
||||
RenderCommand.RendererAPI = _rendererApi;
|
||||
|
||||
_effectLibrary = new EffectLibrary(_window.Context);
|
||||
_effectLibrary = new EffectLibrary();
|
||||
|
||||
Renderer.Init(_window.Context, _effectLibrary);
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace GlitchyEngine.Content
|
||||
{
|
||||
static readonly Matrix RightToLeftHand = .Scaling(1, 1, -1);
|
||||
|
||||
public static void LoadModel(String filename, GraphicsContext context, Effect validationEffect, Material material, EcsWorld world,
|
||||
public static void LoadModel(String filename, Effect validationEffect, Material material, EcsWorld world,
|
||||
List<(Matrix Transform, GeometryBinding Model)> output, out Skeleton skeleton, out List<AnimationClip> clips)
|
||||
{
|
||||
skeleton = null;
|
||||
@@ -30,7 +30,7 @@ namespace GlitchyEngine.Content
|
||||
|
||||
for(var node in data.Scenes[0].Nodes)
|
||||
{
|
||||
NodesToEntities(data, node, null, world, context, validationEffect, material);
|
||||
NodesToEntities(data, node, null, world, validationEffect, material);
|
||||
}
|
||||
|
||||
clips = removeMe___Clips;
|
||||
@@ -47,7 +47,7 @@ namespace GlitchyEngine.Content
|
||||
|
||||
for(var primitive in node.Mesh.Primitives)
|
||||
{
|
||||
GeometryBinding binding = ModelLoader.PrimitiveToGeoBinding(context, primitive, validationEffect);
|
||||
GeometryBinding binding = ModelLoader.PrimitiveToGeoBinding(primitive, validationEffect);
|
||||
|
||||
output.Add((transform, binding));
|
||||
}
|
||||
@@ -67,7 +67,7 @@ namespace GlitchyEngine.Content
|
||||
|
||||
static List<AnimationClip> removeMe___Clips;
|
||||
|
||||
private static void NodesToEntities(CGLTF.Data* data, CGLTF.Node* node, Entity? parentEntity, EcsWorld world, GraphicsContext context, Effect validationEffect, Material material)
|
||||
private static void NodesToEntities(CGLTF.Data* data, CGLTF.Node* node, Entity? parentEntity, EcsWorld world, Effect validationEffect, Material material)
|
||||
{
|
||||
Entity entity = world.NewEntity();
|
||||
|
||||
@@ -126,7 +126,7 @@ namespace GlitchyEngine.Content
|
||||
if(node.Mesh.Primitives.Length == 1)
|
||||
{
|
||||
var mesh = world.AssignComponent<MeshComponent>(entity);
|
||||
mesh.Mesh = ModelLoader.PrimitiveToGeoBinding(context, node.Mesh.Primitives[0], validationEffect);
|
||||
mesh.Mesh = ModelLoader.PrimitiveToGeoBinding(node.Mesh.Primitives[0], validationEffect);
|
||||
|
||||
if(skeleton == null)
|
||||
{
|
||||
@@ -151,7 +151,7 @@ namespace GlitchyEngine.Content
|
||||
meshParent.Entity = entity;
|
||||
|
||||
var mesh = world.AssignComponent<MeshComponent>(meshEntity);
|
||||
mesh.Mesh = ModelLoader.PrimitiveToGeoBinding(context, node.Mesh.Primitives[0], validationEffect);
|
||||
mesh.Mesh = ModelLoader.PrimitiveToGeoBinding(node.Mesh.Primitives[0], validationEffect);
|
||||
|
||||
if(skeleton == null)
|
||||
{
|
||||
@@ -170,13 +170,13 @@ namespace GlitchyEngine.Content
|
||||
|
||||
for(var child in node.Children)
|
||||
{
|
||||
NodesToEntities(data, child, entity, world, context, validationEffect, material);
|
||||
NodesToEntities(data, child, entity, world, validationEffect, material);
|
||||
}
|
||||
}
|
||||
|
||||
public static GeometryBinding PrimitiveToGeoBinding(GraphicsContext context, CGLTF.Primitive primitive, Effect validationEffect)
|
||||
public static GeometryBinding PrimitiveToGeoBinding(CGLTF.Primitive primitive, Effect validationEffect)
|
||||
{
|
||||
GeometryBinding binding = new GeometryBinding(context);
|
||||
GeometryBinding binding = new GeometryBinding();
|
||||
|
||||
// primitive topology
|
||||
switch(primitive.Type)
|
||||
@@ -203,7 +203,7 @@ namespace GlitchyEngine.Content
|
||||
{
|
||||
bool is16bit = indices.ComponentType == .R_16u || indices.ComponentType == .R_16;
|
||||
|
||||
IndexBuffer ib = new IndexBuffer(context, (.)indices.Count, .Immutable, .None, is16bit ? .Index16Bit : .Index32Bit);
|
||||
IndexBuffer ib = new IndexBuffer((.)indices.Count, .Immutable, .None, is16bit ? .Index16Bit : .Index32Bit);
|
||||
|
||||
uint8* bufferData = (uint8*)indices.BufferView.Buffer.Data;
|
||||
bufferData += indices.BufferView.Offset;
|
||||
@@ -255,7 +255,7 @@ namespace GlitchyEngine.Content
|
||||
// if buffer doesn't exist -> create
|
||||
if(!buffers.TryGetValue(bufferView, out vertexBuffer))
|
||||
{
|
||||
vertexBuffer = new VertexBuffer(context, 1, (uint32)bufferView.Size, .Immutable)..ReleaseRefNoDelete();
|
||||
vertexBuffer = new VertexBuffer(1, (uint32)bufferView.Size, .Immutable)..ReleaseRefNoDelete();
|
||||
|
||||
uint8* bufferData = (uint8*)bufferView.Buffer.Data;
|
||||
bufferData += bufferView.Offset;
|
||||
@@ -318,7 +318,7 @@ namespace GlitchyEngine.Content
|
||||
else
|
||||
GenerateNormals(positions, normals);
|
||||
|
||||
VertexBuffer vertexBuffer = new VertexBuffer(context, (uint32)sizeof(Vector3), (uint32)normals.Count, .Immutable);
|
||||
VertexBuffer vertexBuffer = new VertexBuffer((uint32)sizeof(Vector3), (uint32)normals.Count, .Immutable);
|
||||
vertexBuffer.SetData<Vector3>(normals);
|
||||
|
||||
bindings.Add(vertexBuffer.Binding);
|
||||
@@ -338,7 +338,7 @@ namespace GlitchyEngine.Content
|
||||
vertexElements[i] = elements[i];
|
||||
}
|
||||
|
||||
VertexLayout layout = new VertexLayout(context, vertexElements, true, validationEffect.VertexShader);
|
||||
VertexLayout layout = new VertexLayout(vertexElements, true, validationEffect.VertexShader);
|
||||
binding.SetVertexLayout(layout..ReleaseRefNoDelete());
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,13 @@ using ImGuizmo;
|
||||
|
||||
using internal ImGui;
|
||||
|
||||
#if GE_D3D11
|
||||
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
#endif
|
||||
|
||||
namespace GlitchyEngine.ImGui
|
||||
{
|
||||
public class ImGuiLayer : Layer
|
||||
@@ -40,10 +47,10 @@ namespace GlitchyEngine.ImGui
|
||||
#if GE_WINDOWS
|
||||
// Todo: temporary, needs to be platform independent
|
||||
ImGuiImplWin32.Init((void*)(uint)(Windows.HWnd)(int)Application.Get().Window.NativeWindow);
|
||||
#endif
|
||||
|
||||
var context = Application.Get().Window.Context;
|
||||
|
||||
ImGuiImplDX11.Init(context.[Friend]nativeDevice, context.[Friend]nativeContext);
|
||||
#if GE_D3D11
|
||||
ImGuiImplDX11.Init(NativeDevice, NativeContext);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
using DirectX.Common;
|
||||
using DirectX.D3D11;
|
||||
using DirectX.D3D11.SDKLayers;
|
||||
|
||||
namespace GlitchyEngine.Platform.DX11
|
||||
{
|
||||
static
|
||||
{
|
||||
/// Gets whether or not the DX11 device is initialized.
|
||||
protected internal static bool IsDx11Initialized => NativeDevice != null;
|
||||
protected internal static ID3D11Device* NativeDevice;
|
||||
protected internal static ID3D11DeviceContext* NativeContext;
|
||||
|
||||
protected internal static ID3D11Debug* DebugDevice;
|
||||
|
||||
internal static void Dx11Init()
|
||||
{
|
||||
if(!IsDx11Initialized)
|
||||
{
|
||||
Log.EngineLogger.Trace("Creating D3D11 Device and Context...");
|
||||
|
||||
DeviceCreationFlags deviceFlags = .None;
|
||||
#if DEBUG
|
||||
deviceFlags |= .Debug;
|
||||
#endif
|
||||
|
||||
FeatureLevel[] levels = scope .(.Level_11_0);
|
||||
|
||||
FeatureLevel deviceLevel = ?;
|
||||
var deviceResult = D3D11.CreateDevice(null, .Hardware, 0, deviceFlags, levels, &NativeDevice, &deviceLevel, &NativeContext);
|
||||
Log.EngineLogger.Assert(deviceResult.Succeeded, scope $"Failed to create D3D11 Device. Message({(int32)deviceResult}): {deviceResult}");
|
||||
|
||||
#if DEBUG
|
||||
if(NativeDevice.QueryInterface<ID3D11Debug>(out DebugDevice).Succeeded)
|
||||
{
|
||||
ID3D11InfoQueue* infoQueue;
|
||||
if(NativeDevice.QueryInterface<ID3D11InfoQueue>(out infoQueue).Succeeded)
|
||||
{
|
||||
infoQueue.SetBreakOnSeverity(.Corruption, true);
|
||||
infoQueue.SetBreakOnSeverity(.Error, true);
|
||||
|
||||
infoQueue.Release();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Log.EngineLogger.Trace($"D3D11 Device and Context created (Feature level: {deviceLevel})");
|
||||
}
|
||||
else
|
||||
{
|
||||
// We created a second GraphicsContext (for some reason?) just increment references.
|
||||
NativeDevice.AddRef();
|
||||
NativeContext.AddRef();
|
||||
DebugDevice.AddRef();
|
||||
}
|
||||
}
|
||||
|
||||
internal static void Dx11Release()
|
||||
{
|
||||
NativeDevice.Release();
|
||||
NativeContext.Release();
|
||||
DebugDevice.ReportLiveDeviceObjects(.Detail);
|
||||
DebugDevice.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using GlitchyEngine.Renderer;
|
||||
using System.Collections;
|
||||
using DirectX.D3D11;
|
||||
@@ -37,3 +39,5 @@ namespace ImGui
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#if GE_D3D11
|
||||
|
||||
// Disable warning when taking pointer of in-Parameter
|
||||
#pragma warning disable 4204
|
||||
|
||||
@@ -32,3 +34,5 @@ namespace GlitchyEngine.Math
|
||||
public static implicit operator Self(in DirectX.Math.Vector4 value) => *(Self*)&value;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using DirectX.D3D11;
|
||||
using DirectX.Common;
|
||||
using GlitchyEngine.Math;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
extension BlendState
|
||||
@@ -12,7 +17,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public override void Bind(ColorRGBA blendFactor)
|
||||
{
|
||||
_context.nativeContext.OutputMerger.SetBlendState(nativeBlendState, blendFactor);
|
||||
NativeContext.OutputMerger.SetBlendState(nativeBlendState, blendFactor);
|
||||
}
|
||||
|
||||
protected override void PlatformCreateBlendState()
|
||||
@@ -33,9 +38,11 @@ namespace GlitchyEngine.Renderer
|
||||
nativeDesc.RenderTarget[i].RenderTargetWriteMask = _desc.RenderTarget[i].RenderTargetWriteMask;
|
||||
}
|
||||
|
||||
HResult result = _context.nativeDevice.CreateBlendState(ref nativeDesc, &nativeBlendState);
|
||||
HResult result = NativeDevice.CreateBlendState(ref nativeDesc, &nativeBlendState);
|
||||
|
||||
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create blend state. Error({(int)result}): {result}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using System.Diagnostics;
|
||||
using System;
|
||||
using DirectX.D3D11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
extension BufferDescription
|
||||
@@ -67,7 +72,7 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
|
||||
SubresourceData srData = .(byteData, byteLength, 0);
|
||||
var result = _context.nativeDevice.CreateBuffer(ref nativeDescription, &srData, &nativeBuffer);
|
||||
var result = NativeDevice.CreateBuffer(ref nativeDescription, &srData, &nativeBuffer);
|
||||
if(result.Failed)
|
||||
{
|
||||
Log.EngineLogger.Error($"Failed to create buffer. Message({(int)result}):{result}");
|
||||
@@ -91,14 +96,14 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
case .Default:
|
||||
Box dataBox = .(dstByteOffset, 0, 0, dstByteOffset + byteLength, 1, 1);
|
||||
_context.nativeContext.UpdateSubresource(nativeBuffer, 0, &dataBox, data, byteLength, byteLength);
|
||||
NativeContext.UpdateSubresource(nativeBuffer, 0, &dataBox, data, byteLength, byteLength);
|
||||
case .Dynamic:
|
||||
Debug.Assert(mapType.CanWrite, "The map type has to have write access.");
|
||||
// Todo: DoNotWaitFlag
|
||||
MappedSubresource map = ?;
|
||||
_context.nativeContext.Map(nativeBuffer, 0, (.)mapType, .None, &map);
|
||||
NativeContext.Map(nativeBuffer, 0, (.)mapType, .None, &map);
|
||||
Internal.MemCpy(((uint8*)map.Data) + dstByteOffset, data, byteLength);
|
||||
_context.nativeContext.Unmap(nativeBuffer, 0);
|
||||
NativeContext.Unmap(nativeBuffer, 0);
|
||||
case .Immutable:
|
||||
Log.EngineLogger.Error("Can't set the data of an immutable resource.");
|
||||
return .Err;
|
||||
@@ -111,3 +116,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using DirectX.D3D11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
@@ -17,3 +19,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#if GE_D3D11
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
extension CPUAccessFlags
|
||||
@@ -16,3 +18,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using DirectX.Common;
|
||||
using DirectX.D3D11Shader;
|
||||
using System;
|
||||
@@ -47,7 +49,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
extension ConstantBuffer
|
||||
{
|
||||
internal this(GraphicsContext context, ID3D11ShaderReflectionConstantBuffer* bufferReflection) : base(context)
|
||||
internal this(ID3D11ShaderReflectionConstantBuffer* bufferReflection)
|
||||
{
|
||||
Reflect(bufferReflection);
|
||||
|
||||
@@ -80,3 +82,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using DirectX.D3D11;
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
extension DepthStencilFormat
|
||||
@@ -40,21 +45,13 @@ namespace GlitchyEngine.Renderer
|
||||
desc.SampleDesc = .(1, 0);
|
||||
|
||||
ID3D11Texture2D* tex = ?;
|
||||
_context.nativeDevice.CreateTexture2D(ref desc, null, &tex);
|
||||
NativeDevice.CreateTexture2D(ref desc, null, &tex);
|
||||
|
||||
_context.nativeDevice.CreateDepthStencilView(tex, null, &nativeView);
|
||||
NativeDevice.CreateDepthStencilView(tex, null, &nativeView);
|
||||
|
||||
tex.Release();
|
||||
}
|
||||
|
||||
public override void Bind()
|
||||
{
|
||||
_context.SetDepthStencilTarget(this);
|
||||
}
|
||||
|
||||
public override void Clear(float depthValue, uint8 stencilValue, DepthStencilClearFlag clearFlags)
|
||||
{
|
||||
_context.nativeContext.ClearDepthStencilView(nativeView, (.)clearFlags, depthValue, stencilValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using System;
|
||||
using DirectX;
|
||||
using DirectX.D3D11;
|
||||
@@ -11,8 +13,8 @@ namespace GlitchyEngine.Renderer
|
||||
protected override void Compile(String vsPath, String vsEntry, String psPath, String psEntry)
|
||||
{
|
||||
// Todo: macros
|
||||
VertexShader = new VertexShader(_context, vsPath, vsEntry);
|
||||
PixelShader = new PixelShader(_context, psPath, psEntry);
|
||||
VertexShader = new VertexShader(vsPath, vsEntry);
|
||||
PixelShader = new PixelShader(psPath, psEntry);
|
||||
|
||||
Reflect();
|
||||
}
|
||||
@@ -23,3 +25,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using System;
|
||||
using DirectX.D3D11;
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
extension GeometryBinding
|
||||
{
|
||||
internal ID3D11Buffer*[DirectX.D3D11.D3D11_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT] nativeBuffers;
|
||||
@@ -73,33 +77,29 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
}
|
||||
|
||||
public override void Bind(GraphicsContext context = null)
|
||||
public override void Bind()
|
||||
{
|
||||
var context;
|
||||
context ??= _context;
|
||||
|
||||
context.nativeContext.InputAssembler.SetVertexBuffers(0, nativeBuffers.Count, &nativeBuffers, &bufferStrides, &bufferOffsets);
|
||||
context.nativeContext.InputAssembler.SetInputLayout(_vertexLayout.nativeLayout);
|
||||
context.nativeContext.InputAssembler.SetPrimitiveTopology((.)_primitiveTopology);
|
||||
NativeContext.InputAssembler.SetVertexBuffers(0, nativeBuffers.Count, &nativeBuffers, &bufferStrides, &bufferOffsets);
|
||||
NativeContext.InputAssembler.SetInputLayout(_vertexLayout.nativeLayout);
|
||||
NativeContext.InputAssembler.SetPrimitiveTopology((.)_primitiveTopology);
|
||||
|
||||
if(_indexBuffer != null)
|
||||
context.nativeContext.InputAssembler.SetIndexBuffer(_indexBuffer.nativeBuffer, _indexBuffer.Format == .Index32Bit ? .R32_UInt : .R16_UInt, _indexByteOffset);
|
||||
NativeContext.InputAssembler.SetIndexBuffer(_indexBuffer.nativeBuffer, _indexBuffer.Format == .Index32Bit ? .R32_UInt : .R16_UInt, _indexByteOffset);
|
||||
}
|
||||
|
||||
public void Unbind(GraphicsContext context = null)
|
||||
public override void Unbind()
|
||||
{
|
||||
var context;
|
||||
context ??= _context;
|
||||
|
||||
ID3D11Buffer*[nativeBuffers.Count] nullBuffers = .();
|
||||
uint32[nativeBuffers.Count] zeroStrides = .();
|
||||
uint32[nativeBuffers.Count] zeroOffsets = .();
|
||||
|
||||
context.nativeContext.InputAssembler.SetVertexBuffers(0, nativeBuffers.Count, &nullBuffers, &zeroStrides, &zeroOffsets);
|
||||
context.nativeContext.InputAssembler.SetInputLayout(null);
|
||||
context.nativeContext.InputAssembler.SetPrimitiveTopology(.Undefined);
|
||||
NativeContext.InputAssembler.SetVertexBuffers(0, nativeBuffers.Count, &nullBuffers, &zeroStrides, &zeroOffsets);
|
||||
NativeContext.InputAssembler.SetInputLayout(null);
|
||||
NativeContext.InputAssembler.SetPrimitiveTopology(.Undefined);
|
||||
|
||||
context.nativeContext.InputAssembler.SetIndexBuffer(null, .R16_UNorm, 0);
|
||||
NativeContext.InputAssembler.SetIndexBuffer(null, .R16_UNorm, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using DirectX.Common;
|
||||
@@ -7,9 +9,11 @@ using DirectX.DXGI.DXGI1_2;
|
||||
using GlitchyEngine.Renderer;
|
||||
using GlitchyEngine.Math;
|
||||
using DirectX.D3D11.DeviceContextStages;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
/// DirectX 11 specific implementation of the GraphicsContext
|
||||
@@ -19,10 +23,12 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
internal Windows.HWnd nativeWindowHandle;
|
||||
|
||||
internal ID3D11Device* nativeDevice;
|
||||
internal ID3D11DeviceContext* nativeContext;
|
||||
|
||||
private ID3D11Debug* _debugDevice;
|
||||
/// Gets whether or not the DX11 device is initialized.
|
||||
//internal bool IsDx11Initialized => nativeDevice != null;
|
||||
//internal ID3D11Device* nativeDevice => NativeDevice;
|
||||
//internal ID3D11DeviceContext* nativeContext => NativeContext;
|
||||
|
||||
//internal ID3D11Debug* debugDevice => DebugDevice;
|
||||
|
||||
public override SwapChain SwapChain => _swapChain;
|
||||
|
||||
@@ -41,15 +47,12 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
delete _swapChain;
|
||||
|
||||
nativeDevice?.Release();
|
||||
nativeContext?.Release();
|
||||
_debugDevice.ReportLiveDeviceObjects(.Detail);
|
||||
_debugDevice?.Release();
|
||||
Dx11Release();
|
||||
}
|
||||
|
||||
public override void Init()
|
||||
{
|
||||
InitDevice();
|
||||
Dx11Init();
|
||||
|
||||
SwapChain.Init();
|
||||
}
|
||||
@@ -57,47 +60,54 @@ namespace GlitchyEngine.Renderer
|
||||
/**
|
||||
* Initializes the Device and ImmediateContext.
|
||||
*/
|
||||
/*
|
||||
void InitDevice()
|
||||
{
|
||||
nativeDevice?.Release();
|
||||
nativeContext?.Release();
|
||||
|
||||
Log.EngineLogger.Trace("Creating D3D11 Device and Context...");
|
||||
|
||||
DeviceCreationFlags deviceFlags = .None;
|
||||
if(!IsDx11Initialized)
|
||||
{
|
||||
Log.EngineLogger.Trace("Creating D3D11 Device and Context...");
|
||||
|
||||
DeviceCreationFlags deviceFlags = .None;
|
||||
#if DEBUG
|
||||
deviceFlags |= .Debug;
|
||||
deviceFlags |= .Debug;
|
||||
#endif
|
||||
|
||||
FeatureLevel[] levels = scope .(.Level_11_0);
|
||||
FeatureLevel[] levels = scope .(.Level_11_0);
|
||||
|
||||
FeatureLevel deviceLevel = ?;
|
||||
var deviceResult = D3D11.CreateDevice(null, .Hardware, 0, deviceFlags, levels, &nativeDevice, &deviceLevel, &nativeContext);
|
||||
Debug.Assert(deviceResult.Succeeded, scope $"Failed to create D3D11 Device. Message(0x{(int32)deviceResult}): {deviceResult}");
|
||||
FeatureLevel deviceLevel = ?;
|
||||
var deviceResult = D3D11.CreateDevice(null, .Hardware, 0, deviceFlags, levels, &nativeDevice, &deviceLevel, &nativeContext);
|
||||
Log.EngineLogger.Assert(deviceResult.Succeeded, scope $"Failed to create D3D11 Device. Message({(int32)deviceResult}): {deviceResult}");
|
||||
|
||||
#if DEBUG
|
||||
if(nativeDevice.QueryInterface<ID3D11Debug>(out _debugDevice).Succeeded)
|
||||
{
|
||||
ID3D11InfoQueue* infoQueue;
|
||||
if(nativeDevice.QueryInterface<ID3D11InfoQueue>(out infoQueue).Succeeded)
|
||||
if(nativeDevice.QueryInterface<ID3D11Debug>(out debugDevice).Succeeded)
|
||||
{
|
||||
infoQueue.SetBreakOnSeverity(.Corruption, true);
|
||||
infoQueue.SetBreakOnSeverity(.Error, true);
|
||||
ID3D11InfoQueue* infoQueue;
|
||||
if(nativeDevice.QueryInterface<ID3D11InfoQueue>(out infoQueue).Succeeded)
|
||||
{
|
||||
infoQueue.SetBreakOnSeverity(.Corruption, true);
|
||||
infoQueue.SetBreakOnSeverity(.Error, true);
|
||||
|
||||
infoQueue.Release();
|
||||
infoQueue.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Log.EngineLogger.Trace($"D3D11 Device and Context created (Feature level: {deviceLevel})");
|
||||
Log.EngineLogger.Trace($"D3D11 Device and Context created (Feature level: {deviceLevel})");
|
||||
}
|
||||
else
|
||||
{
|
||||
// We created a second GraphicsContext (for some reason?) just increment references.
|
||||
nativeDevice.AddRef();
|
||||
nativeContext.AddRef();
|
||||
debugDevice.AddRef();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
private ID3D11DepthStencilView* _depthStencilTarget;
|
||||
private ID3D11RenderTargetView*[MaxRTVCount] _renderTargets;
|
||||
|
||||
internal void SetDepthStencilTarget(DepthStencilTarget target)
|
||||
public override void SetDepthStencilTarget(DepthStencilTarget target)
|
||||
{
|
||||
_depthStencilTarget = target?.nativeView;
|
||||
}
|
||||
@@ -121,18 +131,18 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public override void BindRenderTargets()
|
||||
{
|
||||
nativeContext.OutputMerger.SetRenderTargets(MaxRTVCount, &_renderTargets, _depthStencilTarget);
|
||||
NativeContext.OutputMerger.SetRenderTargets(MaxRTVCount, &_renderTargets, _depthStencilTarget);
|
||||
}
|
||||
|
||||
public override void ClearRenderTarget(RenderTarget2D renderTarget, ColorRGBA color)
|
||||
{
|
||||
if(renderTarget == null)
|
||||
{
|
||||
nativeContext.ClearRenderTargetView(_swapChain.nativeBackBufferTarget, color);
|
||||
NativeContext.ClearRenderTargetView(_swapChain.nativeBackBufferTarget, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
nativeContext.ClearRenderTargetView(renderTarget._nativeRenderTargetView, color);
|
||||
NativeContext.ClearRenderTargetView(renderTarget._nativeRenderTargetView, color);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,42 +150,42 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
// make stride and offset mutable so that we can take their pointers.
|
||||
var stride, offset;
|
||||
nativeContext.InputAssembler.SetVertexBuffers(slot, 1, &buffer.nativeBuffer, &stride, &offset);
|
||||
NativeContext.InputAssembler.SetVertexBuffers(slot, 1, &buffer.nativeBuffer, &stride, &offset);
|
||||
}
|
||||
|
||||
public override void Draw(uint32 vertexCount, uint32 startVertexIndex = 0)
|
||||
{
|
||||
nativeContext.Draw(vertexCount, startVertexIndex);
|
||||
NativeContext.Draw(vertexCount, startVertexIndex);
|
||||
}
|
||||
|
||||
public override void DrawIndexed(uint32 indexCount, uint32 startIndexLocation = 0, int32 vertexOffset = 0)
|
||||
{
|
||||
nativeContext.DrawIndexed(indexCount, startIndexLocation, vertexOffset);
|
||||
NativeContext.DrawIndexed(indexCount, startIndexLocation, vertexOffset);
|
||||
}
|
||||
|
||||
public override void SetIndexBuffer(Buffer buffer, IndexFormat indexFormat = .Index16Bit, uint32 byteOffset = 0)
|
||||
{
|
||||
nativeContext.InputAssembler.SetIndexBuffer(buffer.nativeBuffer, indexFormat == .Index32Bit ? .R32_UInt : .R16_UInt, byteOffset);
|
||||
NativeContext.InputAssembler.SetIndexBuffer(buffer.nativeBuffer, indexFormat == .Index32Bit ? .R32_UInt : .R16_UInt, byteOffset);
|
||||
}
|
||||
|
||||
protected override void SetRasterizerStateImpl()
|
||||
{
|
||||
nativeContext.Rasterizer.SetState(_currentRasterizerState.nativeRasterizerState);
|
||||
NativeContext.Rasterizer.SetState(_currentRasterizerState.nativeRasterizerState);
|
||||
}
|
||||
|
||||
public override void SetViewports(uint32 viewportsCount, GlitchyEngine.Renderer.Viewport* viewports)
|
||||
{
|
||||
nativeContext.Rasterizer.SetViewports(viewportsCount, (.)viewports);
|
||||
NativeContext.Rasterizer.SetViewports(viewportsCount, (.)viewports);
|
||||
}
|
||||
|
||||
public override void SetVertexLayout(VertexLayout vertexLayout)
|
||||
{
|
||||
nativeContext.InputAssembler.SetInputLayout(vertexLayout.nativeLayout);
|
||||
NativeContext.InputAssembler.SetInputLayout(vertexLayout.nativeLayout);
|
||||
}
|
||||
|
||||
public override void SetPrimitiveTopology(GlitchyEngine.Renderer.PrimitiveTopology primitiveTopology)
|
||||
{
|
||||
nativeContext.InputAssembler.SetPrimitiveTopology((DirectX.Common.PrimitiveTopology)primitiveTopology);
|
||||
NativeContext.InputAssembler.SetPrimitiveTopology((DirectX.Common.PrimitiveTopology)primitiveTopology);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -208,11 +218,11 @@ namespace GlitchyEngine.Renderer
|
||||
// TODO: Add remaining shader stages
|
||||
case typeof(PixelShader):
|
||||
// TODO: bind uavs
|
||||
nativeContext.PixelShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]);
|
||||
nativeContext.PixelShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]);
|
||||
NativeContext.PixelShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]);
|
||||
NativeContext.PixelShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]);
|
||||
case typeof(VertexShader):
|
||||
nativeContext.VertexShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]);
|
||||
nativeContext.VertexShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]);
|
||||
NativeContext.VertexShader.SetShaderResources(_firstTexture, _textureCount, &_textures[_firstTexture]);
|
||||
NativeContext.VertexShader.SetSamplers(_firstTexture, _textureCount, &_samplers[_firstTexture]);
|
||||
default:
|
||||
Runtime.FatalError(scope $"Shader stage \"{typeof(TShader)}\" not implemented.");
|
||||
}
|
||||
@@ -224,11 +234,11 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
// TODO: Add remaining shader stages
|
||||
case typeof(PixelShader):
|
||||
nativeContext.PixelShader.SetConstantBuffers(0, shader.Buffers.nativeBuffers.Count, &shader.Buffers.nativeBuffers);
|
||||
[IgnoreErrors]{ nativeContext.PixelShader.SetShader(((PixelShader)shader).nativeShader); }
|
||||
NativeContext.PixelShader.SetConstantBuffers(0, shader.Buffers.nativeBuffers.Count, &shader.Buffers.nativeBuffers);
|
||||
[IgnoreErrors]{ NativeContext.PixelShader.SetShader(((PixelShader)shader).nativeShader); }
|
||||
case typeof(VertexShader):
|
||||
nativeContext.VertexShader.SetConstantBuffers(0, shader.Buffers.nativeBuffers.Count, &shader.Buffers.nativeBuffers);
|
||||
[IgnoreErrors]{ nativeContext.VertexShader.SetShader(((VertexShader)shader).nativeShader); }
|
||||
NativeContext.VertexShader.SetConstantBuffers(0, shader.Buffers.nativeBuffers.Count, &shader.Buffers.nativeBuffers);
|
||||
[IgnoreErrors]{ NativeContext.VertexShader.SetShader(((VertexShader)shader).nativeShader); }
|
||||
default:
|
||||
Runtime.FatalError(scope $"Shader stage \"{typeof(TShader)}\" not implemented.");
|
||||
}
|
||||
@@ -245,3 +255,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using System;
|
||||
using DirectX.D3D11;
|
||||
using DirectX.D3DCompiler;
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
@@ -14,7 +18,7 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
Shader.PlattformCompileShaderFromSource(code, macros, entryPoint, "ps_5_0", DefaultCompileFlags, out nativeCode);
|
||||
|
||||
var result = _context.nativeDevice.CreatePixelShader(nativeCode.GetBufferPointer(), nativeCode.GetBufferSize(), null, &nativeShader);
|
||||
var result = NativeDevice.CreatePixelShader(nativeCode.GetBufferPointer(), nativeCode.GetBufferSize(), null, &nativeShader);
|
||||
if(result.Failed)
|
||||
{
|
||||
Log.EngineLogger.Error($"Failed to create pixel shader: Message ({(int)result}): {result}");
|
||||
@@ -24,3 +28,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using DirectX.D3D11;
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
@@ -48,12 +52,12 @@ namespace GlitchyEngine.Renderer
|
||||
internal DirectX.D3D11.RasterizerStateDescription nativeDescription;
|
||||
internal ID3D11RasterizerState* nativeRasterizerState ~ _?.Release();
|
||||
|
||||
public override this(GraphicsContext context, GlitchyEngine.Renderer.RasterizerStateDescription description) : this(context)
|
||||
public override this(GlitchyEngine.Renderer.RasterizerStateDescription description)
|
||||
{
|
||||
_description = description;
|
||||
nativeDescription = (.)_description;
|
||||
|
||||
var result = context.nativeDevice.CreateRasterizerState(ref nativeDescription, &nativeRasterizerState);
|
||||
var result = NativeDevice.CreateRasterizerState(ref nativeDescription, &nativeRasterizerState);
|
||||
if(result.Failed)
|
||||
{
|
||||
Log.EngineLogger.Error($"Failed to create rasterizer state. Message({(int)result}): {result}");
|
||||
@@ -61,3 +65,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using DirectX.Common;
|
||||
using DirectX.D3D11;
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
@@ -38,7 +43,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
if(_description.DepthStencilFormat != .None)
|
||||
{
|
||||
_depthStenilTarget = new DepthStencilTarget(_context, _description.Width, _description.Height, _description.DepthStencilFormat);
|
||||
_depthStenilTarget = new DepthStencilTarget(_description.Width, _description.Height, _description.DepthStencilFormat);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +66,7 @@ namespace GlitchyEngine.Renderer
|
||||
Usage = .Default
|
||||
};
|
||||
|
||||
var result = _context.nativeDevice.CreateTexture2D(ref desc, null, &_nativeTexture);
|
||||
var result = NativeDevice.CreateTexture2D(ref desc, null, &_nativeTexture);
|
||||
Log.EngineLogger.Assert(result.Succeeded, "Failed to create RenderTarget2D");
|
||||
|
||||
CreateViews();
|
||||
@@ -69,11 +74,13 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
private void CreateViews()
|
||||
{
|
||||
var result = _context.nativeDevice.CreateShaderResourceView(_nativeTexture, null, &_nativeResourceView);
|
||||
var result = NativeDevice.CreateShaderResourceView(_nativeTexture, null, &_nativeResourceView);
|
||||
Log.EngineLogger.Assert(result.Succeeded, "Failed to create resource view");
|
||||
|
||||
result = _context.nativeDevice.CreateRenderTargetView(_nativeTexture, null, &_nativeRenderTargetView);
|
||||
result = NativeDevice.CreateRenderTargetView(_nativeTexture, null, &_nativeRenderTargetView);
|
||||
Log.EngineLogger.Assert(result.Succeeded, "Failed to create render target view");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using GlitchyEngine.Math;
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
@@ -8,12 +12,12 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
extension RendererAPI
|
||||
{
|
||||
private GraphicsContext _context;
|
||||
private GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
public GraphicsContext Context
|
||||
{
|
||||
get => _context;
|
||||
set => _context = value;
|
||||
set => SetReference!(_context, value);
|
||||
}
|
||||
|
||||
public static override API Api => .D3D11;
|
||||
@@ -30,12 +34,12 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public override void Clear(DepthStencilTarget target, float depthValue, uint8 stencilValue, DepthStencilClearFlag clearFlags)
|
||||
{
|
||||
_context.nativeContext.ClearDepthStencilView(target.nativeView, (.)clearFlags, depthValue, stencilValue);
|
||||
NativeContext.ClearDepthStencilView(target.nativeView, (.)clearFlags, depthValue, stencilValue);
|
||||
}
|
||||
|
||||
public override void Clear(RenderTarget2D renderTarget, float depthValue, uint8 stencilValue, DepthStencilClearFlag clearFlags)
|
||||
{
|
||||
_context.nativeContext.ClearDepthStencilView(renderTarget._depthStenilTarget.nativeView, (.)clearFlags, depthValue, stencilValue);
|
||||
NativeContext.ClearDepthStencilView(renderTarget._depthStenilTarget.nativeView, (.)clearFlags, depthValue, stencilValue);
|
||||
}
|
||||
|
||||
public override void DrawIndexed(GeometryBinding geometry)
|
||||
@@ -48,7 +52,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public override void DrawIndexedInstanced(GeometryBinding geometry)
|
||||
{
|
||||
_context.nativeContext.DrawIndexedInstanced(geometry.IndexCount, geometry.InstanceCount, geometry.IndexByteOffset, 0, 0);
|
||||
NativeContext.DrawIndexedInstanced(geometry.IndexCount, geometry.InstanceCount, geometry.IndexByteOffset, 0, 0);
|
||||
}
|
||||
|
||||
public override void SetViewport(Viewport viewport)
|
||||
@@ -57,3 +61,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
using DirectX.D3D11;
|
||||
#if GE_D3D11
|
||||
|
||||
using DirectX.Common;
|
||||
using DirectX.D3D11;
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace DirectX.D3D11
|
||||
{
|
||||
@@ -73,8 +79,6 @@ namespace DirectX.D3D11
|
||||
}
|
||||
}
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
extension SamplerStateDescription
|
||||
@@ -109,15 +113,17 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
_desc.ToNative(var nativeDesc);
|
||||
|
||||
HResult result = _context.nativeDevice.CreateSamplerState(ref nativeDesc, &nativeSamplerState);
|
||||
HResult result = NativeDevice.CreateSamplerState(ref nativeDesc, &nativeSamplerState);
|
||||
|
||||
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create SamplerState. Error({(int)result}): {result}");
|
||||
}
|
||||
|
||||
public override void Bind(uint32 slot)
|
||||
{
|
||||
_context.nativeContext.VertexShader.SetSamplers(slot, 1, &nativeSamplerState);
|
||||
_context.nativeContext.PixelShader.SetSamplers(slot, 1, &nativeSamplerState);
|
||||
NativeContext.VertexShader.SetSamplers(slot, 1, &nativeSamplerState);
|
||||
NativeContext.PixelShader.SetSamplers(slot, 1, &nativeSamplerState);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using System;
|
||||
using GlitchyEngine.Renderer;
|
||||
using DirectX.Common;
|
||||
@@ -79,7 +81,7 @@ namespace GlitchyEngine.Renderer
|
||||
// ConstantBuffer
|
||||
if(bufferDesc.Type == .D3D11_CT_CBUFFER)
|
||||
{
|
||||
let buffer = new ConstantBuffer(_context, bufferReflection);
|
||||
let buffer = new ConstantBuffer(bufferReflection);
|
||||
|
||||
_buffers.Add(bindDesc.BindPoint, buffer.Name, buffer);
|
||||
|
||||
@@ -98,3 +100,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using DirectX.D3D11;
|
||||
using DirectX.DXGI.DXGI1_2;
|
||||
using System.Diagnostics;
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public extension SwapChain
|
||||
{
|
||||
private GraphicsContext _context;
|
||||
private GraphicsContext _context ~ _.ReleaseRef();
|
||||
|
||||
private bool _changed;
|
||||
|
||||
@@ -51,7 +55,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public this(GraphicsContext context)
|
||||
{
|
||||
_context = context;
|
||||
_context = context..AddRef();
|
||||
|
||||
SetResolutionFromWindow();
|
||||
}
|
||||
@@ -107,7 +111,7 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.nativeDevice.QueryInterface(out nativeDxgiDevice);
|
||||
NativeDevice.QueryInterface(out nativeDxgiDevice);
|
||||
|
||||
nativeDxgiDevice.GetAdapter(let adapter);
|
||||
adapter.GetParent<DirectX.DXGI.IDXGIFactory2>(let factory);
|
||||
@@ -125,7 +129,7 @@ namespace GlitchyEngine.Renderer
|
||||
SwapChainFullscreenDescription fsSwapChainDesc = .();
|
||||
fsSwapChainDesc.Windowed = true;
|
||||
|
||||
var createResult = factory.CreateSwapChainForHwnd((.)_context.nativeDevice, _context.nativeWindowHandle, ref swDesc, &fsSwapChainDesc, null, &nativeSwapChain);
|
||||
var createResult = factory.CreateSwapChainForHwnd((.)NativeDevice, _context.nativeWindowHandle, ref swDesc, &fsSwapChainDesc, null, &nativeSwapChain);
|
||||
if(createResult.Failed)
|
||||
{
|
||||
Log.EngineLogger.Error($"Failed to create swap chain. Message({(int)createResult}):{createResult}");
|
||||
@@ -137,7 +141,7 @@ namespace GlitchyEngine.Renderer
|
||||
nativeSwapChain.GetBuffer<ID3D11Texture2D>(0, let backBuffer);
|
||||
|
||||
RenderTargetViewDescription rtvDesc = .(backBuffer, .Texture2D, backBufferViewFormat);
|
||||
_context.nativeDevice.CreateRenderTargetView(backBuffer, &rtvDesc, &nativeBackBufferTarget);
|
||||
NativeDevice.CreateRenderTargetView(backBuffer, &rtvDesc, &nativeBackBufferTarget);
|
||||
|
||||
_backBufferViewport = GlitchyEngine.Renderer.Viewport(0, 0, _width, _height, 0.0f, 1.0f);
|
||||
|
||||
@@ -150,3 +154,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using System;
|
||||
using DirectX.D3D11;
|
||||
using DirectX.Common;
|
||||
@@ -9,6 +11,9 @@ using internal GlitchyEngine.Renderer;
|
||||
|
||||
typealias NativeTex2DDesc = DirectX.D3D11.Texture2DDescription;
|
||||
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
extension Texture
|
||||
@@ -17,8 +22,9 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
protected override void ImplBind(uint32 slot)
|
||||
{
|
||||
_context.nativeContext.VertexShader.SetShaderResources(slot, 1, &nativeResourceView);
|
||||
_context.nativeContext.PixelShader.SetShaderResources(slot, 1, &nativeResourceView);
|
||||
// TODO: textures don't bind themselves!
|
||||
NativeContext.VertexShader.SetShaderResources(slot, 1, &nativeResourceView);
|
||||
NativeContext.PixelShader.SetShaderResources(slot, 1, &nativeResourceView);
|
||||
}
|
||||
|
||||
/** \brief Loads the texture from the specified path.
|
||||
@@ -31,7 +37,7 @@ namespace GlitchyEngine.Renderer
|
||||
((ID3D11Resource*)texture)?.Release();
|
||||
nativeResourceView?.Release();
|
||||
|
||||
HResult loadResult = DDSTextureLoader.CreateDDSTextureFromFile(_context.nativeDevice, path.ToScopedNativeWChar!(),
|
||||
HResult loadResult = DDSTextureLoader.CreateDDSTextureFromFile(NativeDevice, path.ToScopedNativeWChar!(),
|
||||
(.)&texture, &nativeResourceView);
|
||||
|
||||
if(loadResult.Failed)
|
||||
@@ -107,10 +113,10 @@ namespace GlitchyEngine.Renderer
|
||||
// If data is null, set subresourceData null
|
||||
SubresourceData* resDataPtr = data == null ? null : &resData;
|
||||
|
||||
var result = _context.nativeDevice.CreateTexture2D(ref nativeDesc, resDataPtr, &nativeTexture);
|
||||
var result = NativeDevice.CreateTexture2D(ref nativeDesc, resDataPtr, &nativeTexture);
|
||||
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create texture 2D. Error ({result.Underlying}): {result}");
|
||||
|
||||
result = _context.[Friend]nativeDevice.CreateShaderResourceView(nativeTexture, null, &nativeResourceView);
|
||||
result = NativeDevice.CreateShaderResourceView(nativeTexture, null, &nativeResourceView);
|
||||
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to create texture view. Error ({result.Underlying}): {result}");
|
||||
}
|
||||
|
||||
@@ -157,7 +163,7 @@ namespace GlitchyEngine.Renderer
|
||||
case .Default:
|
||||
Box dataBox = .(destX, destY, 0, destX + destWidth, destY + destHeight, 1);
|
||||
var rowPitch = elementSize * destWidth;
|
||||
_context.nativeContext.UpdateSubresource(nativeTexture, subresourceIndex, &dataBox, data, rowPitch, rowPitch * destHeight);
|
||||
NativeContext.UpdateSubresource(nativeTexture, subresourceIndex, &dataBox, data, rowPitch, rowPitch * destHeight);
|
||||
case .Dynamic:
|
||||
Runtime.NotImplemented();
|
||||
/*
|
||||
@@ -202,7 +208,7 @@ namespace GlitchyEngine.Renderer
|
||||
if(destination.nativeTexture == null)
|
||||
destination.InternalCreateTexture(null, 0, 0);
|
||||
|
||||
source._context.nativeContext.CopySubresourceRegion(destination.nativeTexture,
|
||||
NativeContext.CopySubresourceRegion(destination.nativeTexture,
|
||||
D3D11.CalcSubresource(destMipSlice, destArraySlice, destination.MipLevels), destX, destY, 0,
|
||||
source.nativeTexture, D3D11.CalcSubresource(srcMipSlice, srcArraySlice, source.MipLevels), (.)&sourceBox);
|
||||
}
|
||||
@@ -227,7 +233,7 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
Box sourceBox = .(0, 0, 0, Width, Height, 1);
|
||||
|
||||
_context.nativeContext.CopySubresourceRegion(destination.nativeTexture,
|
||||
NativeContext.CopySubresourceRegion(destination.nativeTexture,
|
||||
D3D11.CalcSubresource(mipSlice, arraySlice, destination.MipLevels), 0, 0, 0,
|
||||
nativeTexture, D3D11.CalcSubresource(mipSlice, arraySlice, MipLevels), (.)&sourceBox);
|
||||
}
|
||||
@@ -258,3 +264,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using System;
|
||||
using DirectX.D3D11;
|
||||
using System.Diagnostics;
|
||||
using DirectX.Common;
|
||||
using DirectX.D3D11;
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
@@ -13,11 +17,10 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public ID3DBlob* nativeShaderCode ~ _?.Release();
|
||||
|
||||
public this(GraphicsContext context, VertexElement[] elements, bool ownsElements, VertexShader vertexShader)
|
||||
public this(VertexElement[] elements, bool ownsElements, VertexShader vertexShader)
|
||||
{
|
||||
nativeShaderCode = vertexShader.nativeCode..AddRef();
|
||||
|
||||
_context = context..AddRef();
|
||||
_elements = elements;
|
||||
_ownsElements = ownsElements;
|
||||
|
||||
@@ -38,7 +41,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
ToNativeLayout(_elements, nativeElements);
|
||||
|
||||
var result = _context.nativeDevice.CreateInputLayout(nativeElements.CArray(), (.)nativeElements.Count, nativeShaderCode.GetBufferPointer(), nativeShaderCode.GetBufferSize(), &nativeLayout);
|
||||
var result = NativeDevice.CreateInputLayout(nativeElements.CArray(), (.)nativeElements.Count, nativeShaderCode.GetBufferPointer(), nativeShaderCode.GetBufferSize(), &nativeLayout);
|
||||
if(result.Failed)
|
||||
{
|
||||
Log.EngineLogger.Error($"Failed to create D3D11 input layout: Message({(int)result}): {result}");
|
||||
@@ -46,3 +49,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
#if GE_D3D11
|
||||
|
||||
using System;
|
||||
using GlitchyEngine.Renderer;
|
||||
using DirectX.Common;
|
||||
using DirectX.D3D11;
|
||||
using DirectX.D3DCompiler;
|
||||
using GlitchyEngine.Platform.DX11;
|
||||
|
||||
using internal GlitchyEngine.Renderer;
|
||||
using internal GlitchyEngine.Platform.DX11;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
@@ -16,7 +19,7 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
Shader.PlattformCompileShaderFromSource(code, macros, entryPoint, "vs_5_0", DefaultCompileFlags, out nativeCode);
|
||||
|
||||
var result = _context.nativeDevice.CreateVertexShader(nativeCode.GetBufferPointer(), nativeCode.GetBufferSize(), null, &nativeShader);
|
||||
var result = NativeDevice.CreateVertexShader(nativeCode.GetBufferPointer(), nativeCode.GetBufferSize(), null, &nativeShader);
|
||||
if(result.Failed)
|
||||
{
|
||||
Log.EngineLogger.Error($"Failed to create vertex shader: Message ({(int)result}): {result}");
|
||||
@@ -26,3 +29,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -74,15 +74,12 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public class BlendState : RefCounter
|
||||
{
|
||||
protected GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
protected BlendStateDescription _desc;
|
||||
|
||||
public BlendStateDescription Description => _desc;
|
||||
|
||||
public this(GraphicsContext context, BlendStateDescription desc)
|
||||
public this(BlendStateDescription desc)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
_desc = desc;
|
||||
|
||||
PlatformCreateBlendState();
|
||||
|
||||
@@ -83,24 +83,17 @@ namespace GlitchyEngine.Renderer
|
||||
/// Represents a buffer containing binary data on the GPU.
|
||||
public class Buffer : RefCounter
|
||||
{
|
||||
internal GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
protected BufferDescription _description;
|
||||
|
||||
public GraphicsContext Context => _context;
|
||||
|
||||
public BufferDescription Description => _description;
|
||||
|
||||
protected this(GraphicsContext context)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
}
|
||||
|
||||
protected this() { }
|
||||
|
||||
/**
|
||||
* Creates a new instance of a Buffer.
|
||||
* @param description The buffer description.
|
||||
*/
|
||||
public this(GraphicsContext context, BufferDescription description) : this(context)
|
||||
public this(BufferDescription description)
|
||||
{
|
||||
_description = description;
|
||||
}
|
||||
@@ -162,10 +155,9 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
/**
|
||||
* Creates a new instance of a Buffer<T>
|
||||
* @param context The graphics context.
|
||||
* @param description Describes the Buffer. Note: description.Size is ignored, as it will always be sizeof(T).
|
||||
*/
|
||||
public this(GraphicsContext context, BufferDescription description) : base(context)
|
||||
public this(BufferDescription description)
|
||||
{
|
||||
_description = description;
|
||||
_description.Size = (uint32)sizeof(T);
|
||||
|
||||
@@ -69,7 +69,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public BufferVariableCollection Variables => _variables;
|
||||
|
||||
protected this(GraphicsContext context) : base(context) {}
|
||||
protected this() {}
|
||||
|
||||
protected internal void AddVariable(BufferVariable ownVariable)
|
||||
{
|
||||
|
||||
@@ -10,14 +10,14 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public static bool DrawEntityTransforms = true;
|
||||
|
||||
public static void Init(GraphicsContext context)
|
||||
public static void Init()
|
||||
{
|
||||
CreateCoordinateCross(context);
|
||||
CreateCoordinateCross();
|
||||
}
|
||||
|
||||
private static void CreateCoordinateCross(GraphicsContext context)
|
||||
private static void CreateCoordinateCross()
|
||||
{
|
||||
CoordinateCross = new GeometryBinding(context);
|
||||
CoordinateCross = new GeometryBinding();
|
||||
//CoordinateCross.PrimitiveTopology
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,6 @@ namespace GlitchyEngine.Renderer
|
||||
// TODO: add all features.
|
||||
public class DepthStencilTarget : RefCounter
|
||||
{
|
||||
protected internal GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
protected uint32 _width, _height;
|
||||
protected DepthStencilFormat _format;
|
||||
|
||||
@@ -23,11 +21,10 @@ namespace GlitchyEngine.Renderer
|
||||
public uint32 Height => _height;
|
||||
public DepthStencilFormat Format => _format;
|
||||
|
||||
public this(GraphicsContext context, uint32 width, uint32 height, DepthStencilFormat format = .D32_Float)
|
||||
public this(uint32 width, uint32 height, DepthStencilFormat format = .D32_Float)
|
||||
{
|
||||
Log.EngineLogger.Assert(format != .None, "DepthStencilFormat None is only valid for RenderTarget");
|
||||
|
||||
_context = context..AddRef();
|
||||
_width = width;
|
||||
_height = height;
|
||||
_format = format;
|
||||
@@ -36,9 +33,5 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
|
||||
protected extern void PlatformCreate();
|
||||
|
||||
public extern void Bind();
|
||||
|
||||
public extern void Clear(float depthValue, uint8 stencilValue, DepthStencilClearFlag clearFlags);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,12 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public class EffectLibrary
|
||||
{
|
||||
private GraphicsContext _context ~ _?.ReleaseRef();
|
||||
private Dictionary<String, Effect> _effects = new .() ~ delete _;
|
||||
|
||||
private List<String> _ownedStrings = new .() ~ DeleteContainerAndItems!(_);
|
||||
|
||||
public this(GraphicsContext context)
|
||||
public this()
|
||||
{
|
||||
_context = context..AddRef();
|
||||
}
|
||||
|
||||
public ~this()
|
||||
@@ -63,7 +61,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
Log.EngineLogger.AssertDebug(!Exists(name), "Can't add two effects with the same name to library.");
|
||||
|
||||
Effect effect = new Effect(_context, filepath, name);
|
||||
Effect effect = new Effect(filepath, name);
|
||||
Add(effect);
|
||||
|
||||
return effect;
|
||||
@@ -92,7 +90,6 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public class Effect : RefCounter
|
||||
{
|
||||
protected GraphicsContext _context ~ _?.ReleaseRef();
|
||||
internal VertexShader _vs ~ _?.ReleaseRef();
|
||||
internal PixelShader _ps ~ _?.ReleaseRef();
|
||||
protected String _name ~ delete _;
|
||||
@@ -104,8 +101,6 @@ namespace GlitchyEngine.Renderer
|
||||
typealias TextureEntry = (Texture Texture, ShaderTextureCollection.ResourceEntry* VsSlot, ShaderTextureCollection.ResourceEntry* PsSlot);
|
||||
Dictionary<String, TextureEntry> _textures ~ delete _;
|
||||
|
||||
public GraphicsContext Context => _context;
|
||||
|
||||
public Dictionary<String, TextureEntry> Textures => _textures;
|
||||
|
||||
public VertexShader VertexShader
|
||||
@@ -140,9 +135,8 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
}
|
||||
|
||||
public this(GraphicsContext context, String filename, String vsEntry, String psEntry, String shaderName = null)
|
||||
public this(String filename, String vsEntry, String psEntry, String shaderName = null)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
CompileFromFile(filename, vsEntry, psEntry);
|
||||
|
||||
if(shaderName == null)
|
||||
@@ -156,10 +150,8 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
|
||||
public this(GraphicsContext context, String filename, String shaderName = null)
|
||||
public this(String filename, String shaderName = null)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
|
||||
String fileContent = scope String();
|
||||
String vsName = scope String();
|
||||
String psName = scope String();
|
||||
@@ -243,10 +235,10 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
private void CompileFromFile(String filename, String vsEntry, String psEntry)
|
||||
{
|
||||
let vs = Shader.FromFile!<VertexShader>(_context, filename, vsEntry);
|
||||
let vs = Shader.FromFile!<VertexShader>(filename, vsEntry);
|
||||
VertexShader = vs;
|
||||
vs.ReleaseRef();
|
||||
let ps = Shader.FromFile!<PixelShader>(_context, filename, psEntry);
|
||||
let ps = Shader.FromFile!<PixelShader>(filename, psEntry);
|
||||
PixelShader = ps;
|
||||
ps.ReleaseRef();
|
||||
}
|
||||
@@ -254,10 +246,10 @@ namespace GlitchyEngine.Renderer
|
||||
private void Compile(String fileContent, String vsEntry, String psEntry)
|
||||
{
|
||||
// TODO: vsEntry and psEntry could be empty (which is a valid case.)
|
||||
let vs = new VertexShader(_context, fileContent, vsEntry);
|
||||
let vs = new VertexShader(fileContent, vsEntry);
|
||||
VertexShader = vs;
|
||||
vs.ReleaseRef();
|
||||
let ps = new PixelShader(_context, fileContent, psEntry);
|
||||
let ps = new PixelShader(fileContent, psEntry);
|
||||
PixelShader = ps;
|
||||
ps.ReleaseRef();
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public class GeometryBinding : RefCounter
|
||||
{
|
||||
internal GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
internal List<VertexBufferBinding> _vertexBuffers = new .() ~ delete _;
|
||||
internal IndexBuffer _indexBuffer ~ _?.ReleaseRef();
|
||||
internal VertexLayout _vertexLayout ~ _?.ReleaseRef();
|
||||
@@ -19,9 +17,8 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public bool IsIndexed => _indexBuffer != null;
|
||||
|
||||
public this(GraphicsContext context)
|
||||
public this()
|
||||
{
|
||||
_context = context..AddRef();
|
||||
}
|
||||
|
||||
public ~this()
|
||||
@@ -120,8 +117,8 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
protected extern void PlatformSetIndexBuffer(IndexBuffer indexBuffer);
|
||||
|
||||
public extern void Bind(GraphicsContext context = null);
|
||||
public extern void Bind();
|
||||
|
||||
public extern void Unbind(GraphicsContext context = null);
|
||||
public extern void Unbind();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,9 @@ namespace GlitchyEngine.Renderer
|
||||
private RasterizerState _currentRasterizerState;
|
||||
|
||||
public extern SwapChain SwapChain {get;}
|
||||
|
||||
|
||||
protected extern void PlatformConstruct();
|
||||
|
||||
/**
|
||||
* The maximum number of simultaneous rendertargets supported.
|
||||
*/
|
||||
@@ -17,6 +19,13 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public extern void Init();
|
||||
|
||||
/** @brief Sets a depthtarget.
|
||||
* @BindRenderTargets has to be called in order to bind the rendertargets.
|
||||
* @param renderTarget The render target to set. If null, the backbuffer will be set.
|
||||
* @param slot The slot to which the rendertarget will be bound. If 0 the depth buffer of the current render target will be set. If it has no depthbuffer the current will be unset.
|
||||
*/
|
||||
public extern void SetDepthStencilTarget(DepthStencilTarget target);
|
||||
|
||||
/** @brief Sets a rendertarget to the given slot.
|
||||
* @BindRenderTargets has to be called in order to bind the rendertargets.
|
||||
* @param renderTarget The render target to set. If null, the backbuffer will be set.
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace GlitchyEngine.Renderer
|
||||
public uint32 IndexCount => _indexCount;
|
||||
public IndexFormat Format => _format;
|
||||
|
||||
public this(GraphicsContext context, uint32 indexCount, Usage usage = .Default, CPUAccessFlags cpuAccess = .None, IndexFormat indexFormat = .Index16Bit) : base(context)
|
||||
public this(uint32 indexCount, Usage usage = .Default, CPUAccessFlags cpuAccess = .None, IndexFormat indexFormat = .Index16Bit)
|
||||
{
|
||||
_indexCount = indexCount;
|
||||
_format = indexFormat;
|
||||
|
||||
@@ -5,8 +5,8 @@ namespace GlitchyEngine.Renderer
|
||||
public class PixelShader : Shader
|
||||
{
|
||||
[AllowAppend]
|
||||
public this(GraphicsContext context, String source, String entryPoint, ShaderDefine[] macros = null)
|
||||
: base(context, source, entryPoint, macros) { }
|
||||
public this(String source, String entryPoint, ShaderDefine[] macros = null)
|
||||
: base(source, entryPoint, macros) { }
|
||||
|
||||
public override extern void CompileFromSource(String code, String entryPoint, ShaderDefine[] macros = null);
|
||||
}
|
||||
|
||||
@@ -54,17 +54,10 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public class RasterizerState : RefCounter
|
||||
{
|
||||
internal GraphicsContext _context ~ _?.ReleaseRef();
|
||||
private RasterizerStateDescription _description;
|
||||
|
||||
public GraphicsContext Context => _context;
|
||||
public RasterizerStateDescription Description => _description;
|
||||
|
||||
protected this(GraphicsContext context)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
}
|
||||
|
||||
public extern this(GraphicsContext context, RasterizerStateDescription description);
|
||||
public extern this(RasterizerStateDescription description);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,6 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public class RenderTarget2D : RefCounter
|
||||
{
|
||||
internal GraphicsContext _context;
|
||||
|
||||
private RenderTarget2DDescription _description;
|
||||
|
||||
public RenderTarget2DDescription Description => _description;
|
||||
@@ -63,9 +61,8 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
|
||||
public this(GraphicsContext context, RenderTarget2DDescription description)
|
||||
public this(RenderTarget2DDescription description)
|
||||
{
|
||||
_context = context;
|
||||
_description = description;
|
||||
|
||||
ApplyChanges();
|
||||
|
||||
@@ -31,10 +31,10 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
_context = context..AddRef();
|
||||
/*
|
||||
_sceneConstants = new Buffer<SceneConstants>(_context, .(0, .Constant, .Dynamic, .Write));
|
||||
_sceneConstants = new Buffer<SceneConstants>(.(0, .Constant, .Dynamic, .Write));
|
||||
_sceneConstants.Update();
|
||||
|
||||
_objectConstants = new Buffer<ObjectConstants>(_context, .(0, .Constant, .Dynamic, .Write));
|
||||
_objectConstants = new Buffer<ObjectConstants>(.(0, .Constant, .Dynamic, .Write));
|
||||
_objectConstants.Update();
|
||||
*/
|
||||
|
||||
@@ -53,23 +53,23 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
LineEffect = effectLibrary.Load("content\\Shaders\\lineShader.hlsl");
|
||||
|
||||
LineGeometry = new GeometryBinding(_context);
|
||||
LineGeometry = new GeometryBinding();
|
||||
LineGeometry.SetPrimitiveTopology(.LineList);
|
||||
|
||||
Vector4[2] vertices = .(.Zero, .Zero);
|
||||
LineVertices = new VertexBuffer(_context, sizeof(Vector4), 2, .Dynamic, .Write);
|
||||
LineVertices = new VertexBuffer(sizeof(Vector4), 2, .Dynamic, .Write);
|
||||
LineVertices.SetData<Vector4>(vertices, 0, .WriteDiscard);
|
||||
LineGeometry.SetVertexBufferSlot(LineVertices, 0);
|
||||
|
||||
uint16[2] indices = .(0, 1);
|
||||
IndexBuffer indexBuffer = new IndexBuffer(_context, 2, .Immutable);
|
||||
IndexBuffer indexBuffer = new IndexBuffer(2, .Immutable);
|
||||
indexBuffer.SetData(indices);
|
||||
LineGeometry.SetIndexBuffer(indexBuffer);
|
||||
indexBuffer.ReleaseRef();
|
||||
|
||||
VertexElement[] vertexElements = new VertexElement[1];
|
||||
vertexElements[0] = .(.R32G32B32_Float, "POSITION");
|
||||
VertexLayout layout = new VertexLayout(_context, vertexElements, true, LineEffect.VertexShader);
|
||||
VertexLayout layout = new VertexLayout(vertexElements, true, LineEffect.VertexShader);
|
||||
LineGeometry.SetVertexLayout(layout..ReleaseRefNoDelete());
|
||||
}
|
||||
|
||||
|
||||
@@ -107,15 +107,15 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
private static void InitEffect()
|
||||
{
|
||||
s_textureColorEffect = new Effect(Renderer._context, "content\\Shaders\\textureColor.hlsl");
|
||||
s_batchEffect = new Effect(Renderer._context, "content\\Shaders\\spritebatch.hlsl");
|
||||
s_textureColorEffect = new Effect("content\\Shaders\\textureColor.hlsl");
|
||||
s_batchEffect = new Effect("content\\Shaders\\spritebatch.hlsl");
|
||||
}
|
||||
|
||||
private static void InitGeometry()
|
||||
{
|
||||
VertexLayout layout = new VertexLayout(Renderer._context, QuadVertex.VertexElements, false, s_textureColorEffect.VertexShader);
|
||||
VertexLayout layout = new VertexLayout(QuadVertex.VertexElements, false, s_textureColorEffect.VertexShader);
|
||||
|
||||
VertexBuffer quadVertices = new VertexBuffer(Renderer._context, typeof(QuadVertex), 4, .Immutable);
|
||||
VertexBuffer quadVertices = new VertexBuffer(typeof(QuadVertex), 4, .Immutable);
|
||||
|
||||
QuadVertex[4] vertices = .(
|
||||
.(-0.5f,-0.5f, 0, 1),
|
||||
@@ -126,7 +126,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
quadVertices.SetData(vertices);
|
||||
|
||||
IndexBuffer quadIndices = new IndexBuffer(Renderer._context, 6, .Immutable);
|
||||
IndexBuffer quadIndices = new IndexBuffer(6, .Immutable);
|
||||
|
||||
uint16[6] indices = .(
|
||||
0, 1, 2,
|
||||
@@ -135,7 +135,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
quadIndices.SetData(indices);
|
||||
|
||||
s_quadGeometry = new GeometryBinding(Renderer._context);
|
||||
s_quadGeometry = new GeometryBinding();
|
||||
s_quadGeometry.SetVertexLayout(layout..ReleaseRefNoDelete());
|
||||
s_quadGeometry.SetPrimitiveTopology(.TriangleList);
|
||||
s_quadGeometry.SetVertexBufferSlot(quadVertices, 0);
|
||||
@@ -147,7 +147,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
private static void InitInstancingGeometry()
|
||||
{
|
||||
s_instanceBuffer = new VertexBuffer(Renderer._context, typeof(BatchVertex), 1024, .Dynamic, .Write);
|
||||
s_instanceBuffer = new VertexBuffer(typeof(BatchVertex), 1024, .Dynamic, .Write);
|
||||
s_instanceBuffer.SetData(0);
|
||||
|
||||
VertexElement[] vertexElements = new .(
|
||||
@@ -162,9 +162,9 @@ namespace GlitchyEngine.Renderer
|
||||
VertexElement(.R32G32B32A32_Float, "TEXCOORD", false, 1, 1, (.)-1, .PerInstanceData, 1)
|
||||
);
|
||||
|
||||
VertexLayout batchLayout = new VertexLayout(Renderer._context, vertexElements, true, s_batchEffect.VertexShader);
|
||||
VertexLayout batchLayout = new VertexLayout(vertexElements, true, s_batchEffect.VertexShader);
|
||||
|
||||
s_batchBinding = new GeometryBinding(Renderer._context);
|
||||
s_batchBinding = new GeometryBinding();
|
||||
s_batchBinding.SetVertexLayout(batchLayout..ReleaseRefNoDelete());
|
||||
s_batchBinding.SetPrimitiveTopology(.TriangleList);
|
||||
|
||||
@@ -191,7 +191,7 @@ namespace GlitchyEngine.Renderer
|
||||
Usage = .Immutable,
|
||||
CpuAccess = .None
|
||||
};
|
||||
s_whiteTexture = new Texture2D(Renderer._context, tex2Ddesc);
|
||||
s_whiteTexture = new Texture2D(tex2Ddesc);
|
||||
|
||||
Color color = .White;
|
||||
s_whiteTexture.SetData(&color);
|
||||
@@ -304,7 +304,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
s_currentEffect.Bind(Renderer._context);
|
||||
s_batchBinding.InstanceCount = s_setInstances;
|
||||
s_batchBinding.Bind(Renderer._context);
|
||||
s_batchBinding.Bind();
|
||||
RenderCommand.DrawIndexedInstanced(s_batchBinding);
|
||||
|
||||
s_setInstances = 0;
|
||||
@@ -389,7 +389,7 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
s_textureColorEffect.Bind(Renderer._context);
|
||||
|
||||
s_quadGeometry.Bind(Renderer._context);
|
||||
s_quadGeometry.Bind();
|
||||
RenderCommand.DrawIndexed(s_quadGeometry);
|
||||
}
|
||||
|
||||
|
||||
@@ -153,8 +153,6 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public static class SamplerStateManager
|
||||
{
|
||||
static GraphicsContext _context;
|
||||
|
||||
static Dictionary<SamplerStateDescription, SamplerState> _samplers;
|
||||
|
||||
public static SamplerState PointClamp;
|
||||
@@ -164,9 +162,8 @@ namespace GlitchyEngine.Renderer
|
||||
public static SamplerState AnisotropicClamp;
|
||||
public static SamplerState AnisotropicWrap;
|
||||
|
||||
public static void Init(GraphicsContext context)
|
||||
public static void Init()
|
||||
{
|
||||
_context = context..AddRef();
|
||||
_samplers = new .();
|
||||
|
||||
// Init point samplers
|
||||
@@ -240,8 +237,6 @@ namespace GlitchyEngine.Renderer
|
||||
AnisotropicClamp.ReleaseRef();
|
||||
AnisotropicWrap.ReleaseRef();
|
||||
|
||||
_context?.ReleaseRef();
|
||||
|
||||
delete _samplers;
|
||||
_samplers = null;
|
||||
}
|
||||
@@ -261,7 +256,7 @@ namespace GlitchyEngine.Renderer
|
||||
return sampler..AddRef();
|
||||
}
|
||||
|
||||
SamplerState newState = new SamplerState(_context, desc);
|
||||
SamplerState newState = new SamplerState(desc);
|
||||
ManageSampler(newState);
|
||||
|
||||
return newState;
|
||||
@@ -288,8 +283,6 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public class SamplerState : RefCounter
|
||||
{
|
||||
protected GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
protected SamplerStateDescription _desc;
|
||||
|
||||
[Inline]
|
||||
@@ -304,12 +297,7 @@ namespace GlitchyEngine.Renderer
|
||||
[Inline]
|
||||
public SamplerStateDescription Description => _desc;
|
||||
|
||||
protected this(GraphicsContext context)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
}
|
||||
|
||||
public this(GraphicsContext context, SamplerStateDescription desc) : this(context)
|
||||
public this(SamplerStateDescription desc)
|
||||
{
|
||||
_desc = desc;
|
||||
|
||||
|
||||
@@ -21,27 +21,22 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public abstract class Shader : RefCounter
|
||||
{
|
||||
protected GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
protected BufferCollection _buffers ~ delete _;//:append _;
|
||||
|
||||
protected ShaderTextureCollection _textures ~ delete _;
|
||||
|
||||
public GraphicsContext Context => _context;
|
||||
|
||||
public BufferCollection Buffers => _buffers;
|
||||
|
||||
public ShaderTextureCollection Textures => _textures;
|
||||
|
||||
[AllowAppend]
|
||||
public this(GraphicsContext context, String source, String entryPoint, ShaderDefine[] macros = null)
|
||||
public this(String source, String entryPoint, ShaderDefine[] macros = null)
|
||||
{
|
||||
// Todo: append as soon as it's fixed.
|
||||
//let buffers = new BufferCollection();
|
||||
_buffers = new BufferCollection();
|
||||
_textures = new ShaderTextureCollection();
|
||||
|
||||
_context = context..AddRef();
|
||||
CompileFromSource(source, entryPoint);
|
||||
}
|
||||
|
||||
@@ -50,12 +45,12 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
}
|
||||
|
||||
public static mixin FromFile<T>(GraphicsContext context, String fileName, String entryPoint, ShaderDefine[] macros = null) where T : Shader
|
||||
public static mixin FromFile<T>(String fileName, String entryPoint, ShaderDefine[] macros = null) where T : Shader
|
||||
{
|
||||
String fileContent = new String();
|
||||
|
||||
File.ReadAllText(fileName, fileContent, true);
|
||||
T shader = new T(context, fileContent, entryPoint, macros);
|
||||
T shader = new T(fileContent, entryPoint, macros);
|
||||
|
||||
delete fileContent;
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ namespace GlitchyEngine.Renderer.Text
|
||||
public bool IsRendered = false;
|
||||
}
|
||||
|
||||
private GraphicsContext _context ~ _.ReleaseRef();
|
||||
internal FT_Face _face ~ FreeType.Done_Face(_face);
|
||||
|
||||
private Font _fallback ~ _?.ReleaseRef();
|
||||
@@ -106,13 +105,11 @@ namespace GlitchyEngine.Renderer.Text
|
||||
return (double(value) / 64.0);
|
||||
}
|
||||
|
||||
public this(GraphicsContext context, String fontPath, uint32 fontSize, bool hasColor = true, char32 firstChar = '\0', uint32 charCount = 128, int32 faceIndex = 0)
|
||||
public this(String fontPath, uint32 fontSize, bool hasColor = true, char32 firstChar = '\0', uint32 charCount = 128, int32 faceIndex = 0)
|
||||
{
|
||||
// Make sure the fontrenderer is initialized (Font only cares about freetype)
|
||||
FontRenderer.Init();
|
||||
|
||||
_context = context..AddRef();
|
||||
|
||||
// Set default sampler
|
||||
Sampler = null;
|
||||
|
||||
@@ -317,7 +314,7 @@ namespace GlitchyEngine.Renderer.Text
|
||||
desc.CpuAccess = .None;
|
||||
desc.Usage = .Default;
|
||||
|
||||
_atlas = new Texture2D(_context, desc);
|
||||
_atlas = new Texture2D(desc);
|
||||
_atlas.SamplerState = _sampler;
|
||||
|
||||
if(oldAtlas != null)
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace GlitchyEngine.Renderer.Text
|
||||
|
||||
InitFreetype();
|
||||
|
||||
_msdfEffect = new Effect(Renderer._context, "content\\Shaders\\msdfShader.hlsl");
|
||||
_msdfEffect = new Effect("content\\Shaders\\msdfShader.hlsl");
|
||||
|
||||
s_isInitialized = true;
|
||||
}
|
||||
|
||||
@@ -6,12 +6,8 @@ namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public abstract class Texture : RefCounter
|
||||
{
|
||||
protected GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
protected SamplerState _samplerState ~ _?.ReleaseRef();
|
||||
|
||||
public GraphicsContext Context => _context;
|
||||
|
||||
public SamplerState SamplerState
|
||||
{
|
||||
get => _samplerState;
|
||||
@@ -41,11 +37,6 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
|
||||
protected extern void ImplBind(uint32 slot);
|
||||
|
||||
protected this(GraphicsContext context)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
}
|
||||
}
|
||||
|
||||
public struct Texture2DDesc
|
||||
@@ -69,15 +60,13 @@ namespace GlitchyEngine.Renderer
|
||||
public override extern uint32 ArraySize {get;}
|
||||
public override extern uint32 MipLevels {get;}
|
||||
|
||||
protected this(GraphicsContext context) : base(context) {}
|
||||
|
||||
public this(GraphicsContext context, String path) : base(context)
|
||||
public this(String path)
|
||||
{
|
||||
this._path = new String(path);
|
||||
LoadTexturePlatform();
|
||||
}
|
||||
|
||||
public this(GraphicsContext context, Texture2DDesc desc) : base(context)
|
||||
public this(Texture2DDesc desc)
|
||||
{
|
||||
PrepareTexturePlatform(desc, false);
|
||||
}
|
||||
@@ -132,9 +121,7 @@ namespace GlitchyEngine.Renderer
|
||||
public override extern uint32 ArraySize {get;}
|
||||
public override extern uint32 MipLevels {get;}
|
||||
|
||||
protected this(GraphicsContext context) : base(context) {}
|
||||
|
||||
public this(GraphicsContext context, String path) : base(context)
|
||||
public this(String path)
|
||||
{
|
||||
this._path = new String(path);
|
||||
LoadTexturePlatform();
|
||||
|
||||
@@ -25,13 +25,13 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public VertexBufferBinding Binding => _defaultBinding;
|
||||
|
||||
public this(GraphicsContext context, Type vertexType, uint32 vertexCount, Usage usage = .Default, CPUAccessFlags cpuAccess = .None) :
|
||||
this(context, (.)vertexType.Stride, vertexCount, usage, cpuAccess)
|
||||
public this(Type vertexType, uint32 vertexCount, Usage usage = .Default, CPUAccessFlags cpuAccess = .None) :
|
||||
this((.)vertexType.Stride, vertexCount, usage, cpuAccess)
|
||||
{
|
||||
_vertexType = vertexType;
|
||||
}
|
||||
|
||||
public this(GraphicsContext context, uint32 vertexStride, uint32 vertexCount, Usage usage = .Default, CPUAccessFlags cpuAccess = .None) : base(context)
|
||||
public this(uint32 vertexStride, uint32 vertexCount, Usage usage = .Default, CPUAccessFlags cpuAccess = .None)
|
||||
{
|
||||
_vertexType = null;
|
||||
|
||||
|
||||
@@ -81,24 +81,11 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public class VertexLayout : RefCounter
|
||||
{
|
||||
private GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
private VertexElement[] _elements;
|
||||
private bool _ownsElements;
|
||||
|
||||
public GraphicsContext Context => _context;
|
||||
|
||||
public VertexElement[] Elements => _elements;
|
||||
|
||||
public this(GraphicsContext context, VertexElement[] elements, bool ownsElements, VertexShader vertexShader)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
_elements = elements;
|
||||
_ownsElements = ownsElements;
|
||||
|
||||
CreateNativeLayout();
|
||||
}
|
||||
|
||||
public ~this()
|
||||
{
|
||||
if(_ownsElements)
|
||||
|
||||
@@ -5,8 +5,8 @@ namespace GlitchyEngine.Renderer
|
||||
public class VertexShader : Shader
|
||||
{
|
||||
[AllowAppend]
|
||||
public this(GraphicsContext context, String source, String entryPoint, ShaderDefine[] macros = null)
|
||||
: base(context, source, entryPoint, macros) { }
|
||||
public this(String source, String entryPoint, ShaderDefine[] macros = null)
|
||||
: base(source, entryPoint, macros) { }
|
||||
|
||||
public override extern void CompileFromSource(String code, String entryPoint, ShaderDefine[] macros = null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user