|
|
|
|
|
|
// #include <d3d11.h>
|
|
|
|
// #include <dxgi.h>
|
|
|
|
#include <flutter/fml/logging.h>
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
// # include <OpenGL/gl3.h>
|
|
|
|
#include "Unity/IUnityGraphics.h"
|
|
|
|
// #include "Unity/IUnityGraphicsD3D11.h"
|
|
|
|
#include "src/shell/platform/unity/unity_console.h"
|
|
|
|
|
|
|
|
#include "include/gpu/vk/GrVkBackendContext.h"
|
|
|
|
// #include "src/gpu/gl/GrGLDefines.h"
|
|
|
|
#include "src/shell/common/shell_io_manager.h"
|
|
|
|
#include "src/shell/gpu/gpu_surface_delegate.h"
|
|
|
|
#include "src/shell/gpu/gpu_surface_gl_delegate.h"
|
|
|
|
#ifdef VulkanX
|
|
|
|
#define UNITY_USED_VULKAN_API_FUNCTIONS(apply) \
|
|
|
|
apply(vkGetDeviceProcAddr); \ |
|
|
|
apply(vkCreateInstance); \ |
|
|
|
apply(vkCmdBeginRenderPass); \ |
|
|
|
apply(vkCreateBuffer); \ |
|
|
|
apply(vkGetPhysicalDeviceMemoryProperties); \ |
|
|
|
apply(vkGetBufferMemoryRequirements); \ |
|
|
|
apply(vkMapMemory); \ |
|
|
|
apply(vkBindBufferMemory); \ |
|
|
|
apply(vkAllocateMemory); \ |
|
|
|
apply(vkDestroyBuffer); \ |
|
|
|
apply(vkFreeMemory); \ |
|
|
|
apply(vkUnmapMemory); \ |
|
|
|
apply(vkQueueWaitIdle); \ |
|
|
|
apply(vkDeviceWaitIdle); \ |
|
|
|
apply(vkCmdCopyBufferToImage); \ |
|
|
|
apply(vkFlushMappedMemoryRanges); \ |
|
|
|
apply(vkCreatePipelineLayout); \ |
|
|
|
apply(vkCreateShaderModule); \ |
|
|
|
apply(vkDestroyShaderModule); \ |
|
|
|
apply(vkCreateGraphicsPipelines); \ |
|
|
|
apply(vkCmdBindPipeline); \ |
|
|
|
apply(vkCmdDraw); \ |
|
|
|
apply(vkCmdPushConstants); \ |
|
|
|
apply(vkCmdBindVertexBuffers); \ |
|
|
|
apply(vkDestroyPipeline); \ |
|
|
|
apply(vkDestroyPipelineLayout); |
|
|
|
|
|
|
|
#define VULKAN_DEFINE_API_FUNCPTR(func) static PFN_##func my_##func
|
|
|
|
VULKAN_DEFINE_API_FUNCPTR(vkGetInstanceProcAddr); |
|
|
|
UNITY_USED_VULKAN_API_FUNCTIONS(VULKAN_DEFINE_API_FUNCPTR); |
|
|
|
#undef VULKAN_DEFINE_API_FUNCPTR
|
|
|
|
|
|
|
|
static void LoadVulkanAPI(PFN_vkGetInstanceProcAddr getInstanceProcAddr, |
|
|
|
VkInstance instance) |
|
|
|
{ |
|
|
|
if (!my_vkGetInstanceProcAddr && getInstanceProcAddr) |
|
|
|
my_vkGetInstanceProcAddr = getInstanceProcAddr; |
|
|
|
|
|
|
|
if (!my_vkCreateInstance) |
|
|
|
my_vkCreateInstance = (PFN_vkCreateInstance)my_vkGetInstanceProcAddr( |
|
|
|
VK_NULL_HANDLE, "vkCreateInstance"); |
|
|
|
|
|
|
|
#define LOAD_VULKAN_FUNC(fn) \
|
|
|
|
if (!my_##fn) \ |
|
|
|
my_##fn = (PFN_##fn)my_vkGetInstanceProcAddr(instance, #fn) |
|
|
|
UNITY_USED_VULKAN_API_FUNCTIONS(LOAD_VULKAN_FUNC); |
|
|
|
#undef LOAD_VULKAN_FUNC
|
|
|
|
} |
|
|
|
#else
|
|
|
|
// #define VERTEX_SHADER_SRC(ver, attr, varying) \ |
|
|
|
// ver \ |
|
|
|
// attr " highp vec3 pos;\n" attr " lowp vec4 color;\n" \ |
|
|
|
// "\n" varying " lowp vec4 ocolor;\n" \ |
|
|
|
// "\n" \ |
|
|
|
// "uniform highp mat4 worldMatrix;\n" \ |
|
|
|
// "uniform highp mat4 projMatrix;\n" \ |
|
|
|
// "\n" \ |
|
|
|
// "void main()\n" \ |
|
|
|
// "{\n" \ |
|
|
|
// " gl_Position = (projMatrix * worldMatrix) * vec4(pos,1);\n" \ |
|
|
|
// " ocolor = color;\n" \ |
|
|
|
// "}\n"
|
|
|
|
|
|
|
|
// static const char *kGlesVProgTextGLES2 = VERTEX_SHADER_SRC("\n", "attribute", "varying");
|
|
|
|
// static const char *kGlesVProgTextGLES3 = VERTEX_SHADER_SRC("#version 300 es\n", "in", "out");
|
|
|
|
|
|
|
|
// #define FRAGMENT_SHADER_SRC(ver, varying, outDecl, outVar) \ |
|
|
|
// ver \ |
|
|
|
// outDecl \ |
|
|
|
// varying " lowp vec4 ocolor;\n" \ |
|
|
|
// "\n" \ |
|
|
|
// "void main()\n" \ |
|
|
|
// "{\n" \ |
|
|
|
// " " outVar " = ocolor;\n" \ |
|
|
|
// "}\n"
|
|
|
|
|
|
|
|
// static const char *kGlesFShaderTextGLES2 = FRAGMENT_SHADER_SRC("\n", "varying", "\n", "gl_FragColor");
|
|
|
|
// static const char *kGlesFShaderTextGLES3 = FRAGMENT_SHADER_SRC("#version 300 es\n", "in", "out lowp vec4 fragColor;\n", "fragColor");
|
|
|
|
|
|
|
|
// enum VertexInputs
|
|
|
|
// {
|
|
|
|
// kVertexInputPosition = 0,
|
|
|
|
// kVertexInputColor = 1
|
|
|
|
// };
|
|
|
|
|
|
|
|
template <class T> |
|
|
|
using EGLResult = std::pair<bool, T>; |
|
|
|
#endif
|
|
|
|
: egl_display_(EGL_NO_DISPLAY), |
|
|
|
: |
|
|
|
#ifdef VulkanX
|
|
|
|
m_UnityVulkan(NULL) |
|
|
|
#else
|
|
|
|
egl_display_(EGL_NO_DISPLAY), |
|
|
|
// ,
|
|
|
|
#endif
|
|
|
|
{ |
|
|
|
initialize_succeeded_ = Initialize(unity_interfaces); |
|
|
|
} |
|
|
|
|
|
|
GLuint UnitySurfaceManager::CreateRenderSurface(void *native_texture_ptr) |
|
|
|
{ |
|
|
|
int width = 100; |
|
|
|
int height = 100; |
|
|
|
#ifdef VulkanX
|
|
|
|
UnityVulkanImage image; |
|
|
|
UnityConsole::WriteLine("test c++"); |
|
|
|
int i = 0; |
|
|
|
EGLContext current_ctx = eglGetCurrentContext(); |
|
|
|
UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str()); |
|
|
|
m_UnityVulkan->AccessTexture( |
|
|
|
native_texture_ptr, UnityVulkanWholeImage, VkImageLayout::VK_IMAGE_LAYOUT_UNDEFINED, 0, |
|
|
|
0, UnityVulkanResourceAccessMode::kUnityVulkanResourceAccess_ObserveOnly, |
|
|
|
&image); |
|
|
|
EGLDisplay hdc = eglGetCurrentDisplay(); |
|
|
|
// wglGetCurrentDC();
|
|
|
|
UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str()); |
|
|
|
|
|
|
|
void *texture1_ptr_ = native_texture_ptr; |
|
|
|
UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str()); |
|
|
|
|
|
|
|
void *surface_texture1_ptr_ = texture1_ptr_; |
|
|
|
textureInfo.fTarget = GR_GL_TEXTURE_2D; |
|
|
|
textureInfo.fID = GrGLuint((long)texture1_ptr_); |
|
|
|
textureInfo.fFormat = GR_GL_RGBA8; |
|
|
|
UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str()); |
|
|
|
|
|
|
|
m_backendTex = |
|
|
|
GrBackendTexture(100, 100, GrMipMapped::kNo, textureInfo); |
|
|
|
UnityConsole::WriteLine(("test c++??" + std::to_string(i++)).c_str()); |
|
|
|
|
|
|
|
// sk_sp<GrContext> gr_context_ = GrContext::MakeGL();
|
|
|
|
// UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str());
|
|
|
|
GrVkImageInfo info( |
|
|
|
image.image, GrVkAlloc(image.memory.memory, image.memory.offset, image.memory.size, image.memory.flags), |
|
|
|
image.tiling, |
|
|
|
image.layout, |
|
|
|
image.format, |
|
|
|
image.mipCount); |
|
|
|
// sk_sp<SkSurface> m_SkSurface = SkSurface::MakeFromBackendTexture(
|
|
|
|
// gr_context_.get(), m_backendTex, kBottomLeft_GrSurfaceOrigin, 1,
|
|
|
|
// kRGBA_8888_SkColorType, nullptr, nullptr);
|
|
|
|
// UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str());
|
|
|
|
GrBackendTexture backendTex(width, height, info); |
|
|
|
m_SkSurface = SkSurface::MakeFromBackendTexture( |
|
|
|
gr_context_.get(), backendTex, kBottomLeft_GrSurfaceOrigin, 1, |
|
|
|
kRGBA_8888_SkColorType, nullptr, nullptr); |
|
|
|
// SkCanvas* canvas = m_SkSurface->getCanvas();
|
|
|
|
// UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str());
|
|
|
|
SkCanvas *canvas = m_SkSurface->getCanvas(); |
|
|
|
// canvas->drawColor(SK_ColorBLUE);
|
|
|
|
// // SkPaint paint;
|
|
|
|
// UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str());
|
|
|
|
canvas->drawColor(SK_ColorBLUE); |
|
|
|
SkPaint paint; |
|
|
|
// SkRect rect = SkRect::MakeXYWH(50, 50, 40, 60);
|
|
|
|
// canvas->drawRect(rect, paint);
|
|
|
|
SkRect rect = SkRect::MakeXYWH(50, 50, 40, 60); |
|
|
|
canvas->drawRect(rect, paint); |
|
|
|
|
|
|
|
// GrBackendTexture m_backendTex =
|
|
|
|
// GrBackendTexture(width_, height_, GrMipMapped::kNo, textureInfo);
|
|
|
|
|
|
|
|
// m_SkSurface = SkSurface::MakeFromBackendTexture(
|
|
|
|
// gr_context_.get(), m_backendTex, kBottomLeft_GrSurfaceOrigin, 1,
|
|
|
|
// kRGBA_8888_SkColorType, nullptr, nullptr);
|
|
|
|
canvas->flush(); |
|
|
|
#else
|
|
|
|
gr_context_ = ShellIOManager::CreateCompatibleResourceLoadingContext( |
|
|
|
GrBackend::kOpenGL_GrBackend, |
|
|
|
GPUSurfaceGLDelegate::GetDefaultPlatformGLInterface()); |
|
|
|
// gr_context_ = GrContext::MakeGL();
|
|
|
|
// width_ = width;
|
|
|
|
// height_ = height;
|
|
|
|
GrGLTextureInfo textureInfo; |
|
|
|
textureInfo.fTarget = GR_GL_TEXTURE_2D; |
|
|
|
textureInfo.fID = GrGLuint((long)native_texture_ptr); |
|
|
|
textureInfo.fFormat = GR_GL_RGBA8; |
|
|
|
// ID3D11Texture2D* d3d11_texture =
|
|
|
|
// static_cast<ID3D11Texture2D*>(native_texture_ptr);
|
|
|
|
// IDXGIResource* image_resource;
|
|
|
|
// HRESULT hr = d3d11_texture->QueryInterface(
|
|
|
|
// __uuidof(IDXGIResource), reinterpret_cast<void**>(&image_resource));
|
|
|
|
// FML_CHECK(SUCCEEDED(hr)) << "UnitySurfaceManager: QueryInterface() failed";
|
|
|
|
GrBackendTexture m_backendTex = |
|
|
|
GrBackendTexture(width, height, GrMipMapped::kNo, textureInfo); |
|
|
|
// HANDLE shared_image_handle;
|
|
|
|
// hr = image_resource->GetSharedHandle(&shared_image_handle);
|
|
|
|
// FML_CHECK(SUCCEEDED(hr)) << "UnitySurfaceManager: GetSharedHandle() failed";
|
|
|
|
m_SkSurface = SkSurface::MakeFromBackendTexture( |
|
|
|
gr_context_.get(), m_backendTex, kBottomLeft_GrSurfaceOrigin, 1, |
|
|
|
kRGBA_8888_SkColorType, nullptr, nullptr); |
|
|
|
|
|
|
|
SkCanvas *canvas = m_SkSurface->getCanvas(); |
|
|
|
// image_resource->Release();
|
|
|
|
canvas->drawColor(SK_ColorBLUE); |
|
|
|
SkPaint paint; |
|
|
|
// FML_CHECK(shared_image_handle != nullptr)
|
|
|
|
// << "UnitySurfaceManager: shared_image_handle is nullptr, miscFlags "
|
|
|
|
// "D3D11_RESOURCE_MISC_SHARED is needed";
|
|
|
|
|
|
|
|
// IDXGIResource* dxgi_resource;
|
|
|
|
// hr = d3d11_device_->OpenSharedResource(
|
|
|
|
// shared_image_handle, __uuidof(ID3D11Resource),
|
|
|
|
// reinterpret_cast<void**>(&dxgi_resource));
|
|
|
|
// FML_CHECK(SUCCEEDED(hr))
|
|
|
|
// << "UnitySurfaceManager: failed to open shared resource";
|
|
|
|
|
|
|
|
// ID3D11Texture2D* image_texture;
|
|
|
|
// hr = dxgi_resource->QueryInterface(__uuidof(ID3D11Texture2D),
|
|
|
|
// reinterpret_cast<void**>(&image_texture));
|
|
|
|
// FML_CHECK(SUCCEEDED(hr))
|
|
|
|
// << "UnitySurfaceManager: failed to query interface ID3D11Texture2D";
|
|
|
|
|
|
|
|
// dxgi_resource->Release();
|
|
|
|
|
|
|
|
// const EGLint attribs[] = {EGL_NONE};
|
|
|
|
// FML_DCHECK(fbo_egl_image_ == nullptr);
|
|
|
|
// fbo_egl_image_ =
|
|
|
|
// eglCreateImageKHR(egl_display_, EGL_NO_CONTEXT, EGL_D3D11_TEXTURE_ANGLE,
|
|
|
|
// static_cast<EGLClientBuffer>(image_texture), attribs);
|
|
|
|
// FML_CHECK(fbo_egl_image_ != EGL_NO_IMAGE_KHR);
|
|
|
|
|
|
|
|
// image_texture->Release();
|
|
|
|
|
|
|
|
// GLint old_texture_binding_2d;
|
|
|
|
// glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture_binding_2d);
|
|
|
|
|
|
|
|
// FML_DCHECK(fbo_texture_ == 0);
|
|
|
|
// glGenTextures(1, &fbo_texture_);
|
|
|
|
// glBindTexture(GL_TEXTURE_2D, fbo_texture_);
|
|
|
|
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
// glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, fbo_egl_image_);
|
|
|
|
// glBindTexture(GL_TEXTURE_2D, old_texture_binding_2d);
|
|
|
|
|
|
|
|
// GLint old_framebuffer_binding;
|
|
|
|
// glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_framebuffer_binding);
|
|
|
|
|
|
|
|
// FML_DCHECK(fbo_ == 0);
|
|
|
|
// glGenFramebuffers(1, &fbo_);
|
|
|
|
// glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
|
|
|
|
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
|
|
|
|
// fbo_texture_, 0);
|
|
|
|
// FML_CHECK(glCheckFramebufferStatus(GL_FRAMEBUFFER) ==
|
|
|
|
// GL_FRAMEBUFFER_COMPLETE);
|
|
|
|
// glBindFramebuffer(GL_FRAMEBUFFER, old_framebuffer_binding);
|
|
|
|
SkRect rect = SkRect::MakeXYWH(50, 50, 40, 60); |
|
|
|
canvas->drawRect(rect, paint); |
|
|
|
#endif
|
|
|
|
|
|
|
|
return fbo_; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
bool UnitySurfaceManager::ClearCurrent() |
|
|
|
{ |
|
|
|
UnityConsole::WriteLine("test c++: make current"); |
|
|
|
UnityConsole::WriteLine("test c++: make current"); |
|
|
|
#ifdef VulkanX
|
|
|
|
return true; |
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EGLBoolean result = eglMakeCurrent(egl_display_, surface, surface, egl_context_) == |
|
|
|
#ifdef Vulkanx
|
|
|
|
return true; |
|
|
|
#else
|
|
|
|
return eglMakeCurrent(egl_display_, surface, surface, egl_context_) == |
|
|
|
draw(); |
|
|
|
|
|
|
|
return result; |
|
|
|
#endif
|
|
|
|
void UnitySurfaceManager::draw() { |
|
|
|
int width_ = 100; |
|
|
|
int height_ = 100; |
|
|
|
int i = 0; |
|
|
|
sk_sp<GrContext> gr_context_ = GrContext::MakeGL(); |
|
|
|
UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str()); |
|
|
|
|
|
|
|
sk_sp<SkSurface> m_SkSurface = SkSurface::MakeFromBackendTexture( |
|
|
|
gr_context_.get(), m_backendTex, kBottomLeft_GrSurfaceOrigin, 1, |
|
|
|
kRGBA_8888_SkColorType, nullptr, nullptr); |
|
|
|
UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str()); |
|
|
|
|
|
|
|
SkCanvas* canvas = m_SkSurface->getCanvas(); |
|
|
|
UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str()); |
|
|
|
bool UnitySurfaceManager::MakeResourceCurrent() |
|
|
|
{ |
|
|
|
#ifdef VulkanX
|
|
|
|
return true; |
|
|
|
#else
|
|
|
|
return eglMakeCurrent(egl_display_, EGL_NO_SURFACE, EGL_NO_SURFACE, |
|
|
|
egl_resource_context_) == EGL_TRUE; |
|
|
|
#endif
|
|
|
|
} |
|
|
|
#ifdef VulkanX
|
|
|
|
static VKAPI_ATTR VkResult VKAPI_CALL Hook_vkCreateInstance( |
|
|
|
const VkInstanceCreateInfo *pCreateInfo, |
|
|
|
const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) |
|
|
|
{ |
|
|
|
my_vkCreateInstance = (PFN_vkCreateInstance)my_vkGetInstanceProcAddr( |
|
|
|
VK_NULL_HANDLE, "vkCreateInstance"); |
|
|
|
VkResult result = my_vkCreateInstance(pCreateInfo, pAllocator, pInstance); |
|
|
|
if (result == VK_SUCCESS) |
|
|
|
LoadVulkanAPI(my_vkGetInstanceProcAddr, *pInstance); |
|
|
|
canvas->drawColor(SK_ColorBLUE); |
|
|
|
SkPaint paint; |
|
|
|
UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
SkRect rect = SkRect::MakeXYWH(50, 50, 40, 60); |
|
|
|
canvas->drawRect(rect, paint); |
|
|
|
static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL |
|
|
|
Hook_vkGetInstanceProcAddr(VkInstance device, const char *funcName) |
|
|
|
{ |
|
|
|
if (!funcName) |
|
|
|
return NULL; |
|
|
|
SkPaint paint2; |
|
|
|
auto text = SkTextBlob::MakeFromString("Hello, Skia!", SkFont(nullptr, 18)); |
|
|
|
canvas->drawTextBlob(text.get(), 50, 25, paint2); |
|
|
|
#define INTERCEPT(fn) \
|
|
|
|
if (strcmp(funcName, #fn) == 0) \ |
|
|
|
return (PFN_vkVoidFunction)&Hook_##fn |
|
|
|
INTERCEPT(vkCreateInstance); |
|
|
|
#undef INTERCEPT
|
|
|
|
GrBackendTexture m_backendTex = |
|
|
|
GrBackendTexture(width_, height_, GrMipMapped::kNo, textureInfo); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
m_SkSurface = SkSurface::MakeFromBackendTexture( |
|
|
|
gr_context_.get(), m_backendTex, kBottomLeft_GrSurfaceOrigin, 1, |
|
|
|
kRGBA_8888_SkColorType, nullptr, nullptr); |
|
|
|
static PFN_vkGetInstanceProcAddr UNITY_INTERFACE_API |
|
|
|
InterceptVulkanInitialization(PFN_vkGetInstanceProcAddr getInstanceProcAddr, |
|
|
|
void *) |
|
|
|
{ |
|
|
|
my_vkGetInstanceProcAddr = getInstanceProcAddr; |
|
|
|
return Hook_vkGetInstanceProcAddr; |
|
|
|
#else
|
|
|
|
// static GLuint CreateShader(GLenum type, const char *sourceText)
|
|
|
|
// {
|
|
|
|
// GLuint ret = glCreateShader(type);
|
|
|
|
// glShaderSource(ret, 1, &sourceText, NULL);
|
|
|
|
// glCompileShader(ret);
|
|
|
|
// return ret;
|
|
|
|
// }
|
|
|
|
bool UnitySurfaceManager::MakeResourceCurrent() |
|
|
|
static EGLResult<EGLConfig> ChooseEGLConfiguration(EGLDisplay display) |
|
|
|
UnityConsole::WriteLine("test c++: make resource"); |
|
|
|
EGLint attributes[] = { |
|
|
|
// clang-format off
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
|
|
|
EGL_RED_SIZE, 8, |
|
|
|
EGL_GREEN_SIZE, 8, |
|
|
|
EGL_BLUE_SIZE, 8, |
|
|
|
EGL_ALPHA_SIZE, 8, |
|
|
|
EGL_DEPTH_SIZE, 0, |
|
|
|
EGL_STENCIL_SIZE, 0, |
|
|
|
EGL_NONE, // termination sentinel
|
|
|
|
// clang-format on
|
|
|
|
}; |
|
|
|
return eglMakeCurrent(egl_display_, EGL_NO_SURFACE, EGL_NO_SURFACE, |
|
|
|
egl_resource_context_) == EGL_TRUE; |
|
|
|
EGLint config_count = 0; |
|
|
|
EGLConfig egl_config = nullptr; |
|
|
|
|
|
|
|
if (eglChooseConfig(display, attributes, &egl_config, 1, &config_count) != |
|
|
|
EGL_TRUE) |
|
|
|
{ |
|
|
|
return {false, nullptr}; |
|
|
|
} |
|
|
|
|
|
|
|
bool success = config_count > 0 && egl_config != nullptr; |
|
|
|
|
|
|
|
return {success, success ? egl_config : nullptr}; |
|
|
|
#endif
|
|
|
|
// IUnityGraphics* graphics = unity_interfaces->Get<IUnityGraphics>();
|
|
|
|
// FML_CHECK(graphics->GetRenderer() == kUnityGfxRendererD3D11)
|
|
|
|
// << "Renderer type is invalid";
|
|
|
|
#ifdef VulkanX
|
|
|
|
IUnityGraphics *graphics = unity_interfaces->Get<IUnityGraphics>(); |
|
|
|
auto result = graphics->GetRenderer(); |
|
|
|
|
|
|
|
FML_CHECK(graphics->GetRenderer() == kUnityGfxRendererVulkan) |
|
|
|
<< "Renderer type is invalid"; |
|
|
|
|
|
|
|
m_UnityVulkan = unity_interfaces->Get<IUnityGraphicsVulkan>(); |
|
|
|
m_UnityVulkan->InterceptInitialization( |
|
|
|
InterceptVulkanInitialization, NULL); |
|
|
|
m_Instance = m_UnityVulkan->Instance(); |
|
|
|
LoadVulkanAPI(m_Instance.getInstanceProcAddr, m_Instance.instance); |
|
|
|
|
|
|
|
UnityVulkanPluginEventConfig config_1; |
|
|
|
config_1.graphicsQueueAccess = kUnityVulkanGraphicsQueueAccess_DontCare; |
|
|
|
config_1.renderPassPrecondition = kUnityVulkanRenderPass_EnsureInside; |
|
|
|
config_1.flags = |
|
|
|
kUnityVulkanEventConfigFlag_EnsurePreviousFrameSubmission | |
|
|
|
kUnityVulkanEventConfigFlag_ModifiesCommandBuffersState; |
|
|
|
m_UnityVulkan->ConfigureEvent(1, &config_1); |
|
|
|
|
|
|
|
// alternative way to intercept API
|
|
|
|
// m_UnityVulkan->InterceptVulkanAPI("vkCmdBeginRenderPass",
|
|
|
|
// (PFN_vkVoidFunction)Hook_vkCmdBeginRenderPass);
|
|
|
|
|
|
|
|
GrVkBackendContext vk_backend_context; |
|
|
|
vk_backend_context.fInstance = m_Instance.instance; |
|
|
|
vk_backend_context.fPhysicalDevice = m_Instance.physicalDevice; |
|
|
|
vk_backend_context.fDevice = m_Instance.device; |
|
|
|
vk_backend_context.fQueue = m_Instance.graphicsQueue; |
|
|
|
vk_backend_context.fGraphicsQueueIndex = m_Instance.queueFamilyIndex; |
|
|
|
vk_backend_context.fGetProc = |
|
|
|
[getInstanceProc = m_Instance.getInstanceProcAddr, |
|
|
|
getDeviceProc = my_vkGetDeviceProcAddr]( |
|
|
|
const char *proc_name, VkInstance instance, VkDevice device) { |
|
|
|
if (device != VK_NULL_HANDLE) |
|
|
|
{ |
|
|
|
return getDeviceProc(device, proc_name); |
|
|
|
} |
|
|
|
return getInstanceProc(instance, proc_name); |
|
|
|
}; |
|
|
|
// IUnityGraphicsD3D11* d3d11 = unity_interfaces->Get<IUnityGraphicsD3D11>();
|
|
|
|
gr_context_ = GrContext::MakeVulkan(vk_backend_context); |
|
|
|
|
|
|
|
#endif
|
|
|
|
egl_display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
|
|
|
if (egl_display_ == EGL_NO_DISPLAY) |
|
|
|
{ |
|
|
|
FML_CHECK(false) |
|
|
|
<< "Renderer type is invalid"; |
|
|
|
} |
|
|
|
|
|
|
|
// Initialize the display connection.
|
|
|
|
if (eglInitialize(egl_display_, nullptr, nullptr) != EGL_TRUE) |
|
|
|
{ |
|
|
|
FML_CHECK(false) |
|
|
|
<< "Renderer type is invalid"; |
|
|
|
} |
|
|
|
|
|
|
|
auto valid_ = true; |
|
|
|
|
|
|
|
bool success = false; |
|
|
|
|
|
|
|
std::tie(success, egl_config_) = ChooseEGLConfiguration(egl_display_); |
|
|
|
if (!success) |
|
|
|
{ |
|
|
|
FML_CHECK(false) << "Could not choose an EGL configuration."; |
|
|
|
// LogLastEGLError();
|
|
|
|
// return;
|
|
|
|
} |
|
|
|
|
|
|
|
EGLDisplay display = egl_display_; |
|
|
|
|
|
|
|
const EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE}; |
|
|
|
|
|
|
|
EGLSurface surface_ = eglCreatePbufferSurface(display, egl_config_, attribs); |
|
|
|
success = surface_ != EGL_NO_SURFACE; |
|
|
|
|
|
|
|
EGLint attributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; |
|
|
|
egl_context_ = eglCreateContext(display, egl_config_, EGL_NO_CONTEXT, attributes); |
|
|
|
|
|
|
|
EGLDisplay display2 = egl_display_; |
|
|
|
|
|
|
|
const EGLint attribs2[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE}; |
|
|
|
|
|
|
|
surface_ = eglCreatePbufferSurface(display2, egl_config_, attribs2); |
|
|
|
auto xxx = surface_ != EGL_NO_SURFACE; |
|
|
|
|
|
|
|
// auto egl_display_ = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,
|
|
|
|
// EGL_DEFAULT_DISPLAY, displayAttribs);
|
|
|
|
// IUnityGraphics *graphics = unity_interfaces->Get<IUnityGraphics>();
|
|
|
|
// UnityGfxRenderer renderer = graphics->GetRenderer();
|
|
|
|
// if (renderer == kUnityGfxRendererOpenGLES20)
|
|
|
|
// {
|
|
|
|
// m_VertexShader = CreateShader(GL_VERTEX_SHADER, kGlesVProgTextGLES2);
|
|
|
|
// m_FragmentShader = CreateShader(GL_FRAGMENT_SHADER, kGlesFShaderTextGLES2);
|
|
|
|
// }
|
|
|
|
// else if (renderer == kUnityGfxRendererOpenGLES30)
|
|
|
|
// {
|
|
|
|
// m_VertexShader = CreateShader(GL_VERTEX_SHADER, kGlesVProgTextGLES3);
|
|
|
|
// m_FragmentShader = CreateShader(GL_FRAGMENT_SHADER, kGlesFShaderTextGLES3);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// FML_CHECK(false)
|
|
|
|
// << "Renderer type is invalid";
|
|
|
|
// }
|
|
|
|
|
|
|
|
// m_Program = glCreateProgram();
|
|
|
|
// glBindAttribLocation(m_Program, kVertexInputPosition, "pos");
|
|
|
|
// glBindAttribLocation(m_Program, kVertexInputColor, "color");
|
|
|
|
// glAttachShader(m_Program, m_VertexShader);
|
|
|
|
// glAttachShader(m_Program, m_FragmentShader);
|
|
|
|
// glLinkProgram(m_Program);
|
|
|
|
// GLint status = 0;
|
|
|
|
// glGetProgramiv(m_Program, GL_LINK_STATUS, &status);
|
|
|
|
// assert(status == GL_TRUE);
|
|
|
|
|
|
|
|
// m_UniformWorldMatrix = glGetUniformLocation(m_Program, "worldMatrix");
|
|
|
|
// m_UniformProjMatrix = glGetUniformLocation(m_Program, "projMatrix");
|
|
|
|
|
|
|
|
// // Create vertex buffer
|
|
|
|
// glGenBuffers(1, &m_VertexBuffer);
|
|
|
|
// glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
|
|
|
|
// glBufferData(GL_ARRAY_BUFFER, 1024, NULL, GL_STREAM_DRAW);
|
|
|
|
|
|
|
|
// FML_CHECK(glGetError() == GL_NO_ERROR);
|
|
|
|
// UnityVulkanInstance instance = vulkan->Instance();
|
|
|
|
// VkDevice device = instance.device;
|
|
|
|
|
|
|
|
// ID3D11Device* d3d11_device = d3d11->GetDevice();
|
|
|
|
|
|
|
|
// IDXGIDevice* dxgi_device;
|
|
|
|
|
|
|
|
|
|
|
void UnitySurfaceManager::CleanUp() |
|
|
|
{ |
|
|
|
#ifdef VulkanX
|
|
|
|
#else
|
|
|
|
// if (egl_display_ != EGL_NO_DISPLAY && egl_context_ != EGL_NO_CONTEXT) {
|
|
|
|
// if (egl_display_ != EGL_NO_DISPLAY && egl_context_ != EGL_NO_CONTEXT)
|
|
|
|
// {
|
|
|
|
// if (result == EGL_FALSE) {
|
|
|
|
// if (result == EGL_FALSE)
|
|
|
|
// {
|
|
|
|
// d3d11_device_ = nullptr;
|
|
|
|
// // d3d11_device_ = nullptr;
|
|
|
|
// egl_resource_context_ != EGL_NO_CONTEXT) {
|
|
|
|
// egl_resource_context_ != EGL_NO_CONTEXT)
|
|
|
|
// {
|
|
|
|
// if (result == EGL_FALSE) {
|
|
|
|
// if (result == EGL_FALSE)
|
|
|
|
// {
|
|
|
|
// if (egl_display_ != EGL_NO_DISPLAY) {
|
|
|
|
// if (egl_display_ != EGL_NO_DISPLAY)
|
|
|
|
// {
|
|
|
|
// if (result == EGL_FALSE) {
|
|
|
|
// if (result == EGL_FALSE)
|
|
|
|
// {
|
|
|
|
#endif
|
|
|
|
void UnitySurfaceManager::draw() |
|
|
|
{ |
|
|
|
// int width_ = 100;
|
|
|
|
// int height_ = 100;
|
|
|
|
// int i = 0;
|
|
|
|
// sk_sp<GrContext> gr_context_ = GrContext::MakeGL();
|
|
|
|
// UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str());
|
|
|
|
|
|
|
|
// sk_sp<SkSurface> m_SkSurface = SkSurface::MakeFromBackendTexture(
|
|
|
|
// gr_context_.get(), m_backendTex, kBottomLeft_GrSurfaceOrigin, 1,
|
|
|
|
// kRGBA_8888_SkColorType, nullptr, nullptr);
|
|
|
|
// UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str());
|
|
|
|
|
|
|
|
// SkCanvas* canvas = m_SkSurface->getCanvas();
|
|
|
|
// UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str());
|
|
|
|
|
|
|
|
// canvas->drawColor(SK_ColorBLUE);
|
|
|
|
// SkPaint paint;
|
|
|
|
// UnityConsole::WriteLine(("test c++" + std::to_string(i++)).c_str());
|
|
|
|
|
|
|
|
// SkRect rect = SkRect::MakeXYWH(50, 50, 40, 60);
|
|
|
|
// canvas->drawRect(rect, paint);
|
|
|
|
|
|
|
|
// SkPaint paint2;
|
|
|
|
// auto text = SkTextBlob::MakeFromString("Hello, Skia!", SkFont(nullptr, 18));
|
|
|
|
// canvas->drawTextBlob(text.get(), 50, 25, paint2);
|
|
|
|
|
|
|
|
// GrBackendTexture m_backendTex =
|
|
|
|
// GrBackendTexture(width_, height_, GrMipMapped::kNo, textureInfo);
|
|
|
|
|
|
|
|
// m_SkSurface = SkSurface::MakeFromBackendTexture(
|
|
|
|
// gr_context_.get(), m_backendTex, kBottomLeft_GrSurfaceOrigin, 1,
|
|
|
|
// kRGBA_8888_SkColorType, nullptr, nullptr);
|
|
|
|
} |
|
|
|
} // namespace uiwidgets
|