下面列出了java.nio.ByteBuffer#putFloat ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void loadStructureData(ByteBuffer bb) {
bb.putFloat(precip6hours);
bb.putShort(snowDepth);
bb.putFloat(precip24hours);
bb.put(precipDuration);
bb.putShort(wavePeriod);
bb.putShort(waveHeight);
bb.put(waveDirection);
bb.putShort(waveSwellPeriod);
bb.putShort(waveSwellHeight);
bb.putFloat(sst);
bb.put(special);
bb.put(special2);
bb.put(shipCourse);
bb.put(shipSpeed);
bb.putFloat(waterEquiv);
}
@Override
public byte[] writeField(final float[] fieldValue) {
if (fieldValue == null) {
return new byte[] {};
}
final ByteBuffer buf = ByteBuffer.allocate(4 * fieldValue.length);
for (final float value : fieldValue) {
buf.putFloat(value);
}
return buf.array();
}
/**
* @param pByteBuffer must be a direct Buffer.
* @param pSource
* @param pLength to copy in pSource.
* @param pOffset in pSource.
*/
public static void put(final ByteBuffer pByteBuffer, final float[] pSource, final int pLength, final int pOffset) {
if(BufferUtils.WORKAROUND_BYTEBUFFER_PUT_FLOATARRAY) {
BufferUtils.jniPut(pByteBuffer, pSource, pLength, pOffset);
} else {
for(int i = pOffset; i < (pOffset + pLength); i++) {
pByteBuffer.putFloat(pSource[i]);
}
}
pByteBuffer.position(0);
pByteBuffer.limit(pLength << 2);
}
/**
* ByteChannelへ書き込む
* @param channel
* @param value
* @param work
* @throws IOException
*/
public static void write(@NonNull final ByteChannel channel,
final float value,
@Nullable final ByteBuffer work) throws IOException {
final ByteBuffer buf = checkBuffer(work, 4);
buf.putFloat(value);
buf.flip();
channel.write(buf);
}
public ByteBuffer toDbb(ByteBuffer res, int index) {
res.putFloat(index + 0 * Float.BYTES, m00);
res.putFloat(index + 1 * Float.BYTES, m01);
res.putFloat(index + 2 * Float.BYTES, m02);
res.putFloat(index + 3 * Float.BYTES, m10);
res.putFloat(index + 4 * Float.BYTES, m11);
res.putFloat(index + 5 * Float.BYTES, m12);
res.putFloat(index + 6 * Float.BYTES, m20);
res.putFloat(index + 7 * Float.BYTES, m21);
res.putFloat(index + 8 * Float.BYTES, m22);
return res;
}
private static ByteBuffer generateRandomByteBuffer(int numElements, Supplier<Float> initializer) {
ByteBuffer result = ByteBuffer.allocateDirect(numElements * FLOAT_BYTES);
result.order(ByteOrder.nativeOrder());
for (int idx = 0; idx < numElements; idx++) {
result.putFloat(initializer.get());
}
result.rewind();
return result;
}
void putOne(ByteBuffer b, PrimitiveType t) {
switch (t) {
case BYTE: b.put((byte)0); break;
case CHAR: b.putChar('0'); break;
case SHORT: b.putShort((short)0); break;
case INT: b.putInt(0); break;
case LONG: b.putLong(0); break;
case FLOAT: b.putFloat(0); break;
case DOUBLE: b.putDouble(0); break;
}
}
private byte[] getBytes() {
//byte, byte[] or long
ByteBuffer buf = ByteBuffer.allocate((int)(bytesize));
for(Object inst : toOutputx) {
if(inst instanceof byte[]) {
buf.put((byte[])inst);
}else if(inst instanceof Long) {
buf.putLong((long)inst);
}else if(inst instanceof Integer) {
buf.putInt((int)inst);
}else if(inst instanceof Short) {
buf.putShort((short)inst);
}else if(inst instanceof Float) {
buf.putFloat((float)inst);
}else if(inst instanceof Double) {
buf.putDouble((double)inst);
}else if(inst instanceof Character) {
buf.putChar((char)inst);
}else if(inst instanceof Byte) {
buf.put((byte)inst);
}
}
return buf.array();
}
public void initShaderBuffer() {
ssbo = new GLShaderStorageBuffer();
ByteBuffer byteBuffer = memAlloc(Float.BYTES * 33 + Integer.BYTES * 6);
byteBuffer.put(BufferUtil.createByteBuffer(getWorldTransform().getWorldMatrix()));
byteBuffer.putInt(config.getUvScale());
byteBuffer.putInt(config.getTessellationFactor());
byteBuffer.putFloat(config.getTessellationSlope());
byteBuffer.putFloat(config.getTessellationShift());
byteBuffer.putFloat(config.getDisplacementScale());
byteBuffer.putInt(config.getHighDetailRange());
byteBuffer.putFloat(config.getChoppiness());
byteBuffer.putFloat(config.getKReflection());
byteBuffer.putFloat(config.getKRefraction());
byteBuffer.putInt(BaseContext.getConfig().getFrameWidth());
byteBuffer.putInt(BaseContext.getConfig().getFrameHeight());
byteBuffer.putInt(config.isDiffuse() ? 1 : 0);
byteBuffer.putFloat(config.getEmission());
byteBuffer.putFloat(config.getSpecularFactor());
byteBuffer.putFloat(config.getSpecularAmplifier());
byteBuffer.putFloat(config.getReflectionBlendFactor());
byteBuffer.putFloat(config.getBaseColor().getX());
byteBuffer.putFloat(config.getBaseColor().getY());
byteBuffer.putFloat(config.getBaseColor().getZ());
byteBuffer.putFloat(config.getFresnelFactor());
byteBuffer.putFloat(config.getCapillarStrength());
byteBuffer.putFloat(config.getCapillarDownsampling());
byteBuffer.putFloat(config.getDudvDownsampling());
byteBuffer.flip();
ssbo.addData(byteBuffer);
}
/**
* Writes this object to {@link ByteBuffer}.
*
* @param buf Buffer.
*/
@SuppressWarnings("ForLoopReplaceableByForEach")
void write(ByteBuffer buf) {
buf.putInt(intVal);
buf.putLong(longVal);
buf.put((byte)(boolVal ? 1 : 0));
buf.putInt(longArr.length);
for (long l : longArr)
buf.putLong(l);
buf.putInt(dblArr.length);
for (double d : dblArr)
buf.putDouble(d);
buf.putInt(list.size());
for (int i = 0; i < list.size(); i++)
buf.putFloat(list.get(i));
buf.putInt(map.size());
for (Map.Entry<Integer, Character> e : map.entrySet()) {
buf.putInt(e.getKey());
buf.putChar(e.getValue());
}
}
public byte[] getMarschaledBytes() throws UnsupportedEncodingException, BufferOverflowException {
ByteBuffer bb = ByteBuffer.allocate(16384);
bb.put((byte) 0x0); //version
bb.putLong(logtime); //8
bb.putInt(mVerbosityLevel); //4
bb.putInt(mLevel.getInt());
bb.putInt(mRessourceId);
if (mMessage == null || mMessage.length() == 0) {
bb.putInt(0);
} else {
marschalString(mMessage, bb);
}
if (mArgs == null || mArgs.length == 0) {
bb.putInt(0);
} else {
bb.putInt(mArgs.length);
for (Object o : mArgs) {
if (o instanceof String) {
bb.putChar('s');
marschalString((String) o, bb);
} else if (o instanceof Integer) {
bb.putChar('i');
bb.putInt((Integer) o);
} else if (o instanceof Float) {
bb.putChar('f');
bb.putFloat((Float) o);
} else if (o instanceof Double) {
bb.putChar('d');
bb.putDouble((Double) o);
} else if (o instanceof Long) {
bb.putChar('l');
bb.putLong((Long) o);
} else if (o == null) {
bb.putChar('0');
} else {
VpnStatus.logDebug("Unknown object for LogItem marschaling " + o);
bb.putChar('s');
marschalString(o.toString(), bb);
}
}
}
int pos = bb.position();
bb.rewind();
return Arrays.copyOf(bb.array(), pos);
}
public static void writeQuat4f(ByteBuffer bb, Quat4f q) {
bb.putFloat(q.x);
bb.putFloat(q.y);
bb.putFloat(q.z);
bb.putFloat(q.w);
}
void loadStructureData(ByteBuffer bb) {
bb.putFloat(press);
bb.putFloat(temp);
bb.putFloat(dewp);
bb.put(quality);
}
void loadStructureData(ByteBuffer bb) {
bb.putFloat(press);
bb.putShort(windDir);
bb.putShort(windSpeed);
bb.put(quality);
}
@Override
public boolean serialize(ByteBuffer byteBuffer) {
if (byteBuffer.remaining() < rowDataSize + 4 + (creator.columnCount << 2)) {
// Fast way to detect whether the remaining cap can hold the serialize data or not.
// We don't need to be exactualy acurrate.
return false;
}
int sizePos = byteBuffer.position();
// put size place holder.
byteBuffer.putInt(0);
int size = 0;
for (int colId = 0; colId < creator.columnCount; colId++) {
byteBuffer.putShort((short) colId);
size += 2;
// Store in real type instead of long to reduce size.
byte type = creator.columnTypes[colId].dataType;
switch (type) {
case ColumnType.INT:
byteBuffer.putInt(getInt(colId));
size += 4;
break;
case ColumnType.LONG:
byteBuffer.putLong(getLong(colId));
size += 8;
break;
case ColumnType.FLOAT:
byteBuffer.putFloat(getFloat(colId));
size += 4;
break;
case ColumnType.DOUBLE:
byteBuffer.putDouble(getDouble(colId));
size += 8;
break;
case ColumnType.STRING:
byte[] bytes = getRaw(colId);
byteBuffer.putShort((short) bytes.length);
byteBuffer.put(bytes);
size += 2 + bytes.length;
break;
default:
throw new IllegalStateException("column type " + type + " is illegal");
}
}
// Put the size info before.
byteBuffer.putInt(sizePos, size);
return true;
}
public TerrainChunk(Map<NodeComponentType, NodeComponent> components, QuadtreeCache quadtreeCache,
Transform worldTransform, Vec2f location, int levelOfDetail, Vec2f index) {
super(components, quadtreeCache, worldTransform, location, levelOfDetail, index);
try {
addComponent(NodeComponentType.MAIN_RENDERINFO, components.get(NodeComponentType.MAIN_RENDERINFO).clone());
addComponent(NodeComponentType.MESH_DATA, components.get(NodeComponentType.MESH_DATA).clone());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
LogicalDevice device = VkContext.getDeviceManager().getLogicalDevice(DeviceType.MAJOR_GRAPHICS_DEVICE);
VkRenderInfo renderInfo = getComponent(NodeComponentType.MAIN_RENDERINFO);
VkMeshData meshData = getComponent(NodeComponentType.MESH_DATA);
int pushConstantsRange = Float.BYTES * 42 + Integer.BYTES * 11;
ByteBuffer pushConstants = memAlloc(pushConstantsRange);
pushConstants.put(BufferUtil.createByteBuffer(getLocalTransform().getWorldMatrix()));
pushConstants.put(BufferUtil.createByteBuffer(getWorldTransform().getWorldMatrixRTS()));
pushConstants.putFloat(quadtreeConfig.getVerticalScaling());
pushConstants.putFloat(quadtreeConfig.getHorizontalScaling());
pushConstants.putInt(chunkConfig.getLod());
pushConstants.putFloat(chunkConfig.getGap());
pushConstants.put(BufferUtil.createByteBuffer(location));
pushConstants.put(BufferUtil.createByteBuffer(index));
for (int morphArea : quadtreeConfig.getLod_morphing_area()){
pushConstants.putInt(morphArea);
}
pushConstants.putInt(quadtreeConfig.getTessellationFactor());
pushConstants.putFloat(quadtreeConfig.getTessellationSlope());
pushConstants.putFloat(quadtreeConfig.getTessellationShift());
pushConstants.putFloat(quadtreeConfig.getUvScaling());
pushConstants.putInt(quadtreeConfig.getHighDetailRange());
pushConstants.flip();
VkPipeline graphicsPipeline = new GraphicsTessellationPipeline(device.getHandle(),
renderInfo.getShaderPipeline(), renderInfo.getVertexInput(),
VkUtil.createLongBuffer(renderInfo.getDescriptorSetLayouts()),
BaseContext.getConfig().getFrameWidth(),
BaseContext.getConfig().getFrameHeight(),
VkContext.getResources().getOffScreenFbo().getRenderPass().getHandle(),
VkContext.getResources().getOffScreenFbo().getColorAttachmentCount(),
BaseContext.getConfig().getMultisampling_sampleCount(),
pushConstantsRange, VK_SHADER_STAGE_ALL_GRAPHICS,
16);
CommandBuffer commandBuffer = new SecondaryDrawCmdBuffer(
device.getHandle(),
device.getGraphicsCommandPool(Thread.currentThread().getId()).getHandle(),
graphicsPipeline.getHandle(), graphicsPipeline.getLayoutHandle(),
VkContext.getResources().getOffScreenFbo().getFrameBuffer().getHandle(),
VkContext.getResources().getOffScreenFbo().getRenderPass().getHandle(),
0,
VkUtil.createLongArray(renderInfo.getDescriptorSets()),
meshData.getVertexBufferObject().getHandle(),
meshData.getVertexCount(),
pushConstants, VK_SHADER_STAGE_ALL_GRAPHICS);
renderInfo.setCommandBuffer(commandBuffer);
renderInfo.setPipeline(graphicsPipeline);
}
public static void copyArrayToBB(Array data, boolean isVlen, ByteBuffer out) {
IndexIterator iterA = data.getIndexIterator();
// VLEN
if (isVlen && data instanceof ArrayObject) {
while (iterA.hasNext()) {
Object inner = iterA.next();
assert (inner instanceof Array);
copyArrayToBB((Array) inner, isVlen, out);
}
return;
}
Class classType = data.getElementType();
if (classType == double.class) {
while (iterA.hasNext())
out.putDouble(iterA.getDoubleNext());
} else if (classType == float.class) {
while (iterA.hasNext())
out.putFloat(iterA.getFloatNext());
} else if (classType == long.class) {
while (iterA.hasNext())
out.putLong(iterA.getLongNext());
} else if (classType == int.class) {
while (iterA.hasNext())
out.putInt(iterA.getIntNext());
} else if (classType == short.class) {
while (iterA.hasNext())
out.putShort(iterA.getShortNext());
} else if (classType == char.class) {
byte[] pa = IospHelper.convertCharToByte((char[]) data.get1DJavaArray(DataType.CHAR));
out.put(pa, 0, pa.length);
} else if (classType == byte.class) {
while (iterA.hasNext())
out.put(iterA.getByteNext());
} else
throw new UnsupportedOperationException("Class type = " + classType.getName());
}
public byte[] getMarschaledBytes() throws UnsupportedEncodingException, BufferOverflowException {
ByteBuffer bb = ByteBuffer.allocate(16384);
bb.put((byte) 0x0); //version
bb.putLong(logtime); //8
bb.putInt(mVerbosityLevel); //4
bb.putInt(mLevel.getInt());
bb.putInt(mRessourceId);
if (mMessage == null || mMessage.length() == 0) {
bb.putInt(0);
} else {
marschalString(mMessage, bb);
}
if (mArgs == null || mArgs.length == 0) {
bb.putInt(0);
} else {
bb.putInt(mArgs.length);
for (Object o : mArgs) {
if (o instanceof String) {
bb.putChar('s');
marschalString((String) o, bb);
} else if (o instanceof Integer) {
bb.putChar('i');
bb.putInt((Integer) o);
} else if (o instanceof Float) {
bb.putChar('f');
bb.putFloat((Float) o);
} else if (o instanceof Double) {
bb.putChar('d');
bb.putDouble((Double) o);
} else if (o instanceof Long) {
bb.putChar('l');
bb.putLong((Long) o);
} else if (o == null) {
bb.putChar('0');
} else {
VpnStatus.logDebug("Unknown object for LogItem marschaling " + o);
bb.putChar('s');
marschalString(o.toString(), bb);
}
}
}
int pos = bb.position();
bb.rewind();
return Arrays.copyOf(bb.array(), pos);
}
protected void putIn(ByteBuffer byteBuffer, Field field, Object obj) throws IllegalAccessException {
if (obj == null)
return;
Class type = field.getType();
if (skipField(field))
return;
if (type.isArray()) {
putArrayIn(byteBuffer, field, obj);
return;
}
if (type.isEnum()) {
try {
int ordinal = getOrdinal(obj, field);
byteBuffer.put((byte)ordinal);
return;
} catch (Exception e) {
byteBuffer.put((byte) -1);
return;
}
}
PrimitiveType primitiveType = SerializationUtils.enumTypes.get(type);
if (primitiveType == null) {
if (String.class.equals(type)) {
this.insertString(byteBuffer, field, obj);
return;
}
Object fieldObject = field.get(obj);
if (fieldObject == null) {
byteBuffer.put((byte) -1);
return;
} else {
byteBuffer.put((byte) 1);
putIn(byteBuffer, getAllFields(fieldObject), fieldObject);
return;
}
}
switch (primitiveType) {
case INT:
byteBuffer.putInt(field.getInt(obj));
return;
case LONG:
byteBuffer.putLong(field.getLong(obj));
return;
case SHORT:
byteBuffer.putShort(field.getShort(obj));
return;
case CHAR:
byteBuffer.putChar(field.getChar(obj));
return;
case BYTE:
byteBuffer.put(field.getByte(obj));
return;
case BOOLEAN:
byteBuffer.put((byte) (field.getBoolean(obj) ? 1 : 0));
return;
case FLOAT:
byteBuffer.putFloat(field.getFloat(obj));
return;
case DOUBLE:
byteBuffer.putDouble(field.getDouble(obj));
return;
default:
throw new IllegalArgumentException(field.getType().toString());
}
}
@Override
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
buffer.putFloat((Float)object);
}