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 Lizfin · Jul 10, 2019 at 06:26 PM · shadergraphicsshader writingdirectx 11

Use contents of RWStructuredBuffer written by shader in another shader

Hello,

I use a vertex-fragment shader to write stuff into a RWStructuredBuffer. I want to use the results inside another shader where I only need read access, so a StructuredBuffer would be enough. I just seem not to be able to make it work.

Basically what I do for the first shader, let's call it ShaderA
Script

 ComputeBuffer cf = new ComputeBuffer(nEntries, System.Runtime.InteropServices.Marshal.SizeOf<MyStruct>(), ComputeBufferType.Default);
 Graphics.SetRandomWriteTarget(1, cf);
 MyStruct[] myStructVals = new MyStruct[nEntries];
 cf.SetData(myStructVals, 0, 0, myStructVals.Length);
 Shader.SetGlobalBuffer("_CustomBuffer", cf);

Shader

 #ifdef SHADER_API_D3D11
     RWStructuredBuffer<MyStruct> _CustomBuffer : register(u1);
 #endif
 ....
 // write to it

What I write into it, I want to use in another shader, ShaderB. And here, I have struggle doing so. I have a working solution, but it is not optimal. So this is working although it gives an error (UAV registers live in the same name space as outputs, so they must be bound to at least u4, manual bind to slot u1 failed):

 #ifdef SHADER_API_D3D11
     RWStructuredBuffer<MyStruct> buffer  : register(u1);
 #endif

I guess it works because u1 is also used in the other shader, if I use the suggested u4, I cannot read values from there. I read somewhere, that the buffers stay in the same state unless altered by set buffer.
This also does not work:

 #ifdef SHADER_API_D3D11
     StructuredBuffer<MyStruct> buffer : register(t1);
 #endif

But it works for another StructuredBuffer of another type which gets registered to t0.
On the C# end, the first approach works if I assign the ComputeBuffer for ShaderA directly and if I copy the values into another, new, ComputeBuffer. Both things do not work for the StructuredBuffer approach.
Copy approach

 MyStruct[] temp = new MyStruct[ShaderAScript.BufferCount];
 ShaderAScript.cf.GetData(temp);
 buffer.SetData(temp);
 shaderBMaterial.SetBuffer("buffer", buffer);

Direct assign

 shaderBMaterial.SetBuffer("buffer", ShaderAScript.cf);
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

0 Replies

· Add your reply
  • Sort: 

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

187 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 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

Shader: Access current pixel screen position from within #ifdef SHADER_API_D3D11 0 Answers

Adding a clip() to the default shader? 0 Answers

Native rendering plugin D3D11 device type not being set in x86 builds. 0 Answers

Making shader not ignore Light on transparent areas ? 0 Answers

cast shadow - export lightmap from 3ds max to unity 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