src/render/direct3d/SDL_shaders_d3d.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Jun 2019 13:56:42 -0700
changeset 12860 0f52dd40abe5
parent 12503 806492103856
child 13422 fd6a12de91c7
permissions -rw-r--r--
Worked around "Undefined symbol: ___isPlatformVersionAtLeast()" link error on Xcode 11 beta
slouken@11702
     1
/*
slouken@11702
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
slouken@11702
     4
slouken@11702
     5
  This software is provided 'as-is', without any express or implied
slouken@11702
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@11702
     7
  arising from the use of this software.
slouken@11702
     8
slouken@11702
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@11702
    10
  including commercial applications, and to alter it and redistribute it
slouken@11702
    11
  freely, subject to the following restrictions:
slouken@11702
    12
slouken@11702
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@11702
    14
     claim that you wrote the original software. If you use this software
slouken@11702
    15
     in a product, an acknowledgment in the product documentation would be
slouken@11702
    16
     appreciated but is not required.
slouken@11702
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@11702
    18
     misrepresented as being the original software.
slouken@11702
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@11702
    20
*/
slouken@11702
    21
#include "../../SDL_internal.h"
slouken@11702
    22
slouken@11702
    23
#include "SDL_render.h"
slouken@11702
    24
#include "SDL_system.h"
slouken@11702
    25
slouken@11702
    26
#if SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED
slouken@11702
    27
slouken@11702
    28
#include "../../core/windows/SDL_windows.h"
slouken@11702
    29
slouken@11702
    30
#include <d3d9.h>
slouken@11702
    31
slouken@11702
    32
#include "SDL_shaders_d3d.h"
slouken@11702
    33
slouken@11702
    34
/* The shaders here were compiled with:
slouken@11702
    35
slouken@11702
    36
       fxc /T ps_2_0 /Fo"<OUTPUT FILE>" "<INPUT FILE>"
slouken@11702
    37
slouken@11702
    38
   Shader object code was converted to a list of DWORDs via the following
slouken@11702
    39
   *nix style command (available separately from Windows + MSVC):
slouken@11702
    40
slouken@11702
    41
     hexdump -v -e '6/4 "0x%08.8x, " "\n"' <FILE>
slouken@11702
    42
*/
slouken@11702
    43
slouken@11702
    44
/* --- D3D9_PixelShader_YUV_JPEG.hlsl ---
slouken@11702
    45
    Texture2D theTextureY : register(t0);
slouken@11702
    46
    Texture2D theTextureU : register(t1);
slouken@11702
    47
    Texture2D theTextureV : register(t2);
slouken@11702
    48
    SamplerState theSampler = sampler_state
slouken@11702
    49
    {
slouken@11702
    50
        addressU = Clamp;
slouken@11702
    51
        addressV = Clamp;
slouken@11702
    52
        mipfilter = NONE;
slouken@11702
    53
        minfilter = LINEAR;
slouken@11702
    54
        magfilter = LINEAR;
slouken@11702
    55
    };
slouken@11702
    56
slouken@11702
    57
    struct PixelShaderInput
slouken@11702
    58
    {
slouken@11702
    59
        float4 pos : SV_POSITION;
slouken@11702
    60
        float2 tex : TEXCOORD0;
slouken@11702
    61
        float4 color : COLOR0;
slouken@11702
    62
    };
slouken@11702
    63
slouken@11702
    64
    float4 main(PixelShaderInput input) : SV_TARGET
slouken@11702
    65
    {
slouken@11702
    66
        const float3 offset = {0.0, -0.501960814, -0.501960814};
slouken@11702
    67
        const float3 Rcoeff = {1.0000,  0.0000,  1.4020};
slouken@11702
    68
        const float3 Gcoeff = {1.0000, -0.3441, -0.7141};
slouken@11702
    69
        const float3 Bcoeff = {1.0000,  1.7720,  0.0000};
slouken@11702
    70
slouken@11702
    71
        float4 Output;
slouken@11702
    72
slouken@11702
    73
        float3 yuv;
slouken@11702
    74
        yuv.x = theTextureY.Sample(theSampler, input.tex).r;
slouken@11702
    75
        yuv.y = theTextureU.Sample(theSampler, input.tex).r;
slouken@11702
    76
        yuv.z = theTextureV.Sample(theSampler, input.tex).r;
slouken@11702
    77
slouken@11702
    78
        yuv += offset;
slouken@11702
    79
        Output.r = dot(yuv, Rcoeff);
slouken@11702
    80
        Output.g = dot(yuv, Gcoeff);
slouken@11702
    81
        Output.b = dot(yuv, Bcoeff);
slouken@11702
    82
        Output.a = 1.0f;
slouken@11702
    83
slouken@11702
    84
        return Output * input.color;
slouken@11702
    85
    }
slouken@11702
    86
*/
slouken@11702
    87
static const DWORD D3D9_PixelShader_YUV_JPEG[] = {
slouken@11702
    88
    0xffff0200, 0x0044fffe, 0x42415443, 0x0000001c, 0x000000d7, 0xffff0200,
slouken@11702
    89
    0x00000003, 0x0000001c, 0x00000100, 0x000000d0, 0x00000058, 0x00010003,
slouken@11702
    90
    0x00000001, 0x00000070, 0x00000000, 0x00000080, 0x00020003, 0x00000001,
slouken@11702
    91
    0x00000098, 0x00000000, 0x000000a8, 0x00000003, 0x00000001, 0x000000c0,
slouken@11702
    92
    0x00000000, 0x53656874, 0x6c706d61, 0x742b7265, 0x65546568, 0x72757478,
slouken@11702
    93
    0xab005565, 0x00070004, 0x00040001, 0x00000001, 0x00000000, 0x53656874,
slouken@11702
    94
    0x6c706d61, 0x742b7265, 0x65546568, 0x72757478, 0xab005665, 0x00070004,
slouken@11702
    95
    0x00040001, 0x00000001, 0x00000000, 0x53656874, 0x6c706d61, 0x742b7265,
slouken@11702
    96
    0x65546568, 0x72757478, 0xab005965, 0x00070004, 0x00040001, 0x00000001,
slouken@11702
    97
    0x00000000, 0x325f7370, 0x4d00305f, 0x6f726369, 0x74666f73, 0x29522820,
slouken@11702
    98
    0x534c4820, 0x6853204c, 0x72656461, 0x6d6f4320, 0x656c6970, 0x2e362072,
slouken@11702
    99
    0x36392e33, 0x312e3030, 0x34383336, 0xababab00, 0x05000051, 0xa00f0000,
slouken@11702
   100
    0x00000000, 0xbf008081, 0xbf008081, 0x3f800000, 0x05000051, 0xa00f0001,
slouken@11702
   101
    0x3f800000, 0x00000000, 0x3fb374bc, 0x00000000, 0x05000051, 0xa00f0002,
slouken@11702
   102
    0x3f800000, 0xbeb02de0, 0xbf36cf42, 0x00000000, 0x05000051, 0xa00f0003,
slouken@11702
   103
    0x3f800000, 0x3fe2d0e5, 0x00000000, 0x00000000, 0x0200001f, 0x80000000,
slouken@11702
   104
    0xb0030000, 0x0200001f, 0x80000000, 0x900f0000, 0x0200001f, 0x90000000,
slouken@11702
   105
    0xa00f0800, 0x0200001f, 0x90000000, 0xa00f0801, 0x0200001f, 0x90000000,
slouken@11702
   106
    0xa00f0802, 0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000042,
slouken@11702
   107
    0x800f0001, 0xb0e40000, 0xa0e40801, 0x03000042, 0x800f0002, 0xb0e40000,
slouken@11702
   108
    0xa0e40802, 0x02000001, 0x80020000, 0x80000001, 0x02000001, 0x80040000,
slouken@11702
   109
    0x80000002, 0x03000002, 0x80070000, 0x80e40000, 0xa0e40000, 0x03000008,
slouken@11702
   110
    0x80010001, 0x80e40000, 0xa0e40001, 0x03000008, 0x80020001, 0x80e40000,
slouken@11702
   111
    0xa0e40002, 0x0400005a, 0x80040001, 0x80e40000, 0xa0e40003, 0xa0aa0003,
slouken@11702
   112
    0x02000001, 0x80080001, 0xa0ff0000, 0x03000005, 0x800f0000, 0x80e40001,
slouken@11702
   113
    0x90e40000, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff
slouken@11702
   114
};
slouken@11702
   115
slouken@11702
   116
