pong and dx12

Started by
15 comments, last by JoeJ 1 year, 2 months ago

@Gnollrunner thanks I will do my best

Advertisement

pbivens67 said:
I thought that using dx12 is good at developing games and is used in the games industry.

Not really. DX12 is used only by one or two AAA titles per month, and its not backwards compatible with older graphics cards and operating systems. I recommend to learn some version of opengl instead, especially if you want to release for android or linux too. Dont specifically target windows, forget that system.

my question is how do I draw a rectangle using 2 triangles, here is the code I am using

    // Record commands.
    const float clearColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
    m_commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
    m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    m_commandList->IASetVertexBuffers(0, 1, &m_vertexBufferView);
    m_commandList->DrawInstanced(3, 2, 0, 0);
    {
        // Define the geometry for a triangle.
        Vertex triangleVertices[] =
        {
            { { -1.0f, 0.1f, 0.0f }, { 1.0f, 0.0f, 0.0f, 0.0f } },
            { { -0.95f, 0.1f , 0.0f }, { 1.0f, 0.0f, 0.0f, 0.0f } },
            { { -1.0f, -0.1f, 0.0f }, { 1.0f, 0.0f, 0.0f, 0.0f } }

//            { { -0.95f, 0.1f, 0.0f }, { 1.0f, 0.0f, 0.0f, 0.0f } },
//            { { -0.95f, -0.1f , 0.0f }, { 1.0f, 0.0f, 0.0f, 0.0f } },
//            { { -1.0f, -0.1f, 0.0f }, { 1.0f, 0.0f, 0.0f, 0.0f } }
        };

pbivens67 said:
my question is how do I draw a rectangle

Search for a tutorial, e.g.: https://learn.microsoft.com/en-us/samples/microsoft/directx-graphics-samples/d3d12-hello-world-samples-win32/

You need to set up a swap chain, and there is no way to draw anything without vertex and pixel shaders i guess, so you need to write those too.

I looked at the tutorial and am using it as boilerplate code to learn dx12. I still cannot draw a rectangle.

pbivens67 said:
I still cannot draw a rectangle.

Some potential reasons, assuming you tried to add a second triangle as shown above in commented code:

Vertex buffer should increase in size, affecting the command list used to upload the buffer.

Triangle draw count should increase, maybe affecting the DrawInstanced command.

Expect frustration, learning nothing, and wasting time you could spend on learning something useful. ‘Doing your best’ may mean to do something you do not want to do initially, like learning to use data structures, learning to use some framework or engine, etc.

Learning low level gfx API only makes sense for experienced graphics programmers. But before you can be that, you need to improve general programming skills.
After you have them, to become an experienced gfx programmer, you will make faster progress by using high level API like OpenGL or DX11.
Maybe, after many years, you will hit some performance issues, which eventually can be solved using low level APIs. Only then you start to deal with this pain. This was my reason at least.

More or less, all people here try to give you the same advise (again and again). Statistical conclusion: People are right, you are wrong.

@JoeJ are you good at dx12?

I finally got Dx12 to draw a rectangle I had to adjust the DrawInstanced function. thanks joe.

pbivens67 said:
are you good at dx12?

No, i use Vulkan, which is very similar. But i cared only about compute shaders. My project became two times faster because we can now upload command lists and reuse them every frame. This solved my problem of having too many GPU → CPU → GPU interactions, which slowed everything down using OpenGL or OpenCL. So i really like low level API for compute.

But for rendering the complexity is much higher. What was a single line of code in OpenGL is now 10 to 50 lines. And it's not just one call - we need to allocate buffer memory, copy stuff, put my commands into command lists, upload them, execute them.
We need to care about synchronization using fences and semaphores, we need to care about resource transitions, descriptors to tell which shaders use which resources, pipelines, etc.

Just look at a texturing example. Texture goes to a staging buffer first, then from there it goes to VRAM. And you don't do this with a simple API call. You need to describe the process in a command list, allocate and free all memory on your own.
With OpenGL, that's one line of code. With low level, you already spend a day or more to learn all this, you check examples, wonder about subtle differences in the examples. You read API docs, but it sounds like patents and you're left with questions and assumptions. You're not sure if what works now will work on other GPUs as well, and if your method is efficient or not. Etc.

With your current state of copy paste coding instead of designing reusable functions and data structures, the complexity is too high for you to manage. It simply becomes way too much code to maintain.
Maybe you succeed on making a Pong game (assuming you already have a working Pong game?), but it will be a mess. API code will be orders of magnitudes more than the actual game, and it will be useless to other, future games.

Have fun anyway, if you enjoy it so much to figure out how APIs work.
But after that fruitless endeavour, go back to back to serious learning.
Last time we found out you have a problem with collision detection on multiple objects. You can do it for two, but if it's more, you hit some wall. And it showed your problem is you do not understand the power of the struct and/or class keywords.
This, and only this is what you must learn first!

Make a Pong game, but ensure you handle both paddles with the same struct or class, using two instances of said struct.
Make a Space Invaders game, but ensure you handle all enemies with the same struct or class, using multiple instances of said struct.
Make a PacMan game, but ensure you handle all ghosts with the same struct or class, using multiple instances of said struct.
Make a Breakout game, but ensure you handle all blocks with the same struct or class, using multiple instances of said struct.

Most important: Don't start another project BEFORE YOUR FIRST GAME ACTUALLY WORKS.

Only after that you have understood how programming works.

This topic is closed to new replies.

Advertisement