#include "shaders.h"
#include "error.h"

struct sw_shaderprogram sw_shaders_copy;
struct sw_shaderprogram sw_shaders_scale;

static char *vert_copy = "#version 300 es\n\
precision highp float;\
in vec2 pos;\
out vec2 out_tex_coord;\
void main() {\
	gl_Position = vec4(pos, 0.0f, 1.0f);\
	out_tex_coord = vec2(pos.x * 0.5 + 0.5, pos.y * 0.5 + 0.5);\
}";

static char *frag_copy = "#version 300 es\n\
precision highp float;\
in vec2 out_tex_coord;\
uniform sampler2D tex;\
out vec4 color;\
void main() {\
	vec4 col = texture(tex, out_tex_coord);\
if(col.a > 0.0) {\
	color = col;\
}\
}";

static char *frag_scale = "#version 300 es\n\
precision highp float;\
in vec2 out_tex_coord;\
uniform sampler2D tex;\
uniform float scale;\
uniform vec2 offset;\
uniform vec2 size_ratio;\
out vec4 color;\
void main() {\
	vec2 tc = (out_tex_coord-offset)*size_ratio;\
	vec4 col = vec4(0,0,0,0);\
	if(tc.x <= 1.0 && tc.y <= 1.0 && tc.x >= 0.0 && tc.y >= 0.0) {\
		col = texture(tex, tc);\
	}\
	if(col.a > 0.0) {\
		color = col;\
	} else { \
		discard;\
	}\
}";

void sw_shaders_static_init() {
	sw_log("initialising shaders");
	sw_shaders_copy = sw_shaderprogram_create(vert_copy, frag_copy);
	sw_shaders_scale = sw_shaderprogram_create(vert_copy, frag_scale);
}
