S
Silk.NET•3y ago
bjr29

ImageSharp thing

(Sorry for butting in) So I'm trying to load a texture from path, but I get this error:
public unsafe Texture(string path) {
Image<Rgba32> image = (Image<Rgba32>) Image.Load(path);

fixed (void* data = MemoryMarshal.GetReference(image.DangerousGetPixelRowMemory(0))) {
Load(data, (uint) image.Width, (uint) image.Height);
}

image.Dispose();
}
public unsafe Texture(string path) {
Image<Rgba32> image = (Image<Rgba32>) Image.Load(path);

fixed (void* data = MemoryMarshal.GetReference(image.DangerousGetPixelRowMemory(0))) {
Load(data, (uint) image.Width, (uint) image.Height);
}

image.Dispose();
}
No description
35 Replies
Perksey
Perksey•3y ago
will check this in a sec
bjr29
bjr29OP•3y ago
ok ty
bjr29
bjr29OP•3y ago
GitHub
Silk.NET/Texture.cs at f07a7d8e3f61c63a9dfa78adc3eaad74b280955d · d...
The high-speed OpenGL, OpenCL, OpenAL, OpenXR, GLFW, SDL, Vulkan, Assimp, and DirectX bindings library your mother warned you about. - Silk.NET/Texture.cs at f07a7d8e3f61c63a9dfa78adc3eaad74b280955...
TechPizza
TechPizza•3y ago
don't you need to call .AsSpan() or something similar on the return of DangerousGetPixelRowMemory?
bjr29
bjr29OP•3y ago
I'll check created a new error and windows snipping tool just crashed
bjr29
bjr29OP•3y ago
No description
Perksey
Perksey•3y ago
try this:
public unsafe Texture(string path)
{
using var image = (Image<Rgba32>) Image.Load(path);
fixed (void* data = image.DangerousGetPixelRowMemory(0).Span)
{
Load(data, (uint) image.Width, (uint) image.Height);
}
}
public unsafe Texture(string path)
{
using var image = (Image<Rgba32>) Image.Load(path);
fixed (void* data = image.DangerousGetPixelRowMemory(0).Span)
{
Load(data, (uint) image.Width, (uint) image.Height);
}
}
lol just realised @thomasmiz you were pinged onto this haha https://github.com/SixLabors/ImageSharp/issues/1739
bjr29
bjr29OP•3y ago
AsSpan() doesn't exist btw, just Span
Perksey
Perksey•3y ago
well then Span 😛
bjr29
bjr29OP•3y ago
😛 Btw it works now thanks
Perksey
Perksey•3y ago
np
roeyskoe
roeyskoe•3y ago
The examples should be updated at some point, since ImageSharp 2.0 changed some stuff. I would also recommend reading this: https://docs.sixlabors.com/articles/imagesharp/memorymanagement.html And note that images larger than 4MB currently do not work, it is fixed for the next release: https://github.com/SixLabors/ImageSharp/issues/1983
bjr29
bjr29OP•3y ago
ah ok I'll probably use jpg to get around that for now then if I need to
roeyskoe
roeyskoe•3y ago
To my understanding that will not help, since 4MB limit is regarding raw image data. So at 4bytes/pixel => ~1000x1000 image is the limit.
bjr29
bjr29OP•3y ago
Oh I see
roeyskoe
roeyskoe•3y ago
But I would still recommend going with Imagesharp 2, since updating later will bring some extra work 😄 And I would expect that they will release a bugfix patch soon-ish.
bjr29
bjr29OP•3y ago
Yeah, I don't want to rewrite code that's taken like 8 hours I'm writing a game engine and making everything very modular and simple as possible to use for the user Well just simple the old design was modular but it would take just too long to write
Perksey
Perksey•3y ago
it's a shame that's a dangerous method now (well, i suppose it always was, but you know what i mean)
bjr29
bjr29OP•3y ago
Yeah, does it affect anything or is it just a rename really?
Perksey
Perksey•3y ago
well it just conveys that it's an unsafe method if you don't know what you're doing
bjr29
bjr29OP•3y ago
Oh ok
Perksey
Perksey•3y ago
@jbsp is there any non-dangerous method you know of to get row memory? i.e. one that copies if necessary. or perhaps a way to iterate through the pixel memory in use for an image? (this would be cool and if that's possible and not already in there i'd look into it!)
Perksey
Perksey•3y ago
oh excellent (sorry for the ping james)
ThomasMiz
ThomasMiz•3y ago
Yeah, I've been checking in on those guys because I use them for loading images They want to make changes that could make images not be stored fully sequentially, which is a bit of a problem for us who want to use it to load textures
Perksey
Perksey•3y ago
yeah sounds like it but hey, at least the think roey linked looks pretty nice. could either be used to flatten the buffer and do one textimage or do a bunch of subimage calls.
Kai
Kai•3y ago
I think this is actually a good thing - you really should not be flushing the whole image to GPU at once anyways. Forcing you to tell the driver which part you are uploading, uploading that, and letting it do the invalidation etc improves perf and memory usage.
ThomasMiz
ThomasMiz•3y ago
Really? I thought it was better to do it all at once
Kai
Kai•3y ago
why?
ThomasMiz
ThomasMiz•3y ago
Less GL calls, so it only needs to do any checks once
Perksey
Perksey•3y ago
the driver will likely divvy it up internally for bus reasons anyways [an uninformed guess]
Kai
Kai•3y ago
GL I have no idea, but in vulkan it's for sure better. Not very informed about GL, but there needs to be some range of host-gpu shared memory that the texture is first loaded into, and then copied into GPU-only memory. There is usually only a small amount of host-gpu shared memory, so flushing a large texture all at once is not possible in the first place now in GL maybe it's best to give it all the data at once cause you don't get to know what these pieces should be sized, but from a driver & VK perspective you can't really flush the whole texture at once
ThomasMiz
ThomasMiz•3y ago
Why would it be more efficient for the user to send the texture in parts then? Wouldn't the driver be able to better optimize the sending if it knew everything from the start?
Perksey
Perksey•3y ago
with opengl perhaps, with vulkan the idea is the driver doesn't need to know anything because it trusts the user
Kai
Kai•3y ago
^ this, you are the only one who can ever know how much host memory your program is going to need and what size of upload buffer you can "afford" the driver can just make some guesses if you upload a huge texture and the driver has decided to send it over 4mb at a time, then those 4mb are dedicated to this task for some time if you now are trying to also stream mesh info to the GPU and are requesting some some visible memory for that, the driver has already taken those 4mb out of it's budget plus it has to do some complicated logic to try and reduce fragementation, as there is no way for it to know for how long each bit is taken up, and compacting is hardly possible
Want results from more Discord servers?
Add your server