Skip to content

Commit

Permalink
get gl display method working, at least, minimally, with the scanline…
Browse files Browse the repository at this point in the history
…s shader. someone else can fix up the others if they need to or make me a bug. at least the hard part is past
  • Loading branch information
zeromus committed Jun 20, 2020
1 parent d4cb18a commit 1b4a6f6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
31 changes: 31 additions & 0 deletions Assets/Shaders/BizHawk/BizScanlines.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//Yeah, I'm sorry this uses really old non-generic attributes
//that's just how old this code is; support on ancient graphics cards is helpful

#ifdef VERTEX
uniform mat4 modelViewProj;

void main()
{
gl_Position = modelViewProj * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
}

#endif

#ifdef FRAGMENT

uniform float uIntensity;
uniform sampler2D s_p;

uniform vec2 output_size;

void main()
{
vec2 vTex = gl_TexCoord[0].xy;
vec4 temp = texture2D(s_p,vTex);
vec2 wpos = gl_FragCoord.xy;
if(floor(wpos.y/2.0) != floor(wpos.y)/2.0) temp.rgb *= uIntensity;
gl_FragColor = temp;
}

#endif
16 changes: 16 additions & 0 deletions Assets/Shaders/shaders-readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Despite using the "cgp" derived "retro shader" approach, we're doing things a bit different than retroarch.

A single .cgp file is used for any graphics backend. The shader references inside it can be written extensionless, or with .cg (or with .hlsl or .glsl for reasons, read on)

In case you have shaders that work only GLSL or HLSL -- well, they simply won't work in the wrong mode.
In that case, try something like bizhawkdir/Shaders/myshaders/glsl/mybadshader.cgp
In this case, the .cgp can reference .glsl shaders internally.

An important point, which you will perceive by checking out the bizhawk shaders, is that a .cgp is now portable due to the extension-ignorant behaviour AND the separate .glsl and .hlsl extensions.
(For instance, BizScanlines.cgp+BizScanlines.hlsl+BizScanlines.glsl).
The separate extensions let there be separate shaders for each backend, each referenced by the same cgp which references .cg (arbitrarily; it could be blank)
However if the .cgp referenced a .glsl, it wouldn't work on D3D.

In case you haven't pieced it together yet, this means there is no automatic mechanism for transpiling shaders. It isn't reliable and created an unmaintainable, slow, mess.

Porting shaders from retroarch will require touching them extensively, probably.
63 changes: 12 additions & 51 deletions src/BizHawk.Client.EmuHawk/GraphicsImplementations/IGL_TK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
// etc
// glBindAttribLocation (programID, 0, "vertexPosition_modelspace");

// for future reference: c# tesselators
// http://www.opentk.com/node/437 (AGG#, codes on Tao forums)

