Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
szellmann committed Jun 7, 2024
1 parent 6580541 commit f63c767
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 133 deletions.
7 changes: 1 addition & 6 deletions DeviceCopyableObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -1251,41 +1251,35 @@ struct Geometry
struct {
basic_triangle<3,float> *data{nullptr};
size_t len{0};
Array vertexAttributes[5];
Array index;
Array normal;
Array tangent;
} asTriangle;
struct {
basic_triangle<3,float> *data{nullptr};
size_t len{0};
Array vertexAttributes[5];
Array index;
Array normal;
Array tangent;
} asQuad;
struct {
basic_sphere<float> *data{nullptr};
size_t len{0};
Array vertexAttributes[5];
Array index;
} asSphere;
struct {
dco::Cone *data{nullptr};
size_t len{0};
Array vertexAttributes[5];
Array index;
} asCone;
struct {
basic_cylinder<float> *data{nullptr};
size_t len{0};
Array vertexAttributes[5];
Array index;
} asCylinder;
struct {
dco::BezierCurve *data{nullptr};
size_t len{0};
Array vertexAttributes[5];
Array index;
} asBezierCurve;
struct {
Expand All @@ -1299,6 +1293,7 @@ struct Geometry
} asInstance;

Array primitiveAttributes[5];
Array vertexAttributes[5];

VSNRAY_FUNC
inline bool isValid() const
Expand Down
191 changes: 82 additions & 109 deletions renderer/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,126 +364,99 @@ inline vec4 getTangent(
return tng;
}

VSNRAY_FUNC
inline dco::Array getVertexColors(const dco::Geometry &geom, dco::Attribute attrib)
{
dco::Array arr;

if (attrib != dco::Attribute::None) {
if (geom.type == dco::Geometry::Triangle)
return geom.asTriangle.vertexAttributes[(int)attrib];
else if (geom.type == dco::Geometry::Quad)
return geom.asQuad.vertexAttributes[(int)attrib];
else if (geom.type == dco::Geometry::Sphere)
return geom.asSphere.vertexAttributes[(int)attrib];
else if (geom.type == dco::Geometry::Cone)
return geom.asCone.vertexAttributes[(int)attrib];
else if (geom.type == dco::Geometry::Cylinder)
return geom.asCylinder.vertexAttributes[(int)attrib];
}

return arr;
}

VSNRAY_FUNC
inline dco::Array getPrimitiveColors(const dco::Geometry &geom, dco::Attribute attrib)
{
dco::Array arr;

if (attrib != dco::Attribute::None)
return geom.primitiveAttributes[(int)attrib];

return arr;
}

