| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any damages |
| 7 | arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any purpose, |
| 10 | including commercial applications, and to alter it and redistribute it |
| 11 | freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must not |
| 14 | claim that you wrote the original software. If you use this software |
| 15 | in a product, an acknowledgment in the product documentation would be |
| 16 | appreciated but is not required. |
| 17 | 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | misrepresented as being the original software. |
| 19 | 3. This notice may not be removed or altered from any source distribution. |
| 20 | */ |
| 21 | #include "SDL_internal.h" |
| 22 | |
| 23 | #ifdef SDL_VIDEO_RENDER_D3D11 |
| 24 | |
| 25 | #define COBJMACROS |
| 26 | #include "../../core/windows/SDL_windows.h" |
| 27 | #include <d3d11_1.h> |
| 28 | |
| 29 | #include "SDL_shaders_d3d11.h" |
| 30 | |
| 31 | #define SDL_COMPOSE_ERROR(str) SDL_STRINGIFY_ARG(__FUNCTION__) ", " str |
| 32 | |
| 33 | #if SDL_WINAPI_FAMILY_PHONE |
| 34 | #error Need to build shaders with level_9_3 |
| 35 | #endif |
| 36 | |
| 37 | // The shaders here were compiled with compile_shaders.bat |
| 38 | |
| 39 | #define g_main D3D11_PixelShader_Colors |
| 40 | #include "D3D11_PixelShader_Colors.h" |
| 41 | #undef g_main |
| 42 | |
| 43 | #define g_main D3D11_PixelShader_Textures |
| 44 | #include "D3D11_PixelShader_Textures.h" |
| 45 | #undef g_main |
| 46 | |
| 47 | #define g_main D3D11_PixelShader_Advanced |
| 48 | #include "D3D11_PixelShader_Advanced.h" |
| 49 | #undef g_main |
| 50 | |
| 51 | #define g_main D3D11_VertexShader |
| 52 | #include "D3D11_VertexShader.h" |
| 53 | #undef g_main |
| 54 | |
| 55 | |
| 56 | static struct |
| 57 | { |
| 58 | const void *shader_data; |
| 59 | SIZE_T shader_size; |
| 60 | } D3D11_shaders[] = { |
| 61 | { NULL, 0 }, |
| 62 | { D3D11_PixelShader_Colors, sizeof(D3D11_PixelShader_Colors) }, |
| 63 | { D3D11_PixelShader_Textures, sizeof(D3D11_PixelShader_Textures) }, |
| 64 | { D3D11_PixelShader_Advanced, sizeof(D3D11_PixelShader_Advanced) }, |
| 65 | }; |
| 66 | SDL_COMPILE_TIME_ASSERT(D3D11_shaders, SDL_arraysize(D3D11_shaders) == NUM_SHADERS); |
| 67 | |
| 68 | bool D3D11_CreateVertexShader(ID3D11Device1 *d3dDevice, ID3D11VertexShader **vertexShader, ID3D11InputLayout **inputLayout) |
| 69 | { |
| 70 | // Declare how the input layout for SDL's vertex shader will be setup: |
| 71 | const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = { |
| 72 | { "POSITION" , 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 73 | { "TEXCOORD" , 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 74 | { "COLOR" , 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
| 75 | }; |
| 76 | HRESULT result; |
| 77 | |
| 78 | // Load in SDL's one and only vertex shader: |
| 79 | result = ID3D11Device_CreateVertexShader(d3dDevice, |
| 80 | D3D11_VertexShader, |
| 81 | sizeof(D3D11_VertexShader), |
| 82 | NULL, |
| 83 | vertexShader); |
| 84 | if (FAILED(result)) { |
| 85 | return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateVertexShader" ), result); |
| 86 | } |
| 87 | |
| 88 | // Create an input layout for SDL's vertex shader: |
| 89 | result = ID3D11Device_CreateInputLayout(d3dDevice, |
| 90 | vertexDesc, |
| 91 | ARRAYSIZE(vertexDesc), |
| 92 | D3D11_VertexShader, |
| 93 | sizeof(D3D11_VertexShader), |
| 94 | inputLayout); |
| 95 | if (FAILED(result)) { |
| 96 | return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreateInputLayout" ), result); |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool D3D11_CreatePixelShader(ID3D11Device1 *d3dDevice, D3D11_Shader shader, ID3D11PixelShader **pixelShader) |
| 102 | { |
| 103 | HRESULT result; |
| 104 | |
| 105 | result = ID3D11Device_CreatePixelShader(d3dDevice, |
| 106 | D3D11_shaders[shader].shader_data, |
| 107 | D3D11_shaders[shader].shader_size, |
| 108 | NULL, |
| 109 | pixelShader); |
| 110 | if (FAILED(result)) { |
| 111 | return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device1::CreatePixelShader" ), result); |
| 112 | } |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | #endif // SDL_VIDEO_RENDER_D3D11 |
| 117 | |