/* --- D3D9_PixelShader_YUV_BT601.hlsl ---
slouken@11702
   117
    Texture2D theTextureY : register(t0);
slouken@11702
   118
    Texture2D theTextureU : register(t1);
slouken@11702
   119
    Texture2D theTextureV : register(t2);
slouken@11702
   120
    SamplerState theSampler = sampler_state
slouken@11702
   121
    {
slouken@11702
   122
        addressU = Clamp;
slouken@11702
   123
        addressV = Clamp;
slouken@11702
   124
        mipfilter = NONE;
slouken@11702
   125
        minfilter = LINEAR;
slouken@11702
   126
        magfilter = LINEAR;
slouken@11702
   127
    };
slouken@11702
   128
slouken@11702
   129
    struct PixelShaderInput
slouken@11702
   130
    {
slouken@11702
   131
        float4 pos : SV_POSITION;
slouken@11702
   132
        float2 tex : TEXCOORD0;
slouken@11702
   133
        float4 color : COLOR0;
slouken@11702
   134
    };
slouken@11702
   135
slouken@11702
   136
    float4 main(PixelShaderInput input) : SV_TARGET
slouken@11702
   137
    {
slouken@11702
   138
        const float3 offset = {-0.0627451017, -0.501960814, -0.501960814};
slouken@11702
   139
        const float3 Rcoeff = {1.1644,  0.0000,  1.5960};
slouken@11702
   140
        const float3 Gcoeff = {1.1644, -0.3918, -0.8130};
slouken@11702
   141
        const float3 Bcoeff = {1.1644,  2.0172,  0.0000};
slouken@11702
   142
slouken@11702
   143
        float4 Output;
slouken@11702
   144
slouken@11702
   145
        float3 yuv;
slouken@11702
   146
        yuv.x = theTextureY.Sample(theSampler, input.tex).r;
slouken@11702
   147
        yuv.y = theTextureU.Sample(theSampler, input.tex).r;
slouken@11702
   148
        yuv.z = theTextureV.Sample(theSampler, input.tex).r;
slouken@11702
   149
slouken@11702
   150
        yuv += offset;
slouken@11702
   151
        Output.r = dot(yuv, Rcoeff);
slouken@11702
   152
        Output.g = dot(yuv, Gcoeff);
slouken@11702
   153
        Output.b = dot(yuv, Bcoeff);
slouken@11702
   154
        Output.a = 1.0f;
slouken@11702
   155
slouken@11702
   156
        return Output * input.color;
slouken@11702
   157
    }
slouken@11702
   158
*/
slouken@11702
   159
static const DWORD D3D9_PixelShader_YUV_BT601[] = {
slouken@11702
   160
    0xffff0200, 0x0044fffe, 0x42415443, 0x0000001c, 0x000000d7, 0xffff0200,
slouken@11702
   161
    0x00000003, 0x0000001c, 0x00000100, 0x000000d0, 0x00000058, 0x00010003,
slouken@11702
   162
    0x00000001, 0x00000070, 0x00000000, 0x00000080, 0x00020003, 0x00000001,
slouken@11702
   163
    0x00000098, 0x00000000, 0x000000a8, 0x00000003, 0x00000001, 0x000000c0,
slouken@11702
   164
    0x00000000, 0x53656874, 0x6c706d61, 0x742b7265, 0x65546568, 0x72757478,
slouken@11702
   165
    0xab005565, 0x00070004, 0x00040001, 0x00000001, 0x00000000, 0x53656874,
slouken@11702
   166
    0x6c706d61, 0x742b7265, 0x65546568, 0x72757478, 0xab005665, 0x00070004,
slouken@11702
   167
    0x00040001, 0x00000001, 0x00000000, 0x53656874, 0x6c706d61, 0x742b7265,
slouken@11702
   168
    0x65546568, 0x72757478, 0xab005965, 0x00070004, 0x00040001, 0x00000001,
slouken@11702
   169
    0x00000000, 0x325f7370, 0x4d00305f, 0x6f726369, 0x74666f73, 0x29522820,
slouken@11702
   170
    0x534c4820, 0x6853204c, 0x72656461, 0x6d6f4320, 0x656c6970, 0x2e362072,
slouken@11702
   171
    0x36392e33, 0x312e3030, 0x34383336, 0xababab00, 0x05000051, 0xa00f0000,
slouken@11702
   172
    0xbd808081, 0xbf008081, 0xbf008081, 0x3f800000, 0x05000051, 0xa00f0001,
slouken@11702
   173
    0x3f950b0f, 0x00000000, 0x3fcc49ba, 0x00000000, 0x05000051, 0xa00f0002,
slouken@11702
   174
    0x3f950b0f, 0xbec89a02, 0xbf5020c5, 0x00000000, 0x05000051, 0xa00f0003,
slouken@11702
   175
    0x3f950b0f, 0x400119ce, 0x00000000, 0x00000000, 0x0200001f, 0x80000000,
slouken@11702
   176
    0xb0030000, 0x0200001f, 0x80000000, 0x900f0000, 0x0200001f, 0x90000000,
slouken@11702
   177
    0xa00f0800, 0x0200001f, 0x90000000, 0xa00f0801, 0x0200001f, 0x90000000,
slouken@11702
   178
    0xa00f0802, 0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000042,
slouken@11702
   179
    0x800f0001, 0xb0e40000, 0xa0e40801, 0x03000042, 0x800f0002, 0xb0e40000,
slouken@11702
   180
    0xa0e40802, 0x02000001, 0x80020000, 0x80000001, 0x02000001, 0x80040000,
slouken@11702
   181
    0x80000002, 0x03000002, 0x80070000, 0x80e40000, 0xa0e40000, 0x03000008,
slouken@11702
   182
    0x80010001, 0x80e40000, 0xa0e40001, 0x03000008, 0x80020001, 0x80e40000,
slouken@11702
   183
    0xa0e40002, 0x0400005a, 0x80040001, 0x80e40000, 0xa0e40003, 0xa0aa0003,
slouken@11702
   184
    0x02000001, 0x80080001, 0xa0ff0000, 0x03000005, 0x800f0000, 0x80e40001,
slouken@11702
   185
    0x90e40000, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff
slouken@11702
   186
};
slouken@11702
   187
