Skip to content

Commit

Permalink
kramv - fix colorspace, fix stretch to checkerboard.
Browse files Browse the repository at this point in the history
Switch to simple srgb color space. The shader saturates hdr values, so despite rgba16f buffer, can't yet display HDR. This gets the colors to align with Preview and Photoshop.  Fix some warnings only on Intel.
  • Loading branch information
alecazam committed Jan 26, 2023
1 parent e7b044c commit b2aa6e5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 29 deletions.
67 changes: 43 additions & 24 deletions kramv/KramRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,15 @@ - (void)_loadMetalWithView:(nonnull MTKView *)view
// Important to set color space, or colors are wrong. Why doesn't one of these work (or the default)
// false is good for srgb -> rgba16f
// true is good for non-srgb -> rgba16f
CGColorSpaceRef viewColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear);
//bool pickOne = false;
// pickOne ? kCGColorSpaceSRGB : kCGColorSpaceLinearSRGB);
CGColorSpaceRef viewColorSpace;

// This doesn't look like Figma or Photoshop for a rgb,a = 255,0 to 255,1 gradient across a 256px wide rect. The shader is saturating
// the color to 0,1. So can get away with SRGB color space for now.
// This also lines up with Preview.
// viewColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear);

viewColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);

view.colorspace = viewColorSpace;

