Bool Resource::Layout::Create(const Description& description) { // Prepare the D3D11 input layout D3D11_INPUT_ELEMENT_DESC* inputLayout = new D3D11_INPUT_ELEMENT_DESC[description.Elements.Count]; // Fill in all of the elements for(UInt i = 0; i < description.Elements.Count; ++i) { inputLayout[i].SemanticName = description.Elements[i].Name.c_str(); inputLayout[i].SemanticIndex = description.Elements[i].Index; inputLayout[i].Format = (DXGI_FORMAT)description.Elements[i].Format; inputLayout[i].InputSlot = description.Elements[i].Slot; inputLayout[i].AlignedByteOffset = description.Elements[i].Offset; // No support for instancing yet inputLayout[i].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; //description.Elements[i].Classification; inputLayout[i].InstanceDataStepRate = 0; //description.Elements[i].InstanceStepRate; } // Get the device handle Graphics::Device& device = Graphics::Manager::Singleton->Device; File shaderFile; // Open the shader associated with this input layout to retrieve its input signature Bool result = shaderFile.Open(description.Shader, File::Mode::Read); if(FAILED(result)) { return false; } // Load the shader data shaderFile.Allocate(shaderFile.FileSize + 1); shaderFile.Load(shaderFile.FileSize); Log::Singleton->Write("Creating input layout with (" + description.Shader + ") vertex shader"); // Create the vertex input layout HRESULT hresult = device->CreateInputLayout(inputLayout, description.Elements.Count, shaderFile.Data, shaderFile.Size, &Handle); if(FAILED(hresult)) { return false; } // We're done with the file for now shaderFile.Close(); // Success! return true; }
Bool Resource::Layout::CreateFromFile(const String& filename) { File layoutFile; // Open the file containing the layout layoutFile.Open(filename, File::Mode::Read); // Prepare a description to create the layout Resource::Layout::Description layoutDesc; String name; // Get the resource name of the input layout UInt length = layoutFile.Read<UByte>(); name += layoutFile.Read<Char>(length); // Read the number of input elements UInt elements = layoutFile.Read<UByte>(); layoutDesc.Elements.Reserve(elements); // Read each input layout for(UInt i = 0; i < elements; ++i) { // Write the size of the string then the string to the file length = layoutFile.Read<UByte>(); layoutDesc.Elements[i].Name += layoutFile.Read<Char>(length); // Simply write the rest of the element to disk layoutDesc.Elements[i].Index = layoutFile.Read<UInt>(); layoutDesc.Elements[i].Format = layoutFile.Read<Texture::Format>(); layoutDesc.Elements[i].Slot = layoutFile.Read<UInt>(); layoutDesc.Elements[i].Offset = layoutFile.Read<UInt>(); } // Read the shader blob length = layoutFile.Read<UInt>(); layoutFile.Allocate(length + 1); layoutFile.Load(length); // Prepare the D3D11 input layout D3D11_INPUT_ELEMENT_DESC* inputLayout = new D3D11_INPUT_ELEMENT_DESC[elements]; // Fill in all of the elements for(UInt i = 0; i < elements; ++i) { inputLayout[i].SemanticName = layoutDesc.Elements[i].Name.c_str(); inputLayout[i].SemanticIndex = layoutDesc.Elements[i].Index; inputLayout[i].Format = (DXGI_FORMAT)layoutDesc.Elements[i].Format; inputLayout[i].InputSlot = layoutDesc.Elements[i].Slot; inputLayout[i].AlignedByteOffset = layoutDesc.Elements[i].Offset; // No support for instancing yet inputLayout[i].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; //description.Elements[i].Classification; inputLayout[i].InstanceDataStepRate = 0; //description.Elements[i].InstanceStepRate; } // Get the device handle Graphics::Device& device = Graphics::Manager::Singleton->Device; Log::Singleton->Write("Creating input layout (" + name + ")"); // Create the vertex input layout HRESULT result = device->CreateInputLayout(inputLayout, elements, layoutFile.Data, layoutFile.Size, &Handle); if(FAILED(result)) { return false; } // And done layoutFile.Close(); // Whoo return true; }