slouken@11702
   188
/* --- D3D9_PixelShader_YUV_BT709.hlsl ---
slouken@11702
   189
    Texture2D theTextureY : register(t0);
slouken@11702
   190
    Texture2D theTextureU : register(t1);
slouken@11702
   191
    Texture2D theTextureV : register(t2);
slouken@11702
   192
    SamplerState theSampler = sampler_state
slouken@11702
   193
    {
slouken@11702
   194
        addressU = Clamp;
slouken@11702
   195
        addressV = Clamp;
slouken@11702
   196
        mipfilter = NONE;
slouken@11702
   197
        minfilter = LINEAR;
slouken@11702
   198
        magfilter = LINEAR;
slouken@11702
   199
    };
slouken@11702
   200
slouken@11702
   201
    struct PixelShaderInput
slouken@11702
   202
    {
slouken@11702
   203
        float4 pos : SV_POSITION;
slouken@11702
   204
        float2 tex : TEXCOORD0;
slouken@11702
   205
        float4 color : COLOR0;
slouken@11702
   206
    };
slouken@11702
   207
slouken@11702
   208
    float4 main(PixelShaderInput input) : SV_TARGET
slouken@11702
   209
    {
slouken@11702
   210
        const float3 offset = {-0.0627451017, -0.501960814, -0.501960814};
slouken@11702
   211
        const float3 Rcoeff = {1.1644,  0.0000,  1.7927};
slouken@11702
   212
        const float3 Gcoeff = {1.1644, -0.2132, -0.5329};
slouken@11702
   213
        const float3 Bcoeff = {1.1644,  2.1124,  0.0000};
slouken@11702
   214
slouken@11702
   215
        float4 Output;
slouken@11702
   216
slouken@11702
   217
        float3 yuv;
slouken@11702
   218
        yuv.x = theTextureY.Sample(theSampler, input.tex).r;
slouken@11702
   219
        yuv.y = theTextureU.Sample(theSampler, input.tex).r;
slouken@11702
   220
        yuv.z = theTextureV.Sample(theSampler, input.tex).r;
slouken@11702
   221
slouken@11702
   222
        yuv += offset;
slouken@11702
   223
        Output.r = dot(yuv, Rcoeff);
slouken@11702
   224
        Output.g = dot(yuv, Gcoeff);
slouken@11702
   225
        Output.b = dot(yuv, Bcoeff);
slouken@11702
   226
        Output.a = 1.0f;
slouken@11702
   227
slouken@11702
   228
        return Output * input.color;
slouken@11702
   229
    }
slouken@11702
   230
*/
slouken@11702
   231