view.colorPixelFormat = MTLPixelFormatRGBA16Float;
Expand Down Expand Up @@ -1212,55 +1218,55 @@ - (void)resetSomeImageSettings:(BOOL)isNewFile
- (void)_updateGameState
{
/// Update any game state before encoding rendering commands to our drawable

Uniforms &uniforms =
*(Uniforms *)_dynamicUniformBuffer[_uniformBufferIndex].contents;

*(Uniforms *)_dynamicUniformBuffer[_uniformBufferIndex].contents;
uniforms.isNormal = _showSettings->texContentType == TexContentTypeNormal;
uniforms.doShaderPremul = _showSettings->doShaderPremul;
uniforms.isSigned = _showSettings->isSigned;
uniforms.isSwizzleAGToRG = _showSettings->isSwizzleAGToRG;

uniforms.isSDF = _showSettings->texContentType == TexContentTypeSDF;
uniforms.numChannels = _showSettings->numChannels;
uniforms.lightingMode = (ShaderLightingMode)_showSettings->lightingMode;

MyMTLTextureType textureType = MyMTLTextureType2D;
MyMTLPixelFormat textureFormat = MyMTLPixelFormatInvalid;
if (_colorMap) {
textureType = (MyMTLTextureType)_colorMap.textureType;
textureFormat = (MyMTLPixelFormat)_colorMap.pixelFormat;
}

uniforms.isCheckerboardShown = _showSettings->isCheckerboardShown;

// addressing mode
bool isCube = (textureType == MyMTLTextureTypeCube ||
textureType == MyMTLTextureTypeCubeArray);
bool doWrap = !isCube && _showSettings->isWrap;
bool doEdge = !doWrap;
bool doZero = !doEdge;
uniforms.isWrap = doWrap ? _showSettings->isWrap : false;

uniforms.isPreview = _showSettings->isPreview;

uniforms.isNormalMapPreview = false;
if (uniforms.isPreview) {
uniforms.isNormalMapPreview = uniforms.isNormal || (_normalMap != nil);

if (_normalMap != nil) {
uniforms.isNormalMapSigned =
isSignedFormat((MyMTLPixelFormat)_normalMap.pixelFormat);
isSignedFormat((MyMTLPixelFormat)_normalMap.pixelFormat);
uniforms.isNormalMapSwizzleAGToRG = false; // TODO: need a prop for this
}
}

// a few things to fix before enabling this
uniforms.useTangent = _showSettings->useTangent;

uniforms.gridX = 0;
uniforms.gridY = 0;

if (_showSettings->isPixelGridShown) {
uniforms.gridX = 1;
uniforms.gridY = 1;
Expand All @@ -1275,19 +1281,19 @@ - (void)_updateGameState
uniforms.gridX = _showSettings->gridSizeX;
uniforms.gridY = _showSettings->gridSizeY;
}

// no debug mode when preview kicks on, make it possible to toggle back and
// forth more easily
uniforms.debugMode = (ShaderDebugMode)_showSettings->debugMode;
uniforms.shapeChannel = (ShaderShapeChannel)_showSettings->shapeChannel;
uniforms.channels = (ShaderTextureChannels)_showSettings->channels;

// turn these off in preview mode, but they may be useful?
if (_showSettings->isPreview) {
uniforms.debugMode = ShaderDebugMode::ShDebugModeNone;
uniforms.shapeChannel = ShaderShapeChannel::ShShapeChannelNone;
}

// crude shape experiment
_showSettings->is3DView = true;
switch (_showSettings->meshNumber) {
Expand All @@ -1304,26 +1310,39 @@ - (void)_updateGameState
case 3:
_mesh = _meshSphereMirrored;
break;
// case 3: _mesh = _meshCylinder; break;
// case 3: _mesh = _meshCylinder; break;
case 4:
_mesh = _meshCapsule;
break;
}
uniforms.is3DView = _showSettings->is3DView;

// on small textures can really see missing pixel (3 instead of 4 pixels)
// so only do this on the sphere/capsule which wrap-around uv space
uniforms.isInsetByHalfPixel = false;
if (_showSettings->meshNumber >= 2 && doZero) {
uniforms.isInsetByHalfPixel = true;
}

_data->updateTransforms();

// this is an animated effect, that overlays the shape uv wires over the image
uniforms.isUVPreview = _showSettings->uvPreview > 0.0;
uniforms.uvPreview = _showSettings->uvPreview;


uniforms.uvToShapeRatio = 1.0f;
switch(_showSettings->meshNumber) {
case 0:
if (_showSettings->imageBoundsY)
uniforms.uvToShapeRatio = _showSettings->imageBoundsX / (float)_showSettings->imageBoundsY;
break;
case 2:
uniforms.uvToShapeRatio = 2.0f;
break;
case 4:
uniforms.uvToShapeRatio = 2.0f * M_PI * 0.3333f;
break;
}
uniforms.projectionViewMatrix = _data->_projectionViewMatrix;
uniforms.cameraPosition = _data->_cameraPosition;

Expand Down
2 changes: 1 addition & 1 deletion kramv/KramViewerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ bool Data::loadAtlasFile(const char* filename)

{
std::vector<double> values;
string_view atlasName = atlasProps["name"].get_string().value_unsafe();
// string_view atlasName = atlasProps["name"].get_string().value_unsafe();

uint64_t width = atlasProps["width"].get_uint64().value_unsafe();
uint64_t height = atlasProps["height"].get_uint64().value_unsafe();
Expand Down
1 change: 1 addition & 0 deletions kramv/Shaders/KramShaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ struct Uniforms {
simd::float4 modelMatrixInvScale2; // to supply inverse, w is determinant
simd::float3 cameraPosition; // world-space
float uvPreview;
float uvToShapeRatio;

bool isSigned;
bool isNormal;
Expand Down
6 changes: 5 additions & 1 deletion kramv/Shaders/KramShaders.metal
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,12 @@ float4 DrawPixels(
// fix that. Also make this scale with zoom.

// https://www.geeks3d.com/hacklab/20190225/demo-checkerboard-in-glsl/

float2 coord = in.texCoord;
coord.x *= uniforms.uvToShapeRatio;

float repeats = 20.0;
float2 checker = floor(repeats * in.texCoord);
float2 checker = floor(repeats * coord);
float selector = sign(fmod(checker.x + checker.y, 2.0));
float cb = mix(float(1), float(222.0/255.0), selector);

Expand Down
6 changes: 5 additions & 1 deletion libkram/bc7enc/bc7decomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ bool unpack_bc7_mode0_2(uint32_t mode, const uint64_t* data_chunks, color_rgba*
const uint32_t ENDPOINT_BITS = (mode == 0) ? 4 : 5;
const uint32_t ENDPOINT_MASK = (1 << ENDPOINT_BITS) - 1;
const uint32_t PBITS = (mode == 0) ? 6 : 0;
#ifndef BC7DECOMP_USE_SSE2
const uint32_t WEIGHT_VALS = 1 << WEIGHT_BITS;
#endif
const uint32_t PART_BITS = (mode == 0) ? 4 : 6;
const uint32_t PART_MASK = (1 << PART_BITS) - 1;

Expand Down Expand Up @@ -272,8 +274,10 @@ bool unpack_bc7_mode1_3_7(uint32_t mode, const uint64_t* data_chunks, color_rgba
const uint32_t ENDPOINT_MASK = (1 << ENDPOINT_BITS) - 1;
const uint32_t PBITS = (mode == 1) ? 2 : 4;
const uint32_t SHARED_PBITS = (mode == 1) ? true : false;
#ifndef BC7DECOMP_USE_SSE2
const uint32_t WEIGHT_VALS = 1 << WEIGHT_BITS;

#endif

const uint64_t low_chunk = data_chunks[0];
const uint64_t high_chunk = data_chunks[1];

Expand Down
8 changes: 6 additions & 2 deletions libkram/kram/KramImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ void KramEncoder::addBaseProps(const ImageInfo& info, KTXImage& dstImage) const
if (info.isNormal) {
dstImage.addChannelProps("Nrm.x,Nrm.y,X,X");
}
else if (info.isSRGBDst || info.isSourcePremultiplied) {
else if (info.isSRGBDst) {
// !hasAlpha doesn't change the channel designation
if (info.isPremultiplied || info.isSourcePremultiplied) {
dstImage.addChannelProps("Alb.ra,Alb.ga,Alb.ba,Alb.a");
Expand All @@ -1471,7 +1471,11 @@ void KramEncoder::addBaseProps(const ImageInfo& info, KTXImage& dstImage) const
dstImage.addChannelProps("Alb.r,Alb.g,Alb.b,Alb.a");
}
}

else if (info.isSourcePremultiplied)
{
dstImage.addChannelProps("Alb.ra,Alb.ga,Alb.ba,Alb.a");
}

// TODO: texture encode can depend on wrap vs. clamp state (f.e. normal map gen, sdf)
// and formsts like PVRTC must know wrap/clamp before encode
// address: Wrap, Clamp, MirrorWrap, MirrorClamp, BorderClamp, BorderClamp0
Expand Down

0 comments on commit b2aa6e5

Please sign in to comment.