Advertisement
Ember

game

Feb 2nd, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.95 KB | None | 0 0
  1. /*
  2. -----------------------------------------------------------------------------
  3.     Filename: Game.cpp
  4. -----------------------------------------------------------------------------
  5. */
  6.  
  7. // Declare Once
  8. #pragma once
  9. // Includes
  10. #include "Game.hpp"
  11.  
  12. // Create ---------------------------------------------------------------------------------
  13. Void Game::Create()
  14. {
  15.     // Blah
  16.     LoadAssets();
  17. }
  18.  
  19. // Release --------------------------------------------------------------------------------
  20. Void Game::Destroy()
  21. {
  22. }
  23.  
  24. // ----------------------------------------------------------------------------------------
  25. Void Game::Update()
  26. {
  27.     Rotation += 0.01f;
  28.     Align(16) cbNode node;
  29.     node.WorldMatrix = To4x4(RotationZ(Rotation)) * Float4(1, 1, 1, 1);
  30.     node.Color = Float4(0.75f, 0.5f, 0.25f, 1.0f);
  31.     node.TextureID = 0;
  32.     node.Unused = 0;
  33.     memcpy(NodeDataBegin, &node, sizeof(node));
  34. }
  35.  
  36. // ----------------------------------------------------------------------------------------
  37. Void Game::Draw()
  38. {
  39.     // Blah
  40.     auto commandList = Graphics::Manager::Singleton->Scheduler->CommandList;
  41.  
  42.     // Bind the view buffer
  43.     commandList->SetGraphicsRootConstantBufferView(1, ViewBuffer.Handle->GetGPUVirtualAddress());
  44.  
  45.     // Execute the commands stored in the bundle.
  46.     commandList->ExecuteBundle(Bundle);
  47. }
  48.  
  49. // ----------------------------------------------------------------------------------------
  50. Void Game::LoadAssets()
  51. {
  52.     // Blah
  53.     HRESULT result;
  54.  
  55.     // Bleh
  56.     auto graphicsManager = Graphics::Manager::Singleton;
  57.     // -- //
  58.     auto device = graphicsManager->Device;
  59.  
  60.     // Create the vertex buffer.
  61.     {
  62.         // CUBE
  63.         Float x = 1.0 / 2;
  64.         Float n = sqrt(0.5);
  65.         // -- //
  66.         Vertex vertices[] =
  67.         {
  68.             {{ x,  x,  x},{0, 0, 0},{0, 0}},
  69.             {{-x,  x,  x},{1, 0, 0},{1, 0}},
  70.             {{ x, -x,  x},{0, 1, 0},{0, 1}},
  71.             {{-x, -x,  x},{1, 1, 0},{1, 1}},
  72.             {{ x,  x, -x},{0, 0, 1},{0, 0}},
  73.             {{-x,  x, -x},{1, 0, 1},{1, 0}},
  74.             {{ x, -x, -x},{0, 1, 1},{0, 1}},
  75.             {{-x, -x, -x},{1, 1, 1},{1, 1}}
  76.         };
  77.         // -- //
  78.         UShort indices[] =
  79.         {
  80.             // +x
  81.             0, 2, 6,
  82.             6, 4, 0,
  83.             // +y
  84.             1, 0, 4,
  85.             4, 5, 1,
  86.             // +z
  87.             0, 3, 2,
  88.             3, 0, 1,
  89.             // -x
  90.             1, 7, 3,
  91.             7, 1, 5,
  92.             // -y
  93.             3, 6, 2,
  94.             6, 3, 7,
  95.             // -z
  96.             4, 6, 7,
  97.             7, 5, 4
  98.         };
  99.  
  100.         VertexBuffer.CreateVertexBuffer(vertices, sizeof(vertices), sizeof(Vertex));
  101.         IndexBuffer.CreateIndexBuffer(indices, sizeof(indices));
  102.     }
  103.  
  104.     // Create the constant buffer.
  105.     {
  106.         ViewBuffer.CreateUploadBuffer(); //CreateConstantBuffer(sizeof(cbViewport));
  107.         NodeBuffer.CreateUploadBuffer();
  108.  
  109.         // Camera
  110.         Float3 Position(3, 3, 1.5);
  111.         Quaternion Orientation = LookAt(Position, Float3(), Identity<Quaternion>());
  112.         //Blah
  113.         Align(16) Float4x4 viewMatrix, projectionMatrix;
  114.         // View matrix
  115.         viewMatrix = Inverse(To4x4(Translate(Multiply(To3x3(Orientation), Identity<Float3x3>()), Position)));
  116.         // Projection matrix
  117.         projectionMatrix = PerspectiveLH(pi / 4.0f, 960.0f / 640.0f, 0.01f, 1000.0f);
  118.         // -- //
  119.         Align(16) cbViewport viewport;
  120.         graphicsManager->Window->ViewProjectionMatrix = Multiply(projectionMatrix, viewMatrix);
  121.         viewport.ViewMatrix = viewMatrix;
  122.         viewport.ProjectionMatrix = projectionMatrix;
  123.         viewport.ViewProjectionMatrix = graphicsManager->Window->ViewProjectionMatrix;
  124.         viewport.CameraPosition = Position;
  125.         // -- //
  126.         cbNode node;
  127.         node.WorldMatrix = Identity<Float4x4>();
  128.         node.Color = Float4(0.75f, 0.5f, 0.25f, 1.0f);
  129.         node.TextureID = 0;
  130.         node.Unused = 0;
  131.  
  132.         // Map and initialize the constant buffer. We don't unmap this until the
  133.         // app closes. Keeping things mapped for the lifetime of the resource is okay.
  134.         ViewDataBegin = ViewBuffer.Map();
  135.         memcpy(ViewDataBegin, &viewport, sizeof(viewport));
  136.         // -- //
  137.         NodeDataBegin = NodeBuffer.Map();
  138.         memcpy(NodeDataBegin, &node, sizeof(node));
  139.     }
  140.  
  141.     // Create the texture.
  142.     {
  143.         const Int width = 64;
  144.         const Int height = 64;
  145.  
  146.         // Texture data
  147.         UByte4 textureData[width * height];
  148.         // -- //
  149.         for(Int y = 0; y < height; ++y)
  150.         {
  151.             for(Int x = 0; x < width; ++x)
  152.             {
  153.                 // Fill with white
  154.                 textureData[x + y * width] = UByte4(x * 4, y * 4, 0, 1);
  155.             }
  156.         }
  157.         // -- //
  158.         Int size = sizeof(textureData);
  159.  
  160.         Graphics::Buffer::Description textureDesc;
  161.         // Describe the texture
  162.         textureDesc.Width = width;
  163.         textureDesc.Height = height;
  164.         textureDesc.Depth = 1;
  165.         textureDesc.Mips = 1;
  166.         textureDesc.Format = Graphics::Buffer::Format::RGBA8UNorm;
  167.         // -- //
  168.         DiffuseTexture.CreateTexture(textureDesc, textureData, size);
  169.  
  170.         D3D12_RESOURCE_DESC testDesc;
  171.         // Buffer resource description
  172.         testDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
  173.         testDesc.Alignment = 0;
  174.         testDesc.Width = 1024;
  175.         testDesc.Height = 1024;
  176.         testDesc.DepthOrArraySize = 1;
  177.         testDesc.MipLevels = 11;
  178.         testDesc.Format = DXGI_FORMAT_R10G10B10A2_UNORM;
  179.         testDesc.SampleDesc.Count = 1;
  180.         testDesc.SampleDesc.Quality = 0;
  181.         testDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
  182.         testDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
  183.         // -- //
  184.         auto info = graphicsManager->Device->GetResourceAllocationInfo(0, 1, &testDesc);
  185.  
  186.         UINT64 RequiredSize = 0;
  187.         D3D12_PLACED_SUBRESOURCE_FOOTPRINT Layouts[11];
  188.         UINT NumRows[11];
  189.         UINT64 RowSizesInBytes[11];
  190.         // -- //
  191.         graphicsManager->Device->GetCopyableFootprints(&testDesc, 0, 11, 0, Layouts, nullptr, nullptr, &RequiredSize);
  192.  
  193.         result = 0;
  194.     }
  195.  
  196.     // Create and record the bundle.
  197.     {
  198.         result = device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, graphicsManager->Scheduler->BundleAllocator, graphicsManager->PSO, IID_PPV_ARGS(&Bundle));
  199.         Bundle->SetGraphicsRootSignature(graphicsManager->RootSignature);
  200.         Bundle->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  201.         Bundle->SetGraphicsRootConstantBufferView(0, NodeBuffer.Handle->GetGPUVirtualAddress());
  202.         Bundle->IASetVertexBuffers(0, 1, &VertexBuffer.View.VertexView);
  203.         Bundle->IASetIndexBuffer(&IndexBuffer.View.IndexView);
  204.         Bundle->DrawIndexedInstanced(36, 1, 0, 0, 0);
  205.         result = Bundle->Close();
  206.     }
  207. }
  208.  
  209. // ----------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement