Skip to content
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

Merged
merged 4 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/presets/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"IVW_MODULE_FFMPEG": { "type": "BOOL", "value": "ON"},
"IVW_MODULE_WEBBROWSER": { "type": "BOOL", "value": "ON"},
"IVW_MODULE_TETRAMESH": { "type": "BOOL", "value": "ON"},
"IVW_MODULE_COMPUTESHADEREXAMPLES": { "type": "BOOL", "value": "ON"},

"IVW_MODULE_GRAPHVIZ": { "type": "BOOL", "value": "ON"},
"IVW_MODULE_OPENMESH": { "type": "BOOL", "value": "ON"},
Expand Down
4 changes: 4 additions & 0 deletions misc/computeshaderexamples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(HEADER_FILES
include/inviwo/computeshaderexamples/computeshaderexamplesmoduledefine.h
include/inviwo/computeshaderexamples/processors/computeshaderbufferexample.h
include/inviwo/computeshaderexamples/processors/computeshaderimageexample.h
include/inviwo/computeshaderexamples/processors/computeshaderminmax.h
)
ivw_group("Header Files" ${HEADER_FILES})

Expand All @@ -18,6 +19,7 @@ set(SOURCE_FILES
src/computeshaderexamplesmodule.cpp
src/processors/computeshaderbufferexample.cpp
src/processors/computeshaderimageexample.cpp
src/processors/computeshaderminmax.cpp
)
ivw_group("Source Files" ${SOURCE_FILES})

Expand All @@ -26,6 +28,8 @@ ivw_group("Source Files" ${SOURCE_FILES})
set(SHADER_FILES
glsl/roll.comp
glsl/spiral.comp
glsl/minmaxlinear.comp
glsl/minmaxvolume.comp
)
ivw_group("Shader Files" ${SHADER_FILES})

Expand Down
1 change: 1 addition & 0 deletions misc/computeshaderexamples/depends.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# List modules on the format "Inviwo<ModuleName>Module"
set(dependencies
InviwoOpenGLModule
InviwoBaseModule
#InviwoOpenGLModule # Example dependency
)

Expand Down
72 changes: 72 additions & 0 deletions misc/computeshaderexamples/glsl/minmaxlinear.comp
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;
}
}
94 changes: 94 additions & 0 deletions misc/computeshaderexamples/glsl/minmaxvolume.comp
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
Expand Up @@ -33,7 +33,6 @@
#include <inviwo/core/common/inviwo.h>
#include <inviwo/core/processors/processor.h>
#include <inviwo/core/properties/ordinalproperty.h>
#include <inviwo/core/properties/transferfunctionproperty.h>
#include <inviwo/core/ports/imageport.h>

#include <modules/opengl/shader/shader.h>
Expand Down
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>

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]

'inviwo/computeshaderexamples/computeshaderexamplesmoduledefine.h' file not found

   32 | #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 {

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:42:44: warning: [cppcoreguidelines-avoid-non-const-global-variables]

variable 'ComputeShaderMinMax' is non-const and globally accessible, consider making it const

   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
Expand Up @@ -31,6 +31,7 @@
#include <inviwo/computeshaderexamples/processors/computeshaderbufferexample.h>
#include <inviwo/computeshaderexamples/processors/computeshaderimageexample.h>

#include <inviwo/computeshaderexamples/processors/computeshaderminmax.h>
#include <modules/opengl/shader/shadermanager.h>

namespace inviwo {
Expand All @@ -45,6 +46,7 @@ ComputeShaderExamplesModule::ComputeShaderExamplesModule(InviwoApplication* app)
// Processors
registerProcessor<ComputeShaderBufferExample>();
registerProcessor<ComputeShaderImageExample>();
registerProcessor<ComputeShaderMinMax>();
}

} // namespace inviwo
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@
*********************************************************************************/

#include <inviwo/computeshaderexamples/processors/computeshaderimageexample.h>

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/src/processors/computeshaderimageexample.cpp:30:10: error: [clang-diagnostic-error]

'inviwo/computeshaderexamples/processors/computeshaderimageexample.h' file not found

   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 {
Expand All @@ -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)
Expand All @@ -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);

Expand All @@ -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

Expand Down
Loading
Loading