Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // note: to keep things minimal, this example is using a "trick" to draw a textured quad without using a vertex buffer.
- // @d7samurai
- #pragma once
- #pragma comment(lib, "d3d11")
- #include <windows.h>
- #include <wrl.h>
- #include <d3d11_1.h>
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- byte* VertexShaderData; // pointer to compiled vertex shader data
- int VertexShaderDataSize; // size of compiled vertex shader data
- byte* PixelShaderData; // pointer to compiled pixel shader data
- int PixelShaderDataSize; // size of compiled pixel shader data
- byte* TextureData; // pointer to bitmap data
- int TextureWidth; // width of bitmap
- int TextureHeight; // height of bitmap
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- HWND Window;
- Microsoft::WRL::ComPtr<IDXGISwapChain1> SwapChain;
- Microsoft::WRL::ComPtr<ID3D11Device1> Device;
- Microsoft::WRL::ComPtr<ID3D11DeviceContext1> DeviceContext;
- Microsoft::WRL::ComPtr<ID3D11RenderTargetView> RenderTargetView;
- Microsoft::WRL::ComPtr<ID3D11VertexShader> VertexShader;
- Microsoft::WRL::ComPtr<ID3D11PixelShader> PixelShader;
- Microsoft::WRL::ComPtr<ID3D11RasterizerState1> RasterizerState;
- Microsoft::WRL::ComPtr<ID3D11BlendState1> BlendState;
- Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> Texture;
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
- void InitializeD3D();
- void ClearFrame(float r, float g, float b);
- void DrawTexture(ID3D11ShaderResourceView** texture, int x, int y, int width, int height);
- void Present();
- void ResizeBackBuffer(UINT width, UINT height);
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
- {
- // todo:
- // load compiled vertex shader data and set VertexShaderData pointer
- // set VertexShaderDataSize
- // load compiled pixel shader data and set PixelShaderData pointer
- // set PixelShaderDataSize
- // load bitmp data (32 bpp BGRA) and set TextureData pointer
- // set TextureWidth
- // set TextureHeight
- ///////////////////////////////////////////////////////////////////////////////////////////////
- char* title = "Minimal D3D11";
- WNDCLASSEX wndClassEx = { sizeof(wndClassEx) };
- wndClassEx.lpfnWndProc = WndProc;
- wndClassEx.hCursor = LoadCursor(nullptr, IDC_ARROW);
- wndClassEx.lpszClassName = title;
- RegisterClassEx(&wndClassEx);
- Window = CreateWindowEx(0, title, title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- InitializeD3D();
- ShowWindow(Window, SW_SHOWNORMAL);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- bool run = true;
- while (run)
- {
- ClearFrame(0.25f, 0.125f, 0.0f);
- DrawTexture(Texture.GetAddressOf(), 100, 100, TextureWidth, TextureHeight);
- Present();
- MSG msg;
- while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) > 0)
- {
- if (msg.message != WM_QUIT)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- run = false;
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_SIZE:
- {
- UINT width = static_cast<UINT>(lParam & 0xffff);
- UINT height = static_cast<UINT>(lParam >> 16);
- ResizeBackBuffer(width, height);
- }
- return 0;
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- }
- return 0;
- }
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void InitializeD3D()
- {
- SwapChain.Reset();
- Device.Reset();
- DeviceContext.Reset();
- RenderTargetView.Reset();
- VertexShader.Reset();
- PixelShader.Reset();
- RasterizerState.Reset();
- BlendState.Reset();
- Texture.Reset();
- ///////////////////////////////////////////////////////////////////////////////////////////////
- D3D_FEATURE_LEVEL featureLevel[] =
- {
- D3D_FEATURE_LEVEL_11_1,
- D3D_FEATURE_LEVEL_11_0,
- D3D_FEATURE_LEVEL_10_1,
- D3D_FEATURE_LEVEL_10_0 };
- Microsoft::WRL::ComPtr<ID3D11Device> device;
- Microsoft::WRL::ComPtr<ID3D11DeviceContext> deviceContext;
- D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, featureLevel, ARRAYSIZE(featureLevel), D3D11_SDK_VERSION, &device, nullptr, &deviceContext);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- device.As(&Device);
- deviceContext.As(&DeviceContext);
- Microsoft::WRL::ComPtr<IDXGIDevice1> dxgiDevice;
- Device.As(&dxgiDevice);
- Microsoft::WRL::ComPtr<IDXGIAdapter> dxgiAdapter;
- dxgiDevice->GetAdapter(&dxgiAdapter);
- Microsoft::WRL::ComPtr<IDXGIFactory2> dxgiFactory;
- dxgiAdapter->GetParent(__uuidof(IDXGIFactory2), &dxgiFactory);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- DXGI_SWAP_CHAIN_DESC1 swapChainDescription =
- {
- 0, // Width
- 0, // Height
- DXGI_FORMAT_B8G8R8A8_UNORM, // Format
- FALSE,{ // Stereo
- 1, // SampleDesc.Count
- 0 }, // SampleDesc.Quality
- DXGI_USAGE_RENDER_TARGET_OUTPUT, // BufferUsage
- 2, // BufferCount
- DXGI_SCALING_NONE, // Scaling (use DXGI_SCALING_STRETCH for Windows 7)
- DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL, // SwapEffect (use DXGI_SWAP_EFFECT_DISCARD for Windows 7)
- DXGI_ALPHA_MODE_UNSPECIFIED, // AlphaMode
- 0 }; // Flags
- dxgiFactory->CreateSwapChainForHwnd(Device.Get(), Window, &swapChainDescription, nullptr, nullptr, &SwapChain);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- Microsoft::WRL::ComPtr<ID3D11Texture2D> backBuffer;
- SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), &backBuffer);
- Device->CreateRenderTargetView(backBuffer.Get(), nullptr, &RenderTargetView);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- Device->CreateVertexShader(VertexShaderData, VertexShaderDataSize, nullptr, &VertexShader);
- Device->CreatePixelShader(PixelShaderData, PixelShaderDataSize, nullptr, &PixelShader);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- D3D11_RASTERIZER_DESC1 rasterizerDescription =
- {
- D3D11_FILL_SOLID, // FillMode
- D3D11_CULL_NONE, // CullMode
- FALSE, // FrontCounterClockwise
- 0, // DepthBias
- 0.0f, // DepthBiasClamp
- 0.0f, // SlopeScaledDepthBias
- FALSE, // DepthClipEnable
- FALSE, // ScissorEnable
- FALSE, // MultisampleEnable
- FALSE, // AntialiasedLineEnable
- 0 }; // ForcedSampleCount
- Device->CreateRasterizerState1(&rasterizerDescription, &RasterizerState);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- D3D11_BLEND_DESC1 blendDescription =
- {
- FALSE, // AlphaToCoverageEnable
- FALSE, { { // IndependentBlendEnable
- TRUE, // RenderTarget[0].BlendEnable
- FALSE, // RenderTarget[0].LogicOpEnable
- D3D11_BLEND_SRC_ALPHA, // RenderTarget[0].SrcBlend
- D3D11_BLEND_INV_SRC_ALPHA, // RenderTarget[0].DestBlend
- D3D11_BLEND_OP_ADD, // RenderTarget[0].BlendOp
- D3D11_BLEND_ONE, // RenderTarget[0].SrcBlendAlpha
- D3D11_BLEND_ONE, // RenderTarget[0].DestBlendAlpha
- D3D11_BLEND_OP_ADD, // RenderTarget[0].BlendOpAlpha
- D3D11_LOGIC_OP_CLEAR, // RenderTarget[0].LogicOp
- D3D11_COLOR_WRITE_ENABLE_ALL } } }; // RenderTarget[0].RenderTargetWriteMask
- Device->CreateBlendState1(&blendDescription, &BlendState);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- D3D11_TEXTURE2D_DESC texture2dDescription =
- {
- static_cast<UINT>(TextureWidth), // Width
- static_cast<UINT>(TextureHeight), // Height
- 1, // MipLevels
- 1, // ArraySize
- DXGI_FORMAT_B8G8R8A8_UNORM, { // Format
- 1, // SampleDesc.Count
- 0 }, // SampleDesc.Quality
- D3D11_USAGE_IMMUTABLE, // Usage
- D3D11_BIND_SHADER_RESOURCE, // BindFlags
- 0, // CPUAccessFlags
- 0 }; // MiscFlags
- D3D11_SUBRESOURCE_DATA subresourceData =
- {
- TextureData, // pSysMem
- static_cast<UINT>(TextureWidth * 4), // SysMemPitch (4 bytes per pixel)
- 0 }; // SysMemSlicePitch
- Microsoft::WRL::ComPtr<ID3D11Texture2D> texture2d;
- Device->CreateTexture2D(&texture2dDescription, &subresourceData, &texture2d);
- Device->CreateShaderResourceView(texture2d.Get(), nullptr, &Texture);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void ClearFrame(float r, float g, float b)
- {
- FLOAT backgroundColor[] = { r, g, b, 1.0f };
- DeviceContext->ClearRenderTargetView(RenderTargetView.Get(), backgroundColor);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void DrawTexture(ID3D11ShaderResourceView** texture, int x, int y, int width, int height)
- {
- D3D11_VIEWPORT viewport = { static_cast<float>(x), static_cast<float>(y), static_cast<float>(x + width), static_cast<float>(y + height), 0.0f, 1.0f };
- DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
- DeviceContext->VSSetShader(VertexShader.Get(), nullptr, 0);
- DeviceContext->RSSetViewports(1, &viewport);
- DeviceContext->RSSetState(RasterizerState.Get());
- DeviceContext->PSSetShader(PixelShader.Get(), nullptr, 0);
- DeviceContext->PSSetShaderResources(0, 1, texture);
- DeviceContext->OMSetBlendState(BlendState.Get(), nullptr, 0xffffffff);
- DeviceContext->OMSetRenderTargets(1, RenderTargetView.GetAddressOf(), nullptr);
- DeviceContext->Draw(4, 0);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void Present()
- {
- DXGI_PRESENT_PARAMETERS presentParameters = { 0, nullptr, nullptr, nullptr };
- HRESULT hr = SwapChain->Present1(1, 0, &presentParameters);
- if (hr != S_OK && hr != DXGI_STATUS_OCCLUDED)
- {
- InitializeD3D();
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- void ResizeBackBuffer(UINT width, UINT height)
- {
- RenderTargetView.Reset();
- ///////////////////////////////////////////////////////////////////////////////////////////////
- HRESULT hr = SwapChain->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
- ///////////////////////////////////////////////////////////////////////////////////////////////
- Microsoft::WRL::ComPtr<ID3D11Texture2D> backBuffer;
- SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), &backBuffer);
- Device->CreateRenderTargetView(backBuffer.Get(), nullptr, &RenderTargetView);
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // vertex shader
- static float2 xy[4] = { { -1.0f, -1.0f }, { -1.0f, 1.0f }, { 1.0f, -1.0f }, { 1.0f, 1.0f } };
- static float2 uv[4] = { { 0.0f, 1.0f }, { 0.0f, 0.0f }, { 1.0f, 1.0f }, { 1.0f, 0.0f } };
- struct vs_out
- {
- float4 position : SV_POSITION;
- float2 texcoord : TEXCOORD;
- };
- vs_out main(uint vertexId : SV_VERTEXID)
- {
- vs_out output;
- output.position = float4(xy[vertexId], 0.0f, 1.0f);
- output.texcoord = uv[vertexId];
- return output;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // pixel shader
- SamplerState state;
- tbuffer tb0
- {
- Texture2D texture2d;
- };
- struct ps_in
- {
- float4 position : SV_POSITION;
- float2 texcoord : TEXCOORD;
- };
- float4 main(ps_in input) : SV_TARGET
- {
- return texture2d.Sample(state, input.texcoord);
- }
Add Comment
Please, Sign In to add comment