opengl-program: Allow loading from file
This commit is contained in:
		@@ -16,6 +16,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include <shimatta-opengl-program.h>
 | 
					#include <shimatta-opengl-program.h>
 | 
				
			||||||
#include <epoxy/gl.h>
 | 
					#include <epoxy/gl.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct _ShimattaOpenglProgram {
 | 
					struct _ShimattaOpenglProgram {
 | 
				
			||||||
	GObject super;
 | 
						GObject super;
 | 
				
			||||||
@@ -149,12 +151,44 @@ gboolean shimatta_opengl_program_is_compiled(ShimattaOpenglProgram *program)
 | 
				
			|||||||
	return priv->compiled;
 | 
						return priv->compiled;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static char *load_shader_from_source_file(const char *src_file)
 | 
					/**
 | 
				
			||||||
 | 
					 * @brief This function loads the entire content of a file into a zero-terminated string
 | 
				
			||||||
 | 
					 * @param src_file Path to the source file
 | 
				
			||||||
 | 
					 * @return Newly created buffer containing file content. NULL in case of an error.
 | 
				
			||||||
 | 
					 * @note The returend buffer has to be deleted by the caller using g_free()
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static gchar *load_shader_from_source_file(const char *src_file)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						FILE *fil;
 | 
				
			||||||
 | 
						long int end_position;
 | 
				
			||||||
 | 
						gchar *file_content;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!src_file)
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fil = fopen(src_file, "rb");
 | 
				
			||||||
 | 
						if (!fil) {
 | 
				
			||||||
 | 
							g_warning("Shader source file not found: %s", src_file);
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int compile_shader(const char *src, char *error_text, size_t error_text_size, gboolean use_error,
 | 
						/* Get length of file */
 | 
				
			||||||
 | 
						fseek(fil, 0, SEEK_END);
 | 
				
			||||||
 | 
						end_position = ftell(fil);
 | 
				
			||||||
 | 
						fseek(fil, 0, SEEK_SET);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Allocate memory for file content */
 | 
				
			||||||
 | 
						file_content = (gchar *)g_malloc(end_position + 1);
 | 
				
			||||||
 | 
						fread(file_content, 1, end_position, fil);
 | 
				
			||||||
 | 
						fclose(fil);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Append null terminator */
 | 
				
			||||||
 | 
						file_content[end_position] = '\0';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return file_content;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int compile_shader(const gchar *src, char *error_text, size_t error_text_size, gboolean use_error,
 | 
				
			||||||
			     GLuint shader_type, GLuint *shader_id_out)
 | 
								     GLuint shader_type, GLuint *shader_id_out)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	GLuint shader_id;
 | 
						GLuint shader_id;
 | 
				
			||||||
@@ -186,7 +220,7 @@ int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	gboolean use_error = FALSE;
 | 
						gboolean use_error = FALSE;
 | 
				
			||||||
	ShimattaOpenglProgramPrivate *priv;
 | 
						ShimattaOpenglProgramPrivate *priv;
 | 
				
			||||||
	char *shader_source_code;
 | 
						gchar *shader_source_code;
 | 
				
			||||||
	int ret_val = 0;
 | 
						int ret_val = 0;
 | 
				
			||||||
	int status;
 | 
						int status;
 | 
				
			||||||
	GLint success;
 | 
						GLint success;
 | 
				
			||||||
@@ -224,7 +258,7 @@ int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_
 | 
				
			|||||||
		status = compile_shader(shader_source_code, error_text, error_text_size, use_error, GL_VERTEX_SHADER,
 | 
							status = compile_shader(shader_source_code, error_text, error_text_size, use_error, GL_VERTEX_SHADER,
 | 
				
			||||||
					&vertex_shader);
 | 
										&vertex_shader);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		free(shader_source_code);
 | 
							g_free(shader_source_code);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (status) {
 | 
							if (status) {
 | 
				
			||||||
			ret_val = -2;
 | 
								ret_val = -2;
 | 
				
			||||||
@@ -251,7 +285,7 @@ int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_
 | 
				
			|||||||
		status = compile_shader(shader_source_code, error_text, error_text_size, use_error, GL_FRAGMENT_SHADER,
 | 
							status = compile_shader(shader_source_code, error_text, error_text_size, use_error, GL_FRAGMENT_SHADER,
 | 
				
			||||||
					&fragment_shader);
 | 
										&fragment_shader);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		free(shader_source_code);
 | 
							g_free(shader_source_code);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (status) {
 | 
							if (status) {
 | 
				
			||||||
			ret_val = -2;
 | 
								ret_val = -2;
 | 
				
			||||||
@@ -278,7 +312,7 @@ int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_
 | 
				
			|||||||
		status = compile_shader(shader_source_code, error_text, error_text_size, use_error, GL_GEOMETRY_SHADER,
 | 
							status = compile_shader(shader_source_code, error_text, error_text_size, use_error, GL_GEOMETRY_SHADER,
 | 
				
			||||||
					&geometry_shader);
 | 
										&geometry_shader);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		free(shader_source_code);
 | 
							g_free(shader_source_code);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (status) {
 | 
							if (status) {
 | 
				
			||||||
			ret_val = -2;
 | 
								ret_val = -2;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user