Skip to content

Runtime effect additions #322

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
36 changes: 33 additions & 3 deletions src/skia/RuntimeEffect.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdexcept>
#include "common.h"
#include <include/effects/SkRuntimeEffect.h>
//#include <include/core/SkM44.h> // defines SkV3, SkV4 ; M44 used in Matrix/Canvas ; Revisit.
//#include <include/core/SkM44.h> // defines SkV2, SkV3, SkV4 ; M44 used in Matrix/Canvas ; Revisit.
#include <pybind11/stl_bind.h>

PYBIND11_MAKE_OPAQUE(std::vector<SkRuntimeEffect::ChildPtr>)
Expand All @@ -16,6 +16,22 @@ py::class_<SkSpan<const SkRuntimeEffect::ChildPtr>> span_runtime_effect_childptr

py::class_<SkRuntimeEffectBuilder> runtime_effect_builder(m, "RuntimeEffectBuilder");

py::class_<SkV2>(m, "V2")
.def(py::init(
[] (float x, float y) {
return SkV2{x, y};
}))
.def(py::init(
[] (py::tuple v2) {
if (v2.size() != 2)
throw py::value_error("V2 must have exactly two elements.");
return SkV2{v2[0].cast<float>(), v2[1].cast<float>()};
}),
py::arg("v2"))
;

py::implicitly_convertible<py::tuple, SkV2>();

py::class_<SkV3>(m, "V3")
.def(py::init(
[] (float x, float y, float z) {
Expand Down Expand Up @@ -183,6 +199,18 @@ runtime_effect_builder
v = uniform;
},
py::arg("name"), py::arg("uniform"))
.def("setUniform",
[] (SkRuntimeEffectBuilder& builder, std::string_view name, float uniform) {
auto v = builder.uniform(name);
v = uniform;
},
py::arg("name"), py::arg("uniform"))
.def("setUniform",
[] (SkRuntimeEffectBuilder& builder, std::string_view name, const SkV2& uniform) {
auto v = builder.uniform(name);
v = uniform;
},
py::arg("name"), py::arg("uniform"))
.def("setUniform",
[] (SkRuntimeEffectBuilder& builder, std::string_view name, const SkV3& uniform) {
auto v = builder.uniform(name);
Expand All @@ -197,9 +225,11 @@ runtime_effect_builder
py::arg("name"), py::arg("uniform"))
.def("setUniform",
[] (SkRuntimeEffectBuilder& builder, std::string_view name, py::list vN) {
if (vN.size() != 3 && vN.size() != 4)
throw py::value_error("Input must have exactly three or four elements.");
if (vN.size() != 2 && vN.size() != 3 && vN.size() != 4)
throw py::value_error("Input must have exactly two, three or four elements.");
auto v = builder.uniform(name);
if (vN.size() == 2)
v = SkV2{vN[0].cast<float>(), vN[1].cast<float>()};
if (vN.size() == 3)
v = SkV3{vN[0].cast<float>(), vN[1].cast<float>(), vN[2].cast<float>()};
if (vN.size() == 4)
Expand Down
Loading