yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
9.1 KiB271 linesraw
1// immediate-renderer-base.h
2#pragma once
3
4// Provides shared implementation of public API objects for targets with
5// an immediate mode execution context.
6
7#include "renderer-shared.h"
8
9namespace gfx
10{
11
12enum class MapFlavor
13{
14    Unknown, ///< Unknown mapping type
15    HostRead,
16    HostWrite,
17    WriteDiscard,
18};
19
20class ImmediateCommandQueueBase : public ICommandQueue, public Slang::ComObject
21{
22public:
23    // Immediate device also holds a strong reference to an instance of `ImmediateCommandQueue`,
24    // forming a cyclic reference. Therefore we need a free-op here to break the cycle when
25    // the public reference count of the queue drops to 0.
26    SLANG_COM_OBJECT_IUNKNOWN_ALL
27    ICommandQueue* getInterface(const Slang::Guid& guid)
28    {
29        if (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_ICommandQueue)
30            return static_cast<ICommandQueue*>(this);
31        return nullptr;
32    }
33    virtual void comFree() override { breakStrongReferenceToDevice(); }
34
35public:
36    BreakableReference<RendererBase> m_renderer;
37    void breakStrongReferenceToDevice() { m_renderer.breakStrongReference(); }
38    void establishStrongReferenceToDevice() { m_renderer.establishStrongReference(); }
39};
40
41struct CommandBufferInfo
42{
43    bool hasWriteTimestamps;
44};
45
46class ImmediateRendererBase : public RendererBase
47{
48public:
49    // Immediate commands to be implemented by each target.
50    virtual Result createRootShaderObject(
51        IShaderProgram* program,
52        ShaderObjectBase** outObject) = 0;
53    virtual void bindRootShaderObject(IShaderObject* rootObject) = 0;
54    virtual void setPipelineState(IPipelineState* state) = 0;
55    virtual void setFramebuffer(IFramebuffer* frameBuffer) = 0;
56    virtual void clearFrame(uint32_t colorBufferMask, bool clearDepth, bool clearStencil) = 0;
57    virtual void setViewports(GfxCount count, const Viewport* viewports) = 0;
58    virtual void setScissorRects(GfxCount count, const ScissorRect* scissors) = 0;
59    virtual void setPrimitiveTopology(PrimitiveTopology topology) = 0;
60    virtual void setVertexBuffers(
61        GfxIndex startSlot,
62        GfxCount slotCount,
63        IBufferResource* const* buffers,
64        const Offset* offsets) = 0;
65    virtual void setIndexBuffer(IBufferResource* buffer, Format indexFormat, Offset offset = 0) = 0;
66    virtual void draw(GfxCount vertexCount, GfxIndex startVertex = 0) = 0;
67    virtual void drawIndexed(
68        GfxCount indexCount,
69        GfxIndex startIndex = 0,
70        GfxIndex baseVertex = 0) = 0;
71    virtual void drawInstanced(
72        GfxCount vertexCount,
73        GfxCount instanceCount,
74        GfxIndex startVertex,
75        GfxIndex startInstanceLocation) = 0;
76    virtual void drawIndexedInstanced(
77        GfxCount indexCount,
78        GfxCount instanceCount,
79        GfxIndex startIndexLocation,
80        GfxIndex baseVertexLocation,
81        GfxIndex startInstanceLocation) = 0;
82    virtual void setStencilReference(uint32_t referenceValue) = 0;
83    virtual void dispatchCompute(int x, int y, int z) = 0;
84    virtual void copyBuffer(
85        IBufferResource* dst,
86        Offset dstOffset,
87        IBufferResource* src,
88        Offset srcOffset,
89        Size size) = 0;
90    virtual void submitGpuWork() = 0;
91    virtual void waitForGpu() = 0;
92    virtual void* map(IBufferResource* buffer, MapFlavor flavor) = 0;
93    virtual void unmap(IBufferResource* buffer, size_t offsetWritten, size_t sizeWritten) = 0;
94    virtual void writeTimestamp(IQueryPool* pool, GfxIndex index) = 0;
95    virtual void beginCommandBuffer(const CommandBufferInfo&) {}
96    virtual void endCommandBuffer(const CommandBufferInfo&) {}
97
98public:
99    Slang::RefPtr<ImmediateCommandQueueBase> m_queue;
100    uint32_t m_queueCreateCount = 0;
101
102    ImmediateRendererBase();
103
104    virtual SLANG_NO_THROW Result SLANG_MCALL
105    createCommandQueue(const ICommandQueue::Desc& desc, ICommandQueue** outQueue) override;
106    virtual SLANG_NO_THROW Result SLANG_MCALL createTransientResourceHeap(
107        const ITransientResourceHeap::Desc& desc,
108        ITransientResourceHeap** outHeap) override;
109    virtual SLANG_NO_THROW Result SLANG_MCALL createRenderPassLayout(
110        const IRenderPassLayout::Desc& desc,
111        IRenderPassLayout** outRenderPassLayout) override;
112
113    void uploadBufferData(IBufferResource* dst, Offset offset, Size size, void* data);
114
115    virtual SLANG_NO_THROW SlangResult SLANG_MCALL readBufferResource(
116        IBufferResource* buffer,
117        Offset offset,
118        Size size,
119        ISlangBlob** outBlob) override;
120};
121
122class ImmediateComputeDeviceBase : public ImmediateRendererBase
123{
124public:
125    // Provide empty implementation for devices without graphics support.
126    virtual void setFramebuffer(IFramebuffer* frameBuffer) override { SLANG_UNUSED(frameBuffer); }
127    virtual void clearFrame(uint32_t colorBufferMask, bool clearDepth, bool clearStencil) override
128    {
129        SLANG_UNUSED(colorBufferMask);
130        SLANG_UNUSED(clearDepth);
131        SLANG_UNUSED(clearStencil);
132    }
133    virtual void setViewports(GfxCount count, const Viewport* viewports) override
134    {
135        SLANG_UNUSED(count);
136        SLANG_UNUSED(viewports);
137    }
138    virtual void setScissorRects(GfxCount count, const ScissorRect* scissors) override
139    {
140        SLANG_UNUSED(count);
141        SLANG_UNUSED(scissors);
142    }
143    virtual void setPrimitiveTopology(PrimitiveTopology topology) override
144    {
145        SLANG_UNUSED(topology);
146    }
147    virtual void setVertexBuffers(
148        GfxIndex startSlot,
149        GfxCount slotCount,
150        IBufferResource* const* buffers,
151        const Offset* offsets) override
152    {
153        SLANG_UNUSED(startSlot);
154        SLANG_UNUSED(slotCount);
155        SLANG_UNUSED(buffers);
156        SLANG_UNUSED(offsets);
157    }
158    virtual void setIndexBuffer(IBufferResource* buffer, Format indexFormat, Offset offset = 0)
159        override
160    {
161        SLANG_UNUSED(buffer);
162        SLANG_UNUSED(indexFormat);
163        SLANG_UNUSED(offset);
164    }
165    virtual void draw(GfxCount vertexCount, GfxIndex startVertex = 0) override
166    {
167        SLANG_UNUSED(vertexCount);
168        SLANG_UNUSED(startVertex);
169    }
170    virtual void drawIndexed(GfxCount indexCount, GfxIndex startIndex = 0, GfxIndex baseVertex = 0)
171        override
172    {
173        SLANG_UNUSED(indexCount);
174        SLANG_UNUSED(startIndex);
175        SLANG_UNUSED(baseVertex);
176    }
177    virtual void drawInstanced(
178        GfxCount vertexCount,
179        GfxCount instanceCount,
180        GfxIndex startVertex,
181        GfxIndex startInstanceLocation) override
182    {
183        SLANG_UNUSED(vertexCount);
184        SLANG_UNUSED(instanceCount);
185        SLANG_UNUSED(startVertex);
186        SLANG_UNUSED(startInstanceLocation);
187    }
188
189    virtual void drawIndexedInstanced(
190        GfxCount indexCount,
191        GfxCount instanceCount,
192        GfxIndex startIndexLocation,
193        GfxIndex baseVertexLocation,
194        GfxIndex startInstanceLocation) override
195    {
196        SLANG_UNUSED(indexCount);
197        SLANG_UNUSED(instanceCount);
198        SLANG_UNUSED(startIndexLocation);
199        SLANG_UNUSED(baseVertexLocation);
200        SLANG_UNUSED(startInstanceLocation);
201    }
202    virtual void setStencilReference(uint32_t referenceValue) override
203    {
204        SLANG_UNUSED(referenceValue);
205    }
206
207    virtual SLANG_NO_THROW Result SLANG_MCALL createSwapchain(
208        const ISwapchain::Desc& desc,
209        WindowHandle window,
210        ISwapchain** outSwapchain) override
211    {
212        SLANG_UNUSED(desc);
213        SLANG_UNUSED(window);
214        SLANG_UNUSED(outSwapchain);
215        return SLANG_FAIL;
216    }
217    virtual SLANG_NO_THROW Result SLANG_MCALL createFramebufferLayout(
218        const IFramebufferLayout::Desc& desc,
219        IFramebufferLayout** outLayout) override
220    {
221        SLANG_UNUSED(desc);
222        SLANG_UNUSED(outLayout);
223        return SLANG_FAIL;
224    }
225    virtual SLANG_NO_THROW Result SLANG_MCALL
226    createFramebuffer(const IFramebuffer::Desc& desc, IFramebuffer** outFramebuffer) override
227    {
228        SLANG_UNUSED(desc);
229        SLANG_UNUSED(outFramebuffer);
230        return SLANG_FAIL;
231    }
232    virtual SLANG_NO_THROW Result SLANG_MCALL createRenderPassLayout(
233        const IRenderPassLayout::Desc& desc,
234        IRenderPassLayout** outRenderPassLayout) override
235    {
236        SLANG_UNUSED(desc);
237        SLANG_UNUSED(outRenderPassLayout);
238        return SLANG_FAIL;
239    }
240
241    virtual SLANG_NO_THROW Result SLANG_MCALL
242    createInputLayout(IInputLayout::Desc const& desc, IInputLayout** outLayout) override
243    {
244        SLANG_UNUSED(desc);
245        SLANG_UNUSED(outLayout);
246        return SLANG_E_NOT_AVAILABLE;
247    }
248    virtual SLANG_NO_THROW Result SLANG_MCALL createGraphicsPipelineState(
249        const GraphicsPipelineStateDesc& desc,
250        IPipelineState** outState) override
251    {
252        SLANG_UNUSED(desc);
253        SLANG_UNUSED(outState);
254        return SLANG_E_NOT_AVAILABLE;
255    }
256    virtual SLANG_NO_THROW SlangResult SLANG_MCALL readTextureResource(
257        ITextureResource* texture,
258        ResourceState state,
259        ISlangBlob** outBlob,
260        Size* outRowPitch,
261        Size* outPixelSize) override
262    {
263        SLANG_UNUSED(texture);
264        SLANG_UNUSED(outBlob);
265        SLANG_UNUSED(outRowPitch);
266        SLANG_UNUSED(outPixelSize);
267
268        return SLANG_E_NOT_AVAILABLE;
269    }
270};
271} // namespace gfx