MCPcopy Create free account
hub / github.com/RavEngine/RavEngine / Material

Method Material

src/Material.cpp:72–241  ·  view source on GitHub ↗

Create a material given a shader. Also registers it in the material manager @param shader the path to the shader */

Source from the content-addressed store, hash-verified

70@param shader the path to the shader
71*/
72Material::Material(const std::string& name, const std::string& vertShaderSrc, const std::string& fragShaderSrc) : name(name) {
73 //check if material is already loaded
74 if (MaterialManager::HasMaterialByName(name)) {
75 throw new runtime_error("Material with name " + name + "is already allocated! Use GetMaterialByName to get it.");
76 }
77
78 struct shader_src{
79 ShaderStage type;
80 std::string source;
81 shader_src(ShaderStage t, const std::string& s) : type(t), source(s){}
82 };
83 vector<shader_src> uncompressed_shaders;
84 {
85 //create shader program
86 Unzipper uz(name + ".bin");
87 auto names = uz.entries();
88 for (const auto& n : names){
89 //uncompress shader
90 std::vector<unsigned char> unzipped_entry;
91 uz.extractEntryToMemory(n.name, unzipped_entry);
92 string name_only = path(n.name).replace_extension("");
93 uncompressed_shaders.emplace_back(stagemap.at(name_only),string(unzipped_entry.begin(),unzipped_entry.end()));
94 }
95 }
96
97 //maps the memory appropriately so uniforms can be set
98 uint32_t constantBufferIndex = 0; //needs to be 1 on Metal
99 LLGL::PipelineLayoutDescriptor pldesc;
100 pldesc.bindings = {
101 LLGL::BindingDescriptor{
102 "Settings", LLGL::ResourceType::Buffer, LLGL::BindFlags::ConstantBuffer,
103 LLGL::StageFlags::VertexStage, //this makes the uniform availabe to the vertex stage. Change to make available to other shaders
104 constantBufferIndex
105 }
106 };
107
108 LLGL::PipelineLayout* pipelinelayout = RenderEngine::GetRenderSystem()->CreatePipelineLayout(pldesc);
109
110 //create the CPU-mirror to update uniforms
111 LLGL::BufferDescriptor constantBufferDesc;
112 constantBufferDesc.size = sizeof(settings);
113 constantBufferDesc.cpuAccessFlags = LLGL::CPUAccessFlags::ReadWrite;
114 constantBufferDesc.bindFlags = LLGL::BindFlags::ConstantBuffer;
115 //constantBufferDesc.miscFlags = LLGL::MiscFlags::DynamicUsage;
116 constantBuffer = RenderEngine::GetRenderSystem()->CreateBuffer(constantBufferDesc, &settings);
117
118 //create a resource heap with the constant buffer
119 LLGL::ResourceHeapDescriptor heapdesc;
120 heapdesc.pipelineLayout = pipelinelayout;
121 heapdesc.resourceViews = { constantBuffer };
122
123 resourceHeap = RenderEngine::GetRenderSystem()->CreateResourceHeap(heapdesc);
124
125 const auto& languages = RenderEngine::GetRenderSystem()->GetRenderingCaps().shadingLanguages;
126
127 LLGL::ShaderDescriptor vertShaderDesc, fragShaderDesc;
128
129 auto path = std::filesystem::current_path();

Callers

nothing calls this directly

Calls 15

findFunction · 0.85
c_strMethod · 0.80
AppendAttributeMethod · 0.80
SetStrideMethod · 0.80
entriesMethod · 0.45
extractEntryToMemoryMethod · 0.45
emplace_backMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
CreatePipelineLayoutMethod · 0.45
CreateBufferMethod · 0.45
CreateResourceHeapMethod · 0.45

Tested by

no test coverage detected