bool IndexBuffer::initialize(GraphicsDevice& device, UsageType usage, void* initialData, unsigned int size) { assert(data.buffer == nullptr); assert(device.getDeviceData().d3dContext != nullptr); ID3D11Device* d3dDevice = device.getDeviceData().d3dDevice; D3D11_BUFFER_DESC bufferDesc; bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; bufferDesc.CPUAccessFlags = 0; bufferDesc.MiscFlags = 0; bufferDesc.ByteWidth = size; bufferDesc.StructureByteStride = 0; bufferDesc.Usage = static_cast<D3D11_USAGE>(usage); if(usage == UsageType::Dynamic) bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; D3D11_SUBRESOURCE_DATA bufferData; bufferData.pSysMem = initialData; bufferData.SysMemPitch = 0; bufferData.SysMemSlicePitch = 0; D3D11_SUBRESOURCE_DATA* bufferDataPtr = nullptr; if(initialData != nullptr) bufferDataPtr = &bufferData; if(FAILED(d3dDevice->CreateBuffer(&bufferDesc, bufferDataPtr, &data.buffer))) { release(); return false; } data.context = device.getDeviceData().d3dContext; return true; }
bool SamplerState::initialize(GraphicsDevice& device, SamplerStateDescription desc) { D3D11_SAMPLER_DESC samplerDesc; samplerDesc.AddressU = static_cast<D3D11_TEXTURE_ADDRESS_MODE>(desc.addressU); samplerDesc.AddressV = static_cast<D3D11_TEXTURE_ADDRESS_MODE>(desc.addressV); samplerDesc.AddressW = static_cast<D3D11_TEXTURE_ADDRESS_MODE>(desc.addressW); samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerDesc.MaxAnisotropy = desc.maxAnisotropy; samplerDesc.MaxLOD = desc.maxMip; samplerDesc.MinLOD = desc.minMip; samplerDesc.MipLODBias = desc.mipBias; if(desc.minFilter == SamplerFilter::Anisotropic || desc.magFilter == SamplerFilter::Anisotropic || desc.mipFilter == SamplerFilter::Anisotropic) samplerDesc.Filter = D3D11_FILTER_ANISOTROPIC; if(desc.minFilter == desc.magFilter && desc.minFilter == desc.mipFilter) { switch(desc.minFilter) { case SamplerFilter::Point: samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; break; case SamplerFilter::Linear: samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; break; } } else if(desc.minFilter == desc.magFilter) { switch(desc.minFilter) { case SamplerFilter::Point: samplerDesc.Filter = D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR; break; case SamplerFilter::Linear: samplerDesc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT; break; } } else if(desc.magFilter == desc.mipFilter) { switch(desc.minFilter) { case SamplerFilter::Point: samplerDesc.Filter = D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR; break; case SamplerFilter::Linear: samplerDesc.Filter = D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT; break; } } else { switch(desc.minFilter) { case SamplerFilter::Point: samplerDesc.Filter = D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT; break; case SamplerFilter::Linear: samplerDesc.Filter = D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR; break; } } if(FAILED(device.getDeviceData().d3dDevice->CreateSamplerState(&samplerDesc, &data.samplerState))) return false; return true; }