2019-08-23 11:56:54 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#ifdef BF_MINGW
|
|
|
|
#define D3D11_APPEND_ALIGNED_ELEMENT ( 0xffffffff )
|
|
|
|
#ifndef __C89_NAMELESS
|
|
|
|
#define __C89_NAMELESS
|
|
|
|
#define __C89_NAMELESSUNIONNAME
|
|
|
|
#endif
|
|
|
|
#pragma clang diagnostic ignored "-Wunknown-pragmas"
|
|
|
|
#pragma clang diagnostic ignored "-Wunknown-attributes"
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-member-function"
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-conversion-function"
|
|
|
|
#define __in
|
|
|
|
#define __in_opt
|
|
|
|
#define __in_ecount(a)
|
|
|
|
#define __in_ecount_opt(a)
|
|
|
|
#define __in_bcount(a)
|
|
|
|
#define __in_bcount_opt(a)
|
|
|
|
#define __inout
|
|
|
|
#define __inout_opt
|
|
|
|
#define __out
|
|
|
|
#define __out_opt
|
|
|
|
#define __out_bcount(a)
|
|
|
|
#define __out_bcount_opt(a)
|
|
|
|
#define __out_ecount(a)
|
|
|
|
#define __out_ecount_opt(a)
|
|
|
|
#define __out_ecount_part_opt(a, b)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#pragma warning (push)
|
|
|
|
#pragma warning (disable:4005)
|
|
|
|
#include <d3d11.h>
|
|
|
|
#pragma warning (pop)
|
|
|
|
|
|
|
|
#ifdef BF_MINGW
|
|
|
|
#undef __in
|
|
|
|
#undef __out
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Common.h"
|
|
|
|
#include "gfx/Shader.h"
|
|
|
|
#include "gfx/Texture.h"
|
|
|
|
#include "gfx/RenderDevice.h"
|
|
|
|
#include "gfx/DrawLayer.h"
|
|
|
|
#include "gfx/ModelInstance.h"
|
|
|
|
#include "util/HashSet.h"
|
2021-05-12 07:24:29 -04:00
|
|
|
#include "util/Dictionary.h"
|
2019-08-23 11:56:54 -07:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
NS_BF_BEGIN;
|
|
|
|
|
2020-05-11 10:31:12 -07:00
|
|
|
class WinBFWindow;
|
2019-08-23 11:56:54 -07:00
|
|
|
class BFApp;
|
|
|
|
class DXRenderDevice;
|
|
|
|
|
|
|
|
class DXTexture : public Texture
|
|
|
|
{
|
|
|
|
public:
|
2021-05-12 07:24:29 -04:00
|
|
|
String mPath;
|
2019-08-23 11:56:54 -07:00
|
|
|
DXRenderDevice* mRenderDevice;
|
|
|
|
ID3D11Texture2D* mD3DTexture;
|
|
|
|
ID3D11ShaderResourceView* mD3DResourceView;
|
2022-05-15 08:00:55 -07:00
|
|
|
ID3D11RenderTargetView* mD3DRenderTargetView;
|
2019-08-23 11:56:54 -07:00
|
|
|
ID3D11Texture2D* mD3DDepthBuffer;
|
2022-05-15 08:00:55 -07:00
|
|
|
ID3D11DepthStencilView* mD3DDepthStencilView;
|
|
|
|
uint32* mContentBits;
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
DXTexture();
|
|
|
|
~DXTexture();
|
2022-05-15 08:00:55 -07:00
|
|
|
|
|
|
|
void ReleaseNative();
|
|
|
|
void ReinitNative();
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
virtual void PhysSetAsTarget() override;
|
|
|
|
virtual void Blt(ImageData* imageData, int x, int y) override;
|
|
|
|
virtual void SetBits(int destX, int destY, int destWidth, int destHeight, int srcPitch, uint32* bits) override;
|
2022-05-15 08:00:55 -07:00
|
|
|
virtual void GetBits(int srcX, int srcY, int srcWidth, int srcHeight, int destPitch, uint32* bits) override;
|
2019-08-23 11:56:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class DXShaderParam : public ShaderParam
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ID3D10EffectVariable* mD3DVariable;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DXShaderParam();
|
|
|
|
~DXShaderParam();
|
|
|
|
|
|
|
|
virtual void SetTexture(Texture* texture);
|
|
|
|
virtual void SetFloat4(float x, float y, float z, float w) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<String, DXShaderParam*> DXShaderParamMap;
|
|
|
|
|
|
|
|
class DXShader : public Shader
|
|
|
|
{
|
2022-05-15 08:00:55 -07:00
|
|
|
public:
|
|
|
|
DXRenderDevice* mRenderDevice;
|
|
|
|
String mSrcPath;
|
|
|
|
VertexDefinition* mVertexDef;
|
|
|
|
|
2019-08-23 11:56:54 -07:00
|
|
|
ID3D11InputLayout* mD3DLayout;
|
|
|
|
ID3D11VertexShader* mD3DVertexShader;
|
|
|
|
ID3D11PixelShader* mD3DPixelShader;
|
|
|
|
DXShaderParamMap mParamsMap;
|
|
|
|
ID3D11Buffer* mConstBuffer;
|
|
|
|
bool mHas2DPosition;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DXShader();
|
|
|
|
~DXShader();
|
|
|
|
|
2022-05-15 08:00:55 -07:00
|
|
|
void ReleaseNative();
|
|
|
|
void ReinitNative();
|
|
|
|
|
|
|
|
bool Load();
|
|
|
|
virtual ShaderParam* GetShaderParam(const StringImpl& name) override;
|
2019-08-23 11:56:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class DXDrawBatch : public DrawBatch
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
public:
|
|
|
|
DXDrawBatch();
|
|
|
|
~DXDrawBatch();
|
|
|
|
|
|
|
|
virtual void Render(RenderDevice* renderDevice, RenderWindow* renderWindow) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DXDrawLayer : public DrawLayer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual DrawBatch* CreateDrawBatch();
|
|
|
|
virtual RenderCmd* CreateSetTextureCmd(int textureIdx, Texture* texture) override;
|
2021-05-12 07:24:29 -04:00
|
|
|
virtual void SetShaderConstantData(int usageIdx, int slotIdx, void* constData, int size) override;
|
|
|
|
virtual void SetShaderConstantDataTyped(int usageIdx, int slotIdx, void* constData, int size, int* typeData, int typeCount) override;
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
DXDrawLayer();
|
|
|
|
~DXDrawLayer();
|
|
|
|
};
|
|
|
|
|
|
|
|
class DXRenderWindow : public RenderWindow
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
HWND mHWnd;
|
|
|
|
DXRenderDevice* mDXRenderDevice;
|
|
|
|
IDXGISwapChain* mDXSwapChain;
|
|
|
|
ID3D11Texture2D* mD3DBackBuffer;
|
|
|
|
ID3D11RenderTargetView* mD3DRenderTargetView;
|
|
|
|
ID3D11Texture2D* mD3DDepthBuffer;
|
|
|
|
ID3D11DepthStencilView* mD3DDepthStencilView;
|
2022-05-15 08:00:55 -07:00
|
|
|
HANDLE mFrameWaitObject;
|
|
|
|
float mRefreshRate;
|
2019-08-23 11:56:54 -07:00
|
|
|
bool mResizePending;
|
2022-05-15 08:00:55 -07:00
|
|
|
bool mWindowed;
|
2019-08-23 11:56:54 -07:00
|
|
|
int mPendingWidth;
|
|
|
|
int mPendingHeight;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void PhysSetAsTarget();
|
|
|
|
|
|
|
|
public:
|
2020-05-11 10:31:12 -07:00
|
|
|
DXRenderWindow(DXRenderDevice* renderDevice, WinBFWindow* window, bool windowed);
|
2019-08-23 11:56:54 -07:00
|
|
|
~DXRenderWindow();
|
|
|
|
|
|
|
|
void ReleaseNative();
|
|
|
|
void ReinitNative();
|
|
|
|
|
|
|
|
void SetAsTarget() override;
|
|
|
|
void Resized() override;
|
|
|
|
virtual void Present() override;
|
|
|
|
|
|
|
|
void CopyBitsTo(uint32* dest, int width, int height);
|
2022-05-15 08:00:55 -07:00
|
|
|
virtual float GetRefreshRate() override;
|
|
|
|
virtual bool WaitForVBlank() override;
|
2019-08-23 11:56:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<DXDrawBatch*> DXDrawBatchVector;
|
|
|
|
|
|
|
|
#define DX_VTXBUFFER_SIZE 1024*1024
|
|
|
|
#define DX_IDXBUFFER_SIZE 64*1024
|
|
|
|
|
|
|
|
class DXDrawBufferPool
|
|
|
|
{
|
|
|
|
public:
|
2022-05-15 08:00:55 -07:00
|
|
|
std::vector<void*> mPooledIndexBuffers;
|
2019-08-23 11:56:54 -07:00
|
|
|
int mIdxPoolIdx;
|
|
|
|
std::vector<void*> mPooledVertexBuffers;
|
|
|
|
int mVtxPoolIdx;
|
|
|
|
|
|
|
|
void* mIndexBuffer;
|
|
|
|
void* mVertexBuffer;
|
|
|
|
int mIdxByteIdx;
|
|
|
|
int mVtxByteIdx;
|
|
|
|
|
|
|
|
void AllocateIndices(int minIndices);
|
|
|
|
void AllocVertices(int minVertices);
|
|
|
|
};
|
|
|
|
|
|
|
|
class DXRenderState : public RenderState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ID3D11RasterizerState* mD3DRasterizerState;
|
|
|
|
ID3D11DepthStencilState* mD3DDepthStencilState;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DXRenderState();
|
|
|
|
~DXRenderState();
|
|
|
|
|
|
|
|
void ReleaseNative();
|
|
|
|
void ReinitNative();
|
|
|
|
|
|
|
|
void InvalidateRasterizerState();
|
|
|
|
void IndalidateDepthStencilState();
|
|
|
|
|
|
|
|
virtual void SetClipped(bool clipped);
|
2021-05-12 07:24:29 -04:00
|
|
|
virtual void SetTexWrap(bool clipped);
|
2019-08-23 11:56:54 -07:00
|
|
|
virtual void SetClipRect(const Rect& rect);
|
|
|
|
virtual void SetWriteDepthBuffer(bool writeDepthBuffer);
|
|
|
|
virtual void SetDepthFunc(DepthFunc depthFunc);
|
|
|
|
};
|
|
|
|
|
2021-05-12 07:24:29 -04:00
|
|
|
class DXModelPrimitives
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
|
|
|
public:
|
2021-05-12 07:24:29 -04:00
|
|
|
String mMaterialName;
|
2019-08-23 11:56:54 -07:00
|
|
|
int mNumIndices;
|
|
|
|
int mNumVertices;
|
2021-05-12 07:24:29 -04:00
|
|
|
Array<DXTexture*> mTextures;
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
ID3D11Buffer* mD3DIndexBuffer;
|
|
|
|
//TODO: Split the vertex buffer up into static and dynamic buffers
|
|
|
|
ID3D11Buffer* mD3DVertexBuffer;
|
|
|
|
|
|
|
|
public:
|
2021-05-12 07:24:29 -04:00
|
|
|
DXModelPrimitives();
|
|
|
|
~DXModelPrimitives();
|
|
|
|
};
|
|
|
|
|
|
|
|
class DXModelMesh
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Array<DXModelPrimitives> mPrimitives;
|
2019-08-23 11:56:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class DXModelInstance : public ModelInstance
|
|
|
|
{
|
|
|
|
public:
|
2022-05-15 08:00:55 -07:00
|
|
|
DXRenderDevice* mD3DRenderDevice;
|
|
|
|
Array<DXModelMesh> mDXModelMeshs;
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
DXModelInstance(ModelDef* modelDef);
|
|
|
|
~DXModelInstance();
|
|
|
|
|
|
|
|
virtual void CommandQueued(DrawLayer* drawLayer) override;
|
|
|
|
virtual void Render(RenderDevice* renderDevice, RenderWindow* renderWindow) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DXVertexDefinition : public VertexDefinition
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~DXVertexDefinition();
|
|
|
|
};
|
|
|
|
|
|
|
|
class DXSetTextureCmd : public RenderCmd
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int mTextureIdx;
|
|
|
|
Texture* mTexture;
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void Render(RenderDevice* renderDevice, RenderWindow* renderWindow) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DXSetConstantData : public RenderCmd
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int mUsageIdx; // 0 = VS, 1 = PS
|
|
|
|
int mSlotIdx;
|
|
|
|
int mSize;
|
|
|
|
uint8 mData[1];
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void Render(RenderDevice* renderDevice, RenderWindow* renderWindow) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DXRenderDevice : public RenderDevice
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IDXGIFactory* mDXGIFactory;
|
|
|
|
ID3D11Device* mD3DDevice;
|
|
|
|
ID3D11DeviceContext* mD3DDeviceContext;
|
2022-05-15 08:00:55 -07:00
|
|
|
ID3D11BlendState* mD3DNormalBlendState;
|
2019-08-23 11:56:54 -07:00
|
|
|
ID3D11SamplerState* mD3DDefaultSamplerState;
|
2021-05-12 07:24:29 -04:00
|
|
|
ID3D11SamplerState* mD3DWrapSamplerState;
|
2022-05-15 08:00:55 -07:00
|
|
|
bool mNeedsReinitNative;
|
2019-08-23 11:56:54 -07:00
|
|
|
|
2022-05-15 08:00:55 -07:00
|
|
|
ID3D11Buffer* mMatrix2DBuffer;
|
2019-08-23 11:56:54 -07:00
|
|
|
ID3D11Buffer* mD3DVertexBuffer;
|
|
|
|
ID3D11Buffer* mD3DIndexBuffer;
|
|
|
|
int mVtxByteIdx;
|
|
|
|
int mIdxByteIdx;
|
|
|
|
|
|
|
|
HashSet<DXRenderState*> mRenderStates;
|
2021-05-12 07:24:29 -04:00
|
|
|
HashSet<DXTexture*> mTextures;
|
2022-05-15 08:00:55 -07:00
|
|
|
HashSet<DXShader*> mShaders;
|
2021-05-12 07:24:29 -04:00
|
|
|
Dictionary<String, DXTexture*> mTextureMap;
|
|
|
|
Dictionary<int, ID3D11Buffer*> mBufferMap;
|
|
|
|
|
2019-08-23 11:56:54 -07:00
|
|
|
public:
|
|
|
|
virtual void PhysSetRenderState(RenderState* renderState) override;
|
|
|
|
virtual void PhysSetRenderWindow(RenderWindow* renderWindow);
|
|
|
|
virtual void PhysSetRenderTarget(Texture* renderTarget) override;
|
|
|
|
virtual RenderState* CreateRenderState(RenderState* srcRenderState) override;
|
2021-05-25 10:57:22 -04:00
|
|
|
virtual ModelInstance* CreateModelInstance(ModelDef* modelDef, ModelCreateFlags flags) override;
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
DXRenderDevice();
|
|
|
|
virtual ~DXRenderDevice();
|
|
|
|
bool Init(BFApp* app) override;
|
|
|
|
|
|
|
|
void ReleaseNative();
|
|
|
|
void ReinitNative();
|
|
|
|
|
|
|
|
void FrameStart() override;
|
|
|
|
void FrameEnd() override;
|
|
|
|
|
2021-05-12 07:24:29 -04:00
|
|
|
Texture* LoadTexture(const StringImpl& fileName, int flags) override;
|
2019-08-23 11:56:54 -07:00
|
|
|
Texture* LoadTexture(ImageData* imageData, int flags) override;
|
|
|
|
Texture* CreateDynTexture(int width, int height) override;
|
|
|
|
Shader* LoadShader(const StringImpl& fileName, VertexDefinition* vertexDefinition) override;
|
|
|
|
Texture* CreateRenderTarget(int width, int height, bool destAlpha) override;
|
|
|
|
|
|
|
|
void SetRenderState(RenderState* renderState) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_BF_END;
|