Skip to content

Commit d5424e7

Browse files
committed
feat: load shader
1 parent e546875 commit d5424e7

10 files changed

Lines changed: 301 additions & 120 deletions

File tree

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"version.h": "c",
1313
"gusl-log.h": "c",
1414
"ranges": "c",
15-
"cstdlib": "c"
15+
"cstdlib": "c",
16+
"format": "c",
17+
"glsl_parser.h": "c",
18+
"fstream": "c",
19+
"istream": "c"
1620
}
1721
}

src/editor-window.c

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
#include <glib/gi18n.h>
3232

3333
#include "editor-window.h"
34+
#include "glsl_parser.h"
3435
#include "gusl-log.h"
36+
#include "gusl-utils.h"
3537
#include "shader_source.h"
3638

3739
struct _EditorWindow
@@ -43,13 +45,41 @@ struct _EditorWindow
4345
G_DEFINE_TYPE (EditorWindow, editor_window, ADW_TYPE_APPLICATION_WINDOW)
4446

4547

46-
void editor_setup (gchar *path);
48+
void editor_setup (gchar *path, gpointer user_data);
49+
void error_cb (const char *str, int lineno, int first_col, int last_col, void *user_data);
50+
void on_shader_load (ShaderSource *src, void *user_data);
4751

4852
void
49-
editor_setup (gchar *path)
53+
editor_setup (gchar *path, void *user_data)
5054
{
55+
int ret;
56+
if ((ret = load_shader_source_async ((const char *)path, on_shader_load, user_data)) < 0)
57+
{
58+
g_error ("Could not load shader file.\n");
59+
return;
60+
}
61+
}
5162

52-
GFile *file = g_file_new_for_path (path);
63+
void
64+
on_shader_load (ShaderSource *src, void *user_data)
65+
{
66+
GtkWindow *win = GTK_WINDOW (user_data);
67+
g_debug ("count: %d, path: %s\n", src->content.count, src->path);
68+
for (int i = 0; i < src->content.count; i++)
69+
{
70+
struct glsl_parse_context parse_ctx;
71+
ShaderFile content = src->content.files[i];
72+
glsl_parse_context_init (&parse_ctx);
73+
glsl_parse_set_error_cb (&parse_ctx, error_cb, (void *)win);
74+
75+
if (glsl_parse_string (&parse_ctx, content.content))
76+
{
77+
}
78+
else
79+
{
80+
g_error ("Could not parse shader: %s\n", content.name);
81+
}
82+
}
5383
}
5484

5585
static void
@@ -89,11 +119,26 @@ editor_window_init (EditorWindow *self)
89119
}
90120

91121
GtkWidget *
92-
editor_window_new (GtkApplication *application, gpointer *user_data)
122+
editor_window_new (GtkApplication *application, gpointer user_data)
93123
{
94124
g_assert (GTK_IS_APPLICATION (application));
95-
return g_object_new (EDITOR_TYPE_WINDOW,
96-
"application",
97-
application,
98-
NULL);
125+
126+
gpointer win = g_object_new (EDITOR_TYPE_WINDOW,
127+
"application",
128+
application,
129+
NULL);
130+
editor_setup ((gchar *)user_data, win);
131+
132+
return win;
133+
}
134+
135+
void
136+
error_cb (const char *str, int lineno, int first_col, int last_col, void *user_data)
137+
{
138+
GtkWindow *win = GTK_WINDOW (user_data);
139+
g_debug ("GLSL parse error line %d(%d-%d): %s\n", lineno, first_col, last_col, str);
140+
141+
AdwMessageDialog *dialog = adw_message_dialog_new_ok (win, _ ("Failed to load"), NULL, NULL);
142+
adw_message_dialog_format_body (dialog, _ ("GLSL parse error line %d(%d-%d): %s"), lineno, first_col, last_col, str);
143+
gtk_window_present (GTK_WINDOW (dialog));
99144
}

src/editor-window.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ G_BEGIN_DECLS
1010

1111
G_DECLARE_FINAL_TYPE (EditorWindow, editor_window, EDITOR, WINDOW, AdwApplicationWindow)
1212

13-
GtkWidget *editor_window_new (GtkApplication *application, gpointer *user_data);
13+
GtkWidget *editor_window_new (GtkApplication *application, gpointer user_data);
1414

1515
G_END_DECLS

