Skip to content

Commit

Permalink
More work on resource loading and tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
broken-bytes committed Feb 8, 2024
1 parent f36a604 commit b9c6bf7
Show file tree
Hide file tree
Showing 27 changed files with 254 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
],
dependencies: [
// Depend on the Swift 5.9 release of SwiftSyntax
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.1.1"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand Down
4 changes: 2 additions & 2 deletions editor/app/src/editor/Editor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <sstream>

// NOTE: These functions come from the Swift libraries. They are *not* unused
extern "C" void kyanitemain();
extern "C" void kyanitemain(bool);
extern "C" void kyaniteeditormain();

std::thread engineThread;
Expand Down Expand Up @@ -65,7 +65,7 @@ namespace kyanite::editor {

auto Editor::InitializeEngine() -> void {
// Start the engine in a separate thread so it does not interfere with the editor
engineThread = std::thread([&]() { kyanitemain(); });
engineThread = std::thread([&]() { kyanitemain(true); });
kyaniteeditormain();
_assetDatabase->Load(_service->CachePath());
}
Expand Down
9 changes: 0 additions & 9 deletions engine/core/src/Bridge_Core.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ NativePointer Core_CreateWindow(
auto allFlags = flags;
// Enable OpenGL on the window

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);

if(renderBackend == 0) {
allFlags |= SDL_WINDOW_OPENGL;
} else if(renderBackend == 1) {
Expand Down Expand Up @@ -65,7 +57,6 @@ NativePointer Core_CreateWindow(
}

NativePointer Core_CreateWindowFromNative(NativePointer nativeWindow) {
SDL_InitSubSystem(SDL_INIT_VIDEO);
SDL_Window* sdlWnd = SDL_CreateWindowFrom(nativeWindow);

return reinterpret_cast<NativePointer>(sdlWnd);
Expand Down
7 changes: 7 additions & 0 deletions engine/ecs/src/Bridge_ECS.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <map>
#include <memory>
#include <Windows.h>

// Store all the component types in a map of int -> uint64_t
// The int is the hash value coming from swift and the uint64_t is the component ID
Expand Down Expand Up @@ -48,6 +49,12 @@ uint64_t ECS_RegisterSystem(const char* name, uint64_t* filter, size_t filterLen

NativePointer ECS_GetComponentsFromIterator(NativePointer iterator, uint8_t index, size_t componentSize) {
auto iter = reinterpret_cast<ecs_iter_t*>(iterator);
// Print the iterator, index and component size
OutputDebugStringA(std::to_string(reinterpret_cast<int64_t>(iter)).c_str());
OutputDebugStringA("\n");
OutputDebugStringA(std::to_string(index).c_str());
OutputDebugStringA("\n");
OutputDebugStringA(std::to_string(componentSize).c_str());

return ecs::EntityRegistry::GetComponentBuffer(iter, index, componentSize);
}
Expand Down
5 changes: 4 additions & 1 deletion engine/ecs/src/EntityRegistry.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <flecs/addons/rest.h>

#include <memory>
#include <Windows.h>

namespace ecs::EntityRegistry {
flecs::world world;
Expand Down Expand Up @@ -98,6 +99,8 @@ namespace ecs::EntityRegistry {
}

auto GetComponentBuffer(ecs_iter_t* iter, uint8_t index, size_t componentSize) -> void* {
return ecs_field_w_size(iter, componentSize, index);
auto ptr = ecs_field_w_size(iter, componentSize, index);
OutputDebugStringA("Component buffer received");
return ptr;
}
}
14 changes: 14 additions & 0 deletions engine/rendering/include/rendering/Bridge_Rendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ EXPORTED void Rendering_DrawImage(uint64_t textureId, uint32_t width, uint32_t h
// Draw a drag and drop target in imgui with the given label and target
EXPORTED void Rendering_DrawDragAndDropTarget(const char* label, void* target);

/**
* @brief Creates a shader
* @param shader The shader code to use
* @param shaderType The type of the shader. 0 for vertex, 1 for fragment
* @return The id of the shader
*/
EXPORTED uint64_t Rendering_CreateShader(const char* shader, uint8_t shaderType);

/**
* @brief Destroys a shader
* @param shaderId The id of the shader to destroy
*/
EXPORTED void Rendering_DestroyShader(uint64_t shaderId);

#ifdef __cplusplus
}
#endif
5 changes: 5 additions & 0 deletions engine/rendering/include/rendering/Device.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "CommandListType.hxx"
#include "CommandQueue.hxx"
#include "Fence.hxx"
#include "Shader.hxx"

#include <memory>

Expand All @@ -27,5 +28,9 @@ namespace kyanite::engine::rendering {
// Creation of resources
virtual auto CreateBuffer(uint64_t size) -> std::shared_ptr<Buffer> = 0;
virtual auto CreateRenderTarget() -> std::shared_ptr<RenderTarget> = 0;
virtual auto CompileShader(const std::string& shaderSource, ShaderType type) -> uint64_t = 0;

// Deleting resources
virtual auto DestroyShader(uint64_t shaderHandle) -> void = 0;
};
}
2 changes: 1 addition & 1 deletion engine/rendering/include/rendering/Rendering.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ namespace kyanite::engine::rendering {
auto PostFrame() -> void;
auto SetClearColor(float r, float g, float b, float a) -> void;
auto LoadTexture(std::string_view path) -> Texture;
auto LoadShader(std::string_view vertexPath, std::string_view fragmentPath) -> Shader;
auto LoadShader(std::string code, ShaderType type) -> uint64_t;
auto LoadModel(std::string_view path) -> std::vector<Mesh>;
}
8 changes: 8 additions & 0 deletions engine/rendering/include/rendering/Shader.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@


namespace kyanite::engine::rendering {
enum class ShaderType {
VERTEX,
FRAGMENT,
GEOMETRY,
TESS_CONTROL,
TESS_EVALUATION,
COMPUTE
};
struct Shader {
std::string name;

Expand Down
5 changes: 5 additions & 0 deletions engine/rendering/include/rendering/opengl/GlDevice.hxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../Device.hxx"
#include "../Shader.hxx"

namespace kyanite::engine::rendering::opengl {
class GlDevice : public Device {
Expand All @@ -18,5 +19,9 @@ namespace kyanite::engine::rendering::opengl {
// Creation of resources
virtual auto CreateBuffer(uint64_t size) -> std::shared_ptr<Buffer> override;
virtual auto CreateRenderTarget() -> std::shared_ptr<RenderTarget> override;
virtual auto CompileShader(const std::string& shaderSource, ShaderType type) -> uint64_t override;

//Delete resources
virtual auto DestroyShader(uint64_t shaderHandle) -> void override;
};
}
8 changes: 8 additions & 0 deletions engine/rendering/src/Bridge_Rendendering.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "rendering/Bridge_Rendering.h"
#include "rendering/Rendering.hxx"
#include "rendering/Shader.hxx"

#include <imgui.h>

Expand Down Expand Up @@ -76,3 +77,10 @@ void Rendering_DrawDragAndDropTarget(const char* label, void* target) {
ImGui::Text("Drag and drop a texture here");
}

uint64_t Rendering_CreateShader(const char* shader, uint8_t shaderType) {
rendering::LoadShader(shader, rendering::ShaderType(shaderType));
}

void Rendering_DestroyShader(uint64_t shaderId) {
rendering::UnloadShader(shaderId);
}
33 changes: 30 additions & 3 deletions engine/rendering/src/Rendering.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "rendering/Rendering.hxx"
#include "rendering/Device.hxx"
#include "rendering/opengl/GLDevice.hxx"
#include "core/Core.hxx"

#include <FreeImagePlus.h>
Expand All @@ -11,6 +12,7 @@

#include <stdexcept>
#include <memory>
#include <rendering/renderdoc_app.h>

namespace core = kyanite::engine::core;

Expand All @@ -24,7 +26,14 @@ namespace kyanite::engine::rendering {
SDL_InitSubSystem(SDL_INIT_VIDEO);
auto sdlWindow = reinterpret_cast<SDL_Window*>(window);
kyanite::engine::rendering::window = sdlWindow;

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetSwapInterval(0);
// Create an OpenGL context
context = SDL_GL_CreateContext(sdlWindow);
if (!context) {
Expand Down Expand Up @@ -53,6 +62,20 @@ namespace kyanite::engine::rendering {
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForOpenGL(sdlWindow, context);
ImGui_ImplOpenGL3_Init("#version 130");

RENDERDOC_API_1_1_2* rdoc_api = NULL;

// At init, on windows
if (HMODULE mod = GetModuleHandleA("renderdoc.dll"))
{
pRENDERDOC_GetAPI RENDERDOC_GetAPI =
(pRENDERDOC_GetAPI)GetProcAddress(mod, "RENDERDOC_GetAPI");
int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2, (void**)&rdoc_api);
assert(ret == 1);
}

// Create a device
device = std::make_shared<opengl::GlDevice>();
}

auto Shutdown() -> void {
Expand Down Expand Up @@ -106,8 +129,12 @@ namespace kyanite::engine::rendering {
return Texture();
}

auto LoadShader(std::string_view vertexPath, std::string_view fragmentPath) -> Shader {
return Shader();
auto LoadShader(std::string code, ShaderType type) -> uint64_t {
return device->CompileShader(code, type);
}

auto UnloadShader(uint64_t shader) -> void {
device->DestroyShader(shader);
}

auto LoadModel(std::string_view path) -> std::vector<Mesh> {
Expand Down
43 changes: 43 additions & 0 deletions engine/rendering/src/opengl/GlDevice.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#include "rendering/opengl/GlCommandList.hxx"
#include "rendering/opengl/GlCommandQueue.hxx"
#include "rendering/opengl/GlFence.hxx"
#include "rendering/Shader.hxx"

#include <glad/glad.h>
#include <GL/gl.h>

#include <stdexcept>

namespace kyanite::engine::rendering::opengl {
GlDevice::GlDevice() {
Expand Down Expand Up @@ -35,4 +41,41 @@ namespace kyanite::engine::rendering::opengl {
auto GlDevice::CreateRenderTarget() -> std::shared_ptr<RenderTarget> {
return nullptr;
}

auto GlDevice::CompileShader(const std::string& shaderSource, ShaderType type) -> uint64_t {
uint64_t shaderTypeId = 0;
switch (type) {
case ShaderType::VERTEX:
shaderTypeId = GL_VERTEX_SHADER;
break;
case ShaderType::FRAGMENT:
shaderTypeId = GL_FRAGMENT_SHADER;
break;
default:
break;
}

const auto shaderCodeGl = reinterpret_cast<const GLchar*>(shaderSource.c_str());

auto shaderId = glCreateShader(shaderTypeId);
glShaderSource(shaderId, 1, &shaderCodeGl, nullptr);
glCompileShader(shaderId);

// Check for errors
GLint iTestReturn;
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &iTestReturn);
if (iTestReturn == GL_FALSE) {
GLchar p_cInfoLog[1024];
int32_t iErrorLength;
glGetShaderInfoLog(shaderId, 1024, &iErrorLength, p_cInfoLog);
glDeleteShader(shaderId);

throw std::runtime_error("Error compiling shader: " + std::string(p_cInfoLog));
}

return shaderId;
}

auto GlDevice::DestroyShader(uint64_t shaderHandle) -> void {
}
}
14 changes: 14 additions & 0 deletions scripting/bridge/include/Bridge_Rendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ EXPORTED void Rendering_DrawImage(uint64_t textureId, uint32_t width, uint32_t h
// Draw a drag and drop target in imgui with the given label and target
EXPORTED void Rendering_DrawDragAndDropTarget(const char* label, void* target);

/**
* @brief Creates a shader
* @param shader The shader code to use
* @param shaderType The type of the shader. 0 for vertex, 1 for fragment
* @return The id of the shader
*/
EXPORTED uint64_t Rendering_CreateShader(const char* shader, uint8_t shaderType);

/**
* @brief Destroys a shader
* @param shaderId The id of the shader to destroy
*/
EXPORTED void Rendering_DestroyShader(uint64_t shaderId);

#ifdef __cplusplus
}
#endif
11 changes: 3 additions & 8 deletions scripting/engine/Engine.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import Foundation
import Native
import WinSDK

@System class TestSystem {
public static func run(transform: UnsafeMutableBufferPointer<TransformComponent>) {
OutputDebugStringA("TestSystem running\n")
transform.map {
OutputDebugStringA("Position: \($0.position.x)\n")
}
for x in 0..<transform.count {
transform[x].position.x += InputManager.shared.getKeyState(key: .keycodeA) == .held ? 1 : 0
}
Expand All @@ -19,7 +14,7 @@ class Engine {
private var window: NativeWindow? = nil
var testSystem: TestSystem

init() {
init(isDebug: Bool = false) {
// Initialize all subsystems
// Initialize the core
NativeCore.shared.start()
Expand All @@ -36,7 +31,7 @@ class Engine {
)
NativeAudio.shared.start()
NativeInput.shared.start()
NativeECS.shared.start()
NativeECS.shared.start(debug: isDebug)
guard let window = window else {
fatalError("Failed to create window")
}
Expand All @@ -62,7 +57,7 @@ class Engine {
entity.addComponent(TransformComponent.self)
var transform = TransformComponent()
transform.position = Vector3(x: 0, y: 0, z: 0)
entity.setComponent(transform)
entity.setComponent(&transform)
}
}
// Attoseconds to milliseconds
Expand Down
4 changes: 2 additions & 2 deletions scripting/engine/Kyanite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import Macros

@_cdecl("kyanitemain")
public func kyanitemain() {
let engine = Engine()
public func kyanitemain(isDebug: Bool = false) {
let engine = Engine(isDebug: isDebug)
engine.start()
}

Expand Down
Loading

0 comments on commit b9c6bf7

Please sign in to comment.