MCPcopy Create free account
hub / github.com/crownengine/crown / compile

Function compile

src/resource/material_resource.cpp:350–445  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

348 }
349
350 s32 compile(CompileOptions &opts)
351 {
352 Buffer buf = opts.read();
353 TempAllocator4096 ta;
354 JsonObject obj(ta);
355 RETURN_IF_ERROR(sjson::parse(obj, buf));
356
357 Data data(default_allocator());
358 Vector<StringView> defines(default_allocator());
359 Vector<ShaderResource::Sampler> samplers_meta(default_allocator());
360 Buffer shader_code(default_allocator());
361 FileBuffer shader_fb(shader_code);
362 StringView shader_name;
363 StringId32 shader_id;
364 DynamicString shader_library(ta);
365 DynamicString shader(ta);
366
367 RETURN_IF_ERROR(sjson::parse_string(shader, obj["shader"]));
368 shader_name_defines(shader_name, defines, shader.c_str());
369
370 s32 err = shader_compiler::compile_variant(shader_fb, &data.uniforms_meta, shader_library, shader_id, shader_name, defines, opts, true, &samplers_meta);
371 ENSURE_OR_RETURN(MATERIAL_RESOURCE, err == 0, opts);
372 opts.add_requirement("shader", shader_library.c_str());
373
374 data.add_shader_uniforms();
375
376 // Parse uniforms and textures.
377 if (json_object::has(obj, "textures")) {
378 err = parse_textures(data, obj["textures"], opts);
379 ENSURE_OR_RETURN(MATERIAL_RESOURCE, err == 0, opts);
380 }
381 for (u32 i = 0; i < array::size(data.textures); ++i) {
382 u32 si = 0;
383 for (; si < vector::size(samplers_meta); ++si) {
384 if (samplers_meta[si].name == data.textures[i].name._id)
385 break;
386 }
387
388 if (si == vector::size(samplers_meta)) {
389 logw(MATERIAL_RESOURCE
390 , "Texture references inactive or unknown sampler '%s' in shader '%s'"
391 , (char *)array::begin(data.names) + data.textures[i].sampler_name_offset
392 , shader.c_str()
393 );
394 }
395 }
396 if (json_object::has(obj, "uniforms")) {
397 err = parse_uniforms(data, obj["uniforms"], opts);
398 ENSURE_OR_RETURN(MATERIAL_RESOURCE, err == 0, opts);
399 }
400
401 data.alloc_blob();
402
403 MaterialResource mr;
404 mr.version = RESOURCE_HEADER(RESOURCE_VERSION_MATERIAL);
405 mr.shader = shader_id;
406 mr.num_textures = array::size(data.textures);
407 mr.texture_data_offset = sizeof(mr);

Callers

nothing calls this directly

Calls 15

shader_name_definesFunction · 0.85
compile_variantFunction · 0.85
parse_texturesFunction · 0.85
parse_uniformsFunction · 0.85
add_requirementMethod · 0.80
add_shader_uniformsMethod · 0.80
alloc_blobMethod · 0.80
parseFunction · 0.70
parse_stringFunction · 0.50
hasFunction · 0.50
sizeFunction · 0.50
beginFunction · 0.50

Tested by

no test coverage detected