// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %rbx
movl %esi, 0x24(%rdi)
movl %edx, 0x28(%rdi)
andq $0x0, 0x2c(%rdi)
movl %r9d, 0x1c(%rsp)
cvtsi2ss %esi, %xmm0
cvtsi2ss %ecx, %xmm1
movl %r8d, 0x18(%rsp)
movaps %xmm0, %xmm2
divss %xmm1, %xmm2
movss %xmm2, 0x34(%rdi)
movl $0x3f800000, 0x38(%rdi) # imm = 0x3F800000
leaq 0x13131a(%rip), %rax    # 0x177250
cmpb $0x0, 0x1d(%rax)
je 0x45f44
movl %edx, %r12d
movl %ecx, %r15d
jmp 0x45f81
pushq $0x1
popq %rsi
movl %esi, %r15d
leal (%r15,%r15), %esi
cmpl %ecx, %r15d
jl 0x45f47
pushq $0x1
popq %rcx
movl %ecx, %r12d
leal (%r12,%r12), %ecx
cmpl %edx, %r12d
jl 0x45f56
movd %r12d, %xmm1
movd %r15d, %xmm2
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
cvtsi2ss %edx, %xmm1
cvtdq2ps %xmm2, %xmm2
unpcklps %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
divps %xmm2, %xmm0
movlps %xmm0, 0x34(%rbx)
movabsq $0x200010000, %rcx      # imm = 0x200010000
movq %rcx, 0x28(%rsp)
movl $0x30002, 0x30(%rsp)    # imm = 0x30002
cmpb $0x0, 0x19(%rax)
movl $0x2900, %eax           # imm = 0x2900
movl $0x812f, %r13d          # imm = 0x812F
cmovel %eax, %r13d
leaq 0x4(%rbx), %rsi
pushq $0x1
popq %rbp
movl %ebp, %edi
callq *0x5ad83e(%rip)         # 0x5f37f8
movl $0x84c1, %edi           # imm = 0x84C1
callq *0x5ad8bb(%rip)         # 0x5f3880
movl 0x4(%rbx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *0x5ad835(%rip)         # 0x5f3808
movl $0x1401, %r14d          # imm = 0x1401
andq $0x0, 0x10(%rsp)
movl %r14d, 0x8(%rsp)
movl $0x1908, (%rsp)         # imm = 0x1908
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl $0x1908, %edx           # imm = 0x1908
movl $0x100, %ecx            # imm = 0x100
movl %ebp, %r8d
xorl %r9d, %r9d
callq *0x5ad808(%rip)         # 0x5f3810
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ad80b(%rip)         # 0x5f3828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ad7f6(%rip)         # 0x5f3828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl %r13d, %edx
callq *0x5ad7e3(%rip)         # 0x5f3828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl %r13d, %edx
callq *0x5ad7d0(%rip)         # 0x5f3828
leaq 0x8(%rbx), %rsi
movl %ebp, %edi
callq *0x5ad794(%rip)         # 0x5f37f8
movl $0x84c0, %edi           # imm = 0x84C0
callq *0x5ad811(%rip)         # 0x5f3880
movl 0x8(%rbx), %esi
movl $0xde1, %edi            # imm = 0xDE1
callq *0x5ad78b(%rip)         # 0x5f3808
andq $0x0, 0x10(%rsp)
movl %r14d, 0x8(%rsp)
movl $0x1903, (%rsp)         # imm = 0x1903
movl $0xde1, %edi            # imm = 0xDE1
xorl %esi, %esi
movl $0x1907, %edx           # imm = 0x1907
movl %r15d, %ecx
movl %r12d, %r8d
xorl %r9d, %r9d
callq *0x5ad766(%rip)         # 0x5f3810
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2800, %esi           # imm = 0x2800
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ad769(%rip)         # 0x5f3828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2801, %esi           # imm = 0x2801
movl $0x2600, %edx           # imm = 0x2600
callq *0x5ad754(%rip)         # 0x5f3828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2802, %esi           # imm = 0x2802
movl %r13d, %edx
callq *0x5ad741(%rip)         # 0x5f3828
movl $0xde1, %edi            # imm = 0xDE1
movl $0x2803, %esi           # imm = 0x2803
movl %r13d, %edx
callq *0x5ad72e(%rip)         # 0x5f3828
movl $0xcf5, %edi            # imm = 0xCF5
movl %ebp, %esi
callq *0x5ad6b1(%rip)         # 0x5f37b8
leaq 0x29072(%rip), %rsi     # 0x6f180
movl $0x8b31, %edi           # imm = 0x8B31
callq 0x45c74
movl %eax, %r15d
movl %eax, 0x20(%rsp)
leaq 0x28d8a(%rip), %rsi     # 0x6eeb0
movl $0x8b30, %edi           # imm = 0x8B30
callq 0x45c74
movl %eax, %ebp
movl %eax, 0x24(%rsp)
testl %r15d, %r15d
setne %al
testl %ebp, %ebp
setne %cl
testb %cl, %al
je 0x462b1
pushq $0x2
popq %rdi
leaq 0x20(%rsp), %rsi
callq 0x45e09
movl %eax, %r12d
movl %r15d, %edi
callq *0x5ad74e(%rip)         # 0x5f38b0
movl %ebp, %edi
callq *0x5ad746(%rip)         # 0x5f38b0
testl %r12d, %r12d
je 0x462d1
movl %r12d, %edi
callq *0x5ad7b4(%rip)         # 0x5f3930
movq 0x5ad77d(%rip), %r14    # 0x5f3900
leaq 0x28907(%rip), %rsi     # 0x6ea91
movl %r12d, %edi
callq *0x5ad755(%rip)         # 0x5f38e8
pushq $0x1
popq %rbp
movl %eax, %edi
movl %ebp, %esi
callq *%r14
movq 0x5ad75c(%rip), %r14    # 0x5f3900
leaq 0x288f0(%rip), %rsi     # 0x6ea9b
movl %r12d, %edi
callq *0x5ad734(%rip)         # 0x5f38e8
xorl %r15d, %r15d
movl %eax, %edi
xorl %esi, %esi
callq *%r14
movl %r12d, (%rbx)
leaq 0x288db(%rip), %rsi     # 0x6eaa3
movl %r12d, %edi
callq *0x5ad6ef(%rip)         # 0x5f38c0
movl %eax, 0x10(%rbx)
leaq 0x288d1(%rip), %rsi     # 0x6eaac
movl %r12d, %edi
callq *0x5ad6dc(%rip)         # 0x5f38c0
movl %eax, 0x14(%rbx)
leaq 0x288c9(%rip), %rsi     # 0x6eab7
movl %r12d, %edi
callq *0x5ad6f1(%rip)         # 0x5f38e8
movl %eax, 0x20(%rbx)
leaq 0x18(%rbx), %rsi
movl %ebp, %edi
callq *0x5ad652(%rip)         # 0x5f3858
movl 0x18(%rbx), %esi
movl $0x8893, %edi           # imm = 0x8893
callq *0x5ad624(%rip)         # 0x5f3838
pushq $0xc
popq %rsi
leaq 0x28(%rsp), %rdx
movl $0x8893, %edi           # imm = 0x8893
movl $0x88e4, %ecx           # imm = 0x88E4
callq *0x5ad614(%rip)         # 0x5f3840
leaq 0x1c(%rbx), %rsi
movl %ebp, %edi
callq *0x5ad620(%rip)         # 0x5f3858
movq %rbx, %rdi
movl 0x18(%rsp), %esi
movl 0x1c(%rsp), %edx
callq 0x462e7
movl 0x14(%rbx), %edi
callq *0x5ad617(%rip)         # 0x5f3868
movl 0x14(%rbx), %edi
pushq $0x2
popq %rbp
pushq $0x10
popq %r14
movl %ebp, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl %r14d, %r8d
xorl %r9d, %r9d
callq *0x5ad608(%rip)         # 0x5f3878
movl 0x10(%rbx), %edi
callq *0x5ad5ef(%rip)         # 0x5f3868
movl 0x10(%rbx), %edi
pushq $0x8
popq %r9
movl %ebp, %esi
movl $0x1406, %edx           # imm = 0x1406
xorl %ecx, %ecx
movl %r14d, %r8d
callq *0x5ad5e6(%rip)         # 0x5f3878
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
xorps %xmm3, %xmm3
callq *0x5ad49c(%rip)         # 0x5f3740
movl $0xb71, %edi            # imm = 0xB71
callq *0x5ad4d9(%rip)         # 0x5f3788
jmp 0x462d5
testl %r15d, %r15d
je 0x462bf
movl %r15d, %edi
callq *0x5ad5f1(%rip)         # 0x5f38b0
pushq $-0x1
popq %r15
testl %ebp, %ebp
je 0x462d5
movl %ebp, %edi
callq *0x5ad5e1(%rip)         # 0x5f38b0
jmp 0x462d5
pushq $-0x1
popq %r15
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq