Created
August 18, 2017 14:31
-
-
Save mgerhardy/cd80fde91414dc8c95770f38960f13e3 to your computer and use it in GitHub Desktop.
Generated c++ code from opencl parser/code generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file | |
*/ | |
#pragma once | |
#include "compute/Shader.h" | |
#include "core/Singleton.h" | |
#include "core/Assert.h" | |
#include <glm/gtc/vec1.hpp> | |
#include <glm/vec2.hpp> | |
#include <glm/vec3.hpp> | |
namespace compute { | |
class NoiseShader : public compute::Shader { | |
private: | |
using Super = compute::Shader; | |
int _setupCalls = 0; | |
mutable compute::Id _buffer_ridgedMF_output = compute::InvalidId; | |
compute::Id _kernelridgedMF = compute::InvalidId; | |
public: | |
static inline NoiseShader& getInstance() { | |
return core::Singleton<NoiseShader>::getInstance(); | |
} | |
/** | |
* @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/noise")) { | |
return false; | |
} | |
_kernelridgedMF = compute::createKernel(_program, "ridgedMF"); | |
return true; | |
} | |
void shutdown() override { | |
if (_setupCalls == 0) { | |
return; | |
} | |
--_setupCalls; | |
if (_setupCalls > 0) { | |
return; | |
} | |
compute::deleteBuffer(_buffer_ridgedMF_output); | |
compute::deleteKernel(_kernelridgedMF); | |
Super::shutdown(); | |
} | |
bool ridgedMF( | |
/* uchar4 **/ std::vector<uint8_t>& output, | |
const glm::vec2& pos, | |
float frequency, | |
float lacunarity, | |
int32_t octaves, | |
float amplitude, | |
const glm::ivec2& workSize | |
) const { | |
if (_buffer_ridgedMF_output == InvalidId) { | |
const compute::BufferFlag flags = compute::BufferFlag::ReadWrite | bufferFlags(&output[0], core::vectorSize(output)); | |
_buffer_ridgedMF_output = compute::createBufferFromType(flags, output); | |
} else { | |
compute::updateBufferFromType(_buffer_ridgedMF_output, output); | |
} | |
compute::kernelArg(_kernelridgedMF, 0, _buffer_ridgedMF_output); | |
compute::kernelArg(_kernelridgedMF, 1, pos); | |
compute::kernelArg(_kernelridgedMF, 2, frequency); | |
compute::kernelArg(_kernelridgedMF, 3, lacunarity); | |
compute::kernelArg(_kernelridgedMF, 4, octaves); | |
compute::kernelArg(_kernelridgedMF, 5, amplitude); | |
glm::ivec3 globalWorkSize(0); | |
for (int i = 0; i < 2; ++i) { | |
globalWorkSize[i] += workSize[i]; | |
} | |
const bool state = compute::kernelRun(_kernelridgedMF, globalWorkSize, 2); | |
if (state) { | |
core_assert_always(compute::readBufferIntoVector(_buffer_ridgedMF_output, output)); | |
} | |
return state; | |
} | |
}; | |
typedef std::shared_ptr<NoiseShader> NoiseShaderPtr; | |
}; |
Notice the detection of the dimensions. The worksize is automatically glm::ivec1, glm::ivec2 or glm::ivec3.
Example for calling the shader: https://github.com/mgerhardy/engine/blob/master/src/modules/noise/tests/NoiseShaderTest.cpp
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the source opencl shader: https://github.com/mgerhardy/engine/blob/master/data/shared/shaders/noise.cl