summaryrefslogtreecommitdiff
path: root/tools/gfx/immediate-renderer-base.h
blob: b1e867edc32542eb8dc20fca54f15e519b1003cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// immediate-renderer-base.h
#pragma once

// Provides shared implementation of public API objects for targets with
// an immediate mode execution context.

#include "renderer-shared.h"

namespace gfx
{

enum class MapFlavor
{
    Unknown, ///< Unknown mapping type
    HostRead,
    HostWrite,
    WriteDiscard,
};

class ImmediateCommandQueueBase
    : public ICommandQueue
    , public Slang::ComObject
{
public:
    // Immediate device also holds a strong reference to an instance of `ImmediateCommandQueue`,
    // forming a cyclic reference. Therefore we need a free-op here to break the cycle when
    // the public reference count of the queue drops to 0.
    SLANG_COM_OBJECT_IUNKNOWN_ALL
    ICommandQueue* getInterface(const Slang::Guid& guid)
    {
        if (guid == GfxGUID::IID_ISlangUnknown || guid == GfxGUID::IID_ICommandQueue)
            return static_cast<ICommandQueue*>(this);
        return nullptr;
    }
    virtual void comFree() override { breakStrongReferenceToDevice(); }

public:
    BreakableReference<RendererBase> m_renderer;
    void breakStrongReferenceToDevice() { m_renderer.breakStrongReference(); }
    void establishStrongReferenceToDevice() { m_renderer.establishStrongReference(); }
};

struct CommandBufferInfo
{
    bool hasWriteTimestamps;
};

class ImmediateRendererBase : public RendererBase
{
public:
    // Immediate commands to be implemented by each target.
    virtual Result createRootShaderObject(
        IShaderProgram* program,
        ShaderObjectBase** outObject) = 0;
    virtual void bindRootShaderObject(IShaderObject* rootObject) = 0;
    virtual void setPipelineState(IPipelineState* state) = 0;
    virtual void setFramebuffer(IFramebuffer* frameBuffer) = 0;
    virtual void clearFrame(uint32_t colorBufferMask, bool clearDepth, bool clearStencil) = 0;
    virtual void setViewports(UInt count, const Viewport* viewports) = 0;
    virtual void setScissorRects(UInt count, const ScissorRect* scissors) = 0;
    virtual void setPrimitiveTopology(PrimitiveTopology topology) = 0;
    virtual void setVertexBuffers(
        UInt startSlot,
        UInt slotCount,
        IBufferResource* const* buffers,
        const UInt* strides,
        const UInt* offsets) = 0;
    virtual void setIndexBuffer(IBufferResource* buffer, Format indexFormat, UInt offset = 0) = 0;
    virtual void draw(UInt vertexCount, UInt startVertex = 0) = 0;
    virtual void drawIndexed(UInt indexCount, UInt startIndex = 0, UInt baseVertex = 0) = 0;
    virtual void setStencilReference(uint32_t referenceValue) = 0;
    virtual void dispatchCompute(int x, int y, int z) = 0;
    virtual void copyBuffer(
        IBufferResource* dst,
        size_t dstOffset,
        IBufferResource* src,
        size_t srcOffset,
        size_t size) = 0;
    virtual void submitGpuWork() = 0;
    virtual void waitForGpu() = 0;
    virtual void* map(IBufferResource* buffer, MapFlavor flavor) = 0;
    virtual void unmap(IBufferResource* buffer, size_t offsetWritten, size_t sizeWritten) = 0;
    virtual void writeTimestamp(IQueryPool* pool, SlangInt index) = 0;
    virtual void beginCommandBuffer(const CommandBufferInfo&) {}
    virtual void endCommandBuffer(const CommandBufferInfo&) {}

public:
    Slang::RefPtr<ImmediateCommandQueueBase> m_queue;
    uint32_t m_queueCreateCount = 0;

    ImmediateRendererBase();

    virtual SLANG_NO_THROW Result SLANG_MCALL
        createCommandQueue(const ICommandQueue::Desc& desc, ICommandQueue** outQueue) override;
    virtual SLANG_NO_THROW Result SLANG_MCALL createTransientResourceHeap(
        const ITransientResourceHeap::Desc& desc,
        ITransientResourceHeap** outHeap) override;
    virtual SLANG_NO_THROW Result SLANG_MCALL createRenderPassLayout(
        const IRenderPassLayout::Desc& desc,
        IRenderPassLayout** outRenderPassLayout) override;

    void uploadBufferData(
        IBufferResource* dst,
        size_t offset,
        size_t size, void* data);

