Question by
ProfessorQu · Sep 27, 2021 at 08:36 AM ·
camerashadermovementrenderingrendertexture
PC blackscreens when "Failed to create RenderTexture",PC screen goes black when "Failed to create rendertexture"
I have tried to create a sort of Mandelbrot set explorer, however I can't find the solution to my problem. I have seen a few other questions where people told them to update their drivers, so I did but it didn't work.
The full error is D3D11: Failed to create RenderTexture (1104 x 585 fmt 19 aa 1), error 0x8007000e
The code I've been using is using a master script for the camera and a compute shader to generate the texture.
Master script to render to the camera (Camera component attached to object):
// Set camera
public Camera cam;
// Set shader and kernel
public ComputeShader shader;
// Set groups
int xGroups;
int yGroups;
// Set width and height
int width;
int height;
// Set aspect ratio and minimum zoom
public float aspectRatio = -1;
public float minZoom = 5e-5f;
// Set rendertexture
RenderTexture tex;
// Set max iterations
public int maxIterations = 200;
// Set the area, the x and y positions and x and y zooms
public Vector4 area = new Vector4(0, 0, 1, 1);
void Start()
{
Init();
}
private void Init()
{
// Setting width and height
Debug.Log("Setting width and height...");
width = cam.pixelWidth;
height = cam.pixelHeight;
// Calculating thread groups
Debug.Log("Calculating thread groups...");
// Calculate kernel groups
xGroups = Mathf.CeilToInt(width / 8f);
yGroups = Mathf.CeilToInt(height / 8f);
// Calculate aspect ratio
if (aspectRatio == -1)
{
Debug.Log("Setting aspect ratio...");
aspectRatio = (float)width / (float)height;
if (aspectRatio > 0)
{
area.z *= aspectRatio;
}
else
{
area.w *= aspectRatio;
}
}
// Create render texture
if (tex == null || tex.width != width || tex.height == height)
{
Debug.Log("Creating texture...");
tex = new RenderTexture(width, height, 24);
tex.enableRandomWrite = true;
tex.Create();
}
// Set shader parameters
SetShaderParameters();
}
void SetShaderParameters()
{
Debug.Log("Setting shader parameters...");
// Set width and height
shader.SetInt("_Width", width);
shader.SetInt("_Height", height);
// Set max iterations
shader.SetInt("_MaxIterations", maxIterations);
// Set area
shader.SetVector("_Area", area);
// Set result
shader.SetTexture(0, "Result", tex);
}
void HandleInputs()
{
// Checking for inputs
Debug.Log("Checking for inputs...");
// Zoom in
if (area.z * 0.99f > minZoom || area.w * 0.99f > minZoom)
{
if (Input.GetKey(KeyCode.LeftBracket))
{
area.z *= 0.99f;
area.w *= 0.99f;
}
}
// Zoom out
if (Input.GetKey(KeyCode.RightBracket))
{
area.z *= 1.01f;
area.w *= 1.01f;
}
// Move to the left
if (Input.GetKey(KeyCode.A)){
area.x -= 0.01f * area.z;
}
// Move to the right
if (Input.GetKey(KeyCode.D))
{
area.x += 0.01f * area.z;
}
// Move to the top
if (Input.GetKey(KeyCode.W))
{
area.y += 0.01f * area.z;
}
// Move to the bottom
if (Input.GetKey(KeyCode.S))
{
area.y -= 0.01f * area.z;
}
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Debug.Log("Rendering texture...");
Init();
Debug.Log("Dispatching shader...");
shader.Dispatch(0, xGroups, yGroups, 1);
// Rendering texture
Graphics.Blit(tex, destination);
}
private void Update()
{
// Handle inputs
HandleInputs();
}
private void OnDestroy()
{
// Release textures
tex.Release();
}
The compute shader I use to create the Mandelbrot set:
#pragma kernel CSMain
RWTexture2D<float4> Result;
unsigned int _Width, _Height;
uniform int _MaxIterations;
uniform float4 _Area;
float map(float value, float istart, float istop,
float ostart, float ostop)
{
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
float rgb(float v)
{
return sin(v) * 0.5 + 0.5;
}
[numthreads(8, 8, 1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
Result[id.xy] = float4(0, 0, 0, 0);
float halfWidth = _Width / 2;
float halfHeight = _Height / 2;
float a = map(id.x, 0, _Width, -1, 1);
float b = map(id.y, 0, _Height, -1, 1);
float2 ab = float2(a, b);
//float ca = -0.8;
//float cb = 0.156;
float2 c = _Area.xy + ab * _Area.zw;
float2 z;
int n = 0;
for (n = 0; n < _MaxIterations; n++)
{
float aa = z.x * z.x - z.y * z.y;
float bb = 2 * z.x * z.y;
z.x = aa + c.x;
z.y = bb + c.y;
if (length(z) > 2)
break;
}
if (n == _MaxIterations)
{
Result[id.xy] = float4(0, 0, 0, 1.0);
}
else
{
float v = sqrt((float) n / _MaxIterations);
Result[id.xy] = float4(rgb(v * 2), rgb(v * 5), rgb(v * 10), 1.0);
}
//float v = sqrt((float) n / _MaxIterations);
//float bright = (float) n / _MaxIterations;
//Result[id.xy] = float4(bright, bright, bright, 1.0);
}
Comment