2018-04-08 12:17:35 -07: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-06-23 17:39:25 -07:00
|
|
|
public class OGLShader : IGalShader
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
|
|
|
private class ShaderStage : IDisposable
|
|
|
|
{
|
|
|
|
public int Handle { get; private set; }
|
|
|
|
|
|
|
|
public bool IsCompiled { get; private set; }
|
|
|
|
|
|
|
|
public GalShaderType Type { get; private set; }
|
|
|
|
|
|
|
|
public string Code { get; private set; }
|
|
|
|
|
|
|
|
public IEnumerable<ShaderDeclInfo> TextureUsage { get; private set; }
|
|
|
|
public IEnumerable<ShaderDeclInfo> UniformUsage { get; private set; }
|
|
|
|
|
|
|
|
public ShaderStage(
|
|
|
|
GalShaderType Type,
|
|
|
|
string Code,
|
|
|
|
IEnumerable<ShaderDeclInfo> TextureUsage,
|
|
|
|
IEnumerable<ShaderDeclInfo> UniformUsage)
|
|
|
|
{
|
|
|
|
this.Type = Type;
|
|
|
|
this.Code = Code;
|
|
|
|
this.TextureUsage = TextureUsage;
|
|
|
|
this.UniformUsage = UniformUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Compile()
|
|
|
|
{
|
|
|
|
if (Handle == 0)
|
|
|
|
{
|
|
|
|
Handle = GL.CreateShader(OGLEnumConverter.GetShaderType(Type));
|
|
|
|
|
|
|
|
CompileAndCheck(Handle, Code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool Disposing)
|
|
|
|
{
|
|
|
|
if (Disposing && Handle != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteShader(Handle);
|
|
|
|
|
|
|
|
Handle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct ShaderProgram
|
|
|
|
{
|
|
|
|
public ShaderStage Vertex;
|
|
|
|
public ShaderStage TessControl;
|
|
|
|
public ShaderStage TessEvaluation;
|
|
|
|
public ShaderStage Geometry;
|
|
|
|
public ShaderStage Fragment;
|
|
|
|
}
|
|
|
|
|
2018-06-25 22:10:54 -07:00
|
|
|
const int ConstBuffersPerStage = 18;
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
private ShaderProgram Current;
|
|
|
|
|
|
|
|
private ConcurrentDictionary<long, ShaderStage> Stages;
|
|
|
|
|
|
|
|
private Dictionary<ShaderProgram, int> Programs;
|
|
|
|
|
|
|
|
public int CurrentProgramHandle { get; private set; }
|
|
|
|
|
2018-06-25 22:10:54 -07:00
|
|
|
private OGLStreamBuffer[][] ConstBuffers;
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
public OGLShader()
|
|
|
|
{
|
|
|
|
Stages = new ConcurrentDictionary<long, ShaderStage>();
|
|
|
|
|
|
|
|
Programs = new Dictionary<ShaderProgram, int>();
|
2018-06-25 22:10:54 -07:00
|
|
|
|
|
|
|
ConstBuffers = new OGLStreamBuffer[5][];
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
{
|
|
|
|
ConstBuffers[i] = new OGLStreamBuffer[ConstBuffersPerStage];
|
|
|
|
}
|
2018-04-08 12:17:35 -07:00
|
|
|
}
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
public void Create(IGalMemory Memory, long Key, GalShaderType Type)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-06-27 19:55:08 -07:00
|
|
|
Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, Key, 0, false, Type));
|
2018-04-08 12:17:35 -07:00
|
|
|
}
|
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
public void Create(IGalMemory Memory, long VpAPos, long Key, GalShaderType Type)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-06-27 19:55:08 -07:00
|
|
|
Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, VpAPos, Key, true, Type));
|
|
|
|
}
|
|
|
|
|
|
|
|
private ShaderStage ShaderStageFactory(
|
|
|
|
IGalMemory Memory,
|
|
|
|
long Position,
|
|
|
|
long PositionB,
|
|
|
|
bool IsDualVp,
|
|
|
|
GalShaderType Type)
|
|
|
|
{
|
|
|
|
GlslProgram Program;
|
|
|
|
|
|
|
|
GlslDecompiler Decompiler = new GlslDecompiler();
|
|
|
|
|
|
|
|
if (IsDualVp)
|
|
|
|
{
|
2018-07-18 22:33:27 -07:00
|
|
|
ShaderDumper.Dump(Memory, Position, Type, "a");
|
|
|
|
ShaderDumper.Dump(Memory, PositionB, Type, "b");
|
2018-07-15 15:37:27 -07:00
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
Program = Decompiler.Decompile(
|
|
|
|
Memory,
|
2018-07-18 22:33:27 -07:00
|
|
|
Position,
|
|
|
|
PositionB,
|
2018-06-27 19:55:08 -07:00
|
|
|
Type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-18 22:33:27 -07:00
|
|
|
ShaderDumper.Dump(Memory, Position, Type);
|
2018-07-15 15:37:27 -07:00
|
|
|
|
2018-07-18 22:33:27 -07:00
|
|
|
Program = Decompiler.Decompile(Memory, Position, Type);
|
2018-06-27 19:55:08 -07:00
|
|
|
}
|
2018-04-08 12:17:35 -07:00
|
|
|
|
|
|
|
return new ShaderStage(
|
|
|
|
Type,
|
|
|
|
Program.Code,
|
|
|
|
Program.Textures,
|
|
|
|
Program.Uniforms);
|
|
|
|
}
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Key)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
if (Stages.TryGetValue(Key, out ShaderStage Stage))
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
|
|
|
return Stage.TextureUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Enumerable.Empty<ShaderDeclInfo>();
|
|
|
|
}
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
public void SetConstBuffer(long Key, int Cbuf, byte[] Data)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
if (Stages.TryGetValue(Key, out ShaderStage Stage))
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
|
|
|
foreach (ShaderDeclInfo DeclInfo in Stage.UniformUsage.Where(x => x.Cbuf == Cbuf))
|
|
|
|
{
|
2018-06-25 22:10:54 -07:00
|
|
|
OGLStreamBuffer Buffer = GetConstBuffer(Stage.Type, Cbuf);
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-06-25 22:10:54 -07:00
|
|
|
int Size = Math.Min(Data.Length, Buffer.Size);
|
2018-05-17 11:25:42 -07:00
|
|
|
|
2018-06-25 22:10:54 -07:00
|
|
|
byte[] Destiny = Buffer.Map(Size);
|
2018-05-17 11:25:42 -07:00
|
|
|
|
2018-06-25 22:10:54 -07:00
|
|
|
Array.Copy(Data, Destiny, Size);
|
|
|
|
|
|
|
|
Buffer.Unmap(Size);
|
2018-04-08 12:17:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
public void EnsureTextureBinding(string UniformName, int Value)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
|
|
|
BindProgram();
|
|
|
|
|
|
|
|
int Location = GL.GetUniformLocation(CurrentProgramHandle, UniformName);
|
|
|
|
|
|
|
|
GL.Uniform1(Location, Value);
|
|
|
|
}
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
public void SetFlip(float X, float Y)
|
2018-04-13 20:39:24 -07:00
|
|
|
{
|
|
|
|
BindProgram();
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
int Location = GL.GetUniformLocation(CurrentProgramHandle, GlslDecl.FlipUniformName);
|
2018-04-13 20:39:24 -07:00
|
|
|
|
|
|
|
GL.Uniform2(Location, X, Y);
|
|
|
|
}
|
|
|
|
|
2018-06-23 17:39:25 -07:00
|
|
|
public void Bind(long Key)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-06-23 17:39:25 -07:00
|
|
|
if (Stages.TryGetValue(Key, out ShaderStage Stage))
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
|
|
|
Bind(Stage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Bind(ShaderStage Stage)
|
|
|
|
{
|
2018-07-18 22:33:27 -07:00
|
|
|
if (Stage.Type == GalShaderType.Geometry)
|
|
|
|
{
|
|
|
|
//Enhanced layouts are required for Geometry shaders
|
|
|
|
//skip this stage if current driver has no ARB_enhanced_layouts
|
|
|
|
if (!OGLExtension.HasEnhancedLayouts())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 12:17:35 -07: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 09:08:39 -07: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 12:17:35 -07: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-25 22:10:54 -07:00
|
|
|
BindUniformBlocks(Handle);
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
Programs.Add(Current, Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.UseProgram(Handle);
|
|
|
|
|
2018-06-25 22:10:54 -07:00
|
|
|
BindUniformBuffers(Handle);
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
CurrentProgramHandle = Handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AttachIfNotNull(int ProgramHandle, ShaderStage Stage)
|
|
|
|
{
|
|
|
|
if (Stage != null)
|
|
|
|
{
|
|
|
|
Stage.Compile();
|
|
|
|
|
|
|
|
GL.AttachShader(ProgramHandle, Stage.Handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 22:10:54 -07:00
|
|
|
private void BindUniformBlocks(int ProgramHandle)
|
|
|
|
{
|
|
|
|
int FreeBinding = 0;
|
|
|
|
|
|
|
|
int BindUniformBlocksIfNotNull(ShaderStage Stage)
|
|
|
|
{
|
|
|
|
if (Stage != null)
|
|
|
|
{
|
|
|
|
foreach (ShaderDeclInfo DeclInfo in Stage.UniformUsage)
|
|
|
|
{
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FreeBinding;
|
|
|
|
}
|
|
|
|
|
|
|
|
BindUniformBlocksIfNotNull(Current.Vertex);
|
|
|
|
BindUniformBlocksIfNotNull(Current.TessControl);
|
|
|
|
BindUniformBlocksIfNotNull(Current.TessEvaluation);
|
|
|
|
BindUniformBlocksIfNotNull(Current.Geometry);
|
|
|
|
BindUniformBlocksIfNotNull(Current.Fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void BindUniformBuffers(int ProgramHandle)
|
|
|
|
{
|
|
|
|
int FreeBinding = 0;
|
|
|
|
|
|
|
|
int BindUniformBuffersIfNotNull(ShaderStage Stage)
|
|
|
|
{
|
|
|
|
if (Stage != null)
|
|
|
|
{
|
|
|
|
foreach (ShaderDeclInfo DeclInfo in Stage.UniformUsage)
|
|
|
|
{
|
|
|
|
OGLStreamBuffer Buffer = GetConstBuffer(Stage.Type, DeclInfo.Cbuf);
|
|
|
|
|
|
|
|
GL.BindBufferBase(BufferRangeTarget.UniformBuffer, FreeBinding, Buffer.Handle);
|
|
|
|
|
|
|
|
FreeBinding++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FreeBinding;
|
|
|
|
}
|
2018-06-26 21:32:28 -07:00
|
|
|
|
2018-06-25 22:10:54 -07:00
|
|
|
BindUniformBuffersIfNotNull(Current.Vertex);
|
|
|
|
BindUniformBuffersIfNotNull(Current.TessControl);
|
|
|
|
BindUniformBuffersIfNotNull(Current.TessEvaluation);
|
|
|
|
BindUniformBuffersIfNotNull(Current.Geometry);
|
|
|
|
BindUniformBuffersIfNotNull(Current.Fragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
private OGLStreamBuffer GetConstBuffer(GalShaderType StageType, int Cbuf)
|
|
|
|
{
|
|
|
|
int StageIndex = (int)StageType;
|
|
|
|
|
|
|
|
OGLStreamBuffer Buffer = ConstBuffers[StageIndex][Cbuf];
|
|
|
|
|
|
|
|
if (Buffer == null)
|
|
|
|
{
|
|
|
|
//Allocate a maximum of 64 KiB
|
|
|
|
int Size = Math.Min(GL.GetInteger(GetPName.MaxUniformBlockSize), 64 * 1024);
|
|
|
|
|
|
|
|
Buffer = OGLStreamBuffer.Create(BufferTarget.UniformBuffer, Size);
|
|
|
|
|
|
|
|
ConstBuffers[StageIndex][Cbuf] = Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Buffer;
|
|
|
|
}
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
public static void CompileAndCheck(int Handle, string Code)
|
|
|
|
{
|
|
|
|
GL.ShaderSource(Handle, Code);
|
|
|
|
GL.CompileShader(Handle);
|
|
|
|
|
|
|
|
CheckCompilation(Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void CheckCompilation(int Handle)
|
|
|
|
{
|
|
|
|
int Status = 0;
|
|
|
|
|
|
|
|
GL.GetShader(Handle, ShaderParameter.CompileStatus, out Status);
|
|
|
|
|
|
|
|
if (Status == 0)
|
|
|
|
{
|
|
|
|
throw new ShaderException(GL.GetShaderInfoLog(Handle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|