    virtual SLANG_NO_THROW SlangResult SLANG_MCALL readBufferResource(
        IBufferResource* buffer,
        size_t offset,
        size_t size,
        ISlangBlob** outBlob) override;
};

class ImmediateComputeDeviceBase : public ImmediateRendererBase
{
public:
    // Provide empty implementation for devices without graphics support.
    virtual void setFramebuffer(IFramebuffer* frameBuffer) override { SLANG_UNUSED(frameBuffer); }
    virtual void clearFrame(uint32_t colorBufferMask, bool clearDepth, bool clearStencil) override
    {
        SLANG_UNUSED(colorBufferMask);
        SLANG_UNUSED(clearDepth);
        SLANG_UNUSED(clearStencil);
    }
    virtual void setViewports(UInt count, const Viewport* viewports) override
    {
        SLANG_UNUSED(count);
        SLANG_UNUSED(viewports);
    }
    virtual void setScissorRects(UInt count, const ScissorRect* scissors) override
    {
        SLANG_UNUSED(count);
        SLANG_UNUSED(scissors);
    }
    virtual void setPrimitiveTopology(PrimitiveTopology topology) override
    {
        SLANG_UNUSED(topology);
    }
    virtual void setVertexBuffers(
        UInt startSlot,
        UInt slotCount,
        IBufferResource* const* buffers,
        const UInt* strides,
        const UInt* offsets) override
    {
        SLANG_UNUSED(startSlot);
        SLANG_UNUSED(slotCount);
        SLANG_UNUSED(buffers);
        SLANG_UNUSED(strides);
        SLANG_UNUSED(offsets);
    }
    virtual void setIndexBuffer(IBufferResource* buffer, Format indexFormat, UInt offset = 0)
        override
    {
        SLANG_UNUSED(buffer);
        SLANG_UNUSED(indexFormat);
        SLANG_UNUSED(offset);
    }
    virtual void draw(UInt vertexCount, UInt startVertex = 0) override
    {
        SLANG_UNUSED(vertexCount);
        SLANG_UNUSED(startVertex);
    }
    virtual void drawIndexed(UInt indexCount, UInt startIndex = 0, UInt baseVertex = 0) override
    {
        SLANG_UNUSED(indexCount);
        SLANG_UNUSED(startIndex);
        SLANG_UNUSED(baseVertex);
    }
    virtual void setStencilReference(uint32_t referenceValue) override
    {
        SLANG_UNUSED(referenceValue);
    }

    virtual SLANG_NO_THROW Result SLANG_MCALL createSwapchain(
        const ISwapchain::Desc& desc,
        WindowHandle window,
        ISwapchain** outSwapchain) override
    {
        SLANG_UNUSED(desc);
        SLANG_UNUSED(window);
        SLANG_UNUSED(outSwapchain);
        return SLANG_FAIL;
    }
    virtual SLANG_NO_THROW Result SLANG_MCALL createFramebufferLayout(
        const IFramebufferLayout::Desc& desc,
        IFramebufferLayout** outLayout) override
    {
        SLANG_UNUSED(desc);
        SLANG_UNUSED(outLayout);
        return SLANG_FAIL;
    }
    virtual SLANG_NO_THROW Result SLANG_MCALL
        createFramebuffer(const IFramebuffer::Desc& desc, IFramebuffer** outFramebuffer) override
    {
        SLANG_UNUSED(desc);
        SLANG_UNUSED(outFramebuffer);
        return SLANG_FAIL;
    }
    virtual SLANG_NO_THROW Result SLANG_MCALL createRenderPassLayout(
        const IRenderPassLayout::Desc& desc,
        IRenderPassLayout** outRenderPassLayout) override
    {
        SLANG_UNUSED(desc);
        SLANG_UNUSED(outRenderPassLayout);
        return SLANG_FAIL;
    }

    virtual SLANG_NO_THROW Result SLANG_MCALL createInputLayout(
        const InputElementDesc* inputElements,
        UInt inputElementCount,
        IInputLayout** outLayout) override
    {
        SLANG_UNUSED(inputElements);
        SLANG_UNUSED(inputElementCount);
        SLANG_UNUSED(outLayout);
        return SLANG_E_NOT_AVAILABLE;
    }
    virtual SLANG_NO_THROW Result SLANG_MCALL createGraphicsPipelineState(
        const GraphicsPipelineStateDesc& desc,
        IPipelineState** outState) override
    {
        SLANG_UNUSED(desc);
        SLANG_UNUSED(outState);
        return SLANG_E_NOT_AVAILABLE;
    }
    virtual SLANG_NO_THROW SlangResult SLANG_MCALL readTextureResource(
        ITextureResource* texture,
        ResourceState state,
        ISlangBlob** outBlob,
        size_t* outRowPitch,
        size_t* outPixelSize) override
    {
        SLANG_UNUSED(texture);
        SLANG_UNUSED(outBlob);
        SLANG_UNUSED(outRowPitch);
        SLANG_UNUSED(outPixelSize);

        return SLANG_E_NOT_AVAILABLE;
    }
};
}