Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
/**
* @file
*/
#pragma once
#include "compute/Shader.h"
#include "core/Singleton.h"
namespace compute {
class TestShader : public compute::Shader {
private:
using Super = compute::Shader;
int _setupCalls = 0;
mutable compute::Id _buffer_example_buf = compute::InvalidId;
mutable compute::Id _buffer_example_buf2 = compute::InvalidId;
compute::Id _kernelexample = compute::InvalidId;
mutable compute::Id _buffer_example2_buf = compute::InvalidId;
mutable compute::Id _buffer_example2_buf2 = compute::InvalidId;
compute::Id _kernelexample2 = compute::InvalidId;
compute::Id _kernelvector_add = compute::InvalidId;
public:
static inline TestShader& getInstance() {
return core::Singleton<TestShader>::getInstance();
}
struct Data {
int foo;
char foo2;
float foo3;
float foo4[4];
};
/**
* @brief Load the vertex and fragment shaders and verifies that its attributes and uniforms are used.
* @note If an attribute or an uniform isn't active, a message will be printed about that fact - but
* the setup process won't fail.
*/
bool setup() override {
++_setupCalls;
if (_initialized) {
return true;
}
if (!init()) {
return false;
}
if (!loadProgram("shaders/test")) {
return false;
}
_kernelexample = compute::createKernel(_program, "example");
_kernelexample2 = compute::createKernel(_program, "example2");
_kernelvector_add = compute::createKernel(_program, "vector_add");
return true;
}
void shutdown() override {
if (_setupCalls == 0) {
return;
}
--_setupCalls;
if (_setupCalls > 0) {
return;
}
compute::deleteBuffer(_buffer_example_buf);
compute::deleteBuffer(_buffer_example_buf2);
compute::deleteKernel(_kernelexample);
compute::deleteBuffer(_buffer_example2_buf);
compute::deleteBuffer(_buffer_example2_buf2);
compute::deleteKernel(_kernelexample2);
compute::deleteKernel(_kernelvector_add);
Super::shutdown();
}
bool example(const char* buf, size_t bufSize, char* buf2, size_t buf2Size, int workSize, int workDim) const {
core_assert(workSize % workDim == 0);
if (_buffer_example_buf == InvalidId) {
_buffer_example_buf = compute::createBuffer(compute::BufferFlag::ReadOnly, bufSize, const_cast<char*>(buf));
} else {
compute::updateBuffer(_buffer_example_buf, bufSize, buf);
}
compute::kernelArg(_kernelexample, 0, _buffer_example_buf);
if (_buffer_example_buf2 == InvalidId) {
_buffer_example_buf2 = compute::createBuffer(compute::BufferFlag::ReadWrite, buf2Size, buf2);
} else {
compute::updateBuffer(_buffer_example_buf2, buf2Size, buf2);
}
compute::kernelArg(_kernelexample, 1, _buffer_example_buf2);
const bool state = compute::kernelRun(_kernelexample, workSize, workDim);
compute::readBuffer(_buffer_example_buf2, buf2Size, buf2);
return state;
}
bool example2(const char* buf, size_t bufSize, char* buf2, size_t buf2Size, int N, int workSize, int workDim) const {
core_assert(workSize % workDim == 0);
if (_buffer_example2_buf == InvalidId) {
_buffer_example2_buf = compute::createBuffer(compute::BufferFlag::ReadOnly, bufSize, const_cast<char*>(buf));
} else {
compute::updateBuffer(_buffer_example2_buf, bufSize, buf);
}
compute::kernelArg(_kernelexample2, 0, _buffer_example2_buf);
if (_buffer_example2_buf2 == InvalidId) {
_buffer_example2_buf2 = compute::createBuffer(compute::BufferFlag::ReadWrite, buf2Size, buf2);
} else {
compute::updateBuffer(_buffer_example2_buf2, buf2Size, buf2);
}
compute::kernelArg(_kernelexample2, 1, _buffer_example2_buf2);
compute::kernelArg(_kernelexample2, 2, N);
const bool state = compute::kernelRun(_kernelexample2, workSize, workDim);
compute::readBuffer(_buffer_example2_buf2, buf2Size, buf2);
return state;
}
bool vector_add(const int *A, const int *B, int *C, int workSize, int workDim) const {
core_assert(workSize % workDim == 0);
compute::kernelArg(_kernelvector_add, 0, *A);
compute::kernelArg(_kernelvector_add, 1, *B);
compute::kernelArg(_kernelvector_add, 2, *C);
const bool state = compute::kernelRun(_kernelvector_add, workSize, workDim);
return state;
}
};
typedef std::shared_ptr<TestShader> TestShaderPtr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment