Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RisingMos · Mar 26, 2018 at 09:29 AM · shaderimage effectsgpucompute shader

[Compute Shader] Porting an Image Effect Shader - Kuwahara Filter

Hi All I am trying to port an image Effect Shader to Compute Shader because I want to learn compute shaders. I chose to port Kuwahara Filter because I am generally interested in image filtering. Below is my port which give a black screen. Any help will be appreciated as I am very new to this and there isn't much info on the subject around. The original image effect shader is in the link above.

 // 
 #pragma kernel CSMain
 #include "UnityCG.cginc"
 
 
 // Create a RenderTexture with enableRandomWrite flag and set it
 // with cs.SetTexture
 
 Texture2D<float4> Source;
 RWTexture2D<float4> Destination;
 int Radius = 5;
 SamplerState samplerSource;
 
 [numthreads(8,8,1)]
 void CSMain(uint3 id : SV_DispatchThreadID)
 {
 
 
     float3 mean[4] = {
     { 0, 0, 0 },
     { 0, 0, 0 },
     { 0, 0, 0 },
     { 0, 0, 0 }
     };
 
     float3 sigma[4] = {
         { 0, 0, 0 },
     { 0, 0, 0 },
     { 0, 0, 0 },
     { 0, 0, 0 }
     };
 
     float2 start[4] = { { -Radius, -Radius },{ -Radius, 0 },{ 0, -Radius },{ 0, 0 } };
 
 
     float2 pos;
     float3 col;
     uint width;
     uint height;
     uint levels;
     Source.GetDimensions(0, width, height, levels);
 
     float widthf = (float)width;
     float heightf = (float)height;
 
 
     for (int k = 0; k < 4; k++) {
         for (int i = 0; i <= Radius; i++) {
             for (int j = 0; j <= Radius; j++) {
                 pos = float2(i, j) + start[k];
                 col = Source.SampleLevel(samplerSource, float2(pos.x * 1.0/width, pos.y * 1.0/height), 0);
                 mean[k] += col;
                 sigma[k] += col * col;
             }
         }
     }
 
     float sigma2;
 
     float n = pow(Radius + 1, 2);
     float4 color = Source[id.xy];
     float min = 1;
 
     for (int l = 0; l < 4; l++) {
         mean[l] /= n;
         sigma[l] = abs(sigma[l] / n - mean[l] * mean[l]);
         sigma2 = sigma[l].r + sigma[l].g + sigma[l].b;
 
         if (sigma2 < min) {
             min = sigma2;
             color.rgb = mean[l].rgb;
         }
     }
 
     Destination[id.xy] = color;
 
 }
   
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by jister · Nov 11, 2021 at 12:21 AM

bit of a late reply, but this works for me:

 #pragma kernel CSMain
 #include "UnityCG.cginc"
 
 Texture2D<float4> source;
 RWTexture2D<float4> output;
 
 int _Radius = 10;
 SamplerState sampler_source;
 
 [numthreads(8, 8, 1)]
 void CSMain(uint3 id : SV_DispatchThreadID)
 {
     float3 mean[4] = {
     { 0, 0, 0 },
     { 0, 0, 0 },
     { 0, 0, 0 },
     { 0, 0, 0 }
     };
 
     float3 sigma[4] = {
     { 0, 0, 0 },
     { 0, 0, 0 },
     { 0, 0, 0 },
     { 0, 0, 0 }
     };
 
     float2 start[4] = { { -_Radius * .5, -_Radius * .5},{ -_Radius * .5, 0 },{ 0, -_Radius * .5},{ 0, 0 } };
 
     float2 pos;
     float3 col;
     
     for (int k = 0; k < 4; k++) {
         for (int i = 0; i <= _Radius; i++) {
             for (int j = 0; j <= _Radius; j++) {
                 pos = float2(i, j) + start[k];
                 col = source[id.xy + uint2(pos.x, pos.y)];
                 mean[k] += col;
                 sigma[k] += col * col;
             }
         }
     }
 
     float sigma2;
 
     float n = pow(_Radius + 1, 2);
     float4 color = source[id.xy];
     float min = 1;
 
     for (int l = 0; l < 4; l++) {
         mean[l] /= n;
         sigma[l] = abs(sigma[l] / n - mean[l] * mean[l]);
         sigma2 = sigma[l].r + sigma[l].g + sigma[l].b;
 
         if (sigma2 < min) {
             min = sigma2;
             color.rgb = mean[l].rgb;
         }
     }
 
     output[id.xy] = color;
 
 }

alt text


image-2021-11-11-071457.png (401.3 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by RisingMos · Nov 11, 2021 at 04:51 AM

Not too late :) Thanks! I will check it out. Also, you another new thing that you might want to Check out is a library called ComputeSharp (not unity related)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom colliders on the GPU 0 Answers

Compute Shader not work on older GPU with DX11 0 Answers

Is it possible to profile compute shaders? 2 Answers

RenderTexture.GrabPixels slowdown - How to fix? 0 Answers

DirectCompute 1byte data type... Is it exist? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges