Skip to content

Commit

Permalink
kramv - add atlas grid
Browse files Browse the repository at this point in the history
This cycles through 32, 64, 128, 256 entries, since these are common atlasing sizes.
Now the d key and shift-d advance through none, pixel, block and the 4 atlas grid sizes.
  • Loading branch information
alecazam committed May 9, 2021
1 parent 8a6ec89 commit 3dbaf86
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
5 changes: 4 additions & 1 deletion kramv/KramRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,15 @@ - (void)_updateGameState
uniforms.gridY = 1;
}
else if (_showSettings->isBlockGridShown) {

if (_showSettings->blockX > 1) {
uniforms.gridX = _showSettings->blockX;
uniforms.gridY = _showSettings->blockY;
}
}
else if (_showSettings->isAtlasGridShown) {
uniforms.gridX = _showSettings->gridSize;
uniforms.gridY = _showSettings->gridSize;
}

// no debug mode when preview kicks on, make it possible to toggle back and forth more easily
uniforms.debugMode = _showSettings->isPreview ? ShaderDebugMode::ShDebugModeNone : (ShaderDebugMode)_showSettings->debugMode;
Expand Down
6 changes: 5 additions & 1 deletion kramv/KramViewerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class ShowSettings {
// draw a 1x1 or blockSize grid, note ASTC has non-square grid sizes
bool isPixelGridShown = false;
bool isBlockGridShown = false;
bool isAtlasGridShown = false;

// show all mips, faces, arrays all at once
bool isShowingAllLevelsAndMips = false;
Expand Down Expand Up @@ -107,10 +108,13 @@ class ShowSettings {
int32_t imageBoundsX = 0; // px
int32_t imageBoundsY = 0; // px

// size of the block, uses in block grid drawing
// size of the block, used in block grid drawing
int32_t blockX = 1;
int32_t blockY = 1;

// set when isGridShow is true
int32_t gridSize = 1;

// for eyedropper, lookup this pixel value, and return it to CPU
int32_t textureLookupX = 0;
int32_t textureLookupY = 0;
Expand Down
61 changes: 46 additions & 15 deletions kramv/KramViewerMain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1232,28 +1232,59 @@ - (void)keyDown:(NSEvent *)theEvent
break;

// toggle pixel grid when magnified above 1 pixel, can happen from mipmap changes too
case Key::D:
case Key::D: {
static int grid = 0;
static const int kNumGrids = 7;

#define advanceGrid(g, dec) \
grid = (grid + kNumGrids + (dec ? -1 : 1)) % kNumGrids

// TODO: display how many blocks there are
if (isShiftKeyDown && _showSettings->blockX > 1) {
// if block size is 1, then this shouldn't toggle
_showSettings->isBlockGridShown = !_showSettings->isBlockGridShown;
_showSettings->isPixelGridShown = false;
sprintf(text, "Block Grid %dx%d %s",
_showSettings->blockX, _showSettings->blockY,
_showSettings->isBlockGridShown ? "On" : "Off");

// if block size is 1, then this shouldn't toggle
_showSettings->isBlockGridShown = false;
_showSettings->isAtlasGridShown = false;
_showSettings->isPixelGridShown = false;

advanceGrid(grid, isShiftKeyDown);

if (grid == 2 && _showSettings->blockX == 1) {
// skip it
advanceGrid(grid, isShiftKeyDown);
}

static const uint32_t gridSizes[kNumGrids] = {
0, 1, 2,
32, 64, 128, 256 // atlas sizes
};

if (grid == 0) {
sprintf(text, "Grid Off");
}
else if (grid == 1) {
_showSettings->isPixelGridShown = true;

sprintf(text, "Pixel Grid 1x1 On");
}
else if (grid == 2) {
_showSettings->isBlockGridShown = true;

sprintf(text, "Block Grid %dx%d On",
_showSettings->blockX, _showSettings->blockY);
}
else {

_showSettings->isPixelGridShown = !_showSettings->isPixelGridShown;
_showSettings->isBlockGridShown = false;
text = "Pixel Grid ";
text += _showSettings->isPixelGridShown ? "On" : "Off";
_showSettings->isAtlasGridShown = true;

_showSettings->gridSize = gridSizes[grid];

sprintf(text, "Atlas Grid %dx%d On",
_showSettings->gridSize, _showSettings->gridSize);
}

isChanged = true;

break;

}
case Key::S:
// TODO: have drawAllMips, drawAllLevels, drawAllLevelsAndMips
_showSettings->isShowingAllLevelsAndMips = !_showSettings->isShowingAllLevelsAndMips;
Expand Down
7 changes: 5 additions & 2 deletions libkram/kram/KramImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ bool Image::loadImageFromKTX(const KTXImage& image)
case MyMTLPixelFormatR8Unorm:
case MyMTLPixelFormatRG8Unorm:
#if SUPPORT_RGB
case MyMTLPixelFormatRGB8Unorm_sRGB_internal: // TODO: not handling srgba yet
case MyMTLPixelFormatRGB8Unorm_sRGB_internal:
case MyMTLPixelFormatRGB8Unorm_internal:
#endif
case MyMTLPixelFormatRGBA8Unorm_sRGB: // TODO: not handling srgba yet
case MyMTLPixelFormatRGBA8Unorm_sRGB:
case MyMTLPixelFormatRGBA8Unorm:
{
const uint8_t* srcPixels =
Expand All @@ -144,6 +144,7 @@ bool Image::loadImageFromKTX(const KTXImage& image)
int32_t numDstChannels = 4;

// Note: clearing unspecified channels to 0000, not 0001
// can set swizzleText when encoding
_pixels.resize(4 * _width * _height);
if (numSrcChannels != 4) {
memset(_pixels.data(), 0, _pixels.size());
Expand Down Expand Up @@ -185,6 +186,7 @@ bool Image::loadImageFromKTX(const KTXImage& image)
int32_t numDstChannels = 4;

// Note: clearing unspecified channels to 0000, not 0001
// can set swizzleText when encoding
_pixelsFloat.resize(_width * _height);
if (numSrcChannels != 4) {
memset(_pixelsFloat.data(), 0,
Expand Down Expand Up @@ -240,6 +242,7 @@ bool Image::loadImageFromKTX(const KTXImage& image)
int32_t numDstChannels = 4;

// Note: clearing unspecified channels to 0000, not 0001
// can set swizzleText when encoding
_pixelsFloat.resize(_width * _height);
if (numSrcChannels != 4) {
memset(_pixelsFloat.data(), 0,
Expand Down

0 comments on commit 3dbaf86

Please sign in to comment.