Skip to content

Commit 3ea1fcd

Browse files
authored
Merge pull request #48 from Unity-Technologies/upstream-update
bring latest changes from upstream ( f60ac47e0193 )
2 parents 45cd512 + bffde14 commit 3ea1fcd

33 files changed

+3950
-2414
lines changed

include/UnityInstancingFlexibleArraySize.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@
44

55
#include <string>
66
#define UNITY_RUNTIME_INSTANCING_ARRAY_SIZE_MACRO "UNITY_RUNTIME_INSTANCING_ARRAY_SIZE"
7+
#define UNITY_PRETRANSFORM_CONSTANT_NAME "UnityDisplayOrientationPreTransform"
78

89
const unsigned int kArraySizeConstantID = 0;
10+
const unsigned int kPreTransformConstantID = 1;
911

1012
// TODO: share with Runtime/GfxDevice/InstancingUtilities.h
1113
inline bool IsUnityInstancingConstantBufferName(const char* cbName)
1214
{
1315
static const char kInstancedCbNamePrefix[] = "UnityInstancing";
1416
return strncmp(cbName, kInstancedCbNamePrefix, sizeof(kInstancedCbNamePrefix) - 1) == 0;
1517
}
18+
19+
inline bool IsPreTransformConstantBufferName(const char* cbName)
20+
{
21+
static const char kPreTransformCbNamePrefix[] = "UnityDisplayOrientationPreTransformData";
22+
return strncmp(cbName, kPreTransformCbNamePrefix, sizeof(kPreTransformCbNamePrefix) - 1) == 0;
23+
}

include/hlslcc.h

Lines changed: 231 additions & 31 deletions
Large diffs are not rendered by default.

include/pstdint.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ typedef uint_least64_t uint_fast64_t;
678678
# elif defined(__i386__) || defined(_WIN32) || defined(WIN32)
679679
# define stdint_intptr_bits 32
680680
# elif defined(__INTEL_COMPILER)
681-
/* TODO -- what did Intel do about x86-64? */
681+
#error Unknown compiler
682682
# endif
683683

684684
# ifdef stdint_intptr_bits
@@ -711,9 +711,7 @@ typedef uint_least64_t uint_fast64_t;
711711
typedef stdint_intptr_glue3 (uint, stdint_intptr_bits, _t) uintptr_t;
712712
typedef stdint_intptr_glue3 (int, stdint_intptr_bits, _t) intptr_t;
713713
# else
714-
/* TODO -- This following is likely wrong for some platforms, and does
715-
nothing for the definition of uintptr_t. */
716-
typedef ptrdiff_t intptr_t;
714+
#error Unknown compiler
717715
# endif
718716
# define STDINT_H_UINTPTR_T_DEFINED
719717
#endif

src/ControlFlowGraph.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
using namespace HLSLcc::ControlFlow;
1010
using HLSLcc::ForEachOperand;
1111

12-
const BasicBlock &ControlFlowGraph::Build(const Instruction *firstInstruction)
12+
const BasicBlock &ControlFlowGraph::Build(const Instruction* firstInstruction, const Instruction* endInstruction)
1313
{
1414
using std::for_each;
1515

1616
m_BlockMap.clear();
1717
m_BlockStorage.clear();
1818

1919
// Self-registering into m_BlockStorage so it goes out of the scope when ControlFlowGraph does
20-
BasicBlock *root = new BasicBlock(Utils::GetNextNonLabelInstruction(firstInstruction), *this, NULL);
20+
BasicBlock *root = new BasicBlock(Utils::GetNextNonLabelInstruction(firstInstruction), *this, NULL, endInstruction);
2121

2222
// Build the reachable set for each block
2323
bool hadChanges;
@@ -58,10 +58,11 @@ BasicBlock *ControlFlowGraph::GetBasicBlockForInstruction(const Instruction *ins
5858

5959
// Generate a basic block. Private constructor, can only be constructed from ControlFlowGraph::Build().
6060
// Auto-registers itself into ControlFlowGraph
61-
BasicBlock::BasicBlock(const Instruction *psFirst, ControlFlowGraph &graph, const Instruction *psPrecedingBlockHead)
61+
BasicBlock::BasicBlock(const Instruction *psFirst, ControlFlowGraph &graph, const Instruction *psPrecedingBlockHead, const Instruction* endInstruction)
6262
: m_Graph(graph)
6363
, m_First(psFirst)
6464
, m_Last(NULL)
65+
, m_End(endInstruction)
6566
{
6667
m_UEVar.clear();
6768
m_VarKill.clear();
@@ -94,7 +95,7 @@ BasicBlock::BasicBlock(const Instruction *psFirst, ControlFlowGraph &graph, cons
9495
void BasicBlock::Build()
9596
{
9697
const Instruction *inst = m_First;
97-
while (1)
98+
while (inst != m_End)
9899
{
99100
// Process sources first
100101
ForEachOperand(inst, inst + 1, FEO_FLAG_SRC_OPERAND | FEO_FLAG_SUBOPERAND,
@@ -158,7 +159,8 @@ void BasicBlock::Build()
158159
default:
159160
break;
160161
case OPCODE_RET:
161-
blockDone = true;
162+
// Continue processing, in the case of unreachable code we still need to translate it properly (case 1160309)
163+
// blockDone = true;
162164
break;
163165
case OPCODE_RETC:
164166
// Basic block is done, start a next one.
@@ -240,7 +242,7 @@ void BasicBlock::Build()
240242
m_Reachable = m_DEDef;
241243

242244
// Tag the end of the basic block
243-
m_Last = inst;
245+
m_Last = std::max(m_First, std::min(inst, m_End - 1));
244246
// printf("Basic Block %d -> %d\n", (int)m_First->id, (int)m_Last->id);
245247
}
246248

@@ -256,7 +258,7 @@ BasicBlock * BasicBlock::AddChildBasicBlock(const Instruction *psFirst)
256258
return b;
257259
}
258260
// Otherwise create one. Self-registering and self-connecting
259-
return new BasicBlock(psFirst, m_Graph, m_First);
261+
return new BasicBlock(psFirst, m_Graph, m_First, m_End);
260262
}
261263

262264
bool BasicBlock::RebuildReachable()
@@ -334,6 +336,7 @@ void BasicBlock::RVarUnion(ReachableVariables &a, const ReachableVariables &b)
334336
#if ENABLE_UNIT_TESTS
335337

336338
#define UNITY_EXTERNAL_TOOL 1
339+
#include "Projects/PrecompiledHeaders/UnityPrefix.h" // Needed for defines such as ENABLE_CPP_EXCEPTIONS
337340
#include "Testing.h" // From Runtime/Testing
338341

339342
UNIT_TEST_SUITE(HLSLcc)
@@ -348,7 +351,7 @@ UNIT_TEST_SUITE(HLSLcc)
348351
};
349352

350353
ControlFlowGraph cfg;
351-
const BasicBlock &root = cfg.Build(inst);
354+
const BasicBlock &root = cfg.Build(inst, inst + ARRAY_SIZE(inst));
352355

353356
CHECK_EQUAL(&inst[0], root.First());
354357
CHECK_EQUAL(&inst[1], root.Last());
@@ -403,7 +406,7 @@ UNIT_TEST_SUITE(HLSLcc)
403406
};
404407

405408
ControlFlowGraph cfg;
406-
const BasicBlock &root = cfg.Build(inst);
409+
const BasicBlock &root = cfg.Build(inst, inst + ARRAY_SIZE(inst));
407410

408411
CHECK_EQUAL(root.First(), &inst[0]);
409412
CHECK_EQUAL(root.Last(), &inst[2]);
@@ -539,7 +542,7 @@ UNIT_TEST_SUITE(HLSLcc)
539542
};
540543

541544
ControlFlowGraph cfg;
542-
const BasicBlock &root = cfg.Build(inst);
545+
const BasicBlock &root = cfg.Build(inst, inst + ARRAY_SIZE(inst));
543546

544547
CHECK_EQUAL(&inst[0], root.First());
545548
CHECK_EQUAL(&inst[4], root.Last());
@@ -699,7 +702,7 @@ UNIT_TEST_SUITE(HLSLcc)
699702
};
700703

701704
ControlFlowGraph cfg;
702-
const BasicBlock &root = cfg.Build(inst);
705+
const BasicBlock &root = cfg.Build(inst, inst + ARRAY_SIZE(inst));
703706

704707
CHECK_EQUAL(&inst[0], root.First());
705708
CHECK_EQUAL(&inst[2], root.Last());

