<commit_before>int backend_compile_shader(GLuint shader_id, unsigned char** shader_source){
	GLint result = GL_FALSE;
	int log_length;
	char* log = NULL;

	glShaderSource(shader_id, 1, (const GLchar**)shader_source, NULL);
	glCompileShader(shader_id);

	glGetShaderiv(shader_id, GL_COMPILE_STATUS, &result);
	if(result != GL_TRUE){
		fprintf(stderr, "Failed to compile shader\n");
		return 1;
	}

	glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &log_length);
	if(log_length > 1){ //contrary to the docs, this returns a single newline character upon success (https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetShaderiv.xml)
		log = calloc(log_length + 1, sizeof(char));
		glGetShaderInfoLog(shader_id, log_length, NULL, log);
		fprintf(stderr, "Shader compiler error: %s\n", log);
		free(log);
		return 1;
	}
	return 0;
}

GLuint backend_compile_program(unsigned char* vertex_source, unsigned char* fragment_source){
	//create two shader handles and a program handle
	GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
	GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
	GLuint program_id = glCreateProgram();

	//compile the shaders
	if(backend_compile_shader(vertex_shader, &vertex_source) 
			|| backend_compile_shader(fragment_shader, &fragment_source)){
		return 0;
	}

	//attach to program
	glAttachShader(program_id, vertex_shader);
	glAttachShader(program_id, fragment_shader);
	glLinkProgram(program_id);

	glDetachShader(program_id, vertex_shader);
	glDetachShader(program_id, fragment_shader);

	glDeleteShader(vertex_shader);
	glDeleteShader(fragment_shader);
	return program_id;
}


int backend_init(XRESOURCES* res, CONFIG* config){
	int major = 0, minor = 0;
	GLenum glew_err;
	//target quad
	GLfloat target_quad[] = {
		-1, -1,
		1, -1,
		-1, 1,
		1, 1
	};

	//create gl context
	res->gl_context = glXCreateContext(res->display, &(res->visual_info), NULL, True);
	if(!(res->gl_context)){
		fprintf(stderr, "Failed to create GL context\n");
		return -1;
	}

	glXMakeContextCurrent(res->display, res->main, res->main, res->gl_context);
	glXQueryVersion(res->display, &major, &minor);
	fprintf(stderr, "OpenGL version %d.%d\n", major, minor);

	//glewExperimental = GL_TRUE;
	glew_err = glewInit();
	if(glew_err != GLEW_OK){
		fprintf(stderr, "Failed to initialize glew: %s\n", glewGetErrorString(glew_err));
		return -1;
	}

	printf("OpenGL implementation: %s\n", glGetString(GL_VERSION));

	glClearColor(0.f, 0.f, 0.f, 1.f);
    	glClear(GL_COLOR_BUFFER_BIT);

    	//Create VAO and ignore it
    	GLuint VertexArrayID;
    	glGenVertexArrays(1, &VertexArrayID);
    	glBindVertexArray(VertexArrayID);

	//enable some features
    	glEnable(GL_TEXTURE_2D);
    	glEnable(GL_MULTISAMPLE);
    	glEnable(GL_BLEND);
    	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);

	//Target texture for render
	glActiveTexture(GL_TEXTURE0);
	glGenTextures(1, &res->fbo_texture);
	glBindTexture( GL_TEXTURE_2D, res->fbo_texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, res->window_width, res->window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0L);

	//RENDER TO TEXTURE
	//Generate Framebuffer and attach Renderbuffer
	glGenFramebuffers(1, &res->fboID);
	glBindFramebuffer(GL_FRAMEBUFFER, res->fboID);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, res->fbo_texture, 0);

	glGenRenderbuffers(1, &res->rbo_depth);
	glBindRenderbuffer(GL_RENDERBUFFER, res->rbo_depth);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, res->window_width, res->window_height);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, res->rbo_depth);

	GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
	glDrawBuffers(1, DrawBuffers);

	glBindFramebuffer(GL_FRAMEBUFFER , 0);

	glGenBuffers(1, &res->fbo_vbo_ID);
	glBindBuffer(GL_ARRAY_BUFFER, res->fbo_vbo_ID);
	glBufferData(GL_ARRAY_BUFFER, sizeof(target_quad), target_quad, GL_STATIC_DRAW);
	//We only use one Arraybuffer, so no need to unbind it.
	//glBindBuffer(GL_ARRAY_BUFFER, 0);

	//GOBO Texture
	glGenTextures(1, &res->gobo_texture_ID);
	glBindTexture(GL_TEXTURE_2D, res->gobo_texture_ID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	//GOBO DATA TO GPU
	//glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, res->gobo[0].width, res->gobo[0].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, res->gobo[0].data );
	glBindTexture(GL_TEXTURE_2D, 0);

	//Get Program ID for Gauss Filter
	res->fbo_program_ID = backend_compile_program(filter_vertex_shader, filter_fragment_shader);
	res->fbo_program_texture_sampler = glGetUniformLocation(res->fbo_program_ID, "textureSampler");
	//res->fbo_program_filter = glGetUniformLocation( res->fbo_program_ID, "filter");
	res->fbo_program_attribute = glGetAttribLocation(res->fbo_program_ID, "vertexCoord");

	//Get Program ID for Gobo
	res->gobo_last = 10;
	res->gobo_program_ID = backend_compile_program(gobo_vertex_shader, gobo_fragment_shader);
	res->gobo_program_texture_sampler = glGetUniformLocation(res->gobo_program_ID, "textureSampler");
	res->gobo_modelview_ID = glGetUniformLocation(res->gobo_program_ID, "modelview");
	res->gobo_program_colormod = glGetUniformLocation(res->gobo_program_ID, "colormod");
	res->gobo_program_attribute = glGetAttribLocation(res->gobo_program_ID, "vertexCoord");
	return 0;
}

