Sign in to follow this  
CdrTomalak

DX11 About to enter the world of 2d animation under SlimDX...

Recommended Posts

The next phase in my 2d game framework is animation. At the moment I have just one sprite texture for my moving sprites.

As I am using SlimDX, I don't have the handy sprite-related classes that XNA has. As such, I need to understand the principles of using sprite sheets instead of separate sprite images.

Specifically:
1. What defines a sprite sheet?

Is this just a large image with multiple animation frames organised into their own tiles in that image? Or does it also have some kind of header file which tells you the vectors of the rectangled which bound each image?

2. How are textures mapped from the sprite sheet?

Is this achieved via calls in the C# calling program, or via the HLSL fx file? I am using DX11 by the way.


I'm sure with a little guidance I can get cracking on this. Thanks in advance anyways.[img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
I think I know what to do now.

http://spritesheetpacker.codeplex.com/

This will allow me to build a sprite sheet from separate images, which I can generate in GIMP easily enough. I would then need to map the texture using the texture coordinate attributes for each CustomVertex object (instance variable of a sprite) in my game.

I'll set the vectors as per the rectangle definitions in the spritesheet config file, and transform them through an orthogonal matrix.

I think this is the principle anyways.[img]http://public.gamedev.net//public/style_emoticons/default/cool.png[/img]

Share this post


Link to post
Share on other sites
OMG No ! Not sprite sheets if you are using DX11. Use a texture array, they are so much better. [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]. Tricky to setup and get to know but so much more powerful after that. Edited by Gavin Williams

Share this post


Link to post
Share on other sites
[quote name='Gavin Williams' timestamp='1347885008' post='4980854']
OMG No ! Not sprite sheets if you are using DX11. Use a texture array, they are so much better. [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]. Tricky to setup and get to know but so much more powerful after that.
[/quote]

Texture arrays are definitely very useful, but I don't think they are a replacement for sprite sheets.
Why do you consider texture arrays so much better for sprite storage?
They definitely have some useful properties, such as texture clamping, which is problematic with sprite sheets,
but what happens when individual sprites have different resolutions or different amounts of animation frames?

Share this post


Link to post
Share on other sites
Of course they are a replacement for sprite sheets. For me it was a no-brainer, one minute i was using sprite sheets and the next minute, life was easier. Texture array's remove the problems inherent to sprite-sheets.

You can contrive any number of scenarios that might break a technique / technology. But that doesn't change the fact that by and large, Texture arrays make sprite sheets (in their regular usage scenario) redundant.

And just thinking about animation frames ... you can have any number of animation frames you like for any particular sprite with texture arrays. Using texture arrays doesn't change how you interpret your sprite collection at the higher level or how you are sampling your sprites to form animation.

And in regards to sprites with different resolutions, how many different resolutions are you talking about ? You could only look at sprite sheets as being more appropriate for particular situations. You can store sprites with different resolutions into a texture array, that's no problem, if you're happy with burning some empty memory (I think compression will mitigate that cost), and if there are only a few exceptionally small/large sprites then you can divide them up, and pass them in via another texture slot, maybe on their own or in another array, though i suppose you might want to stick with a single sprite pipeline. Anyway im not arguing that sprite sheets aren't useful in those scenarios, im just not familar with having to do that, and i stand by my point that texture arrays are better than sprite sheets (in most circumstances).

I would concede though that if the OP described how they were going to use the sprites, what sizes etc, then perhaps Hyunkel's points should be considered if appropriate. Edited by Gavin Williams

Share this post


Link to post
Share on other sites
I agree that texture arrays are ideal in a situation where you have many sprites of the same (or nearly the same) size.
However, as you said, it depends entirely on what the OP is doing.
The last project I worked on for example, had completely arbitrary sprite sizes.

I suppose we both agree that if you can use texture arrays instead of sprite sheets, you should definitely go for it.

Share this post


Link to post
Share on other sites
Thanks guys - but I am not familiar with using texture arrays.

In terms of what I am seeking to achieve, I can explain.

