Added render list so the cube actually shows up.
This commit is contained in:
parent
c44f87ad00
commit
161dfc95bf
@ -67,7 +67,7 @@ class UIRenderer
|
|||||||
public:
|
public:
|
||||||
using primitive_id_t = std::uint64_t;
|
using primitive_id_t = std::uint64_t;
|
||||||
|
|
||||||
static constexpr std::uint64_t UNSET_PRIMITIVE_ID = 0;
|
static constexpr primitive_id_t UNSET_PRIMITIVE_ID = 0;
|
||||||
private:
|
private:
|
||||||
Application* mApplication = nullptr;
|
Application* mApplication = nullptr;
|
||||||
sdlpp::GPUBuffer mVertexBuffer;
|
sdlpp::GPUBuffer mVertexBuffer;
|
||||||
|
@ -15,6 +15,7 @@ void MeshNode::setMesh(std::string resourcePath)
|
|||||||
if (mScene != nullptr)
|
if (mScene != nullptr)
|
||||||
{
|
{
|
||||||
updateMesh();
|
updateMesh();
|
||||||
|
updateSceneMesh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +26,7 @@ void MeshNode::setTexture(std::string resourcePath)
|
|||||||
if (mScene != nullptr)
|
if (mScene != nullptr)
|
||||||
{
|
{
|
||||||
updateTexture();
|
updateTexture();
|
||||||
|
updateSceneMesh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,6 +36,7 @@ void MeshNode::handleEnteredScene()
|
|||||||
|
|
||||||
updateMesh();
|
updateMesh();
|
||||||
updateTexture();
|
updateTexture();
|
||||||
|
updateSceneMesh();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeshNode::updateMesh()
|
void MeshNode::updateMesh()
|
||||||
@ -59,4 +62,16 @@ void MeshNode::updateTexture()
|
|||||||
mTexture = mScene->getRenderer().getTexture(mTexturePath);
|
mTexture = mScene->getRenderer().getTexture(mTexturePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MeshNode::updateSceneMesh()
|
||||||
|
{
|
||||||
|
if (mMeshId == SceneRenderer::UNSET_MESH_ID || !mScene->getRenderer().updateRenderListEntry(mMeshId, {
|
||||||
|
.mesh = mMesh,
|
||||||
|
.texture = mTexture,
|
||||||
|
.transform = mTransform
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
mScene->getRenderer().addMeshToRenderList(mMesh, mTexture, mTransform, mMeshId);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@ private:
|
|||||||
std::shared_ptr<SceneTexture> mTexture;
|
std::shared_ptr<SceneTexture> mTexture;
|
||||||
std::string mMeshPath;
|
std::string mMeshPath;
|
||||||
std::string mTexturePath;
|
std::string mTexturePath;
|
||||||
|
SceneRenderer::mesh_id_t mMeshId = SceneRenderer::UNSET_MESH_ID;
|
||||||
public:
|
public:
|
||||||
MeshNode(MeshNodeCreateArgs args);
|
MeshNode(MeshNodeCreateArgs args);
|
||||||
|
|
||||||
@ -35,6 +36,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
void updateMesh();
|
void updateMesh();
|
||||||
void updateTexture();
|
void updateTexture();
|
||||||
|
void updateSceneMesh();
|
||||||
};
|
};
|
||||||
} // namespace sdl_gpu_test
|
} // namespace sdl_gpu_test
|
||||||
|
|
||||||
|
@ -38,5 +38,6 @@ void Scene::init(const SceneInitArgs& args)
|
|||||||
{
|
{
|
||||||
MIJIN_ASSERT_FATAL(args.renderer != nullptr, "Missing parameter: renderer.");
|
MIJIN_ASSERT_FATAL(args.renderer != nullptr, "Missing parameter: renderer.");
|
||||||
mRenderer = args.renderer;
|
mRenderer = args.renderer;
|
||||||
|
mRootNode.enterScene(this, nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,8 @@ public:
|
|||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void enterScene(Scene* scene, SceneNode* parent);
|
void enterScene(Scene* scene, SceneNode* parent);
|
||||||
|
|
||||||
|
friend class Scene;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SceneInitArgs
|
struct SceneInitArgs
|
||||||
|
@ -22,6 +22,7 @@ void SceneRenderer::init(Application& application)
|
|||||||
{
|
{
|
||||||
mApplication = &application;
|
mApplication = &application;
|
||||||
|
|
||||||
|
createPipeline();
|
||||||
mSampler.create(mApplication->getDevice(), {});
|
mSampler.create(mApplication->getDevice(), {});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,13 +62,13 @@ std::shared_ptr<SceneTexture> SceneRenderer::getTexture(const std::string& resou
|
|||||||
mCachedTextures.erase(it);
|
mCachedTextures.erase(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Bitmap bitmap = loadBitmap(mApplication->getFileSystem().getPath(resourcePath));
|
sdlpp::GPUTextureCreateArgs textureArgs =
|
||||||
std::shared_ptr<SceneTexture> result = std::make_shared<SceneTexture>();
|
{
|
||||||
result->texture.create(mApplication->getDevice(), {
|
|
||||||
.format = sdlpp::GPUTextureFormat::R8G8B8A8_UNORM_SRGB,
|
.format = sdlpp::GPUTextureFormat::R8G8B8A8_UNORM_SRGB,
|
||||||
.usage = {.sampler = true}
|
.usage = {.sampler = true}
|
||||||
});
|
};
|
||||||
mApplication->uploadTextureData(result->texture, bitmap);
|
std::shared_ptr<SceneTexture> result = std::make_shared<SceneTexture>();
|
||||||
|
result->texture = mApplication->loadTexture(resourcePath, textureArgs);
|
||||||
mCachedTextures.emplace(resourcePath, result);
|
mCachedTextures.emplace(resourcePath, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -117,6 +118,72 @@ void SceneRenderer::render(const SceneRendererRenderArgs& args)
|
|||||||
renderPass.end();
|
renderPass.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SceneRenderer::addMeshToRenderList(std::shared_ptr<SceneMesh> mesh, std::shared_ptr<SceneTexture> texture,
|
||||||
|
const Transform3D& transform, mesh_id_t& inOutMeshId)
|
||||||
|
{
|
||||||
|
if (inOutMeshId == UNSET_MESH_ID)
|
||||||
|
{
|
||||||
|
inOutMeshId = mNextMeshId;
|
||||||
|
++mNextMeshId;
|
||||||
|
}
|
||||||
|
mRenderList.push_back({
|
||||||
|
.mesh = std::move(mesh),
|
||||||
|
.texture = std::move(texture),
|
||||||
|
.transform = transform,
|
||||||
|
.meshId = inOutMeshId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SceneRenderer::removeMeshFromRenderList(SceneRenderer::mesh_id_t meshId)
|
||||||
|
{
|
||||||
|
if (meshId == UNSET_MESH_ID)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto it = std::find_if(mRenderList.begin(), mRenderList.end(), [&](const RenderListEntry& entry)
|
||||||
|
{
|
||||||
|
return entry.meshId == meshId;
|
||||||
|
});
|
||||||
|
if (it == mRenderList.end())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
mRenderList.erase(it);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SceneRenderer::updateRenderListEntry(mesh_id_t meshId, const RenderListUpdate& update)
|
||||||
|
{
|
||||||
|
if (meshId == UNSET_MESH_ID)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto it = std::find_if(mRenderList.begin(), mRenderList.end(), [&](const RenderListEntry& entry)
|
||||||
|
{
|
||||||
|
return entry.meshId == meshId;
|
||||||
|
});
|
||||||
|
if (it == mRenderList.end())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (update.mesh.has_value())
|
||||||
|
{
|
||||||
|
it->mesh = *update.mesh;
|
||||||
|
}
|
||||||
|
if (update.texture.has_value())
|
||||||
|
{
|
||||||
|
it->texture = *update.texture;
|
||||||
|
}
|
||||||
|
if (update.transform.has_value())
|
||||||
|
{
|
||||||
|
it->transform = *update.transform;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void SceneRenderer::createPipeline()
|
void SceneRenderer::createPipeline()
|
||||||
{
|
{
|
||||||
// create shaders
|
// create shaders
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_SCENE_RENDERER_HPP_INCLUDED)
|
#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_SCENE_RENDERER_HPP_INCLUDED)
|
||||||
#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_SCENE_RENDERER_HPP_INCLUDED 1
|
#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_SCENE_RENDERER_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -34,23 +35,33 @@ struct SceneRendererRenderArgs
|
|||||||
unsigned targetTextureHeight;
|
unsigned targetTextureHeight;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct RenderListUpdate
|
||||||
|
{
|
||||||
|
std::optional<std::shared_ptr<SceneMesh>> mesh;
|
||||||
|
std::optional<std::shared_ptr<SceneTexture>> texture;
|
||||||
|
std::optional<Transform3D> transform;
|
||||||
|
};
|
||||||
|
|
||||||
class SceneRenderer
|
class SceneRenderer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using mesh_idx_t = std::uint64_t;
|
using mesh_id_t = std::uint64_t;
|
||||||
|
|
||||||
|
static constexpr mesh_id_t UNSET_MESH_ID = 0;
|
||||||
private:
|
private:
|
||||||
struct RenderListEntry
|
struct RenderListEntry
|
||||||
{
|
{
|
||||||
std::shared_ptr<SceneMesh> mesh;
|
std::shared_ptr<SceneMesh> mesh;
|
||||||
std::shared_ptr<SceneTexture> texture;
|
std::shared_ptr<SceneTexture> texture;
|
||||||
Transform3D transform;
|
Transform3D transform;
|
||||||
mesh_idx_t meshIdx;
|
mesh_id_t meshId;
|
||||||
};
|
};
|
||||||
|
|
||||||
Application* mApplication = nullptr;
|
Application* mApplication = nullptr;
|
||||||
std::unordered_map<std::string, std::weak_ptr<SceneMesh>> mCachedMeshes;
|
std::unordered_map<std::string, std::weak_ptr<SceneMesh>> mCachedMeshes;
|
||||||
std::unordered_map<std::string, std::weak_ptr<SceneTexture>> mCachedTextures;
|
std::unordered_map<std::string, std::weak_ptr<SceneTexture>> mCachedTextures;
|
||||||
std::vector<RenderListEntry> mRenderList;
|
std::vector<RenderListEntry> mRenderList;
|
||||||
|
mesh_id_t mNextMeshId = 1;
|
||||||
|
|
||||||
sdlpp::GPUGraphicsPipeline mPipeline;
|
sdlpp::GPUGraphicsPipeline mPipeline;
|
||||||
sdlpp::GPUSampler mSampler;
|
sdlpp::GPUSampler mSampler;
|
||||||
@ -63,6 +74,11 @@ public:
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
std::shared_ptr<SceneTexture> getTexture(const std::string& resourcePath);
|
std::shared_ptr<SceneTexture> getTexture(const std::string& resourcePath);
|
||||||
|
|
||||||
|
void addMeshToRenderList(std::shared_ptr<SceneMesh> mesh, std::shared_ptr<SceneTexture> texture,
|
||||||
|
const Transform3D& transform, mesh_id_t& inOutMeshId);
|
||||||
|
bool removeMeshFromRenderList(mesh_id_t meshId);
|
||||||
|
bool updateRenderListEntry(mesh_id_t meshId, const RenderListUpdate& update);
|
||||||
|
|
||||||
void render(const SceneRendererRenderArgs& args);
|
void render(const SceneRendererRenderArgs& args);
|
||||||
private:
|
private:
|
||||||
void createPipeline();
|
void createPipeline();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user