src/glsl.y

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,7 @@ static void glsl_error(GLSL_LTYPE *loc, struct glsl_parse_context *c, const char
13581358
{
13591359
c->error = true;
13601360
if (c->error_cb)
1361-
c->error_cb(s, loc->first_line, loc->first_column, loc->last_column);
1361+
c->error_cb(s, loc->first_line, loc->first_column, loc->last_column, c->user_data);
13621362
}
13631363

13641364
int list_length(struct glsl_node *n, int list_token)
@@ -1483,9 +1483,10 @@ void glsl_parse_context_init(struct glsl_parse_context *context)
14831483
context->error = false;
14841484
}
14851485

1486-
void glsl_parse_set_error_cb(struct glsl_parse_context *context, glsl_parse_error_cb_t error_cb)
1486+
void glsl_parse_set_error_cb(struct glsl_parse_context *context, glsl_parse_error_cb_t error_cb, void* user_data)
14871487
{
14881488
context->error_cb = error_cb;
1489+
context->user_data = user_data;
14891490
}
14901491

14911492

src/glsl_parser.h

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,105 @@
11
#ifndef GLSL_PARSER_H
22
#define GLSL_PARSER_H
33

4-
#include <stdint.h>
5-
#include <stddef.h>
64
#include <stdbool.h>
5+
#include <stddef.h>
6+
#include <stdint.h>
77
#include <stdio.h>
88