VSNRAY_FUNC
inline vec4 getAttribute(
const dco::Geometry &geom, dco::Attribute attrib, unsigned primID, const vec2 uv)
{
const vec4 dflt{0.f, 0.f, 0.f, 1.f};
vec4f color = dflt;
dco::Array vertexColors = getVertexColors(geom, attrib);
dco::Array primitiveColors = getPrimitiveColors(geom, attrib);

if (attrib == dco::Attribute::None)
return color;

dco::Array vertexColors = geom.vertexAttributes[(int)attrib];
dco::Array primitiveColors = geom.primitiveAttributes[(int)attrib];

const TypeInfo &vertexColorInfo = vertexColors.typeInfo;
const TypeInfo &primitiveColorInfo = primitiveColors.typeInfo;

// vertex colors take precedence over primitive colors
if (geom.type == dco::Geometry::Triangle && vertexColors.len > 0) {
uint3 index = getTriangleIndex(geom, primID);
const auto *source1
= (const uint8_t *)vertexColors.data
+ index.x * vertexColorInfo.sizeInBytes;
const auto *source2
= (const uint8_t *)vertexColors.data
+ index.y * vertexColorInfo.sizeInBytes;
const auto *source3
= (const uint8_t *)vertexColors.data
+ index.z * vertexColorInfo.sizeInBytes;
vec4f c1{dflt}, c2{dflt}, c3{dflt};
convert(&c1, source1, vertexColorInfo);
convert(&c2, source2, vertexColorInfo);
convert(&c3, source3, vertexColorInfo);
color = lerp(c1, c2, c3, uv.x, uv.y);
}
else if (geom.type == dco::Geometry::Quad && vertexColors.len > 0) {
uint4 index = getQuadIndex(geom, primID);
const auto *source1
= (const uint8_t *)vertexColors.data
+ index.x * vertexColorInfo.sizeInBytes;
const auto *source2
= (const uint8_t *)vertexColors.data
+ index.y * vertexColorInfo.sizeInBytes;
const auto *source3
= (const uint8_t *)vertexColors.data
+ index.z * vertexColorInfo.sizeInBytes;
const auto *source4
= (const uint8_t *)vertexColors.data
+ index.w * vertexColorInfo.sizeInBytes;
vec4f c1{dflt}, c2{dflt}, c3{dflt}, c4{dflt};
convert(&c1, source1, vertexColorInfo);
convert(&c2, source2, vertexColorInfo);
convert(&c3, source3, vertexColorInfo);
convert(&c4, source4, vertexColorInfo);
if (primID%2==0)
color = lerp(c1, c2, c4, uv.x, uv.y);
else
color = lerp(c3, c4, c2, 1.f-uv.x, 1.f-uv.y);
}
else if (geom.type == dco::Geometry::Sphere && vertexColors.len > 0) {
uint32_t index = getSphereIndex(geom, primID);
const auto *source
= (const uint8_t *)vertexColors.data
+ index * vertexColorInfo.sizeInBytes;
convert(&color, source, vertexColorInfo);
}
else if (geom.type == dco::Geometry::Cone && vertexColors.len > 0) {
uint2 index = getConeIndex(geom, primID);
const auto *source1
= (const uint8_t *)vertexColors.data
+ index.x * vertexColorInfo.sizeInBytes;
const auto *source2
= (const uint8_t *)vertexColors.data
+ index.y * vertexColorInfo.sizeInBytes;
vec4f c1{dflt}, c2{dflt};
convert(&c1, source1, vertexColorInfo);
convert(&c2, source2, vertexColorInfo);
color = lerp(c1, c2, uv.x);
}
else if (geom.type == dco::Geometry::Cylinder && vertexColors.len > 0) {
uint2 index = getCylinderIndex(geom, primID);
const auto *source1
= (const uint8_t *)vertexColors.data
+ index.x * vertexColorInfo.sizeInBytes;
const auto *source2
= (const uint8_t *)vertexColors.data
+ index.y * vertexColorInfo.sizeInBytes;
vec4f c1{dflt}, c2{dflt};
convert(&c1, source1, vertexColorInfo);
convert(&c2, source2, vertexColorInfo);
color = lerp(c1, c2, uv.x);
}
else if (primitiveColors.len > 0) {
if (vertexColors.len > 0) {
if (geom.type == dco::Geometry::Triangle) {
uint3 index = getTriangleIndex(geom, primID);
const auto *source1
= (const uint8_t *)vertexColors.data
+ index.x * vertexColorInfo.sizeInBytes;
const auto *source2
= (const uint8_t *)vertexColors.data
+ index.y * vertexColorInfo.sizeInBytes;
const auto *source3
= (const uint8_t *)vertexColors.data
+ index.z * vertexColorInfo.sizeInBytes;
vec4f c1{dflt}, c2{dflt}, c3{dflt};
convert(&c1, source1, vertexColorInfo);
convert(&c2, source2, vertexColorInfo);
convert(&c3, source3, vertexColorInfo);
color = lerp(c1, c2, c3, uv.x, uv.y);
}
else if (geom.type == dco::Geometry::Quad) {
uint4 index = getQuadIndex(geom, primID);
const auto *source1
= (const uint8_t *)vertexColors.data
+ index.x * vertexColorInfo.sizeInBytes;
const auto *source2
= (const uint8_t *)vertexColors.data
+ index.y * vertexColorInfo.sizeInBytes;
const auto *source3
= (const uint8_t *)vertexColors.data
+ index.z * vertexColorInfo.sizeInBytes;
const auto *source4
= (const uint8_t *)vertexColors.data
+ index.w * vertexColorInfo.sizeInBytes;
vec4f c1{dflt}, c2{dflt}, c3{dflt}, c4{dflt};
convert(&c1, source1, vertexColorInfo);
convert(&c2, source2, vertexColorInfo);
convert(&c3, source3, vertexColorInfo);
convert(&c4, source4, vertexColorInfo);
if (primID%2==0)
color = lerp(c1, c2, c4, uv.x, uv.y);
else
color = lerp(c3, c4, c2, 1.f-uv.x, 1.f-uv.y);
}
else if (geom.type == dco::Geometry::Sphere) {
uint32_t index = getSphereIndex(geom, primID);
const auto *source
= (const uint8_t *)vertexColors.data
+ index * vertexColorInfo.sizeInBytes;
convert(&color, source, vertexColorInfo);
}
else if (geom.type == dco::Geometry::Cone) {
uint2 index = getConeIndex(geom, primID);
const auto *source1
= (const uint8_t *)vertexColors.data
+ index.x * vertexColorInfo.sizeInBytes;
const auto *source2
= (const uint8_t *)vertexColors.data
+ index.y * vertexColorInfo.sizeInBytes;
vec4f c1{dflt}, c2{dflt};
convert(&c1, source1, vertexColorInfo);
convert(&c2, source2, vertexColorInfo);
color = lerp(c1, c2, uv.x);
}
else if (geom.type == dco::Geometry::Cylinder) {
uint2 index = getCylinderIndex(geom, primID);
const auto *source1
= (const uint8_t *)vertexColors.data
+ index.x * vertexColorInfo.sizeInBytes;
const auto *source2
= (const uint8_t *)vertexColors.data
+ index.y * vertexColorInfo.sizeInBytes;
vec4f c1{dflt}, c2{dflt};
convert(&c1, source1, vertexColorInfo);
convert(&c2, source2, vertexColorInfo);
color = lerp(c1, c2, uv.x);
}
} else if (primitiveColors.len > 0) {
const auto *source
= (const uint8_t *)primitiveColors.data
+ primID * primitiveColorInfo.sizeInBytes;
Expand Down
6 changes: 3 additions & 3 deletions scene/surface/geometry/BezierCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ void BezierCurve::commit()
vattributes[i].resize(sizeInBytes);
vattributes[i].reset(m_vertexAttributes[i]->begin());

vgeom.asBezierCurve.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.asBezierCurve.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.asBezierCurve.vertexAttributes[i].typeInfo
vgeom.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.vertexAttributes[i].typeInfo
= getInfo(m_vertexAttributes[i]->elementType());
}
}
Expand Down
6 changes: 3 additions & 3 deletions scene/surface/geometry/Cone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ void Cone::commit()
vattributes[i].resize(sizeInBytes);
vattributes[i].reset(m_vertexAttributes[i]->begin());

vgeom.asCone.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.asCone.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.asCone.vertexAttributes[i].typeInfo
vgeom.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.vertexAttributes[i].typeInfo
= getInfo(m_vertexAttributes[i]->elementType());
}
}
Expand Down
6 changes: 3 additions & 3 deletions scene/surface/geometry/Cylinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ void Cylinder::commit()
vattributes[i].resize(sizeInBytes);
vattributes[i].reset(m_vertexAttributes[i]->begin());