using System;
using System.IO;
using System.Collections.Generic;
Expand Down Expand Up @@ -198,62 +195,34 @@ public Pipeline CreatePipeline(VertexLayout vertexLayout, Shader vertexShader, S
var fsw = fragmentShader.Opaque as ShaderWrapper;
var sws = new[] { vsw,fsw };

bool mapVariables = vsw.MapCodeToNative != null || fsw.MapCodeToNative != null;

ErrorCode errcode;
int pid = GL.CreateProgram();
GL.AttachShader(pid, vsw.sid);
errcode = GL.GetError();
GL.AttachShader(pid, fsw.sid);
errcode = GL.GetError();

//NOT BEING USED NOW: USING SEMANTICS INSTEAD
////bind the attribute locations from the vertex layout
////as we go, look for attribute mappings (CGC will happily reorder and rename our attribute mappings)
////what's more it will _RESIZE_ them but this seems benign..somehow..
////WELLLLLLL we wish we could do that by names
////but the shaders don't seem to be adequate quality (oddly named attributes.. texCoord vs texCoord1). need to use semantics instead.
//foreach (var kvp in vertexLayout.Items)
//{
// string name = kvp.Value.Name;
// //if (mapVariables)
// //{
// // foreach (var sw in sws)
// // {
// // if (sw.MapNativeToCode.ContainsKey(name))
// // {
// // name = sw.MapNativeToCode[name];
// // break;
// // }
// // }
// //}

// if(mapVariables) {
// ////proxy for came-from-cgc
// //switch (kvp.Value.Usage)
// //{
// // case AttributeUsage.Position:
// //}
// }

// //GL.BindAttribLocation(pid, kvp.Key, name);
//}

GL.LinkProgram(pid);
errcode = GL.GetError();

string resultLog = GL.GetProgramInfoLog(pid);

if (errcode != ErrorCode.NoError)
{
if (required)
throw new InvalidOperationException($"Error creating pipeline (error returned from glLinkProgram): {errcode}\r\n\r\n{resultLog}");
else success = false;
}

GL.GetProgram(pid, GetProgramParameterName.LinkStatus, out var linkStatus);
if (linkStatus == 0)
{
if (required)
throw new InvalidOperationException($"Error creating pipeline (link status false returned from glLinkProgram): \r\n\r\n{resultLog}");
else success = false;
resultLog = GL.GetProgramInfoLog(pid);
Console.WriteLine(resultLog);
}

//need to work on validation. apparently there are some weird caveats to glValidate which make it complicated and possibly excuses (barely) the intel drivers' dysfunctional operation
//"A sampler points to a texture unit used by fixed function with an incompatible target"
Expand Down Expand Up @@ -304,14 +273,6 @@ public Pipeline CreatePipeline(VertexLayout vertexLayout, Shader vertexShader, S
errcode = GL.GetError();
int loc = GL.GetUniformLocation(pid, name);

//translate name if appropriate
//not sure how effective this approach will be, due to confusion of vertex and fragment uniforms
if (mapVariables)
{
if (vsw.MapCodeToNative.ContainsKey(name)) name = vsw.MapCodeToNative[name];
if (fsw.MapCodeToNative.ContainsKey(name)) name = fsw.MapCodeToNative[name];
}

var ui = new UniformInfo { Name = name, Opaque = loc };

if (type == ActiveUniformType.Sampler2D)
Expand Down Expand Up @@ -425,11 +386,7 @@ public void SetPipelineUniform(PipelineUniform uniform, bool value)

public unsafe void SetPipelineUniformMatrix(PipelineUniform uniform, Matrix4 mat, bool transpose)
{
//GL.UniformMatrix4((int)uniform.Opaque, 1, transpose, (float*)&mat);
GL.Uniform4((int)uniform.Sole.Opaque + 0, 1, (float*)&mat.Row0);
GL.Uniform4((int)uniform.Sole.Opaque + 1, 1, (float*)&mat.Row1);
GL.Uniform4((int)uniform.Sole.Opaque + 2, 1, (float*)&mat.Row2);
GL.Uniform4((int)uniform.Sole.Opaque + 3, 1, (float*)&mat.Row3);
GL.UniformMatrix4((int)uniform.Sole.Opaque, 1, transpose, (float*)&mat);
}

public unsafe void SetPipelineUniformMatrix(PipelineUniform uniform, ref Matrix4 mat, bool transpose)
Expand Down Expand Up @@ -694,18 +651,22 @@ GLControl CastControl(swf.Control swfControl)
Shader CreateShader(ShaderType type, string source, string entry, bool required)
{
var sw = new ShaderWrapper();
string info = "";

int sid = GL.CreateShader(type);
bool ok = CompileShaderSimple(sid, source, required);
if(!ok)
{
GL.GetShaderInfoLog(sid, out info);
GL.DeleteShader(sid);
sid = 0;
}

Shader ret = new Shader(this, sw, ok);
ret.Errors = info;
sw.sid = sid;

return new Shader(this, sw, ok);
return ret;
}

bool CompileShaderSimple(int sid, string source, bool required)
Expand Down

0 comments on commit 1b4a6f6

Please sign in to comment.