Jump to content
  • Advertisement
Sign in to follow this  
GuyWithBeard

DX11 Find distinct colors in texture, using a Compute Shader

This topic is 1352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

I have a scenario where I need to find the distinct colors in a large number of textures. Currently I am speeding this up by splitting the textures into four parts and handing off each part to one CPU thread. However, a coworker suggested I could do it on the GPU with a Compute Shader. Now, I have some understanding of compute shaders but I am still wondering how this would be done. Eg. to find the distinct colors on the CPU I have a list of "already encountered" colors that I fill up as I need to. Can this be done on a Compute Shader using shared memory within the thread group? Also, I am not quite sure how to split up the work so that I get the best performance out of the Compute Shader.

 

Has anyone done something like this, or do you have any good online resources for me? I am using Unity, so I should have access to most of the DX11 Compute Shader features.

 

Thanks!

Share this post


Link to post
Share on other sites
Advertisement

I can suggest a different scheme than keeping a list of colours. I'd try it on the CPU first, but you might persuade it to work on a GPU too.

 

Create a bit array of length 256*256*256 - it should be 2MB. For each colour in the image, just set the appropriate bit in the array.

 

At the end of processing you can convert that bit array into a more useful format by iterating through it.

Share this post


Link to post
Share on other sites

That's interesting. On the C# side I can do this quite easily with a BitArray. However, how would I create such a bit array on the HLSL side? Can I just create an array of 32bit ints large enough to hold all the bits needed, and can I shift into that array like I would with a normal bitfield?

Share this post


Link to post
Share on other sites
Yeah, create a buffer of int32's, then make an unordered access view of that buffer so that you can bind it to your compute shader.
If multiple threads are sharing the same buffer though, you'll have to use an atomic operation to OR in the bits.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!