For my simple Pacman game, upon detecting a valid keypress in the game look I initiate movement to the next tile on the game board. I want to change the texture for Pacman every pixel of movement. This means changing the texture of the sprite every iteration of the game loop.

The method of retrieving said texture is the issue here. Should this be via sprite sheet, whereby I just change the texture mapping, or should the texture itself be swapped out for another? If the latter, I take it that I can either a) load a new individual texture, or b) retrieve it from a pre-loaded array of textures. Option b) would certainly seem to be less expensive, since the overheads of the load method will be negated in favour of in-memory data retrieval.

That said, if a spritesheet is in memory, I am not clear on how computationally expensive this is compared against using a texture array.

Do I have the right idea here? [img]http://public.gamedev.net//public/style_emoticons/default/cool.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628327
    • Total Posts
      2982090
  • Similar Content

    • By GalacticCrew
      In some situations, my game starts to "lag" on older computers. I wanted to search for bottlenecks and optimize my game by searching for flaws in the shaders and in the layer between CPU and GPU. My first step was to measure the time my render function needs to solve its tasks. Every second I wrote the accumulated times of each task into my console window. Each second it takes around
      170ms to call render functions for all models (including settings shader resources, updating constant buffers, drawing all indexed and non-indexed vertices, etc.) 40ms to render the UI 790ms to call SwapChain.Present <1ms to do the rest (updating structures, etc.) In my Swap Chain description I set a frame rate of 60 Hz, if it's supported by the computer. It made sense for me that the Present function waits some time until it starts the next frame. However, I wanted to check, if this might be a problem for me. After a web search I found articles like this one, which states 
      My drivers are up-to-date so that's no issue. I installed Microsoft's PIX, but I was unable to use it. I could configure my game for x64, but PIX is not able to process DirectX 11.. After getting only error messages, I installed NVIDIA's NSight. After adjusting my game and installing all components, I couldn't get a proper result, because my game freezes after a few frames. I haven't figured out why. There is no exception or error message and other debug mechanisms like log messages and break points tell me the game freezes at the end of the render function after a few frames. So, I looked for another profiling tool and found Jeremy's GPUProfiler. However, the information returned by this tool is too basic to get an in-depth knowledge about my performance issues.
      Can anyone recommend a GPU Profiler or any other tool that might help me to find bottlenecks in my game and or that is able to indicate performance problems in my shaders? My custom graphics engine can handle subjects like multi-texturing, instancing, soft shadowing, animation, etc. However, I am pretty sure, there are things I can optimize!
      I am using SharpDX to develop a game (engine) based on DirectX 11 with .NET Framework 4.5. My graphics cards is from NVIDIA and my processor is made by Intel.
    • By GreenGodDiary
      SOLVED: I had written 
      Dispatch(32, 24, 0) instead of
      Dispatch(32, 24, 1)  
       
      I'm attempting to implement some basic post-processing in my "engine" and the HLSL part of the Compute Shader and such I think I've understood, however I'm at a loss at how to actually get/use it's output for rendering to the screen.
      Assume I'm doing something to a UAV in my CS:
      RWTexture2D<float4> InputOutputMap : register(u0); I want that texture to essentially "be" the backbuffer.
       
      I'm pretty certain I'm doing something wrong when I create the views (what I think I'm doing is having the backbuffer be bound as render target aswell as UAV and then using it in my CS):
       
      DXGI_SWAP_CHAIN_DESC scd; ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); scd.BufferCount = 1; scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_SHADER_INPUT | DXGI_USAGE_UNORDERED_ACCESS; scd.OutputWindow = wndHandle; scd.SampleDesc.Count = 1; scd.Windowed = TRUE; HRESULT hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &scd, &gSwapChain, &gDevice, NULL, &gDeviceContext); // get the address of the back buffer ID3D11Texture2D* pBackBuffer = nullptr; gSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer); // use the back buffer address to create the render target gDevice->CreateRenderTargetView(pBackBuffer, NULL, &gBackbufferRTV); // set the render target as the back buffer CreateDepthStencilBuffer(); gDeviceContext->OMSetRenderTargets(1, &gBackbufferRTV, depthStencilView); //UAV for compute shader D3D11_UNORDERED_ACCESS_VIEW_DESC uavd; ZeroMemory(&uavd, sizeof(uavd)); uavd.Format = DXGI_FORMAT_R8G8B8A8_UNORM; uavd.ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D; uavd.Texture2D.MipSlice = 1; gDevice->CreateUnorderedAccessView(pBackBuffer, &uavd, &gUAV); pBackBuffer->Release();  
      After I render the scene, I dispatch like this:
      gDeviceContext->OMSetRenderTargets(0, NULL, NULL); m_vShaders["cs1"]->Bind(); gDeviceContext->CSSetUnorderedAccessViews(0, 1, &gUAV, 0); gDeviceContext->Dispatch(32, 24, 0); //hard coded ID3D11UnorderedAccessView* nullview = { nullptr }; gDeviceContext->CSSetUnorderedAccessViews(0, 1, &nullview, 0); gDeviceContext->OMSetRenderTargets(1, &gBackbufferRTV, depthStencilView); gSwapChain->Present(0, 0); Worth noting is the scene is rendered as usual, but I dont get any results from the CS (simple gaussian blur)
      I'm sure it's something fairly basic I'm doing wrong, perhaps my understanding of render targets / views / what have you is just completely wrong and my approach just makes no sense.

      If someone with more experience could point me in the right direction I would really appreciate it!

      On a side note, I'd really like to learn more about this kind of stuff. I can really see the potential of the CS aswell as rendering to textures and using them for whatever in the engine so I would love it if you know some good resources I can read about this!

      Thank you <3
       
      P.S I excluded the .hlsl since I cant imagine that being the issue, but if you think you need it to help me just ask

      P:P:S. As you can see this is my first post however I do have another account, but I can't log in with it because gamedev.net just keeps asking me to accept terms and then logs me out when I do over and over
    • By mister345
      Does buffer number matter in ID3D11DeviceContext::PSSetConstantBuffers()? I added 5 or six constant buffers to my framework, and later realized I had set the buffer number parameter to either 0 or 1 in all of them - but they still all worked! Curious why that is, and should they be set up to correspond to the number of constant buffers?
      Similarly, inside the buffer structs used to pass info into the hlsl shader, I added padding inside the c++ struct to make a struct containing a float3 be 16 bytes, but in the declaration of the same struct inside the hlsl shader file, it was missing the padding value - and it still worked! Do they need to be consistent or not? Thanks.
          struct CameraBufferType
          {
              XMFLOAT3 cameraPosition;
              float padding;
          };
    • By noodleBowl
      I was wondering if anyone could explain the depth buffer and the depth stencil state comparison function to me as I'm a little confused
      So I have set up a depth stencil state where the DepthFunc is set to D3D11_COMPARISON_LESS, but what am I actually comparing here? What is actually written to the buffer, the pixel that should show up in the front?
      I have these 2 quad faces, a Red Face and a Blue Face. The Blue Face is further away from the Viewer with a Z index value of -100.0f. Where the Red Face is close to the Viewer with a Z index value of 0.0f.
      When DepthFunc is set to D3D11_COMPARISON_LESS the Red Face shows up in front of the Blue Face like it should based on the Z index values. BUT if I change the DepthFunc to D3D11_COMPARISON_LESS_EQUAL the Blue Face shows in front of the Red Face. Which does not make sense to me, I would think that when the function is set to D3D11_COMPARISON_LESS_EQUAL the Red Face would still show up in front of the Blue Face as the Z index for the Red Face is still closer to the viewer
      Am I thinking of this comparison function all wrong?
      Vertex data just in case
      //Vertex date that make up the 2 faces Vertex verts[] = { //Red face Vertex(Vector4(0.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(100.0f, 100.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(100.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(0.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(0.0f, 100.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), Vertex(Vector4(100.0f, 100.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)), //Blue face Vertex(Vector4(0.0f, 0.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(100.0f, 100.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(100.0f, 0.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(0.0f, 0.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(0.0f, 100.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), Vertex(Vector4(100.0f, 100.0f, -100.0f), Color(0.0f, 0.0f, 1.0f)), };  
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
  • Popular Now