-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/computeshaders #250
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/********************************************************************************* | ||
* | ||
* Inviwo - Interactive Visualization Workshop | ||
* | ||
* Copyright (c) 2024-2025 Inviwo Foundation | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*********************************************************************************/ | ||
|
||
layout (std430, binding=3) buffer BufferObject { | ||
vec4 minmax[]; | ||
}; | ||
|
||
uniform uint arrayLength; | ||
|
||
shared vec4 workgroup_min[256 / 32]; | ||
shared vec4 workgroup_max[256 / 32]; | ||
|
||
layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in; | ||
|
||
void main(){ | ||
uint threadId = gl_LocalInvocationIndex; | ||
uint gridSize = gl_WorkGroupSize.x * gl_NumWorkGroups.x; | ||
uint i = gl_WorkGroupID.x * gl_WorkGroupSize.x + threadId; | ||
|
||
vec4 minVal = vec4(1.0 / 0.0); | ||
vec4 maxVal = vec4(-1.0 / 0.0); | ||
|
||
while (i < arrayLength) { | ||
minVal = min(minVal, minmax[2 * i]); | ||
maxVal = max(maxVal, minmax[2 * i + 1]); | ||
i += gridSize; | ||
} | ||
|
||
minVal = subgroupMin(minVal); | ||
maxVal = subgroupMax(maxVal); | ||
if (subgroupElect()) { | ||
workgroup_min[gl_SubgroupID] = minVal; | ||
workgroup_max[gl_SubgroupID] = maxVal; | ||
} | ||
barrier(); | ||
|
||
if (gl_LocalInvocationIndex == 0) { | ||
for (uint i = 0; i < gl_NumSubgroups; ++i) { | ||
minVal = min(minVal, workgroup_min[i]); | ||
maxVal = max(maxVal, workgroup_max[i]); | ||
} | ||
uint index = gl_WorkGroupID.x; | ||
minmax[2 * index] = minVal; | ||
minmax[2 * index + 1] = maxVal; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/********************************************************************************* | ||
* | ||
* Inviwo - Interactive Visualization Workshop | ||
* | ||
* Copyright (c) 2024-2025 Inviwo Foundation | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*********************************************************************************/ | ||
|
||
#if !defined IMAGE_FORMAT | ||
#define IMAGE_FORMAT r32f | ||
#endif | ||
#if !defined IMAGE_SOURCE | ||
# define IMAGE_SOURCE image3D | ||
#endif | ||
|
||
#if defined USE_IMAGE_SAMPLER | ||
# define GetValue(gsampler, imageCoord) texelFetch(gsampler, imageCoord, 0) | ||
# define GetSize(gsampler) textureSize(gsampler, 0) | ||
#else | ||
# define GetValue(gimage, imageCoord) imageLoad(gimage, imageCoord) | ||
# define GetSize(gimage) imageSize(gimage) | ||
#endif // USE_IMAGE_SAMPLER | ||
|
||
#if defined USE_IMAGE_SAMPLER | ||
uniform IMAGE_SOURCE volume; | ||
#else | ||
uniform layout(binding=0, IMAGE_FORMAT) readonly IMAGE_SOURCE volume; | ||
#endif // USE_IMAGE_SAMPLER | ||
|
||
layout (std430, binding=3) buffer BufferObject { | ||
vec4 minmax[]; | ||
}; | ||
|
||
// 32x32 per workgroup | ||
shared vec4 workgroup_min[1024 / 32]; | ||
shared vec4 workgroup_max[1024 / 32]; | ||
|
||
layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in; | ||
|
||
void main(){ | ||
ivec3 volDims = GetSize(volume); | ||
|
||
vec4 minVal = vec4(1.0 / 0.0); | ||
vec4 maxVal = vec4(-1.0 / 0.0); | ||
|
||
ivec3 volIndex = ivec3(gl_GlobalInvocationID.x, gl_GlobalInvocationID.y, gl_GlobalInvocationID.z); | ||
|
||
if (all(lessThan(volIndex, volDims))) { | ||
for (int z = 0; z < volDims.z; ++z) { | ||
vec4 val = GetValue(volume, volIndex + ivec3(0, 0, z)); | ||
minVal = min(minVal, val); | ||
maxVal = max(maxVal, val); | ||
} | ||
} | ||
|
||
minVal = subgroupMin(minVal); | ||
maxVal = subgroupMax(maxVal); | ||
if (subgroupElect()) { | ||
workgroup_min[gl_SubgroupID] = minVal; | ||
workgroup_max[gl_SubgroupID] = maxVal; | ||
} | ||
barrier(); | ||
|
||
if (gl_LocalInvocationIndex == 0) { | ||
for (uint i = 0; i < gl_NumSubgroups; ++i) { | ||
minVal = min(minVal, workgroup_min[i]); | ||
maxVal = max(maxVal, workgroup_max[i]); | ||
} | ||
uint index = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; | ||
minmax[2 * index] = minVal; | ||
minmax[2 * index + 1] = maxVal; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/********************************************************************************* | ||
* | ||
* Inviwo - Interactive Visualization Workshop | ||
* | ||
* Copyright (c) 2024-2025 Inviwo Foundation | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
*********************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include <inviwo/computeshaderexamples/computeshaderexamplesmoduledefine.h> | ||
#include <inviwo/core/processors/processor.h> | ||
#include <inviwo/core/properties/ordinalproperty.h> | ||
#include <inviwo/core/properties/boolproperty.h> | ||
#include <inviwo/core/ports/volumeport.h> | ||
|
||
#include <modules/opengl/shader/shader.h> | ||
|
||
namespace inviwo { | ||
|
||
class IVW_MODULE_COMPUTESHADEREXAMPLES_API ComputeShaderMinMax : public Processor { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clang-tidy diagnosticmisc/computeshaderexamples/include/inviwo/computeshaderexamples/processors/computeshaderminmax.h:42:44: warning: [cppcoreguidelines-avoid-non-const-global-variables]
42 | class IVW_MODULE_COMPUTESHADEREXAMPLES_API ComputeShaderMinMax : public Processor {
| ^ |
||
public: | ||
ComputeShaderMinMax(); | ||
|
||
virtual void initializeResources() override; | ||
virtual void process() override; | ||
|
||
virtual const ProcessorInfo& getProcessorInfo() const override; | ||
static const ProcessorInfo processorInfo_; | ||
|
||
private: | ||
VolumeInport volume_; | ||
|
||
FloatVec4Property minValues_; | ||
FloatVec4Property maxValues_; | ||
BoolProperty logErrorOnly_; | ||
|
||
Shader shaderSampleVolume_; | ||
Shader shaderLinear_; | ||
}; | ||
|
||
} // namespace inviwo |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,15 +28,14 @@ | |
*********************************************************************************/ | ||
|
||
#include <inviwo/computeshaderexamples/processors/computeshaderimageexample.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clang-tidy diagnosticmisc/computeshaderexamples/src/processors/computeshaderimageexample.cpp:30:10: error: [clang-diagnostic-error]
30 | #include <inviwo/computeshaderexamples/processors/computeshaderimageexample.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
#include <modules/opengl/openglmodule.h> | ||
#include <modules/opengl/shader/shadermanager.h> | ||
|
||
#include <inviwo/core/datastructures/image/image.h> | ||
#include <inviwo/core/datastructures/image/layer.h> | ||
#include <modules/opengl/texture/texture2d.h> | ||
#include <modules/opengl/image/layergl.h> | ||
|
||
#include <modules/opengl/texture/textureutils.h> | ||
#include <modules/opengl/texture/textureunit.h> | ||
#include <modules/opengl/shader/shaderutils.h> | ||
|
||
namespace inviwo { | ||
|
@@ -51,13 +50,14 @@ const ProcessorInfo ComputeShaderImageExample::processorInfo_{ | |
R"(A processor to show how compute shaders can be utilized to create images. | ||
Uses shader `roll.comp` to create a simple procedural image. | ||
|
||
C++ and GSLS source code is heavily inspired by http://wili.cc/blog/opengl-cs.html)"_help, | ||
C++ and GSLS source code is heavily inspired by | ||
[wili.cc/blog/opengl-cs.html](http://wili.cc/blog/opengl-cs.html))"_help, | ||
}; | ||
const ProcessorInfo& ComputeShaderImageExample::getProcessorInfo() const { return processorInfo_; } | ||
|
||
ComputeShaderImageExample::ComputeShaderImageExample() | ||
: Processor{} | ||
, outport_{"outport", "Generated output image"_help} | ||
, outport_{"outport", "Output image"_help} | ||
, shader_{{{ShaderType::Compute, "roll.comp"}}, Shader::Build::No} | ||
, roll_{"roll", "Roll", | ||
util::ordinalLength(0.0f, 10.0f) | ||
|
@@ -75,13 +75,14 @@ void ComputeShaderImageExample::initializeResources() { shader_.build(); } | |
void ComputeShaderImageExample::process() { | ||
// inspired by http://wili.cc/blog/opengl-cs.html | ||
|
||
glActiveTexture(GL_TEXTURE0); | ||
const TextureUnit unit; | ||
unit.activate(); | ||
|
||
auto img = std::make_shared<Image>(size2_t{512, 512}, DataFormat<float>::get()); | ||
|
||
auto layerGL = img->getColorLayer()->getEditableRepresentation<LayerGL>(); | ||
auto texHandle = layerGL->getTexture()->getID(); | ||
glBindImageTexture(0, texHandle, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); | ||
glBindImageTexture(unit.getUnitNumber(), texHandle, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F); | ||
|
||
layerGL->setSwizzleMask(swizzlemasks::luminance); | ||
|
||
|
@@ -90,7 +91,7 @@ void ComputeShaderImageExample::process() { | |
|
||
layerGL->getTexture()->bind(); | ||
|
||
shader_.setUniform("dest", 0); | ||
shader_.setUniform("dest", unit.getUnitNumber()); | ||
|
||
glDispatchCompute(512 / 16, 512 / 16, 1); // 512^2 threads in blocks of 16^2 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clang-tidy diagnostic
misc/computeshaderexamples/include/inviwo/computeshaderexamples/processors/computeshaderminmax.h:32:10: error: [clang-diagnostic-error]