Skip to content

Instantly share code, notes, and snippets.

@SoylentGraham
Created January 11, 2018 16:37
Show Gist options
  • Save SoylentGraham/438a8f3d24fd98aae05f4a297c7a319a to your computer and use it in GitHub Desktop.
Save SoylentGraham/438a8f3d24fd98aae05f4a297c7a319a to your computer and use it in GitHub Desktop.
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel ParsePositionColour
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
//RWTexture2D<float4> Result;
struct XyzRgba
{
float x;
float y;
float z;
float r;
float g;
float b;
float a;
int Valid;
};
RWStructuredBuffer<int> Data;
RWStructuredBuffer<int> LineStarts;
RWStructuredBuffer<XyzRgba> PositionColours;
bool IsStringDelin(int Char)
{
return ( Char == (int)'\n' ) ||
( Char == (int)'\r' ) ||
( Char == (int)'\0' );
}
bool IsValueDelin(int Char)
{
return IsStringDelin(Char) ||
( Char == (int)' ' ) ||
( Char == (int)',' );
}
int GetChar(int Pos)
{
// Data is bytes, on GPU (metal) they're int's of 4 bytes packed
// gr: and in DX11 we pretend they're packed in multiples of 4 anyway
int Element = ((uint)Pos) % 4;
Pos /= 4;
int Char = Data[Pos];
Char = (Char >> (Element*8)) & 0xff;
return Char;
}
bool ParseFloat(out float Value,inout int Pos)
{
float Major = 0;
float Minor = 0;
float Modifier = 1.0f;
bool ReadMajor = false;
bool ReadMinor = false;
if ( GetChar(Pos) == 0 )
{
Value = 10000+GetChar(Pos);
return false;
}
if (GetChar(Pos) == (int)'-') {
Pos++;
Modifier = -1.0f;
}
/*
int CharNumber = GetChar(Pos) - (int)'0';
Pos++;
Value = CharNumber;
return true;
*/
bool Eof = false;
// parse major
while(!Eof)
{
int Char = GetChar(Pos);
if ( IsStringDelin(Char) )
{
if ( ReadMajor )
{
Pos++;
Eof = true;
break;
}
Pos++;
return false;
}
Eof = Eof || IsValueDelin(Char);
if ( Eof )
{
Pos++;
break;
}
if (Char == (int)'.')
{
Pos++;
break;
}
// throw if non-number
int CharNumber = Char - (int)'0';
if (CharNumber < 0 || CharNumber > 9)
return false;
Major *= 10;
Major += CharNumber;
ReadMajor=true;
Pos++;
}
// parse minor
float MinorScale = 1.0f / 10.0f;
while(!Eof)
{
int Char = GetChar(Pos);
if ( IsStringDelin(Char) )
{
Pos++;
break;
}
Eof = Eof || IsValueDelin(Char);
if ( Eof )
{
Pos++;
break;
}
if ( Char == (int)'f' )
{
Pos++;
continue;
}
// throw if non-number
int CharNumber = Char - (int)'0';
if (CharNumber < 0 || CharNumber > 9)
return false;
Minor += CharNumber * MinorScale;
MinorScale /= 10.0f;
Pos++;
ReadMinor = true;
}
Value = Modifier * (Major + Minor);
return true;
}
[numthreads(8,1,1)]
void ParsePositionColour (uint3 id : SV_DispatchThreadID)
{
int LineIndex = id.x;
int LineStart = LineStarts[LineIndex];
// init
PositionColours[LineIndex].Valid = false;
PositionColours[LineIndex].a = 1;
PositionColours[LineIndex].x = -9999;
PositionColours[LineIndex].y = -9999;
PositionColours[LineIndex].z = -9999;
PositionColours[LineIndex].r = -9999;
PositionColours[LineIndex].g = -9999;
PositionColours[LineIndex].b = -9999;
int StringPos = LineStart;
if ( !ParseFloat( PositionColours[LineIndex].x, StringPos ) ) return;
if ( !ParseFloat( PositionColours[LineIndex].y, StringPos ) ) return;
if ( !ParseFloat( PositionColours[LineIndex].z, StringPos ) ) return;
if ( !ParseFloat( PositionColours[LineIndex].r, StringPos ) ) return;
if ( !ParseFloat( PositionColours[LineIndex].g, StringPos ) ) return;
if ( !ParseFloat( PositionColours[LineIndex].b, StringPos ) ) return;
PositionColours[LineIndex].r /= 255.0f;
PositionColours[LineIndex].g /= 255.0f;
PositionColours[LineIndex].b /= 255.0f;
PositionColours[LineIndex].Valid = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment