summaryrefslogtreecommitdiff
path: root/src/shaders/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/shaders/common')
-rw-r--r--src/shaders/common/core.c30
-rw-r--r--src/shaders/common/core.h72
-rw-r--r--src/shaders/common/light.c0
-rw-r--r--src/shaders/common/light.h0
-rw-r--r--src/shaders/common/mathlib.c138
-rw-r--r--src/shaders/common/mathlib.h63
-rw-r--r--src/shaders/common/shadow.c0
-rw-r--r--src/shaders/common/shadow.h6
8 files changed, 309 insertions, 0 deletions
diff --git a/src/shaders/common/core.c b/src/shaders/common/core.c
new file mode 100644
index 0000000..703458c
--- /dev/null
+++ b/src/shaders/common/core.c
@@ -0,0 +1,30 @@
+#include "core.h"
+
+Vec4 _proj_params;
+Vec2 _time;
+Vec4 _screen_params;
+
+Vec3 unpacknormal(Color32 c32) {
+ Vec3 normal = {
+ c32.r * 2 - 1,
+ c32.g * 2 - 1,
+ c32.b * 2 - 1,
+ };
+ return normal;
+}
+
+Mat4 mat4(Vec4* c1, Vec4* c2, Vec4* c3, Vec4* c4);
+
+Vec2 texsize(Texture* texture) {
+ Vec2 size = {texture->width, texture->height};
+ return size;
+}
+
+float linear01depth(float depth) {
+ float n = _proj_params.x, f = _proj_params.y;
+ return n / ((n-f)*depth + f);
+}
+
+float lineareyedepth(float depth) {
+ return _proj_params.y * linear01depth(depth);
+} \ No newline at end of file
diff --git a/src/shaders/common/core.h b/src/shaders/common/core.h
new file mode 100644
index 0000000..623409e
--- /dev/null
+++ b/src/shaders/common/core.h
@@ -0,0 +1,72 @@
+#ifndef _SOFTSHADEROOM_COMMON_HEADER_H_
+#define _SOFTSHADEROOM_COMMON_HEADER_H_
+#include "../../core/shader.h"
+#include "mathlib.h"
+
+/************************************************************************/
+/* variables */
+/************************************************************************/
+
+#define _model_matrix (uniforms->model)
+#define _view_matrix (uniforms->view)
+#define _proj_matrix (uniforms->projection)
+#define _mvp_matrix (uniforms->mvp)
+#define _it_model_matrix /*inverse-transpose model matrix if needed*/
+
+// near
+// far
+// fov
+// aspect
+Vec4 _proj_params;
+
+// dt
+// duration
+Vec2 _time;
+
+// width
+// height
+// 1 / width
+// 1 / height
+Vec4 _screen_params;
+
+/************************************************************************/
+/* functions */
+/************************************************************************/
+
+/*shader built in functions*/
+Vec3 unpacknormal(Color32 c32);
+
+Mat4 mat4(Vec4* c1, Vec4* c2, Vec4* c3, Vec4* c4);
+Mat3 mat3(Vec3* c1, Vec3* c2, Vec3* c3);
+
+Vec2 texsize(Texture* texture);
+
+#define discardif(cond) \
+do{ \
+if(cond) return 0; \
+}while(0)
+#define discard() return 0
+#define keep() return 1
+
+#define MVP_PROCESS \
+do{ \
+static Vec4 p; p.xyz = in->vertex->position; p.w = 1; \
+internal_mat4_mulvec4(uniforms->mvp, &p, clipcoord); \
+}while(0)
+
+#define object2clip(pos, out) internal_mat4_mulvec4(_mvp_matrix, pos, out);
+
+/*need defined _it_model_matrix of model matrix, i-nverse, t-ranspose*/
+#define object2world_normal(normal, out) \
+internal_mat4_mulvec4(_it_model_matrix, normal, out)
+
+/*take sample from normal map and translate to normal*/
+#define unpack_normal(color, out_v3) \
+out_v3->x = color.x * 2 - 1; \
+out_v3->y = color.y * 2 - 1; \
+out_v3->z = color.z * 2 - 1;
+
+float linear01depth(float depth);
+float lineareyedepth(float depth);
+
+#endif \ No newline at end of file
diff --git a/src/shaders/common/light.c b/src/shaders/common/light.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/shaders/common/light.c
diff --git a/src/shaders/common/light.h b/src/shaders/common/light.h
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/shaders/common/light.h
diff --git a/src/shaders/common/mathlib.c b/src/shaders/common/mathlib.c
new file mode 100644
index 0000000..356357c
--- /dev/null
+++ b/src/shaders/common/mathlib.c
@@ -0,0 +1,138 @@
+#include "mathlib.h"
+#include "../../math/math.h"
+
+#define INTERNAL_FUNCTION(T, NAME)\
+T NAME(T v1, T v2) {\
+ T out; \
+ internal_##NAME(&v1, &v2, &out);\
+ return out;\
+}
+
+/************************************************************************/
+/* Vec2 */
+/************************************************************************/
+Vec2 vec2(float x, float y) {
+ Vec2 v = {x, y};
+ return v;
+}
+
+INTERNAL_FUNCTION(Vec2, vec2_minus);
+INTERNAL_FUNCTION(Vec2, vec2_plus);
+
+float vec2_dot(Vec2 v1, Vec2 v2) {
+ return internal_vec2_dot(&v1, &v2);
+}
+
+/************************************************************************/
+/* Vec3 */
+/************************************************************************/
+Vec3 vec3(float x, float y, float z) {
+ Vec3 v = {x, y, z};
+ return v;
+}
+
+INTERNAL_FUNCTION(Vec3, vec3_minus);
+INTERNAL_FUNCTION(Vec3, vec3_plus);
+INTERNAL_FUNCTION(Vec3, vec3_cross);
+
+float vec3_dot(Vec3 v1, Vec3 v2) {
+ return internal_vec3_dot(&v1, &v2);
+}
+
+Vec3 vec3_lerp(Vec3 v1, Vec3 v2, float t) {
+ Vec3 out;
+ internal_vec3_lerp(&v1, &v2, t, &out);
+ return out;
+}
+
+Vec3 vec3_slerp(Vec3 v1, Vec3 v2, float t) {
+ Vec3 out;
+ internal_vec3_slerp(&v1, &v2, t, &out);
+ return out;
+}
+
+Vec3 vec3_scale(Vec3 v, float scale) {
+ Vec3 out;
+ internal_vec3_scale(&v, scale, &out);
+ return out;
+}
+
+Vec3 vec3_normalize(Vec3 v) {
+ Vec3 out;
+ internal_vec3_normalize(&v, &out);
+ return out;
+}
+
+/************************************************************************/
+/* Vec4 */
+/************************************************************************/
+
+Vec4 vec4(float x, float y, float z, float w) {
+ Vec4 v = {x, y, z, w};
+ return v;
+}
+
+INTERNAL_FUNCTION(Vec4, vec4_minus);
+INTERNAL_FUNCTION(Vec4, vec4_plus);
+
+Vec4 vec4_scale(Vec4 v, float scale) {
+ Vec4 out;
+ internal_vec4_scale(&v, scale, &out);
+ return out;
+}
+
+Vec4 vec4_saturate(Vec4 v) {
+ Vec4 out;
+ out.x = clamp(v.x, 0, 1);
+ out.y = clamp(v.y, 0, 1);
+ out.z = clamp(v.z, 0, 1);
+ out.w = clamp(v.w, 0, 1);
+ return out;
+}
+
+/************************************************************************/
+/* Matrix */
+/************************************************************************/
+
+Mat3 mat3(Vec3 c1, Vec3 c2, Vec3 c3) {
+ Mat3 m = {c1, c2, c3};
+ return m;
+}
+
+Vec3 mat3_mulvec3(Mat3 m, Vec3 v) {
+ Vec3 out;
+ internal_mat3_multvec3(&m, &v, &out);
+ return out;
+}
+
+Mat3 mat3_mulmat3(Mat3 m, Mat3 m2) {
+ Mat3 out;
+ internal_mat3_multmat3(&m, &m2, &out);
+ return out;
+}
+
+Mat4 mat4(Vec4 c1, Vec4 c2, Vec4 c3, Vec4 c4) {
+ Mat4 m ;
+ m.axisx = c1;
+ m.axisy = c2;
+ m.axisz = c3;
+ m.pos = c4;
+ return m;
+}
+
+Vec3 mat4_mulvec3(Mat4 m, Vec3 v) {
+ Vec4 v2 = {v.x, v.y, v.z, 0};
+ return mat4_mulvec4(m, v2).xyz;
+}
+
+Vec4 mat4_mulvec4(Mat4 m, Vec4 v) {
+ Vec4 out;
+ internal_mat4_mulvec4(&m, &v, &out);
+ return out;
+}
+
+Mat4 mat4_mulmat4(Mat4 m1, Mat4 m2) {
+ Mat4 out;
+ internal_mat4_multiply(&m1, &m2, &out);
+ return out;
+}
diff --git a/src/shaders/common/mathlib.h b/src/shaders/common/mathlib.h
new file mode 100644
index 0000000..fcc54ea
--- /dev/null
+++ b/src/shaders/common/mathlib.h
@@ -0,0 +1,63 @@
+#ifndef SSR_SHADER_MATH_H
+#define SSR_SHADER_MATH_H
+
+#include "../../math/structs.h"
+
+/************************************************************************/
+/* constants */
+/************************************************************************/
+
+#define SSR_PI 3.14159265359f
+#define SSR_TWO_PI 6.28318530718f
+#define SSR_FOUR_PI 12.56637061436f
+#define SSR_INV_PI 0.31830988618f
+#define SSR_INV_TWO_PI 0.15915494309f
+#define SSR_INV_FOUR_PI 0.07957747155f
+#define SSR_HALF_PI 1.57079632679f
+#define SSR_INV_HALF_PI 0.636619772367f
+
+/************************************************************************/
+/* Vec3 */
+/************************************************************************/
+Vec2 vec2(float x, float y);
+Vec2 vec2_minus(Vec2 v1, Vec2 v2);
+Vec2 vec2_plus(Vec2 v1, Vec2 v2);
+float vec2_dot(Vec2 v1, Vec2 v2);
+
+/************************************************************************/
+/* Vec3 */
+/************************************************************************/
+Vec3 vec3(float x, float y, float z);
+Vec3 vec3_minus(Vec3 v1, Vec3 v2);
+Vec3 vec3_plus(Vec3 v1, Vec3 v2);
+float vec3_dot(Vec3 v1, Vec3 v2);
+Vec3 vec3_cross(Vec3 v1, Vec3 v2);
+Vec3 vec3_lerp(Vec3 v1, Vec3 v2, float t);
+Vec3 vec3_slerp(Vec3 v1, Vec3 v2, float t);
+Vec3 vec3_scale(Vec3 v, float scale);
+Vec3 vec3_normalize(Vec3 v);
+
+/************************************************************************/
+/* Vec4 */
+/************************************************************************/
+Vec4 vec4(float x, float y, float z, float w);
+Vec4 vec4_minus(Vec4 v1, Vec4 v2);
+Vec4 vec4_plus(Vec4 v1, Vec4 v2);
+Vec4 vec4_scale(Vec4 v, float scale);
+Vec4 vec4_saturate(Vec4 v);
+Vec4 vec4_normalize(Vec4 v);
+
+/************************************************************************/
+/* Matrix */
+/************************************************************************/
+Mat3 mat3(Vec3 colum1, Vec3 colum2, Vec3 colum3); // colum major
+Vec3 mat3_mulvec3(Mat3 m, Vec3 v);
+Mat3 mat3_mulmat3(Mat3 m, Mat3 m2);
+
+Mat4 mat4(Vec4 colum1, Vec4 colum2, Vec4 colum3, Vec4 colum4);
+Vec3 mat4_mulvec3(Mat4 m, Vec3 v);
+Vec4 mat4_mulvec4(Mat4 m, Vec4 v);
+Mat4 mat4_mulmat4(Mat4 m, Mat4 m2);
+
+
+#endif \ No newline at end of file
diff --git a/src/shaders/common/shadow.c b/src/shaders/common/shadow.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/shaders/common/shadow.c
diff --git a/src/shaders/common/shadow.h b/src/shaders/common/shadow.h
new file mode 100644
index 0000000..2fef82d
--- /dev/null
+++ b/src/shaders/common/shadow.h
@@ -0,0 +1,6 @@
+#ifndef _SSR_SHADOW_H_
+#define _SSR_SHADOW_H_
+
+
+
+#endif \ No newline at end of file