successfully creating D3D texture with D3D11_RESOURCE_MISC_SHARED_NTHANDLE;
successfully allocating with VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT working copying vulkan texture successfully passing D3D texture back to Flutter chore: Dart/Windows sample project: remove unnnecessary InvalidateRect from update() chore: Dart/Windows sample project: add generated bindings successfully blitting from Vulkan swapchain to D3D texture working Vulkan texture integration with Flutter refactor to allow disposal of resources in destructors handle destroyTexture correctly correctly implement surface resizing/destruction move Windows engine to Vulkan backend and flush after creating swapchain add vulkan + vkshaders to Windows libs update materials with Vulkan move Vulkan implementation to thermion_Dart remove extras folder thermion_flutter plugin updates update build hook to copy .lib file to output directory and use -vulkan lib zip file thermion_flutter cleanup reinstate stereoscopic on Windows add dxgi and d3d11.lib to windows header pragma update cli_windows sample project copy filament/vulkan headers to output directory. This was originally added to facilitate linking on Windows (where thermion_flutter_plugin.cpp needs the Vulkan-related headers), but this doesn't actually solve the problem because there's no way that I've found to get the directory structure correct in the Dart native_assets build directory unless you explicitly address each inidivual file. The current approach is therefore to just keep a permanent copy of the headers in the thermion_filament directory (meaning these will need to be updated manually if the Filament version changes). However, I decided to keep the changes to build.dart because it doesn't have much negative impact and may be helpful in future. disable stereoscopic on Windows and disable handle use after free checks use filament headers for thermion_flutter throw Exception for MSAA on Windows (note that passing msaa:true for setAntiAliasing doesn't actually set MSAA on other platforms, but at least it won't cause the engine to crash) change header include path for Windows/Vulkan change header include path for Windows/Vulkan add filament/vulkan headers for flutter (Windows) ensure destroyTexture platform methods accept an integer rather than a list handle Android/Windows swapchain creation separately
This commit is contained in:
28
thermion_dart/native/include/windows/vulkan/d3d_context.h
Normal file
28
thermion_dart/native/include/windows/vulkan/d3d_context.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "import.h"
|
||||
#include "d3d_texture.h"
|
||||
|
||||
#include <d3d.h>
|
||||
#include <d3d11_1.h>
|
||||
#include <dxgi1_2.h>
|
||||
#include <d3d11_4.h>
|
||||
#include <Windows.h>
|
||||
#include <wrl.h>
|
||||
|
||||
|
||||
|
||||
namespace thermion::windows::d3d {
|
||||
|
||||
class EMSCRIPTEN_KEEPALIVE D3DContext {
|
||||
public:
|
||||
D3DContext();
|
||||
~D3DContext();
|
||||
void Flush();
|
||||
std::unique_ptr<D3DTexture> CreateTexture(uint32_t width, uint32_t height);
|
||||
|
||||
private:
|
||||
ID3D11DeviceContext* _D3D11DeviceContext = nullptr;
|
||||
ID3D11Device* _D3D11Device = nullptr;
|
||||
};
|
||||
}
|
||||
53
thermion_dart/native/include/windows/vulkan/d3d_texture.h
Normal file
53
thermion_dart/native/include/windows/vulkan/d3d_texture.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include "import.h"
|
||||
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
||||
#include <d3d.h>
|
||||
#include <d3d11_1.h>
|
||||
#include <dxgi1_2.h>
|
||||
#include <d3d11_4.h>
|
||||
#include <Windows.h>
|
||||
#include <wrl.h>
|
||||
|
||||
|
||||
namespace thermion::windows::d3d {
|
||||
|
||||
class EMSCRIPTEN_KEEPALIVE D3DTexture {
|
||||
public:
|
||||
D3DTexture(
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> d3dTexture2D,
|
||||
HANDLE d3dTexture2DHandle,
|
||||
uint32_t width,
|
||||
uint32_t height
|
||||
);
|
||||
~D3DTexture();
|
||||
|
||||
void Flush();
|
||||
HANDLE GetTextureHandle();
|
||||
|
||||
void SaveToBMP(const char* filename);
|
||||
|
||||
uint32_t GetWidth() {
|
||||
return _width;
|
||||
}
|
||||
|
||||
uint32_t GetHeight() {
|
||||
return _height;
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t _width = 0;
|
||||
uint32_t _height = 0;
|
||||
|
||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> _d3dTexture2D;
|
||||
HANDLE _d3dTexture2DHandle = nullptr;
|
||||
|
||||
bool SaveTextureAsBMP(ID3D11Texture2D* texture, const char* filename);
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
5
thermion_dart/native/include/windows/vulkan/import.h
Normal file
5
thermion_dart/native/include/windows/vulkan/import.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#ifdef THERMION_WIN32_KHR_BUILD
|
||||
#define EMSCRIPTEN_KEEPALIVE __declspec(dllimport)
|
||||
#else
|
||||
#define EMSCRIPTEN_KEEPALIVE __declspec(dllexport)
|
||||
#endif
|
||||
72
thermion_dart/native/include/windows/vulkan/utils.h
Normal file
72
thermion_dart/native/include/windows/vulkan/utils.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#include <bluevk/BlueVK.h>
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct BMPHeader {
|
||||
uint16_t signature;
|
||||
uint32_t fileSize;
|
||||
uint32_t reserved;
|
||||
uint32_t dataOffset;
|
||||
uint32_t headerSize;
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
uint16_t planes;
|
||||
uint16_t bitsPerPixel;
|
||||
uint32_t compression;
|
||||
uint32_t imageSize;
|
||||
int32_t xPixelsPerMeter;
|
||||
int32_t yPixelsPerMeter;
|
||||
uint32_t totalColors;
|
||||
uint32_t importantColors;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
// Helper function to convert VkResult to string for error reporting
|
||||
const char *VkResultToString(VkResult result);
|
||||
// bool checkD3D11VulkanInterop(VkPhysicalDevice physicalDevice, ID3D11Device *d3dDevice);
|
||||
uint32_t findOptimalMemoryType(VkPhysicalDevice physicalDevice,
|
||||
uint32_t typeFilter,
|
||||
VkMemoryPropertyFlags requiredProperties,
|
||||
VkMemoryPropertyFlags preferredProperties = 0);
|
||||
|
||||
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties, VkPhysicalDevice physicalDevice);
|
||||
|
||||
VkResult createVulkanInstance(VkInstance *instance);
|
||||
|
||||
uint32_t findGraphicsQueueFamily(VkPhysicalDevice physicalDevice);
|
||||
|
||||
// Structure to hold both command pool and queue
|
||||
struct CommandResources {
|
||||
VkCommandPool commandPool;
|
||||
VkQueue queue;
|
||||
uint32_t queueFamilyIndex;
|
||||
};
|
||||
|
||||
CommandResources createCommandResources(VkDevice device, VkPhysicalDevice physicalDevice);
|
||||
void readVkImageToBitmap(
|
||||
VkPhysicalDevice physicalDevice,
|
||||
VkDevice device,
|
||||
VkCommandPool commandPool,
|
||||
VkQueue queue,
|
||||
VkImage sourceImage,
|
||||
uint32_t width,
|
||||
uint32_t height,
|
||||
const char* outputPath
|
||||
);
|
||||
|
||||
VkResult createLogicalDevice(VkInstance instance, VkPhysicalDevice *physicalDevice, VkDevice *device);
|
||||
|
||||
void createDeviceWithGraphicsQueue(VkPhysicalDevice physicalDevice, uint32_t& queueFamilyIndex, VkDevice* device);
|
||||
void fillImageWithColor(
|
||||
VkDevice device,
|
||||
VkCommandPool commandPool,
|
||||
VkQueue queue,
|
||||
VkImage image,
|
||||
VkFormat format,
|
||||
VkImageLayout currentLayout,
|
||||
VkExtent3D extent,
|
||||
float r, float g, float b, float a
|
||||
);
|
||||
|
||||
bool SavePixelsAsBMP(uint8_t* pixels, uint32_t width, uint32_t height, int rowPitch, const char* filename);
|
||||
104
thermion_dart/native/include/windows/vulkan/vulkan_context.h
Normal file
104
thermion_dart/native/include/windows/vulkan/vulkan_context.h
Normal file
@@ -0,0 +1,104 @@
|
||||
#pragma once
|
||||
|
||||
#include "d3d_context.h"
|
||||
|
||||
#include "vulkan_texture.h"
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
#include "filament/backend/Platform.h"
|
||||
#include "filament/backend/platforms/VulkanPlatform.h"
|
||||
|
||||
#include "import.h"
|
||||
|
||||
namespace thermion::windows::vulkan {
|
||||
|
||||
class TVulkanPlatform : public filament::backend::VulkanPlatform {
|
||||
public:
|
||||
SwapChainPtr createSwapChain(void* nativeWindow, uint64_t flags,
|
||||
VkExtent2D extent = {0, 0}) override {
|
||||
std::lock_guard lock(mutex);
|
||||
_current = filament::backend::VulkanPlatform::createSwapChain(nativeWindow, flags, extent);
|
||||
std::cout << "Created swap chain with flags " << flags << std::endl;
|
||||
return _current;
|
||||
}
|
||||
|
||||
void destroy(SwapChainPtr handle) override {
|
||||
std::lock_guard lock(mutex);
|
||||
_current = nullptr;
|
||||
std::cout << "Destroyed swap chain" << std::endl;
|
||||
}
|
||||
|
||||
// VkResult acquire(SwapChainPtr handle, VkSemaphore clientSignal, uint32_t* index) override {
|
||||
// auto result = filament::backend::VulkanPlatform::acquire(handle, clientSignal, index);
|
||||
// _currentColorIndex = *index;
|
||||
// return result;
|
||||
// }
|
||||
|
||||
VkResult present(SwapChainPtr handle, uint32_t index, VkSemaphore finishedDrawing) override {
|
||||
auto result = filament::backend::VulkanPlatform::present(handle, index, finishedDrawing);
|
||||
_currentColorIndex = index;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
SwapChainPtr _current;
|
||||
std::mutex mutex;
|
||||
uint32_t _currentColorIndex = 0;
|
||||
|
||||
};
|
||||
|
||||
class EMSCRIPTEN_KEEPALIVE ThermionVulkanContext {
|
||||
public:
|
||||
ThermionVulkanContext();
|
||||
void* GetSharedContext();
|
||||
HANDLE CreateRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top);
|
||||
void DestroyRenderingSurface(HANDLE handle);
|
||||
void ResizeRenderingSurface(uint32_t width, uint32_t height, uint32_t left, uint32_t top);
|
||||
void Flush();
|
||||
|
||||
filament::backend::VulkanPlatform *GetPlatform() {
|
||||
return _platform;
|
||||
}
|
||||
|
||||
void BlitFromSwapchain();
|
||||
|
||||
void readPixelsFromImage(
|
||||
uint32_t width,
|
||||
uint32_t height,
|
||||
std::vector<uint8_t>& outPixels
|
||||
);
|
||||
|
||||
|
||||
private:
|
||||
VkInstance instance = VK_NULL_HANDLE;
|
||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
||||
VkDevice device = VK_NULL_HANDLE;
|
||||
VkCommandPool commandPool = VK_NULL_HANDLE;
|
||||
VkQueue queue = VK_NULL_HANDLE;
|
||||
|
||||
std::unique_ptr<thermion::windows::d3d::D3DContext> _d3dContext;
|
||||
|
||||
std::vector<std::unique_ptr<thermion::windows::d3d::D3DTexture>> _d3dTextures;
|
||||
std::vector<std::unique_ptr<thermion::windows::vulkan::VulkanTexture>> _vulkanTextures;
|
||||
|
||||
TVulkanPlatform *_platform;
|
||||
};
|
||||
|
||||
}
|
||||
34
thermion_dart/native/include/windows/vulkan/vulkan_texture.h
Normal file
34
thermion_dart/native/include/windows/vulkan/vulkan_texture.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include "bluevk/BlueVK.h"
|
||||
|
||||
#include "import.h"
|
||||
|
||||
namespace thermion::windows::vulkan {
|
||||
|
||||
typedef void *HANDLE;
|
||||
|
||||
class EMSCRIPTEN_KEEPALIVE VulkanTexture {
|
||||
public:
|
||||
VulkanTexture(VkImage image, VkDevice device, VkDeviceMemory imageMemory, uint32_t width, uint32_t height, HANDLE d3dTextureHandle);
|
||||
~VulkanTexture();
|
||||
|
||||
HANDLE GetD3DTextureHandle() {
|
||||
return _d3dTextureHandle;
|
||||
}
|
||||
|
||||
static std::unique_ptr<VulkanTexture> create(VkDevice device, VkPhysicalDevice physicalDevice, uint32_t width, uint32_t height, HANDLE d3dTextureHandle);
|
||||
|
||||
VkImage GetImage() {
|
||||
return _image;
|
||||
}
|
||||
private:
|
||||
VkImage _image = VK_NULL_HANDLE;
|
||||
VkDevice _device = VK_NULL_HANDLE;
|
||||
VkDeviceMemory _imageMemory = VK_NULL_HANDLE;
|
||||
uint32_t _width = 0;
|
||||
uint32_t _height = 0;
|
||||
HANDLE _d3dTextureHandle;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user