Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mizt
committed
Oct 21, 2021
1 parent
a13ea8e
commit 182054d
Showing
3 changed files
with
211 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#import <string> | ||
|
||
template <typename T> | ||
class PixelBuffer { | ||
|
||
private: | ||
|
||
std::string _type = ""; | ||
|
||
int _width = 0; | ||
int _height = 0; | ||
int _bpp = 0; | ||
|
||
void *_bytes = nullptr; | ||
unsigned int _rowbytes = 0; | ||
|
||
public: | ||
|
||
std::string type() { return this->_type; } | ||
|
||
int width() { return this->_width; } | ||
int height() { return this->_height; } | ||
int bpp() { return this->_bpp; } | ||
|
||
void *bytes() { return this->_bytes; } | ||
unsigned int rowBytes() { return this->_rowbytes; } | ||
|
||
PixelBuffer(int w, int h, int bpp=4) { | ||
this->_width = w; | ||
this->_height = h; | ||
this->_bpp = bpp; | ||
this->_type = @encode(T); | ||
if(this->_type=="I"&&bpp==4) { | ||
int rb = (int)ceil((w*bpp)/4.0)*sizeof(T); | ||
this->_bytes = (void *)new unsigned int[rb*h]; | ||
this->_rowbytes = rb; | ||
} | ||
else if(this->_type=="C"&&bpp==4) { | ||
int rb = (w*bpp)*sizeof(T); | ||
this->_bytes = (void *)new unsigned char[rb*h]; | ||
this->_rowbytes = rb; | ||
} | ||
else if(this->_type=="S"&&bpp==2) { | ||
int rb = (int)ceil(((w*bpp)/2.0)*2.0)*sizeof(T); | ||
this->_bytes = (void *)new unsigned int[rb*h]; | ||
this->_rowbytes = rb; | ||
} | ||
else if(this->_type=="f"&&(bpp==4||bpp==2||bpp==1)) { | ||
this->_bytes = (void *)new float[w*h*bpp]; | ||
this->_rowbytes = w*sizeof(T)*bpp; | ||
} | ||
else { | ||
NSLog(@"type = %s, bpp = %d is not supported",this->_type.c_str(),bpp); | ||
this->_type = ""; | ||
} | ||
} | ||
|
||
~PixelBuffer() { | ||
if(this->_type=="I") { | ||
delete[] (unsigned int *)this->_bytes; | ||
} | ||
else if(this->_type=="C") { | ||
delete[] (unsigned char *)this->_bytes; | ||
} | ||
else if(this->_type=="S") { | ||
delete[] (unsigned int *)this->_bytes; | ||
} | ||
else if(this->_type=="f") { | ||
delete[] (float *)this->_bytes; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
# MTLReadPixels | ||
|
||
For some reason [getBytes:bytesPerRow:fromRegion:mipmapLevel:](https://developer.apple.com/documentation/metal/mtltexture/1515751-getbytes?language=objc) for drawable's texture is very slow on M1, so copy drawable's texture by the compute shader and then get the bytes. | ||
For some reason [getBytes:bytesPerRow:fromRegion:mipmapLevel:](https://developer.apple.com/documentation/metal/mtltexture/1515751-getbytes?language=objc) for drawable's texture is very slow on M1and A14, so copy drawable's texture by the compute shader and then get the bytes. | ||
|
||
#### macosx | ||
xcrun -sdk macosx metal -c copy.metal -o copy.air; xcrun -sdk macosx metallib copy.air -o copy.metallib | ||
|
||
xcrun -sdk macosx metal -c copy.metal -o copy.air; xcrun -sdk macosx metallib copy.air -o copy-macosx.metallib | ||
|
||
#### iphoneos | ||
|
||
xcrun -sdk iphoneos metal -c copy.metal -o copy.air; xcrun -sdk iphoneos metallib copy.air -o copy.metallib | ||
xcrun -sdk iphoneos metal -c copy.metal -o copy.air; xcrun -sdk iphoneos metallib copy.air -o copy-iphoneos.metallib | ||
|
||
#### iphonesimulator | ||
|
||
xcrun -sdk iphonesimulator metal -c copy.metal -o copy.air; xcrun -sdk iphonesimulator metallib copy.air -o copy.metallib | ||
xcrun -sdk iphonesimulator metal -c copy.metal -o copy.air; xcrun -sdk iphonesimulator metallib copy.air -o copy-iphonesimulator.metallib | ||
|
||
#### remove copy.air | ||
|
||
rm ./copy.air | ||
|
||
|
||
| ||
|