summaryrefslogtreecommitdiffstats
path: root/tools/render-test/render.cpp
blob: 6c47a5afe083eaa5e45cafab677938dc1be04e85 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// render.cpp
#include "render.h"

#include "../../source/core/slang-math.h"

namespace renderer_test {
using namespace Slang;

/* static */const Resource::BindFlag::Enum Resource::s_requiredBinding[int(Usage::CountOf)] = 
{
    BindFlag::VertexBuffer,                 // VertexBuffer
    BindFlag::IndexBuffer,                  // IndexBuffer
    BindFlag::ConstantBuffer,               // ConstantBuffer
    BindFlag::StreamOutput,                 // StreamOut
    BindFlag::RenderTarget,                 // RenderTager
    BindFlag::DepthStencil,                 // DepthRead
    BindFlag::DepthStencil,                 // DepthWrite
    BindFlag::UnorderedAccess,              // UnorderedAccess
    BindFlag::PixelShaderResource,          // PixelShaderResource
    BindFlag::NonPixelShaderResource,       // NonPixelShaderResource
    BindFlag::Enum(BindFlag::PixelShaderResource | BindFlag::NonPixelShaderResource), // GenericRead
}; 

static const Resource::DescBase s_emptyDescBase = {};

const Resource::DescBase& Resource::getDescBase() const
{
    if (isBuffer())
    {
        return static_cast<const BufferResource *>(this)->getDesc();
    }
    else if (isTexture())
    {
        return static_cast<const TextureResource *>(this)->getDesc();
    }
    return s_emptyDescBase;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! RendererUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

/* static */const uint8_t RendererUtil::s_formatSize[int(Format::CountOf)] = 
{
    0,                               // Unknown,

    uint8_t(sizeof(float) * 4),      // RGBA_Float32,
    uint8_t(sizeof(float) * 3),      // RGB_Float32,
    uint8_t(sizeof(float) * 2),      // RG_Float32,
    uint8_t(sizeof(float) * 1),      // R_Float32,

    uint8_t(sizeof(uint32_t)),       // RGBA_Unorm_UInt8,

    uint8_t(sizeof(float)),          // D_Float32,
    uint8_t(sizeof(uint32_t)),       // D_Unorm24_S8,
};

/* !!!!!!!!!!!!!!!!!!!!!!!!!!! BindingState::Desc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void BindingState::Desc::addSampler(const SamplerDesc& desc, const ShaderBindSet& shaderBindSet)
{
    int descIndex = int(m_samplerDescs.Count());
    m_samplerDescs.Add(desc);

    Binding binding;
    binding.bindingType = BindingType::Sampler;
    binding.resource = nullptr;
    binding.shaderBindSet = shaderBindSet;
    binding.descIndex = descIndex;

    m_bindings.Add(binding);
}

void BindingState::Desc::addResource(BindingType bindingType, Resource* resource, const ShaderBindSet& shaderBindSet)
{
    assert(resource);

    Binding binding;
    binding.bindingType = bindingType;
    binding.resource = resource;
    binding.descIndex = -1;
    binding.shaderBindSet = shaderBindSet;
    m_bindings.Add(binding);
}

void BindingState::Desc::addCombinedTextureSampler(TextureResource* resource, const SamplerDesc& samplerDesc, const ShaderBindSet& shaderBindSet)
{
    assert(resource);

    int samplerDescIndex = int(m_samplerDescs.Count());
    m_samplerDescs.Add(samplerDesc);

    Binding binding;
    binding.bindingType = BindingType::CombinedTextureSampler;
    binding.resource = resource;
    binding.descIndex = samplerDescIndex;
    binding.shaderBindSet = shaderBindSet;
    m_bindings.Add(binding);
}

BindingState::CompactBindIndexSlice BindingState::Desc::makeCompactSlice(int index)
{
    if (index < 0)
    {
        return CompactBindIndexSlice();
    }
    return CompactBindIndexSlice(index, 1);
}

BindingState::CompactBindIndexSlice BindingState::Desc::makeCompactSlice(const int* srcIndices, int numIndices)
{
    assert(numIndices >= 0);
    switch (numIndices)
    {
        case 0:     return CompactBindIndexSlice(); 
        case 1:     return CompactBindIndexSlice(srcIndices[0], 1);
        default:
        {
            int startIndex = int(m_sharedBindIndices.Count());
            m_sharedBindIndices.SetSize(startIndex + numIndices);
            uint16_t* dstIndices = m_sharedBindIndices.Buffer() + startIndex;
            for (int i = 0; i < numIndices; i++)
            {
                assert(srcIndices[i] >= 0);
                dstIndices[i] = uint16_t(srcIndices[i]);
            }
            return CompactBindIndexSlice(startIndex, numIndices);
        }
    }
}

int BindingState::Desc::getFirst(const CompactBindIndexSlice& set) const
{
    switch (set.m_size)
    {
        case 0:             return -1;
        case 1:             return set.m_indexOrBase;
        default:            return m_sharedBindIndices[set.m_indexOrBase];
    }
}

int BindingState::Desc::getFirst(ShaderStyle style, const ShaderBindSet& shaderBindSet) const
{
    return getFirst(shaderBindSet.shaderSlices[int(style)]); 
}

void BindingState::Desc::clear()
{
    m_bindings.Clear();
    m_samplerDescs.Clear();
    m_sharedBindIndices.Clear();
    m_numRenderTargets = 1;
}

BindingState::BindIndexSlice BindingState::Desc::asSlice(const CompactBindIndexSlice& compactSlice) const
{
    switch (compactSlice.m_size)
    {
        case 0:     return BindIndexSlice{ nullptr, 0 };
        case 1:     return BindIndexSlice{ &compactSlice.m_indexOrBase, 1 };
        default:    return BindIndexSlice{ m_sharedBindIndices.Buffer() + compactSlice.m_indexOrBase, compactSlice.m_size };
    }
}

BindingState::BindIndexSlice BindingState::Desc::asSlice(ShaderStyle style, const ShaderBindSet& shaderBindSet) const
{
    return asSlice(shaderBindSet.shaderSlices[int(style)]);
}


int BindingState::Desc::findBindingIndex(Resource::BindFlag::Enum bindFlag, ShaderStyleFlags shaderStyleFlags, BindIndex index) const
{
    const int numBindings = int(m_bindings.Count());
    for (int i = 0; i < numBindings; ++i)
    {
        const Binding& binding = m_bindings[i];
        if (binding.resource && (binding.resource->getDescBase().bindFlags & bindFlag) != 0)
        {
            for (int j = 0; j < int(ShaderStyle::CountOf); ++j)
            {
                if (indexOf(binding.shaderBindSet.shaderSlices[j], index) >= 0)
                {
                    return i;
                }
            }
        }
    }

    return -1;
}
/* !!!!!!!!!!!!!!!!!!!!!!!!!!! TextureResource::Size !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

int TextureResource::Size::calcMaxDimension(Type type) const
{
    switch (type)
    {
        case Resource::Type::Texture1D:     return this->width;
        case Resource::Type::Texture3D:     return std::max(std::max(this->width, this->height), this->depth);
        case Resource::Type::TextureCube:   // fallthru
        case Resource::Type::Texture2D:     
        {
            return std::max(this->width, this->height);
        }
        default: return 0;
    }
}

TextureResource::Size TextureResource::Size::calcMipSize(int mipLevel) const
{
    Size size;
    size.width = TextureResource::calcMipSize(this->width, mipLevel);
    size.height = TextureResource::calcMipSize(this->height, mipLevel);
    size.depth = TextureResource::calcMipSize(this->depth, mipLevel);
    return size;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!! BufferResource::Desc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void BufferResource::Desc::setDefaults(Usage initialUsage)
{
    if (this->bindFlags == 0)
    {
        this->bindFlags = Resource::s_requiredBinding[int(initialUsage)];
    }
}

/* !!!!!!!!!!!!!!!!!!!!!!!!! TextureResource::Desc !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

int TextureResource::Desc::calcNumMipLevels(Type type) const
{
    const int maxDimensionSize = this->size.calcMaxDimension(type);
    return (maxDimensionSize > 0) ? (Math::Log2Floor(maxDimensionSize) + 1) : 0;
}

int TextureResource::Desc::calcNumSubResources(Type type) const
{
    const int numMipMaps = (this->numMipLevels > 0) ? this->numMipLevels : calcNumMipLevels(type);
    const int arrSize = (this->arraySize > 0) ? this->arraySize : 1;

    switch (type)
    {
        case Resource::Type::Texture1D:     
        case Resource::Type::Texture2D:     
        {
            return numMipMaps * arrSize;
        }
        case Resource::Type::Texture3D:     
        {
            // can't have arrays of 3d textures
            assert(this->arraySize <= 1);
            return numMipMaps * this->size.depth;  
        }
        case Resource::Type::TextureCube:   
        {
            // There are 6 faces to a cubemap
            return numMipMaps * arrSize * 6;
        }
        default:                            return 0;
    } 
}

void TextureResource::Desc::fixSize(Type type)
{
    switch (type)
    {
        case Resource::Type::Texture1D:
        {
            this->size.height = 1;
            this->size.depth = 1;
            break;
        }
        case Resource::Type::TextureCube:
        case Resource::Type::Texture2D:
        {
            this->size.depth = 1;
            break;
        }
        case Resource::Type::Texture3D:
        {
            // Can't have an array
            this->arraySize = 0;
            break;
        }
        default: break;
    }
}

void TextureResource::Desc::setDefaults(Type type, Usage initialUsage)
{
    fixSize(type);
    if (this->bindFlags == 0)
    {
        this->bindFlags = Resource::s_requiredBinding[int(initialUsage)];
    }
    if (this->numMipLevels <= 0)
    {
        this->numMipLevels = calcNumMipLevels(type);
    }
}

int TextureResource::Desc::calcEffectiveArraySize(Type type) const
{
    const int arrSize = (this->arraySize > 0) ? this->arraySize : 1;

    switch (type)
    {
        case Resource::Type::Texture1D:         // fallthru
        case Resource::Type::Texture2D:         
        {
            return arrSize;
        }
        case Resource::Type::TextureCube:       return arrSize * 6;
        case Resource::Type::Texture3D:         return 1;
        default:                                return 0;
    }
}

void TextureResource::Desc::init()
{
    this->size.init();
    
    this->format = Format::Unknown;
    this->arraySize = 0;
    this->numMipLevels = 0;
    this->sampleDesc.init();

    this->bindFlags = 0;
    this->cpuAccessFlags = 0;
}

void TextureResource::Desc::init1D(Format formatIn, int widthIn, int numMipMapsIn)
{
    this->size.init(widthIn);
    
    this->format = format;
    this->arraySize = 0;
    this->numMipLevels = numMipMapsIn;
    this->sampleDesc.init();

    this->bindFlags = 0;
    this->cpuAccessFlags = 0; 
}

void TextureResource::Desc::init2D(Format formatIn, int widthIn, int heightIn, int numMipMapsIn)
{
    this->size.init(widthIn, heightIn);
    
    this->format = format;
    this->arraySize = 0;
    this->numMipLevels = numMipMapsIn;
    this->sampleDesc.init();

    this->bindFlags = 0;
    this->cpuAccessFlags = 0;
}

void TextureResource::Desc::init3D(Format formatIn, int widthIn, int heightIn, int depthIn, int numMipMapsIn)
{
    this->size.init(widthIn, heightIn, depthIn);

    this->format = format;
    this->arraySize = 0;
    this->numMipLevels = numMipMapsIn;
    this->sampleDesc.init();

    this->bindFlags = 0;
    this->cpuAccessFlags = 0;
}

/* !!!!!!!!!!!!!!!!!!!!!!!!! RennderUtil !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

ProjectionStyle RendererUtil::getProjectionStyle(RendererType type)
{
    switch (type)
    {
        case RendererType::DirectX11:
        case RendererType::DirectX12:
        {
            return ProjectionStyle::DirectX;
        }
        case RendererType::OpenGl:              return ProjectionStyle::OpenGl;
        case RendererType::Vulkan:              return ProjectionStyle::Vulkan;
        case RendererType::Unknown:             return ProjectionStyle::Unknown;
        default:
        {
            assert(!"Unhandled type");
            return ProjectionStyle::Unknown;
        }
    }
}

/* static */void RendererUtil::getIdentityProjection(ProjectionStyle style, float projMatrix[16])
{
    switch (style)
    {
        case ProjectionStyle::DirectX:
        case ProjectionStyle::OpenGl:
        {
            static const float kIdentity[] =
            { 
                1, 0, 0, 0,
                0, 1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1
            };
            ::memcpy(projMatrix, kIdentity, sizeof(kIdentity));
            break;
        }
        case ProjectionStyle::Vulkan:
        {
            static const float kIdentity[] =
            {
                1, 0, 0, 0,
                0, -1, 0, 0,
                0, 0, 1, 0,
                0, 0, 0, 1
            };
            ::memcpy(projMatrix, kIdentity, sizeof(kIdentity));
            break;
        }
        default: 
        {
            assert(!"Not handled");
        }
    }
}

} // renderer_test