Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor shaders #9

Merged
merged 6 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions MonoGame.Framework/Graphics/Effect/EffectReader09.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

// Copyright (C)2022 Nick Kastellanos

using System;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -72,10 +74,10 @@ ConstantBuffer ReadConstantBuffer()
}

var buffer = new ConstantBuffer(graphicsDevice,
sizeInBytes,
name,
parameters,
offsets,
name);
sizeInBytes);
return buffer;
}

Expand All @@ -95,10 +97,10 @@ private Shader ReadShader()

private EffectParameterCollection ReadParameters()
{
//TNC: fallback to version 8
// fallback to version 8
int count = (header.Version == 8)
? (int)ReadByte()
: Read7BitEncodedInt();
? (int)ReadByte()
: Read7BitEncodedInt();

if (count == 0)
return EffectParameterCollection.Empty;
Expand All @@ -124,39 +126,37 @@ private EffectParameterCollection ReadParameters()
{
case EffectParameterType.Bool:
case EffectParameterType.Int32:
{
#if !OPENGL
// Under most platforms we properly store integers and
// booleans in an integer type.
//
// MojoShader on the otherhand stores everything in float
// types which is why this code is disabled under OpenGL.
{
var buffer = new int[rowCount * columnCount];
// MojoShader stores Integers and Booleans in a float type.
var buffer = new float[rowCount * columnCount];
#else
// Integers and Booleans are stored in an integer type.
var buffer = new int[rowCount * columnCount];
#endif
for (var j = 0; j < buffer.Length; j++)
buffer[j] = ReadInt32();
data = buffer;
break;
}
#endif
}
break;

case EffectParameterType.Single:
{
var buffer = new float[rowCount * columnCount];
for (var j = 0; j < buffer.Length; j++)
buffer[j] = ReadSingle();
data = buffer;
break;
}
break;

case EffectParameterType.String:
// TODO: We have not investigated what a string
// type should do in the parameter list. Till then
// throw to let the user know.
throw new NotSupportedException();
throw new NotImplementedException();

default:
// NOTE: We skip over all other types as they
// don't get added to the constant buffer.
{
Debug.WriteLine("Parameter {0} of type {1} is ignored", name, type.ToString());
// throw new NotImplementedException();
}
break;
}
}
Expand Down
39 changes: 20 additions & 19 deletions MonoGame.Framework/Graphics/Effect/EffectReader10.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

// Copyright (C)2022 Nick Kastellanos

using System;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -40,6 +42,7 @@ internal Effect ReadEffect()
effect._shaders = ReadShaders();
effect.Parameters = ReadParameters();
effect.Techniques = ReadTechniques(effect);

effect.CurrentTechnique = effect.Techniques[0];

// Check file tail to ensure we parsed the content correctly.
Expand Down Expand Up @@ -76,10 +79,10 @@ ConstantBuffer ReadConstantBuffer()
}

var buffer = new ConstantBuffer(graphicsDevice,
sizeInBytes,
name,
parameters,
offsets,
name);
sizeInBytes);
return buffer;
}

Expand Down Expand Up @@ -125,39 +128,37 @@ private EffectParameterCollection ReadParameters()
{
case EffectParameterType.Bool:
case EffectParameterType.Int32:
{
#if !OPENGL
// Under most platforms we properly store integers and
// booleans in an integer type.
//
// MojoShader on the otherhand stores everything in float
// types which is why this code is disabled under OpenGL.
{
var buffer = new int[rowCount * columnCount];
// MojoShader stores Integers and Booleans in a float type.
var buffer = new float[rowCount * columnCount];
#else
// Integers and Booleans are stored in an integer type.
var buffer = new int[rowCount * columnCount];
#endif
for (var j = 0; j < buffer.Length; j++)
buffer[j] = ReadInt32();
data = buffer;
break;
}
#endif
}
break;

case EffectParameterType.Single:
{
var buffer = new float[rowCount * columnCount];
for (var j = 0; j < buffer.Length; j++)
buffer[j] = ReadSingle();
data = buffer;
break;
}
break;

case EffectParameterType.String:
// TODO: We have not investigated what a string
// type should do in the parameter list. Till then
// throw to let the user know.
throw new NotSupportedException();
throw new NotImplementedException();

default:
// NOTE: We skip over all other types as they
// don't get added to the constant buffer.
{
Debug.WriteLine("Parameter {0} of type {1} is ignored", name, type.ToString());
// throw new NotImplementedException();
}
break;
}
}
Expand Down
15 changes: 0 additions & 15 deletions MonoGame.Framework/Graphics/Effect/Resources/RebuildMGFX.bat

