8using Microsoft::WRL::ComPtr;
28 default:
return DXGI_FORMAT_UNKNOWN;
41 std::wstring Wide(
const char* value)
44 const int count=MultiByteToWideChar(CP_ACP,0,value,-1,
nullptr,0);
if(!count)
return {};
45 std::wstring result(
size_t(count),L
'\0');MultiByteToWideChar(CP_ACP,0,value,-1,result.data(),count);
46 result.pop_back();
return result;
48 UINT PixelBytes(DXGI_FORMAT format)
52 case DXGI_FORMAT_B8G8R8A8_UNORM:
case DXGI_FORMAT_B8G8R8X8_UNORM:
case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_R32_FLOAT:
return 4;
53 case DXGI_FORMAT_R8_UNORM:
return 1;
54 case DXGI_FORMAT_R8G8_UNORM:
case DXGI_FORMAT_R16_FLOAT:
return 2;
55 case DXGI_FORMAT_R16G16B16A16_FLOAT:
return 8;
56 case DXGI_FORMAT_R32G32B32A32_FLOAT:
return 16;
63 D3D11_SHADER_RESOURCE_VIEW_DESC description{};texture.
View()->GetDesc(&description);
65 switch(description.ViewDimension)
67 case D3D11_SRV_DIMENSION_TEXTURE2D:count=&description.Texture2D.MipLevels;
break;
68 case D3D11_SRV_DIMENSION_TEXTURE3D:count=&description.Texture3D.MipLevels;
break;
69 case D3D11_SRV_DIMENSION_TEXTURECUBE:count=&description.TextureCube.MipLevels;
break;
70 default:
return E_INVALIDARG;
72 if(requestedLevels && requestedLevels!=
DefaultSize)*count=(std::min)(*count,requestedLevels);
73 ComPtr<ID3D11ShaderResourceView> next;
74 const HRESULT hr=device.
Native()->CreateShaderResourceView(texture.
Resource(),&description,&next);
if(FAILED(hr))
return hr;
79 if(!path)
return E_INVALIDARG;
80 std::ifstream file(std::filesystem::path(path),std::ios::binary|std::ios::ate);
if(!file)
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
81 const auto length=file.tellg();
if(length<=0 || uint64_t(length)>UINT_MAX)
return E_INVALIDARG;
84 std::vector<uint8_t> bytes(
static_cast<size_t>(length));file.seekg(0);
85 if(!file.read(
reinterpret_cast<char*
>(bytes.data()),
static_cast<std::streamsize
>(bytes.size())))
return HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
86 return Load(device,bytes.data(),bytes.size(),requestedLevels,width,height);
90 std::vector<uint8_t> generated;
92 if(FAILED(hr))
return hr;
93 if(!generated.empty()){data=generated.data();bytes=generated.size();}
95 return FAILED(hr) ? hr :
Adopt(device,loaded,requestedLevels);
99 if(
locked || !width || !height || width>D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION || height>D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION)
return E_INVALIDARG;
100 D3D11_TEXTURE2D_DESC desc{};desc.Width=width;desc.Height=height;desc.MipLevels=requestedLevels==
DefaultSize ? 0 : requestedLevels;
101 desc.ArraySize=1;desc.Format=
NativeFormat(format);desc.SampleDesc.Count=1;desc.Usage=D3D11_USAGE_DEFAULT;
103 ComPtr<ID3D11Texture2D> next;
HRESULT hr=device.
Native()->CreateTexture2D(&desc,
nullptr,&next);
if(FAILED(hr))
return hr;
104 ComPtr<ID3D11ShaderResourceView> nextView;
if(FAILED(hr=device.
Native()->CreateShaderResourceView(next.Get(),
nullptr,&nextView)))
return hr;
109 if(!result)
return E_POINTER;
if(!
resource || level>=
levels)
return E_INVALIDARG;
110 ComPtr<ID3D11Texture2D> image;
HRESULT hr=
resource.As(&image);
if(FAILED(hr))
return hr;
111 D3D11_TEXTURE2D_DESC desc{};image->GetDesc(&desc);
112 *result={
ClientFormat(desc.Format),(std::max)(1u,desc.Width>>level),(std::max)(1u,desc.Height>>level),desc.SampleDesc.Count,desc.SampleDesc.Quality};
return S_OK;
116 if(!result)
return E_POINTER;*result=
nullptr;
if(!
owner || level>=
levels)
return E_INVALIDARG;
117 ComPtr<ID3D11Texture2D> image;
HRESULT hr=
resource.As(&image);
if(FAILED(hr))
return hr;
118 auto surface=std::make_unique<RenderSurface>();
if(FAILED(hr=surface->Initialize(*
owner,image.Get(),level)))
return hr;
119 *result=surface.release();
return S_OK;
129 if(!result)
return E_POINTER;*result={};
if(!
owner ||
locked || level>=
levels)
return E_INVALIDARG;
130 ComPtr<ID3D11Texture2D> image;
HRESULT hr=
resource.As(&image);
if(FAILED(hr))
return hr;
131 D3D11_TEXTURE2D_DESC desc{};image->GetDesc(&desc);
const UINT bytes=PixelBytes(desc.Format);
132 if(!bytes || desc.SampleDesc.Count!=1)
return E_INVALIDARG;
133 desc.Width=(std::max)(1u,desc.Width>>level);desc.Height=(std::max)(1u,desc.Height>>level);
134 const RECT rectangle=region ? *region : RECT{0,0,LONG(desc.Width),LONG(desc.Height)};
135 if(rectangle.left<0 || rectangle.top<0 || rectangle.right>LONG(desc.Width) || rectangle.bottom>LONG(desc.Height)
136 || rectangle.right<=rectangle.left || rectangle.bottom<=rectangle.top)
return E_INVALIDARG;
137 desc.MipLevels=1;desc.ArraySize=1;desc.Usage=D3D11_USAGE_STAGING;desc.BindFlags=desc.MiscFlags=0;
138 desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ|D3D11_CPU_ACCESS_WRITE;
146 result->
pBits=
static_cast<uint8_t*
>(mapped.pData)+
size_t(rectangle.top)*mapped.RowPitch+size_t(rectangle.left)*bytes;
return S_OK;
157 if(!image)
return E_INVALIDARG;D3D11_TEXTURE2D_DESC desc{};image->GetDesc(&desc);
if(mip>=desc.MipLevels)
return E_INVALIDARG;
158 ComPtr<ID3D11RenderTargetView> next;
159 if(desc.BindFlags&D3D11_BIND_RENDER_TARGET)
161 D3D11_RENDER_TARGET_VIEW_DESC rt{};rt.Format=desc.Format;
162 rt.ViewDimension=desc.SampleDesc.Count>1 ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;rt.Texture2D.MipSlice=mip;
163 const HRESULT hr=owner.
Native()->CreateRenderTargetView(image,&rt,&next);
if(FAILED(hr))
return hr;
169 if(!result)
return E_POINTER;
if(!
texture)
return E_UNEXPECTED;D3D11_TEXTURE2D_DESC desc{};
texture->GetDesc(&desc);
170 *result={
ClientFormat(desc.Format),(std::max)(1u,desc.Width>>
level),(std::max)(1u,desc.Height>>
level),desc.SampleDesc.Count,desc.SampleDesc.Quality};
return S_OK;
176 D3D11_QUERY_DESC desc{D3D11_QUERY_OCCLUSION,0};
owner=&device;
return device.
Native()->CreateQuery(&desc,&
query);
185 if(!
owner || !
query)
return E_UNEXPECTED;
if(!result || bytes!=
sizeof(
DWORD))
return E_INVALIDARG;
186 UINT64 samples=0;
const HRESULT hr=
owner->
Context()->GetData(
query.Get(),&samples,
sizeof(samples),flags ? 0 : D3D11_ASYNC_GETDATA_DONOTFLUSH);
187 if(hr==S_OK)*
static_cast<DWORD*
>(result)=
DWORD((std::min)(samples,UINT64(MAXDWORD)));
return hr;
196 if(!strcmp(name,
"ColorMap3"))
198 D3D11_SHADER_RESOURCE_VIEW_DESC description{};
if(view)view->GetDesc(&description);
199 const bool volume=description.ViewDimension==D3D11_SRV_DIMENSION_TEXTURE3D;
200 parameters.Texture(name,volume ?
nullptr : view);parameters.Texture(std::string(name)+
"Volume",volume ? view :
nullptr);
202 else parameters.Texture(name,view);
return S_OK;
206 if(
active)
return E_UNEXPECTED;
if(count)*count=15;
215 if(
active)
return E_UNEXPECTED;
232 if(!
active)
return E_UNEXPECTED;
234 return FAILED(flush) ? flush : end;
238 if(sprite || (flags&DT_CALCRECT))
return font.
Draw(sprite ? &sprite->
Native() :
nullptr,text,count,rectangle,flags,color);
241 const int result=
font.
Draw(&
automatic->Native(),text,count,rectangle,flags,color);
return FAILED(
automatic->End()) ? 0 : result;
246 :
DrawTextW(sprite,text,count,rectangle,flags,color);
250 if(!text || count==0)
return 0;
251 const int length=count<0 ?
int(strlen(text)) : count;
252 const int size=MultiByteToWideChar(CP_ACP,0,text,length,
nullptr,0);
if(!size)
return 0;
253 std::wstring converted(size,L
'\0');MultiByteToWideChar(CP_ACP,0,text,length,converted.data(),size);
254 return DrawTextW(sprite,converted.data(),size,rectangle,flags,color);
265 const D3D11_VIEWPORT viewport=requested ? D3D11_VIEWPORT{float(requested->
X),float(requested->
Y),float(requested->
Width),float(requested->
Height),requested->
MinZ,requested->
MaxZ}
266 : D3D11_VIEWPORT{0,0,float(
width),float(
height),0,1};
274 saved.reset();
return FAILED(end) ? end : restore;
278 if(!owner || !result)
return E_INVALIDARG;*result=
nullptr;
auto value=std::make_unique<RenderSprite>(*owner);
279 const HRESULT hr=value->Initialize();
if(SUCCEEDED(hr))*result=value.release();
return hr;
284 if(!owner || !result || !face)
return E_INVALIDARG;*result=
nullptr;
285 LOGFONTW description{};description.lfHeight=height;description.lfWidth=LONG(width);description.lfWeight=LONG(weight);
286 description.lfItalic=BYTE(italic!=FALSE);description.lfCharSet=BYTE(charset);description.lfOutPrecision=BYTE(precision);
287 description.lfQuality=BYTE(quality);description.lfPitchAndFamily=BYTE(pitch);wcsncpy_s(description.lfFaceName,face,_TRUNCATE);
288 auto value=std::make_unique<RenderFont>(*owner);
const HRESULT hr=value->Initialize(description);
if(SUCCEEDED(hr))*result=value.release();
return hr;
292 {
return CreateFontW(owner,height,width,weight,mip,italic,charset,precision,quality,pitch,Wide(face).c_str(),result);}
295 if(!owner || !result)
return E_INVALIDARG;*result=
nullptr;
auto value=std::make_unique<RenderTexture>();
296 const HRESULT hr=value->Create(*owner,width,height,levels,usage,format);
if(SUCCEEDED(hr))*result=value.release();
return hr;
300 if(!owner || !result)
return E_INVALIDARG;*result=
nullptr;
auto value=std::make_unique<RenderToTexture>(*owner);
301 const HRESULT hr=value->Initialize(width,height,depth!=FALSE);
if(SUCCEEDED(hr))*result=value.release();
return hr;
306 if(!owner || !result)
return E_INVALIDARG;*result=
nullptr;
auto value=std::make_unique<RenderTexture>();
307 const HRESULT hr=value->Load(*owner,path,levels,width,height);
if(SUCCEEDED(hr))*result=value.release();
return hr;
309 catch(
const std::bad_alloc&){
return E_OUTOFMEMORY;}
311 try {
if(result)*result=
nullptr;
return LoadTexture(owner,Wide(path).c_str(),result,levels);}
312 catch(
const std::bad_alloc&){
return E_OUTOFMEMORY;}
316 if(!owner || !result)
return E_INVALIDARG;*result=
nullptr;
auto value=std::make_unique<RenderTexture>();
317 const HRESULT hr=value->Load(*owner,data,bytes,levels);
if(SUCCEEDED(hr))*result=value.release();
return hr;
319 catch(
const std::bad_alloc&){
return E_OUTOFMEMORY;}
SCRIPT_CONTROL SCRIPT_STATE const void int
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void SCRIPT_CONTROL SCRIPT_STATE const int SCRIPT_TABDEF const BYTE SCRIPT_STRING_ANALYSIS BOOL
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
void UnbindShaderResources()
HRESULT ApplyPipeline(bool fixedPoints=false)
Materials11 & Materials()
HRESULT Restore(const Saved &saved)
HRESULT Target(ID3D11RenderTargetView *color, ID3D11DepthStencilView *depth, const D3D11_VIEWPORT &viewport)
int DrawFixed(Sprite11 *sprite, const wchar_t *text, int count, RECT *rectangle, UINT format, uint32_t color)
int Draw(Sprite11 *sprite, const wchar_t *text, int count, RECT *rectangle, UINT format, uint32_t color)
HRESULT Create(ID3D11Device *device, UINT bytes, bool dynamic, bool index=false, bool indices32=false)
void Set(const std::string &name, const void *data, size_t bytes)
HRESULT Create(RenderDevice &owner, UINT bytes, DWORD usage, bool index=false, bool indices32=false)
ID3D11DeviceContext * Context()
HRESULT GetScissorRect(RECT *value) const
RenderSprite * activeSprite
std::unique_ptr< RenderSprite > automatic
INT DrawTextW(RenderSprite *sprite, const wchar_t *text, INT count, RECT *rectangle, DWORD flags, PackedColor color)
INT DrawTextA(RenderSprite *sprite, const char *text, INT count, RECT *rectangle, DWORD flags, PackedColor color)
INT DrawFixedTextW(RenderSprite *sprite, const wchar_t *text, INT count, RECT *rectangle, DWORD flags, PackedColor color)
HRESULT SetValue(const char *name, const void *value, UINT bytes)
HRESULT Begin(UINT *count, DWORD flags)
HRESULT SetTexture(const char *name, RenderTexture *value)
HRESULT Initialize(RenderDevice &owner)
Microsoft::WRL::ComPtr< ID3D11Query > query
HRESULT Issue(DWORD flags)
HRESULT GetData(void *destination, DWORD bytes, DWORD flags)
std::array< DWORD, 3 > filters
HRESULT Begin(DWORD flags)
RenderSprite * previousSprite
Microsoft::WRL::ComPtr< ID3D11Texture2D > texture
ID3D11RenderTargetView * Target() const
HRESULT GetDesc(SurfaceDescription *result) const
HRESULT Initialize(RenderDevice &owner, ID3D11Texture2D *texture, UINT level)
Microsoft::WRL::ComPtr< ID3D11RenderTargetView > target
HRESULT Load(RenderDevice &owner, const wchar_t *path, UINT levels=0, UINT width=0, UINT height=0)
~RenderTexture() override
HRESULT GetSurfaceLevel(UINT level, RenderSurface **result)
Microsoft::WRL::ComPtr< ID3D11DeviceContext > mappedContext
Microsoft::WRL::ComPtr< ID3D11ShaderResourceView > view
Microsoft::WRL::ComPtr< ID3D11Texture2D > staging
Microsoft::WRL::ComPtr< ID3D11Resource > resource
HRESULT GetLevelDesc(UINT level, SurfaceDescription *result) const
ID3D11ShaderResourceView * View() const
HRESULT Adopt(RenderDevice &owner, Texture11 &texture, UINT requestedLevels)
HRESULT Create(RenderDevice &owner, UINT width, UINT height, UINT levels, DWORD usage, ImageFormat format)
HRESULT LockRect(UINT level, LockedRectangle *result, const RECT *region, DWORD flags)
HRESULT UnlockRect(UINT level)
HRESULT EndScene(DWORD filter)
std::unique_ptr< DrawContext11::Saved > saved
HRESULT BeginScene(RenderSurface *surface, const Viewport *viewport)
HRESULT Initialize(UINT width, UINT height, bool hasDepth)
HRESULT Create(ID3D11Device *device, UINT width, UINT height, DXGI_SAMPLE_DESC samples)
ID3D11DepthStencilView * WriteView() const
HRESULT SetScissor(bool enabled, const RECT &rectangle)
HRESULT Begin(ID3D11DeviceContext *context, const SpriteOptions11 &options={})
ID3D11ShaderResourceView * View() const
ID3D11Resource * Resource() const
HRESULT Load(ID3D11Device *device, ID3D11DeviceContext *context, const wchar_t *path)
constexpr uint32_t DYNAMIC
constexpr uint32_t RENDERTARGET
constexpr uint32_t LINEAR
constexpr uint32_t ANISOTROPIC
constexpr uint32_t READONLY
constexpr uint32_t DISCARD
constexpr DWORD AlphaBlend
constexpr DWORD SortTexture
constexpr DWORD SortBackToFront
HRESULT LoadTextureMemory(RenderDevice *owner, const void *data, UINT bytes, RenderTexture **result, UINT levels=0)
ImageFormat ClientFormat(DXGI_FORMAT format)
HRESULT CreateRenderToSurface(RenderDevice *owner, UINT width, UINT height, ImageFormat format, BOOL depth, ImageFormat depthFormat, RenderToTexture **result)
HRESULT CreateFontW(RenderDevice *owner, INT height, UINT width, UINT weight, UINT mipLevels, BOOL italic, DWORD charset, DWORD precision, DWORD quality, DWORD pitch, const wchar_t *face, RenderFont **result)
HRESULT PrepareTextureDDS(const uint8_t *data, size_t size, UINT requested, UINT requestedWidth, UINT requestedHeight, std::vector< uint8_t > &output)
DXGI_FORMAT NativeFormat(ImageFormat format)
HRESULT CreateSprite(RenderDevice *owner, RenderSprite **result)
constexpr unsigned DefaultSize
HRESULT LoadTexture(RenderDevice *owner, const wchar_t *path, RenderTexture **result, UINT levels=0, UINT width=0, UINT height=0)
HRESULT CreateFontA(RenderDevice *owner, INT height, UINT width, UINT weight, UINT mipLevels, BOOL italic, DWORD charset, DWORD precision, DWORD quality, DWORD pitch, const char *face, RenderFont **result)
HRESULT CreateTexture(RenderDevice *owner, UINT width, UINT height, UINT levels, DWORD usage, ImageFormat format, ResourcePool pool, RenderTexture **result)
D3D11_RASTERIZER_DESC raster