aboutsummaryrefslogtreecommitdiff
path: root/gtk/oscilloview-gl.c
blob: b67bdce8f4365934d744ef2be27a9df6a0207f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <gtk/gtk.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/glcorearb.h>
#include <stdlib.h>
#include <string.h>
#include "oscilloview.h"

struct oscilloview oscilloview_g = {
  .flag = ATOMIC_FLAG_INIT
};

enum {
  VIEW_SAMPLES = 1024,
};

static struct {
  GtkWidget *win;
  struct oscillodata oscillodata[LIBOPNA_OSCILLO_TRACK_COUNT];
  GLuint program;
  GLuint vs, fs;
  GLuint vao;
  GLuint vbo_linear;
  GLuint vbo_data;
  GLint uni_xpos;
  GLint uni_ypos;
} g;

static void on_glarea_unrealize(GtkWidget *w, gpointer ptr) {
  (void)w;
  GtkGLArea *area = GTK_GL_AREA(w);
  gtk_gl_area_make_current(area);
  if (gtk_gl_area_get_error(area)) return;
  glDeleteProgram(g.program);
  glDeleteShader(g.vs);
  glDeleteShader(g.fs);
  glDeleteBuffers(1, &g.vbo_linear);
  glDeleteBuffers(1, &g.vbo_data);
  glDeleteVertexArrays(1, &g.vao);
}

static void on_destroy(GtkWidget *w, gpointer ptr) {
  (void)w;
  (void)ptr;
  g.win = 0;
}

static const char v_sh[] =
"#version 110\n"
"attribute float coordx, coordy;\n"
"uniform float xpos, ypos;\n"
"void main(void) {\n"
"  gl_Position = vec4(vec2(coordx/3.0 + xpos, coordy*2.0/3.0 + ypos), 0.0, 1.0);\n"
"}\n";

static const char f_sh[] =
"#version 110\n"
"void main(void) {\n"
"  gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\n";

static GLuint create_shader(const char *shader, GLenum type) {
  GLuint s = glCreateShader(type);
  glShaderSource(s, 1, &shader, 0);
  glCompileShader(s);
  GLint ok;
  glGetShaderiv(s, GL_COMPILE_STATUS, &ok);
  if (!ok) {
    GLint len;
    glGetShaderiv(s, GL_INFO_LOG_LENGTH, &len);
    if (len > 0) {
      char *log = malloc(len);
      if (log) {
        glGetShaderInfoLog(s, len, 0, log);
        printf("%s shader error: \n%s\n",
               (type == GL_VERTEX_SHADER) ? "vertex" : "fragment",
               log);
        free(log);
      }
    }
    glDeleteShader(s);
    return 0;
  }
  return s;
}

static void on_realize(GtkWidget *w, gpointer ptr) {
  (void)ptr;
  GtkGLArea *area = GTK_GL_AREA(w);
  gtk_gl_area_make_current(area);
  if (gtk_gl_area_get_error(area)) return;
  g.program = glCreateProgram();
  g.vs = create_shader(v_sh, GL_VERTEX_SHADER);
  g.fs = create_shader(f_sh, GL_FRAGMENT_SHADER);
  glAttachShader(g.program, g.vs);
  glAttachShader(g.program, g.fs);
  glBindAttribLocation(g.program, 0, "coordx");
  glBindAttribLocation(g.program, 1, "coordy");
  glLinkProgram(g.program);
  glUseProgram(g.program);
  g.uni_xpos = glGetUniformLocation(g.program, "xpos");
  g.uni_ypos = glGetUniformLocation(g.program, "ypos");
  glGenVertexArrays(1, &g.vao);
  glBindVertexArray(g.vao);
  glGenBuffers(1, &g.vbo_linear);
  glBindBuffer(GL_ARRAY_BUFFER, g.vbo_linear);
  static GLfloat linear_pos[VIEW_SAMPLES];
  for (int i = 0; i < VIEW_SAMPLES; i++) {
    linear_pos[i] = 2.0f*((i-512) / (float)VIEW_SAMPLES);
  }
  glBufferData(GL_ARRAY_BUFFER, sizeof(linear_pos), linear_pos, GL_STATIC_DRAW);
  glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, 0);
  glGenBuffers(1, &g.vbo_data);
  glBindBuffer(GL_ARRAY_BUFFER, g.vbo_data);
  glBufferData(GL_ARRAY_BUFFER, 2*VIEW_SAMPLES, 0, GL_STREAM_DRAW);
  glVertexAttribPointer(1, 1, GL_SHORT, GL_TRUE, 0, 0);
}

static gboolean on_render(GtkGLArea *area,
                          GdkGLContext *ctx,
                          gpointer ptr) {
  (void)ptr;
  if (!atomic_flag_test_and_set_explicit(
    &oscilloview_g.flag, memory_order_acquire)) {
    memcpy(g.oscillodata,
           oscilloview_g.oscillodata,
           sizeof(oscilloview_g.oscillodata));
    atomic_flag_clear_explicit(&oscilloview_g.flag, memory_order_release);
  }
  glEnableVertexAttribArray(0);
  glEnableVertexAttribArray(1);
  glClear(GL_COLOR_BUFFER_BIT);

  for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 3; y++) {
      int start = OSCILLO_SAMPLE_COUNT - VIEW_SAMPLES;
      start -= (g.oscillodata[x*3+y].offset >> OSCILLO_OFFSET_SHIFT);
      glBindBuffer(GL_ARRAY_BUFFER, g.vbo_data);
      glBufferData(GL_ARRAY_BUFFER, 2*VIEW_SAMPLES, &g.oscillodata[x*3+y].buf[start], GL_STREAM_DRAW);
      glUniform1f(g.uni_xpos, (x - 1) * (2.0f/3.0f));
      glUniform1f(g.uni_ypos, (y - 1) * (-2.0f/3.0f));
      glDrawArrays(GL_LINE_STRIP, 0, 1024);
    }
  }
  glDisableVertexAttribArray(0);
  glDisableVertexAttribArray(1);
  return TRUE;
}

static gboolean tick_cb(GtkWidget *w, GdkFrameClock *clock, gpointer ptr) {
  (void)clock;
  (void)ptr;
  gtk_widget_queue_draw(w);
  return G_SOURCE_CONTINUE;
}

void show_oscilloview(void) {
  if (!g.win) {
    g.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(g.win), "Oscilloscope view");
    g_signal_connect(g.win, "destroy", G_CALLBACK(on_destroy), 0);
    GtkWidget *glarea = gtk_gl_area_new();
    g_signal_connect(G_OBJECT(glarea), "unrealize", G_CALLBACK(on_glarea_unrealize), 0);
    gtk_gl_area_set_required_version(GTK_GL_AREA(glarea), 3, 2);
    g_signal_connect(glarea, "render", G_CALLBACK(on_render), 0);
    g_signal_connect(glarea, "realize", G_CALLBACK(on_realize), 0);
    gtk_container_add(GTK_CONTAINER(g.win), glarea);
    gtk_widget_add_tick_callback(glarea, tick_cb, 0, 0);
  }
  gtk_widget_show_all(g.win);
}