Skip to content

Commit

Permalink
graph: introduce base class for filters
Browse files Browse the repository at this point in the history
  • Loading branch information
sekrit-twc committed Jul 22, 2022
1 parent 10e0a03 commit 2467b35
Show file tree
Hide file tree
Showing 24 changed files with 125 additions and 243 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ libzimg_internal_la_SOURCES = \
src/zimg/depth/dither.h \
src/zimg/depth/quantize.h \
src/zimg/depth/quantize.cpp \
src/zimg/graph/filter_base.cpp \
src/zimg/graph/filter_base.h \
src/zimg/graph/filtergraph.cpp \
src/zimg/graph/filtergraph.h \
src/zimg/graph/graphbuilder.cpp \
Expand Down
2 changes: 2 additions & 0 deletions _msvc/zimg/zimg.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@
<ClInclude Include="..\..\src\zimg\depth\x86\depth_convert_x86.h" />
<ClInclude Include="..\..\src\zimg\depth\x86\dither_x86.h" />
<ClInclude Include="..\..\src\zimg\depth\x86\f16c_x86.h" />
<ClInclude Include="..\..\src\zimg\graph\filter_base.h" />
<ClInclude Include="..\..\src\zimg\graph\simple_filters.h" />
<ClInclude Include="..\..\src\zimg\graph\filtergraph.h" />
<ClInclude Include="..\..\src\zimg\graph\graphbuilder.h" />
Expand Down Expand Up @@ -461,6 +462,7 @@
<EnableEnhancedInstructionSet Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
<EnableEnhancedInstructionSet Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
</ClCompile>
<ClCompile Include="..\..\src\zimg\graph\filter_base.cpp" />
<ClCompile Include="..\..\src\zimg\graph\simple_filters.cpp" />
<ClCompile Include="..\..\src\zimg\graph\filtergraph.cpp" />
<ClCompile Include="..\..\src\zimg\graph\graphbuilder.cpp" />
Expand Down
18 changes: 12 additions & 6 deletions _msvc/zimg/zimg.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,18 @@
<ClInclude Include="..\..\src\zimg\graph\graphbuilder.h">
<Filter>Header Files\graph</Filter>
</ClInclude>
<ClInclude Include="..\..\src\zimg\graph\simple_filters.h">
<Filter>Header Files\graph</Filter>
</ClInclude>
<ClInclude Include="..\..\src\zimg\graph\filtergraph.h">
<Filter>Header Files\graph</Filter>
</ClInclude>
<ClInclude Include="..\..\src\zimg\graph\graphengine_except.h">
<Filter>Header Files\graph</Filter>
</ClInclude>
<ClInclude Include="..\..\src\zimg\graph\simple_filters.h">
<Filter>Header Files\graph</Filter>
</ClInclude>
<ClInclude Include="..\..\src\zimg\graph\filter_base.h">
<Filter>Header Files\graph</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\zimg\api\zimg.cpp">
Expand Down Expand Up @@ -482,14 +485,17 @@
<ClCompile Include="..\..\src\zimg\graph\graphbuilder.cpp">
<Filter>Source Files\graph</Filter>
</ClCompile>
<ClCompile Include="..\..\src\zimg\graph\simple_filters.cpp">
<Filter>Source Files\graph</Filter>
</ClCompile>
<ClCompile Include="..\..\src\zimg\graph\filtergraph.cpp">
<Filter>Source Files\graph</Filter>
</ClCompile>
<ClCompile Include="..\..\src\zimg\graph\graphengine_except.cpp">
<Filter>Source Files\graph</Filter>
</ClCompile>
<ClCompile Include="..\..\src\zimg\graph\simple_filters.cpp">
<Filter>Source Files\graph</Filter>
</ClCompile>
<ClCompile Include="..\..\src\zimg\graph\filter_base.cpp">
<Filter>Source Files\graph</Filter>
</ClCompile>
</ItemGroup>
</Project>
22 changes: 5 additions & 17 deletions src/zimg/colorspace/colorspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "common/except.h"
#include "common/pixel.h"
#include "common/zassert.h"
#include "graphengine/filter.h"
#include "graph/filter_base.h"
#include "colorspace.h"
#include "graph.h"
#include "operation.h"
Expand All @@ -14,8 +14,7 @@ namespace colorspace {

namespace {

class ColorspaceConversionImpl : public graphengine::Filter {
graphengine::FilterDescriptor m_desc;
class ColorspaceConversionImpl : public graph::PointFilter {
std::array<std::unique_ptr<Operation>, 6> m_operations;

void build_graph(const ColorspaceDefinition &in, const ColorspaceDefinition &out, const OperationParams &params, CPUClass cpu)
Expand All @@ -39,31 +38,20 @@ class ColorspaceConversionImpl : public graphengine::Filter {
}
}
public:
ColorspaceConversionImpl(unsigned width, unsigned height, const ColorspaceDefinition &in, const ColorspaceDefinition &out,
ColorspaceConversionImpl(unsigned width, unsigned height,
const ColorspaceDefinition &in, const ColorspaceDefinition &out,
const OperationParams &params, CPUClass cpu) :
m_desc{}
PointFilter(width, height, PixelType::FLOAT)
{
zassert_d(width <= pixel_max_width(PixelType::FLOAT), "overflow");

m_desc.format = { width, height, sizeof(float) };
m_desc.num_deps = 3;
m_desc.num_planes = 3;
m_desc.step = 1;
m_desc.flags.in_place = 1;

build_graph(in, out, params, cpu);
}

int version() const noexcept override { return VERSION; }

const graphengine::FilterDescriptor &descriptor() const noexcept override { return m_desc; }

pair_unsigned get_row_deps(unsigned i) const noexcept override { return{ i, i + 1 }; }

pair_unsigned get_col_deps(unsigned left, unsigned right) const noexcept override { return{ left, right }; }

void init_context(void *) const noexcept override {}

void process(const graphengine::BufferDescriptor in[3], const graphengine::BufferDescriptor out[3],
unsigned i, unsigned left, unsigned right, void *, void *) const noexcept override
{
Expand Down
36 changes: 5 additions & 31 deletions src/zimg/depth/depth_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "common/except.h"
#include "common/pixel.h"
#include "common/zassert.h"
#include "graphengine/filter.h"
#include "graph/filter_base.h"
#include "depth_convert.h"
#include "quantize.h"

Expand Down Expand Up @@ -88,8 +88,7 @@ depth_convert_func select_depth_convert_func(PixelType type_in, PixelType type_o
}


class IntegerLeftShift : public graphengine::Filter {
graphengine::FilterDescriptor m_desc;
class IntegerLeftShift : public graph::PointFilter {
left_shift_func m_func;
unsigned m_shift;

Expand All @@ -111,31 +110,19 @@ class IntegerLeftShift : public graphengine::Filter {
}
public:
IntegerLeftShift(left_shift_func func, unsigned width, unsigned height, const PixelFormat &pixel_in, const PixelFormat &pixel_out) :
m_desc{},
PointFilter(width, height, pixel_out.type),
m_func{ func },
m_shift{}
{
check_preconditions(width, pixel_in, pixel_out);

m_desc.format = { width, height, pixel_size(pixel_out.type) };
m_desc.num_deps = 1;
m_desc.num_planes = 1;
m_desc.step = 1;
m_desc.flags.in_place = pixel_size(pixel_in.type) == pixel_size(pixel_out.type);

m_shift = pixel_out.depth - pixel_in.depth;
}

int version() const noexcept override { return VERSION; }

const graphengine::FilterDescriptor &descriptor() const noexcept override { return m_desc; }

pair_unsigned get_row_deps(unsigned i) const noexcept override { return{ i, i + 1 }; }

pair_unsigned get_col_deps(unsigned left, unsigned right) const noexcept override { return{ left, right }; }

void init_context(void *) const noexcept override {}

void process(const graphengine::BufferDescriptor *in, const graphengine::BufferDescriptor *out,
unsigned i, unsigned left, unsigned right, void *, void *) const noexcept override
{
Expand All @@ -144,8 +131,7 @@ class IntegerLeftShift : public graphengine::Filter {
};


class ConvertToFloat : public graphengine::Filter {
graphengine::FilterDescriptor m_desc;
class ConvertToFloat : public graph::PointFilter {
depth_convert_func m_func;
depth_f16c_func m_f16c;
float m_scale;
Expand All @@ -166,34 +152,22 @@ class ConvertToFloat : public graphengine::Filter {
public:
ConvertToFloat(depth_convert_func func, depth_f16c_func f16c, unsigned width, unsigned height,
const PixelFormat &pixel_in, const PixelFormat &pixel_out) :
m_desc{},
PointFilter(width, height, pixel_out.type),
m_func{ func },
m_f16c{ f16c },
m_scale{},
m_offset{}
{
check_preconditions(width, pixel_in, pixel_out, !!f16c);

m_desc.format = { width, height, pixel_size(pixel_out.type) };
m_desc.num_deps = 1;
m_desc.num_planes = 1;
m_desc.step = 1;
m_desc.scratchpad_size = m_f16c ? (static_cast<checked_size_t>(width) * sizeof(float)).get() : 0;
m_desc.flags.in_place = pixel_size(pixel_in.type) == pixel_size(pixel_out.type);

std::tie(m_scale, m_offset) = get_scale_offset(pixel_in, pixel_out);
}

int version() const noexcept override { return VERSION; }

const graphengine::FilterDescriptor &descriptor() const noexcept override { return m_desc; }

pair_unsigned get_row_deps(unsigned i) const noexcept override { return{ i, i + 1 }; }

pair_unsigned get_col_deps(unsigned left, unsigned right) const noexcept override { return{ left, right }; }

void init_context(void *) const noexcept override {}

void process(const graphengine::BufferDescriptor *in, const graphengine::BufferDescriptor *out,
unsigned i, unsigned left, unsigned right, void *, void *tmp) const noexcept override
{
Expand Down
27 changes: 4 additions & 23 deletions src/zimg/depth/dither.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "common/except.h"
#include "common/pixel.h"
#include "common/zassert.h"
#include "graphengine/filter.h"
#include "graph/filter_base.h"
#include "blue.h"
#include "depth.h"
#include "dither.h"
Expand Down Expand Up @@ -227,8 +227,7 @@ class RandomDitherTable final : public OrderedDitherTable {
};


class OrderedDither : public graphengine::Filter {
graphengine::FilterDescriptor m_desc;
class OrderedDither : public graph::PointFilter {
std::shared_ptr<OrderedDitherTable> m_dither_table;
dither_convert_func m_func;
dither_f16c_func m_f16c;
Expand All @@ -248,7 +247,7 @@ class OrderedDither : public graphengine::Filter {
public:
OrderedDither(std::shared_ptr<OrderedDitherTable> table, dither_convert_func func, dither_f16c_func f16c, unsigned width, unsigned height,
const PixelFormat &pixel_in, const PixelFormat &pixel_out, unsigned plane) :
m_desc{},
PointFilter(width, height, pixel_out.type),
m_dither_table{ std::move(table) },
m_func{ func },
m_f16c{ f16c },
Expand All @@ -259,26 +258,14 @@ class OrderedDither : public graphengine::Filter {
{
check_preconditions(width, pixel_in, pixel_out);

m_desc.format = { width, height, pixel_size(pixel_out.type) };
m_desc.num_deps = 1;
m_desc.num_planes = 1;
m_desc.step = 1;
m_desc.scratchpad_size = m_f16c ? (static_cast<checked_size_t>(width) * sizeof(float)).get() : 0;
m_desc.flags.in_place = pixel_size(pixel_in.type) == pixel_size(pixel_out.type);

std::tie(m_scale, m_offset) = get_scale_offset(pixel_in, pixel_out);
}

int version() const noexcept override { return VERSION; }

const graphengine::FilterDescriptor &descriptor() const noexcept override { return m_desc; }

pair_unsigned get_row_deps(unsigned i) const noexcept override { return{ i, i + 1 }; }

pair_unsigned get_col_deps(unsigned left, unsigned right) const noexcept override { return{ left, right }; }

void init_context(void *) const noexcept override {}

void process(const graphengine::BufferDescriptor *in, const graphengine::BufferDescriptor *out,
unsigned i, unsigned left, unsigned right, void *, void *tmp) const noexcept override
{
Expand All @@ -297,11 +284,10 @@ class OrderedDither : public graphengine::Filter {
};


class ErrorDiffusion : public graphengine::Filter {
class ErrorDiffusion : public graph::FilterBase {
public:
typedef void (*ed_func)(const void *src, void *dst, void *error_top, void *error_cur, float scale, float offset, unsigned bits, unsigned width);
private:
graphengine::FilterDescriptor m_desc;
ed_func m_func;
dither_f16c_func m_f16c;
float m_scale;
Expand All @@ -318,7 +304,6 @@ class ErrorDiffusion : public graphengine::Filter {
}
public:
ErrorDiffusion(ed_func func, dither_f16c_func f16c, unsigned width, unsigned height, const PixelFormat &pixel_in, const PixelFormat &pixel_out) :
m_desc{},
m_func{ func },
m_f16c{ f16c },
m_scale{},
Expand All @@ -342,10 +327,6 @@ class ErrorDiffusion : public graphengine::Filter {
std::tie(m_scale, m_offset) = get_scale_offset(pixel_in, pixel_out);
}

int version() const noexcept override { return VERSION; }

const graphengine::FilterDescriptor &descriptor() const noexcept override { return m_desc; }

pair_unsigned get_row_deps(unsigned i) const noexcept override { return{ i, i + 1 }; }

pair_unsigned get_col_deps(unsigned, unsigned) const noexcept override { return{ 0, m_desc.format.width }; }
Expand Down
11 changes: 2 additions & 9 deletions src/zimg/depth/x86/error_diffusion_avx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "common/pixel.h"
#include "common/zassert.h"
#include "depth/quantize.h"
#include "graphengine/filter.h"
#include "graph/filter_base.h"
#include "dither_x86.h"

#include "common/x86/avx_util.h"
Expand Down Expand Up @@ -447,9 +447,7 @@ auto select_error_diffusion_avx2_func(PixelType pixel_in, PixelType pixel_out)
}


class ErrorDiffusionAVX2 final : public graphengine::Filter {
graphengine::FilterDescriptor m_desc;

class ErrorDiffusionAVX2 : public graph::FilterBase {
decltype(select_error_diffusion_scalar_func({}, {})) m_scalar_func;
decltype(select_error_diffusion_avx2_func({}, {})) m_avx2_func;

Expand Down Expand Up @@ -480,7 +478,6 @@ class ErrorDiffusionAVX2 final : public graphengine::Filter {
}
public:
ErrorDiffusionAVX2(unsigned width, unsigned height, const PixelFormat &pixel_in, const PixelFormat &pixel_out) try :
m_desc{},
m_scalar_func{ select_error_diffusion_scalar_func(pixel_in.type, pixel_out.type) },
m_avx2_func{ select_error_diffusion_avx2_func(pixel_in.type, pixel_out.type) },
m_scale{},
Expand Down Expand Up @@ -508,10 +505,6 @@ class ErrorDiffusionAVX2 final : public graphengine::Filter {
error::throw_<error::OutOfMemory>();
}

int version() const noexcept override { return VERSION; }

const graphengine::FilterDescriptor &descriptor() const noexcept override { return m_desc; }

pair_unsigned get_row_deps(unsigned i) const noexcept override
{
unsigned last = std::min(i, UINT_MAX - 8) + 8;
Expand Down
11 changes: 2 additions & 9 deletions src/zimg/depth/x86/error_diffusion_sse2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "common/pixel.h"
#include "common/zassert.h"
#include "depth/quantize.h"
#include "graphengine/filter.h"
#include "graph/filter_base.h"
#include "dither_x86.h"

#include "common/x86/sse2_util.h"
Expand Down Expand Up @@ -350,9 +350,7 @@ auto select_error_diffusion_sse2_func(PixelType pixel_in, PixelType pixel_out)
}


class ErrorDiffusionSSE2 : public graphengine::Filter {
graphengine::FilterDescriptor m_desc;

class ErrorDiffusionSSE2 : public graph::FilterBase {
decltype(select_error_diffusion_scalar_func({}, {})) m_scalar_func;
decltype(select_error_diffusion_sse2_func({}, {})) m_sse2_func;
dither_f16c_func m_f16c;
Expand Down Expand Up @@ -388,7 +386,6 @@ class ErrorDiffusionSSE2 : public graphengine::Filter {
}
public:
ErrorDiffusionSSE2(unsigned width, unsigned height, const PixelFormat &pixel_in, const PixelFormat &pixel_out, CPUClass cpu) try :
m_desc{},
m_scalar_func{ select_error_diffusion_scalar_func(pixel_in.type, pixel_out.type) },
m_sse2_func{ select_error_diffusion_sse2_func(pixel_in.type, pixel_out.type) },
m_f16c{},
Expand Down Expand Up @@ -422,10 +419,6 @@ class ErrorDiffusionSSE2 : public graphengine::Filter {
error::throw_<error::OutOfMemory>();
}

int version() const noexcept override { return VERSION; }

const graphengine::FilterDescriptor &descriptor() const noexcept override { return m_desc; }

pair_unsigned get_row_deps(unsigned i) const noexcept override
{
unsigned last = std::min(i, UINT_MAX - 4) + 4;
Expand Down
Loading

0 comments on commit 2467b35

Please sign in to comment.