Commit 0ff4087d authored by pcy's avatar pcy
Browse files

IT WORKS, FUCK YEAH

parent 12a42e47
......@@ -223,6 +223,16 @@ namespace PoroCYon.FNAGLSL {
void ApplyUnifs() {
// TODO
IntPtr data = unifDataBacking;
for (int i = 0; i < unifs.Length; ++i) {
var u = unifs[i];
//var p = Parameters[i];
SetUniform(ref u, data);
data = (IntPtr)((long)data + u.bytesize);
}
}
// TODO: find out a better (i.e. more API-conform) way to do this!
......
......@@ -152,6 +152,193 @@ namespace PoroCYon.FNAGLSL {
return 0;
}
unsafe static void SetUniform(ref UnifData u, IntPtr data) {
int loc = u.location;
void* d = (void*)data;
float* fd = (float*)d;
int* id = (int*)d;
uint* ud = (uint*)d;
Matrix* m = (Matrix*)d;
switch (u.type) {
case FLOAT:
/*if (u.size == 1) GL.Uniform1f(loc, *fd);
else*/ GL.Uniform1fv(loc, u.size, fd);
break;
case FLOAT_VEC2:
/*if (u.size == 1) GL.Uniform2f(loc, fd[0], fd[1]);
else*/ GL.Uniform2fv(loc, u.size, fd);
break;
case FLOAT_VEC3:
/*if (u.size == 1) GL.Uniform3f(loc, fd[0], fd[1], fd[2]);
else*/ GL.Uniform3fv(loc, u.size, fd);
break;
case FLOAT_VEC4:
/*if (u.size == 1) GL.Uniform4f(loc, fd[0], fd[1], fd[2], fd[3]);
else*/ GL.Uniform4fv(loc, u.size, fd);
break;
case INT: case BOOL:
GL.Uniform1iv(loc, u.size, id);
break;
case INT_VEC2: case BOOL_VEC2:
GL.Uniform2iv(loc, u.size, id);
break;
case INT_VEC3: case BOOL_VEC3:
GL.Uniform3iv(loc, u.size, id);
break;
case INT_VEC4: case BOOL_VEC4:
GL.Uniform4iv(loc, u.size, id);
break;
case UNSIGNED_INT:
GL.Uniform1uiv(loc, u.size, ud);
break;
case UNSIGNED_INT_VEC2:
GL.Uniform2uiv(loc, u.size, ud);
break;
case UNSIGNED_INT_VEC3:
GL.Uniform3uiv(loc, u.size, ud);
break;
case UNSIGNED_INT_VEC4:
GL.Uniform4uiv(loc, u.size, ud);
break;
// NOTE: matrices layout:
// Matrix.Mxy: row x, column y
// FLOAT_MATXxY: X cols, Y rows => X row-vals, Y col-vals
// transpose=false: column-major (column per column)
// transpose=true : row-major (row per row)
// FIXME: are these actually passed in the correct order? better check.
case FLOAT_MAT2: {
if (u.size != 1) throw new NotSupportedException("Matrix array");
float* m22 = stackalloc float[2*2];
m22[0*2+0] = m->M11;
m22[0*2+1] = m->M12;
m22[1*2+0] = m->M21;
m22[1*2+1] = m->M22;
GL.UniformMatrix2fv(loc, 1, GL.FALSE, m22);
} break;
case FLOAT_MAT3: {
if (u.size != 1) throw new NotSupportedException("Matrix array");
float* m33 = stackalloc float[3*3];
m33[0*3+0] = m->M11;
m33[0*3+1] = m->M12;
m33[0*3+2] = m->M13;
m33[1*3+0] = m->M21;
m33[1*3+1] = m->M22;
m33[1*3+2] = m->M23;
m33[2*3+0] = m->M31;
m33[2*3+1] = m->M32;
m33[2*3+2] = m->M33;
GL.UniformMatrix3fv(loc, 1, GL.FALSE, m33);
} break;
case FLOAT_MAT4: {
if (u.size != 1) throw new NotSupportedException("Matrix array");
float* m44 = stackalloc float[4*4];
m44[0*4+0] = m->M11;
m44[0*4+1] = m->M12;
m44[0*4+2] = m->M13;
m44[0*4+3] = m->M14;
m44[1*4+0] = m->M21;
m44[1*4+1] = m->M22;
m44[1*4+2] = m->M23;
m44[1*4+3] = m->M24;
m44[2*4+0] = m->M31;
m44[2*4+1] = m->M32;
m44[2*4+2] = m->M33;
m44[2*4+3] = m->M34;
m44[3*4+0] = m->M41;
m44[3*4+1] = m->M42;
m44[3*4+2] = m->M43;
m44[3*4+3] = m->M44;
GL.UniformMatrix4fv(loc, 1, GL.FALSE, m44);
} break;
case FLOAT_MAT2x3: { // 2 cols, 3 rows -> 2 rowvals, 3 colvals
if (u.size != 1) throw new NotSupportedException("Matrix array");
float* m23 = stackalloc float[2*3];
m23[0*3+0] = m->M11;
m23[0*3+1] = m->M12;
m23[0*3+2] = m->M13;
m23[1*3+0] = m->M21;
m23[1*3+1] = m->M22;
m23[1*3+2] = m->M23;
GL.UniformMatrix2x3fv(loc, 1, GL.FALSE, m23);
} break;
case FLOAT_MAT2x4: { // 2 cols, 4 rows -> 2 rowvals, 3 colvals
if (u.size != 1) throw new NotSupportedException("Matrix array");
float* m24 = stackalloc float[2*4];
m24[0*4+0] = m->M11;
m24[0*4+1] = m->M12;
m24[0*4+2] = m->M13;
m24[0*4+3] = m->M14;
m24[1*4+0] = m->M21;
m24[1*4+1] = m->M22;
m24[1*4+2] = m->M23;
m24[1*4+3] = m->M24;
GL.UniformMatrix2x4fv(loc, 1, GL.FALSE, m24);
} break;
case FLOAT_MAT3x2: { // 3 cols, 2 rows -> 2 rowvals, 3 colvals
if (u.size != 1) throw new NotSupportedException("Matrix array");
float* m32 = stackalloc float[3*2];
m32[0*2+0] = m->M11;
m32[0*2+1] = m->M12;
m32[1*2+0] = m->M21;
m32[1*2+1] = m->M22;
m32[2*2+0] = m->M31;
m32[2*2+1] = m->M32;
GL.UniformMatrix3x2fv(loc, 1, GL.FALSE, m32);
} break;
case FLOAT_MAT3x4: { // 3 cols, 4 rows -> 3 rowvals, 4 colvals
if (u.size != 1) throw new NotSupportedException("Matrix array");
float* m34 = stackalloc float[3*4];
m34[0*4+0] = m->M11;
m34[0*4+1] = m->M12;
m34[0*4+2] = m->M13;
m34[0*4+3] = m->M14;
m34[1*4+0] = m->M21;
m34[1*4+1] = m->M22;
m34[1*4+2] = m->M23;
m34[1*4+3] = m->M24;
m34[2*4+0] = m->M31;
m34[2*4+1] = m->M32;
m34[2*4+2] = m->M33;
m34[2*4+3] = m->M34;
GL.UniformMatrix3x4fv(loc, 1, GL.FALSE, m34);
} break;
case FLOAT_MAT4x2: { // 4 cols, 2 rows -> 4 rowvals, 2 colvals
if (u.size != 1) throw new NotSupportedException("Matrix array");
float* m42 = stackalloc float[4*2];
m42[0*2+0] = m->M11;
m42[0*2+1] = m->M12;
m42[1*2+0] = m->M21;
m42[1*2+1] = m->M22;
m42[2*2+0] = m->M31;
m42[2*2+1] = m->M32;
m42[3*2+0] = m->M41;
m42[3*2+1] = m->M42;
GL.UniformMatrix4x2fv(loc, 1, GL.FALSE, m42);
} break;
case FLOAT_MAT4x3: { // 4 cols, 3 rows -> 4 rowvals, 3 colvals
if (u.size != 1) throw new NotSupportedException("Matrix array");
float* m43 = stackalloc float[4*3];
m43[0*3+0] = m->M11;
m43[0*3+1] = m->M12;
m43[0*3+2] = m->M13;
m43[1*3+0] = m->M21;
m43[1*3+1] = m->M22;
m43[1*3+2] = m->M23;
m43[2*3+0] = m->M31;
m43[2*3+1] = m->M32;
m43[2*3+2] = m->M33;
m43[3*3+0] = m->M41;
m43[3*3+1] = m->M42;
m43[3*3+2] = m->M43;
GL.UniformMatrix4x3fv(loc, 1, GL.FALSE, m43);
} break;
default:
throw new NotSupportedException(u.type+"");
}
}
}
}
......@@ -34,6 +34,8 @@ namespace PoroCYon.FNAGLSL {
UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER = 0x8A46,
UNIFORM_BLOCK_REFERENCED_BY_TESS_CONTROL_SHADER = 0x84F0,
UNIFORM_BLOCK_REFERENCED_BY_TESS_EVALUATION_SHADER = 0x84F1;
public const byte FALSE = 0, TRUE = 1;
}
}
......@@ -69,6 +69,19 @@ namespace PoroCYon.FNAGLSL {
_UniformMatrix4x2fv = GetProcAddress<UniformMatrix4x2fv_T>("UniformMatrix4x2fv");
_UniformMatrix3x4fv = GetProcAddress<UniformMatrix3x4fv_T>("UniformMatrix3x4fv");
_UniformMatrix4x3fv = GetProcAddress<UniformMatrix4x3fv_T>("UniformMatrix4x3fv");
_Uniform1f = GetProcAddress<Uniform1f_T>("Uniform1f");
_Uniform2f = GetProcAddress<Uniform2f_T>("Uniform2f");
_Uniform3f = GetProcAddress<Uniform3f_T>("Uniform3f");
_Uniform4f = GetProcAddress<Uniform4f_T>("Uniform4f");
_Uniform1i = GetProcAddress<Uniform1i_T>("Uniform1i");
_Uniform2i = GetProcAddress<Uniform2i_T>("Uniform2i");
_Uniform3i = GetProcAddress<Uniform3i_T>("Uniform3i");
_Uniform4i = GetProcAddress<Uniform4i_T>("Uniform4i");
_Uniform1ui = GetProcAddress<Uniform1ui_T>("Uniform1ui");
_Uniform2ui = GetProcAddress<Uniform2ui_T>("Uniform2ui");
_Uniform3ui = GetProcAddress<Uniform3ui_T>("Uniform3ui");
_Uniform4ui = GetProcAddress<Uniform4ui_T>("Uniform4ui");
}
static void DeinitProcs() {
_GetError = null;
......@@ -116,6 +129,19 @@ namespace PoroCYon.FNAGLSL {
_UniformMatrix4x2fv = null;
_UniformMatrix3x4fv = null;
_UniformMatrix4x3fv = null;
_Uniform1f = null;
_Uniform2f = null;
_Uniform3f = null;
_Uniform4f = null;
_Uniform1i = null;
_Uniform2i = null;
_Uniform3i = null;
_Uniform4i = null;
_Uniform1ui = null;
_Uniform2ui = null;
_Uniform3ui = null;
_Uniform4ui = null;
}
delegate GLenum GetError_T();
......@@ -486,6 +512,79 @@ namespace PoroCYon.FNAGLSL {
public static void UniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, GLfloat* value) {
_UniformMatrix4x3fv(location, count, transpose, value);
}
delegate void Uniform1f_T(GLint location, GLfloat v0);
static Uniform1f_T _Uniform1f;
public static void Uniform1f(GLint location, GLfloat v0) {
_Uniform1f(location, v0);
}
delegate void Uniform2f_T(GLint location, GLfloat v0, GLfloat v1);
static Uniform2f_T _Uniform2f;
public static void Uniform2f(GLint location, GLfloat v0, GLfloat v1) {
_Uniform2f(location, v0, v1);
}
delegate void Uniform3f_T(GLint location, GLfloat v0, GLfloat v1, GLfloat v2);
static Uniform3f_T _Uniform3f;
public static void Uniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2) {
_Uniform3f(location, v0, v1, v2);
}
delegate void Uniform4f_T(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
static Uniform4f_T _Uniform4f;
public static void Uniform4f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) {
_Uniform4f(location, v0, v1, v2, v3);
}
delegate void Uniform1i_T(GLint location, GLint v0);
static Uniform1i_T _Uniform1i;
public static void Uniform1i(GLint location, GLint v0) {
_Uniform1i(location, v0);
}
delegate void Uniform2i_T(GLint location, GLint v0, GLint v1);
static Uniform2i_T _Uniform2i;
public static void Uniform2i(GLint location, GLint v0, GLint v1) {
_Uniform2i(location, v0, v1);
}
delegate void Uniform3i_T(GLint location, GLint v0, GLint v1, GLint v2);
static Uniform3i_T _Uniform3i;
public static void Uniform3i(GLint location, GLint v0, GLint v1, GLint v2) {
_Uniform3i(location, v0, v1, v2);
}
delegate void Uniform4i_T(GLint location, GLint v0, GLint v1, GLint v2, GLint v3);
static Uniform4i_T _Uniform4i;
public static void Uniform4i(GLint location, GLint v0, GLint v1, GLint v2, GLint v3) {
_Uniform4i(location, v0, v1, v2, v3);
}
delegate void Uniform1ui_T(GLint location, GLuint v0);
static Uniform1ui_T _Uniform1ui;
public static void Uniform1ui(GLint location, GLuint v0) {
_Uniform1ui(location, v0);
}
delegate void Uniform2ui_T(GLint location, GLuint v0, GLuint v1);
static Uniform2ui_T _Uniform2ui;
public static void Uniform2ui(GLint location, GLuint v0, GLuint v1) {
_Uniform2ui(location, v0, v1);
}
delegate void Uniform3ui_T(GLint location, GLuint v0, GLuint v1, GLuint v2);
static Uniform3ui_T _Uniform3ui;
public static void Uniform3ui(GLint location, GLuint v0, GLuint v1, GLuint v2) {
_Uniform3ui(location, v0, v1, v2);
}
delegate void Uniform4ui_T(GLint location, GLuint v0, GLuint v1, GLuint v2, GLuint v3);
static Uniform4ui_T _Uniform4ui;
public static void Uniform4ui(GLint location, GLuint v0, GLuint v1, GLuint v2, GLuint v3) {
_Uniform4ui(location, v0, v1, v2, v3);
}
}
}
......@@ -40,12 +40,28 @@ class TestGame : Game {
gdm = new GraphicsDeviceManager(this);
}
// thanks Ralgor
void Resize(Object s, EventArgs ea) {
Window.ClientSizeChanged -= Resize;
gdm.PreferredBackBufferWidth = Window.ClientBounds.Width ;
gdm.PreferredBackBufferHeight = Window.ClientBounds.Height;
gdm.ApplyChanges();
Window.ClientSizeChanged += Resize;
}
protected override void Initialize() {
IsMouseVisible = true;
Window.AllowUserResizing = true;
Window.Title = "FNA GLSL hax";
gdm.SynchronizeWithVerticalRetrace = true;
gdm.ApplyChanges();
base.Initialize();
Window.ClientSizeChanged += Resize;
}
protected override void LoadContent() {
......@@ -158,12 +174,14 @@ class TestGame : Game {
Console.WriteLine("param[" + i + "] = " + p.Name);
++i;
}*/
var vp = GraphicsDevice.Viewport;
glsl.Parameters["fGlobalTime"].SetValue((float)totalTime.TotalSeconds);
glsl.Parameters["v2Resolution"].SetValue(new Vector2(1920, 1080));
glsl.Parameters["v2Resolution"].SetValue(new Vector2(vp.Width, vp.Height));
using (var act = glsl.Bind()) {
GraphicsDevice.DrawUserPrimitives<VertexType>(
PrimitiveType.TriangleStrip, vertices_br, 0, 2, VertexType.Format);
PrimitiveType.TriangleStrip, vertices_fs, 0, 2, VertexType.Format);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment