Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mario8664 · Aug 02, 2019 at 02:03 PM · compute shaderrender texture

Use compute shader to convert RenderTexture to Vector4 array

I am making a picture analyzer which needs the accurate values of color. When I tried to parse float values from compute shader to my script via Texture2D, it's not accurate. For example, when parsing float 0.5 via the r channel texture, it will first convert to value 127, which represents "half of a channel". Then in my script, I use Texture2D.GetPixel(x, y) to read the color, it will convert 127 to 127/255 back. So float 0.5 from compute shader to my script will be converted to 0.498039……, which isn't I expected.

Therefore, I want to use an array to read the output value but not the texture.

However, It has some problem. I do a test. Here are my codes:

Compute shader

 #pragma kernel CSMain
 
 // Create a RenderTexture with enableRandomWrite flag and set it
 // with cs.SetTexture
 RWTexture2D<float4> input;
 RWStructuredBuffer<float4> output;
 
 [numthreads(16,16,1)]
 void CSMain (uint3 id : SV_DispatchThreadID)
 {
     // TODO: insert actual code here!
     uint index = id.x * 16 + id.y;
     //There should be some operations here, then I get the final value
     //Operations.....................
     
     //Then get the result here
     float4 finalValue = input[id.xy];
     output[index] = finalValue;
 }

Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TestParse : MonoBehaviour
 {
     public ComputeShader shader;
     public RenderTexture input;
     public Material material;
     
     private Vector4[] output;
     private ComputeBuffer buffer;
     private int kernel;
 
     private Texture2D texture;
     // Start is called before the first frame update
     void Start()
     {
         output = new Vector4[16 * 16];
         buffer = new ComputeBuffer(16 * 16, 4 * sizeof(float));
         kernel = shader.FindKernel("CSMain");
         input.enableRandomWrite = true;
         shader.SetTexture(kernel, "input", input);
         shader.SetBuffer(kernel, "output", buffer);
         
         texture = new Texture2D(16, 16, TextureFormat.RGBA32, false);
         texture.filterMode = FilterMode.Point;
         material.SetTexture("_MainTex", texture);
     }
 
     // Update is called once per frame
     void Update()
     {
         shader.Dispatch(kernel, 16, 16, 1);
         buffer.GetData(output);
 
         for (int i = 0; i < 16; i++)
         {
             for (int j = 0; j < 16; j++)
             {
                 int index = i * 16 + j;
                 texture.SetPixel(i, j, new Color(output[index].x, output[index].y, output[index].z, output[index].w));
             }
         }
         texture.Apply();
         
     }
 
     private void OnDisable()
     {
         buffer.Release();
     }
 }

In this example, I can parse the RenderTexture to the compute shader, then copy the value to a Vector4 array, then set the color of the test Texture2D based on the array. So in theory, the colors of the test Texture2D will be the same as the colors of the input RenderTexture.

However, this is what I get:

alt text

alt text

Does anyone knows how to solve this problem? If it can't be solve, do you know why? Thanks.

computeshadererror.png (111.4 kB)
gif-animation-001.gif (251.1 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kosowski · Sep 22, 2020 at 05:25 PM

If your input texture is 16x16, and your numthreads is [numthreads(16,16,1)], I think you should call Dispatch like Dispatch(kernel, 1, 1, 1).

Unity reference has a short description on this https://docs.unity3d.com/ScriptReference/ComputeShader.Dispatch.html

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

110 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

Related Questions

Writing to render texture in for loop not working (metal) 0 Answers

Problem with ComputeShader and RHalf RenderTexture 0 Answers

Can Image Effects Be Applied To Render Textures? 0 Answers

Prevent Render Texture clearing 1 Answer

How do I create a reflection render texture and assign it to be used as part of a shader? 0 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