-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvars.go
825 lines (749 loc) · 24.3 KB
/
vars.go
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
// Copyright (c) 2022, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vgpu
import (
"fmt"
"log"
"runtime"
"strings"
"cogentcore.org/core/base/indent"
"cogentcore.org/core/vgpu/szalloc"
vk "github.com/goki/vulkan"
)
// Vars are all the variables that are used by a pipeline,
// organized into Sets (optionally including the special VertexSet
// or PushSet).
// Vars are allocated to bindings / locations sequentially in the
// order added!
type Vars struct {
// map of sets, by set number -- VertexSet is -2, PushSet is -1, rest are added incrementally
SetMap map[int]*VarSet
// map of vars by different roles across all sets -- updated in Config(), after all vars added. This is needed for VkDescPool allocation.
RoleMap map[VarRoles][]*Var
// true if a VertexSet has been added
HasVertex bool `edit:"-"`
// true if PushSet has been added
HasPush bool `edit:"-"`
// number of complete descriptor sets to construct -- each descriptor set can be bound to a specific pipeline at the start of rendering, and updated with specific Value instances to provide values for each Var used during rendering. If multiple rendering passes are performed in parallel, then each requires a separate descriptor set (e.g., typically associated with a different Frame in the swapchain), so this number should be increased.
NDescs int
// our parent memory manager
Mem *Memory `display:"-"`
// if true, variables are statically bound to specific offsets in memory buffers, vs. dynamically bound offsets. Typically a compute shader operating on fixed data variables can use static binding, while graphics (e.g., vphong) requires dynamic binding to efficiently use the same shader code for multiple different values of the same variable type
StaticVars bool `edit:"-"`
// vulkan descriptor layout based on vars
VkDescLayout vk.PipelineLayout `display:"-"`
// vulkan descriptor pool, allocated for NDescs and the different descriptor pools
VkDescPool vk.DescriptorPool `display:"-"`
// allocated descriptor sets -- outer index is Vars.NDescs for different groups of descriptor sets, one of which can be bound to a pipeline at any given time. The inner dimension is per VarSet to cover the different sets of variable updated at different times or with different numbers of items. This variable is used for whole-pipline binding at start of rendering.
VkDescSets [][]vk.DescriptorSet
// currently accumulating set of vals to write to update bindings -- initiated by BindValuesStart, executed by BindValuesEnd
VkWriteValues []vk.WriteDescriptorSet `display:"-"`
// current descriptor collection index, set in BindValuesStart
BindDescIndex int `edit:"-"`
// dynamic offsets for Uniform and Storage variables, -- outer index is Vars.NDescs for different groups of descriptor sets, one of which can be bound to a pipeline at any given time, inner index is DynOffIndex on Var -- offsets are set when Value is bound via BindDynValue*.
DynOffs [][]uint32
// number of textures, at point of creating the DescLayout
NTextures int
}
func (vs *Vars) Destroy(dev vk.Device) {
vk.DestroyPipelineLayout(dev, vs.VkDescLayout, nil)
vk.DestroyDescriptorPool(dev, vs.VkDescPool, nil)
for _, st := range vs.SetMap {
st.Destroy(dev)
}
}
// AddVertexSet adds a new Vertex Set -- this is a special Set holding Vertex, Index vars
func (vs *Vars) AddVertexSet() *VarSet {
if vs.SetMap == nil {
vs.SetMap = make(map[int]*VarSet)
}
st := &VarSet{Set: VertexSet, ParentVars: vs}
vs.SetMap[VertexSet] = st
vs.HasVertex = true
return st
}
// VertexSet returns the Vertex Set -- a special Set holding Vertex, Index vars
func (vs *Vars) VertexSet() *VarSet {
return vs.SetMap[VertexSet]
}
// AddPushSet adds a new push constant Set -- this is a special Set holding
// values sent directly in the command buffer.
func (vs *Vars) AddPushSet() *VarSet {
if vs.SetMap == nil {
vs.SetMap = make(map[int]*VarSet)
}
st := &VarSet{Set: PushSet, ParentVars: vs}
vs.SetMap[PushSet] = st
vs.HasPush = true
return st
}
// PushSet returns the Push Set -- a special Set holding push constants
func (vs *Vars) PushSet() *VarSet {
return vs.SetMap[PushSet]
}
// AddSet adds a new non-Vertex Set for holding Uniforms, Storage, etc
// Sets are automatically numbered sequentially
func (vs *Vars) AddSet() *VarSet {
if vs.SetMap == nil {
vs.SetMap = make(map[int]*VarSet)
}
idx := vs.NSets()
st := &VarSet{Set: idx, ParentVars: vs}
vs.SetMap[idx] = st
return st
}
// VarByNameTry returns Var by name in given set number,
// returning error if not found
func (vs *Vars) VarByNameTry(set int, name string) (*Var, error) {
st, err := vs.SetTry(set)
if err != nil {
return nil, err
}
return st.VarByNameTry(name)
}
// ValueByNameTry returns value by first looking up variable name, then value name,
// within given set number, returning error if not found
func (vs *Vars) ValueByNameTry(set int, varName, valName string) (*Var, *Value, error) {
st, err := vs.SetTry(set)
if err != nil {
return nil, nil, err
}
return st.ValueByNameTry(varName, valName)
}
// ValueByIndexTry returns value by first looking up variable name, then value index,
// returning error if not found
func (vs *Vars) ValueByIndexTry(set int, varName string, valIndex int) (*Var, *Value, error) {
st, err := vs.SetTry(set)
if err != nil {
return nil, nil, err
}
return st.ValueByIndexTry(varName, valIndex)
}
// Config must be called after all variables have been added.
// Configures all Sets and also does validation, returning error
// does DescLayout too, so all ready for Pipeline config.
func (vs *Vars) Config() error {
dev := vs.Mem.Device.Device
ns := vs.NSets()
var cerr error
vs.RoleMap = make(map[VarRoles][]*Var)
for si := vs.StartSet(); si < ns; si++ {
st := vs.SetMap[si]
if st == nil {
continue
}
err := st.Config(dev)
if err != nil {
cerr = err
}
for ri, rl := range st.RoleMap {
vs.RoleMap[ri] = append(vs.RoleMap[ri], rl...)
}
}
vs.DescLayout(dev)
return cerr
}
// StringDoc returns info on variables
func (vs *Vars) StringDoc() string {
ispc := 4
var sb strings.Builder
ns := vs.NSets()
for si := vs.StartSet(); si < ns; si++ {
st := vs.SetMap[si]
if st == nil {
continue
}
sb.WriteString(fmt.Sprintf("Set: %d\n", st.Set))
for ri := Vertex; ri < VarRolesN; ri++ {
rl, has := st.RoleMap[ri]
if !has || len(rl) == 0 {
continue
}
sb.WriteString(fmt.Sprintf("%sRole: %s\n", indent.Spaces(1, ispc), ri.String()))
for _, vr := range rl {
sb.WriteString(fmt.Sprintf("%sVar: %s\n", indent.Spaces(2, ispc), vr.String()))
}
}
}
return sb.String()
}
// NSets returns the number of regular non-VertexSet sets
func (vs *Vars) NSets() int {
ex := 0
if vs.HasVertex {
ex++
}
if vs.HasPush {
ex++
}
return len(vs.SetMap) - ex
}
// StartSet returns the starting set to use for iterating sets
func (vs *Vars) StartSet() int {
switch {
case vs.HasVertex:
return VertexSet
case vs.HasPush:
return PushSet
default:
return 0
}
}
// SetTry returns set by index, returning nil and error if not found
func (vs *Vars) SetTry(set int) (*VarSet, error) {
st, has := vs.SetMap[set]
if !has {
err := fmt.Errorf("vgpu.Vars:SetTry set number %d not found", set)
if Debug {
log.Println(err)
}
return nil, err
}
return st, nil
}
// VkVertexConfig returns vulkan vertex config struct, for VertexSet only!
// Note: there is no support for interleaved arrays so each binding and location
// is assigned the same sequential number, recorded in var BindLoc
func (vs *Vars) VkVertexConfig() *vk.PipelineVertexInputStateCreateInfo {
if vs.HasVertex {
return vs.SetMap[VertexSet].VkVertexConfig()
}
cfg := &vk.PipelineVertexInputStateCreateInfo{}
cfg.SType = vk.StructureTypePipelineVertexInputStateCreateInfo
return cfg
}
// VkPushConfig returns vulkan push constant ranges, only if PushSet used.
func (vs *Vars) VkPushConfig() []vk.PushConstantRange {
if vs.HasPush {
return vs.SetMap[PushSet].VkPushConfig()
}
return nil
}
///////////////////////////////////////////////////////////////////
// Descriptors for Uniforms etc
// key info on descriptorCount -- very confusing.
// https://stackoverflow.com/questions/51715944/descriptor-set-count-ambiguity-in-vulkan
var nPoolDelete = 0
// DescLayout configures the PipelineLayout of DescriptorSetLayout
// info for all of the non-Vertex vars
func (vs *Vars) DescLayout(dev vk.Device) {
vs.NTextures = 0
if vs.VkDescLayout != vk.NullPipelineLayout {
// note: this fixes a reliable crash on windows, where
// deleting the DescriptorPool causes a crash most of the time
// during the xyz demo, when manipulating the scene, and adding
// new elements, which drives a new DescLayout call.
// https://github.com/cogentcore/core/issues/536
if runtime.GOOS == "windows" && nPoolDelete < 3 {
nPoolDelete++
} else {
vk.DestroyPipelineLayout(dev, vs.VkDescLayout, nil)
if vs.VkDescPool != vk.NullDescriptorPool {
vk.DestroyDescriptorPool(dev, vs.VkDescPool, nil)
}
}
}
if vs.NDescs < 1 {
vs.NDescs = 1
}
nset := vs.NSets()
if nset == 0 {
var pipelineLayout vk.PipelineLayout
ret := vk.CreatePipelineLayout(dev, &vk.PipelineLayoutCreateInfo{
SType: vk.StructureTypePipelineLayoutCreateInfo,
SetLayoutCount: 0,
PSetLayouts: nil,
}, nil, &pipelineLayout)
IfPanic(NewError(ret))
vs.VkDescLayout = pipelineLayout
vs.VkDescPool = vk.NullDescriptorPool
vs.VkDescSets = nil
return
}
var pools []vk.DescriptorPoolSize
for ri := Uniform; ri < VarRolesN; ri++ {
vl := vs.RoleMap[ri]
if len(vl) == 0 {
continue
}
dcount := vs.NDescs * len(vl)
if ri > Storage {
dcount = 0
for _, vr := range vl {
vals := vr.Values.ActiveValues()
dcount += vs.NDescs * len(vals)
}
}
if dcount == 0 {
continue
}
pl := vk.DescriptorPoolSize{
DescriptorCount: uint32(dcount),
Type: RoleDescriptors[ri],
}
if vs.StaticVars {
pl.Type = StaticRoleDescriptors[ri]
}
pools = append(pools, pl)
}
var flags vk.DescriptorPoolCreateFlagBits
flags |= vk.DescriptorPoolCreateUpdateAfterBindBit
var descPool vk.DescriptorPool
ret := vk.CreateDescriptorPool(dev, &vk.DescriptorPoolCreateInfo{
SType: vk.StructureTypeDescriptorPoolCreateInfo,
MaxSets: uint32(vs.NDescs * nset),
PoolSizeCount: uint32(len(pools)),
PPoolSizes: pools,
Flags: vk.DescriptorPoolCreateFlags(flags),
}, nil, &descPool)
IfPanic(NewError(ret))
vs.VkDescPool = descPool
vs.DynOffs = make([][]uint32, vs.NDescs)
dlays := make([]vk.DescriptorSetLayout, nset)
for si := 0; si < nset; si++ {
st := vs.SetMap[si]
if st == nil {
continue
}
st.DescLayout(dev, vs)
vs.NTextures += st.NTextures
dlays[si] = st.VkLayout
}
if vs.HasVertex {
vset := vs.SetMap[VertexSet]
for _, vr := range vset.Vars {
vr.BindValueIndex = make([]int, vs.NDescs)
}
}
dsets := make([][]vk.DescriptorSet, vs.NDescs)
for i := 0; i < vs.NDescs; i++ {
dsets[i] = make([]vk.DescriptorSet, nset)
for si := 0; si < nset; si++ {
st := vs.SetMap[si]
if st == nil {
continue
}
dsets[i][si] = st.VkDescSets[i]
}
}
vs.VkDescSets = dsets // for pipeline binding
pushc := vs.VkPushConfig()
var pipelineLayout vk.PipelineLayout
ret = vk.CreatePipelineLayout(dev, &vk.PipelineLayoutCreateInfo{
SType: vk.StructureTypePipelineLayoutCreateInfo,
SetLayoutCount: uint32(len(dlays)),
PSetLayouts: dlays,
PPushConstantRanges: pushc,
PushConstantRangeCount: uint32(len(pushc)),
}, nil, &pipelineLayout)
IfPanic(NewError(ret))
vs.VkDescLayout = pipelineLayout
}
// AddDynOff adds one more dynamic offset across all NDescs
func (vs *Vars) AddDynOff() {
for i := 0; i < vs.NDescs; i++ {
vs.DynOffs[i] = append(vs.DynOffs[i], 0)
}
}
/////////////////////////////////////////////////////////////////////////
// Bind Vars -- call prior to render pass only!!
// BindVarsStart starts a new step of binding vars to descriptor sets,
// using given descIndex description set index (among the NDescs allocated).
// Bound vars determine what the shader programs see,
// in subsequent calls to Pipeline commands.
//
// This must be called *prior* to a render pass, never within it.
// Only BindDyn* and BindVertex* calls can be called within render.
//
// Do NOT use this around BindDynValue or BindVertexValue calls
// only for BindVar* methods.
//
// Subsequent calls of BindVar* methods will add to a list, which
// will be executed when BindValuesEnd is called.
//
// This creates a set of entries in a list of WriteDescriptorSet's
func (vs *Vars) BindVarsStart(descIndex int) {
vs.VkWriteValues = []vk.WriteDescriptorSet{}
vs.BindDescIndex = descIndex
}
// BindVarsEnd finishes a new step of binding started by BindVarsStart.
// Actually executes the binding updates, based on prior BindVar* calls.
func (vs *Vars) BindVarsEnd() {
dev := vs.Mem.Device.Device
if len(vs.VkWriteValues) > 0 {
vk.UpdateDescriptorSets(dev, uint32(len(vs.VkWriteValues)), vs.VkWriteValues, 0, nil)
}
vs.VkWriteValues = nil
}
// BindDynVars binds all dynamic vars in given set, to be able to
// use dynamic vars, in subsequent BindDynValue* calls during the
// render pass, which update the offsets.
// For Uniform & Storage variables, which use dynamic binding.
//
// This is automatically called during Config on the System,
// and usually does not need to be called again.
//
// All vals must be uploaded to Device memory prior to this,
// and it is not possible to update actual values during a render pass.
// The memory buffer is essentially what is bound here.
//
// Must have called BindVarsStart prior to this.
func (vs *Vars) BindDynVars(set int) error {
st, err := vs.SetTry(set)
if err != nil {
return err
}
st.BindDynVars(vs)
return nil
}
// BindDynVarsAll binds all dynamic vars across all sets.
// Called during system config.
func (vs *Vars) BindDynVarsAll() {
nset := vs.NSets()
for i := 0; i < vs.NDescs; i++ {
vs.BindVarsStart(i)
for si := 0; si < nset; si++ {
vs.BindDynVars(si)
}
vs.BindVarsEnd()
}
}
// BindStatVarsAll binds all static vars to their current values,
// for all Uniform and Storage variables, when using Static value binding.
//
// All vals must be uploaded to Device memory prior to this.
func (vs *Vars) BindStatVarsAll() {
for i := 0; i < vs.NDescs; i++ {
vs.BindVarsStart(i)
for si, st := range vs.SetMap {
if si < 0 {
continue
}
st.BindStatVarsAll(vs)
}
vs.BindVarsEnd()
}
}
// BindDynVarName binds dynamic variable for given var
// looked up by name, for Uniform, Storage variables.
//
// All vals must be uploaded to Device memory prior to this,
// and it is not possible to update actual values during a render pass.
// The memory buffer is essentially what is bound here.
//
// Must have called BindVarsStart prior to this.
func (vs *Vars) BindDynVarName(set int, varNm string) error {
st, err := vs.SetTry(set)
if err != nil {
return err
}
return st.BindDynVarName(vs, varNm)
}
// BindStatVars binds all static vars to their current values,
// for given set, for non-Uniform, Storage, variables (e.g., Textures).
// Each Value for a given Var is given a descriptor binding
// and the shader sees an array of values of corresponding length.
//
// All vals must be uploaded to Device memory prior to this,
// and it is not possible to update anything during a render pass.
//
// Must have called BindVarsStart prior to this.
func (vs *Vars) BindStatVars(set int) error {
st, err := vs.SetTry(set)
if err != nil {
return err
}
st.BindStatVars(vs)
return nil
}
// BindStatVarName does static variable binding for given var
// looked up by name, for non-Uniform, Storage, variables (e.g., Textures).
// Each Value for a given Var is given a descriptor binding
// and the shader sees an array of values of corresponding length.
//
// All vals must be uploaded to Device memory prior to this,
// and it is not possible to update anything during a render pass.
//
// Must have called BindVarsStart prior to this.
func (vs *Vars) BindStatVarName(set int, varNm string) error {
st, err := vs.SetTry(set)
if err != nil {
return err
}
return st.BindStatVarName(vs, varNm)
}
// BindAllTextureVars binds all Texture vars in given set to their current values,
// iterating over NTextureDescs in case there are multiple Desc sets
// required to represent more than MaxTexturesPerSet.
// Each Value for a given Var is given a descriptor binding
// and the shader sees an array of values of corresponding length.
// All vals must be uploaded to Device memory prior to this,
// and it is not possible to update anything during a render pass.
// This calls BindStart / Bind
func (vs *Vars) BindAllTextureVars(set int) error {
st, err := vs.SetTry(set)
if err != nil {
return err
}
cbi := vs.BindDescIndex
for i := 0; i < st.NTextureDescs; i++ {
vs.BindVarsStart(i)
st.BindStatVars(vs)
vs.BindVarsEnd()
}
vs.BindDescIndex = cbi
return nil
}
/////////////////////////////////////////////////////////////////////////
// Dynamic Binding
// BindVertexValueName dynamically binds given VertexSet value
// by name for given variable name.
// using given descIndex description set index (among the NDescs allocated).
//
// Value must have already been updated into device memory prior to this,
// ideally through a batch update prior to starting rendering, so that
// all the values are ready to be used during the render pass.
// This dynamically updates the offset to point to the specified val.
//
// Do NOT call BindValuesStart / End around this.
//
// returns error if not found.
func (vs *Vars) BindVertexValueName(varNm, valNm string) error {
st := vs.SetMap[VertexSet]
vr, vl, err := st.ValueByNameTry(varNm, valNm)
if err != nil {
return err
}
vr.BindValueIndex[vs.BindDescIndex] = vl.Index // this is then consumed by draw command
return nil
}
// BindVertexValueIndex dynamically binds given VertexSet value
// by index for given variable name.
// using given descIndex description set index (among the NDescs allocated).
//
// Value must have already been updated into device memory prior to this,
// ideally through a batch update prior to starting rendering, so that
// all the values are ready to be used during the render pass.
// This only dynamically updates the offset to point to the specified val.
//
// Do NOT call BindValuesStart / End around this.
//
// returns error if not found.
func (vs *Vars) BindVertexValueIndex(varNm string, valIndex int) error {
st := vs.SetMap[VertexSet]
vr, vl, err := st.ValueByIndexTry(varNm, valIndex)
if err != nil {
return err
}
vr.BindValueIndex[vs.BindDescIndex] = vl.Index // this is then consumed by draw command
return nil
}
// BindDynValuesAllIndex dynamically binds all uniform, storage values
// by index for all variables in all sets.
//
// This only dynamically updates the offset to point to the specified val.
// MUST call System.BindVars prior to any subsequent draw calls for this
// new offset to be bound at the proper point in the command buffer prior
// (call after all such dynamic bindings are updated.)
//
// Do NOT call BindValuesStart / End around this.
func (vs *Vars) BindDynValuesAllIndex(idx int) {
for si, st := range vs.SetMap {
if si < 0 {
continue
}
st.BindDynValuesAllIndex(vs, idx)
}
}
// BindDynamicValueName dynamically binds given uniform or storage value
// by name for given variable name, in given set.
//
// This only dynamically updates the offset to point to the specified val.
// MUST call System.BindVars prior to any subsequent draw calls for this
// new offset to be bound at the proper point in the command buffer prior
// (call after all such dynamic bindings are updated.)
//
// Do NOT call BindValuesStart / End around this.
//
// returns error if not found.
func (vs *Vars) BindDynamicValueName(set int, varNm, valNm string) error {
st, err := vs.SetTry(set)
if err != nil {
return err
}
return st.BindDynValueName(vs, varNm, valNm)
}
// BindDynamicValueIndex dynamically binds given uniform or storage value
// by index for given variable name, in given set.
//
// This only dynamically updates the offset to point to the specified val.
// MUST call System.BindVars prior to any subsequent draw calls for this
// new offset to be bound at the proper point in the command buffer prior
// (call after all such dynamic bindings are updated.)
//
// Do NOT call BindValuesStart / End around this.
//
// returns error if not found.
func (vs *Vars) BindDynamicValueIndex(set int, varNm string, valIndex int) error {
st, err := vs.SetTry(set)
if err != nil {
return err
}
return st.BindDynValueIndex(vs, varNm, valIndex)
}
// BindDynamicValue dynamically binds given uniform or storage value
// for given variable in given set.
//
// This only dynamically updates the offset to point to the specified val.
// MUST call System.BindVars prior to any subsequent draw calls for this
// new offset to be bound at the proper point in the command buffer prior
// (call after all such dynamic bindings are updated.)
//
// Do NOT call BindValuesStart / End around this.
//
// returns error if not found.
func (vs *Vars) BindDynamicValue(set int, vr *Var, vl *Value) error {
st, err := vs.SetTry(set)
if err != nil {
return err
}
return st.BindDynValue(vs, vr, vl)
}
// TextureGroupSizeIndexes for texture at given index, allocated in groups by size
// using Values.AllocTexBySize, returns the indexes for the texture
// and layer to actually select the texture in the shader, and proportion
// of the Gp allocated texture size occupied by the texture.
func (vs *Vars) TextureGroupSizeIndexes(set int, varNm string, valIndex int) *szalloc.Indexes {
st, err := vs.SetTry(set)
if err != nil {
return nil
}
return st.TextureGroupSizeIndexes(vs, varNm, valIndex)
}
///////////////////////////////////////////////////////////
// Memory allocation
func (vs *Vars) MemSize(buff *MemBuff) int {
tsz := 0
ns := vs.NSets()
for si := vs.StartSet(); si < ns; si++ {
st := vs.SetMap[si]
if st == nil {
continue
}
for _, vr := range st.Vars {
if vr.Role.BuffType() != buff.Type {
continue
}
tsz += vr.ValuesMemSize(buff.AlignBytes)
}
}
return tsz
}
func (vs *Vars) MemSizeStorage(mm *Memory, alignBytes int) {
ns := vs.NSets()
for si := vs.StartSet(); si < ns; si++ {
st := vs.SetMap[si]
if st == nil {
continue
}
for _, vr := range st.Vars {
if vr.Role.BuffType() != StorageBuff {
continue
}
vr.MemSizeStorage(mm, alignBytes)
}
}
}
func (vs *Vars) AllocHost(buff *MemBuff, offset int) int {
ns := vs.NSets()
tsz := 0
for si := vs.StartSet(); si < ns; si++ {
st := vs.SetMap[si]
if st == nil || st.Set == PushSet {
continue
}
for _, vr := range st.Vars {
if vr.Role.BuffType() != buff.Type {
continue
}
sz := vr.Values.AllocHost(vr, buff, offset)
offset += sz
tsz += sz
}
}
return tsz
}
// Free resets the MemPtr for values, resets any self-owned resources (Textures)
func (vs *Vars) Free(buff *MemBuff) {
ns := vs.NSets()
for si := vs.StartSet(); si < ns; si++ {
st := vs.SetMap[si]
if st == nil || st.Set == PushSet {
continue
}
for _, vr := range st.Vars {
if vr.Role.BuffType() != buff.Type {
continue
}
vr.Values.Free()
}
}
}
// ModRegs returns the regions of Values that have been modified
func (vs *Vars) ModRegs(bt BuffTypes) []MemReg {
ns := vs.NSets()
var mods []MemReg
for si := vs.StartSet(); si < ns; si++ {
st := vs.SetMap[si]
if st == nil || st.Set == PushSet {
continue
}
for _, vr := range st.Vars {
if vr.Role.BuffType() != bt {
continue
}
md := vr.Values.ModRegs(vr)
mods = append(mods, md...)
}
}
return mods
}
// ModRegStorage returns the regions of Storage Values that have been modified
func (vs *Vars) ModRegsStorage(bufIndex int, buff *MemBuff) []MemReg {
ns := vs.NSets()
var mods []MemReg
for si := vs.StartSet(); si < ns; si++ {
st := vs.SetMap[si]
if st == nil || st.Set == PushSet {
continue
}
for _, vr := range st.Vars {
if vr.Role.BuffType() != StorageBuff {
continue
}
if vr.StorageBuff != bufIndex {
continue
}
md := vr.Values.ModRegs(vr)
mods = append(mods, md...)
}
}
return mods
}
// AllocTextures allocates images on device memory
func (vs *Vars) AllocTextures(mm *Memory) {
ns := vs.NSets()
for si := vs.StartSet(); si < ns; si++ {
st := vs.SetMap[si]
if st == nil || st.Set == PushSet {
continue
}
for _, vr := range st.Vars {
if vr.Role != TextureRole {
continue
}
vr.Values.AllocTextures(mm)
}
}
}