Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ The CPU backend keeps the same Silky API, but rasterizes into a Pixie image and
presents it through Windy. It is currently intended for simple Windows testing
and bring-up work where a GPU backend is not available.

All backends support independent images through `allocateTexture`,
`uploadTextureRows`, and `drawTexture`. Pass `nearest = true` when allocating a
texture for pixel art. Linear sampling is the default. Call `deleteTexture`
when the image is no longer needed.

## Theming

Silky supports theming via the `theme` object:
Expand Down
46 changes: 11 additions & 35 deletions src/silky/contexts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -552,19 +552,12 @@ proc drawTexture*(
## Queues an independent image with ordinary clipping and layer ordering.
if texture == 0 or size.x <= 0 or size.y <= 0:
return
when not defined(useDirectX) and
not defined(useVulkan) and
not defined(useCpu):
let
layer = sk.drawer.currentLayer
first = sk.drawer.layers[layer].len
sk.drawQuad(pos, size, vec2(0), pixels, color)
for i in first ..< sk.drawer.layers[layer].len:
sk.drawer.layers[layer][i].texture = texture
else:
raise newException(
SilkyError, "Independent image textures require the OpenGL or Metal drawer."
)
let
layer = sk.drawer.currentLayer
first = sk.drawer.layers[layer].len
sk.drawQuad(pos, size, vec2(0), pixels, color)
for i in first ..< sk.drawer.layers[layer].len:
sk.drawer.layers[layer][i].texture = texture

proc drawTriangle*(
sk: Silky,
Expand Down Expand Up @@ -843,16 +836,9 @@ proc uploadAtlasRegion*(sk: Silky, x, y, width, height: int) =
not defined(useCpu):
sk.drawer.uploadAtlasRegion(sk.image, x, y, width, height)

proc allocateTexture*(sk: Silky, width, height: int): uint32 =
## Reserves independent image storage without changing the font atlas.
when not defined(useDirectX) and
not defined(useVulkan) and
not defined(useCpu):
sk.drawer.allocateTexture(width, height)
else:
raise newException(
SilkyError, "Independent image textures require the OpenGL or Metal drawer."
)
proc allocateTexture*(sk: Silky, width, height: int, nearest = false): uint32 =
## Reserves independent image storage with linear or nearest-neighbor sampling.
sk.drawer.allocateTexture(width, height, nearest)

proc uploadTextureRows*(
sk: Silky,
Expand All @@ -861,21 +847,11 @@ proc uploadTextureRows*(
firstRow, rowCount: int
) =
## Uploads selected image rows for incremental preparation across frames.
when not defined(useDirectX) and
not defined(useVulkan) and
not defined(useCpu):
sk.drawer.uploadTextureRows(texture, image, firstRow, rowCount)
else:
raise newException(
SilkyError, "Independent image textures require the OpenGL or Metal drawer."
)
sk.drawer.uploadTextureRows(texture, image, firstRow, rowCount)

proc deleteTexture*(sk: Silky, texture: uint32) =
## Releases independent image storage after its last queued frame.
when not defined(useDirectX) and
not defined(useVulkan) and
not defined(useCpu):
sk.drawer.deleteTexture(texture)
sk.drawer.deleteTexture(texture)

proc addAtlasImage*(sk: Silky, name: string, image: Image) =
## Pack an image into the runtime atlas and reupload the GPU texture.
Expand Down
59 changes: 53 additions & 6 deletions src/silky/drawers/cpu.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import
std/math,
pixie, vmath, windy
std/[math, tables],
pixie, vmath, windy,
../common

const
BackendName* = "CPU"
Expand All @@ -14,12 +15,19 @@ type
clipPos*: Vec2
clipSize*: Vec2
maskUv*: Vec2
texture*: uint32

DrawerTexture = object
image: Image
nearest: bool

Drawer* = ref object
## CPU-backed drawer state.
window: Window
framebuffer: Image
clearColor: ColorRGBX
textures: Table[uint32, DrawerTexture]
nextTexture: uint32
layers*: array[2, seq[DrawerVertex]]
currentLayer*: int
layerStack*: seq[int]
Expand Down Expand Up @@ -88,16 +96,50 @@ proc sampleNearest(image: Image, uv: Vec2): ColorRGBX {.inline.} =
y = clamp(floor(uv.y).int, 0, image.height - 1)
image.data[image.dataIndex(x, y)]

proc allocateTexture*(drawer: Drawer, width, height: int, nearest = false): uint32 =
## Creates separate pixel storage, leaving handle zero for the atlas.
if width <= 0 or height <= 0:
raise newException(SilkyError, "Image texture dimensions must be positive.")
if drawer.nextTexture == high(uint32):
raise newException(SilkyError, "Image texture handles are exhausted.")
let image = newImage(width, height)
inc drawer.nextTexture
result = drawer.nextTexture
drawer.textures[result] = DrawerTexture(image: image, nearest: nearest)

proc uploadTextureRows*(drawer: Drawer, texture: uint32, image: Image,
firstRow, rowCount: int) =
## Copies the selected rows into the independent image.
if texture notin drawer.textures or image.isNil:
raise newException(SilkyError, "Cannot upload an unknown image texture.")
let target = drawer.textures[texture].image
if target.width != image.width or target.height != image.height or
firstRow < 0 or rowCount < 0 or firstRow > image.height or
rowCount > image.height - firstRow:
raise newException(SilkyError, "Image texture upload bounds are invalid.")
if rowCount > 0:
let offset = firstRow * image.width
copyMem(target.data[offset].addr, unsafeAddr image.data[offset],
rowCount * image.width * sizeof(ColorRGBX))

proc deleteTexture*(drawer: Drawer, texture: uint32) =
drawer.textures.del(texture)

proc shadePixel(
atlas: Image,
texture: DrawerTexture,
uv: Vec2,
color: ColorRGBX,
maskUv: Vec2
): ColorRGBX {.inline.} =
let base = atlas.sampleNearest(uv)
let image = texture.image
let base =
if texture.nearest: image.sampleNearest(uv)
else: image.getRgbaSmooth(
clamp(uv.x - 0.5'f32, 0'f32, (image.width - 1).float32),
clamp(uv.y - 0.5'f32, 0'f32, (image.height - 1).float32))
if maskUv.x >= 0:
let
mask = atlas.sampleNearest(maskUv).r
mask = image.sampleNearest(maskUv).r
tintR = mixByte(255, color.r, mask)
tintG = mixByte(255, color.g, mask)
tintB = mixByte(255, color.b, mask)
Expand Down Expand Up @@ -133,6 +175,11 @@ proc rasterTriangle(
atlas: Image,
a, b, c: DrawerVertex
) =
let texture =
if a.texture == 0: DrawerTexture(image: atlas, nearest: true)
else: drawer.textures.getOrDefault(a.texture)
if texture.image.isNil:
return
let area = edge(a.pos, b.pos, c.pos)
if abs(area) <= 0.00001'f32:
return
Expand Down Expand Up @@ -183,7 +230,7 @@ proc rasterTriangle(
uv = interpolate(a.uv, b.uv, c.uv, wa, wb, wc)
color = interpolate(a.color, b.color, c.color, wa, wb, wc)
maskUv = interpolate(a.maskUv, b.maskUv, c.maskUv, wa, wb, wc)
src = shadePixel(atlas, uv, color, maskUv)
src = shadePixel(texture, uv, color, maskUv)
dstIdx = drawer.framebuffer.dataIndex(x, y)
drawer.framebuffer.data[dstIdx] =
blendOver(drawer.framebuffer.data[dstIdx], src)
Expand Down
Loading
Loading