9-
struct glsl_node {
10-
//Type of this node. These values are all members of the enum
11-
int code:16;
12-
13-
//Number of child nodes
14-
int child_count:16;
15-
16-
//Meta data for this node. Only uses for some nodes. The
17-
//field in this unions that should be read (if any)
18-
//is determined by 'code'
19-
union {
20-
double d;
21-
float f;
22-
int i;
23-
unsigned int ui;
24-
bool b;
25-
const char *str;
26-
} data;
27-
28-
//
29-
// Child nodes. Extra data will be allocated past the end
30-
// of the structure to hold the child nodes.
31-
//
32-
struct glsl_node *children[];
9+
struct glsl_node
10+
{
11+
// Type of this node. These values are all members of the enum
12+
int code : 16;
13+
14+
// Number of child nodes
15+
int child_count : 16;
16+
17+
// Meta data for this node. Only uses for some nodes. The
18+
// field in this unions that should be read (if any)
19+
// is determined by 'code'
20+
union {
21+
double d;
22+
float f;
23+
int i;
24+
unsigned int ui;
25+
bool b;
26+
const char *str;
27+
} data;
28+
29+
//
30+
// Child nodes. Extra data will be allocated past the end
31+
// of the structure to hold the child nodes.
32+
//
33+
struct glsl_node *children[];
3334
};
3435

35-
typedef void (*glsl_parse_error_cb_t)(const char *error_str, int lineno, int start_col, int end_col);
36+
typedef void (*glsl_parse_error_cb_t) (const char *error_str, int lineno, int start_col, int end_col, void *user_data);
37+
38+
struct glsl_parse_context
39+
{
40+
struct glsl_node *root;
41+
glsl_parse_error_cb_t error_cb;
3642

37-
struct glsl_parse_context {
38-
struct glsl_node *root;
39-
glsl_parse_error_cb_t error_cb;
43+
void *scanner; // Opaque handle to lexer context
4044

41-
void *scanner; //Opaque handle to lexer context
45+
/* Internal state of the parser's stack allocator */
46+
uint8_t *first_buffer;
47+
uint8_t *cur_buffer_start;
48+
uint8_t *cur_buffer;
49+
uint8_t *cur_buffer_end;
50+
bool error;
4251

43-
/* Internal state of the parser's stack allocator */
44-
uint8_t *first_buffer;
45-
uint8_t *cur_buffer_start;
46-
uint8_t *cur_buffer;
47-
uint8_t *cur_buffer_end;
48-
bool error;
52+
void *user_data;
4953
};
5054

5155
//
5256
// Create a new node for the AST. All values following 'code' will be
5357
// placed in the node's child array.
5458
//
55-
struct glsl_node *new_glsl_node(struct glsl_parse_context *context, int code, ...) __attribute__ ((sentinel));
59+
struct glsl_node *new_glsl_node (struct glsl_parse_context *context, int code, ...) __attribute__ ((sentinel));
5660

5761
//
5862
// Allocate memory in the parser's stack allocator
5963
//
60-
uint8_t *glsl_parse_alloc(struct glsl_parse_context *context, size_t size, int align);
64+
uint8_t *glsl_parse_alloc (struct glsl_parse_context *context, size_t size, int align);
6165

6266
//
6367
// Deallocate all memory previously allocated by glsl_parse_alloc()
6468
// The parser internally uses this allocator so any generated
6569
// AST data will become invalid when glsl_parse_dealloc() is called.
6670
//
67-
void glsl_parse_dealloc(struct glsl_parse_context *context);
71+
void glsl_parse_dealloc (struct glsl_parse_context *context);
6872

6973
//
7074
// Initialize a parsing context
7175
//
72-
void glsl_parse_context_init(struct glsl_parse_context *context);
76+
void glsl_parse_context_init (struct glsl_parse_context *context);
7377

7478

7579
//
7680
// Set the callback to invoke when a parsing error occurs
7781
//
78-
void glsl_parse_set_error_cb(struct glsl_parse_context *context, glsl_parse_error_cb_t error_cb);
82+
void glsl_parse_set_error_cb (struct glsl_parse_context *context, glsl_parse_error_cb_t error_cb, void *user_data);
7983

8084

8185
//
8286
// Destroy a parsing context.
8387
//
84-
void glsl_parse_context_destroy(struct glsl_parse_context *context);
88+
void glsl_parse_context_destroy (struct glsl_parse_context *context);
8589

8690
//
8791
// Parse the supplied file and generate an AST in context->root.
8892
//
8993
// Returns false if a parsing error occured
9094
//
91-
bool glsl_parse_file(struct glsl_parse_context *context, FILE *file);
95+
bool glsl_parse_file (struct glsl_parse_context *context, FILE *file);
9296

9397
//
9498
// Parse the supplied string and generate an AST in context->root.
9599
//
96100
// Returns false if a parsing error occured
97101
//
98-
bool glsl_parse_string(struct glsl_parse_context *context, const char *str);
102+
bool glsl_parse_string (struct glsl_parse_context *context, const char *str);
99103

100104
//
101105
// Include glsl.parse.h to get the enum values that are stored in the 'code'

src/gusl-utils.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ adw_alert_dialog_new_ok (const char *title, const char *body, GCallback ok_callb
103103
return ADW_ALERT_DIALOG (dialog);
104104
}
105105

106+
AdwMessageDialog *
107+
adw_message_dialog_new_ok (GtkWindow *parent, const char *title, const char *body, GCallback ok_callback)
108+
{
109+
GtkWidget *dialog = adw_message_dialog_new (parent, title, body);
110+
adw_message_dialog_add_responses (ADW_MESSAGE_DIALOG (dialog), "ok", _ ("OK"), NULL);
111+
adw_message_dialog_set_response_appearance (ADW_MESSAGE_DIALOG (dialog), "ok", ADW_RESPONSE_SUGGESTED);
112+
adw_message_dialog_set_default_response (ADW_MESSAGE_DIALOG (dialog), "ok");
113+
adw_message_dialog_set_close_response (ADW_MESSAGE_DIALOG (dialog), "ok");
114+
115+
if (ok_callback)
116+
{
117+
g_signal_connect (dialog, "ok", ok_callback, NULL);
118+
}
119+
120+
return ADW_MESSAGE_DIALOG (dialog);
121+
}
122+
106123
void
107124
_adw_destroy_dialog (AdwAlertDialog *dialog,
108125
GAsyncResult *result)

src/gusl-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ _adw_destroy_dialog (AdwAlertDialog *dialog,
3939
GThread *gusl_utils_get_main_thread (void);
4040
AdwAlertDialog *adw_alert_dialog_new_yes_or_no (const char *title, const char *body, GCallback yes_callback, GCallback no_callback);
4141
AdwAlertDialog *adw_alert_dialog_new_ok (const char *title, const char *body, GCallback ok_callback);
42-
42+
AdwMessageDialog *adw_message_dialog_new_ok (GtkWindow *parent, const char *title, const char *body, GCallback ok_callback);
4343

4444
G_END_DECLS

src/meson.build

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ libsrc = [
5858
'shader_source.c',
5959
]
6060

61+
libsrc += ast_sources
62+
6163
libgusl_shared = shared_library(
6264
'gusl',
6365
[libsrc, 'library.c'],
@@ -93,7 +95,7 @@ if gtk_builder_tool.found()
9395
endforeach
9496
endif
9597

96-
src += ['main.c', 'gusl-application.c', revision_tag, resources, ast_sources]
98+
src += ['main.c', 'gusl-application.c', revision_tag, resources]
9799

98100
executable(
99101
'gusl',

0 commit comments

Comments
 (0)