int xlaser_reconfigure(XRESOURCES* xres){
	glBindRenderbuffer( GL_RENDERBUFFER, xres->rbo_depth);
	glBindTexture(GL_TEXTURE_2D, xres->fbo_texture);
	glBindFramebuffer(GL_FRAMEBUFFER, xres->fboID);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xres->window_width, xres->window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0L);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xres->window_width, xres->window_height);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, xres->rbo_depth);
	GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
	glDrawBuffers(1, DrawBuffers);
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	return 0;
}
<commit_msg>Pass shader length to compiler<commit_after>int backend_compile_shader(GLuint shader_id, unsigned char** shader_source, ssize_t shader_length){
	GLint result = GL_FALSE;
	int log_length;
	char* log = NULL;

	glShaderSource(shader_id, 1, (const GLchar**)shader_source, (const GLint*)&shader_length);
	glCompileShader(shader_id);

	glGetShaderiv(shader_id, GL_COMPILE_STATUS, &result);
	if(result != GL_TRUE){
		fprintf(stderr, "Failed to compile shader\n");
		return 1;
	}

	glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &log_length);
	if(log_length > 1){ //contrary to the docs, this returns a single newline character upon success (https://www.khronos.org/opengles/sdk/docs/man/xhtml/glGetShaderiv.xml)
		log = calloc(log_length + 1, sizeof(char));
		glGetShaderInfoLog(shader_id, log_length, NULL, log);
		fprintf(stderr, "Shader compiler error: %s\n", log);
		free(log);
		return 1;
	}
	return 0;
}

GLuint backend_compile_program(unsigned char* vertex_source, ssize_t vertex_length, unsigned char* fragment_source, ssize_t fragment_length){
	//create two shader handles and a program handle
	GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
	GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
	GLuint program_id = glCreateProgram();

	//compile the shaders
	if(backend_compile_shader(vertex_shader, &vertex_source, vertex_length)
			|| backend_compile_shader(fragment_shader, &fragment_source, fragment_length)){
		return 0;
	}

	//attach to program
	glAttachShader(program_id, vertex_shader);
	glAttachShader(program_id, fragment_shader);
	glLinkProgram(program_id);

	glDetachShader(program_id, vertex_shader);
	glDetachShader(program_id, fragment_shader);

	glDeleteShader(vertex_shader);
	glDeleteShader(fragment_shader);
	return program_id;
}


