Skip to content
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

Add GLTF Viewer #431

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions projects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ project(projects)
if (!PPX_ANDROID)
add_subdirectory(sample_00_ppx_info)
endif ()

add_subdirectory(sample_01_triangle)
add_subdirectory(sample_02_triangle_spinning)
add_subdirectory(sample_03_square_textured)
Expand Down Expand Up @@ -48,6 +49,7 @@ add_subdirectory(dynamic_rendering)
add_subdirectory(alloc)
add_subdirectory(fishtornado)
add_subdirectory(fluid_simulation)
add_subdirectory(gltf_viewer)
add_subdirectory(oit_demo)

if (PPX_BUILD_XR)
Expand Down
29 changes: 29 additions & 0 deletions projects/gltf_viewer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

project(gltf_viewer)

add_samples_for_all_apis(
NAME ${PROJECT_NAME}
SOURCES
"GltfViewer.h"
"GltfViewer.cpp"
"main.cpp"
SHADER_DEPENDENCIES
"shader_scene_renderer_vertex_material_vertex"
"shader_scene_renderer_material_debug"
"shader_scene_renderer_material_unlit"
"shader_scene_renderer_material_standard"
)
132 changes: 132 additions & 0 deletions projects/gltf_viewer/GltfViewer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "GltfViewer.h"
#include "ppx/scene/scene_gltf_loader.h"
#include "ppx/graphics_util.h"

using namespace ppx;

#if defined(USE_DX12)
const grfx::Api kApi = grfx::API_DX_12_0;
#elif defined(USE_VK)
const grfx::Api kApi = grfx::API_VK_1_1;
#endif

const uint32_t kNumFramesInFlight = 2;

void GltfViewerApp::Config(ppx::ApplicationSettings& settings)
{
settings.appName = "gltf_viewer";
settings.enableImGui = true;
settings.grfx.api = kApi;
settings.grfx.enableDebug = true;
settings.grfx.numFramesInFlight = kNumFramesInFlight;
settings.window.width = 1920 * 1;
settings.window.height = 1080 * 1;
settings.window.resizable = false;
settings.grfx.swapchain.depthFormat = grfx::FORMAT_D32_FLOAT;
settings.allowThirdPartyAssets = true;
}

void GltfViewerApp::Setup()
{
// Per frame data
{
PerFrame frame = {};

PPX_CHECKED_CALL(GetGraphicsQueue()->CreateCommandBuffer(&frame.cmd));

grfx::SemaphoreCreateInfo semaCreateInfo = {};
PPX_CHECKED_CALL(GetDevice()->CreateSemaphore(&semaCreateInfo, &frame.imageAcquiredSemaphore));

grfx::FenceCreateInfo fenceCreateInfo = {};
PPX_CHECKED_CALL(GetDevice()->CreateFence(&fenceCreateInfo, &frame.imageAcquiredFence));

PPX_CHECKED_CALL(GetDevice()->CreateSemaphore(&semaCreateInfo, &frame.renderCompleteSemaphore));

fenceCreateInfo = {true}; // Create signaled
PPX_CHECKED_CALL(GetDevice()->CreateFence(&fenceCreateInfo, &frame.renderCompleteFence));

mPerFrame.push_back(frame);
}

// Load GLTF scene
{
scene::GltfLoader* pLoader = nullptr;
//
PPX_CHECKED_CALL(scene::GltfLoader::Create(
// GetAssetPath("scene_renderer/scenes/tests/gltf_test_basic_materials.glb"),
GetAssetPath("scene_renderer/scenes/tests/gltf_test_materials.glb"),
nullptr,
&pLoader));

PPX_CHECKED_CALL(pLoader->LoadScene(GetDevice(), 0, &mScene));
PPX_ASSERT_MSG((mScene->GetCameraNodeCount() > 0), "scene doesn't have camera nodes");
PPX_ASSERT_MSG((mScene->GetMeshNodeCount() > 0), "scene doesn't have mesh nodes");

delete pLoader;
}

// Create renderer
{
scene::ForwardRenderer::Create(GetDevice(), kNumFramesInFlight, &mRenderer);
}
}

void GltfViewerApp::Shutdown()
{
delete mRenderer;
delete mScene;
}

void GltfViewerApp::Render()
{
PerFrame& frame = mPerFrame[0];

grfx::SwapchainPtr swapchain = GetSwapchain();

uint32_t imageIndex = UINT32_MAX;
PPX_CHECKED_CALL(swapchain->AcquireNextImage(UINT64_MAX, frame.imageAcquiredSemaphore, frame.imageAcquiredFence, &imageIndex));

// Wait for and reset image acquired fence
PPX_CHECKED_CALL(frame.imageAcquiredFence->WaitAndReset());

// Wait for and reset render complete fence
PPX_CHECKED_CALL(frame.renderCompleteFence->WaitAndReset());

// Build command buffer
PPX_CHECKED_CALL(frame.cmd->Begin());
{
}
PPX_CHECKED_CALL(frame.cmd->End());

grfx::SubmitInfo submitInfo = {};
submitInfo.commandBufferCount = 1;
submitInfo.ppCommandBuffers = &frame.cmd;
submitInfo.waitSemaphoreCount = 1;
submitInfo.ppWaitSemaphores = &frame.imageAcquiredSemaphore;
submitInfo.signalSemaphoreCount = 1;
submitInfo.ppSignalSemaphores = &frame.renderCompleteSemaphore;
submitInfo.pFence = frame.renderCompleteFence;

PPX_CHECKED_CALL(GetGraphicsQueue()->Submit(&submitInfo));

PPX_CHECKED_CALL(swapchain->Present(imageIndex, 1, &frame.renderCompleteSemaphore));
}

void GltfViewerApp::DrawGui()
{
ImGui::Separator();
}
52 changes: 52 additions & 0 deletions projects/gltf_viewer/GltfViewer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GLTF_VIEWER_H
#define GLTF_VIEWER_H

#include "ppx/ppx.h"
#include "ppx/scene/scene_material.h"
#include "ppx/scene/scene_mesh.h"
#include "ppx/scene/scene_forward_renderer.h"

#include <unordered_map>

class GltfViewerApp
: public ppx::Application
{
public:
virtual void Config(ppx::ApplicationSettings& settings) override;
virtual void Setup() override;
virtual void Shutdown() override;
virtual void Render() override;

protected:
virtual void DrawGui() override;

private:
struct PerFrame
{
ppx::grfx::CommandBufferPtr cmd;
ppx::grfx::SemaphorePtr imageAcquiredSemaphore;
ppx::grfx::FencePtr imageAcquiredFence;
ppx::grfx::SemaphorePtr renderCompleteSemaphore;
ppx::grfx::FencePtr renderCompleteFence;
};

std::vector<PerFrame> mPerFrame;
ppx::scene::Scene* mScene = nullptr;
ppx::scene::Renderer* mRenderer = nullptr;
};

#endif // GLTF_VIEWER_H
17 changes: 17 additions & 0 deletions projects/gltf_viewer/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "GltfViewer.h"

SETUP_APPLICATION(GltfViewerApp)
Loading