This file was deleted.

17 changes: 17 additions & 0 deletions MonoGame.Framework/Graphics/Effect/Shaders/BuildShaders.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@echo off
setlocal

SET MGFXC="..\..\..\..\Artifacts\MonoGame.Effect.Compiler\Release\mgfxc.exe"

@for /f %%f IN ('dir /b *.fx') do (

echo %%~nf.fx

call %MGFXC% %%~nf.fx ..\Resources\%%~nf.ogl.mgfxo /Profile:OpenGL

call %MGFXC% %%~nf.fx ..\Resources\%%~nf.dx11.mgfxo /Profile:DirectX_11

)

endlocal
pause
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#define TECHNIQUE(name, vsname, psname ) \
technique name { pass { VertexShader = compile vs_4_0_level_9_1 vsname (); PixelShader = compile ps_4_0_level_9_1 psname(); } }

#define TECHNIQUE_9_3(name, vsname, psname ) \
technique name { pass { VertexShader = compile vs_4_0_level_9_3 vsname (); PixelShader = compile ps_4_0_level_9_3 psname(); } }

#define BEGIN_CONSTANTS cbuffer Parameters : register(b0) {
#define MATRIX_CONSTANTS
#define END_CONSTANTS };
Expand Down Expand Up @@ -40,6 +43,9 @@
#define TECHNIQUE(name, vsname, psname ) \
technique name { pass { VertexShader = compile vs_2_0 vsname (); PixelShader = compile ps_2_0 psname(); } }

#define TECHNIQUE_9_3(name, vsname, psname ) \
technique name { pass { VertexShader = compile vs_3_0 vsname (); PixelShader = compile ps_3_0 psname(); } }

#define BEGIN_CONSTANTS
#define MATRIX_CONSTANTS
#define END_CONSTANTS
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Graphics/GraphicsDevice.Blazor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,8 @@ private void PlatformApplyShaders()
_vertexShaderDirty = _pixelShaderDirty = false;
}

_vertexConstantBuffers.SetConstantBuffers(this, _shaderProgram);
_pixelConstantBuffers.SetConstantBuffers(this, _shaderProgram);
_vertexConstantBuffers.SetConstantBuffers(_shaderProgram);
_pixelConstantBuffers.SetConstantBuffers(_shaderProgram);

Textures.SetTextures(this);
SamplerStates.PlatformSetSamplers(this);
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Graphics/GraphicsDevice.DirectX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1390,8 +1390,8 @@ private void PlatformApplyShaders()
}
}

_vertexConstantBuffers.SetConstantBuffers(this);
_pixelConstantBuffers.SetConstantBuffers(this);
_vertexConstantBuffers.SetConstantBuffers();
_pixelConstantBuffers.SetConstantBuffers();

VertexTextures.SetTextures(this);
VertexSamplerStates.PlatformSetSamplers(this);
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Graphics/GraphicsDevice.OpenGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,8 +1072,8 @@ private void PlatformApplyShaders()
_vertexShaderDirty = _pixelShaderDirty = false;
}

_vertexConstantBuffers.SetConstantBuffers(this, _shaderProgram);
_pixelConstantBuffers.SetConstantBuffers(this, _shaderProgram);
_vertexConstantBuffers.SetConstantBuffers(_shaderProgram);
_pixelConstantBuffers.SetConstantBuffers(_shaderProgram);

