// File: /workspace/github_c_makefile_star2_O1/jonof[P]jftekwar/jfbuild/src/glbuild.c
// Function: glbuild_prepare_8bit_shader
int glbuild_prepare_8bit_shader(glbuild8bit *state, int resx, int resy, int stride, int winx, int winy)
{
	GLuint shaders[2] = {0,0}, prog = 0;

	extern const char default_glbuild_fs_glsl[];
	extern const char default_glbuild_vs_glsl[];

	int tsizx = stride, tsizy = resy;

	state->resx = resx;
	state->resy = resy;
	state->winx = state->winy = 0;
	state->tx = (float)resx / (float)stride;
	state->ty = 1.0;
	if (!glinfo.texnpot) {
		for (tsizx = 1; tsizx < stride; tsizx <<= 1) { }
		for (tsizy = 1; tsizy < resy; tsizy <<= 1) { }
		state->tx = (float)resx / (float)tsizx;
		state->ty = (float)resy / (float)tsizy;
	}

	GLushort indexes[6] = { 0, 1, 2, 0, 2, 3 };
	GLint clamp = glinfo.clamptoedge ? GL_CLAMP_TO_EDGE : GL_CLAMP;
	GLenum intfmt, extfmt;

	// Initialise texture objects for the palette and framebuffer.
	// The textures will be uploaded on the first rendered frame.
	glfunc.glGenTextures(1, &state->paltex);
	glfunc.glActiveTexture(GL_TEXTURE1);
	glfunc.glBindTexture(GL_TEXTURE_2D, state->paltex);
	glfunc.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);	// Allocates memory.
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);

#if (USE_OPENGL == USE_GLES2)
	intfmt = GL_LUMINANCE;
	extfmt = GL_LUMINANCE;
#else
	intfmt = GL_RGB;
	extfmt = GL_RED;
#endif

	glfunc.glGenTextures(1, &state->frametex);
	glfunc.glActiveTexture(GL_TEXTURE0);
	glfunc.glBindTexture(GL_TEXTURE_2D, state->frametex);
	glfunc.glTexImage2D(GL_TEXTURE_2D, 0, intfmt, tsizx, tsizy, 0, extfmt, GL_UNSIGNED_BYTE, NULL);	// Allocates memory.
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
	glfunc.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);

	glfunc.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	// Prepare the vertex and fragment shaders.
	shaders[0] = glbuild_compile_shader(GL_VERTEX_SHADER, default_glbuild_vs_glsl);
	shaders[1] = glbuild_compile_shader(GL_FRAGMENT_SHADER, default_glbuild_fs_glsl);
	if (!shaders[0] || !shaders[1]) {
		if (shaders[0]) glfunc.glDeleteShader(shaders[0]);
		if (shaders[1]) glfunc.glDeleteShader(shaders[1]);
		return -1;
	}

	// Prepare the shader program.
	prog = glbuild_link_program(2, shaders);
	if (!prog) {
		glfunc.glDeleteShader(shaders[0]);
		glfunc.glDeleteShader(shaders[1]);
		return -1;
	}

	// Shaders were detached in glbuild_link_program.
	glfunc.glDeleteShader(shaders[0]);
	glfunc.glDeleteShader(shaders[1]);

	// Connect the textures to the program.
	glfunc.glUseProgram(prog);
	glfunc.glUniform1i(glfunc.glGetUniformLocation(prog, "u_palette"), 1);
	glfunc.glUniform1i(glfunc.glGetUniformLocation(prog, "u_frame"), 0);

	state->program = prog;
	state->attrib_vertex = glfunc.glGetAttribLocation(prog, "a_vertex");
	state->attrib_texcoord = glfunc.glGetAttribLocation(prog, "a_texcoord");
	state->unif_gamma = glfunc.glGetUniformLocation(prog, "u_gamma");

#if (USE_OPENGL == USE_GL3)
	glfunc.glGenVertexArrays(1, &state->vao);
	glfunc.glBindVertexArray(state->vao);
#endif

	// Prepare buffer objects for the vertex/texcoords and indexes.
	glfunc.glGenBuffers(1, &state->buffer_indexes);
	glfunc.glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->buffer_indexes);
	glfunc.glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, GL_STATIC_DRAW);

	glfunc.glGenBuffers(1, &state->buffer_elements);
	glbuild_update_window_size(state, winx, winy);

	glfunc.glEnableVertexAttribArray(state->attrib_texcoord);
	glfunc.glVertexAttribPointer(state->attrib_texcoord, 2, GL_FLOAT, GL_FALSE,
		sizeof(GLfloat)*4, (const GLvoid *)0);

    glfunc.glEnableVertexAttribArray(state->attrib_vertex);
	glfunc.glVertexAttribPointer(state->attrib_vertex, 2, GL_FLOAT, GL_FALSE,
		sizeof(GLfloat)*4, (const GLvoid *)(sizeof(GLfloat)*2));

#if (USE_OPENGL == USE_GL3)
    glfunc.glBindVertexArray(0);
#endif

	// Set some persistent GL state.
	glfunc.glClearColor(0.0, 0.0, 0.0, 0.0);
	glfunc.glDisable(GL_DEPTH_TEST);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, 0x14(%rsp)
movl %r8d, 0x10(%rsp)
movq %rdi, %rbx
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movl %esi, 0x24(%rdi)
cvtsi2ss %esi, %xmm0
movl %edx, 0x28(%rdi)
cvtsi2ss %ecx, %xmm1
movq %rax, 0x2c(%rdi)
movaps %xmm0, %xmm2
divss %xmm1, %xmm2
movss %xmm2, 0x34(%rdi)
movl $0x3f800000, 0x38(%rdi) # imm = 0x3F800000
leaq 0x1302ca(%rip), %rax    # 0x17d250
cmpb $0x0, 0x1d(%rax)
je 0x4cf94
movl %edx, %r13d
movl %ecx, %r15d
jmp 0x4cfe0
movl $0x1, %esi
movl %esi, %r15d
leal (%r15,%r15), %esi
cmpl %ecx, %r15d
jl 0x4cf99
movl $0x1, %ecx
movl %ecx, %r13d
leal (,%r13,2), %ecx
cmpl %edx, %r13d
jl 0x4cfaa
xorps %xmm1, %xmm1
cvtsi2ss %r15d, %xmm1
xorps %xmm2, %xmm2
cvtsi2ss %edx, %xmm2
cvtsi2ss %r13d, %xmm3
divss %xmm1, %xmm0
movss %xmm0, 0x34(%rbx)
divss %xmm3, %xmm2
movss %xmm2, 0x38(%rbx)
movabsq $0x200010000, %rcx      # imm = 0x200010000
movq %rcx, 0x18(%rsp)
movl $0x30002, 0x20(%rsp)    # imm = 0x30002
cmpb $0x0, 0x19(%rax)
movl $0x2900, %eax           # imm = 0x2900
movl $0x812f, %r12d          # imm = 0x812F
cmovel %eax, %r12d
leaq 0x4(%rbx), %rsi
movl $0x1, %edi
callq *0x5ac7df(%rip)         # 0x5f97f8
movl $0x84c1, %edi           # imm = 0x84C1
callq *0x5ac85c(%rip)         # 0x5f9880
movl 0x4(%rbx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *0x5ac7d6(%rip)         # 0x5f9808
subq $0x8, %rsp
xorl %r14d, %r14d
movl $0x1401, %ebp           # imm = 0x1401
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl $0x1908, %edx           # imm = 0x1908
movl $0x100, %ecx            # imm = 0x100
movl $0x1, %r8d
xorl %r9d, %r9d
pushq %r14
pushq %rbp
pushq $0x1908                 # imm = 0x1908
callq *0x5ac7aa(%rip)         # 0x5f9810
addq $0x20, %rsp
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ac7a9(%rip)         # 0x5f9828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ac794(%rip)         # 0x5f9828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl %r12d, %edx
callq *0x5ac781(%rip)         # 0x5f9828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl %r12d, %edx
callq *0x5ac76e(%rip)         # 0x5f9828
leaq 0x8(%rbx), %rsi
movl $0x1, %edi
callq *0x5ac72f(%rip)         # 0x5f97f8
movl $0x84c0, %edi           # imm = 0x84C0
callq *0x5ac7ac(%rip)         # 0x5f9880
movl 0x8(%rbx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *0x5ac726(%rip)         # 0x5f9808
subq $0x8, %rsp
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl $0x1907, %edx           # imm = 0x1907
movl %r15d, %ecx
movl %r13d, %r8d
xorl %r9d, %r9d
pushq %r14
pushq %rbp
pushq $0x1903                 # imm = 0x1903
callq *0x5ac707(%rip)         # 0x5f9810
addq $0x20, %rsp
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ac706(%rip)         # 0x5f9828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ac6f1(%rip)         # 0x5f9828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl %r12d, %edx
callq *0x5ac6de(%rip)         # 0x5f9828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl %r12d, %edx
callq *0x5ac6cb(%rip)         # 0x5f9828
movl $0xcf5, %edi            # imm = 0xCF5
movl $0x1, %esi
callq *0x5ac64b(%rip)         # 0x5f97b8
leaq 0x2bfac(%rip), %rsi     # 0x79120
movl $0x8b31, %edi           # imm = 0x8B31
callq 0x4cc97
movl %eax, %r15d
movl %eax, 0x8(%rsp)
leaq 0x2bcc4(%rip), %rsi     # 0x78e50
movl $0x8b30, %edi           # imm = 0x8B30
callq 0x4cc97
movl %eax, %r13d
movl %eax, 0xc(%rsp)
testl %r15d, %r15d
setne %al
testl %r13d, %r13d
setne %cl
testb %cl, %al
je 0x4d32d
leaq 0x8(%rsp), %rsi
movl $0x2, %edi
callq 0x4ce49
movl %eax, %r12d
movl %r15d, %edi
callq *0x5ac6e4(%rip)         # 0x5f98b0
movl %r13d, %edi
callq *0x5ac6db(%rip)         # 0x5f98b0
testl %r12d, %r12d
je 0x4d354
movl %r12d, %edi
callq *0x5ac749(%rip)         # 0x5f9930
movq 0x5ac712(%rip), %r14    # 0x5f9900
leaq 0x2b83c(%rip), %rsi     # 0x78a31
movl %r12d, %edi
callq *0x5ac6ea(%rip)         # 0x5f98e8
movl %eax, %edi
movl $0x1, %esi
callq *%r14
movq 0x5ac6f1(%rip), %r14    # 0x5f9900
leaq 0x2b825(%rip), %rsi     # 0x78a3b
movl %r12d, %edi
callq *0x5ac6c9(%rip)         # 0x5f98e8
xorl %r15d, %r15d
movl %eax, %edi
xorl %esi, %esi
callq *%r14
movl %r12d, (%rbx)
leaq 0x2b810(%rip), %rsi     # 0x78a43
movl %r12d, %edi
callq *0x5ac684(%rip)         # 0x5f98c0
movl %eax, 0x10(%rbx)
leaq 0x2b806(%rip), %rsi     # 0x78a4c
movl %r12d, %edi
callq *0x5ac671(%rip)         # 0x5f98c0
movl %eax, 0x14(%rbx)
leaq 0x2b7fe(%rip), %rsi     # 0x78a57
movl %r12d, %edi
callq *0x5ac686(%rip)         # 0x5f98e8
movl %eax, 0x20(%rbx)
leaq 0x18(%rbx), %rsi
movl $0x1, %edi
callq *0x5ac5e4(%rip)         # 0x5f9858
movl 0x18(%rbx), %esi
movl $0x8893, %edi           # imm = 0x8893
callq *0x5ac5b6(%rip)         # 0x5f9838
leaq 0x18(%rsp), %rdx
movl $0xc, %esi
movl $0x8893, %edi           # imm = 0x8893
movl $0x88e4, %ecx           # imm = 0x88E4
callq *0x5ac5a4(%rip)         # 0x5f9840
leaq 0x1c(%rbx), %rsi
movl $0x1, %edi
callq *0x5ac5ad(%rip)         # 0x5f9858
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movl 0x14(%rsp), %edx
callq 0x4d36c
movl 0x14(%rbx), %edi
callq *0x5ac5a4(%rip)         # 0x5f9868
movl 0x14(%rbx), %edi
movl $0x2, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl $0x10, %r8d
xorl %r9d, %r9d
callq *0x5ac596(%rip)         # 0x5f9878
movl 0x10(%rbx), %edi
callq *0x5ac57d(%rip)         # 0x5f9868
movl 0x10(%rbx), %edi
movl $0x8, %r9d
movl $0x2, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl $0x10, %r8d
callq *0x5ac56c(%rip)         # 0x5f9878
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
callq *0x5ac422(%rip)         # 0x5f9740
movl $0xb71, %r13d           # imm = 0xB71
leaq 0x5ac45d(%rip), %rax    # 0x5f9788
jmp 0x4d34d
testl %r15d, %r15d
je 0x4d33b
movl %r15d, %edi
callq *0x5ac575(%rip)         # 0x5f98b0
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %r13d, %r13d
je 0x4d35a
leaq 0x5ac563(%rip), %rax    # 0x5f98b0
movl %r13d, %edi
callq *(%rax)
jmp 0x4d35a
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq