2018-04-08 15:17:35 -04:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
using Ryujinx.Graphics.Gal.Shader;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.OpenGL
|
|
|
|
{
|
2018-08-10 00:09:40 -04:00
|
|
|
class OGLShader : IGalShader
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-08-25 00:16:58 -04:00
|
|
|
public const int ReservedCbufCount = 1;
|
|
|
|
|
|
|
|
private const int ExtraDataSize = 4;
|
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
public OGLShaderProgram Current;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
private ConcurrentDictionary<long, OGLShaderStage> Stages;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
private Dictionary<OGLShaderProgram, int> Programs;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
|
|
|
public int CurrentProgramHandle { get; private set; }
|
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
private OGLConstBuffer Buffer;
|
2018-06-26 01:10:54 -04:00
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
private int ExtraUboHandle;
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
public OGLShader(OGLConstBuffer Buffer)
|
|
|
|
{
|
|
|
|
this.Buffer = Buffer;
|
2018-06-26 01:10:54 -04:00
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
Stages = new ConcurrentDictionary<long, OGLShaderStage>();
|
2018-06-26 01:10:54 -04:00
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
Programs = new Dictionary<OGLShaderProgram, int>();
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2018-06-23 20:39:25 -04:00
|
|
|
public void Create(IGalMemory Memory, long Key, GalShaderType Type)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-06-27 22:55:08 -04:00
|
|
|
Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, Key, 0, false, Type));
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2018-06-27 22:55:08 -04:00
|
|
|
public void Create(IGalMemory Memory, long VpAPos, long Key, GalShaderType Type)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-06-27 22:55:08 -04:00
|
|
|
Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, VpAPos, Key, true, Type));
|
|
|
|
}
|
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
private OGLShaderStage ShaderStageFactory(
|
2018-06-27 22:55:08 -04:00
|
|
|
IGalMemory Memory,
|
|
|
|
long Position,
|
|
|
|
long PositionB,
|
|
|
|
bool IsDualVp,
|
|
|
|
GalShaderType Type)
|
|
|
|
{
|
|
|
|
GlslProgram Program;
|
|
|
|
|
|
|
|
GlslDecompiler Decompiler = new GlslDecompiler();
|
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
int ShaderDumpIndex = ShaderDumper.DumpIndex;
|
|
|
|
|
2018-06-27 22:55:08 -04:00
|
|
|
if (IsDualVp)
|
|
|
|
{
|
2018-07-19 01:33:27 -04:00
|
|
|
ShaderDumper.Dump(Memory, Position, Type, "a");
|
|
|
|
ShaderDumper.Dump(Memory, PositionB, Type, "b");
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
Program = Decompiler.Decompile(Memory, Position, PositionB, Type);
|
2018-06-27 22:55:08 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-19 01:33:27 -04:00
|
|
|
ShaderDumper.Dump(Memory, Position, Type);
|
2018-07-15 18:37:27 -04:00
|
|
|
|
2018-07-19 01:33:27 -04:00
|
|
|
Program = Decompiler.Decompile(Memory, Position, Type);
|
2018-06-27 22:55:08 -04:00
|
|
|
}
|
2018-04-08 15:17:35 -04:00
|
|
|
|
2018-10-17 17:02:23 -04:00
|
|
|
string Code = Program.Code;
|
|
|
|
|
|
|
|
if (ShaderDumper.IsDumpEnabled())
|
|
|
|
{
|
|
|
|
Code = "//Shader " + ShaderDumpIndex + Environment.NewLine + Code;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new OGLShaderStage(Type, Code, Program.Uniforms, Program.Textures);
|
2018-08-15 14:59:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<ShaderDeclInfo> GetConstBufferUsage(long Key)
|
|
|
|
{
|
|
|
|
if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
|
|
|
|
{
|
|
|
|
return Stage.ConstBufferUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Enumerable.Empty<ShaderDeclInfo>();
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2018-06-23 20:39:25 -04:00
|
|
|
public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Key)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-08-10 00:09:40 -04:00
|
|
|
if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
|
|
|
return Stage.TextureUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Enumerable.Empty<ShaderDeclInfo>();
|
|
|
|
}
|
|
|
|
|
2018-08-25 00:16:58 -04:00
|
|
|
public unsafe void SetExtraData(float FlipX, float FlipY, int Instance)
|
2018-04-13 23:39:24 -04:00
|
|
|
{
|
|
|
|
BindProgram();
|
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
EnsureExtraBlock();
|
|
|
|
|
|
|
|
GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
|
|
|
|
|
2018-08-25 00:16:58 -04:00
|
|
|
float* Data = stackalloc float[ExtraDataSize];
|
|
|
|
Data[0] = FlipX;
|
|
|
|
Data[1] = FlipY;
|
|
|
|
Data[2] = BitConverter.Int32BitsToSingle(Instance);
|
2018-04-13 23:39:24 -04:00
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
//Invalidate buffer
|
2018-08-25 00:16:58 -04:00
|
|
|
GL.BufferData(BufferTarget.UniformBuffer, ExtraDataSize * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
|
2018-08-10 00:09:40 -04:00
|
|
|
|
2018-08-25 00:16:58 -04:00
|
|
|
GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, ExtraDataSize * sizeof(float), (IntPtr)Data);
|
2018-04-13 23:39:24 -04:00
|
|
|
}
|
|
|
|
|
2018-06-23 20:39:25 -04:00
|
|
|
public void Bind(long Key)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-08-10 00:09:40 -04:00
|
|
|
if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
|
|
|
Bind(Stage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
private void Bind(OGLShaderStage Stage)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
2018-07-19 01:33:27 -04:00
|
|
|
if (Stage.Type == GalShaderType.Geometry)
|
|
|
|
{
|
|
|
|
//Enhanced layouts are required for Geometry shaders
|
|
|
|
//skip this stage if current driver has no ARB_enhanced_layouts
|
2018-09-25 18:55:30 -04:00
|
|
|
if (!OGLExtension.EnhancedLayouts)
|
2018-07-19 01:33:27 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 15:17:35 -04:00
|
|
|
switch (Stage.Type)
|
|
|
|
{
|
|
|
|
case GalShaderType.Vertex: Current.Vertex = Stage; break;
|
|
|
|
case GalShaderType.TessControl: Current.TessControl = Stage; break;
|
|
|
|
case GalShaderType.TessEvaluation: Current.TessEvaluation = Stage; break;
|
|
|
|
case GalShaderType.Geometry: Current.Geometry = Stage; break;
|
|
|
|
case GalShaderType.Fragment: Current.Fragment = Stage; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 12:08:39 -04:00
|
|
|
public void Unbind(GalShaderType Type)
|
|
|
|
{
|
|
|
|
switch (Type)
|
|
|
|
{
|
|
|
|
case GalShaderType.Vertex: Current.Vertex = null; break;
|
|
|
|
case GalShaderType.TessControl: Current.TessControl = null; break;
|
|
|
|
case GalShaderType.TessEvaluation: Current.TessEvaluation = null; break;
|
|
|
|
case GalShaderType.Geometry: Current.Geometry = null; break;
|
|
|
|
case GalShaderType.Fragment: Current.Fragment = null; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 15:17:35 -04:00
|
|
|
public void BindProgram()
|
|
|
|
{
|
|
|
|
if (Current.Vertex == null ||
|
|
|
|
Current.Fragment == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Programs.TryGetValue(Current, out int Handle))
|
|
|
|
{
|
|
|
|
Handle = GL.CreateProgram();
|
|
|
|
|
|
|
|
AttachIfNotNull(Handle, Current.Vertex);
|
|
|
|
AttachIfNotNull(Handle, Current.TessControl);
|
|
|
|
AttachIfNotNull(Handle, Current.TessEvaluation);
|
|
|
|
AttachIfNotNull(Handle, Current.Geometry);
|
|
|
|
AttachIfNotNull(Handle, Current.Fragment);
|
|
|
|
|
|
|
|
GL.LinkProgram(Handle);
|
|
|
|
|
|
|
|
CheckProgramLink(Handle);
|
|
|
|
|
2018-06-26 01:10:54 -04:00
|
|
|
BindUniformBlocks(Handle);
|
2018-08-23 00:54:32 -04:00
|
|
|
BindTextureLocations(Handle);
|
2018-06-26 01:10:54 -04:00
|
|
|
|
2018-04-08 15:17:35 -04:00
|
|
|
Programs.Add(Current, Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.UseProgram(Handle);
|
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
CurrentProgramHandle = Handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsureExtraBlock()
|
|
|
|
{
|
|
|
|
if (ExtraUboHandle == 0)
|
2018-07-26 12:49:29 -04:00
|
|
|
{
|
2018-08-10 00:09:40 -04:00
|
|
|
ExtraUboHandle = GL.GenBuffer();
|
2018-06-26 01:10:54 -04:00
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
|
|
|
|
|
2018-08-25 00:16:58 -04:00
|
|
|
GL.BufferData(BufferTarget.UniformBuffer, ExtraDataSize * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
|
2018-08-10 00:09:40 -04:00
|
|
|
|
|
|
|
GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, ExtraUboHandle);
|
|
|
|
}
|
2018-04-08 15:17:35 -04:00
|
|
|
}
|
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
private void AttachIfNotNull(int ProgramHandle, OGLShaderStage Stage)
|
2018-04-08 15:17:35 -04:00
|
|
|
{
|
|
|
|
if (Stage != null)
|
|
|
|
{
|
|
|
|
Stage.Compile();
|
|
|
|
|
|
|
|
GL.AttachShader(ProgramHandle, Stage.Handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 01:10:54 -04:00
|
|
|
private void BindUniformBlocks(int ProgramHandle)
|
|
|
|
{
|
2018-08-10 00:09:40 -04:00
|
|
|
int ExtraBlockindex = GL.GetUniformBlockIndex(ProgramHandle, GlslDecl.ExtraUniformBlockName);
|
|
|
|
|
|
|
|
GL.UniformBlockBinding(ProgramHandle, ExtraBlockindex, 0);
|
|
|
|
|
2018-08-25 00:16:58 -04:00
|
|
|
int FreeBinding = ReservedCbufCount;
|
2018-06-26 01:10:54 -04:00
|
|
|
|
2018-08-10 00:09:40 -04:00
|
|
|
void BindUniformBlocksIfNotNull(OGLShaderStage Stage)
|
2018-06-26 01:10:54 -04:00
|
|
|
{
|
|
|
|
if (Stage != null)
|
|
|
|
{
|
2018-08-15 14:59:51 -04:00
|
|
|
foreach (ShaderDeclInfo DeclInfo in Stage.ConstBufferUsage)
|
2018-06-26 01:10:54 -04:00
|
|
|
{
|
|
|
|
int BlockIndex = GL.GetUniformBlockIndex(ProgramHandle, DeclInfo.Name);
|
|
|
|
|
|
|
|
if (BlockIndex < 0)
|
|
|
|
{
|
|
|
|
//It is expected that its found, if it's not then driver might be in a malfunction
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.UniformBlockBinding(ProgramHandle, BlockIndex, FreeBinding);
|
|
|
|
|
|
|
|
FreeBinding++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BindUniformBlocksIfNotNull(Current.Vertex);
|
|
|
|
BindUniformBlocksIfNotNull(Current.TessControl);
|
|
|
|
BindUniformBlocksIfNotNull(Current.TessEvaluation);
|
|
|
|
BindUniformBlocksIfNotNull(Current.Geometry);
|
|
|
|
BindUniformBlocksIfNotNull(Current.Fragment);
|
|
|
|
}
|
|
|
|
|
2018-08-23 00:54:32 -04:00
|
|
|
private void BindTextureLocations(int ProgramHandle)
|
|
|
|
{
|
|
|
|
int Index = 0;
|
|
|
|
|
|
|
|
void BindTexturesIfNotNull(OGLShaderStage Stage)
|
|
|
|
{
|
|
|
|
if (Stage != null)
|
|
|
|
{
|
|
|
|
foreach (ShaderDeclInfo Decl in Stage.TextureUsage)
|
|
|
|
{
|
|
|
|
int Location = GL.GetUniformLocation(ProgramHandle, Decl.Name);
|
|
|
|
|
|
|
|
GL.Uniform1(Location, Index);
|
|
|
|
|
|
|
|
Index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.UseProgram(ProgramHandle);
|
|
|
|
|
|
|
|
BindTexturesIfNotNull(Current.Vertex);
|
|
|
|
BindTexturesIfNotNull(Current.TessControl);
|
|
|
|
BindTexturesIfNotNull(Current.TessEvaluation);
|
|
|
|
BindTexturesIfNotNull(Current.Geometry);
|
|
|
|
BindTexturesIfNotNull(Current.Fragment);
|
|
|
|
}
|
|
|
|
|
2018-04-08 15:17:35 -04:00
|
|
|
private static void CheckProgramLink(int Handle)
|
|
|
|
{
|
|
|
|
int Status = 0;
|
|
|
|
|
|
|
|
GL.GetProgram(Handle, GetProgramParameterName.LinkStatus, out Status);
|
|
|
|
|
|
|
|
if (Status == 0)
|
|
|
|
{
|
|
|
|
throw new ShaderException(GL.GetProgramInfoLog(Handle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|