2012-07-23 08:08:58 +04:00
|
|
|
/*
|
2014-02-11 10:07:04 +04:00
|
|
|
* Copyright 2011-2014 Branimir Karadzic. All rights reserved.
|
2012-07-23 08:08:58 +04:00
|
|
|
* License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2013-11-14 09:54:36 +04:00
|
|
|
#ifndef BGFX_RENDERER_D3D11_H_HEADER_GUARD
|
|
|
|
#define BGFX_RENDERER_D3D11_H_HEADER_GUARD
|
2012-07-23 08:08:58 +04:00
|
|
|
|
2012-08-20 05:50:23 +04:00
|
|
|
#define D3D11_NO_HELPERS
|
2012-07-23 08:08:58 +04:00
|
|
|
#include <d3d11.h>
|
|
|
|
#include "renderer_d3d.h"
|
|
|
|
|
2013-06-10 02:28:25 +04:00
|
|
|
#define D3DCOLOR_ARGB(_a, _r, _g, _b) ( (DWORD)( ( ( (_a)&0xff)<<24)|( ( (_r)&0xff)<<16)|( ( (_g)&0xff)<<8)|( (_b)&0xff) ) )
|
|
|
|
#define D3DCOLOR_RGBA(_r, _g, _b, _a) D3DCOLOR_ARGB(_a, _r, _g, _b)
|
|
|
|
|
2012-07-23 08:08:58 +04:00
|
|
|
namespace bgfx
|
|
|
|
{
|
2013-07-25 08:59:59 +04:00
|
|
|
typedef HRESULT (WINAPI * PFN_CREATEDXGIFACTORY)(REFIID _riid, void** _factory);
|
|
|
|
|
2012-07-23 08:08:58 +04:00
|
|
|
template <typename Ty>
|
|
|
|
class StateCacheT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void add(uint64_t _id, Ty* _item)
|
|
|
|
{
|
|
|
|
invalidate(_id);
|
|
|
|
m_hashMap.insert(stl::make_pair(_id, _item) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Ty* find(uint64_t _id)
|
|
|
|
{
|
|
|
|
HashMap::iterator it = m_hashMap.find(_id);
|
|
|
|
if (it != m_hashMap.end() )
|
|
|
|
{
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void invalidate(uint64_t _id)
|
|
|
|
{
|
|
|
|
HashMap::iterator it = m_hashMap.find(_id);
|
|
|
|
if (it != m_hashMap.end() )
|
|
|
|
{
|
2013-10-19 09:34:23 +04:00
|
|
|
DX_RELEASE_WARNONLY(it->second, 0);
|
2012-07-23 08:08:58 +04:00
|
|
|
m_hashMap.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void invalidate()
|
|
|
|
{
|
|
|
|
for (HashMap::iterator it = m_hashMap.begin(), itEnd = m_hashMap.end(); it != itEnd; ++it)
|
|
|
|
{
|
2014-06-25 07:29:43 +04:00
|
|
|
DX_CHECK_REFCOUNT(it->second, 1);
|
2013-10-19 09:34:23 +04:00
|
|
|
it->second->Release();
|
2012-07-23 08:08:58 +04:00
|
|
|
}
|
2013-10-19 09:34:23 +04:00
|
|
|
|
2012-07-23 08:08:58 +04:00
|
|
|
m_hashMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef stl::unordered_map<uint64_t, Ty*> HashMap;
|
|
|
|
HashMap m_hashMap;
|
|
|
|
};
|
|
|
|
|
2014-05-27 01:09:26 +04:00
|
|
|
struct IndexBufferD3D11
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
2014-05-27 01:09:26 +04:00
|
|
|
IndexBufferD3D11()
|
2012-07-23 08:08:58 +04:00
|
|
|
: m_ptr(NULL)
|
|
|
|
, m_dynamic(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void create(uint32_t _size, void* _data);
|
|
|
|
void update(uint32_t _offset, uint32_t _size, void* _data);
|
|
|
|
|
|
|
|
void destroy()
|
|
|
|
{
|
|
|
|
if (NULL != m_ptr)
|
|
|
|
{
|
|
|
|
DX_RELEASE(m_ptr, 0);
|
|
|
|
m_dynamic = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11Buffer* m_ptr;
|
|
|
|
uint32_t m_size;
|
|
|
|
bool m_dynamic;
|
|
|
|
};
|
|
|
|
|
2014-05-27 01:09:26 +04:00
|
|
|
struct VertexBufferD3D11
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
2014-05-27 01:09:26 +04:00
|
|
|
VertexBufferD3D11()
|
2012-07-23 08:08:58 +04:00
|
|
|
: m_ptr(NULL)
|
|
|
|
, m_dynamic(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void create(uint32_t _size, void* _data, VertexDeclHandle _declHandle);
|
|
|
|
void update(uint32_t _offset, uint32_t _size, void* _data);
|
|
|
|
|
|
|
|
void destroy()
|
|
|
|
{
|
|
|
|
if (NULL != m_ptr)
|
|
|
|
{
|
|
|
|
DX_RELEASE(m_ptr, 0);
|
|
|
|
m_dynamic = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11Buffer* m_ptr;
|
|
|
|
uint32_t m_size;
|
|
|
|
VertexDeclHandle m_decl;
|
|
|
|
bool m_dynamic;
|
|
|
|
};
|
|
|
|
|
2014-05-27 01:09:26 +04:00
|
|
|
struct ShaderD3D11
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
2014-05-27 01:09:26 +04:00
|
|
|
ShaderD3D11()
|
2012-07-23 08:08:58 +04:00
|
|
|
: m_ptr(NULL)
|
|
|
|
, m_code(NULL)
|
|
|
|
, m_buffer(NULL)
|
2013-09-21 09:13:58 +04:00
|
|
|
, m_constantBuffer(NULL)
|
2012-07-23 08:08:58 +04:00
|
|
|
, m_hash(0)
|
2013-09-21 09:13:58 +04:00
|
|
|
, m_numUniforms(0)
|
|
|
|
, m_numPredefined(0)
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-30 06:42:57 +04:00
|
|
|
void create(const Memory* _mem);
|
2012-07-23 08:08:58 +04:00
|
|
|
DWORD* getShaderCode(uint8_t _fragmentBit, const Memory* _mem);
|
|
|
|
|
|
|
|
void destroy()
|
|
|
|
{
|
2013-09-21 09:13:58 +04:00
|
|
|
if (NULL != m_constantBuffer)
|
|
|
|
{
|
|
|
|
ConstantBuffer::destroy(m_constantBuffer);
|
|
|
|
m_constantBuffer = NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-23 08:08:58 +04:00
|
|
|
m_numPredefined = 0;
|
|
|
|
|
|
|
|
if (NULL != m_buffer)
|
|
|
|
{
|
|
|
|
DX_RELEASE(m_buffer, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
DX_RELEASE(m_ptr, 0);
|
|
|
|
|
|
|
|
if (NULL != m_code)
|
|
|
|
{
|
|
|
|
release(m_code);
|
|
|
|
m_code = NULL;
|
|
|
|
m_hash = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 07:27:13 +04:00
|
|
|
union
|
|
|
|
{
|
|
|
|
ID3D11ComputeShader* m_computeShader;
|
|
|
|
ID3D11PixelShader* m_pixelShader;
|
|
|
|
ID3D11VertexShader* m_vertexShader;
|
|
|
|
IUnknown* m_ptr;
|
|
|
|
};
|
2012-07-23 08:08:58 +04:00
|
|
|
const Memory* m_code;
|
|
|
|
ID3D11Buffer* m_buffer;
|
|
|
|
ConstantBuffer* m_constantBuffer;
|
|
|
|
|
|
|
|
PredefinedUniform m_predefined[PredefinedUniform::Count];
|
2012-08-29 09:59:48 +04:00
|
|
|
uint8_t m_attrMask[Attrib::Count];
|
2012-07-23 08:08:58 +04:00
|
|
|
|
|
|
|
uint32_t m_hash;
|
|
|
|
|
|
|
|
uint16_t m_numUniforms;
|
|
|
|
uint8_t m_numPredefined;
|
|
|
|
};
|
|
|
|
|
2014-05-27 01:09:26 +04:00
|
|
|
struct ProgramD3D11
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
2014-05-27 01:09:26 +04:00
|
|
|
ProgramD3D11()
|
2012-07-23 08:08:58 +04:00
|
|
|
: m_vsh(NULL)
|
|
|
|
, m_fsh(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-21 07:27:13 +04:00
|
|
|
void create(const ShaderD3D11* _vsh, const ShaderD3D11* _fsh)
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
2014-07-21 07:27:13 +04:00
|
|
|
BX_CHECK(NULL != _vsh->m_ptr, "Vertex shader doesn't exist.");
|
|
|
|
m_vsh = _vsh;
|
|
|
|
memcpy(&m_predefined[0], _vsh->m_predefined, _vsh->m_numPredefined*sizeof(PredefinedUniform) );
|
|
|
|
m_numPredefined = _vsh->m_numPredefined;
|
|
|
|
|
|
|
|
if (NULL != _fsh)
|
|
|
|
{
|
|
|
|
BX_CHECK(NULL != _fsh->m_ptr, "Fragment shader doesn't exist.");
|
|
|
|
m_fsh = _fsh;
|
|
|
|
memcpy(&m_predefined[m_numPredefined], _fsh->m_predefined, _fsh->m_numPredefined*sizeof(PredefinedUniform) );
|
|
|
|
m_numPredefined += _fsh->m_numPredefined;
|
|
|
|
}
|
2012-07-23 08:08:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy()
|
|
|
|
{
|
|
|
|
m_numPredefined = 0;
|
|
|
|
m_vsh = NULL;
|
|
|
|
m_fsh = NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-27 01:09:26 +04:00
|
|
|
const ShaderD3D11* m_vsh;
|
|
|
|
const ShaderD3D11* m_fsh;
|
2012-07-23 08:08:58 +04:00
|
|
|
|
|
|
|
PredefinedUniform m_predefined[PredefinedUniform::Count*2];
|
|
|
|
uint8_t m_numPredefined;
|
|
|
|
};
|
|
|
|
|
2014-05-27 01:09:26 +04:00
|
|
|
struct TextureD3D11
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
|
|
|
enum Enum
|
|
|
|
{
|
|
|
|
Texture2D,
|
|
|
|
Texture3D,
|
|
|
|
TextureCube,
|
|
|
|
};
|
|
|
|
|
2014-05-27 01:09:26 +04:00
|
|
|
TextureD3D11()
|
2012-08-20 05:50:23 +04:00
|
|
|
: m_ptr(NULL)
|
|
|
|
, m_srv(NULL)
|
2014-07-21 07:27:13 +04:00
|
|
|
, m_uav(NULL)
|
2012-08-20 05:50:23 +04:00
|
|
|
, m_sampler(NULL)
|
|
|
|
, m_numMips(0)
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:34:53 +04:00
|
|
|
void create(const Memory* _mem, uint32_t _flags, uint8_t _skip);
|
2013-02-11 06:12:52 +04:00
|
|
|
void destroy();
|
2013-11-16 22:58:15 +04:00
|
|
|
void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem);
|
2013-07-25 08:59:59 +04:00
|
|
|
void commit(uint8_t _stage, uint32_t _flags = BGFX_SAMPLER_DEFAULT_FLAGS);
|
2014-02-06 11:07:11 +04:00
|
|
|
void resolve();
|
2012-07-23 08:08:58 +04:00
|
|
|
|
2012-08-20 05:50:23 +04:00
|
|
|
union
|
|
|
|
{
|
|
|
|
ID3D11Resource* m_ptr;
|
|
|
|
ID3D11Texture2D* m_texture2d;
|
2012-11-26 06:24:50 +04:00
|
|
|
ID3D11Texture3D* m_texture3d;
|
2012-08-20 05:50:23 +04:00
|
|
|
};
|
|
|
|
|
2012-08-13 08:02:11 +04:00
|
|
|
ID3D11ShaderResourceView* m_srv;
|
2014-07-21 07:27:13 +04:00
|
|
|
ID3D11UnorderedAccessView* m_uav;
|
2012-07-23 08:08:58 +04:00
|
|
|
ID3D11SamplerState* m_sampler;
|
2014-02-06 11:07:11 +04:00
|
|
|
uint32_t m_flags;
|
2013-09-09 08:03:03 +04:00
|
|
|
uint8_t m_type;
|
|
|
|
uint8_t m_requestedFormat;
|
|
|
|
uint8_t m_textureFormat;
|
2012-08-20 05:50:23 +04:00
|
|
|
uint8_t m_numMips;
|
2012-07-23 08:08:58 +04:00
|
|
|
};
|
|
|
|
|
2014-05-27 01:09:26 +04:00
|
|
|
struct FrameBufferD3D11
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
2014-05-27 01:09:26 +04:00
|
|
|
FrameBufferD3D11()
|
2014-02-06 11:07:11 +04:00
|
|
|
: m_num(0)
|
2012-07-23 08:08:58 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-06 11:07:11 +04:00
|
|
|
void create(uint8_t _num, const TextureHandle* _handles);
|
2012-08-06 01:51:49 +04:00
|
|
|
void destroy();
|
2014-02-06 11:07:11 +04:00
|
|
|
void resolve();
|
2014-03-26 10:07:51 +04:00
|
|
|
void clear(const Clear& _clear);
|
2012-08-06 01:51:49 +04:00
|
|
|
|
2014-02-06 11:07:11 +04:00
|
|
|
ID3D11RenderTargetView* m_rtv[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS-1];
|
|
|
|
ID3D11ShaderResourceView* m_srv[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS-1];
|
2012-11-26 06:24:50 +04:00
|
|
|
ID3D11DepthStencilView* m_dsv;
|
2014-02-06 11:07:11 +04:00
|
|
|
uint8_t m_num;
|
2012-07-23 08:08:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace bgfx
|
|
|
|
|
2013-11-14 09:54:36 +04:00
|
|
|
#endif // BGFX_RENDERER_D3D11_H_HEADER_GUARD
|