Hi, my current job does not run faster than solution for main thread. What am i wrong ?
This is my job and the way i use it on main thread.
public struct ImagePixel
{
public Vector2 Position;
public Color Color;
}
[BurstCompile]
public struct PaintingJob : IJobParallelFor
{
public int BrushScale;
public int BrushWidth;
public int BrushHeight;
public int TargetWidth;
public int TargetHeight;
public int PaintPositionX;
public int PaintPositionY;
public Color BrushColor;
public NativeArray<ImagePixel> ImagePixelColors;
public void Execute(int index)
{
int i = index % BrushHeight;
int j = index / BrushWidth;
int x = PaintPositionX + i - BrushHeight * BrushScale / 2;
int y = PaintPositionY + j - BrushWidth * BrushScale / 2;
if (x < 0 || x >= TargetWidth || y < 0 || y >= TargetHeight) return;
ImagePixelColors[index] = new ImagePixel
{
Color = BrushColor,
Position = new Vector2(x, y)
};
}
}
This is Function when i execute my job on Update:
void PaintingWithBurst()
{
_imagePixels = new NativeArray<Vector2>(_textureTarget.width * _textureTarget.height, Allocator.TempJob);
_imagePixelColors =
new NativeArray<ImagePixel>(_textureTarget.width * _textureTarget.height, Allocator.TempJob);
Vector3 screenPosition = Input.mousePosition;
screenPosClicked = _camera.ScreenToViewportPoint(screenPosition);
_paintingJob = new PaintingJob
{
BrushScale = brushScale,
BrushWidth = _textureBrush.width,
BrushHeight = _textureBrush.height,
TargetWidth = _textureTarget.width,
TargetHeight = _textureTarget.height,
PaintPositionX = (int)(screenPosClicked.x * _textureTarget.width),
PaintPositionY = (int)(screenPosClicked.y * _textureTarget.height),
BrushColor = brushColor,
ImagePixelColors = _imagePixelColors,
};
_jobHandle = _paintingJob.Schedule(_textureBrush.width * _textureBrush.height, 64);
_jobHandle.Complete();
_imagePixelColors = _paintingJob.ImagePixelColors;
for (int i = 0; i < _imagePixelColors.Length; i++)
{
int x = (int)_imagePixelColors[i].Position.x;
int y = (int)_imagePixelColors[i].Position.y;
_textureTarget.SetPixel(x, y, _imagePixelColors[i].Color);
}
_textureTarget.Apply();
_imagePixels.Dispose();
_imagePixelColors.Dispose();
}
And this is my old code version for main thread(not using job burst), and it run faster:
void Painting()
{
// Get the position of the mouse click in screen space
Vector3 screenPosition = Input.mousePosition;
// Convert the screen space position to img space
screenPosClicked = _camera.ScreenToViewportPoint(screenPosition);
// Convert the screen space position to world space
worldPosClicked = _camera.ScreenToWorldPoint(screenPosition);
// Calculate the x and y indices of the pixel within the target image
int xAxisPixel = (int)(screenPosClicked.x * _textureTarget.width);
int yAxisPixel = (int)(screenPosClicked.y * _textureTarget.height);
for (int i = 0; i < _textureBrush.height * brushScale; i++)
{
for (int j = 0; j < _textureBrush.width * brushScale; j++)
{
int x = xAxisPixel + i - _textureBrush.height * brushScale / 2;
int y = yAxisPixel + j - _textureBrush.width * brushScale / 2;
if (x < 0 || x >= _textureTarget.width || y < 0 || y >= _textureTarget.height) continue;
Color eraseColor = _textureBrush.GetPixel(i % _textureBrush.width, j % _textureBrush.height);
Color currentColor = _textureTarget.GetPixel(x, y);
Color newColor = Color.Lerp(currentColor, brushColor, brushStrength * eraseColor.a);
_textureTarget.SetPixel(x, y, newColor);
}
}
_textureTarget.Apply();
}
This is the way i call my job and normal solution on main thread:
void Update()
{
if (Input.GetMouseButton(0))
{
PaintingWithBurst();
// Painting();
}
}
This is profiler while i run with burst - PaintingWithBurst():
This is profiler while i run without burst - Painting():
Note: I enabled Burst Compilation, Safety Check On.
Noy have any Log Error. And it run Slower than my old Solution. What am i wrong ?