#Drawing a material to a texture in an importer using URP

1 messages · Page 1 of 1 (latest)

bitter rose
#

I wonder if for my case I could skip the blit family of functions and use CoreUtils.DrawFullScreen... (brb trying that)

#

Hmm, same results, sparkly BS with CopyTexture and plain black without

twilit citrus
#

CopyTexture's output reminds me of unallocated memory, perhaps just garbage data floating around on the gpu

bitter rose
#

yep definitely looks like such

twilit citrus
#

Maybe try a sanity check: have your blit shader output just the color red or something

#

See if it's just _BlitTexture not being assigned for whatever reason

bitter rose
#

my blit shader is just that, I don't even declare the _BlitTexture

twilit citrus
#

Huh

bitter rose
#
Shader "Hidden/Keen Vectors/Blit Bezier"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline"}
        ZWrite Off
        Cull Off
        Pass
        {
            Name "ColorBlitPass"

            HLSLPROGRAM
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"

            #pragma vertex Vert
            #pragma fragment frag

            float4 frag (Varyings input) : SV_Target
            {
                 return float4(0, 1000, 0, 1);
            }
            ENDHLSL
        }
    }
}```
#

(i mean it will not be that in the end, but i haven't even began writing the shader part of this thing)

twilit citrus
#

Yeah, makes sense..

#

Try to debug.Log() in the importer. Maybe print the front.rt variable, see if it's null, and if it isn't see what it's dimensions are

#

Maybe the RTHandle isn't being created correctly?

bitter rose
#

the render texture looks alright, but now looking at the handle its weird that the referenceSize is 0

#

even though the system's reference size is non 0

#

(i changed the naming around since with DrawFullPass i dont need to double buffer)

twilit citrus
#

That sounds like it would do it, might be setting the texture's viewport to be 0x0 pixels

#

Explaining why outputting a solid color in the fragment shader doesn't even work

bitter rose
#

at that same point the system has a non 0 reference size though

twilit citrus
#

Is it possible to set a handle's reference size directly? Just to check?

bitter rose
#

maybe, it has a SetCustomHandleProperties method

twilit citrus
#

Another idea depending on how that goes, looking at the static Blitter class, it seems that the bitter as well needs an Initialize method. (Since it's a static class presumably it's called by the render pipeline automatically by the time you're in a monobehaviour)

bitter rose
twilit citrus
#

Try calling Blitter.Initialize()

#

It could be that the importer is so seperate from everything that there isn't even an initialized static class of blitter yet

bitter rose
#

true, but i'm not even using Blitter anymore, just CoreUtils spinning_think

twilit citrus
#

Huh

#

Time to take a look at CoreUtils.cs in that case lol

#

hold on

bitter rose
#

xD

#

it looks a bit magical, CoreUtils.DrawFullScreen calls CommandBuffer.DrawProcedural with a vertex count but i have no idea where those vertices come from

twilit citrus
#

Okay.. Best lead so far is that the textures themselves are the problem, since Blitter isn't involved, and copying them leads to garbage data.

bitter rose
#

yep

twilit citrus
#

RTHandle shouldn't even be the problem, since we are bypassing most things with CoreUtils()

bitter rose
#

what is a very common texture format for vulkan? as a test I just tried RenderTextureFormat.R8, which despite the fact i would not be able to use for my actual application, also yields a pure black texture. so i don't think the exotic 128bit per pixel format is to blame

#

well DefaultFormat.LDR is also black so i don't think its the formats fault anyways, i play video games in this GPU so i'm quite certain it supports at least one LDR rgb format lol

twilit citrus
#

Shouldn't be the format, hopefully.

#

Uhh. Maybe try using plain Rendertextures in the built in RP, to see if it works at all?

bitter rose
#

what do you mean?

#

like, slapping a RT on a camera just to see it works?

twilit citrus
#

Oops

#

I wrote Core RP not Built in

bitter rose
#

ah

#

that's probably a good idea lol

#

i can just unassign the RP asset in the project settings right?

twilit citrus
#

Yeah I think so

bitter rose
#

hmm, the plot thickens

#
var renderTexture = new RenderTexture(128, 128, GraphicsFormat.R32G32B32A32_SFloat, GraphicsFormat.None);
var brush = new Material(Shader.Find("Hidden/Keen Vectors/Blit Bezier"));
RenderTexture.active = renderTexture;
Graphics.Blit(Texture2D.whiteTexture, renderTexture, brush);
ctx.AddObjectToAsset(nameof(renderTexture), renderTexture);```
that should be about it, no?
twilit citrus
bitter rose
#

i now get the warning "Releasing render texture that is set to be RenderTexture.active!"

#

and i'm definitely not destroying the RT at all, this is all the code of my OnImportAsset currently