static const DWORD D3D9_PixelShader_YUV_BT709[] = {
slouken@11702
   232
    0xffff0200, 0x0044fffe, 0x42415443, 0x0000001c, 0x000000d7, 0xffff0200,
slouken@11702
   233
    0x00000003, 0x0000001c, 0x00000100, 0x000000d0, 0x00000058, 0x00010003,
slouken@11702
   234
    0x00000001, 0x00000070, 0x00000000, 0x00000080, 0x00020003, 0x00000001,
slouken@11702
   235
    0x00000098, 0x00000000, 0x000000a8, 0x00000003, 0x00000001, 0x000000c0,
slouken@11702
   236
    0x00000000, 0x53656874, 0x6c706d61, 0x742b7265, 0x65546568, 0x72757478,
slouken@11702
   237
    0xab005565, 0x00070004, 0x00040001, 0x00000001, 0x00000000, 0x53656874,
slouken@11702
   238
    0x6c706d61, 0x742b7265, 0x65546568, 0x72757478, 0xab005665, 0x00070004,
slouken@11702
   239
    0x00040001, 0x00000001, 0x00000000, 0x53656874, 0x6c706d61, 0x742b7265,
slouken@11702
   240
    0x65546568, 0x72757478, 0xab005965, 0x00070004, 0x00040001, 0x00000001,
slouken@11702
   241
    0x00000000, 0x325f7370, 0x4d00305f, 0x6f726369, 0x74666f73, 0x29522820,
slouken@11702
   242
    0x534c4820, 0x6853204c, 0x72656461, 0x6d6f4320, 0x656c6970, 0x2e362072,
slouken@11702
   243
    0x36392e33, 0x312e3030, 0x34383336, 0xababab00, 0x05000051, 0xa00f0000,
slouken@11702
   244
    0xbd808081, 0xbf008081, 0xbf008081, 0x3f800000, 0x05000051, 0xa00f0001,
slouken@11702
   245
    0x3f950b0f, 0x00000000, 0x3fe57732, 0x00000000, 0x05000051, 0xa00f0002,
slouken@11702
   246
    0x3f950b0f, 0xbe5a511a, 0xbf086c22, 0x00000000, 0x05000051, 0xa00f0003,
slouken@11702
   247
    0x3f950b0f, 0x40073190, 0x00000000, 0x00000000, 0x0200001f, 0x80000000,
slouken@11702
   248
    0xb0030000, 0x0200001f, 0x80000000, 0x900f0000, 0x0200001f, 0x90000000,
slouken@11702
   249
    0xa00f0800, 0x0200001f, 0x90000000, 0xa00f0801, 0x0200001f, 0x90000000,
slouken@11702
   250
    0xa00f0802, 0x03000042, 0x800f0000, 0xb0e40000, 0xa0e40800, 0x03000042,
slouken@11702
   251
    0x800f0001, 0xb0e40000, 0xa0e40801, 0x03000042, 0x800f0002, 0xb0e40000,
slouken@11702
   252
    0xa0e40802, 0x02000001, 0x80020000, 0x80000001, 0x02000001, 0x80040000,
slouken@11702
   253
    0x80000002, 0x03000002, 0x80070000, 0x80e40000, 0xa0e40000, 0x03000008,
slouken@11702
   254
    0x80010001, 0x80e40000, 0xa0e40001, 0x03000008, 0x80020001, 0x80e40000,
slouken@11702
   255
    0xa0e40002, 0x0400005a, 0x80040001, 0x80e40000, 0xa0e40003, 0xa0aa0003,
slouken@11702
   256
    0x02000001, 0x80080001, 0xa0ff0000, 0x03000005, 0x800f0000, 0x80e40001,
slouken@11702
   257
    0x90e40000, 0x02000001, 0x800f0800, 0x80e40000, 0x0000ffff
slouken@11702
   258
};
slouken@11702
   259
slouken@11702
   260
slouken@11702
   261
static const DWORD *D3D9_shaders[] = {
slouken@11702
   262
    D3D9_PixelShader_YUV_JPEG,
slouken@11702
   263
    D3D9_PixelShader_YUV_BT601,
slouken@11702
   264
    D3D9_PixelShader_YUV_BT709,
slouken@11702
   265
};
slouken@11702
   266
slouken@11702
   267
HRESULT D3D9_CreatePixelShader(IDirect3DDevice9 *d3dDevice, D3D9_Shader shader, IDirect3DPixelShader9 **pixelShader)
slouken@11702
   268
{
slouken@11702
   269
    return IDirect3DDevice9_CreatePixelShader(d3dDevice, D3D9_shaders[shader], pixelShader);
slouken@11702
   270
}
slouken@11702
   271
slouken@11702
   272
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
slouken@11702
   273
slouken@11702
   274
/* vi: set ts=4 sw=4 expandtab: */