daily pastebin goal
23%
SHARE
TWEET

Untitled

a guest Aug 22nd, 2015 23 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Windows.h>
  2. #include <d3d11.h>
  3. #include "dx11/D3DX11.h"
  4. #include <d3dcompiler.h>
  5. #include <iostream>
  6. #include <vector>
  7. #include "mymath/mymath.h"
  8. #include "SFML/Window.hpp"
  9.  
  10. using namespace mymath;
  11. using namespace std;
  12.  
  13. #include <sstream>
  14. #include <string>
  15.  
  16. #define WINMSG(X) MessageBox(GetActiveWindow(), (X), "message m8", MB_OK)
  17.  
  18. void WINCHECK( HRESULT hr )
  19. {
  20.   if( FAILED( hr ) )
  21.   {
  22.     WINMSG( "WE GOT A PROBLEM, DEBUG M89 :(" );
  23.   }
  24. }
  25.  
  26. struct interleaved_vertex
  27. {
  28.   float x, y, z, u, v;
  29.  
  30.   static const D3D11_INPUT_ELEMENT_DESC layout[2];
  31.   static const unsigned num_layout_elements;
  32. };
  33.  
  34. const D3D11_INPUT_ELEMENT_DESC interleaved_vertex::layout[2] =
  35. { //Interleaved
  36.   { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  37.   { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  38. };
  39. const unsigned interleaved_vertex::num_layout_elements = ARRAYSIZE( interleaved_vertex::layout );
  40.  
  41. class dx11_fullscreen_quad_renderer
  42. {
  43. protected:
  44.   ID3D11Buffer* vertex_buffer, *tex_coord_buffer, *index_buffer;
  45.   ID3D11VertexShader* vertex_shader;
  46.   ID3D11PixelShader* pixel_shader;
  47.   ID3D11InputLayout* input_layout;
  48.   ID3D11DepthStencilState* depth_stencil_state;
  49.   ID3D11RasterizerState* rasterizer_state;
  50.   bool is_set_up;
  51.  
  52.   dx11_fullscreen_quad_renderer()
  53.   {
  54.     is_set_up = false;
  55.     vertex_buffer = tex_coord_buffer = index_buffer = 0;
  56.   }
  57.  
  58.   void set_up( ID3D11Device* dev )
  59.   {
  60.     //set up only once
  61.     if( is_set_up )
  62.       return;
  63.     else
  64.       is_set_up = true;
  65.  
  66.     { //create vertex/texcoord/index buffers
  67.       vector<interleaved_vertex> vertices;
  68.       vector<unsigned> indices;
  69.  
  70.       vertices.reserve( 4 );
  71.       indices.reserve( 2 * 3 );
  72.  
  73.       indices.push_back( 0 );
  74.       indices.push_back( 1 );
  75.       indices.push_back( 2 );
  76.  
  77.       indices.push_back( 0 );
  78.       indices.push_back( 2 );
  79.       indices.push_back( 3 );
  80.  
  81.       //use default coordinate space
  82.       vec3 ll( -1, -1, 0 );
  83.       vec3 lr( 1, -1, 0 );
  84.       vec3 ul( -1, 1, 0 );
  85.       vec3 ur( 1, 1, 0 );
  86.  
  87.       //ccw
  88.       vertices.push_back( { ll.x, ll.y, ll.z, 0, 0 } );
  89.       vertices.push_back( { lr.x, lr.y, lr.z, 1, 0 } );
  90.       vertices.push_back( { ur.x, ur.y, ur.z, 1, 1 } );
  91.       vertices.push_back( { ul.x, ul.y, ul.z, 0, 1 } );
  92.  
  93.       D3D11_BUFFER_DESC bd_vertices = { 0 };
  94.       bd_vertices.Usage = D3D11_USAGE_DEFAULT;
  95.       bd_vertices.ByteWidth = sizeof(interleaved_vertex)* vertices.size();
  96.       bd_vertices.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  97.       D3D11_SUBRESOURCE_DATA srd_vertices = { 0 };
  98.       srd_vertices.pSysMem = vertices.data();
  99.  
  100.       D3D11_BUFFER_DESC bd_indices = { 0 };
  101.       bd_indices.Usage = D3D11_USAGE_DEFAULT;
  102.       bd_indices.ByteWidth = sizeof(float)* indices.size();
  103.       bd_indices.BindFlags = D3D11_BIND_INDEX_BUFFER;
  104.       D3D11_SUBRESOURCE_DATA srd_indices = { 0 };
  105.       srd_indices.pSysMem = indices.data();
  106.  
  107.       WINCHECK( dev->CreateBuffer( &bd_vertices, &srd_vertices, &vertex_buffer ) );
  108.       WINCHECK( dev->CreateBuffer( &bd_indices, &srd_indices, &index_buffer ) );
  109.     }
  110.  
  111.     { //create shaders
  112.       string shader_src =
  113.         //"Texture2D tex; \n"
  114.         //"SamplerState sam{ Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; }; \n"
  115.         "struct vs_in{ float3 pos : POSITION; float2 tex_coord : TEXCOORD0; }; \n"
  116.         "struct vs_out{ float4 pos : SV_POSITION; float2 tex_coord : TEXCOORD0; }; \n"
  117.         "struct ps_in{ float2 tex_coord : TEXCOORD0; }; \n"
  118.         "vs_out vs_main( vs_in i ){ vs_out o; o.pos = float4( i.pos, 1 ); o.tex_coord = i.tex_coord; return o; } \n"
  119.         "float4 ps_main( ps_in i ) : SV_TARGET { \n"
  120.         //"  return tex.Sample( sam, i.tex_coord ); \n"
  121.         "  return float4( 1, 1, 1, 1 ); \n"
  122.         "} \n";
  123.  
  124.       unsigned compile_options = D3DCOMPILE_ENABLE_STRICTNESS
  125.         | D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR; //opengl conventions
  126. #ifdef _DEBUG
  127.       compile_options |= D3DCOMPILE_DEBUG;
  128. #endif
  129.       auto check_shader_compilation = []( ID3DBlob* error )
  130.       {
  131.         if( error )
  132.         {
  133.           WINMSG( (const char*)error->GetBufferPointer() );
  134.         }
  135.       };
  136.  
  137.       ID3DBlob* vertex_shader_buffer, *pixel_shader_buffer;
  138.       ID3DBlob* error = 0;
  139.  
  140.       D3DCompile( shader_src.c_str(), shader_src.length(), 0, 0, 0, "vs_main", "vs_5_0", compile_options, 0, &vertex_shader_buffer, &error );
  141.       check_shader_compilation( error );
  142.       D3DCompile( shader_src.c_str(), shader_src.length(), 0, 0, 0, "ps_main", "ps_5_0", compile_options, 0, &pixel_shader_buffer, &error );
  143.       check_shader_compilation( error );
  144.  
  145.       WINCHECK( dev->CreateVertexShader( vertex_shader_buffer->GetBufferPointer(), vertex_shader_buffer->GetBufferSize(), 0, &vertex_shader ) );
  146.       WINCHECK( dev->CreatePixelShader( pixel_shader_buffer->GetBufferPointer(), pixel_shader_buffer->GetBufferSize(), 0, &pixel_shader ) );
  147.  
  148.       { //create vertex input layout
  149.         WINCHECK( dev->CreateInputLayout( interleaved_vertex::layout, interleaved_vertex::num_layout_elements, vertex_shader_buffer->GetBufferPointer(), vertex_shader_buffer->GetBufferSize(), &input_layout ) );
  150.       }
  151.     }
  152.  
  153.     { //create states used for drawing
  154.       D3D11_DEPTH_STENCIL_DESC dsd = { 0 };
  155.       dsd.DepthEnable = false;
  156.       dsd.StencilEnable = false;
  157.       dsd.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  158.       WINCHECK( dev->CreateDepthStencilState( &dsd, &depth_stencil_state ) );
  159.  
  160.       D3D11_RASTERIZER_DESC rd =
  161.       {
  162.         D3D11_FILL_SOLID,
  163.         D3D11_CULL_NONE,
  164.         true, //CCW triangles
  165.         0,
  166.         0,
  167.         0,
  168.         false,
  169.         false,
  170.         false,
  171.         false
  172.       };
  173.       WINCHECK( dev->CreateRasterizerState( &rd, &rasterizer_state ) );
  174.     }
  175.   }
  176. public:
  177.   static dx11_fullscreen_quad_renderer& get_instance()
  178.   {
  179.     static dx11_fullscreen_quad_renderer qr;
  180.     return qr;
  181.   }
  182.  
  183.   void render( ID3D11Device* dev, ID3D11DeviceContext* devcon )
  184.   {
  185.     set_up( dev );
  186.  
  187.     unsigned stride = sizeof( interleaved_vertex );
  188.     unsigned offset = 0;
  189.  
  190.     devcon->VSSetShader( vertex_shader, 0, 0 );
  191.     devcon->PSSetShader( pixel_shader, 0, 0 );
  192.     devcon->IASetInputLayout( input_layout );
  193.     devcon->IASetVertexBuffers( 0, 1, &vertex_buffer, &stride, &offset );
  194.     devcon->IASetIndexBuffer( index_buffer, DXGI_FORMAT_R32_UINT, 0 );
  195.     devcon->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  196.     devcon->OMSetBlendState( 0, 0, 0 );
  197.     devcon->OMSetDepthStencilState( depth_stencil_state, 0 );
  198.     devcon->RSSetState( rasterizer_state );
  199.     devcon->DrawIndexed( 6, 0, 0 );
  200.   }
  201. };
  202.  
  203. int main( int argc, char** argv )
  204. {
  205.   uvec2 screen( 1280, 720 );
  206.   bool fullscreen = false;
  207.   string title = "DX11 fullscreen quad experiment";
  208.  
  209.   /*
  210.    * Initialize the DX11 context
  211.    */
  212.  
  213.   sf::Window window( sf::VideoMode( screen.x, screen.y ), title.c_str() );
  214.   window.setVisible( true );
  215.  
  216.   //create device and swapchain
  217.   DXGI_SWAP_CHAIN_DESC scd = { 0 };
  218.   scd.BufferCount = 1;                                    // one back buffer
  219.   scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;     // use 32-bit color
  220.   scd.BufferDesc.Width = screen.x;
  221.   scd.BufferDesc.Height = screen.y;
  222.   scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;      // how swap chain is to be used
  223.   scd.OutputWindow = window.getSystemHandle();            // the window to be used
  224.   scd.SampleDesc.Count = 1;                               // how many multisamples
  225.   scd.Windowed = TRUE;
  226.  
  227.   IDXGISwapChain* swapchain = 0;
  228.   ID3D11Device* d3d11_device = 0;
  229.   ID3D11DeviceContext* devcon = 0;
  230.   UINT device_flags = 0;
  231. #ifdef _DEBUG
  232.   device_flags |= D3D11_CREATE_DEVICE_DEBUG;
  233. #endif
  234.   WINCHECK( D3D11CreateDeviceAndSwapChain( 0, D3D_DRIVER_TYPE_HARDWARE, 0, device_flags, 0, 0, D3D11_SDK_VERSION, &scd, &swapchain, &d3d11_device, 0, &devcon ) );
  235.  
  236.   ID3D11Texture2D* back_buffer = 0;
  237.   ID3D11RenderTargetView* render_target_view = 0;
  238.  
  239.   //get back buffer tex
  240.   WINCHECK( swapchain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID*)&back_buffer ) );
  241.   //create render target view of it
  242.   WINCHECK( d3d11_device->CreateRenderTargetView( back_buffer, 0, &render_target_view ) );
  243.  
  244.   //create depth stencil texture
  245.   ID3D11Texture2D* ds_tex = 0;
  246.   D3D11_TEXTURE2D_DESC ds_desc = { 0 };
  247.   ds_desc.Width = screen.x;
  248.   ds_desc.Height = screen.y;
  249.   ds_desc.MipLevels = 1;
  250.   ds_desc.ArraySize = 1;
  251.   ds_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  252.   ds_desc.SampleDesc.Count = 1;
  253.   ds_desc.SampleDesc.Quality = 0;
  254.   ds_desc.Usage = D3D11_USAGE_DEFAULT;
  255.   ds_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  256.   ds_desc.CPUAccessFlags = 0;
  257.   ds_desc.MiscFlags = 0;
  258.   WINCHECK( d3d11_device->CreateTexture2D( &ds_desc, 0, &ds_tex ) );
  259.  
  260.   ID3D11DepthStencilView* depth_stencil_view = 0;
  261.   D3D11_DEPTH_STENCIL_VIEW_DESC ds_view_desc = {
  262.     ds_desc.Format,
  263.     D3D11_DSV_DIMENSION_TEXTURE2D,
  264.     0,
  265.     { 0 }
  266.   };
  267.   WINCHECK( d3d11_device->CreateDepthStencilView( ds_tex, &ds_view_desc, &depth_stencil_view ) );
  268.  
  269.   //we'll render to the back buffer ofc...
  270.   devcon->OMSetRenderTargets( 1, &render_target_view, depth_stencil_view );
  271.  
  272.   //set viewport
  273.   D3D11_VIEWPORT vp = { 0 };
  274.   vp.Width = screen.x;
  275.   vp.Height = screen.y;
  276.   vp.MinDepth = 0;
  277.   vp.MaxDepth = 1;
  278.   vp.TopLeftX = 0;
  279.   vp.TopLeftY = 0;
  280.   devcon->RSSetViewports( 1, &vp );
  281.  
  282.   /*
  283.    * Handle events
  284.    */
  285.  
  286.   bool run = true;
  287.  
  288.   auto event_handler = [&]( const sf::Event & ev )
  289.   {
  290.     switch( ev.type )
  291.     {
  292.       default:
  293.         break;
  294.     }
  295.   };
  296.  
  297.   /*
  298.    * Render
  299.    */
  300.  
  301.   sf::Event ev;
  302.  
  303.   while( run )
  304.   {
  305.     //-----------------------------
  306.     //do event handling
  307.     //-----------------------------
  308.  
  309.     while( window.pollEvent( ev ) )
  310.     {
  311.       if( ev.type == sf::Event::Closed ||
  312.           (
  313.           ev.type == sf::Event::KeyPressed &&
  314.           ev.key.code == sf::Keyboard::Escape
  315.           )
  316.           )
  317.       {
  318.         run = false;
  319.       }
  320.  
  321.       event_handler( ev );
  322.     }
  323.  
  324.     //-----------------------------
  325.     //render
  326.     //-----------------------------
  327.  
  328.     vec4 sky( 0.5, 0.5, 0.8, 1 );
  329.  
  330.     devcon->ClearRenderTargetView( render_target_view, &sky.x );
  331.     devcon->ClearDepthStencilView( depth_stencil_view, D3D11_CLEAR_DEPTH, 1.0f, 0 );
  332.  
  333.     //render full screen quad
  334.     dx11_fullscreen_quad_renderer::get_instance().render( d3d11_device, devcon );
  335.  
  336.     //-----------------------------
  337.     //present to screen
  338.     //-----------------------------
  339.  
  340.     swapchain->Present( 0, 0 );
  341.   }
  342.  
  343.   return 0;
  344. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top