Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // minimal d3d 11.0 framework
- // for software rendered framebuffer
- // d7samurai 2016
- #pragma comment(lib, "dxgi")
- #pragma comment(lib, "d3d11")
- #include <windows.h>
- #include <d3d11.h>
- #define FRAMEBUFFER_WIDTH 32
- #define FRAMEBUFFER_HEIGHT 8
- HWND Window;
- IDXGISwapChain* SwapChain;
- ID3D11Device* Device;
- ID3D11DeviceContext* DeviceContext;
- ID3D11RenderTargetView* RenderTargetView;
- ID3D11VertexShader* VertexShader;
- ID3D11PixelShader* PixelShader;
- ID3D11RasterizerState* RasterizerState;
- ID3D11SamplerState* SamplerState;
- ID3D11Texture2D* FrameBuffer;
- ID3D11ShaderResourceView* FrameBufferView;
- unsigned char VertexShader_data[] =
- {
- 0x44, 0x58, 0x42, 0x43, 0xe9, 0xbb, 0x34, 0xe5, 0xd7, 0x73, 0x53, 0xa4,
- 0x62, 0x83, 0xa0, 0xd3, 0xcf, 0xef, 0x25, 0x60, 0x01, 0x00, 0x00, 0x00,
- 0x88, 0x03, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
- 0x8c, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x18, 0x01, 0x00, 0x00,
- 0x0c, 0x03, 0x00, 0x00, 0x52, 0x44, 0x45, 0x46, 0x50, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x1c, 0x00, 0x00, 0x00, 0x00, 0x04, 0xfe, 0xff, 0x00, 0x01, 0x00, 0x00,
- 0x1c, 0x00, 0x00, 0x00, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66,
- 0x74, 0x20, 0x28, 0x52, 0x29, 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53,
- 0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c,
- 0x65, 0x72, 0x20, 0x36, 0x2e, 0x33, 0x2e, 0x39, 0x36, 0x30, 0x30, 0x2e,
- 0x31, 0x36, 0x33, 0x38, 0x34, 0x00, 0xab, 0xab, 0x49, 0x53, 0x47, 0x4e,
- 0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
- 0x53, 0x56, 0x5f, 0x56, 0x45, 0x52, 0x54, 0x45, 0x58, 0x49, 0x44, 0x00,
- 0x4f, 0x53, 0x47, 0x4e, 0x50, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x08, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x0f, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x03, 0x0c, 0x00, 0x00, 0x53, 0x56, 0x5f, 0x50, 0x4f, 0x53, 0x49, 0x54,
- 0x49, 0x4f, 0x4e, 0x00, 0x54, 0x45, 0x58, 0x43, 0x4f, 0x4f, 0x52, 0x44,
- 0x00, 0xab, 0xab, 0xab, 0x53, 0x48, 0x44, 0x52, 0xec, 0x01, 0x00, 0x00,
- 0x40, 0x00, 0x01, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x04,
- 0x12, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
- 0x67, 0x00, 0x00, 0x04, 0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x03, 0x32, 0x20, 0x10, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00,
- 0x69, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
- 0x04, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
- 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
- 0x32, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0xbf, 0x00, 0x00, 0x80, 0xbf,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
- 0x32, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0xbf, 0x00, 0x00, 0x80, 0x3f,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
- 0x32, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80, 0xbf,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
- 0x32, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80, 0x3f,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
- 0x32, 0x30, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
- 0x32, 0x30, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
- 0x32, 0x30, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80, 0x3f,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
- 0x32, 0x30, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x05,
- 0x12, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x10, 0x10, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x07, 0x32, 0x20, 0x10, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x46, 0x30, 0x20, 0x04, 0x00, 0x00, 0x00, 0x00,
- 0x0a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x07,
- 0x32, 0x20, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x46, 0x30, 0x20, 0x04,
- 0x01, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x36, 0x00, 0x00, 0x08, 0xc2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x3e, 0x00, 0x00, 0x01,
- 0x53, 0x54, 0x41, 0x54, 0x74, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x08, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00
- };
- unsigned char PixelShader_data[] =
- {
- 0x44, 0x58, 0x42, 0x43, 0x2c, 0xa2, 0xbe, 0xee, 0x11, 0x9c, 0xfd, 0x0e,
- 0x0e, 0x37, 0x37, 0xd2, 0x91, 0x80, 0xc6, 0x41, 0x01, 0x00, 0x00, 0x00,
- 0x50, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
- 0xdc, 0x00, 0x00, 0x00, 0x34, 0x01, 0x00, 0x00, 0x68, 0x01, 0x00, 0x00,
- 0xd4, 0x01, 0x00, 0x00, 0x52, 0x44, 0x45, 0x46, 0xa0, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x1c, 0x00, 0x00, 0x00, 0x00, 0x04, 0xff, 0xff, 0x00, 0x01, 0x00, 0x00,
- 0x6c, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x62, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
- 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x73, 0x74, 0x61, 0x74,
- 0x65, 0x00, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x32, 0x64, 0x00,
- 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x20, 0x28, 0x52,
- 0x29, 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53, 0x68, 0x61, 0x64, 0x65,
- 0x72, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x72, 0x20, 0x36,
- 0x2e, 0x33, 0x2e, 0x39, 0x36, 0x30, 0x30, 0x2e, 0x31, 0x36, 0x33, 0x38,
- 0x34, 0x00, 0xab, 0xab, 0x49, 0x53, 0x47, 0x4e, 0x50, 0x00, 0x00, 0x00,
- 0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x53, 0x56, 0x5f, 0x50,
- 0x4f, 0x53, 0x49, 0x54, 0x49, 0x4f, 0x4e, 0x00, 0x54, 0x45, 0x58, 0x43,
- 0x4f, 0x4f, 0x52, 0x44, 0x00, 0xab, 0xab, 0xab, 0x4f, 0x53, 0x47, 0x4e,
- 0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
- 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
- 0x53, 0x56, 0x5f, 0x54, 0x41, 0x52, 0x47, 0x45, 0x54, 0x00, 0xab, 0xab,
- 0x53, 0x48, 0x44, 0x52, 0x64, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
- 0x19, 0x00, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x03, 0x00, 0x60, 0x10, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x58, 0x18, 0x00, 0x04, 0x00, 0x70, 0x10, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x55, 0x55, 0x00, 0x00, 0x62, 0x10, 0x00, 0x03,
- 0x32, 0x10, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x03,
- 0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x09,
- 0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x10, 0x10, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x46, 0x7e, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x60, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x01,
- 0x53, 0x54, 0x41, 0x54, 0x74, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00
- };
- unsigned int FrameBuffer_data[FRAMEBUFFER_WIDTH * FRAMEBUFFER_HEIGHT];
- void InitializeD3D()
- {
- D3D_FEATURE_LEVEL featureLevels[] =
- {
- D3D_FEATURE_LEVEL_11_0,
- D3D_FEATURE_LEVEL_10_1,
- D3D_FEATURE_LEVEL_10_0, };
- D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, featureLevels, ARRAYSIZE(featureLevels), D3D11_SDK_VERSION, &Device, nullptr, &DeviceContext);
- IDXGIFactory* dxgiFactory;
- CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&dxgiFactory);
- DXGI_SWAP_CHAIN_DESC swpDesc;
- swpDesc.BufferDesc.Width = 0;
- swpDesc.BufferDesc.Height = 0;
- swpDesc.BufferDesc.RefreshRate.Numerator = 0;
- swpDesc.BufferDesc.RefreshRate.Denominator = 0;
- swpDesc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
- swpDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
- swpDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
- swpDesc.SampleDesc.Count = 1;
- swpDesc.SampleDesc.Quality = 0;
- swpDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- swpDesc.BufferCount = 2;
- swpDesc.OutputWindow = Window;
- swpDesc.Windowed = TRUE;
- swpDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- swpDesc.Flags = 0;
- dxgiFactory->CreateSwapChain(Device, &swpDesc, &SwapChain);
- ID3D11Texture2D* backBuffer;
- SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
- Device->CreateRenderTargetView(backBuffer, nullptr, &RenderTargetView);
- Device->CreateVertexShader(VertexShader_data, sizeof(VertexShader_data), nullptr, &VertexShader);
- Device->CreatePixelShader(PixelShader_data, sizeof(PixelShader_data), nullptr, &PixelShader);
- D3D11_RASTERIZER_DESC rstDesc;
- rstDesc.FillMode = D3D11_FILL_SOLID;
- rstDesc.CullMode = D3D11_CULL_NONE;
- rstDesc.FrontCounterClockwise = FALSE;
- rstDesc.DepthBias = 0;
- rstDesc.DepthBiasClamp = 0.0f;
- rstDesc.SlopeScaledDepthBias = 0.0f;
- rstDesc.DepthClipEnable = FALSE;
- rstDesc.ScissorEnable = FALSE;
- rstDesc.MultisampleEnable = FALSE;
- rstDesc.AntialiasedLineEnable = FALSE;
- Device->CreateRasterizerState(&rstDesc, &RasterizerState);
- D3D11_SAMPLER_DESC smpDesc;
- smpDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
- smpDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
- smpDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
- smpDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
- smpDesc.MipLODBias = 0.0f;
- smpDesc.MaxAnisotropy = 0;
- smpDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
- smpDesc.BorderColor[0] = 0.0f;
- smpDesc.BorderColor[1] = 0.0f;
- smpDesc.BorderColor[2] = 0.0f;
- smpDesc.BorderColor[3] = 0.0f;
- smpDesc.MinLOD = 0.0f;
- smpDesc.MaxLOD = D3D11_FLOAT32_MAX;
- Device->CreateSamplerState(&smpDesc, &SamplerState);
- D3D11_TEXTURE2D_DESC texDesc;
- texDesc.Width = FRAMEBUFFER_WIDTH;
- texDesc.Height = FRAMEBUFFER_HEIGHT;
- texDesc.MipLevels = 1;
- texDesc.ArraySize = 1;
- texDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
- texDesc.SampleDesc.Count = 1;
- texDesc.SampleDesc.Quality = 0;
- texDesc.Usage = D3D11_USAGE_DYNAMIC;
- texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
- texDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
- texDesc.MiscFlags = 0;
- Device->CreateTexture2D(&texDesc, nullptr, &FrameBuffer);
- Device->CreateShaderResourceView(FrameBuffer, nullptr, &FrameBufferView);
- dxgiFactory->Release();
- backBuffer->Release();
- }
- void Render()
- {
- D3D11_MAPPED_SUBRESOURCE frameBuffer;
- DeviceContext->Map(FrameBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &frameBuffer);
- {
- unsigned char* src = reinterpret_cast<unsigned char*>(FrameBuffer_data);
- unsigned char* dst = reinterpret_cast<unsigned char*>(frameBuffer.pData);
- for (int y = 0; y < FRAMEBUFFER_HEIGHT; ++y)
- {
- memcpy(dst, src, FRAMEBUFFER_WIDTH * 4);
- src += FRAMEBUFFER_WIDTH * 4;
- dst += frameBuffer.RowPitch;
- }
- }
- DeviceContext->Unmap(FrameBuffer, 0);
- FLOAT backgroundColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
- DeviceContext->ClearRenderTargetView(RenderTargetView, backgroundColor);
- DXGI_SWAP_CHAIN_DESC swpDesc;
- SwapChain->GetDesc(&swpDesc);
- D3D11_VIEWPORT viewport;
- viewport.TopLeftX = 0.0f;
- viewport.TopLeftY = 0.0f;
- viewport.Width = (float)swpDesc.BufferDesc.Width;
- viewport.Height = (float)swpDesc.BufferDesc.Height;
- viewport.MinDepth = 0.0f;
- viewport.MaxDepth = 1.0f;
- DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
- DeviceContext->VSSetShader(VertexShader, nullptr, 0);
- DeviceContext->RSSetViewports(1, &viewport);
- DeviceContext->RSSetState(RasterizerState);
- DeviceContext->PSSetShader(PixelShader, nullptr, 0);
- DeviceContext->PSSetShaderResources(0, 1, &FrameBufferView);
- DeviceContext->PSSetSamplers(0, 1, &SamplerState);
- DeviceContext->OMSetRenderTargets(1, &RenderTargetView, nullptr);
- DeviceContext->Draw(4, 0);
- HRESULT hr = SwapChain->Present(1, 0);
- if (hr != S_OK && hr != DXGI_STATUS_OCCLUDED)
- {
- SwapChain->Release();
- Device->Release();
- DeviceContext->Release();
- RenderTargetView->Release();
- VertexShader->Release();
- PixelShader->Release();
- RasterizerState->Release();
- SamplerState->Release();
- FrameBuffer->Release();
- FrameBufferView->Release();
- InitializeD3D();
- }
- }
- void Update() // example code that draws a 'd7' logo to the framebuffer
- {
- static int index = 0;
- static int d7[] = { 0x0000037e, 0x03063f0c, 0x63186330, 0x3f600000 };
- for (int data = 0; data < 4; ++data)
- {
- for (int bit = 31; bit >= 0; --bit)
- {
- int line = index + (data << 1) + ((31 - bit) >> 4);
- for (int pixel = 0; pixel < 2; ++pixel)
- {
- FrameBuffer_data[(data << 6) + ((31 - bit) << 1) + pixel] = (d7[data] >> bit) & 1 ? 0xffffffff : 0xff000000 + (0x00100000 * (line & 0xf));
- }
- }
- }
- ++index;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_SIZE:
- {
- UINT width = (UINT)(lParam & 0xffff);
- UINT height = (UINT)(lParam >> 16);
- RenderTargetView->Release();
- SwapChain->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
- ID3D11Texture2D* backBuffer;
- SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
- Device->CreateRenderTargetView(backBuffer, nullptr, &RenderTargetView);
- backBuffer->Release();
- }
- return 0;
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- }
- return 0;
- }
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
- {
- char* title = "Minimal D3D11 by d7samurai";
- 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)
- {
- MSG msg;
- while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) > 0)
- {
- if (msg.message != WM_QUIT)
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- run = false;
- }
- }
- Update();
- Render();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment