Skip to content

Commit

Permalink
Implement material information reading (#156)
Browse files Browse the repository at this point in the history
Implement material information reading (#149)

* Add API call to create mesh with uvs

* Add texture placeholder

* Cleanup

* Add texture api call

* Add Texture constructor with cuda objects

* Add texture to SBT

* Rename object to be consistent

* Add texture to api objects

* Add texture sampling to OptiX program

* Change API Calls policy

* Add placeholder for basic texture test

* rename texture slot

* Add texture coordinates to mesh

* First porsion of small cleanups

* change approach to TextureData struct

* Add TextureData as a texture field

* add channel descriptor creator

* Fix potential issues with non square and tiling textures

* Fix float interplotation bug

* Fix texture sampling and write test to that

* Define non hit value (#146)

* Add texture sampling to OptiX program

* Cleanup before PR

* Make SBT request more elegant

* Make TAPE_HOOK for void* array work

* PR fix chabges v1

* Rebuild texture test and fix tape

* Change texture logic to only float approach

* Smol fixes

* invalid param tests

* change suit name

* Update src/api/apiCore.cpp



* Update test/src/textureTest.cpp



* Update src/api/apiCore.cpp



* Update src/Tape.hpp



* Update src/api/apiCore.cpp



* Add delete_texture call

* PR changes v2

* PR changes v3

* Zero pointer and change date

* change texture to char and small fixes

* tweak optix to half-float textures

* Playground commit

* fix texture to 8bit char

* Update test/src/tapeSurfaceTest.cpp

* Fix PR changes

* RGL_TEXTURE_TEXEL_FORMAT -> TextureTexelFormat

* remove unsafe include in cuda

---------

Co-authored-by: Piotr Mrozik <[email protected]>
  • Loading branch information
msz-rai and PiotrMrozik committed Jun 22, 2023
1 parent 80c1b26 commit 8cc154f
Show file tree
Hide file tree
Showing 21 changed files with 651 additions and 50 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ add_library(RobotecGPULidar SHARED
src/scene/Scene.cpp
src/scene/Mesh.cpp
src/scene/Entity.cpp
src/scene/Texture.cpp
src/scene/ASBuildScratchpad.cpp
src/graph/Graph.cpp
src/graph/Node.cpp
Expand Down
74 changes: 64 additions & 10 deletions include/rgl/api/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
// It is assigned by default if the user does not specify it.
#define RGL_DEFAULT_ENTITY_ID 2147483647

/**
* Two consecutive 32-bit floats.
*/
typedef struct
{
float value[2];
} rgl_vec2f;

/**
* Three consecutive 32-bit floats.
*/
Expand Down Expand Up @@ -86,24 +94,30 @@ typedef struct
* Represents on-GPU mesh that can be referenced by entities on the scene.
* Each mesh can be referenced by any number of entities on different scenes.
*/
typedef struct Mesh *rgl_mesh_t;
typedef struct Mesh* rgl_mesh_t;

/**
* Opaque handle representing an object visible to lidars.
* An entity is always bound to exactly one scene.
*/
typedef struct Entity *rgl_entity_t;
typedef struct Entity* rgl_entity_t;

/**
* Represents on-GPU texture that can be referenced by Entities on the scene.
* Each texture can be referenced by any number of Entities on different scenes.
*/
typedef struct Texture* rgl_texture_t;

/**
* TODO(prybicki)
*/
typedef struct Node *rgl_node_t;
typedef struct Node* rgl_node_t;

/**
* Opaque handle representing a scene - a collection of entities.
* Using scene is optional. NULL can be passed to use an implicit default scene.
*/
typedef struct Scene *rgl_scene_t;
typedef struct Scene* rgl_scene_t;

/**
* Enumerates available extensions in RGL which can be queried using `rgl_get_extension_info`.
Expand Down Expand Up @@ -313,12 +327,24 @@ rgl_cleanup(void);
* @param index_count Number of elements in the indices array
*/
RGL_API rgl_status_t
rgl_mesh_create(rgl_mesh_t *out_mesh,
const rgl_vec3f *vertices,
rgl_mesh_create(rgl_mesh_t* out_mesh,
const rgl_vec3f* vertices,
int32_t vertex_count,
const rgl_vec3i *indices,
const rgl_vec3i* indices,
int32_t index_count);

/**
* Assign texture coordinates to given mesh. Pair of texture coordinates is assigned to each vertex.
*
* @param mesh Address to store the resulting mesh handle
* @param uvs An array of rgl_vec2f or binary-compatible data representing mesh uv coordinates
* @param vertex_count Number of elements in the vertices array. It has to be equal to vertex buffer size.
*/
RGL_API rgl_status_t
rgl_mesh_set_texture_coords(rgl_mesh_t mesh,
const rgl_vec2f* uvs,
int32_t uv_count);

/**
* Informs that the given mesh will be no longer used.
* The mesh will be destroyed after all referring entities are destroyed.
Expand All @@ -336,10 +362,9 @@ rgl_mesh_destroy(rgl_mesh_t mesh);
*/
RGL_API rgl_status_t
rgl_mesh_update_vertices(rgl_mesh_t mesh,
const rgl_vec3f *vertices,
const rgl_vec3f* vertices,
int32_t vertex_count);


/******************************** ENTITY ********************************/

/**
Expand All @@ -350,7 +375,7 @@ rgl_mesh_update_vertices(rgl_mesh_t mesh,
* @param mesh Handle to the mesh which will represent the entity on the scene.
*/
RGL_API rgl_status_t
rgl_entity_create(rgl_entity_t *out_entity, rgl_scene_t scene, rgl_mesh_t mesh);
rgl_entity_create(rgl_entity_t* out_entity, rgl_scene_t scene, rgl_mesh_t mesh);

/**
* Removes an entity from the scene and releases its resources (memory).
Expand All @@ -376,6 +401,35 @@ rgl_entity_set_pose(rgl_entity_t entity, const rgl_mat3x4f *transform);
RGL_API rgl_status_t
rgl_entity_set_id(rgl_entity_t entity, int32_t id);

/**
* Assign intensity texture to the given entity. The assumption is that the entity can hold only one intensity texture.
* @param entity Entity to modify.
* @apram texture Texture to assign.
*/
RGL_API rgl_status_t
rgl_entity_set_intensity_texture(rgl_entity_t entity, rgl_texture_t texture);

/******************************* TEXTURE *******************************/

/**
* Creates a Texture.
* Texture is a container object which holds device pointer to texture resource.
* @param out_texture Handle to the created Texture.
* @param texels Pointer to the texture data. Should be pass as raw byte data of unsigned char array .
* @param width Width of the texture. Has to be positive.
* @param height Height of the texture. It is not demanded that width == height. Has to be positive.
*/
RGL_API rgl_status_t
rgl_texture_create(rgl_texture_t* out_texture, const void* texels, int32_t width, int32_t height);

/**
* Informs that the given texture will be no longer used.
* The texture will be destroyed after all referring entities are destroyed.
* @param mesh Texture to be marked as no longer needed
*/
RGL_API rgl_status_t
rgl_texture_destroy(rgl_texture_t texture);

/******************************** SCENE ********************************/

/**
Expand Down
2 changes: 2 additions & 0 deletions src/RGLFields.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#define NON_HIT_VALUE FLT_MAX

typedef unsigned char TextureTexelFormat;

// Shorter versions to avoid long type names
#define XYZ_F32 RGL_FIELD_XYZ_F32
#define IS_HIT_I32 RGL_FIELD_IS_HIT_I32
Expand Down
4 changes: 4 additions & 0 deletions src/Tape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ TapePlayer::TapePlayer(const char* path)
{ "rgl_mesh_create", std::bind(&TapePlayer::tape_mesh_create, this, _1) },
{ "rgl_mesh_destroy", std::bind(&TapePlayer::tape_mesh_destroy, this, _1) },
{ "rgl_mesh_update_vertices", std::bind(&TapePlayer::tape_mesh_update_vertices, this, _1) },
{ "rgl_mesh_set_texture_coords", std::bind(&TapePlayer::tape_mesh_set_texture_coords, this, _1) },
{ "rgl_texture_create", std::bind(&TapePlayer::tape_texture_create, this, _1) },
{ "rgl_texture_destroy", std::bind(&TapePlayer::tape_texture_destroy, this, _1) },
{ "rgl_entity_create", std::bind(&TapePlayer::tape_entity_create, this, _1) },
{ "rgl_entity_destroy", std::bind(&TapePlayer::tape_entity_destroy, this, _1) },
{ "rgl_entity_set_pose", std::bind(&TapePlayer::tape_entity_set_pose, this, _1) },
{ "rgl_entity_set_intensity_texture", std::bind(&TapePlayer::tape_entity_set_intensity_texture, this, _1) },
{ "rgl_scene_set_time", std::bind(&TapePlayer::tape_scene_set_time, this, _1) },
{ "rgl_graph_run", std::bind(&TapePlayer::tape_graph_run, this, _1) },
{ "rgl_graph_destroy", std::bind(&TapePlayer::tape_graph_destroy, this, _1) },
Expand Down
12 changes: 12 additions & 0 deletions src/Tape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class TapeRecorder
uintptr_t valueToYaml(rgl_mesh_t value) { return (uintptr_t) value; }
uintptr_t valueToYaml(rgl_mesh_t* value) { return (uintptr_t) *value; }

uintptr_t valueToYaml(rgl_texture_t value) { return (uintptr_t) value; }
uintptr_t valueToYaml(rgl_texture_t* value) { return (uintptr_t) *value; }

uintptr_t valueToYaml(rgl_scene_t value) { return (uintptr_t) value; }
uintptr_t valueToYaml(rgl_scene_t* value) { return (uintptr_t) *value; }

Expand All @@ -140,6 +143,9 @@ class TapeRecorder
template<typename T, typename N>
size_t valueToYaml(std::pair<T, N> value) { return writeToBin(value.first, value.second); }

template<typename N>
size_t valueToYaml(std::pair<const void*, N> value) { return writeToBin(static_cast<const char*>(value.first), value.second); }

public:
explicit TapeRecorder(const std::filesystem::path& path);

Expand Down Expand Up @@ -180,6 +186,7 @@ struct TapePlayer

std::unordered_map<TapeAPIObjectID, rgl_mesh_t> tapeMeshes;
std::unordered_map<TapeAPIObjectID, rgl_entity_t> tapeEntities;
std::unordered_map<TapeAPIObjectID, rgl_texture_t> tapeTextures;
std::unordered_map<TapeAPIObjectID, rgl_node_t> tapeNodes;

std::map<std::string, std::function<void(const YAML::Node&)>> tapeFunctions;
Expand All @@ -195,10 +202,14 @@ struct TapePlayer
void tape_mesh_create(const YAML::Node& yamlNode);
void tape_mesh_destroy(const YAML::Node& yamlNode);
void tape_mesh_update_vertices(const YAML::Node& yamlNode);
void tape_mesh_set_texture_coords(const YAML::Node& yamlNode);
void tape_texture_create(const YAML::Node &yamlNode);
void tape_texture_destroy(const YAML::Node &yamlNode);
void tape_entity_create(const YAML::Node& yamlNode);
void tape_entity_destroy(const YAML::Node& yamlNode);
void tape_entity_set_pose(const YAML::Node& yamlNode);
void tape_entity_set_id(const YAML::Node& yamlNode);
void tape_entity_set_intensity_texture(const YAML::Node &yamlNode);
void tape_scene_set_time(const YAML::Node& yamlNode);
void tape_graph_run(const YAML::Node& yamlNode);
void tape_graph_destroy(const YAML::Node& yamlNode);
Expand Down Expand Up @@ -231,6 +242,7 @@ struct TapePlayer
void tape_node_points_ros2_publish(const YAML::Node& yamlNode);
void tape_node_points_ros2_publish_with_qos(const YAML::Node& yamlNode);
#endif

};

extern std::optional<TapeRecorder> tapeRecorder;
94 changes: 94 additions & 0 deletions src/api/apiCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <scene/Scene.hpp>
#include <scene/Entity.hpp>
#include <scene/Mesh.hpp>
#include <scene/Texture.hpp>

#include <graph/NodesCore.hpp>
#include <graph/Graph.hpp>
Expand Down Expand Up @@ -141,6 +142,7 @@ rgl_cleanup(void)
CHECK_CUDA(cudaStreamSynchronize(nullptr));
Entity::instances.clear();
Mesh::instances.clear();
Texture::instances.clear();
Scene::defaultInstance()->clear();
while (!Node::instances.empty()) {
// Note: Graph::destroy calls Node::release() to remove its from APIObject::instances
Expand Down Expand Up @@ -191,6 +193,30 @@ void TapePlayer::tape_mesh_create(const YAML::Node& yamlNode)
tapeMeshes.insert(std::make_pair(yamlNode[0].as<TapeAPIObjectID>(), mesh));
}

RGL_API rgl_status_t
rgl_mesh_set_texture_coords(rgl_mesh_t mesh, const rgl_vec2f* uvs, int32_t uv_count) {
auto status = rglSafeCall([&]() {
RGL_API_LOG("rgl_mesh_set_texture_coords(mesh={}, uvs={}, uv_count={})",
(void*) mesh, repr(uvs, uv_count), uv_count);
CHECK_ARG(mesh != nullptr);
CHECK_ARG(uvs != nullptr);
CHECK_CUDA(cudaStreamSynchronize(nullptr));
CHECK_ARG(uv_count == Mesh::validatePtr(mesh)->getVertexCount());

Mesh::validatePtr(mesh)->setTexCoords(reinterpret_cast<const Vec2f*>(uvs), uv_count);

});
TAPE_HOOK(mesh, TAPE_ARRAY(uvs, uv_count), uv_count);
return status;
}

void TapePlayer::tape_mesh_set_texture_coords(const YAML::Node& yamlNode)
{
rgl_mesh_set_texture_coords(tapeMeshes.at(yamlNode[0].as<TapeAPIObjectID>()),
reinterpret_cast<const rgl_vec2f*>(fileMmap + yamlNode[1].as<size_t>()),
yamlNode[2].as<int32_t>());
}

RGL_API rgl_status_t
rgl_mesh_destroy(rgl_mesh_t mesh)
{
Expand Down Expand Up @@ -323,6 +349,74 @@ void TapePlayer::tape_entity_set_id(const YAML::Node& yamlNode)
yamlNode[1].as<Field<ENTITY_ID_I32>::type>());
}

RGL_API rgl_status_t
rgl_entity_set_intensity_texture(rgl_entity_t entity, rgl_texture_t texture )
{
auto status = rglSafeCall([&](){
RGL_API_LOG("rgl_entity_set_intensity_texture(entity={}, texture={})", (void*) entity, (void*) texture);
CHECK_ARG(entity != nullptr);
CHECK_ARG(texture != nullptr);
Entity::validatePtr(entity)->setIntensityTexture(Texture::validatePtr(texture));
});

TAPE_HOOK(entity, texture);
return status;
}

void TapePlayer::tape_entity_set_intensity_texture(const YAML::Node &yamlNode)
{
rgl_entity_set_intensity_texture(tapeEntities.at(yamlNode[0].as<TapeAPIObjectID>()),
tapeTextures.at(yamlNode[1].as<TapeAPIObjectID>()));
}

RGL_API rgl_status_t
rgl_texture_create(rgl_texture_t* out_texture, const void* texels, int32_t width, int32_t height)
{
auto status = rglSafeCall([&]() {
RGL_API_LOG("rgl_texture_create(out_texture={}, width={}, height={})", (void*) out_texture, width, height);
CHECK_ARG(out_texture != nullptr);
CHECK_ARG(texels != nullptr);
CHECK_ARG(width > 0);
CHECK_ARG(height > 0);

*out_texture = Texture::create(texels, width, height).get();
});
TAPE_HOOK(out_texture, TAPE_ARRAY(texels, (width * height * sizeof(TextureTexelFormat))), width, height);
return status;
}

void TapePlayer::tape_texture_create(const YAML::Node& yamlNode)
{
rgl_texture_t texture = nullptr;

rgl_texture_create(&texture,
reinterpret_cast<const void*>(fileMmap + yamlNode[1].as<size_t>()),
yamlNode[2].as<int32_t>(),
yamlNode[3].as<int32_t>());

tapeTextures.insert(std::make_pair(yamlNode[0].as<TapeAPIObjectID>(), texture));
}

RGL_API rgl_status_t
rgl_texture_destroy(rgl_texture_t texture)
{
auto status = rglSafeCall([&]() {
RGL_API_LOG("rgl_texture_destroy(texture={})", (void*) texture);
CHECK_ARG(texture != nullptr);
CHECK_CUDA(cudaStreamSynchronize(nullptr));
Texture::release(texture);
});
TAPE_HOOK(texture);
return status;
}

void TapePlayer::tape_texture_destroy(const YAML::Node &yamlNode)
{
auto textureId = yamlNode[0].as<TapeAPIObjectID>();
rgl_texture_destroy(tapeTextures.at(textureId));
tapeTextures.erase(textureId);
}

RGL_API rgl_status_t
rgl_scene_set_time(rgl_scene_t scene, uint64_t nanoseconds)
{
Expand Down
11 changes: 8 additions & 3 deletions src/gpu/ShaderBindingTableTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
#include <optix.h>

struct TriangleMeshSBTData {
const Vec3f *vertex;
const Vec3i *index;
const Vec3f* vertex;
const Vec3i* index;
size_t vertex_count;
size_t index_count;
int entity_id;

int entity_id;

const Vec2f* texture_coords;
size_t texture_coords_count;
cudaTextureObject_t texture;
};


Expand Down
Loading

0 comments on commit 8cc154f

Please sign in to comment.