Skip to content

Commit

Permalink
Start rendering in avalonia
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Sep 3, 2024
1 parent 07ecf2a commit 880891d
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ internal class Framework {
[DllImport("RenderLibrary", EntryPoint = "DestroyNativeWindow", CallingConvention = CallingConvention.Cdecl)]
public static extern void DestroyNativeWindow();

[DllImport("RenderLibrary", EntryPoint = "Frame", CallingConvention = CallingConvention.Cdecl)]
public static extern bool Frame();
[DllImport("RenderLibrary", EntryPoint = "FrameNative", CallingConvention = CallingConvention.Cdecl)]
public static extern bool FrameNative();

[DllImport("RenderLibrary", EntryPoint = "MessageLoop", CallingConvention = CallingConvention.Cdecl)]
public static extern void MessageLoop(IntPtr hWnd);
[DllImport("RenderLibrary", EntryPoint = "MessageNativeLoop", CallingConvention = CallingConvention.Cdecl)]
public static extern void MessageNativeLoop(IntPtr hWnd);

[DllImport("RenderLibrary", EntryPoint = "CreateScene", CallingConvention = CallingConvention.Cdecl)]
public static extern void CreateScene(IntPtr hWnd);
[DllImport("RenderLibrary", EntryPoint = "CreateNativeScene", CallingConvention = CallingConvention.Cdecl)]
public static extern void CreateNativeScene(IntPtr hWnd);
}
4 changes: 2 additions & 2 deletions src/ImeSense.ShaderPlayground.Interop/Viewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle paren
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
_nativeWindowHandle = Framework.CreateNativeWindow();

Framework.CreateScene(_nativeWindowHandle);
Framework.CreateNativeScene(_nativeWindowHandle);

string currentDirectory1 = AppDomain.CurrentDomain.BaseDirectory;
string currentDirectory2 = System.IO.Directory.GetCurrentDirectory();
Expand Down Expand Up @@ -49,7 +49,7 @@ protected override void DestroyNativeControlCore(IPlatformHandle control) {

private static void RunRender() {
while (true) {
Framework.Frame();
Framework.FrameNative();
}
}
}
105 changes: 105 additions & 0 deletions src/RenderLibrary/CreateDirectXDevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "StdAfx.h"

#include <d3d11.h>
#include <Windows.h>

IDXGISwapChain* g_pSwapChain = nullptr;
ID3D11Device* g_pd3dDevice = nullptr;
ID3D11DeviceContext* g_pImmediateContext = nullptr;
ID3D11RenderTargetView* g_pRenderTargetView = nullptr;

#include "CreateDirectXDevice.hpp"

//using namespace RenderLibary;

void CreateCustomDirectXScene(HWND hwnd)
{
// Описание цепочки свопа
DXGI_SWAP_CHAIN_DESC sd = {};
sd.BufferCount = 1;
sd.BufferDesc.Width = 800;
sd.BufferDesc.Height = 600;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hwnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags = 0;

D3D_FEATURE_LEVEL featureLevel;
HRESULT hr = D3D11CreateDeviceAndSwapChain
(
nullptr,
D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr, 0,
D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice,
&featureLevel, &g_pImmediateContext
);

if (FAILED(hr))
{
MessageBox(hwnd, L"Failed to create Direct3D device and swap chain!", L"Error", MB_ICONERROR);
return;
}

ID3D11Texture2D* pBackBuffer = nullptr;
g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_pRenderTargetView);
pBackBuffer->Release();

// Установка представления цели рендеринга
g_pImmediateContext->OMSetRenderTargets(1, &g_pRenderTargetView, nullptr);

D3D11_VIEWPORT vp;

vp.Width = (FLOAT)800;
vp.Height = (FLOAT)600;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;

g_pImmediateContext->RSSetViewports(1, &vp);
}

// Очистка ресурсов DirectX
void CleanupDirectX()
{
if (g_pImmediateContext)
{
g_pImmediateContext->ClearState();
}

if (g_pRenderTargetView)
{
g_pRenderTargetView->Release();
}

if (g_pSwapChain)
{
g_pSwapChain->Release();
}

if (g_pImmediateContext)
{
g_pImmediateContext->Release();
}

if (g_pd3dDevice)
{
g_pd3dDevice->Release();
}
}

bool RenderFrame()
{
const float clearColor[4] = { 0.0f, 0.2f, 0.4f, 1.0f };
g_pImmediateContext->ClearRenderTargetView(g_pRenderTargetView, clearColor);

g_pSwapChain->Present(0, 0);

return true;
}
8 changes: 8 additions & 0 deletions src/RenderLibrary/CreateDirectXDevice.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

extern "C"
{
void CreateCustomDirectXScene(HWND hwnd);
void CleanupDirectX();
bool RenderFrame();
}
7 changes: 6 additions & 1 deletion src/RenderLibrary/FabricCustomWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using namespace DirectX;

#include "Exports.hpp"
#include "FabricCustomWindow.hpp"
#include "CreateDirectXDevice.hpp"

using namespace RenderLibary;

Expand Down Expand Up @@ -95,7 +96,7 @@ API HWND CreateCustomWindow()

API void CreateCustomScene(HWND pointer)
{

CreateCustomDirectXScene(pointer);
}

API void MessageCustomLoop()
Expand All @@ -109,6 +110,7 @@ API void MessageCustomLoop()

API void DestroyCustomWindow()
{
CleanupDirectX();
DestroyWindow(_renderCustomObject._window);
_renderCustomObject._window = nullptr;
}
Expand All @@ -117,5 +119,8 @@ API bool FrameCustom()
{
MessageCustomLoop();

if (!RenderFrame())
return false;

return true;
}
7 changes: 6 additions & 1 deletion src/RenderLibrary/FabricNativeWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using namespace DirectX;

#include "Exports.hpp"
#include "FabricNativeWindow.hpp"
#include "CreateDirectXDevice.hpp"

using namespace RenderLibary;

Expand Down Expand Up @@ -93,7 +94,7 @@ API HWND CreateNativeWindow()

API void CreateNativeScene(HWND pointer)
{

CreateCustomDirectXScene(pointer);
}

API void MessageNativeLoop()
Expand All @@ -107,6 +108,7 @@ API void MessageNativeLoop()

API void DestroyNativeWindow()
{
CleanupDirectX();
DestroyWindow(_renderNativeObject._window);
_renderNativeObject._window = nullptr;
}
Expand All @@ -115,5 +117,8 @@ API bool FrameNative()
{
MessageNativeLoop();

if (!RenderFrame())
return false;

return true;
}

0 comments on commit 880891d

Please sign in to comment.