2018-04-08 20:17:35 +01: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 05:09:40 +01:00
|
|
|
class OGLShader : IGalShader
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-08-10 05:09:40 +01:00
|
|
|
public OGLShaderProgram Current;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
private ConcurrentDictionary<long, OGLShaderStage> Stages;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
private Dictionary<OGLShaderProgram, int> Programs;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
|
|
|
public int CurrentProgramHandle { get; private set; }
|
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
private OGLConstBuffer Buffer;
|
2018-06-26 06:10:54 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
private int ExtraUboHandle;
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
public OGLShader(OGLConstBuffer Buffer)
|
|
|
|
{
|
|
|
|
this.Buffer = Buffer;
|
2018-06-26 06:10:54 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
Stages = new ConcurrentDictionary<long, OGLShaderStage>();
|
2018-06-26 06:10:54 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
Programs = new Dictionary<OGLShaderProgram, int>();
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
|
2018-06-24 01:39:25 +01:00
|
|
|
public void Create(IGalMemory Memory, long Key, GalShaderType Type)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-06-28 03:55:08 +01:00
|
|
|
Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, Key, 0, false, Type));
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 03:55:08 +01:00
|
|
|
public void Create(IGalMemory Memory, long VpAPos, long Key, GalShaderType Type)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-06-28 03:55:08 +01:00
|
|
|
Stages.GetOrAdd(Key, (Stage) => ShaderStageFactory(Memory, VpAPos, Key, true, Type));
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
private OGLShaderStage ShaderStageFactory(
|
2018-06-28 03:55:08 +01:00
|
|
|
IGalMemory Memory,
|
|
|
|
long Position,
|
|
|
|
long PositionB,
|
|
|
|
bool IsDualVp,
|
|
|
|
GalShaderType Type)
|
|
|
|
{
|
|
|
|
GlslProgram Program;
|
|
|
|
|
|
|
|
GlslDecompiler Decompiler = new GlslDecompiler();
|
|
|
|
|
|
|
|
if (IsDualVp)
|
|
|
|
{
|
2018-07-19 06:33:27 +01:00
|
|
|
ShaderDumper.Dump(Memory, Position, Type, "a");
|
|
|
|
ShaderDumper.Dump(Memory, PositionB, Type, "b");
|
2018-07-15 23:37:27 +01:00
|
|
|
|
2018-06-28 03:55:08 +01:00
|
|
|
Program = Decompiler.Decompile(
|
|
|
|
Memory,
|
2018-07-19 06:33:27 +01:00
|
|
|
Position,
|
|
|
|
PositionB,
|
2018-06-28 03:55:08 +01:00
|
|
|
Type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-19 06:33:27 +01:00
|
|
|
ShaderDumper.Dump(Memory, Position, Type);
|
2018-07-15 23:37:27 +01:00
|
|
|
|
2018-07-19 06:33:27 +01:00
|
|
|
Program = Decompiler.Decompile(Memory, Position, Type);
|
2018-06-28 03:55:08 +01:00
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
return new OGLShaderStage(
|
2018-04-08 20:17:35 +01:00
|
|
|
Type,
|
|
|
|
Program.Code,
|
2018-08-15 19:59:51 +01:00
|
|
|
Program.Uniforms,
|
|
|
|
Program.Textures);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<ShaderDeclInfo> GetConstBufferUsage(long Key)
|
|
|
|
{
|
|
|
|
if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
|
|
|
|
{
|
|
|
|
return Stage.ConstBufferUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Enumerable.Empty<ShaderDeclInfo>();
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
|
2018-06-24 01:39:25 +01:00
|
|
|
public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Key)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-08-10 05:09:40 +01:00
|
|
|
if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
|
|
|
return Stage.TextureUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Enumerable.Empty<ShaderDeclInfo>();
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
public unsafe void SetFlip(float X, float Y)
|
2018-04-14 04:39:24 +01:00
|
|
|
{
|
|
|
|
BindProgram();
|
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
EnsureExtraBlock();
|
|
|
|
|
|
|
|
GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
|
|
|
|
|
|
|
|
float* Data = stackalloc float[4];
|
|
|
|
Data[0] = X;
|
|
|
|
Data[1] = Y;
|
2018-04-14 04:39:24 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
//Invalidate buffer
|
|
|
|
GL.BufferData(BufferTarget.UniformBuffer, 4 * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
|
|
|
|
|
|
|
|
GL.BufferSubData(BufferTarget.UniformBuffer, IntPtr.Zero, 4 * sizeof(float), (IntPtr)Data);
|
2018-04-14 04:39:24 +01:00
|
|
|
}
|
|
|
|
|
2018-06-24 01:39:25 +01:00
|
|
|
public void Bind(long Key)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-08-10 05:09:40 +01:00
|
|
|
if (Stages.TryGetValue(Key, out OGLShaderStage Stage))
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
|
|
|
Bind(Stage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
private void Bind(OGLShaderStage Stage)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
2018-07-19 06:33:27 +01: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 20:17:35 +01: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 17:08:39 +01: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 20:17:35 +01: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 06:10:54 +01:00
|
|
|
BindUniformBlocks(Handle);
|
2018-08-23 05:54:32 +01:00
|
|
|
BindTextureLocations(Handle);
|
2018-06-26 06:10:54 +01:00
|
|
|
|
2018-04-08 20:17:35 +01:00
|
|
|
Programs.Add(Current, Handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.UseProgram(Handle);
|
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
CurrentProgramHandle = Handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EnsureExtraBlock()
|
|
|
|
{
|
|
|
|
if (ExtraUboHandle == 0)
|
2018-07-26 17:49:29 +01:00
|
|
|
{
|
2018-08-10 05:09:40 +01:00
|
|
|
ExtraUboHandle = GL.GenBuffer();
|
2018-06-26 06:10:54 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
GL.BindBuffer(BufferTarget.UniformBuffer, ExtraUboHandle);
|
|
|
|
|
|
|
|
GL.BufferData(BufferTarget.UniformBuffer, 4 * sizeof(float), IntPtr.Zero, BufferUsageHint.StreamDraw);
|
|
|
|
|
|
|
|
GL.BindBufferBase(BufferRangeTarget.UniformBuffer, 0, ExtraUboHandle);
|
|
|
|
}
|
2018-04-08 20:17:35 +01:00
|
|
|
}
|
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
private void AttachIfNotNull(int ProgramHandle, OGLShaderStage Stage)
|
2018-04-08 20:17:35 +01:00
|
|
|
{
|
|
|
|
if (Stage != null)
|
|
|
|
{
|
|
|
|
Stage.Compile();
|
|
|
|
|
|
|
|
GL.AttachShader(ProgramHandle, Stage.Handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 06:10:54 +01:00
|
|
|
private void BindUniformBlocks(int ProgramHandle)
|
|
|
|
{
|
2018-08-10 05:09:40 +01:00
|
|
|
int ExtraBlockindex = GL.GetUniformBlockIndex(ProgramHandle, GlslDecl.ExtraUniformBlockName);
|
|
|
|
|
|
|
|
GL.UniformBlockBinding(ProgramHandle, ExtraBlockindex, 0);
|
|
|
|
|
|
|
|
//First index is reserved
|
|
|
|
int FreeBinding = 1;
|
2018-06-26 06:10:54 +01:00
|
|
|
|
2018-08-10 05:09:40 +01:00
|
|
|
void BindUniformBlocksIfNotNull(OGLShaderStage Stage)
|
2018-06-26 06:10:54 +01:00
|
|
|
{
|
|
|
|
if (Stage != null)
|
|
|
|
{
|
2018-08-15 19:59:51 +01:00
|
|
|
foreach (ShaderDeclInfo DeclInfo in Stage.ConstBufferUsage)
|
2018-06-26 06:10:54 +01: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 05:54:32 +01: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 20:17:35 +01: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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|