aboutsummaryrefslogtreecommitdiff
path: root/src/compute_shader.h
blob: 9f5c5cb508f30d745f686bf35e008a01fa4549c4 (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
#pragma once

#include "util.h"

class ComputeShader {
private:
	const GLuint _id;

public:
	struct Guard {
		const GLuint _id;

		Guard(GLuint id): _id(id) {
			glUseProgram(_id);
		}
		~Guard() {
			glUseProgram(0);
		}
	};

	Guard use() const {
		return Guard(_id);
	}

	ComputeShader(const std::string& src):
		_id(glCreateProgram()) {
		glAttachShader(_id, util::compileShader(src, GL_COMPUTE_SHADER));
		glLinkProgram(_id);
	};
	~ComputeShader() {
		glDeleteProgram(_id);
	}

	GLuint setUniform(const std::string& name, float x, float y) const {
		GLuint id = util::getUniform(_id, name);
		glUniform2f(id, x, y);
		return id;
	}

	void workOn(GLuint buffer) const {
		glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, buffer);
	}

	void dispatch(std::size_t dimX) const {
		glDispatchCompute(dimX, 1, 1);
	}
};