This commit is contained in:
2025-05-13 02:46:21 +03:00
parent 143e16692e
commit 86914b7fcc
610 changed files with 92343 additions and 2 deletions
+33
View File
@@ -0,0 +1,33 @@
#ifndef IBlendState11_h__
#define IBlendState11_h__
#include "render/IBlendState.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
namespace LifeGraphics
{
class IBlendState11 : public IBlendState
{
private:
ID3D11Device* device;
ID3D11DeviceContext* context;
public:
ID3D11BlendState* state;
IBlendState11(ID3D11Device* _device, ID3D11BlendState* _state)
{
device = _device;
device->GetImmediateContext(&context);
state = _state;
}
~IBlendState11()
{
SAFE_RELEASE(state);
}
};
}
#endif // IBlendState11_h__
@@ -0,0 +1,12 @@
#include "render/dx11/IConstantBuffer11.h"
#include <stdio.h>
namespace LifeGraphics
{
void IConstantBuffer11::Update(void* pData, int dataLength)
{
context->Map(buffer,0, D3D11_MAP_WRITE_DISCARD, 0, &subresource);
memcpy(subresource.pData, pData, dataLength);
context->Unmap(buffer, 0);
}
}
@@ -0,0 +1,61 @@
#ifndef IConstantBuffer11_h__
#define IConstantBuffer11_h__
#include "render/IConstantBuffer.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include "Globals.h"
#include <log/CLog.h>
namespace LifeGraphics
{
class IConstantBuffer11 : public IConstantBuffer
{
public:
ID3D11Buffer* buffer;
ID3D11Device* device;
ID3D11DeviceContext* context;
int size;
D3D11_MAPPED_SUBRESOURCE subresource;
IConstantBuffer11(ID3D11Device* _device, int _size)
{
if((_size % 0x10) !=0 )
{
LOG_ERROR("Buffer size must be multiple of 16", "IConstantBuffer11", "ctor()");
return;
}
device = _device;
size = _size;
D3D11_BUFFER_DESC desc = {0};
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
desc.ByteWidth = _size;
desc.Usage = D3D11_USAGE_DYNAMIC;
HRESULT hr = device->CreateBuffer(&desc,NULL, &buffer);
device->GetImmediateContext(&context);
/*
BufferDescription desc = new BufferDescription();
desc.BindFlags = BindFlags.ConstantBuffer;
desc.CpuAccessFlags = CpuAccessFlags.Write;
desc.OptionFlags = ResourceOptionFlags.None;
desc.SizeInBytes = size;
desc.StructureByteStride = 0;
desc.Usage = D3D11.ResourceUsage.Dynamic;
buffer = new D3D11.Buffer(device, desc);*/
}
~IConstantBuffer11()
{
SAFE_RELEASE(buffer);
context = NULL;
}
void Update(void* pData, int dataLength);
};
}
#endif // IConstantBuffer11_h__
@@ -0,0 +1,31 @@
#ifndef IDepthStencilState11_h__
#define IDepthStencilState11_h__
#include "render/IDepthStencilState.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
namespace LifeGraphics
{
class IDepthStencilState11 : public IDepthStencilState
{
private:
ID3D11Device* device;
ID3D11DeviceContext* context;
public:
ID3D11DepthStencilState* state;
IDepthStencilState11(ID3D11Device* _device, ID3D11DepthStencilState* _state)
{
device = _device;
device->GetImmediateContext(&context);
state = _state;
}
~IDepthStencilState11()
{
SAFE_RELEASE(state);
}
};
}
#endif // IDepthStencilState11_h__
+98
View File
@@ -0,0 +1,98 @@
#ifndef I_INDEX_BUFFER11_H__
#define I_INDEX_BUFFER11_H__
#include "render/IIndexBuffer.h"
#include "Globals.h"
#include "ResourceManager.h"
#include "IMemoryStream.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
namespace LifeGraphics
{
class IIndexBuffer11 : public IIndexBuffer
{
public:
ID3D11Buffer* ib;
ID3D11Device* device;
ID3D11DeviceContext* context;
D3D11_MAPPED_SUBRESOURCE* mappedData;
LifeCore::IMemoryStream stm;
~IIndexBuffer11()
{
SAFE_RELEASE(ib);
}
IIndexBuffer11(ID3D11Device* _device, void* pBuffer, int _size, int _sixteenBit, int staging)
{
device = _device;
length = _size;
isShort = _sixteenBit;
//g_desc = new BufferDescription(size, D3D11.ResourceUsage.Default,
// BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
if(pBuffer)
bd.Usage = D3D11_USAGE_DEFAULT;
else
bd.Usage = D3D11_USAGE_DYNAMIC;
if(staging)
bd.Usage = D3D11_USAGE_STAGING;
bd.ByteWidth = _size;
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
if(bd.Usage == D3D11_USAGE_DEFAULT)
bd.CPUAccessFlags = 0;
else if(bd.Usage == D3D11_USAGE_DYNAMIC)
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
else if(bd.Usage == D3D11_USAGE_STAGING)
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
if(pBuffer)
{
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = pBuffer;
device->CreateBuffer( &bd, &InitData, &ib );
}
else
device->CreateBuffer( &bd, NULL, &ib );
device->GetImmediateContext(&context);
//if( FAILED( hr ) )
// return hr;
}
LifeCore::IDataStream* Map(int offset, int flags)
{
D3D11_MAP mapFlags;
if (flags & LF_Discard)
mapFlags = D3D11_MAP_WRITE_DISCARD;
else if ((flags & LF_NoOverwrite))
mapFlags = D3D11_MAP_WRITE_NO_OVERWRITE;
else if ((flags & LF_ReadOnly))
mapFlags = D3D11_MAP_READ;
else if (flags == LF_None)
mapFlags = D3D11_MAP_READ_WRITE;
else
mapFlags = D3D11_MAP_WRITE;
context->Map(ib,0, mapFlags,0, mappedData);
stm.Reset(((char*)mappedData->pData) + offset, length);
return &stm;
}
void Unmap()
{
context->Unmap(ib, 0);
}
};
}
#endif
+23
View File
@@ -0,0 +1,23 @@
#ifndef IInputLayout11_h__
#define IInputLayout11_h__
#include "render/IInputLayout.h"
#include "Globals.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
namespace LifeGraphics
{
class IInputLayout11 : public IInputLayout
{
public:
ID3D11InputLayout* Decl;
IInputLayout11(){}
~IInputLayout11()
{
SAFE_RELEASE(Decl);
}
};
}
#endif // IInputLayout11_h__
+109
View File
@@ -0,0 +1,109 @@
#ifndef IPixelShader11_h__
#define IPixelShader11_h__
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include "render/IShader.h"
#include "render/IMacroDefine.h"
#include <vector>
#include "Globals.h"
#include "D3Dcompiler.h"
namespace LifeGraphics
{
class IPixelShader11 : public IShader
{
public:
ID3D11PixelShader* g_shader;
ID3DBlob* g_bytecode;
ID3D11Device* g_device;
IPixelShader11(ID3D11Device* device)
{
g_device = device;
}
~IPixelShader11()
{
SAFE_RELEASE(g_bytecode);
SAFE_RELEASE(g_shader);
}
void* Disassemble()
{
/*ID3D10Blob* errorMsg = NULL;
D3D10DisassembleShader(g_bytecode->GetBufferPointer(), g_bytecode->GetBufferSize(),TRUE, "OLOLO", &errorMsg);
return errorMsg->GetBufferPointer();*/
return 0;
}
SHADER_TYPE getShaderType()
{
return ST_PS;
}
char* GetByteCode()
{
return (char*)0;
}
void Compile(LifeCore::IDataStreamPtr stream, std::vector<IMacroDefine>* defines)
{
char* shader = new char[stream->Length()];
stream->Read(shader, stream->Length());
Compile(shader, stream->Length(), defines);
delete[]shader;
}
void Compile(const char* pData, int length, std::vector<IMacroDefine>* defines)
{
DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#ifdef _DEBUG
dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif
HRESULT hr;
g_bytecode = NULL;
// char* ololo = new char[10*1024*1024];
// void* lol = malloc(10*1024*1024);
ID3D10Blob* errorMsg = NULL;
if(!defines)
{
hr = D3DX11CompileFromMemory(pData, length, "system_shader", NULL, NULL, "main_PS", "ps_5_0",dwShaderFlags,0,NULL, &g_bytecode,&errorMsg, NULL);
}
else
{
//create macro
D3D10_SHADER_MACRO* macros = new D3D10_SHADER_MACRO[defines->size() + 1];
int cnt = defines->size();
for(int i = 0; i < cnt; i++)
{
macros[i].Definition = defines[0][i].definition.c_str();
macros[i].Name = defines[0][i].name.c_str();
}
D3D10_SHADER_MACRO m;
m.Definition = NULL;
m.Name = NULL;
macros[cnt] = m;
hr = D3DX11CompileFromMemory(pData, length, "system_shader", macros, NULL, "main_PS", "ps_5_0",dwShaderFlags,0,NULL, &g_bytecode, &errorMsg, NULL);
delete[] macros;
}
if(hr != S_OK)
{
printf("SHADER COMPILE ERROR: %s", errorMsg->GetBufferPointer());
}
SAFE_RELEASE(errorMsg);
hr = g_device->CreatePixelShader(g_bytecode->GetBufferPointer(), g_bytecode->GetBufferSize(), NULL, &g_shader);
SAFE_RELEASE(g_bytecode);
}
void Load(LifeCore::IDataStreamPtr src)
{
printf("Load VS is not implemented: %i\n", src->Length());
}
void CompleteAsyncRequest(IResource* source)
{
// printf("VS async not implemented: %s\n", source->Name.c_str());
}
};
}
#endif // IPixelShader11_h__
@@ -0,0 +1,62 @@
#ifndef IRasterizerState11_h__
#define IRasterizerState11_h__
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include "render/IRasterizerState.h"
#include "Globals.h"
namespace LifeGraphics
{
class IRasterizerState11 : public IRasterizerState
{
public:
ID3D11Device* device;
ID3D11RasterizerState* state;
IRasterizerState11(FILL_MODE mode, ID3D11Device* _device)
{
device = _device;
D3D11_RASTERIZER_DESC rsDesc;
rsDesc.AntialiasedLineEnable = false;
rsDesc.CullMode = D3D11_CULL_BACK;
rsDesc.DepthBias = 0;
rsDesc.DepthBiasClamp = 0.0f;
rsDesc.SlopeScaledDepthBias = 0.0f;
rsDesc.DepthClipEnable = TRUE;
rsDesc.FillMode = static_cast<D3D11_FILL_MODE>(mode);
rsDesc.FrontCounterClockwise = FALSE;
rsDesc.MultisampleEnable = FALSE;
rsDesc.ScissorEnable = TRUE;
device->CreateRasterizerState(&rsDesc, &state);
}
IRasterizerState11(FILL_MODE mode, ID3D11Device* _device, CULL_MODE cmode)
{
device = _device;
D3D11_RASTERIZER_DESC rsDesc;
rsDesc.AntialiasedLineEnable = false;
rsDesc.CullMode = static_cast<D3D11_CULL_MODE>(cmode);
rsDesc.DepthBias = 0;
rsDesc.DepthBiasClamp = 0.0f;
rsDesc.SlopeScaledDepthBias = 0.0f;
rsDesc.DepthClipEnable = TRUE;
rsDesc.FillMode = static_cast<D3D11_FILL_MODE>(mode);
rsDesc.FrontCounterClockwise = FALSE;
rsDesc.MultisampleEnable = FALSE;
rsDesc.ScissorEnable = TRUE;
device->CreateRasterizerState(&rsDesc, &state);
}
~IRasterizerState11()
{
SAFE_RELEASE(state);
}
};
}
#endif // IRasterizerState11_h__
@@ -0,0 +1,27 @@
#ifndef IRenderTargetViewDX11_h__
#define IRenderTargetViewDX11_h__
#include "render/IRenderTargetView.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include "Globals.h"
namespace LifeGraphics
{
class IRenderTargetView11 : public IRenderTargetView
{
public:
ID3D11RenderTargetView* view;
IRenderTargetView11(ID3D11RenderTargetView* _view)
{
view = _view;
}
~IRenderTargetView11()
{
SAFE_RELEASE(view);
}
};
}
#endif // IRenderTargetViewDX11_h__
@@ -0,0 +1,26 @@
#ifndef IShaderResourceView11_h__
#define IShaderResourceView11_h__
#include "../IShaderResourceView.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include "Globals.h"
namespace LifeGraphics
{
class IShaderResourceView11 : public IShaderResourceView
{
public:
ID3D11ShaderResourceView* view;
IShaderResourceView11(ID3D11ShaderResourceView* _view)
{
view = _view;
}
~IShaderResourceView11()
{
SAFE_RELEASE(view);
}
};
}
#endif // IShaderResourceView11_h__
+33
View File
@@ -0,0 +1,33 @@
#include "render/dx11/ISurface11.h"
namespace LifeGraphics
{
ISurface11::ISurface11()
{
}
void ISurface11::setRTV(ID3D11RenderTargetView* _rtv)
{
rtv = _rtv;
}
void ISurface11::setSRV(ID3D11ShaderResourceView* _srv)
{
srv = _srv;
}
void ISurface11::setDSV(ID3D11DepthStencilView* _dsv)
{
dsv = _dsv;
}
ID3D11RenderTargetView* ISurface11::getRTV()
{
return rtv;
}
ID3D11ShaderResourceView* ISurface11::getSRV()
{
return srv;
}
ID3D11DepthStencilView* ISurface11::getDSV()
{
return dsv;
}
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef ISurface11_h__
#define ISurface11_h__
#include "render/ISurface.h"
#include "render/dx11/IRenderTargetViewDX11.h"
#include "render/dx11/IShaderResourceView11.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
namespace LifeGraphics
{
class ISurface11 : public ISurface
{
public:
ISurface11();
void setRTV(ID3D11RenderTargetView* _rtv);
void setSRV(ID3D11ShaderResourceView* _srv);
void setDSV(ID3D11DepthStencilView* _dsv);
ID3D11RenderTargetView* getRTV();
ID3D11ShaderResourceView* getSRV();
ID3D11DepthStencilView* getDSV();
protected:
ID3D11RenderTargetView* rtv;
ID3D11ShaderResourceView* srv;
ID3D11DepthStencilView* dsv;
};
}
#endif // ISurface11_h__
+85
View File
@@ -0,0 +1,85 @@
#include "render/dx11/ISwapChain11.h"
#include <Windows.h>
#include "render/dx11/ITexture2D11.h"
namespace LifeGraphics
{
ISwapChain11::ISwapChain11(ID3D11Device* device, LifeCore::IRenderWindow* wnd){
IDXGIDevice* dxgi_dev;
IDXGIAdapter* dxgi_adapter;
IDXGIFactory* gxgi_factory;
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
//fill swap chain description parameters
sd.BufferCount = 1;
sd.BufferDesc.Width = wnd->GetWidth();
sd.BufferDesc.Height = wnd->GetHeight();
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)wnd->GetWindowHandle();
sd.Windowed = true;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
device->QueryInterface(__uuidof(IDXGIDevice), (void**)(&dxgi_dev));
dxgi_dev->GetParent(__uuidof(IDXGIAdapter), (void**)(&dxgi_adapter));
dxgi_adapter->GetParent(__uuidof(IDXGIFactory), (void**)(&gxgi_factory));
// dxgi_adapter->EnumOutputs(0, &pDXGIOutput);
gxgi_factory->CreateSwapChain(device, &sd, &swap_chain);
gxgi_factory->MakeWindowAssociation((HWND)wnd->GetWindowHandle(), DXGI_MWA_NO_ALT_ENTER | DXGI_MWA_NO_WINDOW_CHANGES);
ID3D11Texture2D* back_buffer_raw;
ID3D11RenderTargetView* back_buffer_rtv;
swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&back_buffer_raw);
device->CreateRenderTargetView(back_buffer_raw, NULL, &back_buffer_rtv);
back_buffer = new LifeGraphics::ITexture2D11(device);
back_buffer->FromTexture(back_buffer_raw);
back_buffer->addRenderTargetView(new LifeGraphics::IRenderTargetView11(back_buffer_rtv));
D3D11_TEXTURE2D_DESC dDesc = {0};
dDesc.ArraySize = 1;
dDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
dDesc.CPUAccessFlags = 0;
dDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
dDesc.MipLevels = 1;
dDesc.SampleDesc.Count = 1;
dDesc.SampleDesc.Quality = 0;
dDesc.Usage = D3D11_USAGE_DEFAULT;
dDesc.Width = wnd->GetWidth();
dDesc.Height = wnd->GetHeight();
ID3D11Texture2D* depth_buffer_raw;
ID3D11DepthStencilView* depth_buffer_raw_dsv;
device->CreateTexture2D(&dDesc, NULL, &depth_buffer_raw);
device->CreateDepthStencilView(depth_buffer_raw,NULL, &depth_buffer_raw_dsv);
depth_buffer = new LifeGraphics::ITexture2D11(device);
depth_buffer->FromTexture(depth_buffer_raw);
depth_buffer->addDepthStencilView(depth_buffer_raw_dsv);
}
LifeGraphics::ITexture2D* ISwapChain11::GetBackBuffer(uint8 id){
return static_cast<ITexture2D*>(back_buffer);
}
LifeGraphics::ITexture2D* ISwapChain11::GetDepthBuffer(uint8 id){
return static_cast<ITexture2D*>(depth_buffer);
}
void ISwapChain11::Resize(uint16 width, uint16 height){
}
void ISwapChain11::Present(){
swap_chain->Present(0,0);
}
ISwapChain11::~ISwapChain11(){
}
}
+24
View File
@@ -0,0 +1,24 @@
#ifndef ISwapChain11_h__
#define ISwapChain11_h__
#include "../../ISwapChain.h"
#include <IRenderWindow.h>
#include "render/dx11/ITexture2D11.h"
#include <d3d11.h>
namespace LifeGraphics{
class ISwapChain11 : public ISwapChain{
public:
ISwapChain11(ID3D11Device* device, LifeCore::IRenderWindow* wnd);
LifeGraphics::ITexture2D* GetBackBuffer(uint8 id);
LifeGraphics::ITexture2D* GetDepthBuffer(uint8 id);
void Resize(uint16 width, uint16 height);
void Present();
~ISwapChain11();
private:
IDXGISwapChain* swap_chain;
LifeGraphics::ITexture2D11* back_buffer;
LifeGraphics::ITexture2D11* depth_buffer;
};
}
#endif // ISwapChain11_h__
+325
View File
@@ -0,0 +1,325 @@
#ifndef ITEXTURE2D11_H__
#define ITEXTURE2D11_H__
#include "render/ITexture2D.h"
#include "render/dx11/IRenderTargetViewDX11.h"
#include "render/dx11/IShaderResourceView11.h"
#include "render/dx11/ISurface11.h"
#include "ResourceManager.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include <vector>
#include "Globals.h"
#include <log/CLog.h>
namespace LifeGraphics
{
class ITexture2D11 : public ITexture2D
{
private:
int FormatToSize(DXGI_FORMAT format)
{
switch(format)
{
case DXGI_FORMAT_R32G32B32A32_TYPELESS:
case DXGI_FORMAT_R32G32B32A32_FLOAT:
case DXGI_FORMAT_R32G32B32A32_UINT:
case DXGI_FORMAT_R32G32B32A32_SINT:
return 16;
case DXGI_FORMAT_R32G32B32_TYPELESS:
case DXGI_FORMAT_R32G32B32_FLOAT:
case DXGI_FORMAT_R32G32B32_UINT:
case DXGI_FORMAT_R32G32B32_SINT:
return 12;
case DXGI_FORMAT_R16G16B16A16_TYPELESS:
case DXGI_FORMAT_R16G16B16A16_FLOAT:
case DXGI_FORMAT_R16G16B16A16_UNORM:
case DXGI_FORMAT_R16G16B16A16_UINT:
case DXGI_FORMAT_R16G16B16A16_SNORM:
case DXGI_FORMAT_R16G16B16A16_SINT:
case DXGI_FORMAT_R32G32_TYPELESS:
case DXGI_FORMAT_R32G32_FLOAT:
case DXGI_FORMAT_R32G32_UINT:
case DXGI_FORMAT_R32G32_SINT:
case DXGI_FORMAT_R32G8X24_TYPELESS:
case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
return 8;
case DXGI_FORMAT_R10G10B10A2_TYPELESS:
case DXGI_FORMAT_R10G10B10A2_UNORM:
case DXGI_FORMAT_R10G10B10A2_UINT:
case DXGI_FORMAT_R11G11B10_FLOAT:
case DXGI_FORMAT_R8G8B8A8_TYPELESS:
case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
case DXGI_FORMAT_R8G8B8A8_UINT:
case DXGI_FORMAT_R8G8B8A8_SNORM:
case DXGI_FORMAT_R8G8B8A8_SINT:
case DXGI_FORMAT_R16G16_TYPELESS:
case DXGI_FORMAT_R16G16_FLOAT:
case DXGI_FORMAT_R16G16_UNORM:
case DXGI_FORMAT_R16G16_UINT:
case DXGI_FORMAT_R16G16_SNORM:
case DXGI_FORMAT_R16G16_SINT:
case DXGI_FORMAT_R32_TYPELESS:
case DXGI_FORMAT_D32_FLOAT:
case DXGI_FORMAT_R32_FLOAT:
case DXGI_FORMAT_R32_UINT:
case DXGI_FORMAT_R32_SINT:
case DXGI_FORMAT_R24G8_TYPELESS:
case DXGI_FORMAT_D24_UNORM_S8_UINT:
case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
case DXGI_FORMAT_R9G9B9E5_SHAREDEXP:
case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_B8G8R8X8_UNORM:
case DXGI_FORMAT_B8G8R8A8_TYPELESS:
case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
case DXGI_FORMAT_B8G8R8X8_TYPELESS:
case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
return 4;
case DXGI_FORMAT_R8G8_TYPELESS:
case DXGI_FORMAT_R8G8_UNORM:
case DXGI_FORMAT_R8G8_UINT:
case DXGI_FORMAT_R8G8_SNORM:
case DXGI_FORMAT_R8G8_SINT:
case DXGI_FORMAT_R16_TYPELESS:
case DXGI_FORMAT_R16_FLOAT:
case DXGI_FORMAT_D16_UNORM:
case DXGI_FORMAT_R16_UNORM:
case DXGI_FORMAT_R16_UINT:
case DXGI_FORMAT_R16_SNORM:
case DXGI_FORMAT_R16_SINT:
case DXGI_FORMAT_B5G6R5_UNORM:
case DXGI_FORMAT_B5G5R5A1_UNORM:
return 2;
case DXGI_FORMAT_R8_TYPELESS:
case DXGI_FORMAT_R8_UNORM:
case DXGI_FORMAT_R8_UINT:
case DXGI_FORMAT_R8_SNORM:
case DXGI_FORMAT_R8_SINT:
case DXGI_FORMAT_A8_UNORM:
return 1;
}
return -1;
}
public:
ID3D11ShaderResourceView* cubeResourceView;
ISurface* getSurface(int id)
{
return &surfaces[id];
}
bool IsVideoTexture()
{
return false;
}
ITexture2D11(ID3D11Device* _device)
{
IsRenderTarget = 0;
IsDepthTarget = 0;
pDevice = _device;
pDevice->GetImmediateContext(&context);
}
~ITexture2D11()
{
for(UINT i = 0; i<rtViews.size(); i++)
{
SAFE_DELETE(rtViews[i]);
}
for(UINT i = 0; i<srViews.size(); i++)
{
SAFE_DELETE(srViews[i]);
}
//SAFE_RELEASE(dsView); FIX ME
rtViews.clear();
srViews.clear();
SAFE_RELEASE(texture);
}
std::vector<IRenderTargetView11*> rtViews;
std::vector<IShaderResourceView11*> srViews;
std::vector<ID3D11DepthStencilView*> dsViews;
ID3D11Texture2D* texture;
ID3D11Device* pDevice;
ID3D11DeviceContext* context;
LifeCore::IDataRectangle rectangle;
D3D11_MAPPED_SUBRESOURCE mappedData;
int t_width, t_height;
int IsRenderTarget;
int IsDepthTarget;
IShaderResourceView* getSRView()
{
return srViews[0];
}
IShaderResourceView* getSRView(int index)
{
return srViews[index];
}
ID3D11DepthStencilView* getDSView()
{
return dsViews[0];
}
ID3D11DepthStencilView* getDSView(int index)
{
return dsViews[index];
}
IRenderTargetView* getRTView()
{
return rtViews[0];
}
IRenderTargetView* getRTView(int index)
{
return rtViews[index];
}
void addRenderTargetView(IRenderTargetView11* view)
{
rtViews.push_back(view);
surfaces[rtViews.size() - 1].setRTV(view->view);
}
void addDepthStencilView(ID3D11DepthStencilView* view)
{
dsViews.push_back(view);
surfaces[dsViews.size() - 1].setDSV(view);
}
void addShaderResourceView(IShaderResourceView11* view)
{
srViews.push_back(view);
surfaces[srViews.size() - 1].setSRV(view->view);
}
void FromTexture(ID3D11Texture2D* tex)
{
if (tex)
{
texture = tex;
D3D11_TEXTURE2D_DESC desc;
texture->GetDesc(&desc);
t_width = desc.Width;
t_height = desc.Height;
bpp = FormatToSize(desc.Format);
this->Description.Width = desc.Width;
this->Description.Height = desc.Height;
this->Description.SampleCount = desc.SampleDesc.Count;
this->Description.SampleQuality = desc.SampleDesc.Quality;
this->Description.Format = desc.Format;
}
}
ID3D11Texture2D* GetData()
{
return texture;
}
LifeCore::IDataRectangle* Map(int level, int read, int write)
{
D3D11_MAP flags = D3D11_MAP_WRITE_DISCARD;
if(!read && !write)
throw "ff";
if(read && !write)
flags = D3D11_MAP_READ;
else if(!read && write)
flags = D3D11_MAP_WRITE_DISCARD;
else if (read && write)
flags = D3D11_MAP_READ_WRITE;
context->Map(texture, level, flags,0, &mappedData);
rectangle.pitch = mappedData.RowPitch;
rectangle.data.Reset((char*)mappedData.pData, t_width * t_height * bpp);
return &rectangle;
// return new LifeCore::IDataRectangle(stm.RowPitch, stm.Data.DataPointer);
}
void Unmap(int level)
{
context->Unmap(texture, level);
// g_textureData.Device.ImmediateContext.UnmapSubresource(g_textureData, 0);
//g_textureData.AsSurface().Unmap();
}
int Width()
{
return t_width;
}
int Height()
{
return t_height;
}
void ToFile(const String& path)
{
D3DX11SaveTextureToFile(context,texture, D3DX11_IFF_DDS, path.c_str());
LifeCore::CLog::me()->Write(AS_STRING("OH SHIIIT!!! Texture saving!"));
//Logger.Instance.Write("WARNING! Saving render target to file!" + "\n" + path);
//Texture2D.ToFile((this.g_parent as RendererDX11).GetDeviceContext(), g_textureData,ImageFileFormat.Jpg, path );
}
void CompleteAsyncRequest(IResource* source)
{
//Core::CLog::me()->Write(String(_text("CompleteAsyncRequest called in texture2D!")) + source->Name);
//Texture2D11 src = (Texture2D11)source;
// this.FromTexture(src.g_textureData);
}
void Load(LifeCore::IDataStreamPtr source)
{
ID3D11Resource* tex = 0;
char* dataBuffer = new char[source->Length()];
source->Read(dataBuffer, source->Length());
HRESULT result = 0;
D3DX11_IMAGE_LOAD_INFO info;
info.Width = D3DX11_DEFAULT;
info.Height = D3DX11_DEFAULT;
info.Depth = D3DX11_DEFAULT;
info.FirstMipLevel = 0;
info.MipLevels = 6;
info.Usage = (D3D11_USAGE) D3DX11_DEFAULT;
info.BindFlags = D3DX11_DEFAULT;
info.CpuAccessFlags = D3DX11_DEFAULT;
info.MiscFlags = D3DX11_DEFAULT;
info.Format = DXGI_FORMAT_FROM_FILE;
info.Filter = D3DX11_FILTER_TRIANGLE;
info.MipFilter = D3DX11_FILTER_TRIANGLE;
info.pSrcInfo = NULL;
result = D3DX11CreateTextureFromMemory(pDevice, dataBuffer,source->Length(),&info, 0, &tex, 0);
delete[] dataBuffer;
if(result == S_OK)
FromTexture((ID3D11Texture2D*) tex);
else
LifeCore::CLog::me()->Write(String(_text("Can not load texture!")));
}
private:
ISurface11 surfaces[6];
/* virtual int Width();
virtual int Height();
virtual LifeCore::IDataRectangle* Map(int level, bool read, bool write, int bpp) = 0;
virtual void Unmap(int level) = 0;
virtual void ToFile(const String& path) = 0;
virtual void Load(const Core::IDataStream* src) {}
virtual void CompleteAsyncRequest(IResource* source) {}*/
};
}
#endif
@@ -0,0 +1,36 @@
#ifndef ITextureSampler11_h__
#define ITextureSampler11_h__
#include "render/ITextureSampler.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include "Globals.h"
namespace LifeGraphics
{
class ITextureSampler11 : public ITextureSampler
{
public:
ID3D11SamplerState* state;
ITextureSampler11(ISamplerDescription* desc, ID3D11Device* dev)
{
D3D11_SAMPLER_DESC d;
ZeroMemory(&d, sizeof(D3D11_SAMPLER_DESC));
d.AddressU = static_cast<D3D11_TEXTURE_ADDRESS_MODE>(desc->AddressU);
d.AddressV = static_cast<D3D11_TEXTURE_ADDRESS_MODE>(desc->AddressV);
d.AddressW = static_cast<D3D11_TEXTURE_ADDRESS_MODE>(desc->AddressW);
d.MaxAnisotropy = 1;
d.Filter = static_cast<D3D11_FILTER>(desc->Filter);
d.ComparisonFunc = static_cast<D3D11_COMPARISON_FUNC>(desc->CmpFunc);
d.MaxLOD = 3.402823466e+38F;
dev->CreateSamplerState(&d, &state);
}
~ITextureSampler11()
{
SAFE_RELEASE(state);
}
};
}
#endif // ITextureSampler11_h__
+109
View File
@@ -0,0 +1,109 @@
#ifndef I_VERTEX_BUFFER11_H__
#define I_VERTEX_BUFFER11_H__
#include "render/IVertexBuffer.h"
#include "ResourceManager.h"
#include "IMemoryStream.h"
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include "Globals.h"
namespace LifeGraphics
{
class IVertexBuffer11 : public IVertexBuffer
{
public:
ID3D11Buffer* vb;
ID3D11Device* device;
ID3D11DeviceContext* context;
D3D11_MAPPED_SUBRESOURCE mappedData;
LifeCore::IMemoryStream stm;
~IVertexBuffer11()
{
SAFE_RELEASE(vb);
}
IVertexBuffer11(ID3D11Device* _device, void* pBuffer, int _size, int _stride, int staging)
{
device = _device;
length = _size;
stride = _stride;
//g_desc = new BufferDescription(size, D3D11.ResourceUsage.Default,
// BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
if(pBuffer)
bd.Usage = D3D11_USAGE_DEFAULT;
else
bd.Usage = D3D11_USAGE_DYNAMIC;
if(staging)
bd.Usage = D3D11_USAGE_STAGING;
bd.ByteWidth = _size;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
if(bd.Usage == D3D11_USAGE_DEFAULT)
bd.CPUAccessFlags = 0;
else if(bd.Usage == D3D11_USAGE_DYNAMIC)
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
else if(bd.Usage == D3D11_USAGE_STAGING)
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
if(pBuffer)
{
D3D11_SUBRESOURCE_DATA InitData;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = pBuffer;
device->CreateBuffer( &bd, &InitData, &vb );
}
else
device->CreateBuffer( &bd, NULL, &vb );
device->GetImmediateContext(&context);
//if( FAILED( hr ) )
// return hr;
}
LifeCore::IDataStream* Map(int offset, int flags)
{
D3D11_MAP mapFlags;
if (flags & LF_Discard)
mapFlags = D3D11_MAP_WRITE_DISCARD;
else if ((flags & LF_NoOverwrite))
mapFlags = D3D11_MAP_WRITE_NO_OVERWRITE;
else if ((flags & LF_ReadOnly))
mapFlags = D3D11_MAP_READ;
else if (flags == LF_None)
mapFlags = D3D11_MAP_READ_WRITE;
else
mapFlags = D3D11_MAP_WRITE;
context->Map(vb,0, mapFlags,0, &mappedData);
stm.Reset(((char*)mappedData.pData) + offset, length);
return &stm;
}
void Update(void* data, size_t length)
{
D3D11_BOX b;
b.left = 0;
b.right = 1;
b.top = 0;
b.bottom = 1;
b.front = 0;
b.back = 0;
context->UpdateSubresource(vb, 0, NULL, data, length, 0);
}
void Unmap()
{
context->Unmap(vb, 0);
}
};
}
#endif
@@ -0,0 +1,93 @@
#ifndef IVertexShader11_h__
#define IVertexShader11_h__
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include "render/IShader.h"
#include "render/IMacroDefine.h"
#include <vector>
#include "Globals.h"
namespace LifeGraphics
{
class IVertexShader11 : public IShader
{
public:
ID3D11VertexShader* g_shader;
ID3DBlob* g_bytecode;
ID3D11Device* g_device;
IVertexShader11(ID3D11Device* device)
{
g_device = device;
}
~IVertexShader11()
{
SAFE_RELEASE(g_bytecode);
SAFE_RELEASE(g_shader);
}
void* Disassemble()
{
return 0;
}
SHADER_TYPE getShaderType()
{
return ST_VS;
}
char* GetByteCode()
{
return (char*)0;
}
void Compile(LifeCore::IDataStreamPtr stream, std::vector<IMacroDefine>* defines)
{
char* shader = new char[stream->Length()];
stream->Read(shader, stream->Length());
Compile(shader, stream->Length(), defines);
delete[]shader;
}
void Compile(const char* pData, int length, std::vector<IMacroDefine>* defines)
{
HRESULT hr;
g_bytecode = NULL;
ID3D10Blob* errorBlob;
if(!defines)
{
hr = D3DX11CompileFromMemory(pData, length, "system_shader", NULL, NULL, "main_VS", "vs_5_0",0,0,NULL, &g_bytecode,&errorBlob, NULL);
}
else
{
//create macro
D3D10_SHADER_MACRO* macros = new D3D10_SHADER_MACRO[defines->size() + 1];
int cnt = defines->size();
for(int i = 0; i < cnt; i++)
{
macros[i].Definition = defines[0][i].definition.c_str();
macros[i].Name = defines[0][i].name.c_str();
}
D3D10_SHADER_MACRO m;
m.Definition = NULL;
m.Name = NULL;
macros[cnt] = m;
hr = D3DX11CompileFromMemory(pData, length, "system_shader", macros, NULL, "main_VS", "vs_5_0",0,0,NULL, &g_bytecode,&errorBlob, NULL);
delete[] macros;
}
if( FAILED( hr ) )
{
printf("SHADER COMPILE ERROR: %s", errorBlob->GetBufferPointer());
}
hr = g_device->CreateVertexShader(g_bytecode->GetBufferPointer(), g_bytecode->GetBufferSize(), NULL, &g_shader);
}
void Load(LifeCore::IDataStreamPtr src)
{
printf("Load VS is not implemented: %i\n", src->Length());
}
void CompleteAsyncRequest(IResource* source)
{
//printf("VS async not implemented: %s\n", source->Name.c_str());
}
};
}
#endif // IPixelShader11_h__
File diff suppressed because it is too large Load Diff
+213
View File
@@ -0,0 +1,213 @@
#ifndef RendererDX11_h__
#define RendererDX11_h__
#include "Globals.h"
#include <render/IRenderer.h>
#include "math/LifeMath.h"
#include <Build.h>
#include "ITexture2D11.h"
#include <d3dcompiler.h>
#include "render/dx11/IVertexBuffer11.h"
#include "render/dx11/IIndexBuffer11.h"
#include "Globals.h"
#include "render/RenderParams.h"
#include <PluginServer.h>
#include <d3d11.h>
#include <dxgi.h>
#include <d3dx11.h>
#include "render/dx11/IVertexShader11.h"
#include "render/dx11/IPixelShader11.h"
#include "render/dx11/IInputLayout11.h"
#include "render/dx11/IBlendState11.h"
#include "math/LifeMath.h"
#include "render/dx11/IRasterizerState11.h"
#include "render/dx11/ITextureSampler11.h"
#include "render/dx11/ISurface11.h"
#include "render/dx11/ISwapChain11.h"
namespace LifeGraphics
{
struct SimpleVertex
{
LifeMath::float4 Pos;
LifeMath::float2 TC;
};
class RendererDX11 : public IRenderer
{
public:
RendererDX11(){};
~RendererDX11(){printf("Destroying renderer...");Close();};
virtual HRESULT Open(RenderParams params);
virtual void Close();
//virtual void ClearBuffers(int flags, int rtColor, float zValue, int stencilValue);
virtual const String& GetRendererName() const {
static String sName("DirectX11 graphics driver");
return sName;
}
void ClearRenderTarget(ISurface* target, LifeMath::float4* color);
void ClearDepthStencil(ISurface* target, int flag, float zValue, int stencilValue);
ITexture2D* CreateRenderTarget(int width, int height, int format);
ITexture2D* CreateDepthTarget(int width, int height, int format);
ITexture2D* LoadTexture2D(LifeCore::IDataStreamPtr src);
ITexture2D* CreateTexture2D(int canRead, int canWrite, int width, int height, int format);
void RenderToTexture();
void RenderToTextureNoPs();
void RenderToTextureNoShader();
IVertexBuffer* CreateVertexBuffer(int size, void* data, int stride);
IVertexBuffer* CreateVertexBufferDynamic(int size, int stride);
IIndexBuffer* CreateIndexBuffer(int size, void* data, int sixteenBit);
IIndexBuffer* CreateIndexBufferDynamic(int size, bool sixteenBit);
IInputLayout* CreateInputLayout(IInputElement* elements, int elementCount, IShader* vertexShader);
IShader* CompileVertexShader(const char* pData, int length);
IShader* CompileVertexShader(LifeCore::IDataStreamPtr stream);
IShader* CompileVertexShader(LifeCore::IDataStreamPtr stream, std::vector<IMacroDefine>* defines);
IShader* CompilePixelShader(const char* pData, int length);
IShader* CompilePixelShader(LifeCore::IDataStreamPtr stream);
IShader* CompilePixelShader(LifeCore::IDataStreamPtr stream, std::vector<IMacroDefine>* defines);
IConstantBuffer* CreateConstantBuffer(int size);
void SetVSConstantBuffer(IConstantBuffer* buffer, int slot);
void SetPSConstantBuffer(IConstantBuffer* buffer, int slot);
void SetInputLayout(IInputLayout* layout);
void RenderMesh(IMesh* mesh);
void SetMaterial(IMaterial* material);
void SetTargets(ITexture2D* colorTarget, ITexture2D* depthTarget);
void SetTargets(ISurface* colorTarget, ISurface* depthTarget);
void SetCubeTarget(ITexture2D* colorTarget, ITexture2D* depthTarget, int faceIndex);
void SetVertexBuffer(IVertexBuffer* vb);
void SetIndexBuffer(IIndexBuffer* ib);
void SetPrimitiveTopology(PRIMITIVE_TOPOLOGY topology);
void SetPSTextureCube(ITexture2D* texture, int slot);
void DrawIndexed(int offset, int length);
void SetVertexShader(IShader* shader);
void SetPixelShader(IShader* shader);
void SetPSTexture(ITexture2D* texture, int slot);
void SetVSTexture(ITexture2D* texture, int slot);
void UnbindResourcesPS();
void Draw(int vertexCount, int vertexOffset);
IBlendState* CreateBlendState(bool blendEnable, bool atocEnable, BLEND_OP blendOperation, BLEND_SRC srcBlend, BLEND_SRC dstBlend, uint8 colorMask);
void SetBlendState(IBlendState* state);
IDepthStencilState* CreateDSState(bool zEnable, bool zWrite, COMPARISON_FUNC zFunc);
IDepthStencilState* CreateDSState(bool zEnable, bool zWrite, COMPARISON_FUNC zFunc, bool stencil, int stencilMask, int stencilFunc, int stencilFail, int depthFail, int depthFailBack,int stencilPass);
void SetDepthStencilState(IDepthStencilState* state, UINT sref = 0);
void SetScissorRectangle(const LifeMath::Rect2i& rect);
void SetViewport(const LifeMath::Rect2i& rect);
ITexture2D* CreateRenderTargetCube(int size, int format);
ITexture2D* CreateDepthTargetCube(int width, int format);
void SetPSTextureNULL(int slot);
void SetPSTextureSampler(ITextureSampler* sampler, int slot);
ITextureSampler* CreateTextureSampler(ISamplerDescription* desc);
void SetColorTarget(ITexture2D* colorTarget);
void SetDepthTarget(ITexture2D* depthTarget);
void VerticalBlur(ITexture2D* src, ITexture2D* dst, int numSamples);
void SetPSTexture(ITexture2D* texture, ITexture2D* texture2, int start, int count);
IShader* CompileUserShaderPS(char* data);
IVertexBuffer11* testVb;
IRasterizerState* CreateRasterizerState(FILL_MODE mode);
void DrawIndexed(int offset, int length, int baseVertex);
void SetRasterizerState(IRasterizerState* state);
void SetTargets(ITexture2D** colorTargets, int targetCount, ITexture2D* depthTarget);
void UnbindRenderTargets();
void SetVertexBuffer(IVertexBuffer** vb, int count);
void DrawIndexedInstanced(int offset, int length, int instanceCnt);
void DrawIndexedInstanced(int offset, int length, int baseVertex, int instanceCnt);
void SetViewport(const LifeMath::Rect2i& rect, float minDepth, float maxDepth);
IRasterizerState* CreateRasterizerState(FILL_MODE mode, CULL_MODE cmode);
void DownSample2x2(ITexture2D* src, ITexture2D* dst, ITexture2D* depth);
void BlurGaussH(ITexture2D* src, ITexture2D* dst, ITexture2D* depth);
void BlurGaussV(ITexture2D* src, ITexture2D* dst, ITexture2D* depth);
void BeginCleanup();
void EndCleanup();
void ClearRenderTarget(ITexture2D* tex, LifeMath::float4* color);
void ClearDepthStencil(ITexture2D* tex, float zValue, int stencilValue);
void ClearDepthStencil(ITexture2D* tex, int flag, float zValue, int stencilValue);
int GetDisplayModes(LifeCore::DisplayMode** modeList);
ISwapChain* CreateSwapChain(LifeCore::IRenderWindow* window);
private:
IShader* ds2x2;
IConstantBuffer* ds2x2cb;
IDXGIAdapter* pDXGIAdapter;
IDXGIDevice* pDXGIDevice;
IDXGIOutput* pDXGIOutput;
ID3D11RasterizerState* rState;
ID3D11Device* g_pDevice;
ID3D11DeviceContext* g_pDeviceContext;
//IDXGISwapChain* g_pSwapChain;
//ID3D11RenderTargetView* g_pBackBuffer;
//ID3D11DepthStencilView* g_pDepthBuffer;
//ID3D11RenderTargetView* currentBackBuffer;
//ID3D11DepthStencilView* currentDepthBuffer;
D3D_FEATURE_LEVEL g_pFeatureLevel;
D3D_DRIVER_TYPE g_driverType;
ID3D11Buffer* fsQuadBuffer;
ID3D11PixelShader* psQuad;
ID3D11VertexShader* vsQuad;
ID3D11InputLayout* fsQuadLayout;
ID3D11SamplerState* samplerLinear;
ID3D11SamplerState* samplerPoint;
//ID3D11Texture2D* pBuffer;
//ID3D11Texture2D* dBuf;
//Camera* cam;
ID3D11PixelShader* psVerticalBlur;
IConstantBuffer* vbBuffer;
IDXGIFactory* pFactory;
};
}
#endif // RendererDX11_h__