Rise
The Vieneo Province
Loading...
Searching...
No Matches
ClientRenderer11.h
Go to the documentation of this file.
1#pragma once
2#include "ClientTypes11.h"
3#include "DrawContext11.h"
4#include "Texture11.h"
5#include "Font11.h"
6#include <atomic>
7#include <memory>
8
9namespace Rendering
10{
11 // Intrusive ownership keeps the existing game resource lifetimes explicit.
12 // These are engine objects; their only graphics resources are native D3D11.
14 {
15 public:
16 ULONG AddRef(){return ++references;}
17 ULONG Release(){const ULONG left=--references;if(!left)delete this;return left;}
18 virtual ~RenderResource()=default;
19 private:
20 std::atomic<ULONG> references{1};
21 };
22 class RenderDevice;
23 class ImageBuffer final : public RenderResource
24 {
25 public:
26 std::vector<uint8_t> bytes;
27 void* GetBufferPointer(){return bytes.data();}
28 DWORD GetBufferSize()const{return DWORD(bytes.size());}
29 };
30 class RenderSurface;
31 class RenderTexture final : public RenderResource
32 {
33 public:
34 ~RenderTexture() override;
35 HRESULT Load(RenderDevice& owner,const wchar_t* path,UINT levels=0,UINT width=0,UINT height=0);
36 HRESULT Load(RenderDevice& owner,const void* bytes,size_t size,UINT levels=0,UINT width=0,UINT height=0);
37 HRESULT Create(RenderDevice& owner,UINT width,UINT height,UINT levels,DWORD usage,ImageFormat format);
38 HRESULT GetLevelDesc(UINT level,SurfaceDescription* result) const;
39 HRESULT GetSurfaceLevel(UINT level,RenderSurface** result);
40 HRESULT LockRect(UINT level,LockedRectangle* result,const RECT* region,DWORD flags);
41 HRESULT UnlockRect(UINT level);
42 UINT GetLevelCount() const {return levels;}
43 ID3D11ShaderResourceView* View() const {return view.Get();}
44 ID3D11Resource* Native() const {return resource.Get();}
45 private:
46 HRESULT Adopt(RenderDevice& owner,Texture11& texture,UINT requestedLevels);
48 Microsoft::WRL::ComPtr<ID3D11Resource> resource;
49 Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> view;
50 Microsoft::WRL::ComPtr<ID3D11Texture2D> staging;
51 Microsoft::WRL::ComPtr<ID3D11DeviceContext> mappedContext;
53 bool locked=false,readOnly=false;
54 };
55 class RenderSurface final : public RenderResource
56 {
57 public:
58 HRESULT Initialize(RenderDevice& owner,ID3D11Texture2D* texture,UINT level);
59 HRESULT GetDesc(SurfaceDescription* result) const;
60 ID3D11Texture2D* Native() const {return texture.Get();}
61 ID3D11RenderTargetView* Target() const {return target.Get();}
62 UINT Level() const {return level;}
63 private:
64 Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
65 Microsoft::WRL::ComPtr<ID3D11RenderTargetView> target;
66 UINT level=0;
67 };
68 class RenderBuffer final : public RenderResource
69 {
70 public:
71 HRESULT Create(RenderDevice& owner,UINT bytes,DWORD usage,bool index=false,bool indices32=false);
72 HRESULT Lock(UINT offset,UINT bytes,void** output,DWORD flags)
73 {return geometry.Lock(offset,bytes,output,(flags&LockFlags::READONLY)!=0,(flags&LockFlags::DISCARD)!=0);}
76 private:
78 };
79 class RenderQuery final : public RenderResource
80 {
81 public:
83 HRESULT Issue(DWORD flags);
84 HRESULT GetData(void* destination,DWORD bytes,DWORD flags);
85 private:
87 Microsoft::WRL::ComPtr<ID3D11Query> query;
88 };
90 class RenderSprite;
92 {
93 public:
95 void Reset();
97 Device11& Owner(){return *draws.Owner();}
98 ID3D11Device* Native(){return Owner().Device();}
99 ID3D11DeviceContext* Context(){return Owner().Context();}
100 HRESULT CreateVertexBuffer(UINT bytes,DWORD usage,DWORD format,ResourcePool pool,RenderBuffer** result,void* shared);
101 HRESULT CreateIndexBuffer(UINT bytes,DWORD usage,ImageFormat format,ResourcePool pool,RenderBuffer** result,void* shared);
102 HRESULT CreateQuery(UINT type,RenderQuery** result);
103 HRESULT CreateStateBlock(UINT type,RenderStateSnapshot** result);
104 HRESULT SetStreamSource(UINT stream,RenderBuffer* buffer,UINT offset,UINT stride);
105 HRESULT SetIndices(RenderBuffer* buffer){indices=buffer;return S_OK;}
106 HRESULT SetFVF(DWORD value);
107 HRESULT GetFVF(DWORD* value) const {if(!value)return E_POINTER;*value=vertexFormat;return S_OK;}
108 HRESULT SetRenderState(DWORD state,DWORD value);
109 HRESULT GetRenderState(DWORD state,DWORD* value) const;
110 HRESULT SetSamplerState(DWORD sampler,DWORD state,DWORD value);
111 HRESULT SetTextureStageState(DWORD stage,DWORD state,DWORD value);
112 HRESULT SetTexture(DWORD stage,RenderTexture* texture);
113 HRESULT SetTransform(DWORD slot,const RiseMath::Matrix* value);
114 HRESULT GetTransform(DWORD slot,RiseMath::Matrix* value) const;
115 HRESULT SetLight(DWORD index,const Light* value);
117 HRESULT SetMaterial(const Material* value);
118 HRESULT SetScissorRect(const RECT* value){if(!value)return E_POINTER;draws.Scissor(*value);return S_OK;}
119 HRESULT GetScissorRect(RECT* value) const {if(!value)return E_POINTER;*value=draws.Scissor();return S_OK;}
120 HRESULT SetViewport(const Viewport* value);
121 HRESULT GetViewport(Viewport* value) const;
122 HRESULT SetClipPlane(DWORD index,const float* value);
123 HRESULT SetVertexShader(std::nullptr_t){draws.Material(-1);return S_OK;}
124 HRESULT SetPixelShader(std::nullptr_t){draws.Material(-1);return S_OK;}
125 HRESULT DrawPrimitive(Primitive type,UINT first,UINT count);
126 HRESULT DrawIndexedPrimitive(Primitive type,INT baseVertex,UINT minimum,UINT vertices,UINT startIndex,UINT count);
127 HRESULT DrawPrimitiveUP(Primitive type,UINT count,const void* vertices,UINT stride);
128 HRESULT Clear(DWORD count,const RECT* rectangles,DWORD flags,PackedColor color,float depth,DWORD stencil);
129 HRESULT BeginScene(){if(inScene)return E_UNEXPECTED;inScene=true;return S_OK;}
130 HRESULT EndScene(){if(!inScene)return E_UNEXPECTED;inScene=false;return S_OK;}
131 HRESULT GetBackBuffer(UINT chain,UINT buffer,UINT type,RenderSurface** result);
132 UINT GetAvailableTextureMem() const;
133 bool InScene() const {return inScene;}
134 struct Saved
135 {
137 std::map<DWORD,DWORD> states;
138 Microsoft::WRL::ComPtr<RenderBuffer> vertices,indices;
140 UINT offset=0,stride=0;
141 std::array<Light,8> lights;
142 std::array<bool,8> enabled{};
143 std::array<std::array<DWORD,3>,8> filters{};
144 };
145 Saved Capture() const;
146 HRESULT Restore(const Saved& value);
147 private:
148 friend class RenderSprite;
150 HRESULT Layout(VertexLayout11& result,UINT stride) const;
151 void UpdateBlend();
153 std::map<DWORD,DWORD> states;
154 Microsoft::WRL::ComPtr<RenderBuffer> vertices,indices;
156 UINT offset=0,stride=0;
157 std::array<Light,8> lights;
158 std::array<bool,8> enabled{};
159 std::array<std::array<DWORD,3>,8> filters{};
160 bool inScene=false;
161 };
172 class RenderMaterial final : public RenderResource
173 {
174 public:
176 HRESULT SetTechnique(const char* name){return name && !strcmp(name,"TShader") ? S_OK : E_INVALIDARG;}
177 HRESULT SetMatrix(const char* name,const RiseMath::Matrix* value){return SetValue(name,value,sizeof(*value));}
178 HRESULT SetVector(const char* name,const RiseMath::Vector4* value){return SetValue(name,value,sizeof(*value));}
179 HRESULT SetVectorArray(const char* name,const RiseMath::Vector4* value,UINT count){return SetValue(name,value,count*sizeof(*value));}
180 HRESULT SetIntArray(const char* name,const int* value,UINT count){return SetValue(name,value,count*sizeof(*value));}
181 HRESULT SetFloat(const char* name,float value){return SetValue(name,&value,sizeof(value));}
182 HRESULT SetInt(const char* name,int value){return SetValue(name,&value,sizeof(value));}
183 HRESULT SetTexture(const char* name,RenderTexture* value);
184 HRESULT SetValue(const char* name,const void* value,UINT bytes);
185 HRESULT Begin(UINT* count,DWORD flags);
186 HRESULT BeginPass(UINT pass){if(pass>=15)return E_INVALIDARG;owner.Draws().Material(int(pass));return S_OK;}
187 HRESULT EndPass(){owner.Draws().Material(-1);return S_OK;}
188 HRESULT CommitChanges(){return S_OK;} // Reflected constants upload at the next draw.
189 HRESULT End();
190 HRESULT OnLostDevice(){return S_OK;}
191 HRESULT OnResetDevice(){return S_OK;}
192 private:
194 bool active=false;
196 };
197 class RenderSprite final : public RenderResource
198 {
199 public:
203 HRESULT Begin(DWORD flags);
204 HRESULT Draw(RenderTexture* image,const RECT* rectangle,const RiseMath::Vector3* center,const RiseMath::Vector3* position,PackedColor color)
205 {return sprite.Draw(image ? image->View() : nullptr,rectangle,center,position,color);}
206 HRESULT SetTransform(const RiseMath::Matrix* value){if(!value)return E_POINTER;sprite.SetTransform(*value);return S_OK;}
207 HRESULT GetTransform(RiseMath::Matrix* value) const {if(!value)return E_POINTER;*value=sprite.Transform();return S_OK;}
208 HRESULT Flush();
209 HRESULT End();
210 HRESULT SetSamplerState(DWORD state,DWORD value);
211 HRESULT OnLostDevice(){return S_OK;}
212 HRESULT OnResetDevice(){return S_OK;}
214 private:
217 bool active=false;
220 };
221 class RenderFont final : public RenderResource
222 {
223 public:
225 HRESULT Initialize(const LOGFONTW& description){return font.Initialize(owner.Native(),description);}
226 INT DrawTextW(RenderSprite* sprite,const wchar_t* text,INT count,RECT* rectangle,DWORD flags,PackedColor color);
227 INT DrawFixedTextW(RenderSprite* sprite,const wchar_t* text,INT count,RECT* rectangle,DWORD flags,PackedColor color);
228 INT DrawTextA(RenderSprite* sprite,const char* text,INT count,RECT* rectangle,DWORD flags,PackedColor color);
229 HDC GetDC() const {return font.DC();}
230 BOOL GetTextMetricsW(TEXTMETRICW* result) const {return result && font.Metrics(*result);}
231 HRESULT OnLostDevice(){return S_OK;}
232 HRESULT OnResetDevice(){return S_OK;}
233 private:
236 std::unique_ptr<RenderSprite> automatic;
237 };
238 class RenderToTexture final : public RenderResource
239 {
240 public:
242 HRESULT Initialize(UINT width,UINT height,bool hasDepth);
243 HRESULT BeginScene(RenderSurface* surface,const Viewport* viewport);
244 HRESULT EndScene(DWORD filter);
245 HRESULT OnLostDevice(){return S_OK;}
246 HRESULT OnResetDevice(){return S_OK;}
247 private:
250 std::unique_ptr<DrawContext11::Saved> saved;
251 UINT width=0,height=0;
252 };
254 HRESULT CreateFontW(RenderDevice* owner,INT height,UINT width,UINT weight,UINT mipLevels,BOOL italic,DWORD charset,
255 DWORD precision,DWORD quality,DWORD pitch,const wchar_t* face,RenderFont** result);
256 HRESULT CreateFontA(RenderDevice* owner,INT height,UINT width,UINT weight,UINT mipLevels,BOOL italic,DWORD charset,
257 DWORD precision,DWORD quality,DWORD pitch,const char* face,RenderFont** result);
258 HRESULT CreateTexture(RenderDevice* owner,UINT width,UINT height,UINT levels,DWORD usage,ImageFormat format,ResourcePool pool,RenderTexture** result);
259 HRESULT CreateRenderToSurface(RenderDevice* owner,UINT width,UINT height,ImageFormat format,BOOL depth,ImageFormat depthFormat,RenderToTexture** result);
260 HRESULT LoadTexture(RenderDevice* owner,const wchar_t* path,RenderTexture** result,UINT levels=0,UINT width=0,UINT height=0);
261 HRESULT LoadTexture(RenderDevice* owner,const char* path,RenderTexture** result,UINT levels=0);
262 HRESULT LoadTextureMemory(RenderDevice* owner,const void* data,UINT bytes,RenderTexture** result,UINT levels=0);
263 DXGI_FORMAT NativeFormat(ImageFormat format);
264 ImageFormat ClientFormat(DXGI_FORMAT format);
265 HRESULT SaveSurfaceToMemory(ImageBuffer** result,ImageFile format,RenderSurface* surface,const void* palette,const RECT* crop);
266 HRESULT SaveSurfaceToFile(const wchar_t* path,ImageFile format,RenderSurface* surface,const void* palette,const RECT* crop);
267 HRESULT SaveTextureToFile(const wchar_t* path,ImageFile format,RenderTexture* texture,const void* palette);
268}
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
ID3D11DeviceContext * Context() const
Definition Device11.h:66
ID3D11Device * Device() const
Definition Device11.h:65
void Scissor(const RECT &value)
Device11 * Owner() const
bool Metrics(TEXTMETRICW &result) const
Definition Font11.h:25
HDC DC() const
Definition Font11.h:24
HRESULT Initialize(ID3D11Device *device, const LOGFONTW &description)
Definition Font11.cpp:22
HRESULT Lock(UINT offset, UINT size, void **destination, bool readOnly=false, bool discard=false)
std::vector< uint8_t > bytes
HRESULT Lock(UINT offset, UINT bytes, void **output, DWORD flags)
HRESULT Create(RenderDevice &owner, UINT bytes, DWORD usage, bool index=false, bool indices32=false)
HRESULT DrawPrimitive(Primitive type, UINT first, UINT count)
HRESULT GetFVF(DWORD *value) const
HRESULT CreateStateBlock(UINT type, RenderStateSnapshot **result)
HRESULT SetIndices(RenderBuffer *buffer)
HRESULT SetPixelShader(std::nullptr_t)
HRESULT GetViewport(Viewport *value) const
DrawContext11 & Draws()
std::array< Light, 8 > lights
HRESULT SetStreamSource(UINT stream, RenderBuffer *buffer, UINT offset, UINT stride)
HRESULT SetMaterial(const Material *value)
HRESULT LightEnable(DWORD index, BOOL enabled)
HRESULT GetRenderState(DWORD state, DWORD *value) const
HRESULT SetLight(DWORD index, const Light *value)
ID3D11DeviceContext * Context()
HRESULT CreateIndexBuffer(UINT bytes, DWORD usage, ImageFormat format, ResourcePool pool, RenderBuffer **result, void *shared)
Microsoft::WRL::ComPtr< RenderBuffer > vertices
HRESULT SetClipPlane(DWORD index, const float *value)
HRESULT SetRenderState(DWORD state, DWORD value)
std::array< bool, 8 > enabled
HRESULT GetTransform(DWORD slot, RiseMath::Matrix *value) const
HRESULT SetFVF(DWORD value)
HRESULT SetViewport(const Viewport *value)
HRESULT Layout(VertexLayout11 &result, UINT stride) const
HRESULT SetScissorRect(const RECT *value)
HRESULT CreateQuery(UINT type, RenderQuery **result)
RenderDevice(DrawContext11 &draws)
HRESULT SetTransform(DWORD slot, const RiseMath::Matrix *value)
HRESULT DrawPrimitiveUP(Primitive type, UINT count, const void *vertices, UINT stride)
HRESULT Clear(DWORD count, const RECT *rectangles, DWORD flags, PackedColor color, float depth, DWORD stencil)
std::map< DWORD, DWORD > states
std::array< std::array< DWORD, 3 >, 8 > filters
HRESULT CreateVertexBuffer(UINT bytes, DWORD usage, DWORD format, ResourcePool pool, RenderBuffer **result, void *shared)
HRESULT SetTextureStageState(DWORD stage, DWORD state, DWORD value)
HRESULT SetTexture(DWORD stage, RenderTexture *texture)
HRESULT GetScissorRect(RECT *value) const
HRESULT DrawIndexedPrimitive(Primitive type, INT baseVertex, UINT minimum, UINT vertices, UINT startIndex, UINT count)
HRESULT GetBackBuffer(UINT chain, UINT buffer, UINT type, RenderSurface **result)
Microsoft::WRL::ComPtr< RenderBuffer > indices
HRESULT SetSamplerState(DWORD sampler, DWORD state, DWORD value)
HRESULT SetVertexShader(std::nullptr_t)
HRESULT Restore(const Saved &value)
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)
HRESULT Initialize(const LOGFONTW &description)
RenderFont(RenderDevice &owner)
INT DrawFixedTextW(RenderSprite *sprite, const wchar_t *text, INT count, RECT *rectangle, DWORD flags, PackedColor color)
BOOL GetTextMetricsW(TEXTMETRICW *result) const
HRESULT SetInt(const char *name, int value)
HRESULT SetVectorArray(const char *name, const RiseMath::Vector4 *value, UINT count)
HRESULT SetValue(const char *name, const void *value, UINT bytes)
HRESULT SetVector(const char *name, const RiseMath::Vector4 *value)
HRESULT SetFloat(const char *name, float value)
HRESULT SetIntArray(const char *name, const int *value, UINT count)
HRESULT Begin(UINT *count, DWORD flags)
HRESULT BeginPass(UINT pass)
RenderMaterial(RenderDevice &owner)
HRESULT SetTexture(const char *name, RenderTexture *value)
HRESULT SetMatrix(const char *name, const RiseMath::Matrix *value)
HRESULT SetTechnique(const char *name)
HRESULT Initialize(RenderDevice &owner)
Microsoft::WRL::ComPtr< ID3D11Query > query
HRESULT Issue(DWORD flags)
HRESULT GetData(void *destination, DWORD bytes, DWORD flags)
virtual ~RenderResource()=default
std::atomic< ULONG > references
std::array< DWORD, 3 > filters
RenderSprite(RenderDevice &owner)
HRESULT Begin(DWORD flags)
HRESULT SetTransform(const RiseMath::Matrix *value)
HRESULT SetSamplerState(DWORD state, DWORD value)
HRESULT GetTransform(RiseMath::Matrix *value) const
HRESULT Draw(RenderTexture *image, const RECT *rectangle, const RiseMath::Vector3 *center, const RiseMath::Vector3 *position, PackedColor color)
RenderStateSnapshot(RenderDevice &owner)
Microsoft::WRL::ComPtr< ID3D11Texture2D > texture
ID3D11RenderTargetView * Target() const
HRESULT GetDesc(SurfaceDescription *result) const
ID3D11Texture2D * Native() 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)
ID3D11Resource * Native() const
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)
RenderToTexture(RenderDevice &owner)
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 Initialize(ID3D11Device *device, const std::filesystem::path &shaderDirectory={})
Definition Sprite11.cpp:26
const Matrix & Transform() const
Definition Sprite11.h:22
void SetTransform(const Matrix &value)
Definition Sprite11.h:21
HRESULT Draw(ID3D11ShaderResourceView *texture, const RECT *source=nullptr, const Vector *center=nullptr, const Vector *position=nullptr, uint32_t color=0xffffffff)
Definition Sprite11.cpp:100
constexpr uint32_t LINEAR
constexpr uint32_t ANISOTROPIC
constexpr uint32_t READONLY
constexpr uint32_t DISCARD
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 SaveSurfaceToFile(const wchar_t *path, ImageFile format, RenderSurface *surface, const void *, const RECT *crop)
HRESULT SaveSurfaceToMemory(ImageBuffer **result, ImageFile format, RenderSurface *surface, const void *, const RECT *crop)
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)
DXGI_FORMAT NativeFormat(ImageFormat format)
HRESULT CreateSprite(RenderDevice *owner, RenderSprite **result)
HRESULT SaveTextureToFile(const wchar_t *path, ImageFile format, RenderTexture *texture, const void *)
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)
uint32_t PackedColor
HRESULT CreateTexture(RenderDevice *owner, UINT width, UINT height, UINT levels, DWORD usage, ImageFormat format, ResourcePool pool, RenderTexture **result)
Model.
std::map< DWORD, DWORD > states
std::array< Light, 8 > lights
Microsoft::WRL::ComPtr< RenderBuffer > vertices
std::array< std::array< DWORD, 3 >, 8 > filters
Microsoft::WRL::ComPtr< RenderBuffer > indices