Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
3x3 blur in Halide
#include <Halide.h>
using namespace Halide;
class Blur3x3 : public Generator<Blur3x3> {
public:
Input<Buffer<uint8_t>> _input{"input", 2};
Output<Buffer<uint8_t>> blurV{"blurV", 2};
Func blurH{"blurH"};
Var x, y, xi, yi;
void generate() {
Func input;
input(x, y) = cast<uint16_t>(BoundaryConditions::repeat_edge(_input)(x, y));
blurH(x, y) = (input(x - 1, y) + input(x, y) + input(x + 1, y)) / 3;
blurV(x, y) = cast<uint8_t>((blurH(x, y - 1) + blurH(x, y) + blurH(x, y + 1)) / 3);
}
void schedule() {
blurV.tile(x, y, xi, yi, 256, 32)
.vectorize(xi, 8)
.parallel(y);
blurH.compute_at(blurV, x)
.vectorize(x, 8);
}
};
HALIDE_REGISTER_GENERATOR(Blur3x3, blur3x3)
cmake_minimum_required(VERSION 3.14)
project(blur3x3)
# Set up language settings
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)
# Find Halide
find_package(Halide REQUIRED)
add_executable(blur_generator blur.gen.cpp)
target_link_libraries(blur_generator PRIVATE Halide::Generator)
add_halide_library(blur3x3 FROM blur_generator
REGISTRATION REG_CPP)
add_executable(run_blur ${REG_CPP})
target_link_libraries(run_blur PRIVATE Halide::RunGenMain blur3x3)
@alexreinking

This comment has been minimized.

Copy link
Owner Author

@alexreinking alexreinking commented May 4, 2020

Note that this requires the forthcoming CMake support in Halide's cmake-modern branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment