88
99#include "arena.h"
1010#include "util.h"
11+ #include "type.h"
1112
1213#include "../../shady/transform/memory_layout.h"
1314
@@ -42,6 +43,46 @@ VkDescriptorType as_to_descriptor_type(AddressSpace as) {
4243 }
4344}
4445
46+ static void write_value (unsigned char * tgt , const Node * value ) {
47+ IrArena * a = value -> arena ;
48+ switch (value -> tag ) {
49+ case IntLiteral_TAG : {
50+ switch (value -> payload .int_literal .width ) {
51+ case IntTy8 : * ((uint8_t * ) tgt ) = (uint8_t ) (value -> payload .int_literal .value & 0xFF ); break ;
52+ case IntTy16 : * ((uint16_t * ) tgt ) = (uint16_t ) (value -> payload .int_literal .value & 0xFFFF ); break ;
53+ case IntTy32 : * ((uint32_t * ) tgt ) = (uint32_t ) (value -> payload .int_literal .value & 0xFFFFFFFF ); break ;
54+ case IntTy64 : * ((uint64_t * ) tgt ) = (uint64_t ) (value -> payload .int_literal .value ); break ;
55+ }
56+ break ;
57+ }
58+ case Composite_TAG : {
59+ Nodes values = value -> payload .composite .contents ;
60+ const Type * struct_t = value -> payload .composite .type ;
61+ struct_t = get_maybe_nominal_type_body (struct_t );
62+
63+ if (struct_t -> tag == RecordType_TAG ) {
64+ LARRAY (FieldLayout , fields , values .count );
65+ get_record_layout (a , struct_t , fields );
66+ for (size_t i = 0 ; i < values .count ; i ++ ) {
67+ // TypeMemLayout layout = get_mem_layout(value->arena, get_unqualified_type(element->type));
68+ write_value (tgt + fields -> offset_in_bytes , values .nodes [i ]);
69+ }
70+ } else if (struct_t -> tag == ArrType_TAG ) {
71+ for (size_t i = 0 ; i < values .count ; i ++ ) {
72+ TypeMemLayout layout = get_mem_layout (value -> arena , get_unqualified_type (values .nodes [i ]-> type ));
73+ write_value (tgt , values .nodes [i ]);
74+ tgt += layout .size_in_bytes ;
75+ }
76+ } else {
77+ assert (false);
78+ }
79+ break ;
80+ }
81+ default :
82+ assert (false);
83+ }
84+ }
85+
4586static bool extract_resources_layout (VkrSpecProgram * program , VkDescriptorSetLayout layouts []) {
4687 VkDescriptorSetLayoutCreateInfo layout_create_infos [MAX_DESCRIPTOR_SETS ] = { 0 };
4788 Growy * bindings_lists [MAX_DESCRIPTOR_SETS ] = { 0 };
@@ -78,6 +119,8 @@ static bool extract_resources_layout(VkrSpecProgram* program, VkDescriptorSetLay
78119
79120 for (size_t j = 0 ; j < struct_t -> payload .record_type .members .count ; j ++ ) {
80121 const Type * member_t = struct_t -> payload .record_type .members .nodes [j ];
122+ assert (member_t -> tag == PtrType_TAG );
123+ member_t = get_pointee_type (program -> arena , member_t );
81124 TypeMemLayout layout = get_mem_layout (program -> specialized_module -> arena , member_t );
82125
83126 ProgramResourceInfo * constant_res_info = arena_alloc (program -> arena , sizeof (ProgramResourceInfo ));
@@ -93,6 +136,15 @@ static bool extract_resources_layout(VkrSpecProgram* program, VkDescriptorSetLay
93136 res_info -> size += sizeof (void * );
94137
95138 // TODO initial value
139+ Nodes annotations = get_declaration_annotations (decl );
140+ for (size_t k = 0 ; k < annotations .count ; k ++ ) {
141+ const Node * a = annotations .nodes [k ];
142+ if ((strcmp (get_annotation_name (a ), "InitialValue" ) == 0 ) && resolve_to_int_literal (first (get_annotation_values (a )))-> value == j ) {
143+ constant_res_info -> default_data = calloc (1 , layout .size_in_bytes );
144+ write_value (constant_res_info -> default_data , get_annotation_values (a ).nodes [1 ]);
145+ //printf("wowie");
146+ }
147+ }
96148 }
97149
98150 if (vkr_can_import_host_memory (program -> device ))
@@ -394,11 +446,13 @@ static bool prepare_resources(VkrSpecProgram* program) {
394446 resource -> buffer = allocate_buffer_device (program -> device , resource -> size );
395447 }
396448
397- // TODO: initial data!
398- // if (!resource->host_owned)
399- char * zeroes = calloc (1 , resource -> size );
400- copy_to_buffer (resource -> buffer , 0 , zeroes , resource -> size );
401- free (zeroes );
449+ if (resource -> default_data ) {
450+ copy_to_buffer (resource -> buffer , 0 , resource -> default_data , resource -> size );
451+ } else {
452+ char * zeroes = calloc (1 , resource -> size );
453+ copy_to_buffer (resource -> buffer , 0 , zeroes , resource -> size );
454+ free (zeroes );
455+ }
402456
403457 if (resource -> parent ) {
404458 char * dst = resource -> parent -> host_ptr ;
0 commit comments