src/DataTypeAnalysis.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,17 @@ void HLSLcc::DataTypeAnalysis::SetDataTypes(HLSLCrossCompilerContext* psContext,
430430

431431
case OPCODE_LD:
432432
case OPCODE_LD_MS:
433-
// TODO: Would need to know the sampler return type
434-
MarkOperandAs(&psInst->asOperands[0], SVT_FLOAT, aeTempVecType);
433+
{
434+
SHADER_VARIABLE_TYPE samplerReturnType = psInst->asOperands[2].aeDataType[0];
435+
MarkOperandAs(&psInst->asOperands[0], samplerReturnType, aeTempVecType);
435436
MarkOperandAs(&psInst->asOperands[1], SVT_UINT, aeTempVecType);
436437
break;
438+
}
437439

438440
case OPCODE_MOVC:
439441
MarkOperandAs(&psInst->asOperands[1], SVT_BOOL, aeTempVecType);
442+
break;
443+
440444
case OPCODE_SWAPC:
441445
MarkOperandAs(&psInst->asOperands[2], SVT_BOOL, aeTempVecType);
442446
break;
@@ -455,6 +459,7 @@ void HLSLcc::DataTypeAnalysis::SetDataTypes(HLSLCrossCompilerContext* psContext,
455459
MarkOperandAs(&psInst->asOperands[0], SVT_UINT, aeTempVecType);
456460
break;
457461
}
462+
break;
458463

459464
case OPCODE_SAMPLE_INFO:
460465
// Sample_info uses the same RESINFO_RETURN_TYPE for storage. 0 = float, 1 = uint.
@@ -594,28 +599,29 @@ void HLSLcc::DataTypeAnalysis::SetDataTypes(HLSLCrossCompilerContext* psContext,
594599
case OPCODE_DCL_RESOURCE_STRUCTURED:
595600
case OPCODE_SYNC:
596601
597-
// TODO
598-
case OPCODE_DADD:
599-
case OPCODE_DMAX:
600-
case OPCODE_DMIN:
601-
case OPCODE_DMUL:
602-
case OPCODE_DEQ:
603-
case OPCODE_DGE:
604-
case OPCODE_DLT:
605-
case OPCODE_DNE:
606-
case OPCODE_DMOV:
607-
case OPCODE_DMOVC:
608-
case OPCODE_DTOF:
609-
case OPCODE_FTOD:
610-
611602
case OPCODE_EVAL_SNAPPED:
612603
case OPCODE_EVAL_SAMPLE_INDEX:
613604
case OPCODE_EVAL_CENTROID:
614605
615606
case OPCODE_DCL_GS_INSTANCE_COUNT:
616607
617608
case OPCODE_ABORT:
618-
case OPCODE_DEBUG_BREAK:*/
609+
case OPCODE_DEBUG_BREAK:
610+
611+
// Double not supported
612+
case OPCODE_DADD:
613+
case OPCODE_DMAX:
614+
case OPCODE_DMIN:
615+
case OPCODE_DMUL:
616+
case OPCODE_DEQ:
617+
case OPCODE_DGE:
618+
case OPCODE_DLT:
619+
case OPCODE_DNE:
620+
case OPCODE_DMOV:
621+
case OPCODE_DMOVC:
622+
case OPCODE_DTOF:
623+
case OPCODE_FTOD:
624+
*/
619625

620626
default:
621627
break;

src/HLSLCrossCompilerContext.cpp

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "internal_includes/debug.h"
88
#include "internal_includes/Translator.h"
99
#include "internal_includes/ControlFlowGraph.h"
10+
#include "internal_includes/languages.h"
1011
#include "include/hlslcc.h"
1112
#include <sstream>
1213

@@ -49,8 +50,8 @@ void HLSLCrossCompilerContext::DoDataTypeAnalysis(ShaderPhase *psPhase)
4950

5051
CalculateStandaloneDefinitions(duChains, psPhase->ui32TotalTemps);
5152

52-
// Only do sampler precision downgrade on pixel shaders.
53-
if (psShader->eShaderType == PIXEL_SHADER)
53+
// Only do sampler precision downgrade with pixel shaders on mobile targets / Switch
54+
if (psShader->eShaderType == PIXEL_SHADER && (IsMobileTarget(this) || IsSwitch()))
5455
UpdateSamplerPrecisions(psShader->sInfo, duChains, psPhase->ui32TotalTemps);
5556

5657
UDSplitTemps(&psPhase->ui32TotalTemps, duChains, udChains, psPhase->pui32SplitInfo);
@@ -64,6 +65,55 @@ void HLSLCrossCompilerContext::DoDataTypeAnalysis(ShaderPhase *psPhase)
6465
psPhase->psTempDeclaration->value.ui32NumTemps = psPhase->ui32TotalTemps;
6566
}
6667

68+
void HLSLCrossCompilerContext::ReserveFramebufferFetchInputs()
69+
{
70+
if (psShader->eShaderType != PIXEL_SHADER)
71+
return;
72+
73+
if (!psShader->extensions->EXT_shader_framebuffer_fetch)
74+
return;
75+
76+
if ((flags & HLSLCC_FLAG_SHADER_FRAMEBUFFER_FETCH) == 0)
77+
return;
78+
79+
if (!(psShader->eTargetLanguage >= LANG_ES_300 && psShader->eTargetLanguage <= LANG_ES_LAST))
80+
return;
81+
82+
if (!psDependencies)
83+
return;
84+
85+
if (!HaveUniformBindingsAndLocations(psShader->eTargetLanguage, psShader->extensions, flags) &&
86+
((flags & HLSLCC_FLAG_FORCE_EXPLICIT_LOCATIONS) == 0 || (flags & HLSLCC_FLAG_COMBINE_TEXTURE_SAMPLERS) != 0))
87+
return;
88+
89+
// The Adreno GLSL compiler fails to compile shaders that use the same location for textures and inout attachments
90+
// So here we figure out the maximum index of any inout render target and then make sure that we never use those for textures.
91+
int maxInOutRenderTargetIndex = -1;
92+
for (const Declaration& decl : psShader->asPhases[0].psDecl)
93+
{
94+
if (decl.eOpcode != OPCODE_DCL_INPUT_PS)
95+
continue;
96+
97+
const Operand& operand = decl.asOperands[0];
98+
if (!operand.iPSInOut)
99+
continue;
100+
101+
const ShaderInfo::InOutSignature* signature = NULL;
102+
if (!psShader->sInfo.GetInputSignatureFromRegister(operand.ui32RegisterNumber, operand.ui32CompMask, &signature, true))
103+
continue;
104+
105+
const int index = signature->ui32SemanticIndex;
106+
if (index > maxInOutRenderTargetIndex)
107+
maxInOutRenderTargetIndex = index;
108+
}
109+
110+
if (maxInOutRenderTargetIndex >= 0)
111+
{
112+
if (maxInOutRenderTargetIndex >= psDependencies->m_NextAvailableGLSLResourceBinding[GLSLCrossDependencyData::BufferType_Texture])
113+
psDependencies->m_NextAvailableGLSLResourceBinding[GLSLCrossDependencyData::BufferType_Texture] = maxInOutRenderTargetIndex + 1;
114+
}
115+
}
116+
67117
void HLSLCrossCompilerContext::ClearDependencyData()
68118
{
69119
switch (psShader->eShaderType)

src/HLSLcc.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "hlslcc.h"
22

33
#include <memory>
4+
#include <sstream>
45
#include "internal_includes/HLSLCrossCompilerContext.h"
56
#include "internal_includes/toGLSL.h"
67
#include "internal_includes/toMetal.h"
@@ -27,6 +28,27 @@
2728
#define GL_COMPUTE_SHADER 0x91B9
2829
#endif
2930

31+
static bool CheckConstantBuffersNoDuplicateNames(const std::vector<ConstantBuffer>& buffers, HLSLccReflection& reflectionCallbacks)
32+
{
33+
uint32_t count = buffers.size();
34+
for (uint32_t i = 0; i < count; ++i)
35+
{
36+
const ConstantBuffer& lhs = buffers[i];
37+
for (uint32_t j = i + 1; j < count; ++j)
38+
{
39+
const ConstantBuffer& rhs = buffers[j];
40+
if (lhs.name == rhs.name)
41+
{
42+
std::ostringstream oss;
43+
oss << "Duplicate constant buffer declaration: " << lhs.name;
44+
reflectionCallbacks.OnDiagnostics(oss.str(), 0, true);
45+
return false;
46+
}
47+
}
48+
}
49+
50+
return true;
51+
}
3052

3153
HLSLCC_API int HLSLCC_APIENTRY TranslateHLSLFromMem(const char* shader,
3254
unsigned int flags,
@@ -49,6 +71,10 @@ HLSLCC_API int HLSLCC_APIENTRY TranslateHLSLFromMem(const char* shader,
4971

5072
if (psShader.get())
5173
{
74+
Shader* shader = psShader.get();
75+
if (!CheckConstantBuffersNoDuplicateNames(shader->sInfo.psConstantBuffers, reflectionCallbacks))
76+
return 0;
77+
5278
HLSLCrossCompilerContext sContext(reflectionCallbacks);
5379

5480
// Add shader precisions from the list
@@ -59,7 +85,11 @@ HLSLCC_API int HLSLCC_APIENTRY TranslateHLSLFromMem(const char* shader,
5985
flags &= ~HLSLCC_FLAG_COMBINE_TEXTURE_SAMPLERS;
6086
}
6187

62-
sContext.psShader = psShader.get();
88+
#ifdef _DEBUG
89+
flags |= HLSLCC_FLAG_INCLUDE_INSTRUCTIONS_COMMENTS;
90+
#endif
91+
92+
sContext.psShader = shader;
6393
sContext.flags = flags;
6494

6595
// If dependencies == NULL, we'll create a dummy object for it so that there's always something there.
@@ -68,6 +98,7 @@ HLSLCC_API int HLSLCC_APIENTRY TranslateHLSLFromMem(const char* shader,
6898
{
6999
depPtr.reset(new GLSLCrossDependencyData());
70100
sContext.psDependencies = depPtr.get();
101+
sContext.psDependencies->SetupGLSLResourceBindingSlotsIndices();
71102
}
72103
else
73104
sContext.psDependencies = dependencies;

0 commit comments

Comments
 (0)