int backend_init(XRESOURCES* res, CONFIG* config){
	int major = 0, minor = 0;
	GLenum glew_err;
	//target quad
	GLfloat target_quad[] = {
		-1, -1,
		1, -1,
		-1, 1,
		1, 1
	};

	//create gl context
	res->gl_context = glXCreateContext(res->display, &(res->visual_info), NULL, True);
	if(!(res->gl_context)){
		fprintf(stderr, "Failed to create GL context\n");
		return -1;
	}

	glXMakeContextCurrent(res->display, res->main, res->main, res->gl_context);
	glXQueryVersion(res->display, &major, &minor);
	fprintf(stderr, "OpenGL version %d.%d\n", major, minor);

	//glewExperimental = GL_TRUE;
	glew_err = glewInit();
	if(glew_err != GLEW_OK){
		fprintf(stderr, "Failed to initialize glew: %s\n", glewGetErrorString(glew_err));
		return -1;
	}

	printf("OpenGL implementation: %s\n", glGetString(GL_VERSION));

	glClearColor(0.f, 0.f, 0.f, 1.f);
    	glClear(GL_COLOR_BUFFER_BIT);

    	//Create VAO and ignore it
    	GLuint VertexArrayID;
    	glGenVertexArrays(1, &VertexArrayID);
    	glBindVertexArray(VertexArrayID);

	//enable some features
    	glEnable(GL_TEXTURE_2D);
    	glEnable(GL_MULTISAMPLE);
    	glEnable(GL_BLEND);
    	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);

	//Target texture for render
	glActiveTexture(GL_TEXTURE0);
	glGenTextures(1, &res->fbo_texture);
	glBindTexture( GL_TEXTURE_2D, res->fbo_texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, res->window_width, res->window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0L);

	//RENDER TO TEXTURE
	//Generate Framebuffer and attach Renderbuffer
	glGenFramebuffers(1, &res->fboID);
	glBindFramebuffer(GL_FRAMEBUFFER, res->fboID);
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, res->fbo_texture, 0);

	glGenRenderbuffers(1, &res->rbo_depth);
	glBindRenderbuffer(GL_RENDERBUFFER, res->rbo_depth);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, res->window_width, res->window_height);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, res->rbo_depth);

	GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
	glDrawBuffers(1, DrawBuffers);

	glBindFramebuffer(GL_FRAMEBUFFER , 0);

	glGenBuffers(1, &res->fbo_vbo_ID);
	glBindBuffer(GL_ARRAY_BUFFER, res->fbo_vbo_ID);
	glBufferData(GL_ARRAY_BUFFER, sizeof(target_quad), target_quad, GL_STATIC_DRAW);
	//We only use one Arraybuffer, so no need to unbind it.
	//glBindBuffer(GL_ARRAY_BUFFER, 0);

	//GOBO Texture
	glGenTextures(1, &res->gobo_texture_ID);
	glBindTexture(GL_TEXTURE_2D, res->gobo_texture_ID);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	//GOBO DATA TO GPU
	//glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, res->gobo[0].width, res->gobo[0].height, 0, GL_RGBA, GL_UNSIGNED_BYTE, res->gobo[0].data );
	glBindTexture(GL_TEXTURE_2D, 0);

	//Get Program ID for Gauss Filter
	res->fbo_program_ID = backend_compile_program(filter_vertex_shader, filter_vertex_shader_len, filter_fragment_shader, filter_fragment_shader_len);
	res->fbo_program_texture_sampler = glGetUniformLocation(res->fbo_program_ID, "textureSampler");
	//res->fbo_program_filter = glGetUniformLocation( res->fbo_program_ID, "filter");
	res->fbo_program_attribute = glGetAttribLocation(res->fbo_program_ID, "vertexCoord");

	//Get Program ID for Gobo
	res->gobo_last = 10;
	res->gobo_program_ID = backend_compile_program(gobo_vertex_shader, gobo_vertex_shader_len, gobo_fragment_shader, gobo_fragment_shader_len);
	res->gobo_program_texture_sampler = glGetUniformLocation(res->gobo_program_ID, "textureSampler");
	res->gobo_modelview_ID = glGetUniformLocation(res->gobo_program_ID, "modelview");
	res->gobo_program_colormod = glGetUniformLocation(res->gobo_program_ID, "colormod");
	res->gobo_program_attribute = glGetAttribLocation(res->gobo_program_ID, "vertexCoord");
	return 0;
}

int xlaser_reconfigure(XRESOURCES* xres){
	glBindRenderbuffer( GL_RENDERBUFFER, xres->rbo_depth);
	glBindTexture(GL_TEXTURE_2D, xres->fbo_texture);
	glBindFramebuffer(GL_FRAMEBUFFER, xres->fboID);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xres->window_width, xres->window_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0L);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, xres->window_width, xres->window_height);
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, xres->rbo_depth);
	GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
	glDrawBuffers(1, DrawBuffers);
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	return 0;
}

{'fraction_non_alphanumeric': 0.11549253052248426, 'fraction_numerical': 0.011829832410707515, 'mean_word_length': 7.13571869216533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 111, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1726312', 'n_tokens_mistral': 5248, 'n_tokens_neox': 5036, 'n_words': 851}