bgfx/src/renderer_d3d9.h

396 lines
9.1 KiB
C
Raw Normal View History

2012-04-03 23:30:07 -04:00
/*
* Copyright 2011-2012 Branimir Karadzic. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
#ifndef __RENDERER_D3D9_H__
#define __RENDERER_D3D9_H__
2012-07-08 22:10:07 -04:00
#define BGFX_CONFIG_RENDERER_DIRECT3D9EX (BX_PLATFORM_WINDOWS && 0)
2012-04-03 23:30:07 -04:00
2012-07-08 20:59:19 -04:00
#ifndef D3DSTREAMSOURCE_INDEXEDDATA
# define D3DSTREAMSOURCE_INDEXEDDATA (1<<30)
#endif// D3DSTREAMSOURCE_INDEXEDDATA
#ifndef D3DSTREAMSOURCE_INSTANCEDATA
# define D3DSTREAMSOURCE_INSTANCEDATA (2<<30)
#endif // D3DSTREAMSOURCE_INSTANCEDATA
2012-04-03 23:30:07 -04:00
#if BX_PLATFORM_WINDOWS
2012-07-08 22:10:07 -04:00
# if !BGFX_CONFIG_RENDERER_DIRECT3D9EX
2012-04-03 23:30:07 -04:00
# define D3D_DISABLE_9EX
2012-07-08 22:10:07 -04:00
# endif // !BGFX_CONFIG_RENDERER_DIRECT3D9EX
2012-04-03 23:30:07 -04:00
# include <d3d9.h>
2012-07-08 22:10:07 -04:00
# if BGFX_CONFIG_RENDERER_DIRECT3D9EX
2012-07-23 00:08:58 -04:00
typedef HRESULT (WINAPI *Direct3DCreate9ExFn)(UINT SDKVersion, IDirect3D9Ex**);
2012-04-03 23:30:07 -04:00
# else
2012-07-23 00:08:58 -04:00
typedef IDirect3D9* (WINAPI *Direct3DCreate9Fn)(UINT SDKVersion);
2012-07-08 22:10:07 -04:00
# endif // BGFX_CONFIG_RENDERER_DIRECT3D9EX
2012-04-03 23:30:07 -04:00
typedef int (WINAPI *D3DPERF_BeginEventFunc)(D3DCOLOR col, LPCWSTR wszName);
typedef int (WINAPI *D3DPERF_EndEventFunc)();
typedef void (WINAPI *D3DPERF_SetMarkerFunc)(D3DCOLOR col, LPCWSTR wszName);
typedef void (WINAPI *D3DPERF_SetRegionFunc)(D3DCOLOR col, LPCWSTR wszName);
typedef BOOL (WINAPI *D3DPERF_QueryRepeatFrameFunc)();
typedef void (WINAPI *D3DPERF_SetOptionsFunc)(DWORD dwOptions);
typedef DWORD (WINAPI *D3DPERF_GetStatusFunc)();
2012-08-05 17:51:49 -04:00
# define _PIX_SETMARKER(_col, _name) s_renderCtx.m_D3DPERF_SetMarker(_col, L#_name)
# define _PIX_BEGINEVENT(_col, _name) s_renderCtx.m_D3DPERF_BeginEvent(_col, L#_name)
2012-04-03 23:30:07 -04:00
# define _PIX_ENDEVENT() s_renderCtx.m_D3DPERF_EndEvent()
#elif BX_PLATFORM_XBOX360
# include <xgraphics.h>
# define D3DUSAGE_DYNAMIC 0 // not supported on X360
# define D3DLOCK_DISCARD 0 // not supported on X360
# define D3DERR_DEVICEHUNG D3DERR_DEVICELOST // not supported on X360
# define D3DERR_DEVICEREMOVED D3DERR_DEVICELOST // not supported on X360
# define D3DMULTISAMPLE_8_SAMPLES D3DMULTISAMPLE_4_SAMPLES
# define D3DMULTISAMPLE_16_SAMPLES D3DMULTISAMPLE_4_SAMPLES
2012-06-04 22:24:08 -04:00
# define D3DFMT_DF24 D3DFMT_D24FS8
2012-06-01 22:02:28 -04:00
# define _PIX_SETMARKER(_col, _name) do {} while(0)
# define _PIX_BEGINEVENT(_col, _name) do {} while(0)
# define _PIX_ENDEVENT() do {} while(0)
2012-04-03 23:30:07 -04:00
#endif // BX_PLATFORM_
2012-07-23 00:08:58 -04:00
#include "renderer_d3d.h"
2012-04-03 23:30:07 -04:00
namespace bgfx
{
#if BGFX_CONFIG_DEBUG_PIX
2012-04-03 23:30:07 -04:00
# define PIX_SETMARKER(_col, _name) _PIX_SETMARKER(_col, _name)
# define PIX_BEGINEVENT(_col, _name) _PIX_BEGINEVENT(_col, _name)
# define PIX_ENDEVENT() _PIX_ENDEVENT()
#else
# define PIX_SETMARKER(_col, _name)
# define PIX_BEGINEVENT(_col, _name)
# define PIX_ENDEVENT()
#endif // BGFX_CONFIG_DEBUG_PIX
2012-04-03 23:30:07 -04:00
2012-07-08 02:22:52 -04:00
# ifndef D3DFMT_ATI1
# define D3DFMT_ATI1 ( (D3DFORMAT)MAKEFOURCC('A','T','I','1') )
# endif // D3DFMT_ATI1
# ifndef D3DFMT_ATI2
# define D3DFMT_ATI2 ( (D3DFORMAT)MAKEFOURCC('A','T','I','2') )
# endif // D3DFMT_ATI2
# ifndef D3DFMT_ATOC
# define D3DFMT_ATOC ( (D3DFORMAT)MAKEFOURCC('A','T','O','C') )
# endif // D3DFMT_ATOC
# ifndef D3DFMT_DF16
# define D3DFMT_DF16 ( (D3DFORMAT)MAKEFOURCC('D','F','1','6') )
# endif // D3DFMT_DF16
# ifndef D3DFMT_DF24
# define D3DFMT_DF24 ( (D3DFORMAT)MAKEFOURCC('D','F','2','4') )
# endif // D3DFMT_DF24
# ifndef D3DFMT_INST
# define D3DFMT_INST ( (D3DFORMAT)MAKEFOURCC('I','N','S','T') )
# endif // D3DFMT_INST
# ifndef D3DFMT_INTZ
# define D3DFMT_INTZ ( (D3DFORMAT)MAKEFOURCC('I','N','T','Z') )
# endif // D3DFMT_INTZ
# ifndef D3DFMT_NULL
# define D3DFMT_NULL ( (D3DFORMAT)MAKEFOURCC('N','U','L','L') )
# endif // D3DFMT_NULL
# ifndef D3DFMT_RESZ
# define D3DFMT_RESZ ( (D3DFORMAT)MAKEFOURCC('R','E','S','Z') )
# endif // D3DFMT_RESZ
# ifndef D3DFMT_RAWZ
# define D3DFMT_RAWZ ( (D3DFORMAT)MAKEFOURCC('R','A','W','Z') )
# endif // D3DFMT_RAWZ
struct ExtendedFormat
{
enum Enum
{
Ati1,
Ati2,
Df16,
Df24,
Inst,
Intz,
Null,
Resz,
Rawz,
Count,
};
D3DFORMAT m_fmt;
DWORD m_usage;
D3DRESOURCETYPE m_type;
bool m_supported;
};
2012-06-01 22:02:28 -04:00
struct Msaa
{
D3DMULTISAMPLE_TYPE m_type;
DWORD m_quality;
};
2012-04-03 23:30:07 -04:00
struct IndexBuffer
{
IndexBuffer()
: m_ptr(NULL)
, m_dynamic(false)
{
}
void create(uint32_t _size, void* _data);
2012-06-23 14:44:22 -04:00
void update(uint32_t _offset, uint32_t _size, void* _data)
2012-04-03 23:30:07 -04:00
{
void* buffer;
2012-06-23 14:44:22 -04:00
DX_CHECK(m_ptr->Lock(_offset
2012-04-03 23:30:07 -04:00
, _size
, &buffer
2012-06-23 14:44:22 -04:00
, m_dynamic && 0 == _offset && m_size == _size ? D3DLOCK_DISCARD : 0
2012-04-03 23:30:07 -04:00
) );
memcpy(buffer, _data, _size);
m_ptr->Unlock();
}
void destroy()
{
if (NULL != m_ptr)
{
DX_RELEASE(m_ptr, 0);
m_dynamic = false;
}
}
void preReset();
void postReset();
IDirect3DIndexBuffer9* m_ptr;
uint32_t m_size;
bool m_dynamic;
};
struct VertexBuffer
{
VertexBuffer()
: m_ptr(NULL)
, m_dynamic(false)
{
}
void create(uint32_t _size, void* _data, VertexDeclHandle _declHandle);
2012-06-23 14:44:22 -04:00
void update(uint32_t _offset, uint32_t _size, void* _data)
2012-04-03 23:30:07 -04:00
{
void* buffer;
2012-06-23 14:44:22 -04:00
DX_CHECK(m_ptr->Lock(_offset
2012-04-03 23:30:07 -04:00
, _size
, &buffer
2012-06-23 14:44:22 -04:00
, m_dynamic && 0 == _offset && m_size == _size ? D3DLOCK_DISCARD : 0
2012-04-03 23:30:07 -04:00
) );
memcpy(buffer, _data, _size);
m_ptr->Unlock();
}
void destroy()
{
if (NULL != m_ptr)
{
DX_RELEASE(m_ptr, 0);
m_dynamic = false;
}
}
void preReset();
void postReset();
IDirect3DVertexBuffer9* m_ptr;
uint32_t m_size;
VertexDeclHandle m_decl;
bool m_dynamic;
};
struct VertexDeclaration
{
void create(const VertexDecl& _decl);
void destroy()
{
DX_RELEASE(m_ptr, 0);
}
IDirect3DVertexDeclaration9* m_ptr;
VertexDecl m_decl;
};
struct Shader
{
Shader()
: m_ptr(NULL)
{
}
void create(bool _fragment, const Memory* _mem);
DWORD* getShaderCode(uint8_t _fragmentBit, const Memory* _mem);
void destroy()
{
ConstantBuffer::destroy(m_constantBuffer);
m_constantBuffer = NULL;
m_numPredefined = 0;
DX_RELEASE(m_ptr, 0);
}
IUnknown* m_ptr;
ConstantBuffer* m_constantBuffer;
PredefinedUniform m_predefined[PredefinedUniform::Count];
uint8_t m_numPredefined;
};
2012-09-16 20:36:08 -04:00
struct Program
2012-04-03 23:30:07 -04:00
{
void create(const Shader& _vsh, const Shader& _fsh)
{
BX_CHECK(NULL != _vsh.m_ptr, "Vertex shader doesn't exist.");
m_vsh = &_vsh;
BX_CHECK(NULL != _fsh.m_ptr, "Fragment shader doesn't exist.");
m_fsh = &_fsh;
memcpy(&m_predefined[0], _vsh.m_predefined, _vsh.m_numPredefined*sizeof(PredefinedUniform) );
memcpy(&m_predefined[_vsh.m_numPredefined], _fsh.m_predefined, _fsh.m_numPredefined*sizeof(PredefinedUniform) );
m_numPredefined = _vsh.m_numPredefined + _fsh.m_numPredefined;
}
void destroy()
{
m_numPredefined = 0;
m_vsh = NULL;
m_fsh = NULL;
}
const Shader* m_vsh;
const Shader* m_fsh;
PredefinedUniform m_predefined[PredefinedUniform::Count*2];
uint8_t m_numPredefined;
};
struct Texture
{
2012-06-09 21:25:50 -04:00
enum Enum
{
Texture2D,
Texture3D,
TextureCube,
};
2012-04-03 23:30:07 -04:00
Texture()
: m_ptr(NULL)
{
}
2012-06-09 21:25:50 -04:00
void createTexture(uint32_t _width, uint32_t _height, uint8_t _numMips, D3DFORMAT _fmt);
void createVolumeTexture(uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _numMips, D3DFORMAT _fmt);
void createCubeTexture(uint32_t _edge, uint32_t _numMips, D3DFORMAT _fmt);
2012-08-13 00:02:11 -04:00
uint8_t* lock(uint8_t _side, uint8_t _lod, uint32_t& _pitch, uint32_t& _slicePitch, const Rect* _rect = NULL);
2012-06-09 21:25:50 -04:00
void unlock(uint8_t _side, uint8_t _lod);
2012-04-03 23:30:07 -04:00
void create(const Memory* _mem, uint32_t _flags);
void destroy()
{
DX_RELEASE(m_ptr, 0);
}
2012-08-19 21:50:23 -04:00
void update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, const Memory* _mem);
2012-04-03 23:30:07 -04:00
void commit(uint8_t _stage);
2012-08-13 00:02:11 -04:00
union
{
IDirect3DBaseTexture9* m_ptr;
IDirect3DTexture9* m_texture2d;
IDirect3DVolumeTexture9* m_texture3d;
IDirect3DCubeTexture9* m_textureCube;
};
2012-04-03 23:30:07 -04:00
D3DTEXTUREFILTERTYPE m_minFilter;
D3DTEXTUREFILTERTYPE m_magFilter;
D3DTEXTUREFILTERTYPE m_mipFilter;
D3DTEXTUREADDRESS m_tau;
D3DTEXTUREADDRESS m_tav;
2012-06-09 21:25:50 -04:00
D3DTEXTUREADDRESS m_taw;
Enum m_type;
2012-06-01 22:02:28 -04:00
bool m_srgb;
2012-04-03 23:30:07 -04:00
};
struct RenderTarget
{
RenderTarget()
2012-06-01 22:02:28 -04:00
: m_rt(NULL)
, m_colorTexture(NULL)
2012-04-03 23:30:07 -04:00
, m_color(NULL)
, m_depthTexture(NULL)
2012-04-03 23:30:07 -04:00
, m_depth(NULL)
2012-06-22 22:44:30 -04:00
, m_minFilter(D3DTEXF_LINEAR)
, m_magFilter(D3DTEXF_LINEAR)
2012-04-03 23:30:07 -04:00
, m_width(0)
, m_height(0)
, m_flags(0)
, m_depthOnly(false)
2012-04-03 23:30:07 -04:00
{
}
2012-06-22 22:44:30 -04:00
void create(uint16_t _width, uint16_t _height, uint32_t _flags, uint32_t _textureFlags);
2012-04-03 23:30:07 -04:00
void createTextures();
void destroyTextures();
void destroy()
{
destroyTextures();
m_flags = 0;
}
void preReset()
{
destroyTextures();
}
void postReset()
{
createTextures();
}
void commit(uint8_t _stage);
2012-06-01 22:02:28 -04:00
void resolve();
2012-04-03 23:30:07 -04:00
2012-06-01 22:02:28 -04:00
Msaa m_msaa;
IDirect3DSurface9* m_rt;
2012-04-03 23:30:07 -04:00
IDirect3DTexture9* m_colorTexture;
IDirect3DSurface9* m_color;
IDirect3DTexture9* m_depthTexture;
2012-04-03 23:30:07 -04:00
IDirect3DSurface9* m_depth;
2012-06-22 22:44:30 -04:00
D3DTEXTUREFILTERTYPE m_minFilter;
D3DTEXTUREFILTERTYPE m_magFilter;
2012-04-03 23:30:07 -04:00
uint16_t m_width;
uint16_t m_height;
uint32_t m_flags;
bool m_depthOnly;
2012-04-03 23:30:07 -04:00
};
} // namespace bgfx
#endif // __RENDERER_D3D9_H__