Direct2D 1.1 and later

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Direct2D 1.1 and later

Post by albinopapa » October 1st, 2015, 1:15 am

Has anyone looked into using Vertex and/or Pixel shaders with Direct2D 1.1 or later? Have you had any luck? I don't just mean following along with the MSDN examples either, I mean writing your own custom effects. This is what I'm attempting to do for the game I'm working on and wondering if there is merit in continuing with the SSE, the Direct2D or just move to Direct3D.

Here are the pros and cons as I see them.
SSE:
- Pros:
-- Implementation is already in place.
-- I understand SSE a little better than I do either D2D or D3D.
-- Easier access to resources
-- Able to code in a language I'm already familiar with
- Cons
-- It's more than likely going to be slower than using the GPU

Direct2D:
- Pros:
-- More than likely faster than SSE
-- The original idea was to do this project without 3D models, and the game will have 2D elements anyway.
-- Initialization of Direct2D and most of it's resources are simple

- Cons: (As I seem them)
-- Implementing custom effects are one of the resources that aren't as straight forward as the rest of Direct2D's resources. Basically, there's a learning curve to implement them on top of the learning how the shaders themselves work.
-- Since it would be using vertex/pixel shaders, it seems pointless and should just use D3D.

Direct3D:
- Pros:
-- Definitely faster than SSE
-- Use of vertex/pixel shaders are a natural part of the API
-- 2D effects can be done in this API
- Cons:
-- A lot more initialization (I'm kind of lazy, but there is a deeper reason. I like to view my results as I go, so it's more of an instant gratification thing.)
-- I've not had very good luck writing my own shaders in the past, and usually can't figure out what I'm doing wrong.

An API I didn't mention is DirectCompute. I have yet to write my own shader for this API so I'm not sure how this will go. I know there are other GPGPU API's like OpenCL and CUDA, but hey it's Windows, so why not DirectCompute.
DirectCompute:
- Pros:
-- Like SSE, you have pretty good control over your resources and it's almost treated the same. The most efficient way to use it is with an array of data, like images.
-- Uses the gpu, so will be faster than SSE
- Cons:
-- Slower than using Direct3D for sure because of context switching between api's and because you would either have to send the manipulated data back to the cpu to be copied to the back buffer, or sent through the D3D pipeline anyway to have the pixel shader copy to the back buffer.

Anyway, perhaps you all can help influence my decision
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Direct2D 1.1 and later

Post by albinopapa » October 1st, 2015, 4:38 am

I'm going to try using Direct2D to draw the resources to a shared DXGI surface, then pass that as a shader resource to the shader pipeline, kind of like the DirectCompute way I mentioned above.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Direct2D 1.1 and later

Post by albinopapa » October 1st, 2015, 4:38 am

I'm going to try using Direct2D to draw the resources to a shared DXGI surface, then pass that as a shader resource to the shader pipeline, kind of like the DirectCompute way I mentioned above.

Process:
1) Load the image data into ID3D11Texture2D interfaces.
2) Get the underlying IDXGISurface interfaces by calling ID3D11Texture2D::QueryInterface(&pDxgiSurface).
3) Call ID2D1DeviceContext::CreateBitmapFromDxgiSurface and pass in each of the DXGI surfaces.
4) Create an ID3D11Texture2D rendertargetview to which Direct2D will render
5) Repeat first 3 steps and set that ID2D1Bitmap as the render target for Direct2D

That way I can draw the bitmaps using direct2d, then I can use it's render target as a shader resource in the shader pipeline of direct3d. Here's hoping.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Direct2D 1.1 and later

Post by chili » October 1st, 2015, 8:43 am

Personally, I would recommend biting the bullet and going with full D3D11 on this one. Gonna have to tackle shaders sooner or later ;)
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Direct2D 1.1 and later

Post by albinopapa » October 1st, 2015, 3:48 pm

Argh! So, I'm trying to create a quad that takes up the whole screen in the vertex shader. I've looked around and supposedly you can use a system-value semantic called SV_VertexID to set the position of the vertex that is being worked on, like here.

I get an error though when the FXC compiler tries to compile the shader saying that the value needs to be integral, which 'uint' is definitely an integral data type which is what I use, just like the example.

Code: Select all

error X4555: invalid type used for 'SV_VertexID' input semantics, must be integral.
I've included the source code, you will need Windows 8.1 or higher to be able to compile it.

Haven't tried just creating a vertex and index buffer, MSDN docs says it's possible to do without them.
Attachments
WicTut.zip
Dismiss the title, I started it thinking of doing a tutorial on the WIC API.
(12.55 KiB) Downloaded 210 times
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Direct2D 1.1 and later

Post by albinopapa » October 1st, 2015, 4:14 pm

Dang it, I should have caught this, but even though I have the code to compile the shader in the runtime using D3DCompileFromFile, I forgot that VS2012 onward calls FXC upon compiling the project, so if you right click on the hlsl files, you can set the entry point, shader type and version in the files properties. Turns out that the default is shader model 4 feature level 9.1 (i.e. DirectX 9 shaders) which doesn't support SV_VertexID;
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Direct2D 1.1 and later

Post by LuX » October 4th, 2015, 9:57 pm

I agree, I tried to make a game in D2D a long time a go, but ended scrapping it when I came to shaders. Implementing them is way harder than it needs to be. Plus there's nothing D2D can do that D3D can't, but D3D requires a bit more code, so there's that.

Once you manage to make a 2D framework using D3D, and implement a semi-automatic shader loader, it's gonna make things a lot easier and simpler. I encourage you start with making some drawing and loading functions that suit your needs, as that will not only teach you a lot about D3D, but also make the code much shorter.
ʕ •ᴥ•ʔ

Post Reply