Textures.SetTextures(this);
SamplerStates.PlatformSetSamplers(this);
Expand Down
20 changes: 17 additions & 3 deletions MonoGame.Framework/Graphics/Shader/ConstantBuffer.Blazor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

// Copyright (C)2022 Nick Kastellanos

using System;
using Microsoft.Xna.Platform.Graphics;
using nkast.Wasm.Canvas.WebGL;


namespace Microsoft.Xna.Framework.Graphics
{
internal partial class ConstantBuffer
{
private IWebGLRenderingContext GL { get { return GraphicsDevice._glContext; } }

private ShaderProgram _shaderProgram = null;
private WebGLUniformLocation _location;

Expand Down Expand Up @@ -39,9 +45,9 @@ private void PlatformClear()
_shaderProgram = null;
}

public unsafe void PlatformApply(GraphicsDevice device, ShaderProgram program)
internal unsafe void PlatformApplyEx(ShaderStage stage, int slot, ShaderProgram program)
{
// NOTE: We assume here the program has
// NOTE: We assume here the program has
// already been set on the device.

// If the program changed then lookup the
Expand Down Expand Up @@ -72,7 +78,7 @@ public unsafe void PlatformApply(GraphicsDevice device, ShaderProgram program)
// and cast this correctly... else it doesn't work as i guess
// GL is checking the type of the uniform.

GraphicsDevice._glContext.Uniform4fv(_location, _buffer);
GL.Uniform4fv(_location, _buffer);
GraphicsExtensions.CheckGLError();
}

Expand All @@ -81,5 +87,13 @@ public unsafe void PlatformApply(GraphicsDevice device, ShaderProgram program)

_lastConstantBufferApplied = this;
}

private void PlatformDispose(bool disposing)
{
if (disposing)
{
}

}
}
}
12 changes: 8 additions & 4 deletions MonoGame.Framework/Graphics/Shader/ConstantBuffer.DirectX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

// Copyright (C)2022 Nick Kastellanos


namespace Microsoft.Xna.Framework.Graphics
{
internal partial class ConstantBuffer : GraphicsResource
internal partial class ConstantBuffer
{
private SharpDX.Direct3D11.Buffer _cbuffer;

Expand All @@ -27,7 +29,7 @@ private void PlatformClear()
_dirty = true;
}

internal void PlatformApply(GraphicsDevice device, ShaderStage stage, int slot)
internal void PlatformApply(ShaderStage stage, int slot)
{
if (_cbuffer == null)
PlatformInitialize();
Expand All @@ -50,11 +52,13 @@ internal void PlatformApply(GraphicsDevice device, ShaderStage stage, int slot)
d3dContext.PixelShader.SetConstantBuffer(slot, _cbuffer);
}

protected override void Dispose(bool disposing)
private void PlatformDispose(bool disposing)
{
if (disposing)
{
SharpDX.Utilities.Dispose(ref _cbuffer);
base.Dispose(disposing);
}

}
}
}
13 changes: 12 additions & 1 deletion MonoGame.Framework/Graphics/Shader/ConstantBuffer.OpenGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

// Copyright (C)2022 Nick Kastellanos

using System;
using MonoGame.OpenGL;


namespace Microsoft.Xna.Framework.Graphics
{
internal partial class ConstantBuffer
Expand Down Expand Up @@ -39,7 +42,7 @@ private void PlatformClear()
_shaderProgram = null;
}

public unsafe void PlatformApply(GraphicsDevice device, ShaderProgram program)
internal unsafe void PlatformApplyEx(ShaderStage stage, int slot, ShaderProgram program)
{
// NOTE: We assume here the program has
// already been set on the device.
Expand Down Expand Up @@ -81,5 +84,13 @@ public unsafe void PlatformApply(GraphicsDevice device, ShaderProgram program)

_lastConstantBufferApplied = this;
}

private void PlatformDispose(bool disposing)
{
if (disposing)
{
}

}
}
}
Loading