979 lines
32 KiB
C
979 lines
32 KiB
C
#include <linmath.h>
|
|
#include <log.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
|
|
#include "types.h"
|
|
|
|
#include "config.h"
|
|
#include "config_file.h"
|
|
#include "constants.h"
|
|
#include "file.h"
|
|
#include "shaders.h"
|
|
|
|
#define GLAD_GL_IMPLEMENTATION
|
|
#include <glad/gl.h>
|
|
|
|
#ifdef VIDEO_IN
|
|
#define GLAD_EGL_IMPLEMENTATION
|
|
#include <glad/egl.h>
|
|
#endif /* VIDEO_IN */
|
|
|
|
#include <GLFW/glfw3.h>
|
|
#ifdef VIDEO_IN
|
|
#include <GLFW/glfw3native.h>
|
|
#endif /* VIDEO_IN */
|
|
|
|
static const GLuint unused_uniform = (GLuint)-1;
|
|
|
|
static bool check_glerror_ro(const char *context) {
|
|
unsigned int code;
|
|
|
|
code = glGetError();
|
|
|
|
if (code > 0) {
|
|
log_warn("GL Error: %04x (%s)", code, context);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static bool check_glerror(ShaderProgram *program, const char *context) {
|
|
if (check_glerror_ro(context)) {
|
|
program->error = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#ifdef GL_DEBUG
|
|
|
|
static void gl_debug_callback(GLenum source, GLenum type, GLuint id,
|
|
GLenum severity,
|
|
__attribute__((unused)) GLsizei length,
|
|
const GLchar *message,
|
|
__attribute__((unused)) const void *userParam) {
|
|
log_debug("GL Debug: source=%x, type=%x, id=%d, severity=%x, message=%s",
|
|
source, type, id, severity, message ? message : "(null)");
|
|
}
|
|
|
|
#endif /* GL_DEBUG */
|
|
|
|
static bool check_eglerror_ro(const char *context) {
|
|
#ifdef VIDEO_IN
|
|
unsigned int code;
|
|
|
|
code = eglGetError();
|
|
|
|
if (code > 0 && code != EGL_SUCCESS) {
|
|
log_warn("EGL Error: %04x (%s)", code, context);
|
|
return true;
|
|
}
|
|
#endif /* VIDEO_IN */
|
|
return false;
|
|
}
|
|
|
|
static bool check_eglerror(ShaderProgram *program, const char *context) {
|
|
if (check_eglerror_ro(context)) {
|
|
program->error = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#ifdef VIDEO_IN
|
|
#ifdef EGL_DEBUG
|
|
|
|
static void egl_debug_callback(EGLenum error, const char *command,
|
|
EGLint messageType, EGLLabelKHR threadLabel,
|
|
EGLLabelKHR objectLabel, const char *message) {
|
|
log_debug("EGL Debug: error=%04x, command=%s, type=%d, "
|
|
"thread=%p, object=%p, message=%s",
|
|
error, command ? command : "(null)", messageType, threadLabel,
|
|
objectLabel, message ? message : "(null)");
|
|
}
|
|
|
|
#endif /* EGL_DEBUG */
|
|
#endif /* VIDEO_IN */
|
|
|
|
static bool init_gl(ShaderProgram *program) {
|
|
gladLoadGL(glfwGetProcAddress);
|
|
|
|
#ifdef GL_DEBUG
|
|
glEnable(GL_DEBUG_OUTPUT);
|
|
glDebugMessageCallback(gl_debug_callback, NULL);
|
|
#endif /* GL_DEBUG */
|
|
|
|
#ifdef VIDEO_IN
|
|
program->egl_display = glfwGetEGLDisplay();
|
|
if (program->egl_display == EGL_NO_DISPLAY) {
|
|
log_error("error: glfwGetEGLDisplay no EGLDisplay returned");
|
|
program->error = true;
|
|
return false;
|
|
}
|
|
|
|
gladLoadEGL(program->egl_display, glfwGetProcAddress);
|
|
|
|
#ifdef EGL_DEBUG
|
|
{
|
|
PFNEGLDEBUGMESSAGECONTROLKHRPROC debug_ctrl =
|
|
(PFNEGLDEBUGMESSAGECONTROLKHRPROC)glfwGetProcAddress(
|
|
"eglDebugMessageControlKHR");
|
|
if (debug_ctrl != NULL) {
|
|
const EGLAttrib attrib_list[] = {EGL_NONE};
|
|
debug_ctrl(egl_debug_callback, attrib_list);
|
|
}
|
|
}
|
|
|
|
#endif /* EGL_DEBUG */
|
|
|
|
#endif /* VIDEO_IN */
|
|
|
|
return !check_glerror(program, "init_gl") &&
|
|
!check_eglerror(program, "init_gl");
|
|
}
|
|
|
|
static bool init_textures(ShaderProgram *program, const Context *context) {
|
|
glGenTextures(program->tex_count, program->textures);
|
|
if (check_glerror(program, "init_textures/glGenTextures")) {
|
|
return false;
|
|
}
|
|
|
|
for (unsigned int i = 0; i < program->tex_count; i++) {
|
|
// selects which texture unit subsequent texture state calls will affect
|
|
glActiveTexture(GL_TEXTURE0 + i);
|
|
if (check_glerror(program, "init_textures/glActiveTexture")) {
|
|
return false;
|
|
}
|
|
|
|
glBindTexture(GL_TEXTURE_2D, program->textures[i]);
|
|
if (check_glerror(program, "init_textures/glBindTexture")) {
|
|
return false;
|
|
}
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
glDisable(GL_BLEND);
|
|
if (check_glerror(program, "init_textures/glDisable")) {
|
|
return false;
|
|
}
|
|
|
|
// define texture image as empty
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, context->tex_resolution[0],
|
|
context->tex_resolution[1], 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
|
if (check_glerror(program, "init_textures/glTexImage2D")) {
|
|
return false;
|
|
}
|
|
|
|
// setup mipmap context
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
if (check_glerror(program, "init_textures/glTexParameteri")) {
|
|
return false;
|
|
}
|
|
|
|
log_info("Texture %d initialized", i);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void rebind_textures(const ShaderProgram *program) {
|
|
for (unsigned int i = 0; i < program->tex_count; i++) {
|
|
glActiveTexture(GL_TEXTURE0 + i);
|
|
check_glerror_ro("rebind_textures/glActiveTexture");
|
|
|
|
glBindTexture(GL_TEXTURE_2D, program->textures[i]);
|
|
check_glerror_ro("rebind_textures/glBindTexture");
|
|
}
|
|
}
|
|
|
|
#ifdef VIDEO_IN
|
|
static bool link_input_to_texture(ShaderProgram *program, VideoCapture *input,
|
|
unsigned int input_index,
|
|
unsigned int sub_index, bool reload) {
|
|
unsigned int texture_index =
|
|
program->tex_count + input_index * program->sub_video_count + sub_index;
|
|
|
|
if (reload) {
|
|
glDeleteTextures(1, program->textures + texture_index);
|
|
if (check_glerror(program, "link_input_to_texture/glDeleteTextures")) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
glGenTextures(1, program->textures + texture_index);
|
|
if (check_glerror(program, "link_input_to_texture/glGenTextures")) {
|
|
return false;
|
|
}
|
|
|
|
EGLImageKHR dma_image;
|
|
|
|
dma_image = EGL_NO_IMAGE_KHR;
|
|
|
|
// https://registry.khronos.org/EGL/extensions/EXT/EGL_EXT_image_dma_buf_import.txt
|
|
const EGLint attrib_list[] = {EGL_WIDTH,
|
|
input->width,
|
|
EGL_HEIGHT,
|
|
input->height,
|
|
EGL_LINUX_DRM_FOURCC_EXT,
|
|
input->pixelformat,
|
|
EGL_DMA_BUF_PLANE0_FD_EXT,
|
|
input->exp_fd[sub_index],
|
|
EGL_DMA_BUF_PLANE0_OFFSET_EXT,
|
|
0,
|
|
EGL_DMA_BUF_PLANE0_PITCH_EXT,
|
|
input->bytesperline,
|
|
EGL_NONE};
|
|
|
|
dma_image = eglCreateImageKHR(program->egl_display, EGL_NO_CONTEXT,
|
|
EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list);
|
|
if (check_eglerror(program, "link_input_to_texture/eglCreateImageKHR")) {
|
|
return false;
|
|
}
|
|
|
|
program->dma_images[input_index * program->sub_video_count + sub_index] =
|
|
dma_image;
|
|
|
|
glActiveTexture(GL_TEXTURE0 + texture_index);
|
|
if (check_glerror(program, "link_input_to_texture/glActiveTexture")) {
|
|
return false;
|
|
}
|
|
|
|
glBindTexture(GL_TEXTURE_2D, program->textures[texture_index]);
|
|
if (check_glerror(program, "link_input_to_texture/glBindTexture")) {
|
|
return false;
|
|
}
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, input->width, input->height, 0, GL_RGB,
|
|
GL_UNSIGNED_BYTE, 0);
|
|
if (check_glerror(program, "link_input_to_texture/glTexImage2D")) {
|
|
return false;
|
|
}
|
|
|
|
// https://registry.khronos.org/OpenGL/extensions/EXT/EXT_EGL_image_storage.txt
|
|
glEGLImageTargetTexStorageEXT(GL_TEXTURE_2D, dma_image, NULL);
|
|
if (check_eglerror(program,
|
|
"link_input_to_texture/glEGLImageTargetTexStorageEXT")) {
|
|
return false;
|
|
}
|
|
|
|
log_info("Texture %d linked to %s[%d]", texture_index, input->name,
|
|
sub_index);
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool init_input(ShaderProgram *program, const ConfigFile *config,
|
|
VideoCaptureArray *inputs, unsigned int input_index,
|
|
bool reload) {
|
|
unsigned int tex_i;
|
|
unsigned int sub_index;
|
|
char name[STR_LEN];
|
|
|
|
if (input_index < inputs->length && input_index < program->in_count &&
|
|
!inputs->values[input_index].error) {
|
|
snprintf(name, STR_LEN, "IN_%d_OUT", input_index + 1);
|
|
tex_i = config_file_get_int(config, name, 0);
|
|
for (sub_index = 0; sub_index < inputs->values[input_index].buf_count;
|
|
sub_index++) {
|
|
if (!link_input_to_texture(program, &inputs->values[input_index],
|
|
input_index, sub_index, reload)) {
|
|
return false;
|
|
}
|
|
}
|
|
program->input_tex_map[input_index] = tex_i;
|
|
program->tex_map[tex_i] =
|
|
program->tex_count + input_index * program->sub_video_count;
|
|
} else {
|
|
log_warn("Cannot link input %d", input_index + 1);
|
|
}
|
|
return true;
|
|
}
|
|
#endif /* VIDEO_IN */
|
|
|
|
static bool init_framebuffers(ShaderProgram *program,
|
|
const ConfigFile *config) {
|
|
unsigned int tex_i;
|
|
char name[STR_LEN];
|
|
GLenum framebuffer_status;
|
|
|
|
glGenFramebuffers(program->frag_count, program->frame_buffers);
|
|
if (check_glerror(program, "init_framebuffers/glGenFramebuffers")) {
|
|
return false;
|
|
}
|
|
|
|
for (unsigned int i = 0; i < program->frag_count; i++) {
|
|
if (i == program->frag_output_index || i == program->frag_monitor_index) {
|
|
continue;
|
|
}
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, program->frame_buffers[i]);
|
|
if (check_glerror(program, "init_framebuffers/glBindFramebuffer")) {
|
|
return false;
|
|
}
|
|
|
|
snprintf(name, STR_LEN, "FRAG_%d_OUT", i + 1);
|
|
tex_i = config_file_get_int(config, name, 0);
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
|
program->textures[tex_i], 0);
|
|
if (check_glerror(program, "init_framebuffers/glFramebufferTexture2D")) {
|
|
return false;
|
|
}
|
|
|
|
// check framebuffer status
|
|
framebuffer_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
if (check_glerror(program, "init_framebuffers/glCheckFramebufferStatus")) {
|
|
return false;
|
|
}
|
|
if (framebuffer_status != GL_FRAMEBUFFER_COMPLETE) {
|
|
log_error("Framebuffer %d is KO: %x", i + 1, framebuffer_status);
|
|
program->error = true;
|
|
return false;
|
|
}
|
|
|
|
log_info("Framebuffer %d initialized", i);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool init_vertices(ShaderProgram *program) {
|
|
glGenBuffers(1, &program->vertex_buffer);
|
|
if (check_glerror(program, "init_vertices/glGenBuffers")) {
|
|
return false;
|
|
}
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, program->vertex_buffer);
|
|
if (check_glerror(program, "init_vertices/glBindBuffer")) {
|
|
return false;
|
|
}
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
if (check_glerror(program, "init_vertices/glBufferData")) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool bind_vertices(ShaderProgram *program, unsigned int index) {
|
|
glBindBuffer(GL_ARRAY_BUFFER, program->vertex_buffer);
|
|
if (check_glerror(program, "bind_vertices/glBindBuffer")) {
|
|
return false;
|
|
}
|
|
|
|
glGenVertexArrays(1, &program->vertex_array[index]);
|
|
if (check_glerror(program, "bind_vertices/glGenVertexArrays")) {
|
|
return false;
|
|
}
|
|
|
|
glBindVertexArray(program->vertex_array[index]);
|
|
if (check_glerror(program, "bind_vertices/glBindVertexArray")) {
|
|
return false;
|
|
}
|
|
|
|
for (unsigned int i = 0; i < program->frag_count; i++) {
|
|
// enable attribute pointer
|
|
glEnableVertexAttribArray(program->vpos_locations[i]);
|
|
if (check_glerror(program, "bind_vertices/glEnableVertexAttribArray")) {
|
|
return false;
|
|
}
|
|
|
|
// specify the location and data format of the array of generic vertex
|
|
// attributes to use when rendering
|
|
glVertexAttribPointer(program->vpos_locations[i], 2, GL_FLOAT, GL_FALSE,
|
|
sizeof(Vertex), (void *)offsetof(Vertex, pos));
|
|
if (check_glerror(program, "bind_vertices/glVertexAttribPointer")) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool compile_shader(GLuint shader_id, const char *name,
|
|
const char *source_code) {
|
|
GLint status_params;
|
|
char log[STR_LEN];
|
|
|
|
log_info("Compiling '%s'...", name);
|
|
|
|
// update shader source code
|
|
glShaderSource(shader_id, 1, &source_code, NULL);
|
|
check_glerror_ro("compile_shader/glShaderSource");
|
|
|
|
// compile shader
|
|
glCompileShader(shader_id);
|
|
check_glerror_ro("compile_shader/glCompileShader");
|
|
|
|
// get compilation status
|
|
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &status_params);
|
|
check_glerror_ro("compile_shader/glGetShaderiv");
|
|
|
|
glGetShaderInfoLog(shader_id, STR_LEN, NULL, (GLchar *)log);
|
|
check_glerror_ro("compile_shader/glGetShaderInfoLog");
|
|
|
|
if (status_params == GL_FALSE) {
|
|
log_error("Failed to compile\n%s", log);
|
|
file_dump("error.glsl", source_code);
|
|
} else {
|
|
log_info("Compilation successful");
|
|
}
|
|
|
|
return status_params == GL_TRUE;
|
|
}
|
|
|
|
static bool init_shaders(ShaderProgram *program, const Project *project) {
|
|
// compile vertex shader
|
|
program->vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
|
if (check_glerror(program, "init_shaders/glCreateShader")) {
|
|
return false;
|
|
}
|
|
|
|
program->error = program->error || !compile_shader(program->vertex_shader,
|
|
"internal vertex shader",
|
|
vertex_shader_text);
|
|
|
|
// compile fragment shaders
|
|
for (unsigned int i = 0; i < program->frag_count; i++) {
|
|
program->fragment_shaders[i] = glCreateShader(GL_FRAGMENT_SHADER);
|
|
if (check_glerror(program, "init_shaders/glCreateShader")) {
|
|
return false;
|
|
}
|
|
|
|
program->error = program->error ||
|
|
!compile_shader(program->fragment_shaders[i],
|
|
project->fragment_shaders[i][0].path,
|
|
project->fragment_shaders[i][0].content);
|
|
|
|
if (program->error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool init_single_program(ShaderProgram *program, unsigned int i,
|
|
const ConfigFile *config,
|
|
const StateConfig *state_config) {
|
|
unsigned int index1;
|
|
unsigned int index2;
|
|
char name[STR_LEN];
|
|
const char *prefix;
|
|
GLint link_status;
|
|
char link_log[STR_LEN];
|
|
|
|
program->programs[i] = glCreateProgram();
|
|
if (check_glerror(program, "init_single_program/glCreateProgram")) {
|
|
return false;
|
|
}
|
|
|
|
glAttachShader(program->programs[i], program->vertex_shader);
|
|
glAttachShader(program->programs[i], program->fragment_shaders[i]);
|
|
if (check_glerror(program, "init_single_program/glAttachShader")) {
|
|
return false;
|
|
}
|
|
|
|
glLinkProgram(program->programs[i]);
|
|
if (check_glerror(program, "init_single_program/glLinkProgram")) {
|
|
return false;
|
|
}
|
|
|
|
glGetProgramiv(program->programs[i], GL_LINK_STATUS, &link_status);
|
|
if (check_glerror(program, "init_single_program/glGetProgramiv")) {
|
|
return false;
|
|
}
|
|
if (link_status != GL_TRUE) {
|
|
glGetProgramInfoLog(program->programs[i], STR_LEN, NULL, link_log);
|
|
if (check_glerror(program, "init_single_program/glGetProgramInfoLog")) {
|
|
return false;
|
|
}
|
|
log_error("Program %d link error: %s", i + 1, link_log);
|
|
return false;
|
|
}
|
|
|
|
// create uniforms pointers
|
|
program->itime_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_TIME", "iTime"));
|
|
program->itempo_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_TEMPO", "iTempo"));
|
|
program->ibeats_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_BEATS", "iBeats"));
|
|
program->ifps_locations[i] = glGetUniformLocation(
|
|
program->programs[i], config_file_get_str(config, "UNIFORM_FPS", "iFPS"));
|
|
program->ires_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_RESOLUTION", "iResolution"));
|
|
program->itexres_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_TEX_RESOLUTION", "iTexResolution"));
|
|
program->idemo_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_DEMO", "iDemo"));
|
|
program->iautorand_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_AUTORAND", "iAutoRand"));
|
|
program->iautorandcycle_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_AUTORANDCYCLE", "iAutoRandCycle"));
|
|
program->ipage_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_PAGE", "iPage"));
|
|
program->iselected_locations[i] = glGetUniformLocation(
|
|
program->programs[i],
|
|
config_file_get_str(config, "UNIFORM_SELECTED", "iSelected"));
|
|
|
|
prefix = config_file_get_str(config, "UNIFORM_IN_RESOLUTION_PREFIX",
|
|
"iInputResolution");
|
|
for (unsigned int j = 0; j < program->in_count; j++) {
|
|
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
|
program->iinres_locations[i * program->in_count + j] =
|
|
glGetUniformLocation(program->programs[i], name);
|
|
}
|
|
|
|
prefix =
|
|
config_file_get_str(config, "UNIFORM_IN_FORMAT_PREFIX", "iInputFormat");
|
|
for (unsigned int j = 0; j < program->in_count; j++) {
|
|
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
|
program->iinfmt_locations[i * program->in_count + j] =
|
|
glGetUniformLocation(program->programs[i], name);
|
|
}
|
|
|
|
prefix = config_file_get_str(config, "UNIFORM_IN_FPS_PREFIX", "iInputFPS");
|
|
for (unsigned int j = 0; j < program->in_count; j++) {
|
|
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
|
program->iinfps_locations[i * program->in_count + j] =
|
|
glGetUniformLocation(program->programs[i], name);
|
|
}
|
|
|
|
prefix = config_file_get_str(config, "UNIFORM_SEED_PREFIX", "iSeed");
|
|
for (unsigned int j = 0; j < program->frag_count; j++) {
|
|
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
|
program->iseed_locations[i * program->frag_count + j] =
|
|
glGetUniformLocation(program->programs[i], name);
|
|
}
|
|
|
|
prefix = config_file_get_str(config, "UNIFORM_STATE_PREFIX", "iState");
|
|
for (unsigned int j = 0; j < program->frag_count; j++) {
|
|
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
|
program->istate_locations[i * program->frag_count + j] =
|
|
glGetUniformLocation(program->programs[i], name);
|
|
}
|
|
|
|
prefix = config_file_get_str(config, "UNIFORM_ACTIVE_PREFIX", "iActive");
|
|
for (unsigned int j = 0; j < program->active_count; j++) {
|
|
snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
|
|
program->iactive_locations[i * program->active_count + j] =
|
|
glGetUniformLocation(program->programs[i], name);
|
|
}
|
|
|
|
if (program->midi_lengths.length == 0) {
|
|
index1 = 0;
|
|
for (unsigned int j = 0; j < state_config->group_active_counts.length;
|
|
j++) {
|
|
for (unsigned int k = 0; k < state_config->group_active_counts.values[j];
|
|
k++) {
|
|
program->midi_lengths.values[index1++] =
|
|
state_config->group_counts.values[j];
|
|
}
|
|
}
|
|
program->midi_lengths.length = index1;
|
|
}
|
|
|
|
prefix = config_file_get_str(config, "UNIFORM_GROUP_PREFIX", "iGroup");
|
|
index2 = 0;
|
|
for (unsigned int j = 0; j < state_config->group_active_counts.length; j++) {
|
|
for (unsigned int k = 0; k < state_config->group_active_counts.values[j];
|
|
k++) {
|
|
snprintf(name, STR_LEN, "%s%d_%d", prefix, j + 1, k + 1);
|
|
program->igroup_locations[i * program->midi_lengths.length + index2++] =
|
|
glGetUniformLocation(program->programs[i], name);
|
|
}
|
|
}
|
|
|
|
// create texX uniforms pointer
|
|
prefix = config_file_get_str(config, "UNIFORM_TEX_PREFIX", "iTex");
|
|
for (unsigned int j = 0; j < program->tex_count; j++) {
|
|
snprintf(name, STR_LEN, "%s%d", prefix, j);
|
|
program->textures_locations[i * program->tex_count + j] =
|
|
glGetUniformLocation(program->programs[i], name);
|
|
}
|
|
|
|
if (check_glerror(program, "init_single_program/glGetUniformLocation")) {
|
|
return false;
|
|
}
|
|
|
|
for (unsigned int j = 0; j < program->sub_type_count; j++) {
|
|
snprintf(name, STR_LEN, "SUB_%d_PREFIX", j + 1);
|
|
prefix = config_file_get_str(config, name, 0);
|
|
for (unsigned int k = 0; k < program->sub_variant_count; k++) {
|
|
snprintf(name, STR_LEN, "%s%d", prefix, k + 1);
|
|
program->sub_locations[i * program->sub_variant_count *
|
|
program->sub_type_count +
|
|
j * program->sub_variant_count + k] =
|
|
glGetSubroutineIndex(program->programs[i], GL_FRAGMENT_SHADER, name);
|
|
}
|
|
}
|
|
|
|
if (check_glerror(program, "init_single_program/glGetSubroutineIndex")) {
|
|
return false;
|
|
}
|
|
|
|
// create attribute pointer
|
|
program->vpos_locations[i] =
|
|
glGetAttribLocation(program->programs[i], "vPos");
|
|
|
|
if (check_glerror(program, "init_single_program/glGetAttribLocation")) {
|
|
return false;
|
|
}
|
|
|
|
log_info("Program %d initialized", i + 1);
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool init_programs(ShaderProgram *program, const ConfigFile *config,
|
|
const StateConfig *state_config) {
|
|
for (unsigned int i = 0; i < program->frag_count; i++) {
|
|
if (!init_single_program(program, i, config, state_config)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static void update_viewport(ShaderProgram *program, const Context *context) {
|
|
// viewport changed
|
|
if (context->resolution[0] != program->last_resolution[0] ||
|
|
context->resolution[1] != program->last_resolution[1]) {
|
|
// clean and resize all textures
|
|
for (unsigned int i = 0; i < program->tex_count; i++) {
|
|
glActiveTexture(GL_TEXTURE0 + i);
|
|
check_glerror_ro("update_viewport/glActiveTexture");
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, context->tex_resolution[0],
|
|
context->tex_resolution[1], 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
|
check_glerror_ro("update_viewport/glTexImage2D");
|
|
}
|
|
program->last_resolution[0] = context->resolution[0];
|
|
program->last_resolution[1] = context->resolution[1];
|
|
}
|
|
}
|
|
|
|
static void write_uniform_1f(GLuint location, float value) {
|
|
if (location != unused_uniform) {
|
|
glUniform1f(location, value);
|
|
}
|
|
}
|
|
|
|
static void write_uniform_1i(GLuint location, unsigned int value) {
|
|
if (location != unused_uniform) {
|
|
glUniform1i(location, (const GLint)value);
|
|
}
|
|
}
|
|
|
|
static void write_uniform_2f(GLuint location, const vec2 *value) {
|
|
if (location != unused_uniform) {
|
|
glUniform2fv(location, 1, (const GLfloat *)value);
|
|
}
|
|
}
|
|
|
|
static void write_uniform_multi_3f(GLuint location, unsigned int count,
|
|
const vec3 *value) {
|
|
if (location != unused_uniform) {
|
|
glUniform3fv(location, count, (const GLfloat *)value);
|
|
}
|
|
}
|
|
|
|
static void use_program(const ShaderProgram *program, int i, bool output,
|
|
const Context *context) {
|
|
unsigned int k;
|
|
unsigned int offset;
|
|
unsigned int subcount;
|
|
GLuint subroutines[ARRAY_SIZE];
|
|
// use specific shader program
|
|
glUseProgram(program->programs[i]);
|
|
|
|
if (output) {
|
|
glViewport(0, 0, context->resolution[0], context->resolution[1]);
|
|
|
|
// use default framebuffer (output)
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
// clear buffer
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
} else {
|
|
glViewport(0, 0, context->tex_resolution[0], context->tex_resolution[1]);
|
|
|
|
// use memory framebuffer
|
|
glBindFramebuffer(GL_FRAMEBUFFER, program->frame_buffers[i]);
|
|
glDrawBuffer(GL_COLOR_ATTACHMENT0);
|
|
}
|
|
// set fragment uniforms
|
|
write_uniform_1f(program->itime_locations[i], context->time);
|
|
write_uniform_1f(program->itempo_locations[i], context->tempo.tempo);
|
|
write_uniform_1f(program->ibeats_locations[i], context->tempo_total);
|
|
write_uniform_1i(program->ifps_locations[i], context->fps);
|
|
write_uniform_1i(program->idemo_locations[i], context->demo ? 1 : 0);
|
|
write_uniform_1i(program->iautorand_locations[i],
|
|
context->auto_random ? 1 : 0);
|
|
write_uniform_1i(program->iautorandcycle_locations[i],
|
|
context->auto_random_cycle);
|
|
write_uniform_1i(program->ipage_locations[i], context->page);
|
|
write_uniform_1i(program->iselected_locations[i], context->selected + 1);
|
|
write_uniform_2f(program->ires_locations[i], &context->resolution);
|
|
write_uniform_2f(program->itexres_locations[i], &context->tex_resolution);
|
|
|
|
for (unsigned int j = 0; j < program->active_count; j++) {
|
|
write_uniform_1i(program->iactive_locations[i * program->active_count + j],
|
|
context->active[j] + 1);
|
|
}
|
|
|
|
#ifdef VIDEO_IN
|
|
|
|
for (unsigned int j = 0; j < program->in_count; j++) {
|
|
write_uniform_2f(program->iinres_locations[i * program->in_count + j],
|
|
&context->input_resolutions[j]);
|
|
write_uniform_1i(program->iinfmt_locations[i * program->in_count + j],
|
|
context->input_formats[j]);
|
|
write_uniform_1i(program->iinfps_locations[i * program->in_count + j],
|
|
context->input_fps[j]);
|
|
}
|
|
|
|
#endif /* VIDEO_IN */
|
|
|
|
// set seeds uniforms
|
|
for (unsigned int j = 0; j < program->frag_count; j++) {
|
|
write_uniform_1i(program->iseed_locations[i * program->frag_count + j],
|
|
context->seeds[j]);
|
|
}
|
|
|
|
for (unsigned int j = 0; j < program->frag_count; j++) {
|
|
write_uniform_1i(program->istate_locations[i * program->frag_count + j],
|
|
context->state.values[j] + 1);
|
|
}
|
|
|
|
offset = 0;
|
|
for (unsigned int j = 0; j < program->midi_lengths.length; j++) {
|
|
write_uniform_multi_3f(
|
|
program->igroup_locations[i * program->midi_lengths.length + j],
|
|
program->midi_lengths.values[j], context->values + offset);
|
|
offset += program->midi_lengths.values[j];
|
|
}
|
|
|
|
// set subroutines for fragment and update state uniforms
|
|
k = context->state.values[i];
|
|
subcount = 0;
|
|
for (unsigned int j = 0; j < program->sub_type_count; j++) {
|
|
if (program->sub_locations[i * program->sub_variant_count *
|
|
program->sub_type_count +
|
|
j * program->sub_variant_count + k] !=
|
|
unused_uniform) {
|
|
subroutines[subcount++] =
|
|
program->sub_locations[i * program->sub_variant_count *
|
|
program->sub_type_count +
|
|
j * program->sub_variant_count + k];
|
|
}
|
|
}
|
|
|
|
if (subcount > 0) {
|
|
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, subcount, subroutines);
|
|
}
|
|
|
|
// set GL_TEXTURE(X) to uniform sampler2D texX
|
|
for (unsigned int j = 0; j < program->tex_count; j++) {
|
|
write_uniform_1i(program->textures_locations[i * program->tex_count + j],
|
|
program->tex_map[j]);
|
|
}
|
|
|
|
// draw output
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
}
|
|
|
|
void shaders_init(ShaderProgram *program, const Project *project,
|
|
const Context *context, bool rebind) {
|
|
unsigned int i;
|
|
if (!rebind) {
|
|
program->error = false;
|
|
program->last_resolution[0] = context->resolution[0];
|
|
program->last_resolution[1] = context->resolution[1];
|
|
program->tex_count = config_file_get_int(&project->config, "TEX_COUNT", 9);
|
|
memset(program->tex_map, 0, sizeof(program->tex_map));
|
|
for (i = 0; i < program->tex_count; i++) {
|
|
program->tex_map[i] = i;
|
|
}
|
|
program->frag_count = project->frag_count;
|
|
program->frag_output_index =
|
|
config_file_get_int(&project->config, "FRAG_OUTPUT", 1) - 1;
|
|
program->frag_monitor_index =
|
|
config_file_get_int(&project->config, "FRAG_MONITOR", 1) - 1;
|
|
program->sub_type_count =
|
|
config_file_get_int(&project->config, "SUB_TYPE_COUNT", 0);
|
|
program->in_count = config_file_get_int(&project->config, "IN_COUNT", 0);
|
|
program->sub_variant_count = project->state_config.state_max;
|
|
program->active_count = project->state_config.group_active_counts.length;
|
|
program->midi_lengths.length = 0;
|
|
|
|
#ifdef VIDEO_IN
|
|
memset(program->dma_images, 0, sizeof(program->dma_images));
|
|
memset(program->input_tex_map, 0, sizeof(program->input_tex_map));
|
|
#endif /* VIDEO_IN */
|
|
|
|
if (!init_gl(program)) {
|
|
return;
|
|
}
|
|
|
|
if (!init_shaders(program, project)) {
|
|
return;
|
|
}
|
|
|
|
if (!init_textures(program, context)) {
|
|
return;
|
|
}
|
|
|
|
if (!init_framebuffers(program, &project->config)) {
|
|
return;
|
|
}
|
|
|
|
if (!init_programs(program, &project->config, &project->state_config)) {
|
|
return;
|
|
}
|
|
|
|
if (!init_vertices(program)) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (!bind_vertices(program, rebind ? 1 : 0)) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
#ifdef VIDEO_IN
|
|
|
|
void shaders_relink_input(ShaderProgram *program, const Project *project,
|
|
VideoCaptureArray *inputs, unsigned int input_index) {
|
|
shaders_free_input(program, input_index);
|
|
init_input(program, &project->config, inputs, input_index, true);
|
|
}
|
|
|
|
void shaders_link_inputs(ShaderProgram *program, const Project *project,
|
|
VideoCaptureArray *inputs, unsigned int buffer_count) {
|
|
unsigned int i;
|
|
program->sub_video_count = buffer_count;
|
|
for (i = 0; i < program->in_count; i++) {
|
|
init_input(program, &project->config, inputs, i, false);
|
|
}
|
|
}
|
|
|
|
void shaders_free_input(ShaderProgram *program, unsigned int input_index) {
|
|
unsigned int sub_index;
|
|
for (sub_index = 0; sub_index < program->sub_video_count; sub_index++) {
|
|
if (program->dma_images[input_index * program->sub_video_count +
|
|
sub_index] != EGL_NO_IMAGE_KHR) {
|
|
eglDestroyImageKHR(
|
|
program->egl_display,
|
|
program
|
|
->dma_images[input_index * program->sub_video_count + sub_index]);
|
|
program->dma_images[input_index * program->sub_video_count + sub_index] =
|
|
EGL_NO_IMAGE_KHR;
|
|
}
|
|
}
|
|
check_eglerror_ro("shaders_free_input/eglDestroyImageKHR");
|
|
}
|
|
|
|
static void update_inputs_tex_map(ShaderProgram *program,
|
|
const Context *context) {
|
|
unsigned int i;
|
|
unsigned int tex_i;
|
|
for (i = 0; i < program->in_count; i++) {
|
|
if (context->input_formats[i] != 0) {
|
|
tex_i = program->input_tex_map[i];
|
|
program->tex_map[tex_i] = program->tex_count +
|
|
i * program->sub_video_count +
|
|
context->input_index[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif /* VIDEO_IN */
|
|
|
|
void shaders_update(ShaderProgram *program, const File *fragment_shader,
|
|
unsigned int i, const Project *project) {
|
|
if (compile_shader(program->fragment_shaders[i], fragment_shader->path,
|
|
fragment_shader->content) &&
|
|
init_single_program(program, i, &project->config,
|
|
&project->state_config)) {
|
|
log_info("Program %d updated", i + 1);
|
|
}
|
|
}
|
|
|
|
void shaders_compute(ShaderProgram *program, const Context *context,
|
|
bool monitor, bool output_only) {
|
|
#ifdef VIDEO_IN
|
|
update_inputs_tex_map(program, context);
|
|
#endif /* VIDEO_IN */
|
|
|
|
if (!output_only) {
|
|
glBindVertexArray(program->vertex_array[0]);
|
|
|
|
update_viewport(program, context);
|
|
|
|
for (unsigned int i = 0; i < program->frag_count; i++) {
|
|
if (i != program->frag_output_index && i != program->frag_monitor_index) {
|
|
use_program(program, i, false, context);
|
|
}
|
|
}
|
|
} else {
|
|
glBindVertexArray(program->vertex_array[1]);
|
|
|
|
rebind_textures(program);
|
|
}
|
|
|
|
use_program(program,
|
|
monitor ? program->frag_monitor_index
|
|
: program->frag_output_index,
|
|
true, context);
|
|
|
|
check_glerror(program, "shaders_compute");
|
|
}
|
|
|
|
void shaders_free(const ShaderProgram *program) {
|
|
for (unsigned int i = 0; i < program->frag_count; i++) {
|
|
glDeleteProgram(program->programs[i]);
|
|
}
|
|
check_glerror_ro("shaders_free/glDeleteProgram");
|
|
|
|
glDeleteShader(program->vertex_shader);
|
|
for (unsigned int i = 0; i < program->frag_count; i++) {
|
|
glDeleteShader(program->fragment_shaders[i]);
|
|
}
|
|
check_glerror_ro("shaders_free/glDeleteShader");
|
|
|
|
glDeleteFramebuffers(program->frag_count, program->frame_buffers);
|
|
check_glerror_ro("shaders_free/glDeleteFramebuffers");
|
|
|
|
glDeleteTextures(program->tex_count, program->textures);
|
|
check_glerror_ro("shaders_free/glDeleteTextures");
|
|
|
|
#ifdef VIDEO_IN
|
|
glDeleteTextures(program->in_count * program->sub_video_count,
|
|
program->textures + program->tex_count);
|
|
check_glerror_ro("shaders_free/glDeleteTextures");
|
|
#endif
|
|
|
|
glDeleteBuffers(1, &program->vertex_buffer);
|
|
check_glerror_ro("shaders_free/glDeleteBuffers");
|
|
}
|
|
|
|
void shaders_free_window(const ShaderProgram *program, bool secondary) {
|
|
glDeleteVertexArrays(1, &program->vertex_array[secondary ? 1 : 0]);
|
|
check_glerror_ro("shaders_free_window/glDeleteVertexArrays");
|
|
}
|