vgeom.asCylinder.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.asCylinder.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.asCylinder.vertexAttributes[i].typeInfo
vgeom.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.vertexAttributes[i].typeInfo
= getInfo(m_vertexAttributes[i]->elementType());
}
}
Expand Down
6 changes: 3 additions & 3 deletions scene/surface/geometry/Quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ void Quad::commit()
vattributes[i].resize(sizeInBytes);
vattributes[i].reset(m_vertexAttributes[i]->begin());

vgeom.asQuad.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.asQuad.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.asQuad.vertexAttributes[i].typeInfo
vgeom.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.vertexAttributes[i].typeInfo
= getInfo(m_vertexAttributes[i]->elementType());
}
}
Expand Down
6 changes: 3 additions & 3 deletions scene/surface/geometry/Sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ void Sphere::commit()
vattributes[i].resize(sizeInBytes);
vattributes[i].reset(m_vertexAttributes[i]->begin());

vgeom.asSphere.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.asSphere.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.asSphere.vertexAttributes[i].typeInfo
vgeom.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.vertexAttributes[i].typeInfo
= getInfo(m_vertexAttributes[i]->elementType());
}
}
Expand Down
6 changes: 3 additions & 3 deletions scene/surface/geometry/Triangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ void Triangle::commit()
vattributes[i].resize(sizeInBytes);
vattributes[i].reset(m_vertexAttributes[i]->begin());

vgeom.asTriangle.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.asTriangle.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.asTriangle.vertexAttributes[i].typeInfo
vgeom.vertexAttributes[i].data = vattributes[i].devicePtr();
vgeom.vertexAttributes[i].len = m_vertexAttributes[i]->size();
vgeom.vertexAttributes[i].typeInfo
= getInfo(m_vertexAttributes[i]->elementType());
}
}
Expand Down

0 comments on commit f63c767

Please sign in to comment.