twilit citrus
#

Probably happens when the importer ends, and tries to cleanup anything left over

#

Like when the scope changes

bitter rose
#

yeah maybe it just doesn't like render textures in it

twilit citrus
#

Still all black?

bitter rose
#

aye

#

also tried sticking a Graphics.CopyTexture(renderTexture, output); to a texture2d of same dimensions and format and export that, the warning is gone but the output is still black

twilit citrus
#

Grasping at straws now.. Maybe the shader doesn't work anymore since you swapped to built in?

#

Like perhaps the vertex function provided by blit.hlsl only works on URP

bitter rose
#

thought so too, it is now even simpler: ```Shader "Hidden/Keen Vectors/Blit Bezier"
{
SubShader
{
Tags { "RenderType"="Opaque"}
ZWrite Off
Cull Off
Pass
{
Name "ColorBlitPass"

        HLSLPROGRAM
        #pragma fragment frag

        float4 frag (Varyings input) : SV_Target
        {
             return float4(100, 100, 100, 1);
        }
        ENDHLSL
    }
}

}

#

still black

#

i'm gonna make a new empty project lol, maybe unity is cursed

twilit citrus
#

Nowhere is Varyings defined, neither is a vertex function

bitter rose
#

oh true

#

no warnings tho

#

lovely

twilit citrus
#

That's kind of weird too

twilit citrus
bitter rose
#

and by lovely i mean i want to scream at a wall

twilit citrus
#

Isn't graphics programming just wonderful

bitter rose
#

ok there were secret warnings on the shader asset itself

twilit citrus
#

Ahhh

bitter rose
#

can i just return void from the vertex function?

twilit citrus
#

I see

bitter rose
#

unity doesnt complain

#

but then again

#

it doesnt have a good track record at this point in time

twilit citrus
#

you could probably just throw on the default image effect shader from the asset create menu

#

It just inverts colors

bitter rose
#

nope, all black

#

(i edited the default image effect to always output white)

#

"you might not like it but this is what peak graphics programming is like"

#

alright so on a clean, built in (not even Core RP package is installed) renderer, its also black 🤓

twilit citrus
twilit citrus
bitter rose
#

at this point im almost writing this 💩 on the CPU

twilit citrus
#

How fast does this asset importer even need to be? They can't stop you from writing it on the CPU...

bitter rose
#

well i plan on selling this so i would hope to not completely obliterate editor performance

twilit citrus
#

if only the frame debugger / RenderDoc could work on asset importers

bitter rose
#

indeed

#

or at all, i'm on linux and even though we have renderdoc, unitys integration specifically has an "if not linux" setting

twilit citrus
#

Damn.

#

This is starting to seem like a question for the forums / unity developers. (Assuming they respond)

#

I'm pretty sure my knowledge doesn't extend far enough to help much more here, aside from being a second set of eyes

bitter rose
#

oooop

#

output.Apply to the rescue?

twilit citrus
#

:0

bitter rose
#

never have i been so happy to see a white square

twilit citrus
#

Oh my god
That must feel good

bitter rose
#
var buffer = new RenderTexture(128, 128, GraphicsFormat.R32G32B32A32_SFloat, GraphicsFormat.None);
var source = new Texture2D(...);
var output = Instantiate(source);

var brush = new Material(Shader.Find("Hidden/CLOWN"));

Graphics.Blit(source, buffer, brush);
RenderTexture.active = buffer;
output.ReadPixels(new Rect(Vector2.zero, new(buffer.width, buffer.height)), 0, 0);
output.Apply();
RenderTexture.active = null;

this works, on built in empty project (shader = image effect set to return 1,1,1,1)

#

now i'll try backtracking

#

and to be sure, its not a white default color either, it is the shader. this is fixed4(sin(i.uv.x),0,0,1)

twilit citrus
#

The cat wishes you luck integrating with URP

bitter rose
#

backtrack step 1. same code works on the actual project in builtin, so it's not some satanic haunted setting

bitter rose
#

backtrack step 2. added the URP back onto the project settings, still imports fine with clown shader

#

backtrack step 3. straight up Graphics.Blit works with the clown shader

#

i wonder if unity is doing some shenanigans, gotta restart it to ensure its not using builtin or something behind the scenes

bitter rose
#

yep, restarted and it still works

#

backtrack step 4. _commands.Blit(_canvas, _canvas, material, pass) works (?! it doesnt even complain of reading and writing to the same thing anymore)

bitter rose
#

i am glad to declare

#

Backtrack Complete check

#

thank you so much again Kabinet for your technical and emotional support, as well as your cat

#

also thanks Joachim Ante for founding the abomination that dominates my professional life otherwise known as Unity

#

what was I doing wrong? i have absolutely no clue