* @param UI_Context *ctx : the exisitng and already initialized context where this render backend should be added to. * **/
| 115 | * @param UI_Context *ctx : the exisitng and already initialized context where this render backend should be added to. |
| 116 | * **/ |
| 117 | void UI_d3d11_init(UI_Context *ctx, ID3D11Device1* device, ID3D11DeviceContext1* device_context) { |
| 118 | |
| 119 | UI_assert(ctx != nullptr && "UI context *ctx is a null pointer!"); |
| 120 | UI_assert(ctx->initialized && "UI context wasn't initialized!"); |
| 121 | UI_assert(ctx->backend == UI_Render_Backend_Type::None && "A different backend was already initalized!"); |
| 122 | UI_assert(device != nullptr && "D3D11 device is null! check if device is correctly initialized."); |
| 123 | UI_assert(device_context != nullptr && "D3D11 device context is null! check if device is correctly initialized."); |
| 124 | UI_D3D11_Context *d3d_ctx = (UI_D3D11_Context *)malloc(sizeof(UI_D3D11_Context)); |
| 125 | d3d_ctx->device = device; |
| 126 | d3d_ctx->device_ctx = device_context; |
| 127 | ctx->backend = UI_Render_Backend_Type::D3D11; |
| 128 | |
| 129 | // Get factory from device |
| 130 | IDXGIDevice1* DXGIDevice = NULL; |
| 131 | IDXGIAdapter* DXGIAdapter = NULL; |
| 132 | IDXGIFactory* factory = NULL; |
| 133 | |
| 134 | if (device->QueryInterface(IID_PPV_ARGS(&DXGIDevice)) == S_OK) |
| 135 | if (DXGIDevice->GetParent(IID_PPV_ARGS(&DXGIAdapter)) == S_OK) |
| 136 | if (DXGIAdapter->GetParent(IID_PPV_ARGS(&factory)) == S_OK) |
| 137 | { |
| 138 | d3d_ctx->device = device; |
| 139 | d3d_ctx->device_ctx = device_context; |
| 140 | d3d_ctx->factory = factory; |
| 141 | } |
| 142 | if (DXGIDevice) DXGIDevice->Release(); |
| 143 | if (DXGIAdapter) DXGIAdapter->Release(); |
| 144 | |
| 145 | size_t shader_len = strlen(UI_d3d11_shader_string); |
| 146 | |
| 147 | ID3DBlob *vs_blob = UI_d3d11_compile_memory(UI_d3d11_shader_string, shader_len, "VSMain", "vs_5_0"); |
| 148 | err(d3d_ctx->device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(), nullptr, &d3d_ctx->vertex_shader)); |
| 149 | |
| 150 | ID3DBlob *ps_blob = UI_d3d11_compile_memory(UI_d3d11_shader_string, shader_len, "PSMain", "ps_5_0"); |
| 151 | err(d3d_ctx->device->CreatePixelShader(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(), nullptr, &d3d_ctx->pixel_shader)); |
| 152 | |
| 153 | D3D11_BUFFER_DESC constant_buffer_desc = {}; |
| 154 | constant_buffer_desc.ByteWidth = sizeof(UI_D3D11_Constants); |
| 155 | constant_buffer_desc.Usage = D3D11_USAGE_DYNAMIC; |
| 156 | constant_buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
| 157 | constant_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 158 | err(d3d_ctx->device->CreateBuffer(&constant_buffer_desc, nullptr, &d3d_ctx->constant_buffer)); |
| 159 | |
| 160 | D3D11_BUFFER_DESC vertex_buffer_desc = {}; |
| 161 | vertex_buffer_desc.ByteWidth = sizeof(UI_Vertex) * UI_MAX_VERTICES; |
| 162 | vertex_buffer_desc.Usage = D3D11_USAGE_DYNAMIC; |
| 163 | vertex_buffer_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; |
| 164 | vertex_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 165 | vertex_buffer_desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED; |
| 166 | vertex_buffer_desc.StructureByteStride = sizeof(UI_Vertex); |
| 167 | err(d3d_ctx->device->CreateBuffer(&vertex_buffer_desc, nullptr, &d3d_ctx->vertex_buffer)); |
| 168 | |
| 169 | D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc = {}; |
| 170 | SRVDesc.Format = DXGI_FORMAT_UNKNOWN; |
| 171 | SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER; |
| 172 | SRVDesc.Buffer.NumElements = UI_MAX_VERTICES; |
| 173 | err(d3d_ctx->device->CreateShaderResourceView(d3d_ctx->vertex_buffer, &SRVDesc, &d3d_ctx->vertex_SRV)); |
| 174 |
no test coverage detected