Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- -----------------------------------------------------------------------------
- Filename: Game.cpp
- -----------------------------------------------------------------------------
- */
- // Declare Once
- #pragma once
- // Includes
- #include "Game.hpp"
- // Create ---------------------------------------------------------------------------------
- Void Game::Create()
- {
- // Blah
- LoadAssets();
- }
- // Release --------------------------------------------------------------------------------
- Void Game::Destroy()
- {
- }
- // ----------------------------------------------------------------------------------------
- Void Game::Update()
- {
- Rotation += 0.01f;
- Align(16) cbNode node;
- node.WorldMatrix = To4x4(RotationZ(Rotation)) * Float4(1, 1, 1, 1);
- node.Color = Float4(0.75f, 0.5f, 0.25f, 1.0f);
- node.TextureID = 0;
- node.Unused = 0;
- memcpy(NodeDataBegin, &node, sizeof(node));
- }
- // ----------------------------------------------------------------------------------------
- Void Game::Draw()
- {
- // Blah
- auto commandList = Graphics::Manager::Singleton->Scheduler->CommandList;
- // Bind the view buffer
- commandList->SetGraphicsRootConstantBufferView(1, ViewBuffer.Handle->GetGPUVirtualAddress());
- // Execute the commands stored in the bundle.
- commandList->ExecuteBundle(Bundle);
- }
- // ----------------------------------------------------------------------------------------
- Void Game::LoadAssets()
- {
- // Blah
- HRESULT result;
- // Bleh
- auto graphicsManager = Graphics::Manager::Singleton;
- // -- //
- auto device = graphicsManager->Device;
- // Create the vertex buffer.
- {
- // CUBE
- Float x = 1.0 / 2;
- Float n = sqrt(0.5);
- // -- //
- Vertex vertices[] =
- {
- {{ x, x, x},{0, 0, 0},{0, 0}},
- {{-x, x, x},{1, 0, 0},{1, 0}},
- {{ x, -x, x},{0, 1, 0},{0, 1}},
- {{-x, -x, x},{1, 1, 0},{1, 1}},
- {{ x, x, -x},{0, 0, 1},{0, 0}},
- {{-x, x, -x},{1, 0, 1},{1, 0}},
- {{ x, -x, -x},{0, 1, 1},{0, 1}},
- {{-x, -x, -x},{1, 1, 1},{1, 1}}
- };
- // -- //
- UShort indices[] =
- {
- // +x
- 0, 2, 6,
- 6, 4, 0,
- // +y
- 1, 0, 4,
- 4, 5, 1,
- // +z
- 0, 3, 2,
- 3, 0, 1,
- // -x
- 1, 7, 3,
- 7, 1, 5,
- // -y
- 3, 6, 2,
- 6, 3, 7,
- // -z
- 4, 6, 7,
- 7, 5, 4
- };
- VertexBuffer.CreateVertexBuffer(vertices, sizeof(vertices), sizeof(Vertex));
- IndexBuffer.CreateIndexBuffer(indices, sizeof(indices));
- }
- // Create the constant buffer.
- {
- ViewBuffer.CreateUploadBuffer(); //CreateConstantBuffer(sizeof(cbViewport));
- NodeBuffer.CreateUploadBuffer();
- // Camera
- Float3 Position(3, 3, 1.5);
- Quaternion Orientation = LookAt(Position, Float3(), Identity<Quaternion>());
- //Blah
- Align(16) Float4x4 viewMatrix, projectionMatrix;
- // View matrix
- viewMatrix = Inverse(To4x4(Translate(Multiply(To3x3(Orientation), Identity<Float3x3>()), Position)));
- // Projection matrix
- projectionMatrix = PerspectiveLH(pi / 4.0f, 960.0f / 640.0f, 0.01f, 1000.0f);
- // -- //
- Align(16) cbViewport viewport;
- graphicsManager->Window->ViewProjectionMatrix = Multiply(projectionMatrix, viewMatrix);
- viewport.ViewMatrix = viewMatrix;
- viewport.ProjectionMatrix = projectionMatrix;
- viewport.ViewProjectionMatrix = graphicsManager->Window->ViewProjectionMatrix;
- viewport.CameraPosition = Position;
- // -- //
- cbNode node;
- node.WorldMatrix = Identity<Float4x4>();
- node.Color = Float4(0.75f, 0.5f, 0.25f, 1.0f);
- node.TextureID = 0;
- node.Unused = 0;
- // Map and initialize the constant buffer. We don't unmap this until the
- // app closes. Keeping things mapped for the lifetime of the resource is okay.
- ViewDataBegin = ViewBuffer.Map();
- memcpy(ViewDataBegin, &viewport, sizeof(viewport));
- // -- //
- NodeDataBegin = NodeBuffer.Map();
- memcpy(NodeDataBegin, &node, sizeof(node));
- }
- // Create the texture.
- {
- const Int width = 64;
- const Int height = 64;
- // Texture data
- UByte4 textureData[width * height];
- // -- //
- for(Int y = 0; y < height; ++y)
- {
- for(Int x = 0; x < width; ++x)
- {
- // Fill with white
- textureData[x + y * width] = UByte4(x * 4, y * 4, 0, 1);
- }
- }
- // -- //
- Int size = sizeof(textureData);
- Graphics::Buffer::Description textureDesc;
- // Describe the texture
- textureDesc.Width = width;
- textureDesc.Height = height;
- textureDesc.Depth = 1;
- textureDesc.Mips = 1;
- textureDesc.Format = Graphics::Buffer::Format::RGBA8UNorm;
- // -- //
- DiffuseTexture.CreateTexture(textureDesc, textureData, size);
- D3D12_RESOURCE_DESC testDesc;
- // Buffer resource description
- testDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
- testDesc.Alignment = 0;
- testDesc.Width = 1024;
- testDesc.Height = 1024;
- testDesc.DepthOrArraySize = 1;
- testDesc.MipLevels = 11;
- testDesc.Format = DXGI_FORMAT_R10G10B10A2_UNORM;
- testDesc.SampleDesc.Count = 1;
- testDesc.SampleDesc.Quality = 0;
- testDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
- testDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
- // -- //
- auto info = graphicsManager->Device->GetResourceAllocationInfo(0, 1, &testDesc);
- UINT64 RequiredSize = 0;
- D3D12_PLACED_SUBRESOURCE_FOOTPRINT Layouts[11];
- UINT NumRows[11];
- UINT64 RowSizesInBytes[11];
- // -- //
- graphicsManager->Device->GetCopyableFootprints(&testDesc, 0, 11, 0, Layouts, nullptr, nullptr, &RequiredSize);
- result = 0;
- }
- // Create and record the bundle.
- {
- result = device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, graphicsManager->Scheduler->BundleAllocator, graphicsManager->PSO, IID_PPV_ARGS(&Bundle));
- Bundle->SetGraphicsRootSignature(graphicsManager->RootSignature);
- Bundle->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
- Bundle->SetGraphicsRootConstantBufferView(0, NodeBuffer.Handle->GetGPUVirtualAddress());
- Bundle->IASetVertexBuffers(0, 1, &VertexBuffer.View.VertexView);
- Bundle->IASetIndexBuffer(&IndexBuffer.View.IndexView);
- Bundle->DrawIndexedInstanced(36, 1, 0, 0, 0);
- result = Bundle->Close();
- }
- }
- // ----------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement