- Home /
Interger return value in shader with R8 R16 RenderTexture
I am trying to construct a render texture with format R8 our R16 that is integer format. I only need 1 channel for my texture. My shader is very simple :
Properties
{
_ID ("ID", Int) = 1
}
int frag () : SV_Target
{
return _ID;
}
This code doesn't work and it seems that only float works for the return statement of the fragment shader. How can I use format R8 or R16 ? How can I convert my int to float and them float to int if it's impossible to store int ?
Answer by Renardjojo · Sep 19, 2021 at 05:39 PM
After some test in rederDock, shader is correctly create but I use custom render pass with my camera and VBO seams to be disable for undifned reason.
Here the corerct fragment shader used :
#version 450
#extension GL_ARB_explicit_attrib_location : require
#ifdef GL_ARB_shader_bit_encoding
#extension GL_ARB_shader_bit_encoding : enable
#endif
#define HLSLCC_ENABLE_UNIFORM_BUFFERS 1
#if HLSLCC_ENABLE_UNIFORM_BUFFERS
#define UNITY_UNIFORM
#else
#define UNITY_UNIFORM uniform
#endif
#define UNITY_SUPPORTS_UNIFORM_LOCATION 1
#if UNITY_SUPPORTS_UNIFORM_LOCATION
#define UNITY_LOCATION(x) layout(location = x)
#define UNITY_BINDING(x) layout(binding = x, std140)
#else
#define UNITY_LOCATION(x)
#define UNITY_BINDING(x) layout(std140)
#endif
uniform int _ID;
layout(location = 0) out int SV_Target0;
void main()
{
SV_Target0 = _ID;
return;
}
Here the disabled VBO :
So, I will try to create my own render pipeline to avoid this problem (And also because I need to create them for my feature). However, I'm not sure why it works with float because my VBO is also disabled ... maybe undefined behaviour is happening and luckily works with the float instruction ... I don't know
RESOLVE: If you use camera.Render, you need to wait the end of frame. To do this, you can use coroutine like :
IEnumerator Start()
{
while (true)
{
// Wait until all rendering + UI is done.
yield return new WaitForEndOfFrame();
YOUR_RENDER_FUNCTION();
}
}
Your answer
Follow this Question
Related Questions
How to Sample colors from a quad to a RenderTexture? 1 Answer
RenderTexture to Shader everyframe 0 Answers
Pixelart camera shader 2 Answers
[Unity SRP] How do I clear 3d RW RenderTexture? 1 Answer
CustomRenderTexture not working 5 Answers